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