UFO 1.0.0
An Efficient Probabilistic 3D Mapping Framework That Embraces the Unknown
Loading...
Searching...
No Matches
orthogonal.hpp
1
46#ifndef UFO_VISION_CAMERA_ORTHOGONAL_HPP
47#define UFO_VISION_CAMERA_ORTHOGONAL_HPP
48
49// UFO
50#include <ufo/execution/algorithm.hpp>
51#include <ufo/execution/execution.hpp>
52#include <ufo/geometry/ray.hpp>
53#include <ufo/numeric/mat.hpp>
54#include <ufo/numeric/transform.hpp>
55#include <ufo/numeric/vec.hpp>
56#include <ufo/vision/camera/orthogonal_intrinsics.hpp>
57#include <ufo/vision/image.hpp>
58
59// STL
60#include <cmath>
61#include <cstddef>
62#include <format>
63#include <ostream>
64#include <utility>
65
66namespace ufo
67{
94 float near_clip;
98 float far_clip;
99
106 void lookAt(Vec3f center, Vec3f const& target, Vec3f const& up)
107 {
108 pose = Transform3f{inverse(ufo::lookAt(center, target, up))};
109 }
110
111 [[nodiscard]] Mat4f projection() const
112 {
113 float const half_w = intrinsics.width / 2.0f;
114 float const half_h = intrinsics.height / 2.0f;
115 Mat4f m = orthogonal(-half_w, half_w, -half_h, half_h, near_clip, far_clip);
116 m[0][0] *= intrinsics.zoom;
117 m[1][1] *= intrinsics.zoom;
118 return m;
119 }
120
121 [[nodiscard]] Mat4f view() const { return Mat4f(pose); }
122
123 [[nodiscard]] Image<Ray3f> rays() const { return rays(ufo::execution::seq); }
124
125 template <class ExecutionPolicy>
126 [[nodiscard]] Image<Ray3f> rays(ExecutionPolicy&& policy) const
127 {
128 auto const proj_inv = inverse(projection());
129 auto const view_inv = view();
130 Image<Ray3f> rays(intrinsics.rows, intrinsics.cols);
131
132 ufo::for_each(std::forward<ExecutionPolicy>(policy), std::size_t{}, intrinsics.rows,
133 [&rays, &proj_inv, &view_inv](std::size_t y) {
134 float const vy = ((y + 0.5f) / rays.rows()) * 2.0f - 1.0f;
135 for (std::size_t x{}; rays.cols() > x; ++x) {
136 float const vx = ((x + 0.5f) / rays.cols()) * 2.0f - 1.0f;
137 Vec3f origin(ufo::convert<Vec3f>(view_inv * proj_inv *
138 Vec4f(vx, vy, 0.0f, 1.0f)));
139 Vec3f direction(
140 ufo::convert<Vec3f>(view_inv * Vec4f(0.0f, 0.0f, 1.0f, 0.0f)));
141 rays[y, x] = {origin, direction};
142 }
143 });
144 return rays;
145 }
146
147 [[nodiscard]] friend bool operator==(OrthogonalCamera const&,
148 OrthogonalCamera const&) noexcept = default;
149};
150
154inline std::ostream& operator<<(std::ostream& os, OrthogonalCamera const& camera)
155{
156 os << "Pose: " << camera.pose << '\n'
157 << camera.intrinsics << "\nNear clip: " << camera.near_clip
158 << ", Far clip: " << camera.far_clip << '\n';
159 return os;
160}
161
165} // namespace ufo
166
167template <>
168struct std::formatter<ufo::OrthogonalCamera> {
169 constexpr auto parse(std::format_parse_context& ctx) const { return ctx.begin(); }
170
171 auto format(ufo::OrthogonalCamera const& camera, std::format_context& ctx) const
172 {
173 return std::format_to(ctx.out(), "Pose: {}\n{}\nNear clip: {}, Far clip: {}",
174 camera.pose, camera.intrinsics, camera.near_clip,
175 camera.far_clip);
176 }
177};
178
179#endif // UFO_VISION_CAMERA_ORTHOGONAL_HPP
Mat< 4, 4, T > orthogonal(T left, T right, T bottom, T top)
Builds a right-handed 2-D orthographic projection matrix (no depth clipping).
Definition mat.hpp:1359
All vision-related classes and functions.
Definition cloud.hpp:49
constexpr UnaryFunc for_each(Index first, Index last, UnaryFunc f, Proj proj={})
Applies f to each integer index in [first, last) sequentially.
Definition algorithm.hpp:80
constexpr M inverse(M const &m) noexcept
Computes the inverse of a square floating-point matrix.
Definition mat.hpp:1097
Mat< 4, 4, T > lookAt(Vec< 3, T > const &eye, Vec< 3, T > const &target, Vec< 3, T > const &up)
Builds a view matrix (OpenCV convention).
Definition mat.hpp:1471
Orthogonal camera model for ray-generation and projection.
Transform3f pose
Camera-to-world pose transform.
void lookAt(Vec3f center, Vec3f const &target, Vec3f const &up)
Orients the camera so it looks from center toward target.
OrthogonalIntrinsics intrinsics
The intrinsic parameters of the camera.
float near_clip
Distance to the near clipping plane.
float far_clip
Distance to the far clipping plane.
Stores the intrinsic parameters of an orthogonal camera.
float height
Viewport height in world-space units (e.g., meters).
std::size_t rows
Image height in pixels.
float width
Viewport width in world-space units (e.g., meters).
std::size_t cols
Image width in pixels.
Rigid-body transform: a rotation matrix plus a translation vector.
Definition transform.hpp:81