fw4spl
SNegatoOneSlice.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/SNegatoOneSlice.hpp"
8 
9 #include "visuVTKAdaptor/SImage.hpp"
10 #include "visuVTKAdaptor/SImageSlice.hpp"
11 
12 #include <fwCom/Slot.hpp>
13 #include <fwCom/Slot.hxx>
14 #include <fwCom/Slots.hpp>
15 #include <fwCom/Slots.hxx>
16 
17 #include <fwData/Color.hpp>
18 #include <fwData/Image.hpp>
19 #include <fwData/String.hpp>
20 #include <fwData/TransferFunction.hpp>
21 
22 #include <fwDataTools/fieldHelper/Image.hpp>
23 #include <fwDataTools/fieldHelper/MedicalImageHelpers.hpp>
24 
25 #include <fwServices/macros.hpp>
26 #include <fwServices/op/Add.hpp>
27 
28 #include <fwVtkIO/vtk.hpp>
29 
30 #include <vtkImageBlend.h>
31 #include <vtkImageCheckerboard.h>
32 #include <vtkImageData.h>
33 #include <vtkImageMapToColors.h>
34 
35 fwServicesRegisterMacro( ::fwRenderVTK::IAdaptor, ::visuVTKAdaptor::SNegatoOneSlice);
36 
37 namespace visuVTKAdaptor
38 {
39 
40 static const ::fwCom::Slots::SlotKeyType s_UPDATE_SLICE_TYPE_SLOT = "updateSliceType";
41 static const ::fwCom::Slots::SlotKeyType s_UPDATE_IMAGE_SLOT = "updateImage";
42 
43 static const ::fwServices::IService::KeyType s_IMAGE_INOUT = "image";
44 static const ::fwServices::IService::KeyType s_TF_INOUT = "tf";
45 
46 //------------------------------------------------------------------------------
47 
48 SNegatoOneSlice::SNegatoOneSlice() noexcept :
49  m_manageImageSource(false),
50  m_imageSource(nullptr),
51  m_allowAlphaInTF(false),
52  m_interpolation(true),
53  m_actorOpacity(1.0)
54 {
55  newSlot(s_UPDATE_SLICE_TYPE_SLOT, &SNegatoOneSlice::updateSliceType, this);
56  newSlot(s_UPDATE_IMAGE_SLOT, &SNegatoOneSlice::updateImage, this);
57 }
58 
59 //------------------------------------------------------------------------------
60 
61 SNegatoOneSlice::~SNegatoOneSlice() noexcept
62 {
63  this->unregisterServices();
64  this->cleanImageSource();
65 }
66 
67 //------------------------------------------------------------------------------
68 
69 vtkObject* SNegatoOneSlice::getImageSource()
70 {
71  if ( !m_imageSource )
72  {
73  OSLM_TRACE(this->getID() << ": Create ImageSource");
74  if (!m_imageSourceId.empty())
75  {
76  m_imageSource = this->getVtkObject(m_imageSourceId);
77  }
78  else
79  {
80  m_imageSource = vtkImageMapToColors::New();
81  m_manageImageSource = true;
82  }
83  }
84 
85  return m_imageSource;
86 }
87 
88 //------------------------------------------------------------------------------
89 
90 void SNegatoOneSlice::cleanImageSource()
91 {
92  if (m_manageImageSource && m_imageSource)
93  {
94  m_imageSource->Delete();
95  m_imageSource = nullptr;
96  }
97 }
98 
99 //------------------------------------------------------------------------------
100 
101 ::fwRenderVTK::IAdaptor::sptr SNegatoOneSlice::getImageSliceAdaptor()
102 {
103  if (m_imageSliceAdaptor.expired())
104  {
105  OSLM_TRACE(this->getID() << ": Create SImageSlice Adaptor Service");
106  ::fwData::Image::sptr image = this->getInOut< ::fwData::Image >(s_IMAGE_INOUT);
107  SLM_ASSERT("Missing image", image);
108 
109  // create the srv configuration for objects auto-connection
110  auto imgSliceAdaptor = this->registerService< ::visuVTKAdaptor::SImageSlice>("::visuVTKAdaptor::SImageSlice");
111  imgSliceAdaptor->registerInOut(image, s_IMAGE_INOUT, true);
112 
113  imgSliceAdaptor->setRenderService(this->getRenderService());
114  imgSliceAdaptor->setRendererId( this->getRendererId() );
115  imgSliceAdaptor->setPickerId( this->getPickerId() );
116  imgSliceAdaptor->setTransformId( this->getTransformId() );
117 
118  imgSliceAdaptor->setVtkImageSource(this->getImageSource());
119  imgSliceAdaptor->setInterpolation(m_interpolation);
120  imgSliceAdaptor->setActorOpacity(m_actorOpacity);
121  imgSliceAdaptor->setOrientation(m_orientation);
122 
123  m_imageSliceAdaptor = imgSliceAdaptor;
124  }
125 
126  return m_imageSliceAdaptor.lock();
127 }
128 
129 //------------------------------------------------------------------------------
130 
131 ::fwRenderVTK::IAdaptor::sptr SNegatoOneSlice::getImageAdaptor()
132 {
133  if (m_imageAdaptor.expired())
134  {
135  OSLM_TRACE(this->getID() << ": Create Image Adaptor Service");
136  ::fwData::Image::sptr image = this->getInOut< ::fwData::Image >(s_IMAGE_INOUT);
137  SLM_ASSERT("Missing image", image);
138 
139  // create the srv configuration for objects auto-connection
140  auto imgAdaptor = this->registerService< ::visuVTKAdaptor::SImage>("::visuVTKAdaptor::SImage");
141  imgAdaptor->registerInOut(image, s_IMAGE_INOUT, true);
142 
143  imgAdaptor->setRenderService(this->getRenderService());
144  imgAdaptor->setRendererId( this->getRendererId() );
145  imgAdaptor->setPickerId( this->getPickerId() );
146  imgAdaptor->setTransformId( this->getTransformId() );
147 
148  imgAdaptor->setVtkImageRegister(this->getImageSource());
149 
150  ::fwData::TransferFunction::sptr tf = this->getInOut< ::fwData::TransferFunction >(s_TF_INOUT);
151  if (tf)
152  {
153  imgAdaptor->registerInOut(tf, s_TF_INOUT, false, true);
154  }
155 
156  imgAdaptor->setImageOpacity(1.);
157  imgAdaptor->setAllowAlphaInTF(m_allowAlphaInTF);
158 
159  m_imageAdaptor = imgAdaptor;
160  }
161 
162  return m_imageAdaptor.lock();
163 }
164 
165 //------------------------------------------------------------------------------
166 
168 {
169  this->initialize();
170  if (nullptr == vtkImageBlend::SafeDownCast(this->getImageSource())
171  && nullptr == vtkImageCheckerboard::SafeDownCast(this->getImageSource()))
172  {
173  this->getImageAdaptor()->start();
174  }
175  this->getImageSliceAdaptor()->start();
176 }
177 
178 //------------------------------------------------------------------------------
179 
181 {
182  this->unregisterServices();
183  this->cleanImageSource();
184 }
185 
186 //------------------------------------------------------------------------------
187 
189 {
190  if (nullptr == vtkImageBlend::SafeDownCast(this->getImageSource())
191  && nullptr == vtkImageCheckerboard::SafeDownCast(this->getImageSource()))
192  {
193  this->getImageAdaptor()->update();
194  }
195  this->getImageSliceAdaptor()->update();
196 }
197 
198 //------------------------------------------------------------------------------
199 
200 void SNegatoOneSlice::swapping(const KeyType& key)
201 {
202  if (key == s_TF_INOUT
203  && nullptr == vtkImageBlend::SafeDownCast(this->getImageSource())
204  && nullptr == vtkImageCheckerboard::SafeDownCast(this->getImageSource()))
205  {
206  IAdaptor::sptr imageAdaptor = this->getImageAdaptor();
207  ::fwData::TransferFunction::sptr tf = this->getInOut< ::fwData::TransferFunction >(s_TF_INOUT);
208  if (tf)
209  {
210  imageAdaptor->registerInOut(tf, s_TF_INOUT, false, true);
211  imageAdaptor->swapKey(s_TF_INOUT, nullptr);
212  }
213  else if(::fwServices::OSR::isRegistered(s_TF_INOUT, AccessType::INOUT, imageAdaptor))
214  {
215  ::fwServices::OSR::unregisterService(s_TF_INOUT, AccessType::INOUT, imageAdaptor);
216  imageAdaptor->swapKey(s_TF_INOUT, nullptr);
217  }
218  }
219 }
220 
221 //-----------------------------------------------------------------------------
222 
223 void SNegatoOneSlice::updateSliceType(int from, int to)
224 {
225  if( to == static_cast<int>(m_orientation) )
226  {
227  setOrientation( static_cast< Orientation >( from ));
228  }
229  else if(from == static_cast<int>(m_orientation))
230  {
231  setOrientation( static_cast< Orientation >( to ));
232  }
233 }
234 
235 //-----------------------------------------------------------------------------
236 
237 void SNegatoOneSlice::updateImage()
238 {
239  this->stopping();
240  this->starting();
241  this->updating();
242 }
243 
244 //------------------------------------------------------------------------------
245 
247 {
248  this->configureParams();
249 
250  const ConfigType config = this->getConfigTree().get_child("config.<xmlattr>");
251 
252  const std::string orientation = config.get<std::string>("sliceIndex", "axial");
253  if(orientation == "axial" )
254  {
255  m_orientation = Z_AXIS;
256  }
257  else if(orientation == "frontal" )
258  {
259  m_orientation = Y_AXIS;
260  }
261  else if(orientation == "sagittal" )
262  {
263  m_orientation = X_AXIS;
264  }
265 
266  const std::string tfalpha = config.get<std::string>("tfalpha", "no");
267  SLM_ASSERT("'tfalpha' value must be 'yes' or 'no', actual: " + tfalpha, tfalpha == "yes" || tfalpha == "no");
268  this->setAllowAlphaInTF(tfalpha == "yes");
269 
270  const std::string interpolation = config.get<std::string>("interpolation", "off");
271  SLM_ASSERT("'interpolation' value must be 'on' or 'off', actual: " + interpolation,
272  interpolation == "on" || interpolation == "off");
273  this->setInterpolation(interpolation == "yes");
274 
275  this->setVtkImageSourceId( config.get<std::string>("vtkimagesource", ""));
276 
277  m_actorOpacity = config.get<double>("actorOpacity", 1.);
278 }
279 
280 //------------------------------------------------------------------------------
281 
283 {
284  KeyConnectionsMap connections;
285  connections.push(s_IMAGE_INOUT, ::fwData::Image::s_MODIFIED_SIG, s_UPDATE_IMAGE_SLOT );
286  connections.push(s_IMAGE_INOUT, ::fwData::Image::s_SLICE_TYPE_MODIFIED_SIG, s_UPDATE_SLICE_TYPE_SLOT );
287  connections.push(s_IMAGE_INOUT, ::fwData::Image::s_BUFFER_MODIFIED_SIG, s_UPDATE_IMAGE_SLOT );
288 
289  return connections;
290 }
291 
292 //------------------------------------------------------------------------------
293 
294 } //namespace visuVTKAdaptor
This class is a helper to define the connections of a service and its data.
Definition: IService.hpp:454
FWRENDERVTK_API vtkObject * getVtkObject(const SRender::VtkObjectIdType &objectId) const
Returns the vtk object defined by &#39;objectId&#39; in the vtk scene.
VISUVTKADAPTOR_API void configuring() override
Configure the service before starting. Apply the configuration to service.
Display a negato image with one slice.
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 ...
virtual FWDATATOOLS_API void setOrientation(Orientation orientation)
Set the image orientation.
virtual void swapping()
Swap the service from associated object to another object.
Definition: IService.hpp:613
#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.
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
virtual VISUVTKADAPTOR_API KeyConnectionsMap getAutoConnections() const override
Returns proposals to connect service slots to associated object signals, this method is used for obj/...
Orientation m_orientation
Image orientation.
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.
VISUVTKADAPTOR_API void stopping() override
Uninitialize the service activity. The stop() method is always invoked before destroying a service...
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_BUFFER_MODIFIED_SIG
Type of signal when image&#39;s buffer is added.
VISUVTKADAPTOR_API void starting() override
Initialize the service activity.
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).
FWSERVICES_API bool isRegistered(const ::fwServices::IService::KeyType &objKey,::fwServices::IService::AccessType access,::fwServices::IService::sptr service)
Return true if a key is registered for a given service.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_SLICE_TYPE_MODIFIED_SIG
Type of signal when image&#39;s buffer is added.
FWSERVICES_API ConfigType getConfigTree() const
Return the configuration, in an boost property tree.
Definition: IService.cpp:247