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/build/re_type_definitions/rerun/archetypes/tensor.def.rs".
3
4#pragma once
5
6#include "../archetype_utils.hpp"
7#include "../collection.hpp"
8#include "../component_batch.hpp"
9#include "../component_column.hpp"
10#include "../components/tensor_data.hpp"
11#include "../components/value_range.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::encodings::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(int argc, char* argv[]) {
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(), [&] {
48 /// return static_cast<uint8_t>(dist(gen));
49 /// });
50 ///
51 /// rec.log(
52 /// "tensor",
53 /// rerun::Tensor({8, 6, 3, 5}, data)
54 /// .with_dim_names({"width", "height", "channel", "batch"})
55 /// );
56 /// }
57 /// ```
58 struct Tensor {
59 /// The tensor data
60 std::optional<ComponentBatch> data;
61
62 /// The expected range of values.
63 ///
64 /// This is typically the expected range of valid values.
65 /// Everything outside of the range is clamped to the range for the purpose of colormpaping.
66 /// Any colormap applied for display, will map this range.
67 ///
68 /// If not specified, the range will be automatically estimated from the data.
69 /// Note that the Viewer may try to guess a wider range than the minimum/maximum of values
70 /// in the contents of the tensor.
71 /// E.g. if all values are positive, some bigger than 1.0 and all smaller than 255.0,
72 /// the Viewer will guess that the data likely came from an 8bit image, thus assuming a range of 0-255.
73 std::optional<ComponentBatch> value_range;
74
75 public:
76 /// The name of the archetype as used in `ComponentDescriptor`s.
77 static constexpr const char ArchetypeName[] = "rerun.archetypes.Tensor";
78
79 /// `ComponentDescriptor` for the `data` field.
80 static constexpr auto Descriptor_data = ComponentDescriptor(
82 );
83 /// `ComponentDescriptor` for the `value_range` field.
85 ArchetypeName, "Tensor:value_range",
87 );
88
89 public: // START of extensions from tensor_ext.cpp:
90 RR_DISABLE_MAYBE_UNINITIALIZED_PUSH
91
92 /// New Tensor from dimensions and tensor buffer.
94 : Tensor(encodings::TensorData(std::move(shape), std::move(buffer))) {}
95
96 RR_DISABLE_MAYBE_UNINITIALIZED_POP
97
98 /// New tensor from dimensions and pointer to tensor data.
99 ///
100 /// Type must be one of the types supported by `rerun::encodings::TensorData`.
101 /// \param shape
102 /// Shape of the image. Determines the number of elements expected to be in `data`.
103 /// \param data_
104 /// Target of the pointer must outlive the archetype.
105 template <typename TElement>
106 explicit Tensor(Collection<uint64_t> shape, const TElement* data_)
107 : Tensor(encodings::TensorData(std::move(shape), data_)) {}
108
109 /// Update the `names` of the contained `TensorData` dimensions.
110 ///
111 /// Any existing Dimension names will be overwritten.
112 ///
113 /// If too many, or too few names are provided, this function will call
114 /// Error::handle and then proceed to only update the subset of names that it can.
116
117 // END of extensions from tensor_ext.cpp, start of generated code:
118
119 public:
120 Tensor() = default;
121 Tensor(Tensor&& other) = default;
122 Tensor(const Tensor& other) = default;
123 Tensor& operator=(const Tensor& other) = default;
124 Tensor& operator=(Tensor&& other) = default;
125
127 : data(ComponentBatch::from_loggable(std::move(_data), Descriptor_data).value_or_throw()
128 ) {}
129
130 /// Update only some specific fields of a `Tensor`.
132 return Tensor();
133 }
134
135 /// Clear all the fields of a `Tensor`.
137
138 /// The tensor data
140 data = ComponentBatch::from_loggable(_data, Descriptor_data).value_or_throw();
141 return std::move(*this);
142 }
143
144 /// This method makes it possible to pack multiple `data` in a single component batch.
145 ///
146 /// This only makes sense when used in conjunction with `columns`. `with_data` should
147 /// be used when logging a single row's worth of data.
149 data = ComponentBatch::from_loggable(_data, Descriptor_data).value_or_throw();
150 return std::move(*this);
151 }
152
153 /// The expected range of values.
154 ///
155 /// This is typically the expected range of valid values.
156 /// Everything outside of the range is clamped to the range for the purpose of colormpaping.
157 /// Any colormap applied for display, will map this range.
158 ///
159 /// If not specified, the range will be automatically estimated from the data.
160 /// Note that the Viewer may try to guess a wider range than the minimum/maximum of values
161 /// in the contents of the tensor.
162 /// E.g. if all values are positive, some bigger than 1.0 and all smaller than 255.0,
163 /// the Viewer will guess that the data likely came from an 8bit image, thus assuming a range of 0-255.
166 .value_or_throw();
167 return std::move(*this);
168 }
169
170 /// This method makes it possible to pack multiple `value_range` in a single component batch.
171 ///
172 /// This only makes sense when used in conjunction with `columns`. `with_value_range` should
173 /// be used when logging a single row's worth of data.
175 ) && {
177 .value_or_throw();
178 return std::move(*this);
179 }
180
181 /// Partitions the component data into multiple sub-batches.
182 ///
183 /// Specifically, this transforms the existing `ComponentBatch` data into `ComponentColumn`s
184 /// instead, via `ComponentBatch::partitioned`.
185 ///
186 /// This makes it possible to use `RecordingStream::send_columns` to send columnar data directly into Rerun.
187 ///
188 /// The specified `lengths` must sum to the total length of the component batch.
190 return detail::partition_batches({&data, &value_range}, lengths_).value_or_throw();
191 }
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 return detail::partition_batches_of_unit_length({&data, &value_range}).value_or_throw();
199 }
200 };
201
202} // namespace rerun::archetypes
203
204namespace rerun {
205 /// \private
206 template <typename T>
207 struct AsComponents;
208
209 /// \private
210 template <>
211 struct AsComponents<archetypes::Tensor> {
212 /// Serialize all set component batches.
213 static Result<Collection<ComponentBatch>> as_batches(const archetypes::Tensor& archetype) {
214 return detail::collect_batches({&archetype.data, &archetype.value_range});
215 }
216 };
217} // namespace rerun
Generic collection of elements that are roughly contiguous in memory.
Definition collection.hpp:49
All built-in archetypes. See Types in the Rerun manual.
Definition rerun.hpp:90
All Rerun C++ types and functions are in the rerun namespace or one of its nested namespaces.
Definition rerun.hpp:26
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)
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:16
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:58
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:174
static constexpr auto Descriptor_value_range
ComponentDescriptor for the value_range field.
Definition tensor.hpp:84
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:106
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:148
static Tensor update_fields()
Update only some specific fields of a Tensor.
Definition tensor.hpp:131
RR_DISABLE_MAYBE_UNINITIALIZED_PUSH Tensor(Collection< uint64_t > shape, encodings::TensorBuffer buffer)
New Tensor from dimensions and tensor buffer.
Definition tensor.hpp:93
Tensor with_data(const rerun::components::TensorData &_data) &&
The tensor data.
Definition tensor.hpp:139
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.
Definition tensor.hpp:189
Collection< ComponentColumn > columns()
Partitions the component data into unit-length sub-batches.
Definition tensor.hpp:197
Tensor with_value_range(const rerun::components::ValueRange &_value_range) &&
The expected range of values.
Definition tensor.hpp:164
std::optional< ComponentBatch > value_range
The expected range of values.
Definition tensor.hpp:73
std::optional< ComponentBatch > data
The tensor data.
Definition tensor.hpp:60
static constexpr const char ArchetypeName[]
The name of the archetype as used in ComponentDescriptors.
Definition tensor.hpp:77
static constexpr auto Descriptor_data
ComponentDescriptor for the data field.
Definition tensor.hpp:80
Component: An N-dimensional array of numbers.
Definition tensor_data.hpp:22
Component: Range of expected or valid values, specifying a lower and upper bound.
Definition value_range.hpp:18
Encoding: The underlying storage for archetypes::Tensor.
Definition tensor_buffer.hpp:98
Encoding: An N-dimensional array of numbers.
Definition tensor_data.hpp:30