iMSTK
Interactive Medical Simulation Toolkit
imstkCamera.h
1 /*
2 ** This file is part of the Interactive Medical Simulation Toolkit (iMSTK)
3 ** iMSTK is distributed under the Apache License, Version 2.0.
4 ** See accompanying NOTICE for details.
5 */
6 
7 #pragma once
8 
9 #include "imstkMath.h"
10 
11 #include <iostream>
12 
13 namespace imstk
14 {
18 static Mat4d
19 lookAt(const Vec3d& pos, const Vec3d& target, const Vec3d& up)
20 {
21  Mat4d results = Mat4d::Identity();
22  Mat3d R;
23  R.col(2) = (pos - target).normalized();
24  R.col(0) = up.cross(R.col(2)).normalized();
25  R.col(1) = R.col(2).cross(R.col(0));
26  results.topLeftCorner<3, 3>() = R.transpose();
27  results.topRightCorner<3, 1>() = -R.transpose() * pos;
28  results(3, 3) = 1.0;
29 
30  return results;
31 }
32 
36 //static Mat4d
37 //perspective(const double fovY, const double aspect, const double nearZ, const double farZ)
38 //{
39 // Mat4d results;
40 // results.setZero();
41 // const double theta = fovY * 0.5;
42 // const double range = (farZ - nearZ);
43 // const double invTan = 1.0 / tan(theta);
44 //
45 // results(0, 0) = invTan / aspect;
46 // results(1, 1) = invTan;
47 // results(2, 2) = -(nearZ + farZ) / range;
48 // results(3, 2) = -1.0;
49 // results(2, 3) = -2.0 * nearZ * farZ / range;
50 // return results;
51 //}
52 
58 class Camera
59 {
60 public:
61  Camera() = default;
62  virtual ~Camera() = default;
63 
68  friend class VTKRenderer;
69 
74  Mat4d& getView() { return m_view; }
75 
79  const Mat4d& getHMDView() const { return m_hmdView; }
80 
86  Mat4d& getProj() { return m_proj; }
87 
91  const Mat4d& getInvView() { return m_invView; }
92 
96  void setView(const Mat4d& view)
97  {
98  m_viewModified = false;
99  m_view = view;
100  m_invView = m_view.inverse();
101  }
102 
107  double getFieldOfView() const { return m_fieldOfView; }
108 
113  void setFieldOfView(const double fov)
114  {
115  m_fieldOfView = fov;
116  //m_projModified = true;
117  }
118 
124  void setNearZ(const double nearZ) { m_nearZ = nearZ; }
125  double getNearZ() const { return m_nearZ; }
126 
132  void setFarZ(const double farZ) { m_farZ = farZ; }
133 
134  double getFarZ() const { return m_farZ; }
135 
136  virtual void update()
137  {
138  if (m_viewModified)
139  {
141  m_invView = m_view.inverse();
142  m_viewModified = false;
143  }
144  /*if (m_projModified)
145  {
146  m_proj = perspective();
147  }*/
148  }
149 
154  const Vec3d& getPosition() const { return m_position; }
155 
159  void setPosition(const Vec3d& pos)
160  {
161  m_position = pos;
162  m_viewModified = true;
163  }
164 
165  void setPosition(const double x,
166  const double y,
167  const double z)
168  {
169  setPosition(Vec3d(x, y, z));
170  }
171 
177  const Vec3d& getFocalPoint() const { return m_focalPoint; }
178 
182  void setFocalPoint(const Vec3d& focalPt)
183  {
184  m_focalPoint = focalPt;
185  m_viewModified = true;
186  }
187 
188  void setFocalPoint(const double x,
189  const double y,
190  const double z)
191  {
192  setFocalPoint(Vec3d(x, y, z));
193  }
194 
198  const Vec3d& getViewUp() const { return m_viewUp; }
199 
203  Vec3d getForward() const { return m_view.col(2).head<3>(); }
204 
209  Vec3d getEyeRayDir(const Vec2d& ndcPos) const
210  {
211  const Vec4d worldPos = (m_proj * m_view).inverse() * Vec4d(ndcPos[0], ndcPos[1], 0.0, 1.0);
212  return (worldPos.head<3>() / worldPos[3] - m_position).normalized();
213  }
214 
218  void setViewUp(const Vec3d& up)
219  {
220  m_viewUp = up.normalized();
221  m_viewModified = true;
222  }
223 
224  void setViewUp(const double x,
225  const double y,
226  const double z)
227  {
228  setViewUp(Vec3d(x, y, z));
229  }
230 
234  void print();
235 
236 protected:
237  // Base camera values
238  Mat4d m_view = Mat4d::Identity();
239  Mat4d m_invView = Mat4d::Identity();
240  Mat4d m_proj;
241  bool m_viewModified = true;
242 
243  // Base projection parameters
244  double m_fieldOfView = 40.0;
245  double m_nearZ = 0.01;
246  double m_farZ = 1000.0;
247 
248  // Lookat camera parameters
249  Vec3d m_position = Vec3d(0.0, 0.0, 0.0);
250  Vec3d m_focalPoint = -Vec3d::UnitZ();
251  Vec3d m_viewUp = Vec3d::UnitY();
252 
253  // Optional extra view for VR, which gives (HMD_view * m_view)
254  Mat4d m_hmdView = Mat4d::Identity();
255 };
256 } // namespace imstk
void setFieldOfView(const double fov)
Sets the field of view.
Definition: imstkCamera.h:113
Vec3d m_viewUp
camera up vector
Definition: imstkCamera.h:251
Vec3d m_position
camera position
Definition: imstkCamera.h:249
double getFieldOfView() const
Gets the field of view.
Definition: imstkCamera.h:107
const Vec3d & getViewUp() const
Get the up direction of the view.
Definition: imstkCamera.h:198
Mat4d m_invView
Inverse is often needed so we maintain it.
Definition: imstkCamera.h:239
void print()
Utility function to quickly print cam stats.
Definition: imstkCamera.cpp:12
Mat4d & getView()
Get camera view matrix.
Definition: imstkCamera.h:74
void setFarZ(const double farZ)
Set clipping near note: You lose depth accuracy as the range between near and far increases could cau...
Definition: imstkCamera.h:132
const Mat4d & getInvView()
Get the inverse view matrix.
Definition: imstkCamera.h:91
const Vec3d & getFocalPoint() const
Returns the focal point The focal point is the point that the camera points to.
Definition: imstkCamera.h:177
Compound Geometry.
double m_fieldOfView
field of view in degrees
Definition: imstkCamera.h:244
Mat4d m_view
Actual view matrix used.
Definition: imstkCamera.h:238
Mat4d m_proj
Only modifiable through projection parameters (fov,near,far)
Definition: imstkCamera.h:240
double m_farZ
far plane of the camera
Definition: imstkCamera.h:246
Vec3d getEyeRayDir(const Vec2d &ndcPos) const
Compute ray emanating from the camera position that travels through the point in normalized device co...
Definition: imstkCamera.h:209
Mat4d & getProj()
Get camera projection matrix, this matrix will be identity until first render is done.
Definition: imstkCamera.h:86
const Vec3d & getPosition() const
Gets the camera position.
Definition: imstkCamera.h:154
void setPosition(const Vec3d &pos)
Sets the camera position.
Definition: imstkCamera.h:159
void setViewUp(const Vec3d &up)
Set the up vector.
Definition: imstkCamera.h:218
double m_nearZ
near plane of the camera
Definition: imstkCamera.h:245
void setNearZ(const double nearZ)
Set clipping near note: You lose depth accuracy as the range between near and far increases could cau...
Definition: imstkCamera.h:124
Vec3d m_focalPoint
camera focal point
Definition: imstkCamera.h:250
Produces a perpsective transformation matrix.
Definition: imstkCamera.h:58
void setFocalPoint(const Vec3d &focalPt)
Sets the point to look at, point the camera towards.
Definition: imstkCamera.h:182
Vec3d getForward() const
Get the forward/look direction of the view.
Definition: imstkCamera.h:203
Wraps a vtkRenderer.
const Mat4d & getHMDView() const
Get the HMD view, supplied when using VR.
Definition: imstkCamera.h:79
void setView(const Mat4d &view)
Set the camera view matrix.
Definition: imstkCamera.h:96