7 #include "visuVTKAdaptor/SCamera.hpp" 9 #include <fwCom/Signal.hpp> 10 #include <fwCom/Signal.hxx> 12 #include <fwData/Boolean.hpp> 13 #include <fwData/mt/ObjectReadLock.hpp> 14 #include <fwData/mt/ObjectWriteLock.hpp> 15 #include <fwData/TransformationMatrix3D.hpp> 17 #include <fwDataTools/fieldHelper/Image.hpp> 19 #include <fwServices/macros.hpp> 22 #include <vtkCamera.h> 23 #include <vtkCommand.h> 24 #include <vtkIdentityTransform.h> 25 #include <vtkMatrix4x4.h> 26 #include <vtkPerspectiveTransform.h> 27 #include <vtkRenderer.h> 28 #include <vtkTransform.h> 39 cb->m_adaptor = adaptor;
53 virtual void Execute( ::vtkObject* pCaller,
unsigned long eventId,
void* )
55 m_adaptor->updateFromVtk();
63 static const ::fwServices::IService::KeyType s_TRANSFORM_INOUT =
"transform";
70 SCamera::SCamera() noexcept :
71 m_cameraCommand(CameraClallback::New(
this)),
79 SCamera::~SCamera() noexcept
85 void SCamera::configuring()
87 this->configureParams();
89 const ConfigType config = this->getConfigTree().get_child(
"config.<xmlattr>.");
90 m_viewAngle = config.get<
double>(
"viewAngle", m_viewAngle);
96 void SCamera::starting()
100 vtkCamera* camera = this->getRenderer()->GetActiveCamera();
102 double position[] = {0.0, 0.0, 0.0};
103 double focal[] = {1.0, 0.0, 0.0};
104 double viewUp[] = {0.0, 0.0, 1.0};
106 m_transOrig = vtkPerspectiveTransform::New();
107 m_transOrig->Identity();
108 m_transOrig->SetupCamera(position, focal, viewUp );
110 camera->SetPosition(position);
111 camera->SetFocalPoint(focal);
112 camera->SetViewUp(viewUp);
113 camera->SetViewAngle(m_viewAngle);
115 camera->AddObserver( ::vtkCommand::ModifiedEvent, m_cameraCommand );
116 this->requestRender();
121 void SCamera::updating()
123 vtkCamera* camera = this->getRenderer()->GetActiveCamera();
124 camera->RemoveObserver( m_cameraCommand );
126 ::fwData::TransformationMatrix3D::csptr transMat =
127 this->getInOut< ::fwData::TransformationMatrix3D >(s_TRANSFORM_INOUT);
129 vtkMatrix4x4* mat = vtkMatrix4x4::New();
133 for(
int lt = 0; lt < 4; lt++)
135 for(
int ct = 0; ct < 4; ct++)
137 mat->SetElement(lt, ct, transMat->getCoefficient(static_cast<size_t>(lt), static_cast<size_t>(ct)));
144 vtkPerspectiveTransform* oldTrans = vtkPerspectiveTransform::New();
145 oldTrans->Identity();
146 oldTrans->SetupCamera(camera->GetPosition(), camera->GetFocalPoint(), camera->GetViewUp());
148 oldTrans->Concatenate(m_transOrig);
152 vtkTransform* trans = vtkTransform::New();
153 trans->SetMatrix(mat);
154 trans->Concatenate(oldTrans->GetMatrix());
155 camera->ApplyTransform(trans);
157 this->getRenderer()->ResetCameraClippingRange();
158 this->setVtkPipelineModified();
160 camera->AddObserver( ::vtkCommand::ModifiedEvent, m_cameraCommand );
165 this->requestRender();
170 void SCamera::stopping()
172 vtkCamera* camera = this->getRenderer()->GetActiveCamera();
173 camera->RemoveObserver( m_cameraCommand );
174 this->unregisterServices();
175 m_transOrig->Delete();
176 this->requestRender();
181 void SCamera::updateFromVtk()
183 vtkCamera* camera = this->getRenderer()->GetActiveCamera();
184 camera->RemoveObserver( m_cameraCommand );
186 ::fwData::TransformationMatrix3D::sptr trf = this->getInOut< ::fwData::TransformationMatrix3D >(s_TRANSFORM_INOUT);
189 vtkPerspectiveTransform* trans = vtkPerspectiveTransform::New();
191 trans->SetupCamera(camera->GetPosition(), camera->GetFocalPoint(), camera->GetViewUp());
194 trans->Concatenate(m_transOrig);
195 vtkMatrix4x4* mat = trans->GetMatrix();
197 for(
int lt = 0; lt < 4; lt++)
199 for(
int ct = 0; ct < 4; ct++)
201 trf->setCoefficient(static_cast<size_t>(lt), static_cast<size_t>(ct), mat->GetElement(lt, ct));
213 camera->AddObserver( ::vtkCommand::ModifiedEvent, m_cameraCommand );
This class is a helper to define the connections of a service and its data.
Class allowing to block a Connection.
The namespace visuVTKAdaptor contains the list of adaptors available for the generic scene...
A helper to lock object on read mode.
A helper to lock object on exclusive mode.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_MODIFIED_SIG
Key in m_signals map of signal m_sigModified.
FWDATA_API void unlock()
Releases lock on object mutex.
This adaptor updates the camera position according to a matrix (and vice versa)
Base class for VTK adaptors.