35 #include <vtkRenderer.h>
36 #include <vtkCamera.h>
41 #include "boost/bind.hpp"
42 #include <vtkRenderWindow.h>
43 #include "vtkInteractorStyleUnicam.h"
44 #include "vtkInteractorStyleTrackballCamera.h"
49 #include <vtkRenderWindowInteractor.h>
68 mBlockCameraUpdate(false),
72 mViewportListener->setCallback(boost::bind(&CameraStyleForView::viewportChangedSlot,
this));
75 mPreRenderListener->setCallback(boost::bind(&CameraStyleForView::onPreRender,
this));
77 connect(mBackend->tracking().get(), SIGNAL(activeToolChanged(
const QString&)),
this, SLOT(activeToolChangedSlot()));
82 this->disconnectTool();
88 ViewPtr CameraStyleForView::getView()
const
93 void CameraStyleForView::viewportChangedSlot()
95 if (mBlockCameraUpdate)
100 void CameraStyleForView::onPreRender()
103 this->moveCameraToolStyleSlot(mFollowingTool->get_prMt(), mFollowingTool->getTimestamp());
108 if (!this->getView())
111 ToolRep3DPtr rep = RepContainer(this->getView()->getReps()).findFirst<ToolRep3D>(mFollowingTool);
117 if (!this->getView())
119 return this->getView()->getRenderer();
124 if (!this->getRenderer())
126 return this->getRenderer()->GetActiveCamera();
129 void CameraStyleForView::setModified()
131 mPreRenderListener->setModified();
134 void CameraStyleForView::updateCamera()
139 void CameraStyleForView::moveCameraToolStyleSlot(
Transform3D prMt,
double timestamp)
151 Transform3D rMpr = mBackend->patient()->get_rMpr();
155 double offset = mFollowingTool->getTooltipOffset();
157 double cameraOffset = camera->GetDistance();
168 double height = cameraOffset * tan(20 / 180.0 *
M_PI);
169 camera_r += vup_r * height;
170 Vector3D elevated = camera_r + vup_r * height;
171 Vector3D n_foc2eye = (elevated - focus_r).normalized();
172 camera_r = focus_r + cameraOffset * n_foc2eye;
175 Vector3D pos_old(camera->GetPosition());
176 Vector3D focus_old(camera->GetFocalPoint());
177 Vector3D vup_old(camera->GetViewUp());
179 if (
similar(pos_old, camera_r, 0.1) &&
similar(focus_old, focus_r, 0.1) &&
similar(vup_old, vup_r,0.1 ))
185 mBlockCameraUpdate =
true;
186 camera->SetPosition(camera_r.begin());
187 camera->SetFocalPoint(focus_r.begin());
188 camera->SetViewUp(vup_r.begin());
189 camera->SetClippingRange(1, std::max<double>(1000, cameraOffset * 1.5));
190 mBlockCameraUpdate =
false;
193 void CameraStyleForView::activeToolChangedSlot()
195 ToolPtr newTool = mBackend->tracking()->getActiveTool();
196 if (newTool == mFollowingTool)
199 this->disconnectTool();
203 bool CameraStyleForView::isToolFollowingStyle(CAMERA_STYLE_TYPE style)
const
208 void CameraStyleForView::connectTool()
210 if (!this->isToolFollowingStyle(mCameraStyleForView))
213 mFollowingTool = mBackend->tracking()->getActiveTool();
218 if (!this->getView())
221 connect(mFollowingTool.get(), SIGNAL(toolTransformAndTimestamp(
Transform3D,
double)),
this,
222 SLOT(setModified()));
227 rep->setOffsetPointVisibleAtZeroOffset(
true);
229 rep->setStayHiddenAfterVisible(
true);
232 mViewportListener->startListen(this->getRenderer());
233 mPreRenderListener->startListen(this->getRenderer());
235 this->updateCamera();
237 report(
"Camera is following " + mFollowingTool->getName());
240 void CameraStyleForView::disconnectTool()
245 mViewportListener->stopListen();
246 mPreRenderListener->stopListen();
250 disconnect(mFollowingTool.get(), SIGNAL(toolTransformAndTimestamp(
Transform3D,
double)),
this,
251 SLOT(setModified()));
255 rep->setStayHiddenAfterVisible(
false);
258 mFollowingTool.reset();
263 if (mCameraStyleForView == style)
266 this->disconnectTool();
268 ViewPtr view = this->getView();
271 vtkRenderWindowInteractor* interactor = view->getRenderWindow()->GetInteractor();
278 interactor->SetInteractorStyle(vtkInteractorStyleTrackballCameraPtr::New());
281 interactor->SetInteractorStyle(vtkInteractorStyleUnicamPtr::New());
286 mCameraStyleForView = style;
293 return mCameraStyleForView;
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
boost::shared_ptr< class View > ViewPtr
bool similar(const DoubleBoundingBox3D &a, const DoubleBoundingBox3D &b, double tol)
Listens to changes in viewport and camera matrix.
vtkSmartPointer< class vtkRenderer > vtkRendererPtr
void setView(ViewPtr widget)
SNW_DEFINE_ENUM_STRING_CONVERTERS_BEGIN(cx, CAMERA_STYLE_TYPE, cstCOUNT)
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
boost::shared_ptr< class CoreServices > CoreServicesPtr
Listens to the start render event in a vtkRenderer.
boost::shared_ptr< class ToolRep3D > ToolRep3DPtr
void setCameraStyle(CAMERA_STYLE_TYPE style)
CAMERA_STYLE_TYPE getCameraStyle()
SNW_DEFINE_ENUM_STRING_CONVERTERS_END(cx, ORIENTATION_TYPE, otCOUNT)
vtkSmartPointer< class vtkCamera > vtkCameraPtr
boost::shared_ptr< class Tool > ToolPtr