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_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() {
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 /// std::fill_n(data.begin() + y * WIDTH, 6, static_cast<uint8_t>(1)); // left half
56 /// }
57 /// for (auto y = 4; y <8; ++y) { // bottom half
58 /// std::fill_n(data.begin() + y * WIDTH + 6, 6, static_cast<uint8_t>(2)); // right half
59 /// }
60 ///
61 /// // create an annotation context to describe the classes
62 /// rec.log_static(
63 /// "/",
64 /// rerun::AnnotationContext({
65 /// rerun::AnnotationInfo(1, "red", rerun::Rgba32(255, 0, 0)),
66 /// rerun::AnnotationInfo(2, "green", rerun::Rgba32(0, 255, 0)),
67 /// })
68 /// );
69 ///
70 /// rec.log("image", rerun::SegmentationImage(data.data(), {WIDTH, HEIGHT}));
71 /// }
72 /// ```
74 /// The raw image data.
75 std::optional<ComponentBatch> buffer;
76
77 /// The format of the image.
78 std::optional<ComponentBatch> format;
79
80 /// Opacity of the image, useful for layering the segmentation image on top of another image.
81 ///
82 /// Defaults to 0.5 if there's any other images in the scene, otherwise 1.0.
83 std::optional<ComponentBatch> opacity;
84
85 /// An optional floating point value that specifies the 2D drawing order.
86 ///
87 /// Objects with higher values are drawn on top of those with lower values.
88 /// Defaults to `0.0`.
89 std::optional<ComponentBatch> draw_order;
90
91 public:
92 /// The name of the archetype as used in `ComponentDescriptor`s.
93 static constexpr const char ArchetypeName[] = "rerun.archetypes.SegmentationImage";
94
95 /// `ComponentDescriptor` for the `buffer` field.
96 static constexpr auto Descriptor_buffer = ComponentDescriptor(
97 ArchetypeName, "SegmentationImage:buffer",
99 );
100 /// `ComponentDescriptor` for the `format` field.
101 static constexpr auto Descriptor_format = ComponentDescriptor(
102 ArchetypeName, "SegmentationImage:format",
104 );
105 /// `ComponentDescriptor` for the `opacity` field.
107 ArchetypeName, "SegmentationImage:opacity",
109 );
110 /// `ComponentDescriptor` for the `draw_order` field.
112 ArchetypeName, "SegmentationImage:draw_order",
114 );
115
116 public: // START of extensions from segmentation_image_ext.cpp:
117 /// Constructs image from pointer + resolution, inferring the datatype from the pointer type.
118 ///
119 /// @param pixels The raw image data.
120 /// ⚠️ Does not take ownership of the data, the caller must ensure the data outlives the image.
121 /// The number of elements is assumed to be `W * H`.
122 /// @param resolution The resolution of the image as {width, height}.
123 template <typename TElement>
124 SegmentationImage(const TElement* pixels, WidthHeight resolution)
126 reinterpret_cast<const uint8_t*>(pixels), resolution, get_datatype(pixels)} {}
127
128 /// Constructs image from pixel data + resolution with datatype inferred from the passed collection.
129 ///
130 /// @param pixels The raw image data.
131 /// If the data does not outlive the image, use `std::move` or create the `rerun::Collection`
132 /// explicitly ahead of time with `rerun::Collection::take_ownership`.
133 /// The length of the data should be `W * H`.
134 /// @param resolution The resolution of the image as {width, height}.
135 template <typename TElement>
137 : SegmentationImage{pixels.to_uint8(), resolution, get_datatype(pixels.data())} {}
138
139 /// Constructs image from pixel data + resolution with explicit datatype. Borrows data from a pointer (i.e. data must outlive the image!).
140 ///
141 /// @param bytes The raw image data.
142 /// ⚠️ Does not take ownership of the data, the caller must ensure the data outlives the image.
143 /// The byte size of the data is assumed to be `W * H * datatype.size`
144 /// @param resolution The resolution of the image as {width, height}.
145 /// @param datatype How the data should be interpreted.
147 const void* bytes, WidthHeight resolution, datatypes::ChannelDatatype datatype
148 )
150 Collection<uint8_t>::borrow(bytes, num_bytes(resolution, datatype)),
151 resolution,
152 datatype} {}
153
154 /// Constructs image from pixel data + resolution + datatype.
155 ///
156 /// @param bytes The raw image data as bytes.
157 /// If the data does not outlive the image, use `std::move` or create the `rerun::Collection`
158 /// explicitly ahead of time with `rerun::Collection::take_ownership`.
159 /// The length of the data should be `W * H`.
160 /// @param resolution The resolution of the image as {width, height}.
161 /// @param datatype How the data should be interpreted.
164 ) {
165 auto image_format = datatypes::ImageFormat{resolution, datatype};
166 if (bytes.size() != image_format.num_bytes()) {
167 Error(
168 ErrorCode::InvalidTensorDimension,
169 "SegmentationImage buffer has the wrong size. Got " +
170 std::to_string(bytes.size()) + " bytes, expected " +
171 std::to_string(image_format.num_bytes())
172 )
173 .handle();
174 }
175 *this = std::move(*this).with_buffer(bytes).with_format(image_format);
176 }
177
178 // END of extensions from segmentation_image_ext.cpp, start of generated code:
179
180 public:
181 SegmentationImage() = default;
182 SegmentationImage(SegmentationImage&& other) = default;
183 SegmentationImage(const SegmentationImage& other) = default;
184 SegmentationImage& operator=(const SegmentationImage& other) = default;
185 SegmentationImage& operator=(SegmentationImage&& other) = default;
186
187 /// Update only some specific fields of a `SegmentationImage`.
189 return SegmentationImage();
190 }
191
192 /// Clear all the fields of a `SegmentationImage`.
194
195 /// The raw image data.
197 buffer = ComponentBatch::from_loggable(_buffer, Descriptor_buffer).value_or_throw();
198 return std::move(*this);
199 }
200
201 /// This method makes it possible to pack multiple `buffer` in a single component batch.
202 ///
203 /// This only makes sense when used in conjunction with `columns`. `with_buffer` should
204 /// be used when logging a single row's worth of data.
206 ) && {
207 buffer = ComponentBatch::from_loggable(_buffer, Descriptor_buffer).value_or_throw();
208 return std::move(*this);
209 }
210
211 /// The format of the image.
213 format = ComponentBatch::from_loggable(_format, Descriptor_format).value_or_throw();
214 return std::move(*this);
215 }
216
217 /// This method makes it possible to pack multiple `format` in a single component batch.
218 ///
219 /// This only makes sense when used in conjunction with `columns`. `with_format` should
220 /// be used when logging a single row's worth of data.
222 ) && {
223 format = ComponentBatch::from_loggable(_format, Descriptor_format).value_or_throw();
224 return std::move(*this);
225 }
226
227 /// Opacity of the image, useful for layering the segmentation image on top of another image.
228 ///
229 /// Defaults to 0.5 if there's any other images in the scene, otherwise 1.0.
231 opacity = ComponentBatch::from_loggable(_opacity, Descriptor_opacity).value_or_throw();
232 return std::move(*this);
233 }
234
235 /// This method makes it possible to pack multiple `opacity` in a single component batch.
236 ///
237 /// This only makes sense when used in conjunction with `columns`. `with_opacity` should
238 /// be used when logging a single row's worth of data.
240 ) && {
241 opacity = ComponentBatch::from_loggable(_opacity, Descriptor_opacity).value_or_throw();
242 return std::move(*this);
243 }
244
245 /// An optional floating point value that specifies the 2D drawing order.
246 ///
247 /// Objects with higher values are drawn on top of those with lower values.
248 /// Defaults to `0.0`.
250 draw_order =
251 ComponentBatch::from_loggable(_draw_order, Descriptor_draw_order).value_or_throw();
252 return std::move(*this);
253 }
254
255 /// This method makes it possible to pack multiple `draw_order` in a single component batch.
256 ///
257 /// This only makes sense when used in conjunction with `columns`. `with_draw_order` should
258 /// be used when logging a single row's worth of data.
261 ) && {
262 draw_order =
263 ComponentBatch::from_loggable(_draw_order, Descriptor_draw_order).value_or_throw();
264 return std::move(*this);
265 }
266
267 /// Partitions the component data into multiple sub-batches.
268 ///
269 /// Specifically, this transforms the existing `ComponentBatch` data into `ComponentColumn`s
270 /// instead, via `ComponentBatch::partitioned`.
271 ///
272 /// This makes it possible to use `RecordingStream::send_columns` to send columnar data directly into Rerun.
273 ///
274 /// The specified `lengths` must sum to the total length of the component batch.
276
277 /// Partitions the component data into unit-length sub-batches.
278 ///
279 /// This is semantically similar to calling `columns` with `std::vector<uint32_t>(n, 1)`,
280 /// where `n` is automatically guessed.
282 };
283
284} // namespace rerun::archetypes
285
286namespace rerun {
287 /// \private
288 template <typename T>
289 struct AsComponents;
290
291 /// \private
292 template <>
293 struct AsComponents<archetypes::SegmentationImage> {
294 /// Serialize all set component batches.
295 static Result<Collection<ComponentBatch>> as_batches(
296 const archetypes::SegmentationImage& archetype
297 );
298 };
299} // 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:99
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:76
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:73
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:205
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:83
SegmentationImage with_buffer(const rerun::components::ImageBuffer &_buffer) &&
The raw image data.
Definition segmentation_image.hpp:196
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:221
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:162
std::optional< ComponentBatch > buffer
The raw image data.
Definition segmentation_image.hpp:75
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:249
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:89
static constexpr auto Descriptor_buffer
ComponentDescriptor for the buffer field.
Definition segmentation_image.hpp:96
SegmentationImage(Collection< TElement > pixels, WidthHeight resolution)
Constructs image from pixel data + resolution with datatype inferred from the passed collection.
Definition segmentation_image.hpp:136
static constexpr auto Descriptor_format
ComponentDescriptor for the format field.
Definition segmentation_image.hpp:101
static constexpr auto Descriptor_opacity
ComponentDescriptor for the opacity field.
Definition segmentation_image.hpp:106
SegmentationImage(const TElement *pixels, WidthHeight resolution)
Constructs image from pointer + resolution, inferring the datatype from the pointer type.
Definition segmentation_image.hpp:124
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:239
std::optional< ComponentBatch > format
The format of the image.
Definition segmentation_image.hpp:78
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:259
SegmentationImage(const void *bytes, WidthHeight resolution, datatypes::ChannelDatatype datatype)
Constructs image from pixel data + resolution with explicit datatype.
Definition segmentation_image.hpp:146
static constexpr auto Descriptor_draw_order
ComponentDescriptor for the draw_order field.
Definition segmentation_image.hpp:111
static constexpr const char ArchetypeName[]
The name of the archetype as used in ComponentDescriptors.
Definition segmentation_image.hpp:93
static SegmentationImage update_fields()
Update only some specific fields of a SegmentationImage.
Definition segmentation_image.hpp:188
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:230
SegmentationImage with_format(const rerun::components::ImageFormat &_format) &&
The format of the image.
Definition segmentation_image.hpp:212
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