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->getToolManager().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->getPatientService()->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());
178 if (
similar(pos_old, camera_r, 1) &&
similar(focus_old, focus_r, 1))
184 mBlockCameraUpdate =
true;
185 camera->SetPosition(camera_r.begin());
186 camera->SetFocalPoint(focus_r.begin());
187 camera->SetViewUp(vup_r.begin());
188 camera->SetClippingRange(1, std::max<double>(1000, cameraOffset * 1.5));
189 mBlockCameraUpdate =
false;
192 void CameraStyleForView::activeToolChangedSlot()
194 ToolPtr newTool = mBackend->getToolManager()->getActiveTool();
195 if (newTool == mFollowingTool)
198 this->disconnectTool();
202 bool CameraStyleForView::isToolFollowingStyle(CAMERA_STYLE_TYPE style)
const
207 void CameraStyleForView::connectTool()
209 if (!this->isToolFollowingStyle(mCameraStyleForView))
212 mFollowingTool = mBackend->getToolManager()->getActiveTool();
217 if (!this->getView())
220 connect(mFollowingTool.get(), SIGNAL(toolTransformAndTimestamp(
Transform3D,
double)),
this,
221 SLOT(setModified()));
226 rep->setOffsetPointVisibleAtZeroOffset(
true);
228 rep->setStayHiddenAfterVisible(
true);
231 mViewportListener->startListen(this->getRenderer());
232 mPreRenderListener->startListen(this->getRenderer());
234 this->updateCamera();
236 report(
"Camera is following " + mFollowingTool->getName());
239 void CameraStyleForView::disconnectTool()
244 mViewportListener->stopListen();
245 mPreRenderListener->stopListen();
249 disconnect(mFollowingTool.get(), SIGNAL(toolTransformAndTimestamp(
Transform3D,
double)),
this,
250 SLOT(setModified()));
254 rep->setStayHiddenAfterVisible(
false);
257 mFollowingTool.reset();
262 if (mCameraStyleForView == style)
265 this->disconnectTool();
267 ViewPtr view = this->getView();
270 vtkRenderWindowInteractor* interactor = view->getRenderWindow()->GetInteractor();
277 interactor->SetInteractorStyle(vtkInteractorStyleTrackballCameraPtr::New());
280 interactor->SetInteractorStyle(vtkInteractorStyleUnicamPtr::New());
285 mCameraStyleForView = style;
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)
SNW_DEFINE_ENUM_STRING_CONVERTERS_END(cx, ORIENTATION_TYPE, otCOUNT)
vtkSmartPointer< class vtkCamera > vtkCameraPtr
boost::shared_ptr< class Tool > ToolPtr