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 struct Pinhole {
80 /// Camera projection, from image coordinates to view coordinates.
81 std::optional<ComponentBatch> image_from_camera;
82
83 /// Pixel resolution (usually integers) of child image space. Width and height.
84 ///
85 /// Example:
86 /// ```text
87 /// [1920.0, 1440.0]
88 /// ```
89 ///
90 /// `image_from_camera` project onto the space spanned by `(0,0)` and `resolution - 1`.
91 std::optional<ComponentBatch> resolution;
92
93 /// Sets the view coordinates for the camera.
94 ///
95 /// All common values are available as constants on the `components::ViewCoordinates` class.
96 ///
97 /// The default is `ViewCoordinates::RDF`, i.e. X=Right, Y=Down, Z=Forward, and this is also the recommended setting.
98 /// This means that the camera frustum will point along the positive Z axis of the parent space,
99 /// and the cameras "up" direction will be along the negative Y axis of the parent space.
100 ///
101 /// The camera frustum will point whichever axis is set to `F` (or the opposite of `B`).
102 /// When logging a depth image under this entity, this is the direction the point cloud will be projected.
103 /// With `RDF`, the default forward is +Z.
104 ///
105 /// The frustum's "up" direction will be whichever axis is set to `U` (or the opposite of `D`).
106 /// This will match the negative Y direction of pixel space (all images are assumed to have xyz=RDF).
107 /// With `RDF`, the default is up is -Y.
108 ///
109 /// The frustum's "right" direction will be whichever axis is set to `R` (or the opposite of `L`).
110 /// This will match the positive X direction of pixel space (all images are assumed to have xyz=RDF).
111 /// With `RDF`, the default right is +x.
112 ///
113 /// Other common formats are `RUB` (X=Right, Y=Up, Z=Back) and `FLU` (X=Forward, Y=Left, Z=Up).
114 ///
115 /// NOTE: setting this to something else than `RDF` (the default) will change the orientation of the camera frustum,
116 /// and make the pinhole matrix not match up with the coordinate system of the pinhole entity.
117 ///
118 /// The pinhole matrix (the `image_from_camera` argument) always project along the third (Z) axis,
119 /// but will be re-oriented to project along the forward axis of the `camera_xyz` argument.
120 std::optional<ComponentBatch> camera_xyz;
121
122 /// The distance from the camera origin to the image plane when the projection is shown in a 3D viewer.
123 ///
124 /// This is only used for visualization purposes, and does not affect the projection itself.
125 std::optional<ComponentBatch> image_plane_distance;
126
127 public:
128 static constexpr const char IndicatorComponentName[] = "rerun.components.PinholeIndicator";
129
130 /// Indicator component, used to identify the archetype when converting to a list of components.
132 /// The name of the archetype as used in `ComponentDescriptor`s.
133 static constexpr const char ArchetypeName[] = "rerun.archetypes.Pinhole";
134
135 /// `ComponentDescriptor` for the `image_from_camera` field.
137 ArchetypeName, "image_from_camera",
139 );
140 /// `ComponentDescriptor` for the `resolution` field.
142 ArchetypeName, "resolution",
144 );
145 /// `ComponentDescriptor` for the `camera_xyz` field.
147 ArchetypeName, "camera_xyz",
149 );
150 /// `ComponentDescriptor` for the `image_plane_distance` field.
152 ArchetypeName, "image_plane_distance",
154 );
155
156 public: // START of extensions from pinhole_ext.cpp:
157 /// Creates a pinhole from the camera focal length and resolution, both specified in pixels.
158 ///
159 /// The focal length is the diagonal of the projection matrix.
160 /// Set the same value for x & y value for symmetric cameras, or two values for anamorphic
161 /// cameras.
162 ///
163 /// Assumes the principal point to be in the middle of the sensor.
165 const datatypes::Vec2D& focal_length, const datatypes::Vec2D& resolution
166 );
167
168 /// Creates a symmetric pinhole from the camera focal length and resolution, both specified
169 /// in pixels.
170 ///
171 /// The focal length is the diagonal of the projection matrix.
172 ///
173 /// Assumes the principal point to be in the middle of the sensor.
175 float focal_length, const datatypes::Vec2D& resolution
176 ) {
177 return from_focal_length_and_resolution({focal_length, focal_length}, resolution);
178 }
179
180 /// Creates a pinhole from the camera vertical field of view (in radians) and aspect ratio (width/height).
181 ///
182 /// Assumes the principal point to be in the middle of the sensor.
183 static Pinhole from_fov_and_aspect_ratio(float fov_y, float aspect_ratio) {
184 const float EPSILON = std::numeric_limits<float>::epsilon();
185 // `max` has explicit template args to avoid preprocessor replacement when <windows.h> is included without NOMINMAX.
186 const float focal_length_y = 0.5f / std::tan(std::max<float>(fov_y * 0.5f, EPSILON));
188 {focal_length_y, focal_length_y},
189 {aspect_ratio, 1.0}
190 );
191 }
192
193 /// Pixel resolution (usually integers) of child image space. Width and height.
194 ///
195 /// `image_from_camera` project onto the space spanned by `(0,0)` and `resolution - 1`.
196 Pinhole with_resolution(float width, float height) && {
197 return std::move(*this).with_resolution(rerun::components::Resolution(width, height));
198 }
199
200 /// Pixel resolution (usually integers) of child image space. Width and height.
201 ///
202 /// `image_from_camera` project onto the space spanned by `(0,0)` and `resolution - 1`.
203 Pinhole with_resolution(int width, int height) && {
204 return std::move(*this).with_resolution(rerun::components::Resolution(width, height));
205 }
206
207 // END of extensions from pinhole_ext.cpp, start of generated code:
208
209 public:
210 Pinhole() = default;
211 Pinhole(Pinhole&& other) = default;
212 Pinhole(const Pinhole& other) = default;
213 Pinhole& operator=(const Pinhole& other) = default;
214 Pinhole& operator=(Pinhole&& other) = default;
215
216 explicit Pinhole(rerun::components::PinholeProjection _image_from_camera)
217 : image_from_camera(ComponentBatch::from_loggable(
218 std::move(_image_from_camera), Descriptor_image_from_camera
219 )
220 .value_or_throw()) {}
221
222 /// Update only some specific fields of a `Pinhole`.
224 return Pinhole();
225 }
226
227 /// Clear all the fields of a `Pinhole`.
229
230 /// Camera projection, from image coordinates to view coordinates.
232 const rerun::components::PinholeProjection& _image_from_camera
233 ) && {
236 .value_or_throw();
237 return std::move(*this);
238 }
239
240 /// This method makes it possible to pack multiple `image_from_camera` in a single component batch.
241 ///
242 /// This only makes sense when used in conjunction with `columns`. `with_image_from_camera` should
243 /// be used when logging a single row's worth of data.
246 ) && {
249 .value_or_throw();
250 return std::move(*this);
251 }
252
253 /// Pixel resolution (usually integers) of child image space. Width and height.
254 ///
255 /// Example:
256 /// ```text
257 /// [1920.0, 1440.0]
258 /// ```
259 ///
260 /// `image_from_camera` project onto the space spanned by `(0,0)` and `resolution - 1`.
262 resolution =
263 ComponentBatch::from_loggable(_resolution, Descriptor_resolution).value_or_throw();
264 return std::move(*this);
265 }
266
267 /// This method makes it possible to pack multiple `resolution` in a single component batch.
268 ///
269 /// This only makes sense when used in conjunction with `columns`. `with_resolution` should
270 /// be used when logging a single row's worth of data.
272 ) && {
273 resolution =
274 ComponentBatch::from_loggable(_resolution, Descriptor_resolution).value_or_throw();
275 return std::move(*this);
276 }
277
278 /// Sets the view coordinates for the camera.
279 ///
280 /// All common values are available as constants on the `components::ViewCoordinates` class.
281 ///
282 /// The default is `ViewCoordinates::RDF`, i.e. X=Right, Y=Down, Z=Forward, and this is also the recommended setting.
283 /// This means that the camera frustum will point along the positive Z axis of the parent space,
284 /// and the cameras "up" direction will be along the negative Y axis of the parent space.
285 ///
286 /// The camera frustum will point whichever axis is set to `F` (or the opposite of `B`).
287 /// When logging a depth image under this entity, this is the direction the point cloud will be projected.
288 /// With `RDF`, the default forward is +Z.
289 ///
290 /// The frustum's "up" direction will be whichever axis is set to `U` (or the opposite of `D`).
291 /// This will match the negative Y direction of pixel space (all images are assumed to have xyz=RDF).
292 /// With `RDF`, the default is up is -Y.
293 ///
294 /// The frustum's "right" direction will be whichever axis is set to `R` (or the opposite of `L`).
295 /// This will match the positive X direction of pixel space (all images are assumed to have xyz=RDF).
296 /// With `RDF`, the default right is +x.
297 ///
298 /// Other common formats are `RUB` (X=Right, Y=Up, Z=Back) and `FLU` (X=Forward, Y=Left, Z=Up).
299 ///
300 /// NOTE: setting this to something else than `RDF` (the default) will change the orientation of the camera frustum,
301 /// and make the pinhole matrix not match up with the coordinate system of the pinhole entity.
302 ///
303 /// The pinhole matrix (the `image_from_camera` argument) always project along the third (Z) axis,
304 /// but will be re-oriented to project along the forward axis of the `camera_xyz` argument.
306 camera_xyz =
307 ComponentBatch::from_loggable(_camera_xyz, Descriptor_camera_xyz).value_or_throw();
308 return std::move(*this);
309 }
310
311 /// This method makes it possible to pack multiple `camera_xyz` in a single component batch.
312 ///
313 /// This only makes sense when used in conjunction with `columns`. `with_camera_xyz` should
314 /// be used when logging a single row's worth of data.
317 ) && {
318 camera_xyz =
319 ComponentBatch::from_loggable(_camera_xyz, Descriptor_camera_xyz).value_or_throw();
320 return std::move(*this);
321 }
322
323 /// The distance from the camera origin to the image plane when the projection is shown in a 3D viewer.
324 ///
325 /// This is only used for visualization purposes, and does not affect the projection itself.
327 const rerun::components::ImagePlaneDistance& _image_plane_distance
328 ) && {
330 _image_plane_distance,
332 )
333 .value_or_throw();
334 return std::move(*this);
335 }
336
337 /// This method makes it possible to pack multiple `image_plane_distance` in a single component batch.
338 ///
339 /// This only makes sense when used in conjunction with `columns`. `with_image_plane_distance` should
340 /// be used when logging a single row's worth of data.
342 const Collection<rerun::components::ImagePlaneDistance>& _image_plane_distance
343 ) && {
345 _image_plane_distance,
347 )
348 .value_or_throw();
349 return std::move(*this);
350 }
351
352 /// Partitions the component data into multiple sub-batches.
353 ///
354 /// Specifically, this transforms the existing `ComponentBatch` data into `ComponentColumn`s
355 /// instead, via `ComponentBatch::partitioned`.
356 ///
357 /// This makes it possible to use `RecordingStream::send_columns` to send columnar data directly into Rerun.
358 ///
359 /// The specified `lengths` must sum to the total length of the component batch.
361
362 /// Partitions the component data into unit-length sub-batches.
363 ///
364 /// This is semantically similar to calling `columns` with `std::vector<uint32_t>(n, 1)`,
365 /// where `n` is automatically guessed.
367 };
368
369} // namespace rerun::archetypes
370
371namespace rerun {
372 /// \private
373 template <typename T>
374 struct AsComponents;
375
376 /// \private
377 template <>
378 struct AsComponents<archetypes::Pinhole> {
379 /// Serialize all set component batches.
380 static Result<Collection<ComponentBatch>> as_batches(const archetypes::Pinhole& archetype);
381 };
382} // 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:79
Pinhole with_resolution(const rerun::components::Resolution &_resolution) &&
Pixel resolution (usually integers) of child image space.
Definition pinhole.hpp:261
static constexpr const char ArchetypeName[]
The name of the archetype as used in ComponentDescriptors.
Definition pinhole.hpp:133
Pinhole with_resolution(float width, float height) &&
Pixel resolution (usually integers) of child image space.
Definition pinhole.hpp:196
static constexpr auto Descriptor_resolution
ComponentDescriptor for the resolution field.
Definition pinhole.hpp:141
std::optional< ComponentBatch > image_from_camera
Camera projection, from image coordinates to view coordinates.
Definition pinhole.hpp:81
Pinhole with_resolution(int width, int height) &&
Pixel resolution (usually integers) of child image space.
Definition pinhole.hpp:203
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:305
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:341
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:315
static Pinhole update_fields()
Update only some specific fields of a Pinhole.
Definition pinhole.hpp:223
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:125
static constexpr auto Descriptor_camera_xyz
ComponentDescriptor for the camera_xyz field.
Definition pinhole.hpp:146
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:183
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:151
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:120
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:174
std::optional< ComponentBatch > resolution
Pixel resolution (usually integers) of child image space.
Definition pinhole.hpp:91
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:244
Pinhole with_image_from_camera(const rerun::components::PinholeProjection &_image_from_camera) &&
Camera projection, from image coordinates to view coordinates.
Definition pinhole.hpp:231
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:271
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:326
static constexpr auto Descriptor_image_from_camera
ComponentDescriptor for the image_from_camera field.
Definition pinhole.hpp:136
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:33
Datatype: A vector in 2D space.
Definition vec2d.hpp:21