Rerun C++ SDK
Loading...
Searching...
No Matches
pinhole.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/pinhole.fbs".
3
4#pragma once
5
6#include "../collection.hpp"
7#include "../component_batch.hpp"
8#include "../component_column.hpp"
9#include "../components/color.hpp"
10#include "../components/image_plane_distance.hpp"
11#include "../components/pinhole_projection.hpp"
12#include "../components/radius.hpp"
13#include "../components/resolution.hpp"
14#include "../components/view_coordinates.hpp"
15#include "../result.hpp"
16
17#include <cmath>
18#include <cstdint>
19#include <limits>
20#include <optional>
21#include <utility>
22#include <vector>
23
24namespace rerun::archetypes {
25 /// **Archetype**: Camera perspective projection (a.k.a. intrinsics).
26 ///
27 /// ## Examples
28 ///
29 /// ### Simple pinhole camera
30 /// ![image](https://static.rerun.io/pinhole_simple/9af9441a94bcd9fd54e1fea44fb0c59ff381a7f2/full.png)
31 ///
32 /// ```cpp
33 /// #include <rerun.hpp>
34 ///
35 /// #include <algorithm> // std::generate
36 /// #include <cstdlib> // std::rand
37 /// #include <vector>
38 ///
39 /// int main() {
40 /// const auto rec = rerun::RecordingStream("rerun_example_pinhole");
41 /// rec.spawn().exit_on_failure();
42 ///
43 /// rec.log("world/image", rerun::Pinhole::from_focal_length_and_resolution(3.0f, {3.0f, 3.0f}));
44 ///
45 /// std::vector<uint8_t> random_data(3 * 3 * 3);
46 /// std::generate(random_data.begin(), random_data.end(), [] {
47 /// return static_cast<uint8_t>(std::rand());
48 /// });
49 ///
50 /// rec.log("world/image", rerun::Image::from_rgb24(random_data, {3, 3}));
51 /// }
52 /// ```
53 ///
54 /// ### Perspective pinhole camera
55 /// ![image](https://static.rerun.io/pinhole_perspective/317e2de6d212b238dcdad5b67037e9e2a2afafa0/full.png)
56 ///
57 /// ```cpp
58 /// #include <rerun.hpp>
59 ///
60 /// int main() {
61 /// const auto rec = rerun::RecordingStream("rerun_example_pinhole_perspective");
62 /// rec.spawn().exit_on_failure();
63 ///
64 /// const float fov_y = 0.7853982f;
65 /// const float aspect_ratio = 1.7777778f;
66 /// rec.log(
67 /// "world/cam",
68 /// rerun::Pinhole::from_fov_and_aspect_ratio(fov_y, aspect_ratio)
69 /// .with_camera_xyz(rerun::components::ViewCoordinates::RUB)
70 /// .with_image_plane_distance(0.1f)
71 /// .with_color(rerun::Color(255, 128, 0))
72 /// .with_line_width(0.003f)
73 /// );
74 ///
75 /// rec.log(
76 /// "world/points",
77 /// rerun::Points3D({{0.0f, 0.0f, -0.5f}, {0.1f, 0.1f, -0.5f}, {-0.1f, -0.1f, -0.5f}}
78 /// ).with_radii({0.025f})
79 /// );
80 /// }
81 /// ```
82 ///
83 /// ⚠ **This type is _unstable_ and may change significantly in a way that the data won't be backwards compatible.**
84 ///
85 struct Pinhole {
86 /// Camera projection, from image coordinates to view coordinates.
87 std::optional<ComponentBatch> image_from_camera;
88
89 /// Pixel resolution (usually integers) of child image space. Width and height.
90 ///
91 /// Example:
92 /// ```text
93 /// [1920.0, 1440.0]
94 /// ```
95 ///
96 /// `image_from_camera` project onto the space spanned by `(0,0)` and `resolution - 1`.
97 std::optional<ComponentBatch> resolution;
98
99 /// Sets the view coordinates for the camera.
100 ///
101 /// All common values are available as constants on the `components::ViewCoordinates` class.
102 ///
103 /// The default is `ViewCoordinates::RDF`, i.e. X=Right, Y=Down, Z=Forward, and this is also the recommended setting.
104 /// This means that the camera frustum will point along the positive Z axis of the parent space,
105 /// and the cameras "up" direction will be along the negative Y axis of the parent space.
106 ///
107 /// The camera frustum will point whichever axis is set to `F` (or the opposite of `B`).
108 /// When logging a depth image under this entity, this is the direction the point cloud will be projected.
109 /// With `RDF`, the default forward is +Z.
110 ///
111 /// The frustum's "up" direction will be whichever axis is set to `U` (or the opposite of `D`).
112 /// This will match the negative Y direction of pixel space (all images are assumed to have xyz=RDF).
113 /// With `RDF`, the default is up is -Y.
114 ///
115 /// The frustum's "right" direction will be whichever axis is set to `R` (or the opposite of `L`).
116 /// This will match the positive X direction of pixel space (all images are assumed to have xyz=RDF).
117 /// With `RDF`, the default right is +x.
118 ///
119 /// Other common formats are `RUB` (X=Right, Y=Up, Z=Back) and `FLU` (X=Forward, Y=Left, Z=Up).
120 ///
121 /// NOTE: setting this to something else than `RDF` (the default) will change the orientation of the camera frustum,
122 /// and make the pinhole matrix not match up with the coordinate system of the pinhole entity.
123 ///
124 /// The pinhole matrix (the `image_from_camera` argument) always project along the third (Z) axis,
125 /// but will be re-oriented to project along the forward axis of the `camera_xyz` argument.
126 std::optional<ComponentBatch> camera_xyz;
127
128 /// The distance from the camera origin to the image plane when the projection is shown in a 3D viewer.
129 ///
130 /// This is only used for visualization purposes, and does not affect the projection itself.
131 std::optional<ComponentBatch> image_plane_distance;
132
133 /// Color of the camera wireframe.
134 std::optional<ComponentBatch> color;
135
136 /// Width of the camera wireframe lines.
137 std::optional<ComponentBatch> line_width;
138
139 public:
140 /// The name of the archetype as used in `ComponentDescriptor`s.
141 static constexpr const char ArchetypeName[] = "rerun.archetypes.Pinhole";
142
143 /// `ComponentDescriptor` for the `image_from_camera` field.
145 ArchetypeName, "Pinhole:image_from_camera",
147 );
148 /// `ComponentDescriptor` for the `resolution` field.
150 ArchetypeName, "Pinhole:resolution",
152 );
153 /// `ComponentDescriptor` for the `camera_xyz` field.
155 ArchetypeName, "Pinhole:camera_xyz",
157 );
158 /// `ComponentDescriptor` for the `image_plane_distance` field.
160 ArchetypeName, "Pinhole:image_plane_distance",
162 );
163 /// `ComponentDescriptor` for the `color` field.
164 static constexpr auto Descriptor_color = ComponentDescriptor(
166 );
167 /// `ComponentDescriptor` for the `line_width` field.
170 );
171
172 public: // START of extensions from pinhole_ext.cpp:
173 /// Creates a pinhole from the camera focal length and resolution, both specified in pixels.
174 ///
175 /// The focal length is the diagonal of the projection matrix.
176 /// Set the same value for x & y value for symmetric cameras, or two values for anamorphic
177 /// cameras.
178 ///
179 /// Assumes the principal point to be in the middle of the sensor.
181 const datatypes::Vec2D& focal_length, const datatypes::Vec2D& resolution
182 );
183
184 /// Creates a symmetric pinhole from the camera focal length and resolution, both specified
185 /// in pixels.
186 ///
187 /// The focal length is the diagonal of the projection matrix.
188 ///
189 /// Assumes the principal point to be in the middle of the sensor.
191 float focal_length, const datatypes::Vec2D& resolution
192 ) {
193 return from_focal_length_and_resolution({focal_length, focal_length}, resolution);
194 }
195
196 /// Creates a pinhole from the camera vertical field of view (in radians) and aspect ratio (width/height).
197 ///
198 /// Assumes the principal point to be in the middle of the sensor.
199 static Pinhole from_fov_and_aspect_ratio(float fov_y, float aspect_ratio) {
200 const float EPSILON = std::numeric_limits<float>::epsilon();
201 // `max` has explicit template args to avoid preprocessor replacement when <windows.h> is included without NOMINMAX.
202 const float focal_length_y = 0.5f / std::tan(std::max<float>(fov_y * 0.5f, EPSILON));
204 {focal_length_y, focal_length_y},
205 {aspect_ratio, 1.0}
206 );
207 }
208
209 /// Pixel resolution (usually integers) of child image space. Width and height.
210 ///
211 /// `image_from_camera` project onto the space spanned by `(0,0)` and `resolution - 1`.
212 Pinhole with_resolution(float width, float height) && {
213 return std::move(*this).with_resolution(rerun::components::Resolution(width, height));
214 }
215
216 /// Pixel resolution (usually integers) of child image space. Width and height.
217 ///
218 /// `image_from_camera` project onto the space spanned by `(0,0)` and `resolution - 1`.
219 Pinhole with_resolution(int width, int height) && {
220 return std::move(*this).with_resolution(rerun::components::Resolution(width, height));
221 }
222
223 // END of extensions from pinhole_ext.cpp, start of generated code:
224
225 public:
226 Pinhole() = default;
227 Pinhole(Pinhole&& other) = default;
228 Pinhole(const Pinhole& other) = default;
229 Pinhole& operator=(const Pinhole& other) = default;
230 Pinhole& operator=(Pinhole&& other) = default;
231
232 explicit Pinhole(rerun::components::PinholeProjection _image_from_camera)
233 : image_from_camera(ComponentBatch::from_loggable(
234 std::move(_image_from_camera), Descriptor_image_from_camera
235 )
236 .value_or_throw()) {}
237
238 /// Update only some specific fields of a `Pinhole`.
240 return Pinhole();
241 }
242
243 /// Clear all the fields of a `Pinhole`.
245
246 /// Camera projection, from image coordinates to view coordinates.
248 const rerun::components::PinholeProjection& _image_from_camera
249 ) && {
252 .value_or_throw();
253 return std::move(*this);
254 }
255
256 /// This method makes it possible to pack multiple `image_from_camera` in a single component batch.
257 ///
258 /// This only makes sense when used in conjunction with `columns`. `with_image_from_camera` should
259 /// be used when logging a single row's worth of data.
262 ) && {
265 .value_or_throw();
266 return std::move(*this);
267 }
268
269 /// Pixel resolution (usually integers) of child image space. Width and height.
270 ///
271 /// Example:
272 /// ```text
273 /// [1920.0, 1440.0]
274 /// ```
275 ///
276 /// `image_from_camera` project onto the space spanned by `(0,0)` and `resolution - 1`.
278 resolution =
279 ComponentBatch::from_loggable(_resolution, Descriptor_resolution).value_or_throw();
280 return std::move(*this);
281 }
282
283 /// This method makes it possible to pack multiple `resolution` in a single component batch.
284 ///
285 /// This only makes sense when used in conjunction with `columns`. `with_resolution` should
286 /// be used when logging a single row's worth of data.
288 ) && {
289 resolution =
290 ComponentBatch::from_loggable(_resolution, Descriptor_resolution).value_or_throw();
291 return std::move(*this);
292 }
293
294 /// Sets the view coordinates for the camera.
295 ///
296 /// All common values are available as constants on the `components::ViewCoordinates` class.
297 ///
298 /// The default is `ViewCoordinates::RDF`, i.e. X=Right, Y=Down, Z=Forward, and this is also the recommended setting.
299 /// This means that the camera frustum will point along the positive Z axis of the parent space,
300 /// and the cameras "up" direction will be along the negative Y axis of the parent space.
301 ///
302 /// The camera frustum will point whichever axis is set to `F` (or the opposite of `B`).
303 /// When logging a depth image under this entity, this is the direction the point cloud will be projected.
304 /// With `RDF`, the default forward is +Z.
305 ///
306 /// The frustum's "up" direction will be whichever axis is set to `U` (or the opposite of `D`).
307 /// This will match the negative Y direction of pixel space (all images are assumed to have xyz=RDF).
308 /// With `RDF`, the default is up is -Y.
309 ///
310 /// The frustum's "right" direction will be whichever axis is set to `R` (or the opposite of `L`).
311 /// This will match the positive X direction of pixel space (all images are assumed to have xyz=RDF).
312 /// With `RDF`, the default right is +x.
313 ///
314 /// Other common formats are `RUB` (X=Right, Y=Up, Z=Back) and `FLU` (X=Forward, Y=Left, Z=Up).
315 ///
316 /// NOTE: setting this to something else than `RDF` (the default) will change the orientation of the camera frustum,
317 /// and make the pinhole matrix not match up with the coordinate system of the pinhole entity.
318 ///
319 /// The pinhole matrix (the `image_from_camera` argument) always project along the third (Z) axis,
320 /// but will be re-oriented to project along the forward axis of the `camera_xyz` argument.
322 camera_xyz =
323 ComponentBatch::from_loggable(_camera_xyz, Descriptor_camera_xyz).value_or_throw();
324 return std::move(*this);
325 }
326
327 /// This method makes it possible to pack multiple `camera_xyz` in a single component batch.
328 ///
329 /// This only makes sense when used in conjunction with `columns`. `with_camera_xyz` should
330 /// be used when logging a single row's worth of data.
333 ) && {
334 camera_xyz =
335 ComponentBatch::from_loggable(_camera_xyz, Descriptor_camera_xyz).value_or_throw();
336 return std::move(*this);
337 }
338
339 /// The distance from the camera origin to the image plane when the projection is shown in a 3D viewer.
340 ///
341 /// This is only used for visualization purposes, and does not affect the projection itself.
343 const rerun::components::ImagePlaneDistance& _image_plane_distance
344 ) && {
346 _image_plane_distance,
348 )
349 .value_or_throw();
350 return std::move(*this);
351 }
352
353 /// This method makes it possible to pack multiple `image_plane_distance` in a single component batch.
354 ///
355 /// This only makes sense when used in conjunction with `columns`. `with_image_plane_distance` should
356 /// be used when logging a single row's worth of data.
358 const Collection<rerun::components::ImagePlaneDistance>& _image_plane_distance
359 ) && {
361 _image_plane_distance,
363 )
364 .value_or_throw();
365 return std::move(*this);
366 }
367
368 /// Color of the camera wireframe.
370 color = ComponentBatch::from_loggable(_color, Descriptor_color).value_or_throw();
371 return std::move(*this);
372 }
373
374 /// This method makes it possible to pack multiple `color` in a single component batch.
375 ///
376 /// This only makes sense when used in conjunction with `columns`. `with_color` should
377 /// be used when logging a single row's worth of data.
379 color = ComponentBatch::from_loggable(_color, Descriptor_color).value_or_throw();
380 return std::move(*this);
381 }
382
383 /// Width of the camera wireframe lines.
385 line_width =
386 ComponentBatch::from_loggable(_line_width, Descriptor_line_width).value_or_throw();
387 return std::move(*this);
388 }
389
390 /// This method makes it possible to pack multiple `line_width` in a single component batch.
391 ///
392 /// This only makes sense when used in conjunction with `columns`. `with_line_width` should
393 /// be used when logging a single row's worth of data.
395 line_width =
396 ComponentBatch::from_loggable(_line_width, Descriptor_line_width).value_or_throw();
397 return std::move(*this);
398 }
399
400 /// Partitions the component data into multiple sub-batches.
401 ///
402 /// Specifically, this transforms the existing `ComponentBatch` data into `ComponentColumn`s
403 /// instead, via `ComponentBatch::partitioned`.
404 ///
405 /// This makes it possible to use `RecordingStream::send_columns` to send columnar data directly into Rerun.
406 ///
407 /// The specified `lengths` must sum to the total length of the component batch.
409
410 /// Partitions the component data into unit-length sub-batches.
411 ///
412 /// This is semantically similar to calling `columns` with `std::vector<uint32_t>(n, 1)`,
413 /// where `n` is automatically guessed.
415 };
416
417} // namespace rerun::archetypes
418
419namespace rerun {
420 /// \private
421 template <typename T>
422 struct AsComponents;
423
424 /// \private
425 template <>
426 struct AsComponents<archetypes::Pinhole> {
427 /// Serialize all set component batches.
428 static Result<Collection<ComponentBatch>> as_batches(const archetypes::Pinhole& archetype);
429 };
430} // 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: Camera perspective projection (a.k.a.
Definition pinhole.hpp:85
Pinhole with_resolution(const rerun::components::Resolution &_resolution) &&
Pixel resolution (usually integers) of child image space.
Definition pinhole.hpp:277
Pinhole with_many_color(const Collection< rerun::components::Color > &_color) &&
This method makes it possible to pack multiple color in a single component batch.
Definition pinhole.hpp:378
std::optional< ComponentBatch > color
Color of the camera wireframe.
Definition pinhole.hpp:134
static constexpr const char ArchetypeName[]
The name of the archetype as used in ComponentDescriptors.
Definition pinhole.hpp:141
Pinhole with_resolution(float width, float height) &&
Pixel resolution (usually integers) of child image space.
Definition pinhole.hpp:212
static constexpr auto Descriptor_color
ComponentDescriptor for the color field.
Definition pinhole.hpp:164
static constexpr auto Descriptor_resolution
ComponentDescriptor for the resolution field.
Definition pinhole.hpp:149
std::optional< ComponentBatch > image_from_camera
Camera projection, from image coordinates to view coordinates.
Definition pinhole.hpp:87
Pinhole with_resolution(int width, int height) &&
Pixel resolution (usually integers) of child image space.
Definition pinhole.hpp:219
Collection< ComponentColumn > columns()
Partitions the component data into unit-length sub-batches.
Pinhole with_camera_xyz(const rerun::components::ViewCoordinates &_camera_xyz) &&
Sets the view coordinates for the camera.
Definition pinhole.hpp:321
Pinhole with_many_image_plane_distance(const Collection< rerun::components::ImagePlaneDistance > &_image_plane_distance) &&
This method makes it possible to pack multiple image_plane_distance in a single component batch.
Definition pinhole.hpp:357
std::optional< ComponentBatch > line_width
Width of the camera wireframe lines.
Definition pinhole.hpp:137
Pinhole with_many_camera_xyz(const Collection< rerun::components::ViewCoordinates > &_camera_xyz) &&
This method makes it possible to pack multiple camera_xyz in a single component batch.
Definition pinhole.hpp:331
static Pinhole update_fields()
Update only some specific fields of a Pinhole.
Definition pinhole.hpp:239
std::optional< ComponentBatch > image_plane_distance
The distance from the camera origin to the image plane when the projection is shown in a 3D viewer.
Definition pinhole.hpp:131
Pinhole with_color(const rerun::components::Color &_color) &&
Color of the camera wireframe.
Definition pinhole.hpp:369
static constexpr auto Descriptor_camera_xyz
ComponentDescriptor for the camera_xyz field.
Definition pinhole.hpp:154
static Pinhole from_fov_and_aspect_ratio(float fov_y, float aspect_ratio)
Creates a pinhole from the camera vertical field of view (in radians) and aspect ratio (width/height)...
Definition pinhole.hpp:199
static Pinhole clear_fields()
Clear all the fields of a Pinhole.
Collection< ComponentColumn > columns(const Collection< uint32_t > &lengths_)
Partitions the component data into multiple sub-batches.
static constexpr auto Descriptor_image_plane_distance
ComponentDescriptor for the image_plane_distance field.
Definition pinhole.hpp:159
static Pinhole from_focal_length_and_resolution(const datatypes::Vec2D &focal_length, const datatypes::Vec2D &resolution)
Creates a pinhole from the camera focal length and resolution, both specified in pixels.
std::optional< ComponentBatch > camera_xyz
Sets the view coordinates for the camera.
Definition pinhole.hpp:126
static Pinhole from_focal_length_and_resolution(float focal_length, const datatypes::Vec2D &resolution)
Creates a symmetric pinhole from the camera focal length and resolution, both specified in pixels.
Definition pinhole.hpp:190
static constexpr auto Descriptor_line_width
ComponentDescriptor for the line_width field.
Definition pinhole.hpp:168
Pinhole with_many_line_width(const Collection< rerun::components::Radius > &_line_width) &&
This method makes it possible to pack multiple line_width in a single component batch.
Definition pinhole.hpp:394
std::optional< ComponentBatch > resolution
Pixel resolution (usually integers) of child image space.
Definition pinhole.hpp:97
Pinhole with_line_width(const rerun::components::Radius &_line_width) &&
Width of the camera wireframe lines.
Definition pinhole.hpp:384
Pinhole with_many_image_from_camera(const Collection< rerun::components::PinholeProjection > &_image_from_camera) &&
This method makes it possible to pack multiple image_from_camera in a single component batch.
Definition pinhole.hpp:260
Pinhole with_image_from_camera(const rerun::components::PinholeProjection &_image_from_camera) &&
Camera projection, from image coordinates to view coordinates.
Definition pinhole.hpp:247
Pinhole with_many_resolution(const Collection< rerun::components::Resolution > &_resolution) &&
This method makes it possible to pack multiple resolution in a single component batch.
Definition pinhole.hpp:287
Pinhole with_image_plane_distance(const rerun::components::ImagePlaneDistance &_image_plane_distance) &&
The distance from the camera origin to the image plane when the projection is shown in a 3D viewer.
Definition pinhole.hpp:342
static constexpr auto Descriptor_image_from_camera
ComponentDescriptor for the image_from_camera field.
Definition pinhole.hpp:144
Component: An RGBA color with unmultiplied/separate alpha, in sRGB gamma space with linear alpha.
Definition color.hpp:17
Component: The distance from the camera origin to the image plane when the projection is shown in a 3...
Definition image_plane_distance.hpp:16
Component: Camera projection, from image coordinates to view coordinates.
Definition pinhole_projection.hpp:25
Component: The radius of something, e.g.
Definition radius.hpp:21
Component: Pixel resolution width & height, e.g.
Definition resolution.hpp:18
Component: How we interpret the coordinate system of an entity/space.
Definition view_coordinates.hpp:35
Datatype: A vector in 2D space.
Definition vec2d.hpp:20