fw4spl
SReconstruction.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2018.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "visuVTKAdaptor/SReconstruction.hpp"
8 
9 #include "visuVTKAdaptor/SMesh.hpp"
10 
11 #include <fwCom/Slot.hpp>
12 #include <fwCom/Slot.hxx>
13 #include <fwCom/Slots.hpp>
14 #include <fwCom/Slots.hxx>
15 
16 #include <fwData/Material.hpp>
17 #include <fwData/Mesh.hpp>
18 #include <fwData/Reconstruction.hpp>
19 
20 #include <fwServices/macros.hpp>
21 #include <fwServices/op/Add.hpp>
22 
23 #include <fwTools/fwID.hpp>
24 
25 #include <vtkActor.h>
26 #include <vtkCamera.h>
27 #include <vtkMath.h>
28 #include <vtkMatrix4x4.h>
29 #include <vtkPicker.h>
30 #include <vtkPlaneCollection.h>
31 #include <vtkPolyDataMapper.h>
32 #include <vtkPolyDataNormals.h>
33 #include <vtkProperty.h>
34 #include <vtkRenderer.h>
35 #include <vtkRenderWindowInteractor.h>
36 #include <vtkTransform.h>
37 
38 fwServicesRegisterMacro( ::fwRenderVTK::IAdaptor, ::visuVTKAdaptor::SReconstruction);
39 
40 namespace visuVTKAdaptor
41 {
42 
43 const ::fwCom::Slots::SlotKeyType SReconstruction::s_UPDATE_VISIBILITY_SLOT = "updateVisibility";
44 const ::fwCom::Slots::SlotKeyType SReconstruction::s_UPDATE_NORMAL_MODE_SLOT = "updateNormalMode";
45 
46 const ::fwServices::IService::KeyType SReconstruction::s_RECONSTRUCTION_INPUT = "reconstruction";
47 
48 //------------------------------------------------------------------------------
49 SReconstruction::SReconstruction() noexcept :
50  m_autoResetCamera(true)
51 {
52  newSlot(s_UPDATE_VISIBILITY_SLOT, &SReconstruction::updateVisibility, this);
54 }
55 
56 //------------------------------------------------------------------------------
57 
58 SReconstruction::~SReconstruction() noexcept
59 {
60 }
61 
62 //------------------------------------------------------------------------------
63 
65 {
66  this->configureParams();
67 
68  const ConfigType config = this->getConfigTree().get_child("config.<xmlattr>");
69 
70  const std::string autoresetcamera = config.get<std::string>("autoresetcamera", "yes");
71  SLM_ASSERT("'autoresetcamera' value must be 'yes' or 'no', actual: " + autoresetcamera,
72  autoresetcamera == "yes" || autoresetcamera == "no");
73  m_autoResetCamera = (autoresetcamera == "yes");
74 }
75 
76 //------------------------------------------------------------------------------
77 
79 {
80  this->initialize();
81  this->createMeshService();
82  this->requestRender();
83 }
84 
85 //------------------------------------------------------------------------------
86 
87 void SReconstruction::createMeshService()
88 {
89  auto reconstruction = this->getInput < ::fwData::Reconstruction >(s_RECONSTRUCTION_INPUT);
90  SLM_ASSERT("Missing Reconstruction", reconstruction);
91 
92  ::fwData::Mesh::sptr mesh = reconstruction->getMesh();
93 
94  SLM_TRACE_IF("Mesh is null", !mesh);
95  if (mesh)
96  {
97  // create the srv configuration for objects auto-connection
98  auto meshAdaptor = this->registerService< ::visuVTKAdaptor::SMesh >("::visuVTKAdaptor::SMesh");
99  meshAdaptor->registerInput(mesh, SMesh::s_MESH_INPUT, true);
100 
101  meshAdaptor->setRenderService( this->getRenderService() );
102  meshAdaptor->setRendererId( this->getRendererId() );
103  meshAdaptor->setPickerId( this->getPickerId() );
104  meshAdaptor->setTransformId( this->getTransformId() );
105 
106  meshAdaptor->setClippingPlanesId( m_clippingPlanesId);
107  meshAdaptor->setShowClippedPart( true );
108  meshAdaptor->setMaterial( reconstruction->getMaterial() );
109  meshAdaptor->setAutoResetCamera( m_autoResetCamera );
110  meshAdaptor->start();
111  meshAdaptor->updateVisibility( reconstruction->getIsVisible() );
112  meshAdaptor->update();
113 
114  m_meshService = meshAdaptor;
115  OSLM_TRACE("Mesh is visible : "<< reconstruction->getIsVisible());
116  OSLM_TRACE("Mesh nb points : "<< mesh->getNumberOfPoints());
117  }
118 }
119 
120 //------------------------------------------------------------------------------
121 
123 {
124  ::fwRenderVTK::IAdaptor::sptr meshService = m_meshService.lock();
125  if (meshService)
126  {
127  auto reconstruction = this->getInput < ::fwData::Reconstruction >(s_RECONSTRUCTION_INPUT);
128  SLM_ASSERT("Missing Reconstruction", reconstruction);
129  ::visuVTKAdaptor::SMesh::sptr meshAdaptor = SMesh::dynamicCast(meshService);
130 
131  ::fwData::Mesh::csptr newMesh = reconstruction->getMesh();
132  ::fwData::Mesh::csptr oldMesh = meshAdaptor->getInput< ::fwData::Mesh >(SMesh::s_MESH_INPUT);
133  ::fwData::Material::csptr newMat = reconstruction->getMaterial();
134  ::fwData::Material::csptr oldMat = meshAdaptor->getMaterial();
135 
136  // replace the mesh adaptor if the mesh or the material changed
137  if (newMesh != oldMesh || newMat != oldMat)
138  {
139  this->unregisterService(meshAdaptor);
140  this->createMeshService();
141  }
142  else
143  {
144  meshAdaptor->updateVisibility( reconstruction->getIsVisible());
145  meshAdaptor->updateOptionsMode();
146  }
147  }
148  else
149  {
150  this->createMeshService();
151  }
152  this->requestRender();
153 }
154 
155 //------------------------------------------------------------------------------
156 
158 {
160 
161  this->unregisterServices();
162  this->requestRender();
163 }
164 
165 //------------------------------------------------------------------------------
166 
167 void SReconstruction::setForceHide(bool hide)
168 {
169  ::fwData::Reconstruction::csptr reconstruction =
170  this->getInput < ::fwData::Reconstruction >(s_RECONSTRUCTION_INPUT);
171  SLM_ASSERT("Missing Reconstruction", reconstruction);
172  this->updateVisibility((hide ? false : reconstruction->getIsVisible()));
173 }
174 
175 //------------------------------------------------------------------------------
176 
177 void SReconstruction::updateVisibility(bool visible)
178 {
179  if (!m_meshService.expired())
180  {
181  ::fwRenderVTK::IAdaptor::sptr meshService = m_meshService.lock();
182  ::visuVTKAdaptor::SMesh::sptr meshAdaptor
183  = SMesh::dynamicCast(meshService);
184 
185  if (meshAdaptor)
186  {
187  meshAdaptor->updateVisibility( visible );
188  }
189  }
190 }
191 
192 //------------------------------------------------------------------------------
193 
194 void SReconstruction::setAutoResetCamera(bool autoResetCamera)
195 {
196  m_autoResetCamera = autoResetCamera;
197 }
198 
199 //------------------------------------------------------------------------------
200 
201 void SReconstruction::updateNormalMode(std::uint8_t mode)
202 {
203  if (!m_meshService.expired())
204  {
205  ::visuVTKAdaptor::SMesh::sptr meshAdaptor = ::visuVTKAdaptor::SMesh::dynamicCast(m_meshService.lock());
206  if (meshAdaptor)
207  {
208  meshAdaptor->updateNormalMode(mode);
209  }
210  }
211 }
212 
213 //------------------------------------------------------------------------------
214 
216 {
217  KeyConnectionsMap connections;
218  connections.push(s_RECONSTRUCTION_INPUT, ::fwData::Reconstruction::s_MODIFIED_SIG, s_UPDATE_SLOT );
219  connections.push(s_RECONSTRUCTION_INPUT, ::fwData::Reconstruction::s_MESH_CHANGED_SIG, s_UPDATE_SLOT );
220  connections.push(s_RECONSTRUCTION_INPUT, ::fwData::Reconstruction::s_VISIBILITY_MODIFIED_SIG,
222 
223  return connections;
224 }
225 
226 //------------------------------------------------------------------------------
227 
228 } //namespace visuVTKAdaptor
This class is a helper to define the connections of a service and its data.
Definition: IService.hpp:454
#define SLM_TRACE_IF(message, cond)
Definition: spyLog.hpp:232
VISUVTKADAPTOR_API void configuring() override
Configure the service before starting. Apply the configuration to service.
The namespace visuVTKAdaptor contains the list of adaptors available for the generic scene...
FWRENDERVTK_API void configureParams()
Parse the xml configuration for renderer, picker and transform.
VISUVTKADAPTOR_API void updating() override
Perform some computations according to object (this service is attached to) attribute values and its ...
FWSERVICES_API void unregisterService(const ::fwTools::fwID::IDType &_id)
Unregister a specific service.
FWRENDERVTK_API void requestRender()
notify a render request iff vtkPipeline is modified
#define OSLM_TRACE(message)
Definition: spyLog.hpp:230
FWRENDERVTK_API SRender::sptr getRenderService() const
Returd the associated render service.
FWRENDERVTK_API SRender::PickerIdType getPickerId() const
Gets the identifier of the picker used by this adaptor.
FWRENDERVTK_API void removeAllPropFromRenderer()
Removes all the vtkProp from the renderer.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_MESH_CHANGED_SIG
Key in m_signals map of signal m_sigMeshModified.
Displays a Reconstruction.
virtual VISUVTKADAPTOR_API KeyConnectionsMap getAutoConnections() const override
Returns proposals to connect service slots to associated object signals, this method is used for obj/...
FWSERVICES_API void unregisterServices(const std::string &_implType="")
Unregister all services linked to this service, optionally matches only a given type of services...
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
FWRENDERVTK_API SRender::VtkObjectIdType getTransformId() const
Returns the identifier of the transform used by this adaptor.
static VISUVTKADAPTOR_APIconst::fwCom::Slots::SlotKeyType s_UPDATE_VISIBILITY_SLOT
slot used to update visibility
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_VISIBILITY_MODIFIED_SIG
Key in m_signals map of signal m_sigVisibilityModified.
static VISUVTKADAPTOR_APIconst::fwCom::Slots::SlotKeyType s_UPDATE_NORMAL_MODE_SLOT
slot used to update normal mode (0: none, 1: point, 2: cell)
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_MODIFIED_SIG
Key in m_signals map of signal m_sigModified.
FWRENDERVTK_API SRender::RendererIdType getRendererId() const
Returns the renderer identifier.
FWRENDERVTK_API void initialize()
Initialize the adaptor with the associated render service. (must be call in starting).
VISUVTKADAPTOR_API void stopping() override
Uninitialize the service activity. The stop() method is always invoked before destroying a service...
VISUVTKADAPTOR_API void starting() override
Initialize the service activity.
Data holding a geometric structure composed of points, lines, triangles, quads or polygons...
static FWSERVICES_APIconst::fwCom::Slots::SlotKeyType s_UPDATE_SLOT
Slot to call start method.
Definition: IService.hpp:177
VISUVTKADAPTOR_API void setAutoResetCamera(bool autoResetCamera)
Active/Inactive automatic reset on camera for triangular mesh adaptor. By default =true...
FWSERVICES_API ConfigType getConfigTree() const
Return the configuration, in an boost property tree.
Definition: IService.cpp:247
void updateNormalMode(std::uint8_t mode)
Slot: used to update normal display(0: none, 1: point, 2: cell)