Fraxinus  17.12-rc1
An IGT application
cxVBcameraPath.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
33 #include <iostream>
34 #include "vtkForwardDeclarations.h"
35 #include "vtkPolyData.h"
36 #include "vtkCardinalSpline.h"
37 #include "vtkPoints.h"
38 #include "vtkCellArray.h"
39 #include "vtkCamera.h"
40 #include "vtkParametricSpline.h"
41 #include "vtkSpline.h"
42 
43 #include "cxVBcameraPath.h"
44 #include "cxMesh.h"
45 #include "cxTrackingService.h"
46 #include "cxPatientModelService.h"
47 #include "cxViewServiceProxy.h"
48 #include "cxView.h"
49 
50 namespace cx {
51 
53  : mTrackingService(tracker)
54  , mPatientModelService(patientModel)
55  , mViewService(visualization)
56  , mLastCameraViewAngle(0)
57  , mLastCameraRotAngle(0)
58 {
59  mManualTool = mTrackingService->getManualTool();
60  mSpline = vtkParametricSplinePtr::New();
61  mLastStoredViewVector.Identity();
62 
63 }
64 
66 {
67 
68  if(!mesh)
69  {
70  std::cout << "cameraRawPointsSlot is empty !" << std::endl;
71  return;
72  }
73 
74  this->generateSplineCurve(mesh);
75 
76 }
77 
78 void CXVBcameraPath::generateSplineCurve(MeshPtr mesh)
79 {
80  vtkPolyDataPtr polyDataInput = mesh->getTransformedPolyDataCopy(mesh->get_rMd());
81  vtkPoints *vtkpoints = polyDataInput->GetPoints();
82 
83  mNumberOfInputPoints = polyDataInput->GetNumberOfPoints();
84 
85  mNumberOfControlPoints = mNumberOfInputPoints;
86 
87  // Setting the spline curve points
88  // First clean up previous stored data
89  mSpline->GetXSpline()->RemoveAllPoints();
90  mSpline->GetYSpline()->RemoveAllPoints();
91  mSpline->GetZSpline()->RemoveAllPoints();
92 
93  mSpline->SetPoints(vtkpoints);
94 }
95 
96 
97 
98 
100 {
101 
102  double splineParameter = pos / 100.0;
103 
104 // std::cout << "CXVBcameraPath::cameraPathPositionSlot , pos : " << pos
105 // << ", spline parameter : " << splineParameter << std::endl;
106 
107  double pos_r[3], focus_r[3], d_r[3];
108  double splineParameterArray[3];
109  splineParameterArray[0] = splineParameter;
110  splineParameterArray[1] = splineParameter;
111  splineParameterArray[2] = splineParameter;
112 
113  mSpline->Evaluate(splineParameterArray, pos_r, d_r);
114  splineParameterArray[0] = splineParameter+0.05;
115  splineParameterArray[1] = splineParameter+0.05;
116  splineParameterArray[2] = splineParameter+0.05;
117  mSpline->Evaluate(splineParameterArray, focus_r, d_r);
118 
119  mLastCameraPos_r = Vector3D(pos_r[0], pos_r[1], pos_r[2]);
120  mLastCameraFocus_r = Vector3D(focus_r[0], focus_r[1], focus_r[2]);
121  this->updateManualToolPosition();
122 
123 }
124 
125 void CXVBcameraPath::updateManualToolPosition()
126 {
127  Vector3D viewDirection_r;
128  // New View direction
129  if(similar(mLastCameraFocus_r, mLastCameraPos_r, 0.01)) {
130  viewDirection_r = mLastStoredViewVector;
131  } else {
132  viewDirection_r = (mLastCameraFocus_r - mLastCameraPos_r).normalized();
133  mLastStoredViewVector = viewDirection_r;
134  }
135 
136 
137  Vector3D xVector = Vector3D(0,1,0);
138  Vector3D yVector = cross(viewDirection_r, xVector).normalized();
139 
140  // Construct tool transform
141  Transform3D rMt = Transform3D::Identity();
142  rMt.matrix().col(0).head(3) = xVector;
143  rMt.matrix().col(1).head(3) = yVector;
144  rMt.matrix().col(2).head(3) = viewDirection_r;
145  rMt.matrix().col(3).head(3) = mLastCameraPos_r;
146 
147  Transform3D rotateX = createTransformRotateX(mLastCameraViewAngle);
148  Transform3D rotateZ = createTransformRotateZ(mLastCameraRotAngle);
149 
150  Transform3D rMpr = mPatientModelService->get_rMpr();
151  Transform3D prMt = rMpr.inv() * rMt * rotateZ * rotateX;
152 
153  mManualTool->set_prMt(prMt);
154 
155 }
156 
157 
159 {
160  mLastCameraViewAngle = static_cast<double>(angle) * (M_PI / 180.0);
161  this->updateManualToolPosition();
162 }
163 
165 {
166  mLastCameraRotAngle = static_cast<double>(angle) * (M_PI / 180.0);
167  this->updateManualToolPosition();
168 }
169 
170 } /* namespace cx */
void cameraRawPointsSlot(MeshPtr mesh)
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
boost::shared_ptr< class TrackingService > TrackingServicePtr
boost::shared_ptr< class ViewService > ViewServicePtr
void cameraRotateAngleSlot(int angle)
Vector3D cross(const Vector3D &a, const Vector3D &b)
compute cross product of a and b.
Definition: cxVector3D.cpp:62
boost::shared_ptr< class PatientModelService > PatientModelServicePtr
void cameraViewAngleSlot(int angle)
vtkSmartPointer< vtkPolyData > vtkPolyDataPtr
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:63
bool similar(const CameraInfo &lhs, const CameraInfo &rhs, double tol)
Transform3D createTransformRotateZ(const double angle)
void cameraPathPositionSlot(int pos)
Transform3D createTransformRotateX(const double angle)
boost::shared_ptr< class Mesh > MeshPtr
CXVBcameraPath(TrackingServicePtr tracker, PatientModelServicePtr patientModel, ViewServicePtr visualization)
#define M_PI
Namespace for all CustusX production code.