14 #include "vtkPolyData.h" 15 #include "vtkCardinalSpline.h" 16 #include "vtkPoints.h" 17 #include "vtkCellArray.h" 18 #include "vtkCamera.h" 19 #include "vtkParametricSpline.h" 20 #include "vtkSpline.h" 33 : mTrackingService(tracker)
34 , mPatientModelService(patientModel)
35 , mViewService(visualization)
36 , mLastCameraViewAngle(0)
37 , mLastCameraRotAngle(0)
38 , mAutomaticRotation(true)
40 mManualTool = mTrackingService->getManualTool();
41 mSpline = vtkParametricSplinePtr::New();
42 mLastStoredViewVector.Identity();
48 if(mRoutePositions.size() > 0)
49 if(mRoutePositions.size() == mCameraRotations.size())
51 this->generateSplineCurve(mRoutePositions);
57 std::cout <<
"cameraRawPointsSlot is empty !" << std::endl;
61 this->generateSplineCurve(mesh);
64 void CXVBcameraPath::generateSplineCurve(
MeshPtr mesh)
66 vtkPolyDataPtr polyDataInput = mesh->getTransformedPolyDataCopy(mesh->get_rMd());
67 vtkPoints *vtkpoints = polyDataInput->GetPoints();
69 mNumberOfInputPoints = polyDataInput->GetNumberOfPoints();
71 mNumberOfControlPoints = mNumberOfInputPoints;
75 mSpline->GetXSpline()->RemoveAllPoints();
76 mSpline->GetYSpline()->RemoveAllPoints();
77 mSpline->GetZSpline()->RemoveAllPoints();
79 mSpline->SetPoints(vtkpoints);
82 void CXVBcameraPath::generateSplineCurve(std::vector< Eigen::Vector3d > routePositions)
85 for (
int i = 0; i < routePositions.size(); i++)
86 vtkPoints->InsertNextPoint(routePositions[i](0),routePositions[i](1),routePositions[i](2));
90 mSpline->GetXSpline()->RemoveAllPoints();
91 mSpline->GetYSpline()->RemoveAllPoints();
92 mSpline->GetZSpline()->RemoveAllPoints();
94 mSpline->SetPoints(vtkPoints);
104 double splineFocusDistance = 0.05;
105 if (splineParameter > 0.8)
106 splineFocusDistance = 0.02;
108 double pos_r[3], focus_r[3], d_r[3];
109 double splineParameterArray[3];
110 splineParameterArray[0] = splineParameter;
111 splineParameterArray[1] = splineParameter;
112 splineParameterArray[2] = splineParameter;
114 mSpline->Evaluate(splineParameterArray, pos_r, d_r);
115 splineParameterArray[0] = splineParameter + splineFocusDistance;
116 splineParameterArray[1] = splineParameter + splineFocusDistance;
117 splineParameterArray[2] = splineParameter + splineFocusDistance;
118 mSpline->Evaluate(splineParameterArray, focus_r, d_r);
120 mLastCameraPos_r =
Vector3D(pos_r[0], pos_r[1], pos_r[2]);
121 mLastCameraFocus_r =
Vector3D(focus_r[0], focus_r[1], focus_r[2]);
123 if(mAutomaticRotation)
124 if(mRoutePositions.size() > 0 && mRoutePositions.size() == mCameraRotationsSmoothed.size())
126 int index = (int) (splineParameter * (mRoutePositions.size() - 1));
127 mLastCameraRotAngle = mCameraRotationsSmoothed[index];
131 this->updateManualToolPosition();
135 void CXVBcameraPath::updateManualToolPosition()
139 if(
similar(mLastCameraFocus_r, mLastCameraPos_r, 0.01)) {
140 viewDirection_r = mLastStoredViewVector;
142 viewDirection_r = (mLastCameraFocus_r - mLastCameraPos_r).normalized();
143 mLastStoredViewVector = viewDirection_r;
148 Vector3D yVector =
cross(viewDirection_r, xVector).normalized();
152 rMt.matrix().col(0).head(3) = xVector;
153 rMt.matrix().col(1).head(3) = yVector;
154 rMt.matrix().col(2).head(3) = viewDirection_r;
155 rMt.matrix().col(3).head(3) = mLastCameraPos_r;
160 Transform3D rMpr = mPatientModelService->get_rMpr();
161 Transform3D prMt = rMpr.inv() * rMt * rotateZ * rotateX;
163 mManualTool->set_prMt(prMt);
168 std::vector< double > CXVBcameraPath::smoothCameraRotations(std::vector< double > cameraRotations)
171 int numberOfElements = cameraRotations.size();
172 std::vector< double > cameraRotationsSmoothed = cameraRotations;
175 int maxPositionsToSmooth = (int) (10 * numberOfElements/100);
176 int positionsToSmooth = maxPositionsToSmooth;
177 for(
int i=0; i<numberOfElements; i++)
179 positionsToSmooth = std::min((
int) (positionsToSmooth+.5*numberOfElements/100), maxPositionsToSmooth);
180 bool firstTurnPassed =
false;
181 for(
int j=i+1; j<std::min(i+positionsToSmooth, numberOfElements); j++)
182 if (cameraRotations[j] != cameraRotations[j-1])
186 positionsToSmooth = j-i;
190 firstTurnPassed =
true;
193 std::vector< double > averageElements(cameraRotations.begin()+i, cameraRotations.begin()+std::min(i+positionsToSmooth,numberOfElements-1));
194 if(averageElements.size() > 0)
195 cameraRotationsSmoothed[i] = std::accumulate(averageElements.begin(), averageElements.end(), 0.0) / averageElements.size();
198 return cameraRotationsSmoothed;
204 mLastCameraViewAngle =
static_cast<double>(angle) * (
M_PI / 180.0);
205 this->updateManualToolPosition();
210 mLastCameraRotAngle =
static_cast<double>(angle) * (
M_PI / 180.0);
211 this->updateManualToolPosition();
216 mRoutePositions = routePositions;
221 mCameraRotations = cameraRotations;
222 mCameraRotationsSmoothed = smoothCameraRotations(mCameraRotations);
227 mAutomaticRotation = automaticRotation;
233 return 2*positionPercentage / (1 + positionPercentage/100.0);
void rotationChanged(int value)
void setAutomaticRotation(bool automaticRotation)
void cameraRawPointsSlot(MeshPtr mesh)
double positionPercentageAdjusted(double positionPercentage)
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
boost::shared_ptr< class TrackingService > TrackingServicePtr
void setCameraRotations(std::vector< double > cameraRotations)
vtkSmartPointer< vtkPoints > vtkPointsPtr
void cameraPathPositionSlot(int positionPercentage)
void cameraRotateAngleSlot(int angle)
Vector3D cross(const Vector3D &a, const Vector3D &b)
compute cross product of a and b.
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.
void setRoutePositions(std::vector< Eigen::Vector3d > routePositions)
bool similar(const CameraInfo &lhs, const CameraInfo &rhs, double tol)
Transform3D createTransformRotateZ(const double angle)
Transform3D createTransformRotateX(const double angle)
boost::shared_ptr< class Mesh > MeshPtr
CXVBcameraPath(TrackingServicePtr tracker, PatientModelServicePtr patientModel, ViewServicePtr visualization)
Namespace for all CustusX production code.