Visual Computing Library  devel
Loading...
Searching...
No Matches
matrix_camera.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2026 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_ALGORITHMS_CORE_MATRIX_CAMERA_H
24#define VCL_ALGORITHMS_CORE_MATRIX_CAMERA_H
25
26#include <vclib/space/core.h>
27
28namespace vcl {
29
46template<Point3Concept PointType>
48 auto* res,
49 const PointType& eye,
50 const PointType& center,
51 const PointType& up,
52 Handedness handedness = RIGHT_HAND)
53{
54 detail::lookAtMatrix(res, eye, center, up, handedness);
55}
56
72template<MatrixConcept Matrix44, Point3Concept PointType>
74 const PointType& eye,
75 const PointType& center,
76 const PointType& up,
77 Handedness handedness = RIGHT_HAND)
78{
79 Matrix44 res(4, 4);
80 lookAtMatrix(res.data(), eye, center, up, handedness);
81 return res;
82}
83
99template<Point3Concept PointType>
101 auto* res,
102 const PointType& eye,
103 const PointType& center,
104 const PointType& up)
105{
106 lookAtMatrix(res, eye, center, up, LEFT_HAND);
107}
108
123template<MatrixConcept Matrix44, Point3Concept PointType>
125 const PointType& eye,
126 const PointType& center,
127 const PointType& up)
128{
129 Matrix44 res(4, 4);
130 lookAtMatrix(res.data(), eye, center, up, LEFT_HAND);
131 return res;
132}
133
134template<typename Scalar>
135void projectionMatrix(
136 auto* res,
137 Scalar fov,
138 Scalar aspect,
139 Scalar nearPlane,
140 Scalar farPlane,
141 bool homogeneousNDC,
142 Handedness handedness = RIGHT_HAND)
143{
144 detail::projectionMatrix(
145 res, fov, aspect, nearPlane, farPlane, homogeneousNDC, handedness);
146}
147
148template<MatrixConcept Matrix44, typename Scalar>
149Matrix44 projectionMatrix(
150 Scalar fov,
151 Scalar aspect,
152 Scalar nearPlane,
153 Scalar farPlane,
154 bool homogeneousNDC,
155 Handedness handedness = RIGHT_HAND)
156{
157 Matrix44 res(4, 4);
158 projectionMatrix(
159 res.data(),
160 fov,
161 aspect,
162 nearPlane,
163 farPlane,
164 homogeneousNDC,
165 handedness);
166 return res;
167}
168
169template<typename Scalar>
170void projectionMatrixLeftHanded(
171 auto* res,
172 Scalar fov,
173 Scalar aspect,
174 Scalar nearPlane,
175 Scalar farPlane,
176 bool homogeneousNDC)
177{
178 Scalar h = 1.0 / std::tan(vcl::toRad(fov) * 0.5);
179 Scalar w = h * 1.0 / aspect;
180 projectionMatrix(
181 res, fov, aspect, nearPlane, farPlane, homogeneousNDC, LEFT_HAND);
182}
183
184template<MatrixConcept Matrix44, typename Scalar>
185Matrix44 projectionMatrixLeftHanded(
186 Scalar fov,
187 Scalar aspect,
188 Scalar nearPlane,
189 Scalar farPlane,
190 bool homogeneousNDC)
191{
192 Matrix44 res(4, 4);
193 projectionMatrix(
194 res.data(),
195 fov,
196 aspect,
197 nearPlane,
198 farPlane,
199 homogeneousNDC,
200 LEFT_HAND);
201 return res;
202}
203
204template<typename Scalar>
205void orthoProjectionMatrix(
206 auto* res,
207 Scalar left,
208 Scalar right,
209 Scalar top,
210 Scalar bottom,
211 Scalar nearPlane,
212 Scalar farPlane,
213 bool homogeneousNDC,
214 Handedness handedness = RIGHT_HAND)
215{
216 detail::orthoProjectionMatrix(
217 res,
218 left,
219 right,
220 top,
221 bottom,
222 nearPlane,
223 farPlane,
224 homogeneousNDC,
225 handedness);
226}
227
228template<MatrixConcept Matrix44, typename Scalar>
229Matrix44 orthoProjectionMatrix(
230 Scalar left,
231 Scalar right,
232 Scalar top,
233 Scalar bottom,
234 Scalar nearPlane,
235 Scalar farPlane,
236 bool homogeneousNDC,
237 Handedness handedness = RIGHT_HAND)
238{
239 Matrix44 res(4, 4);
240 orthoProjectionMatrix(
241 res.data(),
242 left,
243 right,
244 top,
245 bottom,
246 nearPlane,
247 farPlane,
248 homogeneousNDC,
249 handedness);
250 return res;
251}
252
270template<MatrixConcept Matrix44, Point3Concept PointType>
272 const PointType& screenPos,
275 bool homogeneousNDC)
276{
277 using Scalar = Matrix44::Scalar;
278 const Matrix44 inv = modelViewProjection.inverse();
280 (screenPos.x() - viewport[0]) / viewport[2] * 2.0 - 1.0,
281 (screenPos.y() - viewport[1]) / viewport[3] * 2.0 - 1.0,
282 homogeneousNDC ? 2.0 * screenPos.z() - 1.0 : screenPos.z(),
283 1.0);
284 p = inv * p;
285 if (p.w() == 0.0) {
286 throw std::runtime_error("unproject: division by zero");
287 }
288 return p.template head<3>() / p.w();
289}
290
291} // namespace vcl
292
293#endif // VCL_ALGORITHMS_CORE_MATRIX_CAMERA_H
Definition matrix.h:34
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:41
void lookAtMatrixLeftHanded(auto *res, const PointType &eye, const PointType &center, const PointType &up)
Creates a left handed look at matrix.
Definition matrix_camera.h:100
PointType unprojectScreenPosition(const PointType &screenPos, const Matrix44 &modelViewProjection, const Point4< typename Matrix44::Scalar > &viewport, bool homogeneousNDC)
Unprojects a screen position to a 3D point.
Definition matrix_camera.h:271
void lookAtMatrix(auto *res, const PointType &eye, const PointType &center, const PointType &up, Handedness handedness=RIGHT_HAND)
Creates a look at matrix.
Definition matrix_camera.h:47
Scalar toRad(const Scalar &deg)
Converts an angle in degrees to radians.
Definition math.h:81