Rerun C++ SDK
Loading...
Searching...
No Matches
segmentation_image.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/segmentation_image.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/draw_order.hpp"
11#include "../components/image_buffer.hpp"
12#include "../components/image_format.hpp"
13#include "../components/opacity.hpp"
14#include "../image_utils.hpp"
15#include "../result.hpp"
16
17#include <cstdint>
18#include <optional>
19#include <utility>
20#include <vector>
21
22namespace rerun::archetypes {
23 /// **Archetype**: An image made up of integer `components::ClassId`s.
24 ///
25 /// Each pixel corresponds to a `components::ClassId` that will be mapped to a color based on `archetypes::AnnotationContext`.
26 ///
27 /// In the case of floating point images, the label will be looked up based on rounding to the nearest
28 /// integer value.
29 ///
30 /// Use `archetypes::AnnotationContext` to associate each class with a color and a label.
31 ///
32 /// Since the underlying `rerun::encodings::TensorData` uses `rerun::Collection` internally,
33 /// data can be passed in without a copy from raw pointers or by reference from `std::vector`/`std::array`/c-arrays.
34 /// If needed, this "borrow-behavior" can be extended by defining your own `rerun::CollectionAdapter`.
35 ///
36 /// ## Example
37 ///
38 /// ### Simple segmentation image
39 /// ![image](https://static.rerun.io/segmentation_image_simple/f8aac62abcf4c59c5d62f9ebc2d86fd0285c1736/full.png)
40 ///
41 /// ```cpp
42 /// #include <rerun.hpp>
43 ///
44 /// #include <algorithm> // std::fill_n
45 /// #include <vector>
46 ///
47 /// int main(int argc, char* argv[]) {
48 /// const auto rec = rerun::RecordingStream("rerun_example_segmentation_image");
49 /// rec.spawn().exit_on_failure();
50 ///
51 /// // Create a segmentation image
52 /// const int HEIGHT = 8;
53 /// const int WIDTH = 12;
54 /// std::vector<uint8_t> data(WIDTH * HEIGHT, 0);
55 /// for (auto y = 0; y <4; ++y) { // top half
56 /// // left half:
57 /// std::fill_n(data.begin() + y * WIDTH, 6, static_cast<uint8_t>(1));
58 /// }
59 /// for (auto y = 4; y <8; ++y) { // bottom half
60 /// // right half:
61 /// std::fill_n(data.begin() + y * WIDTH + 6, 6, static_cast<uint8_t>(2));
62 /// }
63 ///
64 /// // create an annotation context to describe the classes
65 /// rec.log_static(
66 /// "/",
67 /// rerun::AnnotationContext({
68 /// rerun::AnnotationInfo(1, "red", rerun::Rgba32(255, 0, 0)),
69 /// rerun::AnnotationInfo(2, "green", rerun::Rgba32(0, 255, 0)),
70 /// })
71 /// );
72 ///
73 /// rec.log("image", rerun::SegmentationImage(data.data(), {WIDTH, HEIGHT}));
74 /// }
75 /// ```
77 /// The raw image data.
78 std::optional<ComponentBatch> buffer;
79
80 /// The format of the image.
81 std::optional<ComponentBatch> format;
82
83 /// Opacity of the image, useful for layering the segmentation image on top of another image.
84 ///
85 /// Defaults to 0.5 if there's any other images in the scene, otherwise 1.0.
86 std::optional<ComponentBatch> opacity;
87
88 /// An optional floating point value that specifies the 2D drawing order.
89 ///
90 /// Objects with higher values are drawn on top of those with lower values.
91 /// Defaults to `0.0`.
92 std::optional<ComponentBatch> draw_order;
93
94 public:
95 /// The name of the archetype as used in `ComponentDescriptor`s.
96 static constexpr const char ArchetypeName[] = "rerun.archetypes.SegmentationImage";
97
98 /// `ComponentDescriptor` for the `buffer` field.
99 static constexpr auto Descriptor_buffer = ComponentDescriptor(
100 ArchetypeName, "SegmentationImage:buffer",
102 );
103 /// `ComponentDescriptor` for the `format` field.
104 static constexpr auto Descriptor_format = ComponentDescriptor(
105 ArchetypeName, "SegmentationImage:format",
107 );
108 /// `ComponentDescriptor` for the `opacity` field.
110 ArchetypeName, "SegmentationImage:opacity",
112 );
113 /// `ComponentDescriptor` for the `draw_order` field.
115 ArchetypeName, "SegmentationImage:draw_order",
117 );
118
119 public: // START of extensions from segmentation_image_ext.cpp:
120 /// Constructs image from pointer + resolution, inferring the datatype from the pointer type.
121 ///
122 /// @param pixels The raw image data.
123 /// ⚠️ Does not take ownership of the data, the caller must ensure the data outlives the image.
124 /// The number of elements is assumed to be `W * H`.
125 /// @param resolution The resolution of the image as {width, height}.
126 template <typename TElement>
127 SegmentationImage(const TElement* pixels, WidthHeight resolution)
129 reinterpret_cast<const uint8_t*>(pixels), resolution, get_datatype(pixels)
130 } {}
131
132 /// Constructs image from pixel data + resolution with datatype inferred from the passed collection.
133 ///
134 /// @param pixels The raw image data.
135 /// If the data does not outlive the image, use `std::move` or create the `rerun::Collection`
136 /// explicitly ahead of time with `rerun::Collection::take_ownership`.
137 /// The length of the data should be `W * H`.
138 /// @param resolution The resolution of the image as {width, height}.
139 template <typename TElement>
141 : SegmentationImage{pixels.to_uint8(), resolution, get_datatype(pixels.data())} {}
142
143 /// Constructs image from pixel data + resolution with explicit datatype. Borrows data from a pointer (i.e. data must outlive the image!).
144 ///
145 /// @param bytes The raw image data.
146 /// ⚠️ Does not take ownership of the data, the caller must ensure the data outlives the image.
147 /// The byte size of the data is assumed to be `W * H * datatype.size`
148 /// @param resolution The resolution of the image as {width, height}.
149 /// @param datatype How the data should be interpreted.
151 const void* bytes, WidthHeight resolution, encodings::ChannelDatatype datatype
152 )
154 Collection<uint8_t>::borrow(bytes, num_bytes(resolution, datatype)),
155 resolution,
156 datatype
157 } {}
158
159 /// Constructs image from pixel data + resolution + datatype.
160 ///
161 /// @param bytes The raw image data as bytes.
162 /// If the data does not outlive the image, use `std::move` or create the `rerun::Collection`
163 /// explicitly ahead of time with `rerun::Collection::take_ownership`.
164 /// The length of the data should be `W * H`.
165 /// @param resolution The resolution of the image as {width, height}.
166 /// @param datatype How the data should be interpreted.
169 ) {
170 auto image_format = encodings::ImageFormat{resolution, datatype};
171 if (bytes.size() != image_format.num_bytes()) {
172 Error(
173 ErrorCode::InvalidTensorDimension,
174 "SegmentationImage buffer has the wrong size. Got " +
175 std::to_string(bytes.size()) + " bytes, expected " +
176 std::to_string(image_format.num_bytes())
177 )
178 .handle();
179 }
180 *this = std::move(*this).with_buffer(bytes).with_format(image_format);
181 }
182
183 // END of extensions from segmentation_image_ext.cpp, start of generated code:
184
185 public:
186 SegmentationImage() = default;
187 SegmentationImage(SegmentationImage&& other) = default;
188 SegmentationImage(const SegmentationImage& other) = default;
189 SegmentationImage& operator=(const SegmentationImage& other) = default;
190 SegmentationImage& operator=(SegmentationImage&& other) = default;
191
192 /// Update only some specific fields of a `SegmentationImage`.
194 return SegmentationImage();
195 }
196
197 /// Clear all the fields of a `SegmentationImage`.
199
200 /// The raw image data.
202 buffer = ComponentBatch::from_loggable(_buffer, Descriptor_buffer).value_or_throw();
203 return std::move(*this);
204 }
205
206 /// This method makes it possible to pack multiple `buffer` in a single component batch.
207 ///
208 /// This only makes sense when used in conjunction with `columns`. `with_buffer` should
209 /// be used when logging a single row's worth of data.
211 ) && {
212 buffer = ComponentBatch::from_loggable(_buffer, Descriptor_buffer).value_or_throw();
213 return std::move(*this);
214 }
215
216 /// The format of the image.
218 format = ComponentBatch::from_loggable(_format, Descriptor_format).value_or_throw();
219 return std::move(*this);
220 }
221
222 /// This method makes it possible to pack multiple `format` in a single component batch.
223 ///
224 /// This only makes sense when used in conjunction with `columns`. `with_format` should
225 /// be used when logging a single row's worth of data.
227 ) && {
228 format = ComponentBatch::from_loggable(_format, Descriptor_format).value_or_throw();
229 return std::move(*this);
230 }
231
232 /// Opacity of the image, useful for layering the segmentation image on top of another image.
233 ///
234 /// Defaults to 0.5 if there's any other images in the scene, otherwise 1.0.
236 opacity = ComponentBatch::from_loggable(_opacity, Descriptor_opacity).value_or_throw();
237 return std::move(*this);
238 }
239
240 /// This method makes it possible to pack multiple `opacity` in a single component batch.
241 ///
242 /// This only makes sense when used in conjunction with `columns`. `with_opacity` should
243 /// be used when logging a single row's worth of data.
245 ) && {
246 opacity = ComponentBatch::from_loggable(_opacity, Descriptor_opacity).value_or_throw();
247 return std::move(*this);
248 }
249
250 /// An optional floating point value that specifies the 2D drawing order.
251 ///
252 /// Objects with higher values are drawn on top of those with lower values.
253 /// Defaults to `0.0`.
255 draw_order =
256 ComponentBatch::from_loggable(_draw_order, Descriptor_draw_order).value_or_throw();
257 return std::move(*this);
258 }
259
260 /// This method makes it possible to pack multiple `draw_order` in a single component batch.
261 ///
262 /// This only makes sense when used in conjunction with `columns`. `with_draw_order` should
263 /// be used when logging a single row's worth of data.
266 ) && {
267 draw_order =
268 ComponentBatch::from_loggable(_draw_order, Descriptor_draw_order).value_or_throw();
269 return std::move(*this);
270 }
271
272 /// Partitions the component data into multiple sub-batches.
273 ///
274 /// Specifically, this transforms the existing `ComponentBatch` data into `ComponentColumn`s
275 /// instead, via `ComponentBatch::partitioned`.
276 ///
277 /// This makes it possible to use `RecordingStream::send_columns` to send columnar data directly into Rerun.
278 ///
279 /// The specified `lengths` must sum to the total length of the component batch.
281 return detail::partition_batches({&buffer, &format, &opacity, &draw_order}, lengths_)
282 .value_or_throw();
283 }
284
285 /// Partitions the component data into unit-length sub-batches.
286 ///
287 /// This is semantically similar to calling `columns` with `std::vector<uint32_t>(n, 1)`,
288 /// where `n` is automatically guessed.
290 return detail::partition_batches_of_unit_length({&buffer, &format, &opacity, &draw_order
291 })
292 .value_or_throw();
293 }
294 };
295
296} // namespace rerun::archetypes
297
298namespace rerun {
299 /// \private
300 template <typename T>
301 struct AsComponents;
302
303 /// \private
304 template <>
305 struct AsComponents<archetypes::SegmentationImage> {
306 /// Serialize all set component batches.
307 static Result<Collection<ComponentBatch>> as_batches(
308 const archetypes::SegmentationImage& archetype
309 ) {
310 return detail::collect_batches(
311 {&archetype.buffer, &archetype.format, &archetype.opacity, &archetype.draw_order}
312 );
313 }
314 };
315} // namespace rerun
Generic collection of elements that are roughly contiguous in memory.
Definition collection.hpp:49
size_t size() const
Returns the number of instances in this collection.
Definition collection.hpp:295
Status outcome object (success or error) returned for fallible operations.
Definition error.hpp:104
void handle() const
Handle this error based on the set log handler.
All built-in archetypes. See Types in the Rerun manual.
Definition rerun.hpp:90
ChannelDatatype
Encoding: The innermost datatype of an image.
Definition channel_datatype.hpp:26
All Rerun C++ types and functions are in the rerun namespace or one of its nested namespaces.
Definition rerun.hpp:26
Collection< TElement > borrow(const TElement *data, size_t num_instances=1)
Borrows binary data into a Collection from a pointer.
Definition collection.hpp:453
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
The width and height of an image.
Definition image_utils.hpp:13
Archetype: An image made up of integer components::ClassIds.
Definition segmentation_image.hpp:76
SegmentationImage with_many_buffer(const Collection< rerun::components::ImageBuffer > &_buffer) &&
This method makes it possible to pack multiple buffer in a single component batch.
Definition segmentation_image.hpp:210
Collection< ComponentColumn > columns()
Partitions the component data into unit-length sub-batches.
Definition segmentation_image.hpp:289
std::optional< ComponentBatch > opacity
Opacity of the image, useful for layering the segmentation image on top of another image.
Definition segmentation_image.hpp:86
SegmentationImage with_buffer(const rerun::components::ImageBuffer &_buffer) &&
The raw image data.
Definition segmentation_image.hpp:201
SegmentationImage with_many_format(const Collection< rerun::components::ImageFormat > &_format) &&
This method makes it possible to pack multiple format in a single component batch.
Definition segmentation_image.hpp:226
Collection< ComponentColumn > columns(const Collection< uint32_t > &lengths_)
Partitions the component data into multiple sub-batches.
Definition segmentation_image.hpp:280
SegmentationImage(const void *bytes, WidthHeight resolution, encodings::ChannelDatatype datatype)
Constructs image from pixel data + resolution with explicit datatype.
Definition segmentation_image.hpp:150
std::optional< ComponentBatch > buffer
The raw image data.
Definition segmentation_image.hpp:78
SegmentationImage with_draw_order(const rerun::components::DrawOrder &_draw_order) &&
An optional floating point value that specifies the 2D drawing order.
Definition segmentation_image.hpp:254
static SegmentationImage clear_fields()
Clear all the fields of a SegmentationImage.
std::optional< ComponentBatch > draw_order
An optional floating point value that specifies the 2D drawing order.
Definition segmentation_image.hpp:92
static constexpr auto Descriptor_buffer
ComponentDescriptor for the buffer field.
Definition segmentation_image.hpp:99
SegmentationImage(Collection< TElement > pixels, WidthHeight resolution)
Constructs image from pixel data + resolution with datatype inferred from the passed collection.
Definition segmentation_image.hpp:140
static constexpr auto Descriptor_format
ComponentDescriptor for the format field.
Definition segmentation_image.hpp:104
static constexpr auto Descriptor_opacity
ComponentDescriptor for the opacity field.
Definition segmentation_image.hpp:109
SegmentationImage(const TElement *pixels, WidthHeight resolution)
Constructs image from pointer + resolution, inferring the datatype from the pointer type.
Definition segmentation_image.hpp:127
SegmentationImage with_many_opacity(const Collection< rerun::components::Opacity > &_opacity) &&
This method makes it possible to pack multiple opacity in a single component batch.
Definition segmentation_image.hpp:244
std::optional< ComponentBatch > format
The format of the image.
Definition segmentation_image.hpp:81
SegmentationImage with_many_draw_order(const Collection< rerun::components::DrawOrder > &_draw_order) &&
This method makes it possible to pack multiple draw_order in a single component batch.
Definition segmentation_image.hpp:264
static constexpr auto Descriptor_draw_order
ComponentDescriptor for the draw_order field.
Definition segmentation_image.hpp:114
static constexpr const char ArchetypeName[]
The name of the archetype as used in ComponentDescriptors.
Definition segmentation_image.hpp:96
SegmentationImage(Collection< uint8_t > bytes, WidthHeight resolution, encodings::ChannelDatatype datatype)
Constructs image from pixel data + resolution + datatype.
Definition segmentation_image.hpp:167
static SegmentationImage update_fields()
Update only some specific fields of a SegmentationImage.
Definition segmentation_image.hpp:193
SegmentationImage with_opacity(const rerun::components::Opacity &_opacity) &&
Opacity of the image, useful for layering the segmentation image on top of another image.
Definition segmentation_image.hpp:235
SegmentationImage with_format(const rerun::components::ImageFormat &_format) &&
The format of the image.
Definition segmentation_image.hpp:217
Component: Draw order of 2D elements.
Definition draw_order.hpp:30
Component: A buffer that is known to store image data.
Definition image_buffer.hpp:18
Component: The metadata describing the contents of a components::ImageBuffer.
Definition image_format.hpp:14
Component: Degree of transparency ranging from 0.0 (fully transparent) to 1.0 (fully opaque).
Definition opacity.hpp:17
Encoding: The metadata describing the contents of a components::ImageBuffer.
Definition image_format.hpp:25