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