Visual Computing Library  devel
Loading...
Searching...
No Matches
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_SPACE_CORE_CAMERA_H
24#define VCL_SPACE_CORE_CAMERA_H
25
26#include "camera/matrix.h"
27
28namespace vcl {
29
35template<typename Scalar>
36class Camera
37{
38public:
39 using ScalarType = Scalar;
42
43 enum class ProjectionMode { ORTHO, PERSPECTIVE };
44
45private:
46 /* Extrinsics */
47
49 PointType mCenter = PointType(0.0f, 0.0f, 0.0f);
50
52 PointType mEye = PointType(0.0f, 0.0f, 1.0f);
53
55 PointType mUp = PointType(0.0f, 1.0f, 0.0f);
56
57 /* Intrinsics */
58
60 Scalar mFovDeg = 54.0;
61
63 ProjectionMode mProjectionMode = ProjectionMode::PERSPECTIVE;
64
68 // |\___
69 // | \___
70 // h/2 | \___
71 // | \__
72 // | \__
73 //--- target ---------------X- eye --
74 // | __/
75 // | __/
76 // h/2 | __/
77 // | __/
78 // | __/
79 // | /
80 Scalar mVerticalHeight = 2.0;
81
83 Scalar mAspect = 1.0;
84
86 Scalar mNear = 0.1;
87
89 Scalar mFar = 500.0;
90
91public:
92 Camera() { setFieldOfViewAdaptingEyeDistance(mFovDeg); };
93
94 void reset() { *this = {}; }
95
96 PointType& center() { return mCenter; }
97
98 const PointType& center() const { return mCenter; }
99
100 PointType& eye() { return mEye; }
101
102 const PointType& eye() const { return mEye; }
103
104 PointType& up() { return mUp; }
105
106 const PointType& up() const { return mUp; }
107
108 Scalar& fieldOfView() { return mFovDeg; }
109
110 const Scalar& fieldOfView() const { return mFovDeg; }
111
112 void setFieldOfViewAdaptingEyeDistance(const Scalar& fovDeg)
113 {
114 mFovDeg = fovDeg;
115 PointType targetToEye = (mEye - mCenter).normalized();
116 mEye =
117 mCenter + targetToEye * ((mVerticalHeight / 2.0) /
118 std::tan((fovDeg / 2.0) / 180.0 * M_PI));
119 }
120
121 ProjectionMode& projectionMode() { return mProjectionMode; }
122
123 ProjectionMode projectionMode() const { return mProjectionMode; }
124
125 Scalar& verticalHeight() { return mVerticalHeight; }
126
127 const Scalar& verticalHeight() const { return mVerticalHeight; }
128
129 Scalar& aspectRatio() { return mAspect; }
130
131 const Scalar& aspectRatio() const { return mAspect; }
132
133 Scalar& nearPlane() { return mNear; }
134
135 const Scalar& nearPlane() const { return mNear; }
136
137 Scalar& farPlane() { return mFar; }
138
139 const Scalar& farPlane() const { return mFar; }
140
141 MatrixType viewMatrix() const
142 {
143 MatrixType res;
144 detail::lookAtMatrix(res.data(), mEye, mCenter, mUp);
145 return res;
146 }
147
148 MatrixType projectionMatrix() const
149 {
150 MatrixType res;
151 switch (mProjectionMode) {
152 case ProjectionMode::ORTHO: {
153 const Scalar h = mVerticalHeight / 2.0;
154 const Scalar w = h * mAspect;
155 detail::orthoProjectionMatrix(
156 res.data(), -w, w, h, -h, mNear, mFar, false);
157 break;
158 }
159 case ProjectionMode::PERSPECTIVE: {
160 detail::projectionMatrix(
161 res.data(), mFovDeg, mAspect, mNear, mFar, false);
162 break;
163 }
164 default: assert(false); return MatrixType::Identity();
165 }
166 return res;
167 }
168
173 void serialize(std::ostream& os) const
174 {
175 mCenter.serialize(os);
176 mEye.serialize(os);
177 mUp.serialize(os);
178 vcl::serialize(
179 os,
180 mFovDeg,
183 mAspect,
184 mNear,
185 mFar);
186 }
187
192 void deserialize(std::istream& is)
193 {
194 mCenter.deserialize(is);
195 mEye.deserialize(is);
196 mUp.deserialize(is);
197 vcl::deserialize(
198 is,
199 mFovDeg,
202 mAspect,
203 mNear,
204 mFar);
205 }
206};
207
208/* Specialization Aliases */
209using Cameraf = Camera<float>;
210
211/* Concepts */
212
223template<typename T>
224concept GenericCameraConcept = requires (T&& c) {
225 { c.viewMatrix() } -> Matrix44Concept;
226 { c.projectionMatrix() } -> Matrix44Concept;
227};
228
239template<typename T>
240concept CameraConcept = std::derived_from< // same type or derived type
241 std::remove_cvref_t<T>,
242 Camera<typename RemoveRef<T>::ScalarType>>;
243
244} // namespace vcl
245
246#endif // VCL_SPACE_CORE_CAMERA_H
A Pinhole camera model.
Definition camera.h:37
Scalar mNear
Z position of the mNear plane.
Definition camera.h:86
Scalar mVerticalHeight
Height of the target in world space (used for ortho projection, and adapting the eye distance for per...
Definition camera.h:80
Scalar mFovDeg
Field of view in degrees.
Definition camera.h:60
void serialize(std::ostream &os) const
Serializes the camera to the given output stream.
Definition camera.h:173
ProjectionMode mProjectionMode
Projection mode.
Definition camera.h:63
PointType mCenter
Position where the camera is looking at (i.e. target point)
Definition camera.h:49
void deserialize(std::istream &is)
Deserializes the camera from the given input stream.
Definition camera.h:192
Scalar mFar
Z position of the mFar plane.
Definition camera.h:89
PointType mUp
Up direction of the camera.
Definition camera.h:55
Scalar mAspect
Aspect ratio.
Definition camera.h:83
PointType mEye
Position of (eye of) the camera.
Definition camera.h:52
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:41
Segment()
Default constructor. Creates a segment with endpoints at the origin.
Definition segment.h:66
A concept representing a Camera.
Definition camera.h:240
A concept representing a generic Camera.
Definition camera.h:224
Concept for 4x4 matrices.
Definition matrix.h:62