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