7 #include "vtkCompositeMesh/RendererService.hpp" 9 #include <fwCom/Signal.hxx> 10 #include <fwCom/Slots.hxx> 12 #include <fwData/Composite.hpp> 13 #include <fwData/Material.hpp> 14 #include <fwData/Mesh.hpp> 15 #include <fwData/TransformationMatrix3D.hpp> 17 #include <fwServices/macros.hpp> 18 #include <fwServices/registry/ActiveWorkers.hpp> 20 #include <fwVtkIO/helper/Mesh.hpp> 21 #include <fwVtkIO/vtk.hpp> 23 #include <vtkCamera.h> 24 #include <vtkCommand.h> 25 #include <vtkInteractorStyleTrackballCamera.h> 26 #include <vtkMatrix4x4.h> 27 #include <vtkPolyData.h> 28 #include <vtkPolyDataMapper.h> 29 #include <vtkPolyDataNormals.h> 30 #include <vtkProperty.h> 31 #include <vtkRenderer.h> 32 #include <vtkRenderWindow.h> 33 #include <vtkTransform.h> 40 const ::fwCom::Slots::SlotKeyType RendererService::s_UPDATE_CAM_POSITION_SLOT =
"updateCamPosition";
42 const ::fwCom::Signals::SignalKeyType RendererService::s_CAM_UPDATED_SIG =
"camUpdated";
51 this->m_isMousePressed =
false;
55 void Execute(vtkObject* _caller,
unsigned long _event,
void* _obj)
57 if (_event == vtkCommand::StartInteractionEvent )
59 this->m_isMousePressed =
true;
61 else if (_event == vtkCommand::EndInteractionEvent )
63 this->m_isMousePressed =
false;
65 else if ( (_event == vtkCommand::ModifiedEvent && this->m_isMousePressed)
66 || _event == vtkCommand::MouseWheelBackwardEvent || _event == vtkCommand::MouseWheelForwardEvent)
69 ::fwThread::Worker::sptr worker = m_service->
getWorker();
70 worker->processTasks();
75 bool m_isMousePressed;
80 m_bPipelineIsInit(false)
82 m_slotUpdateCamPosition = newSlot( s_UPDATE_CAM_POSITION_SLOT, &RendererService::updateCamPosition,
this );
83 newSlot(s_UPDATE_PIPELINE_SLOT, &RendererService::updatePipeline,
this);
85 m_sigCamUpdated = CamUpdatedSignalType::New();
88 ::fwCom::HasSignals::m_signals( s_CAM_UPDATED_SIG, m_sigCamUpdated);
104 this->IGuiContainerSrv::create();
106 m_bPipelineIsInit =
false;
108 m_interactorManager = ::fwRenderVTK::IVtkRenderWindowInteractorManager::createManager();
109 m_interactorManager->installInteractor( this->getContainer() );
112 m_render = vtkRenderer::New();
113 m_interactorManager->getInteractor()->GetRenderWindow()->AddRenderer(m_render);
120 this->IGuiContainerSrv::initialize();
132 assert( m_interactorManager->getInteractor() );
133 m_interactorManager->getInteractor()->RemoveObserver(m_loc);
135 m_interactorManager->uninstallInteractor();
136 m_interactorManager.reset();
138 SLM_ASSERT(
"m_render not instanced", m_render);
142 this->IGuiContainerSrv::destroy();
149 m_interactorManager->getInteractor()->Render();
154 void RendererService::updateCamPosition(SharedArray positionValue,
155 SharedArray focalValue,
156 SharedArray viewUpValue)
158 vtkCamera* camera = m_render->GetActiveCamera();
160 camera->SetPosition(positionValue.get());
161 camera->SetFocalPoint(focalValue.get());
162 camera->SetViewUp(viewUpValue.get());
163 camera->SetClippingRange(0.1, 1000000);
165 m_interactorManager->getInteractor()->Render();
170 void RendererService::initVTKPipeline()
173 createAndAddActorToRender();
177 void RendererService::createAndAddActorToRender()
180 assert(this->getObject< ::fwData::Composite >());
183 ::fwData::Composite::sptr myComposite = this->getObject< ::fwData::Composite >();
185 OSLM_INFO(
"VTK Pipeline ready TO UPDATE" <<
'\n' <<
"Object received:" << myComposite->getLeafClassname());
188 unsigned int elementNumber = 0;
191 for(::fwData::Composite::ContainerType::const_iterator it = myComposite->begin(); it != myComposite->end(); ++it)
194 OSLM_INFO(
"ObjectPointer: " << it->second);
195 OSLM_INFO(
"ObjectType: " << it->second->getClassname() <<
'\n');
197 ::fwData::Mesh::sptr myMesh = ::fwData::Mesh::dynamicCast(it->second);
202 vtkSmartPointer<vtkPolyData> vtk_polyData = vtkSmartPointer<vtkPolyData>::New();
205 vtkPolyDataMapper* mapper = vtkPolyDataMapper::New();
207 m_normals = vtkPolyDataNormals::New();
208 m_normals->SetInputData(vtk_polyData);
209 mapper->SetInputConnection(m_normals->GetOutputPort());
211 vtkActor* actor = vtkActor::New();
212 actor->SetMapper( mapper);
215 m_render->AddActor( actor);
218 if (!myMesh->getField(
"MaterialMesh" ))
221 if(elementNumber == 0)
223 actor->GetProperty()->EdgeVisibilityOn();
224 actor->GetProperty()->SetInterpolationToFlat();
225 actor->GetProperty()->SetColor(1.0, 0.0, 0.0);
226 actor->GetProperty()->SetEdgeColor(1.0, 0.0, 0.0);
230 actor->GetProperty()->SetRepresentationToWireframe();
231 actor->GetProperty()->SetColor(1.0, 1.0, 1.0);
237 ::fwData::Material::sptr matObjPtr = myMesh->getField<
::fwData::Material >(
"MaterialMesh" );
238 actor->GetProperty()->SetColor(matObjPtr->diffuse()->red(),
239 matObjPtr->diffuse()->green(), matObjPtr->diffuse()->blue());
246 m_interactorManager->getInteractor()->SetInteractorStyle(vtkInteractorStyleTrackballCamera::New());
248 m_interactorManager->getInteractor()->AddObserver(vtkCommand::AnyEvent, m_loc);
251 m_render->ResetCamera();
256 void RendererService::updateVTKPipeline()
259 m_render->RemoveAllViewProps();
261 createAndAddActorToRender();
266 void RendererService::updatePipeline()
268 if(!m_bPipelineIsInit)
270 this->initVTKPipeline();
271 m_bPipelineIsInit =
true;
275 this->updateVTKPipeline();
277 m_interactorManager->getInteractor()->Render();
284 vtkCamera* camera = m_render->GetActiveCamera();
286 SharedArray position = SharedArray(
new double[3]);
287 SharedArray focal = SharedArray(
new double[3]);
288 SharedArray viewUp = SharedArray(
new double[3]);
290 std::copy(camera->GetPosition(), camera->GetPosition()+3, position.get());
291 std::copy(camera->GetFocalPoint(), camera->GetFocalPoint()+3, focal.get());
292 std::copy(camera->GetViewUp(), camera->GetViewUp()+3, viewUp.get());
296 m_sigCamUpdated->asyncEmit(position, focal, viewUp);
virtual VTKCOMPOSITEMESH_API void starting() override
Starting method.
Class allowing to block a Connection.
This class defines a material. A material is represented by an ambient color and a diffuse color...
static FWVTKIO_API void toVTKMesh(const ::fwData::Mesh::csptr &_mesh, vtkSmartPointer< vtkPolyData > _polyData)
Convert a ::fwData::Mesh::csptr to a vtkPolyData.
Service rendering fwData::Mesh contained in a fwData::Composite using VTK.
#define OSLM_INFO(message)
virtual VTKCOMPOSITEMESH_API void configuring() override
Configuring method.
#define FW_DEPRECATED_MSG(message, version)
Use this macro when deprecating a function to warn the developer.
Defines the service interface managing the rendering service for object.
void notifyCamPositionUpdated()
This method is used to notify that the VTK camera position is updated.
virtual VTKCOMPOSITEMESH_API KeyConnectionsType getObjSrvConnections() const override
Returns proposals to connect service slots to associated object signals, this method is used for obj/...
virtual VTKCOMPOSITEMESH_API void stopping() override
Stopping method.
The namespace vtkCompositeMesh contains a service which renders several meshes and store it on Compos...
VTKCOMPOSITEMESH_API RendererService() noexcept
Constructor.
#define SLM_ASSERT(message, cond)
work like 'assert' from 'cassert', with in addition a message logged by spylog (with FATAL loglevel) ...
virtual VTKCOMPOSITEMESH_API void updating() override
Updating method.
static VTKCOMPOSITEMESH_APIconst::fwCom::Slots::SlotKeyType s_UPDATE_PIPELINE_SLOT
Slot to update pipeline.
::fwCom::helper::SigSlotConnection::KeyConnectionsType KeyConnectionsType
Returns proposals to connect service slots to associated object signals, this method is used for obj/...
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_MODIFIED_SIG
Key in m_signals map of signal m_sigModified.
This class defines a composite object.
virtual VTKCOMPOSITEMESH_API ~RendererService() noexcept
Destructor.
FWSERVICES_API std::shared_ptr< ::fwThread::Worker > getWorker() const
Returns associate worker.