iMSTK
Interactive Medical Simulation Toolkit
VRCameraControl.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 "VRCameraControl.h"
8 #include "imstkCamera.h"
9 #include "imstkLogger.h"
10 #include "imstkOpenVRDeviceClient.h"
11 
12 using namespace imstk;
13 
14 void
16 {
17  LOG(INFO) << "VRCameraControl controls";
18  LOG(INFO) << "----------------------------------------------------------------------";
19  LOG(INFO) << " | Left Trackpad - rotate view";
20  LOG(INFO) << " | Right Trakcpad - translate view";
21  LOG(INFO) << "----------------------------------------------------------------------";
22 }
23 
24 void
25 VRCameraControl::update(const double& dt)
26 {
27  // We may switch cameras on the controller
28  m_deltaTransform = Mat4d::Identity();
29  if (m_camera == nullptr)
30  {
31  return;
32  }
33 
34  if (m_rotateDevice != nullptr)
35  {
36  m_rotateDevice->update();
37 
38  const Vec2d& pos = m_rotateDevice->getTrackpadPosition();
39  const Mat4d& view = m_camera->getView();
40  m_camera->setView(
41  view * mat4dRotation(Rotd(-pos[0] * m_rotateSpeedScale * dt, Vec3d(0.0, 1.0, 0.0))));
42  }
43  if (m_translateDevice != nullptr)
44  {
45  m_translateDevice->update();
46 
47  const Vec2d& pos = m_translateDevice->getTrackpadPosition();
48 
49  double dy = 0.0;
50  if (m_translateDevice->getButton(2))
51  {
52  dy = m_translateVerticalSpeedScale;
53  }
54  if (m_translateDevice->getButton(3))
55  {
56  dy = -m_translateVerticalSpeedScale;
57  }
58 
59  // Final view applied to scene
60  const Mat4d& finalView = m_camera->getHMDView();
61  // User view (multiplied into with hardware view)
62  const Mat4d& userView = m_camera->getView();
63 
64  //Vec3d translate = Vec3d::Zero();
65  //Mat3d rotation = Mat3d::Identity();
66  //Vec3d scale = Vec3d::Ones();
67  //mat4dTRS(finalView, translate, rotation, scale);
68 
70  //rotation = rotation.inverse();
71 
72  const Mat4d inverseFinalView = finalView.inverse();
73 
74  /*Matrix3f n;
75  n = AngleAxisf(ea[0], Vector3f::UnitX())
76  * AngleAxisf(ea[1], Vector3f::UnitY())
77  * AngleAxisf(ea[2], Vector3f::UnitZ());*/
78 
79  //
80  // View directions/basis in our world space
81  Vec3d worldViewXDir = inverseFinalView.col(0).head<3>().normalized();
82  const Vec3d yDir = Vec3d(0.0, 1.0, 0.0);
83  Vec3d worldViewZDir = inverseFinalView.col(2).head<3>().normalized();
84 
85  // Orthognalize into world space
86  // ie: Project z and x onto yDir plane
87  //xDir = zDir.cross(yDir);
88  //zDir = xDir.cross(yDir);
89 
90  const Vec3d movement = worldViewXDir * -pos[0] + worldViewZDir * pos[1] + yDir * dy;
91  m_deltaTransform = mat4dTranslate(movement * m_translateSpeedScale * dt);
92  m_camera->setView(userView * m_deltaTransform);
93  }
94 }
void update(const double &dt) override
Updates control based on current device state.
Compound Geometry.
void printControls() override
Prints the controls.