fw4spl
SModelSeries.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/SModelSeries.hpp"
8 
9 #include "visuVTKAdaptor/SMesh.hpp"
10 #include "visuVTKAdaptor/SReconstruction.hpp"
11 #include "visuVTKAdaptor/STexture.hpp"
12 
13 #include <fwCom/Signal.hpp>
14 #include <fwCom/Signal.hxx>
15 #include <fwCom/Slots.hpp>
16 #include <fwCom/Slots.hxx>
17 
18 #include <fwData/Boolean.hpp>
19 #include <fwData/Material.hpp>
20 #include <fwData/Mesh.hpp>
21 #include <fwData/Reconstruction.hpp>
22 
23 #include <fwMedData/ModelSeries.hpp>
24 
25 #include <fwServices/macros.hpp>
26 #include <fwServices/op/Add.hpp>
27 
28 #include <vtkActor.h>
29 #include <vtkPolyDataMapper.h>
30 
32 
33 namespace visuVTKAdaptor
34 {
35 
36 const ::fwCom::Signals::SignalKeyType SModelSeries::s_TEXTURE_APPLIED_SIG = "textureApplied";
37 
38 // Public slots
39 const ::fwCom::Slots::SlotKeyType SModelSeries::s_UPDATE_NORMAL_MODE_SLOT = "updateNormalMode";
40 const ::fwCom::Slots::SlotKeyType SModelSeries::s_SHOW_RECONSTRUCTIONS_SLOT = "showReconstructions";
41 
42 // Private slot
43 static const ::fwCom::Slots::SlotKeyType s_CHANGE_FIELD_SLOT = "changeField";
44 
45 static const ::fwServices::IService::KeyType s_MODEL_INPUT = "model";
46 
47 //------------------------------------------------------------------------------
48 
49 SModelSeries::SModelSeries() noexcept :
50  m_autoResetCamera(true)
51 {
52  m_clippingPlanes = "";
53 
54  m_sigTextureApplied = newSignal<TextureAppliedSignalType>(s_TEXTURE_APPLIED_SIG);
55 
56  newSlot(s_UPDATE_NORMAL_MODE_SLOT, &SModelSeries::updateNormalMode, this);
57  newSlot(s_SHOW_RECONSTRUCTIONS_SLOT, &SModelSeries::showReconstructions, this);
58  newSlot(s_CHANGE_FIELD_SLOT, &SModelSeries::showReconstructionsOnFieldChanged, this);
59 }
60 
61 //------------------------------------------------------------------------------
62 
63 SModelSeries::~SModelSeries() noexcept
64 {
65 }
66 
67 //------------------------------------------------------------------------------
68 
70 {
71  this->configureParams();
72 
73  const ConfigType config = this->getConfigTree().get_child("config.<xmlattr>");
74 
75  this->setClippingPlanes( config.get<std::string>("clippingplanes", "") );
76 
77  const std::string autoresetcamera = config.get<std::string>("autoresetcamera", "yes");
78  SLM_ASSERT("'autoresetcamera' must be 'yes' or 'no'", autoresetcamera == "yes" || autoresetcamera == "no");
79  m_autoResetCamera = (autoresetcamera == "yes");
80 
81  if (config.count("texture"))
82  {
83  SLM_FATAL("'texture' is deprecated, you need to connect manually the SModelSeries::textureApplied signal to "
84  "the STexture::applyTexture slot.");
85  }
86 }
87 
88 //------------------------------------------------------------------------------
89 
91 {
92  this->initialize();
93  this->updating();
94 }
95 
96 //------------------------------------------------------------------------------
97 
99 {
100  ::fwMedData::ModelSeries::csptr modelSeries = this->getInput< ::fwMedData::ModelSeries >(s_MODEL_INPUT);
101  SLM_ASSERT("Missing ModelSeries '" + s_MODEL_INPUT + "'", modelSeries);
102 
103  this->stopping();
104 
105  ::fwData::Boolean::csptr showField = modelSeries->getField< ::fwData::Boolean >("ShowReconstructions");
106  bool showRec = true;
107  if (showField && showField->getValue() == false)
108  {
109  showRec = false;
110  }
111 
112  for( ::fwData::Reconstruction::sptr reconstruction : modelSeries->getReconstructionDB() )
113  {
114  // create the srv configuration for objects auto-connection
115  auto adaptor = registerService< ::visuVTKAdaptor::SReconstruction >("::visuVTKAdaptor::SReconstruction");
116  adaptor->registerInput(reconstruction, SReconstruction::s_RECONSTRUCTION_INPUT, true);
117 
118  adaptor->setTransformId( this->getTransformId() );
119  adaptor->setRendererId( this->getRendererId() );
120  adaptor->setPickerId( this->getPickerId() );
121  adaptor->setRenderService(this->getRenderService());
122 
123  adaptor->setClippingPlanes( m_clippingPlanes );
124  adaptor->setAutoResetCamera(m_autoResetCamera);
125  adaptor->start();
126  adaptor->setForceHide( !showRec );
127 
128  m_sigTextureApplied->asyncEmit(reconstruction->getMaterial());
129  }
130  this->requestRender();
131 }
132 
133 //------------------------------------------------------------------------------
134 
136 {
137  m_connections.disconnect();
138  this->unregisterServices();
139 }
140 
141 //------------------------------------------------------------------------------
142 
143 void SModelSeries::updateNormalMode(std::uint8_t mode, std::string recID)
144 {
145  auto subServices = this->getRegisteredServices();
146  for( auto& wService : subServices)
147  {
148  auto service = wService.lock();
149  if(service)
150  {
151  auto reconstructionAdaptor = ::visuVTKAdaptor::SReconstruction::dynamicCast(service);
152  if (reconstructionAdaptor && reconstructionAdaptor->getInput< ::fwData::Object >(
153  SReconstruction::s_RECONSTRUCTION_INPUT)->getID() == recID)
154  {
155  reconstructionAdaptor->updateNormalMode(mode);
156  break;
157  }
158  }
159  }
160 }
161 
162 //------------------------------------------------------------------------------
163 
165 {
166  ::fwMedData::ModelSeries::csptr modelSeries = this->getInput< ::fwMedData::ModelSeries >(s_MODEL_INPUT);
167  SLM_ASSERT("Missing ModelSeries '" + s_MODEL_INPUT + "'", modelSeries);
168 
169  auto subServices = this->getRegisteredServices();
170  for( auto& wService : subServices)
171  {
172  auto service = wService.lock();
173  if(service)
174  {
175  auto reconstructionAdaptor = ::visuVTKAdaptor::SReconstruction::dynamicCast(service);
176  if (reconstructionAdaptor)
177  {
178  reconstructionAdaptor->setForceHide( !show );
179  }
180  }
181  }
182  this->setVtkPipelineModified();
183  this->requestRender();
184 }
185 
186 //------------------------------------------------------------------------------
187 
189 {
190  const auto modelSeries = this->getInput< ::fwMedData::ModelSeries >(s_MODEL_INPUT);
191  const bool show = modelSeries->getField("ShowReconstructions", ::fwData::Boolean::New(true))->value();
192 
193  auto subServices = this->getRegisteredServices();
194  for( auto& wService : subServices)
195  {
196  auto service = wService.lock();
197  if(service)
198  {
199  auto reconstructionAdaptor = ::visuVTKAdaptor::SReconstruction::dynamicCast(service);
200  if (reconstructionAdaptor)
201  {
202  reconstructionAdaptor->setForceHide( !show );
203  }
204  }
205  }
206 }
207 
208 //------------------------------------------------------------------------------
209 
211 {
212  KeyConnectionsMap connections;
213  connections.push(s_MODEL_INPUT, ::fwMedData::ModelSeries::s_MODIFIED_SIG, s_UPDATE_SLOT);
214  connections.push(s_MODEL_INPUT, ::fwMedData::ModelSeries::s_RECONSTRUCTIONS_ADDED_SIG, s_UPDATE_SLOT);
215  connections.push(s_MODEL_INPUT, ::fwMedData::ModelSeries::s_RECONSTRUCTIONS_REMOVED_SIG, s_UPDATE_SLOT);
216  connections.push(s_MODEL_INPUT, ::fwMedData::ModelSeries::s_ADDED_FIELDS_SIG, s_CHANGE_FIELD_SLOT );
217  connections.push(s_MODEL_INPUT, ::fwMedData::ModelSeries::s_REMOVED_FIELDS_SIG, s_CHANGE_FIELD_SLOT );
218  connections.push(s_MODEL_INPUT, ::fwMedData::ModelSeries::s_CHANGED_FIELDS_SIG, s_CHANGE_FIELD_SLOT );
219 
220  return connections;
221 }
222 
223 //------------------------------------------------------------------------------
224 
225 } //namespace visuVTKAdaptor
This class is a helper to define the connections of a service and its data.
Definition: IService.hpp:454
FWCOM_API void disconnect()
Disconnect all registered connections and clear m_connections.
void showReconstructionsOnFieldChanged()
Slot: update all reconstructions visibility using "ShowReconstructions" field.
The namespace visuVTKAdaptor contains the list of adaptors available for the generic scene...
VISUVTKADAPTOR_API void updating() override
Perform some computations according to object (this service is attached to) attribute values and its ...
FWRENDERVTK_API void configureParams()
Parse the xml configuration for renderer, picker and transform.
const ServiceVector & getRegisteredServices() const
Get all subservices linked to this service.
FWRENDERVTK_API void requestRender()
notify a render request iff vtkPipeline is modified
FWRENDERVTK_API void setVtkPipelineModified()
End-user have to call this method when a vtk structure has been modified, thus a render request will ...
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_CHANGED_FIELDS_SIG
Type of signal m_sigModified.
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.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_ADDED_FIELDS_SIG
Type of signal m_sigModified.
FWTOOLS_API IDType getID(Policy policy=GENERATE) const
Returns the id of the object. If it is not set and the policy value is.
Definition: fwID.cpp:78
#define SLM_FATAL(message)
Definition: spyLog.hpp:283
FWSERVICES_API void unregisterServices(const std::string &_implType="")
Unregister all services linked to this service, optionally matches only a given type of services...
static FWMEDDATA_APIconst::fwCom::Signals::SignalKeyType s_RECONSTRUCTIONS_ADDED_SIG
Key in m_signals map of signal m_sigReconstructionsAdded.
#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.
Base class for each data object.
void updateNormalMode(std::uint8_t mode, std::string recID)
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_MODIFIED_SIG
Key in m_signals map of signal m_sigModified.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_REMOVED_FIELDS_SIG
Type 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).
void showReconstructions(bool show)
Slot: show(or hide) reconstructions.
virtual VISUVTKADAPTOR_API KeyConnectionsMap getAutoConnections() const override
Returns proposals to connect service slots to associated object signals, this method is used for obj/...
VISUVTKADAPTOR_API void configuring() override
Configure the service before starting. Apply the configuration to service.
This adaptor shows ModelSeries. Creates adaptors for each reconstruction in model.
VISUVTKADAPTOR_API void starting() override
Initialize the service activity.
VISUVTKADAPTOR_API void stopping() override
Uninitialize the service activity. The stop() method is always invoked before destroying a service...
static FWSERVICES_APIconst::fwCom::Slots::SlotKeyType s_UPDATE_SLOT
Slot to call start method.
Definition: IService.hpp:177
static FWMEDDATA_APIconst::fwCom::Signals::SignalKeyType s_RECONSTRUCTIONS_REMOVED_SIG
Key in m_signals map of signal m_sigReconstructionsRemoved.
This class contains a boolean value.
FWSERVICES_API ConfigType getConfigTree() const
Return the configuration, in an boost property tree.
Definition: IService.cpp:247