iMSTK
Interactive Medical Simulation Toolkit
imstkCameraController.cpp
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 #include "imstkCameraController.h"
8 #include "imstkCamera.h"
9 #include "imstkLogger.h"
10 
11 namespace imstk
12 {
13 void
14 CameraController::update(const double& dt)
15 {
16  if (!updateTrackingData(dt))
17  {
18  LOG(WARNING) << "warning: could not update tracking info.";
19  return;
20  }
21 
22  Vec3d p = getPosition();
23  Quatd r = getOrientation();
24 
25  // Apply Offsets over the device pose
26  p = p + m_translationOffset; // Offset the device position
27  r *= m_rotationOffset; // Apply camera head rotation offset
28 
29  // Set camera info
30  m_camera->setPosition(p);
31  m_camera->setFocalPoint((r * Vec3d(0.0, 0.0, -1.0)) + p);
32  m_camera->setViewUp(r * Vec3d(0.0, 1.0, 0.0));
33 }
34 
35 void
37 {
38  const auto pos = m_camera->getPosition();
39  const auto focus = m_camera->getFocalPoint();
40  auto viewUp = m_camera->getViewUp();
41 
42  m_translationOffset = pos;
43 
44  auto viewNormal = (pos - focus).normalized();
45  auto viewSide = viewUp.cross(viewNormal).normalized();
46  viewUp = viewNormal.cross(viewSide);
47  Mat3d rot;
48  rot.col(0) = viewSide;
49  rot.col(1) = viewUp;
50  rot.col(2) = viewNormal;
51  m_rotationOffset = Quatd(rot);
52 }
53 } // namespace imstk
Vec3d m_translationOffset
Translation concatenated to the device translation.
std::shared_ptr< Camera > m_camera
Camera controlled by the external device.
virtual bool updateTrackingData(const double dt)
Update tracking data.
void update(const double &dt) override
Updates the view of the provided camera.
Compound Geometry.
const Quatd & getOrientation() const
Set/Get the orientation of the tracker.
Quatd m_rotationOffset
Rotation concatenated to the device rotation.
const Vec3d & getPosition() const
Set/Get the position of the tracker.
void setOffsetUsingCurrentCameraPose()
Set the offsets based on the current camera pose.