Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
Camera3d.hpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
10#pragma once
11
18
23#include "shambackends/math.hpp"
24#include "shambackends/vec.hpp"
25#include "shammath/AABB.hpp"
26#include "shammath/matrix.hpp"
27#include <cmath>
28#include <vector>
29
30namespace shammath {
31
45 template<class Tvec>
46 class Camera3d {
47 public:
48 using Tscal = shambase::VecComponent<Tvec>;
49
64 Camera3d(Tvec pos, Tvec dir, Tvec up, u32 nx, u32 ny, Tscal fovy, Tscal znear, Tscal zfar)
65 : Camera3d(pos, dir, up, nx, ny, Tscal(nx) / Tscal(ny), fovy, znear, zfar) {}
66
76 Tvec pos,
77 Tvec dir,
78 Tvec up,
79 u32 nx,
80 Tscal aspect,
81 Tscal fovy,
82 Tscal znear,
83 Tscal zfar) {
84 if (!(aspect > 0)) {
86 sham::format("Camera3d: aspect must be > 0, got aspect = {}", aspect));
87 }
88 u32 ny = static_cast<u32>(sycl::max(Tscal(1), std::round(Tscal(nx) / aspect)));
89 return Camera3d(pos, dir, up, nx, ny, aspect, fovy, znear, zfar);
90 }
91
101 Tvec pos, Tvec target, Tvec up, u32 nx, u32 ny, Tscal fovy, Tscal znear, Tscal zfar) {
102 if (sham::equals(pos, target)) {
104 "Camera3d: target must differ from pos, got pos = target = {}", pos));
105 }
106 return Camera3d(pos, target - pos, up, nx, ny, fovy, znear, zfar);
107 }
108
109 inline Tvec get_pos() const { return pos; }
111 inline Tvec get_dir() const { return dir; }
113 inline Tvec get_up() const { return up; }
115 inline Tvec get_right() const { return right; }
116 inline u32 get_nx() const { return nx; }
117 inline u32 get_ny() const { return ny; }
118 inline Tscal get_aspect() const { return aspect; }
119 inline Tscal get_fovy() const { return fovy; }
120 inline Tscal get_znear() const { return znear; }
121 inline Tscal get_zfar() const { return zfar; }
122
127 inline Ray<Tvec> get_pixel_ray(u32 ix, u32 iy) const {
128 Tscal tan_half = sycl::tan(fovy / 2);
129 Tscal ndc_x = Tscal(2) * (Tscal(ix) + Tscal(0.5)) / Tscal(nx) - Tscal(1);
130 Tscal ndc_y = Tscal(2) * (Tscal(iy) + Tscal(0.5)) / Tscal(ny) - Tscal(1);
131
132 // camera space direction (ndc_x * tan_half * aspect, ndc_y * tan_half, -1)
133 Tvec d = dir + right * (ndc_x * tan_half * aspect) + up * (ndc_y * tan_half);
134
135 // the component of d along dir is 1, so this point lies on the near plane
136 return Ray<Tvec>(pos + d * znear, d);
137 }
138
140 inline std::vector<Ray<Tvec>> get_rays() const {
141 std::vector<Ray<Tvec>> rays;
142 rays.reserve(size_t(nx) * size_t(ny));
143 for (u32 iy = 0; iy < ny; iy++) {
144 for (u32 ix = 0; ix < nx; ix++) {
145 rays.push_back(get_pixel_ray(ix, iy));
146 }
147 }
148 return rays;
149 }
150
153 mat<Tscal, 4, 4> view{};
154
155 view(0, 0) = right.x();
156 view(0, 1) = right.y();
157 view(0, 2) = right.z();
158 view(0, 3) = -sycl::dot(right, pos);
159
160 view(1, 0) = up.x();
161 view(1, 1) = up.y();
162 view(1, 2) = up.z();
163 view(1, 3) = -sycl::dot(up, pos);
164
165 view(2, 0) = -dir.x();
166 view(2, 1) = -dir.y();
167 view(2, 2) = -dir.z();
168 view(2, 3) = sycl::dot(dir, pos);
169
170 view(3, 3) = 1;
171
172 return view;
173 }
174
177 mat<Tscal, 4, 4> view_inv{};
178
179 view_inv(0, 0) = right.x();
180 view_inv(1, 0) = right.y();
181 view_inv(2, 0) = right.z();
182
183 view_inv(0, 1) = up.x();
184 view_inv(1, 1) = up.y();
185 view_inv(2, 1) = up.z();
186
187 view_inv(0, 2) = -dir.x();
188 view_inv(1, 2) = -dir.y();
189 view_inv(2, 2) = -dir.z();
190
191 view_inv(0, 3) = pos.x();
192 view_inv(1, 3) = pos.y();
193 view_inv(2, 3) = pos.z();
194
195 view_inv(3, 3) = 1;
196
197 return view_inv;
198 }
199
202 mat<Tscal, 4, 4> proj{};
203
204 Tscal f = Tscal(1) / sycl::tan(fovy / 2);
205
206 proj(0, 0) = f / aspect;
207 proj(1, 1) = f;
208 proj(2, 2) = (zfar + znear) / (znear - zfar);
209 proj(2, 3) = Tscal(2) * zfar * znear / (znear - zfar);
210 proj(3, 2) = -1;
211
212 return proj;
213 }
214
218 mat<Tscal, 4, 4> proj_inv{};
219
220 proj_inv(0, 0) = Tscal(1) / proj(0, 0);
221 proj_inv(1, 1) = Tscal(1) / proj(1, 1);
222 proj_inv(2, 3) = -1;
223 proj_inv(3, 2) = Tscal(1) / proj(2, 3);
224 proj_inv(3, 3) = proj(2, 2) / proj(2, 3);
225
226 return proj_inv;
227 }
228
234 mat_prod(proj.get_mdspan(), view.get_mdspan(), ret.get_mdspan());
235 return ret;
236 }
237
243 mat_prod(view_inv.get_mdspan(), proj_inv.get_mdspan(), ret.get_mdspan());
244 return ret;
245 }
246
247 private:
248 Tvec pos;
249 Tvec dir;
250 Tvec up;
251 Tvec right;
252
253 u32 nx;
254 u32 ny;
255
256 Tscal aspect;
257 Tscal fovy;
258 Tscal znear;
259 Tscal zfar;
260
261 Camera3d(
262 Tvec pos,
263 Tvec dir,
264 Tvec up,
265 u32 nx,
266 u32 ny,
267 Tscal aspect,
268 Tscal fovy,
269 Tscal znear,
270 Tscal zfar)
271 : pos(pos), nx(nx), ny(ny), aspect(aspect), fovy(fovy), znear(znear), zfar(zfar) {
272
273 auto throw_invalid = [&](const std::string &reason) {
275 "Camera3d: {}\n"
276 " args :\n"
277 " pos = {}\n"
278 " dir = {}\n"
279 " up = {}\n"
280 " nx = {}\n"
281 " ny = {}\n"
282 " aspect = {}\n"
283 " fovy = {}\n"
284 " znear = {}\n"
285 " zfar = {}",
286 reason,
287 pos,
288 dir,
289 up,
290 nx,
291 ny,
292 aspect,
293 fovy,
294 znear,
295 zfar));
296 };
297
298 if (nx == 0 || ny == 0) {
299 throw_invalid("nx and ny must be > 0");
300 }
301 if (!(aspect > 0)) {
302 throw_invalid("aspect must be > 0");
303 }
304 if (!(fovy > 0 && fovy < shambase::constants::pi<Tscal>) ) {
305 throw_invalid("fovy must be in (0, pi)");
306 }
307 if (!(znear > 0 && zfar > znear)) {
308 throw_invalid("znear and zfar must satisfy 0 < znear < zfar");
309 }
310
311 Tscal len_dir = sycl::length(dir);
312 if (!(len_dir > 0)) {
313 throw_invalid("dir must be non zero");
314 }
315 this->dir = dir / len_dir;
316
317 Tvec r = sycl::cross(this->dir, up);
318 Tscal len_r = sycl::length(r);
319 if (!(len_r > 0)) {
320 throw_invalid("up must be non zero and not colinear with dir");
321 }
322 this->right = r / len_r;
323 this->up = sycl::cross(this->right, this->dir);
324 }
325 };
326
327} // namespace shammath
std::uint32_t u32
32 bit unsigned integer
Tvec get_dir() const
Normalized viewing direction.
Definition Camera3d.hpp:111
mat< Tscal, 4, 4 > get_camera_transform() const
Full camera transform (world -> clip space), projection * view.
Definition Camera3d.hpp:230
Tvec get_up() const
Normalized up direction, orthogonal to dir.
Definition Camera3d.hpp:113
Ray< Tvec > get_pixel_ray(u32 ix, u32 iy) const
Ray going through the center of pixel (ix, iy).
Definition Camera3d.hpp:127
static Camera3d from_aspect(Tvec pos, Tvec dir, Tvec up, u32 nx, Tscal aspect, Tscal fovy, Tscal znear, Tscal zfar)
Construct a camera from the horizontal resolution and the aspect ratio.
Definition Camera3d.hpp:75
mat< Tscal, 4, 4 > get_camera_transform_inv() const
Inverse of the full camera transform (clip -> world space).
Definition Camera3d.hpp:239
mat< Tscal, 4, 4 > get_view_matrix_inv() const
Inverse of the view matrix (camera -> world space).
Definition Camera3d.hpp:176
Tvec get_right() const
Normalized right direction dir x up.
Definition Camera3d.hpp:115
std::vector< Ray< Tvec > > get_rays() const
Rays of all the pixels, pixel (ix, iy) being at index iy * nx + ix.
Definition Camera3d.hpp:140
mat< Tscal, 4, 4 > get_projection_matrix() const
Projection matrix (camera -> clip space), equivalent to gluPerspective.
Definition Camera3d.hpp:201
static Camera3d look_at(Tvec pos, Tvec target, Tvec up, u32 nx, u32 ny, Tscal fovy, Tscal znear, Tscal zfar)
Construct a camera looking at a target point (gluLookAt convention).
Definition Camera3d.hpp:100
mat< Tscal, 4, 4 > get_projection_matrix_inv() const
Inverse of the projection matrix (clip -> camera space).
Definition Camera3d.hpp:216
Camera3d(Tvec pos, Tvec dir, Tvec up, u32 nx, u32 ny, Tscal fovy, Tscal znear, Tscal zfar)
Construct a camera from the image size in pixels.
Definition Camera3d.hpp:64
mat< Tscal, 4, 4 > get_view_matrix() const
View matrix (world -> camera space), equivalent to gluLookAt.
Definition Camera3d.hpp:152
Matrix class based on std::array storage and mdspan.
Definition matrix.hpp:37
constexpr auto get_mdspan()
Get the matrix data as a mdspan.
Definition matrix.hpp:43
Class holding the value of numerous constants generated from the following source.
This header file contains utility functions related to exception handling in the code.
Define the fmt formatters for sycl::vec.
Core formatting functions: format, vformat, and format_printf.
ExcptTypes make_except_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Create an exception with a message and a location.
namespace for math utility
Definition AABB.hpp:26
void mat_prod(const std::mdspan< Ta, Extents1, Layout1, Accessor1 > &input1, const std::mdspan< Ta, Extents2, Layout2, Accessor2 > &input2, const std::mdspan< Tb, Extents3, Layout3, Accessor3 > &output)
Compute the product of two matrices.
Ray representation for intersection testing.
Definition AABB.hpp:34