Fraxinus  16.5.0-fx-rc9
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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), mPatientModelService(patientModel), mViewService(visualization)
54 {
55  mManualTool = mTrackingService->getManualTool();
56  mSpline = vtkParametricSplinePtr::New();
57  mLastStoredViewVector.Identity();
58 
59 }
60 
62 {
63 
64  if(!mesh)
65  {
66  std::cout << "cameraRawPointsSlot is empty !" << std::endl;
67  return;
68  }
69 
70  this->generateSplineCurve(mesh);
71 
72 }
73 
74 void CXVBcameraPath::generateSplineCurve(MeshPtr mesh)
75 {
76  vtkPolyDataPtr polyDataInput = mesh->getTransformedPolyData(mesh->get_rMd());
77  vtkPoints *vtkpoints = polyDataInput->GetPoints();
78 
79  mNumberOfInputPoints = polyDataInput->GetNumberOfPoints();
80 
81  mNumberOfControlPoints = mNumberOfInputPoints;
82 
83  // Setting the spline curve points
84  // First clean up previous stored data
85  mSpline->GetXSpline()->RemoveAllPoints();
86  mSpline->GetYSpline()->RemoveAllPoints();
87  mSpline->GetZSpline()->RemoveAllPoints();
88 
89  mSpline->SetPoints(vtkpoints);
90 }
91 
92 
93 
94 
96 {
97 
98  double splineParameter = pos / 100.0;
99 
100 // std::cout << "CXVBcameraPath::cameraPathPositionSlot , pos : " << pos
101 // << ", spline parameter : " << splineParameter << std::endl;
102 
103  double pos_r[3], focus_r[3], d_r[3];
104  double splineParameterArray[3];
105  splineParameterArray[0] = splineParameter;
106  splineParameterArray[1] = splineParameter;
107  splineParameterArray[2] = splineParameter;
108 
109  mSpline->Evaluate(splineParameterArray, pos_r, d_r);
110  splineParameterArray[0] = splineParameter+0.1;
111  splineParameterArray[1] = splineParameter+0.1;
112  splineParameterArray[2] = splineParameter+0.1;
113  mSpline->Evaluate(splineParameterArray, focus_r, d_r);
114 
115  mLastCameraPos_r = Vector3D(pos_r[0], pos_r[1], pos_r[2]);
116  mLastCameraFocus_r = Vector3D(focus_r[0], focus_r[1], focus_r[2]);
117  this->updateManualToolPosition();
118 
119 }
120 
121 void CXVBcameraPath::updateManualToolPosition()
122 {
123  Vector3D viewDirection_r;
124  // New View direction
125  if(similar(mLastCameraFocus_r, mLastCameraPos_r, 0.01)) {
126  viewDirection_r = mLastStoredViewVector;
127  } else {
128  viewDirection_r = (mLastCameraFocus_r - mLastCameraPos_r).normalized();
129  mLastStoredViewVector = viewDirection_r;
130  }
131 
132 
133  Vector3D xVector = Vector3D(0,1,0);
134  Vector3D yVector = cross(viewDirection_r, xVector).normalized();
135 
136  // Construct tool transform
137  Transform3D rMt = Transform3D::Identity();
138  rMt.matrix().col(0).head(3) = xVector;
139  rMt.matrix().col(1).head(3) = yVector;
140  rMt.matrix().col(2).head(3) = viewDirection_r;
141  rMt.matrix().col(3).head(3) = mLastCameraPos_r;
142 
143  Transform3D rotateX = createTransformRotateX(mLastCameraViewAngle);
144  Transform3D rotateZ = createTransformRotateZ(mLastCameraRotAngle);
145 
146  Transform3D rMpr = mPatientModelService->get_rMpr();
147  Transform3D prMt = rMpr.inv() * rMt * rotateZ * rotateX;
148 
149  mManualTool->set_prMt(prMt);
150 
151 }
152 
153 
155 {
156  mLastCameraViewAngle = static_cast<double>(angle) * (M_PI / 180.0);
157  this->updateManualToolPosition();
158 }
159 
161 {
162  mLastCameraRotAngle = static_cast<double>(angle) * (M_PI / 180.0);
163  this->updateManualToolPosition();
164 }
165 
166 } /* 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)
bool similar(const DoubleBoundingBox3D &a, const DoubleBoundingBox3D &b, double tol)
Vector3D cross(const Vector3D &a, const Vector3D &b)
compute cross product of a and b.
Definition: cxVector3D.cpp:62
vtkSmartPointer< class vtkPolyData > vtkPolyDataPtr
boost::shared_ptr< class PatientModelService > PatientModelServicePtr
void cameraViewAngleSlot(int angle)
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:63
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