VolViz
A volume visualization tool
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
Camera.h
Go to the documentation of this file.
1 #ifndef VolViz_Camera_h
2 #define VolViz_Camera_h
3 
4 #include "AtomicCache.h"
5 #include "AtomicWrapper.h"
6 #include "Types.h"
7 
8 namespace VolViz {
9 
10 namespace Private_ {
11 class CameraClient;
12 } // namespace Private_
13 
20 class Camera {
21  template <class T> using Property = AtomicWrapper<T, SetAndNotifyPolicy>;
22 
23 public:
24  Camera() noexcept;
25 
27  Property<Orientation> orientation{Orientation::Identity()};
28 
32  PhysicalPosition{0 * milli * meter, 0 * milli *meter, 0 * milli *meter}};
33 
35  Property<Angle> verticalFieldOfView{110 * degree_angle};
36 
39 
45  Private_::CameraClient client() const noexcept;
46 
50  inline DepthRange depthRange() const noexcept { return {1.f, 0.f}; }
51 
52 private:
53  friend class Private_::CameraClient;
54 
56  Matrix4 projectionMatrix() const noexcept;
57 
59  Matrix4 viewMatrix() const noexcept;
60 
62  Matrix4 viewProjectionMatrix() const noexcept;
63 
73  Position unproject(Position2 const &screenPos, float depth,
74  Length ambientScale) const noexcept;
75 
78  [this]() { return projectionMatrix(); }};
79 
82  [this]() { return viewMatrix(); }};
83 
86  [this]() { return viewProjectionMatrix(); }};
87 
90  [this]() { return static_cast<Angle>(verticalFieldOfView); }};
91 
94 };
95 
96 namespace Private_ {
97 class VisualizerImpl;
98 class AxisAlignedPlane;
99 class Cube;
100 class Mesh;
101 
103  friend class VisualizerImpl;
104  friend class ::VolViz::Camera;
105 
106  // All geometry subclasses should also have access. This smells like a dirty
107  // workaround. TODO: find a better solution
108  friend class AxisAlignedPlane;
109  friend class Cube;
110  friend class Mesh;
111 
112  CameraClient(Camera const &cam) : cam_(cam) {}
113 
115  inline Matrix4 projectionMatrix() const noexcept {
117  }
118 
120  inline Matrix4 viewMatrix(Length ambientScale) const noexcept {
121  using std::abs;
122  using namespace literals;
123 
124  Expects(ambientScale > 0_mm);
125 
126  if (abs(ambientScale - cam_.cachedScale_) > 1e-3_nm) {
129  cam_.cachedScale_ = ambientScale;
130  }
131  return cam_.cachedViewMatrix_;
132  }
133 
135  inline Matrix4 viewProjectionMatrix(Length ambientScale) const noexcept {
136  using std::abs;
137  using namespace literals;
138 
139  Expects(ambientScale > 0_mm);
140 
141  if (abs(ambientScale - cam_.cachedScale_) > 1e-3_nm) {
144  cam_.cachedScale_ = ambientScale;
145  }
147  }
148 
158  inline Position unproject(Position2 const &screenPos, float depth,
159  Length ambientScale) const noexcept {
160  return cam_.unproject(screenPos, depth, ambientScale);
161  }
162 
169  inline Position project(Position const &position, Length ambientScale) const
170  noexcept {
171 
172  PositionH const projPos =
173  viewProjectionMatrix(ambientScale) * position.homogeneous();
174 
175  auto const w = projPos.w();
176 
177  return projPos.head<3>() / w;
178  }
179 
180  Camera const &cam_;
181 };
182 
183 } // namespace Private_
184 
185 } // namespace VolViz
186 
187 #endif // VolViz_Camera_h
void markAsDirty() noexcept
Definition: AtomicCache.h:38
Eigen::Matrix< Length, 3, 1 > PhysicalPosition
Definition: Types.h:66
Definition: AtomicWrapper.h:48
Property< float > aspectRatio
Aspect ratio (width / height) or horizontal FOV / vertical FOV.
Definition: Camera.h:38
Eigen::Quaternionf Orientation
6-DOF orientation, represented as a quaternion
Definition: Types.h:54
Definition: Types.h:56
Length cachedScale_
Cached scale of last call to viewMatrix(...)
Definition: Camera.h:93
Definition: Mesh.h:13
GLint GLint GLsizei GLsizei GLsizei depth
Definition: glad.h:2276
GLubyte GLubyte GLubyte GLubyte w
Definition: glad.h:2643
Eigen::Vector3f Position
Position in 3D euclidean space.
Definition: Types.h:21
Property< Angle > verticalFieldOfView
Vertical field of view in rad.
Definition: Camera.h:35
Private_::CameraClient client() const noexcept
Definition: Camera.cpp:5
Definition: Camera.h:102
Camera() noexcept
Definition: Camera.cpp:9
Definition: Camera.h:20
AtomicCache< Angle > cachedVerticalFOV_
Cached vertical FOV.
Definition: Camera.h:89
CameraClient(Camera const &cam)
Definition: Camera.h:112
Matrix4 projectionMatrix() const noexcept
Returns the camera's projection matrix.
Definition: Camera.h:115
double Angle
Definition: Types.h:65
Matrix4 viewMatrix() const noexcept
Returns the camera's view matrix, i.e. the inverse camera transform.
Definition: Camera.cpp:56
Definition: Cube.h:11
Definition: AtomicCache.h:22
Matrix4 viewProjectionMatrix(Length ambientScale) const noexcept
Returns the product of projectionMatrix() * viewMatrix(...)
Definition: Camera.h:135
Eigen::Matrix4f Matrix4
Definition: Types.h:18
Property< PhysicalPosition > position
Definition: Camera.h:31
Matrix4 viewMatrix(Length ambientScale) const noexcept
Returns the camera's view matrix, i.e. the inverse camera transform.
Definition: Camera.h:120
AtomicCache< Matrix4 > cachedProjectionMatrix_
Cached projection matrix.
Definition: Camera.h:77
Position unproject(Position2 const &screenPos, float depth, Length ambientScale) const noexcept
Definition: Camera.cpp:79
Matrix4 projectionMatrix() const noexcept
Returns the camera's projection matrix.
Definition: Camera.cpp:32
Property< Orientation > orientation
The camera's orientation.
Definition: Camera.h:27
Definition: VisualizerImpl.h:29
Camera const & cam_
Definition: Camera.h:180
phys::units::quantity< phys::units::length_d > Length
Definition: Types.h:64
Eigen::Vector2f Position2
Position in 2D space.
Definition: Types.h:26
DepthRange depthRange() const noexcept
Definition: Camera.h:50
Position project(Position const &position, Length ambientScale) const noexcept
Definition: Camera.h:169
AtomicCache< Matrix4 > cachedViewMatrix_
Cached view Matrix.
Definition: Camera.h:81
Matrix4 viewProjectionMatrix() const noexcept
Returns the product of projectionMatrix() * viewMatrix(...)
Definition: Camera.cpp:72
Definition: AxisAlignedPlane.h:10
AtomicCache< Matrix4 > cachedViewProjectionMatrix_
Cached product viewMatrix * projectionMatrix.
Definition: Camera.h:85
Eigen::Vector4f PositionH
Position in homogenous coordinates.
Definition: Types.h:23
Position unproject(Position2 const &screenPos, float depth, Length ambientScale) const noexcept
Definition: Camera.h:158