Rerun C++ SDK
Loading...
Searching...
No Matches
tensor.hpp
1// DO NOT EDIT! This file was auto-generated by crates/build/re_types_builder/src/codegen/cpp/mod.rs
2// Based on "crates/store/re_types/definitions/rerun/archetypes/tensor.fbs".
3
4#pragma once
5
6#include "../collection.hpp"
7#include "../component_batch.hpp"
8#include "../component_column.hpp"
9#include "../components/tensor_data.hpp"
10#include "../components/value_range.hpp"
11#include "../indicator_component.hpp"
12#include "../result.hpp"
13
14#include <cstdint>
15#include <optional>
16#include <utility>
17#include <vector>
18
19namespace rerun::archetypes {
20 /// **Archetype**: An N-dimensional array of numbers.
21 ///
22 /// Since the underlying `rerun::datatypes::TensorData` uses `rerun::Collection` internally,
23 /// data can be passed in without a copy from raw pointers or by reference from `std::vector`/`std::array`/c-arrays.
24 /// If needed, this "borrow-behavior" can be extended by defining your own `rerun::CollectionAdapter`.
25 ///
26 /// ## Example
27 ///
28 /// ### Simple tensor
29 /// ![image](https://static.rerun.io/tensor_simple/baacb07712f7b706e3c80e696f70616c6c20b367/full.png)
30 ///
31 /// ```cpp
32 /// #include <rerun.hpp>
33 ///
34 /// #include <algorithm> // std::generate
35 /// #include <random>
36 /// #include <vector>
37 ///
38 /// int main() {
39 /// const auto rec = rerun::RecordingStream("rerun_example_tensor");
40 /// rec.spawn().exit_on_failure();
41 ///
42 /// std::default_random_engine gen;
43 /// // On MSVC uint8_t distributions are not supported.
44 /// std::uniform_int_distribution<int> dist(0, 255);
45 ///
46 /// std::vector<uint8_t> data(8 * 6 * 3 * 5);
47 /// std::generate(data.begin(), data.end(), [&] { return static_cast<uint8_t>(dist(gen)); });
48 ///
49 /// rec.log(
50 /// "tensor",
51 /// rerun::Tensor({8, 6, 3, 5}, data).with_dim_names({"width", "height", "channel", "batch"})
52 /// );
53 /// }
54 /// ```
55 struct Tensor {
56 /// The tensor data
57 std::optional<ComponentBatch> data;
58
59 /// The expected range of values.
60 ///
61 /// This is typically the expected range of valid values.
62 /// Everything outside of the range is clamped to the range for the purpose of colormpaping.
63 /// Any colormap applied for display, will map this range.
64 ///
65 /// If not specified, the range will be automatically estimated from the data.
66 /// Note that the Viewer may try to guess a wider range than the minimum/maximum of values
67 /// in the contents of the tensor.
68 /// E.g. if all values are positive, some bigger than 1.0 and all smaller than 255.0,
69 /// the Viewer will guess that the data likely came from an 8bit image, thus assuming a range of 0-255.
70 std::optional<ComponentBatch> value_range;
71
72 public:
73 static constexpr const char IndicatorComponentName[] = "rerun.components.TensorIndicator";
74
75 /// Indicator component, used to identify the archetype when converting to a list of components.
77 /// The name of the archetype as used in `ComponentDescriptor`s.
78 static constexpr const char ArchetypeName[] = "rerun.archetypes.Tensor";
79
80 /// `ComponentDescriptor` for the `data` field.
81 static constexpr auto Descriptor_data = ComponentDescriptor(
82 ArchetypeName, "data",
84 );
85 /// `ComponentDescriptor` for the `value_range` field.
87 ArchetypeName, "value_range",
89 );
90
91 public: // START of extensions from tensor_ext.cpp:
92 RR_DISABLE_MAYBE_UNINITIALIZED_PUSH
93
94 /// New Tensor from dimensions and tensor buffer.
96 : Tensor(datatypes::TensorData(std::move(shape), std::move(buffer))) {}
97
98 RR_DISABLE_MAYBE_UNINITIALIZED_POP
99
100 /// New tensor from dimensions and pointer to tensor data.
101 ///
102 /// Type must be one of the types supported by `rerun::datatypes::TensorData`.
103 /// \param shape
104 /// Shape of the image. Determines the number of elements expected to be in `data`.
105 /// \param data_
106 /// Target of the pointer must outlive the archetype.
107 template <typename TElement>
108 explicit Tensor(Collection<uint64_t> shape, const TElement* data_)
109 : Tensor(datatypes::TensorData(std::move(shape), data_)) {}
110
111 /// Update the `names` of the contained `TensorData` dimensions.
112 ///
113 /// Any existing Dimension names will be overwritten.
114 ///
115 /// If too many, or too few names are provided, this function will call
116 /// Error::handle and then proceed to only update the subset of names that it can.
118
119 // END of extensions from tensor_ext.cpp, start of generated code:
120
121 public:
122 Tensor() = default;
123 Tensor(Tensor&& other) = default;
124 Tensor(const Tensor& other) = default;
125 Tensor& operator=(const Tensor& other) = default;
126 Tensor& operator=(Tensor&& other) = default;
127
129 : data(ComponentBatch::from_loggable(std::move(_data), Descriptor_data).value_or_throw()
130 ) {}
131
132 /// Update only some specific fields of a `Tensor`.
134 return Tensor();
135 }
136
137 /// Clear all the fields of a `Tensor`.
139
140 /// The tensor data
142 data = ComponentBatch::from_loggable(_data, Descriptor_data).value_or_throw();
143 return std::move(*this);
144 }
145
146 /// This method makes it possible to pack multiple `data` in a single component batch.
147 ///
148 /// This only makes sense when used in conjunction with `columns`. `with_data` should
149 /// be used when logging a single row's worth of data.
151 data = ComponentBatch::from_loggable(_data, Descriptor_data).value_or_throw();
152 return std::move(*this);
153 }
154
155 /// The expected range of values.
156 ///
157 /// This is typically the expected range of valid values.
158 /// Everything outside of the range is clamped to the range for the purpose of colormpaping.
159 /// Any colormap applied for display, will map this range.
160 ///
161 /// If not specified, the range will be automatically estimated from the data.
162 /// Note that the Viewer may try to guess a wider range than the minimum/maximum of values
163 /// in the contents of the tensor.
164 /// E.g. if all values are positive, some bigger than 1.0 and all smaller than 255.0,
165 /// the Viewer will guess that the data likely came from an 8bit image, thus assuming a range of 0-255.
168 .value_or_throw();
169 return std::move(*this);
170 }
171
172 /// This method makes it possible to pack multiple `value_range` in a single component batch.
173 ///
174 /// This only makes sense when used in conjunction with `columns`. `with_value_range` should
175 /// be used when logging a single row's worth of data.
177 ) && {
179 .value_or_throw();
180 return std::move(*this);
181 }
182
183 /// Partitions the component data into multiple sub-batches.
184 ///
185 /// Specifically, this transforms the existing `ComponentBatch` data into `ComponentColumn`s
186 /// instead, via `ComponentBatch::partitioned`.
187 ///
188 /// This makes it possible to use `RecordingStream::send_columns` to send columnar data directly into Rerun.
189 ///
190 /// The specified `lengths` must sum to the total length of the component batch.
192
193 /// Partitions the component data into unit-length sub-batches.
194 ///
195 /// This is semantically similar to calling `columns` with `std::vector<uint32_t>(n, 1)`,
196 /// where `n` is automatically guessed.
198 };
199
200} // namespace rerun::archetypes
201
202namespace rerun {
203 /// \private
204 template <typename T>
205 struct AsComponents;
206
207 /// \private
208 template <>
209 struct AsComponents<archetypes::Tensor> {
210 /// Serialize all set component batches.
211 static Result<Collection<ComponentBatch>> as_batches(const archetypes::Tensor& archetype);
212 };
213} // namespace rerun
Generic collection of elements that are roughly contiguous in memory.
Definition collection.hpp:49
A class for representing either a usable value, or an error.
Definition result.hpp:14
All built-in archetypes. See Types in the Rerun manual.
Definition rerun.hpp:76
All Rerun C++ types and functions are in the rerun namespace or one of its nested namespaces.
Definition rerun.hpp:23
Arrow-encoded data of a single batch of components together with a component descriptor.
Definition component_batch.hpp:28
static Result< ComponentBatch > from_loggable(const rerun::Collection< T > &components, const ComponentDescriptor &descriptor=rerun::Loggable< T >::Descriptor)
Creates a new component batch from a collection of component instances.
Definition component_batch.hpp:46
A ComponentDescriptor fully describes the semantics of a column of data.
Definition component_descriptor.hpp:14
The Loggable trait is used by all built-in implementation of rerun::AsComponents to serialize a colle...
Definition loggable.hpp:11
Archetype: An N-dimensional array of numbers.
Definition tensor.hpp:55
RR_DISABLE_MAYBE_UNINITIALIZED_PUSH Tensor(Collection< uint64_t > shape, datatypes::TensorBuffer buffer)
New Tensor from dimensions and tensor buffer.
Definition tensor.hpp:95
Tensor with_many_value_range(const Collection< rerun::components::ValueRange > &_value_range) &&
This method makes it possible to pack multiple value_range in a single component batch.
Definition tensor.hpp:176
static constexpr auto Descriptor_value_range
ComponentDescriptor for the value_range field.
Definition tensor.hpp:86
static Tensor clear_fields()
Clear all the fields of a Tensor.
RR_DISABLE_MAYBE_UNINITIALIZED_POP Tensor(Collection< uint64_t > shape, const TElement *data_)
New tensor from dimensions and pointer to tensor data.
Definition tensor.hpp:108
Tensor with_many_data(const Collection< rerun::components::TensorData > &_data) &&
This method makes it possible to pack multiple data in a single component batch.
Definition tensor.hpp:150
static Tensor update_fields()
Update only some specific fields of a Tensor.
Definition tensor.hpp:133
Tensor with_data(const rerun::components::TensorData &_data) &&
The tensor data.
Definition tensor.hpp:141
Tensor with_dim_names(Collection< std::string > names) &&
Update the names of the contained TensorData dimensions.
Collection< ComponentColumn > columns(const Collection< uint32_t > &lengths_)
Partitions the component data into multiple sub-batches.
Collection< ComponentColumn > columns()
Partitions the component data into unit-length sub-batches.
Tensor with_value_range(const rerun::components::ValueRange &_value_range) &&
The expected range of values.
Definition tensor.hpp:166
std::optional< ComponentBatch > value_range
The expected range of values.
Definition tensor.hpp:70
std::optional< ComponentBatch > data
The tensor data.
Definition tensor.hpp:57
static constexpr const char ArchetypeName[]
The name of the archetype as used in ComponentDescriptors.
Definition tensor.hpp:78
static constexpr auto Descriptor_data
ComponentDescriptor for the data field.
Definition tensor.hpp:81
Indicator component used by archetypes when converting them to component lists.
Definition indicator_component.hpp:32
Component: An N-dimensional array of numbers.
Definition tensor_data.hpp:23
Component: Range of expected or valid values, specifying a lower and upper bound.
Definition value_range.hpp:16
Datatype: The underlying storage for archetypes::Tensor.
Definition tensor_buffer.hpp:99
Datatype: An N-dimensional array of numbers.
Definition tensor_data.hpp:31