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