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