fw4spl
fwRenderVTK/src/fwRenderVTK/IAdaptor.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 "fwRenderVTK/IAdaptor.hpp"
8 
9 #include "fwRenderVTK/registry/adaptors.hpp"
10 
11 #include <fwData/String.hpp>
12 
13 #include <fwRuntime/EConfigurationElement.hpp>
14 
15 #include <fwServices/macros.hpp>
16 #include <fwServices/registry/ObjectService.hpp>
17 
18 #include <fwTools/fwID.hpp>
19 
20 #include <vtkAbstractPropPicker.h>
21 #include <vtkRenderer.h>
22 #include <vtkRenderWindow.h>
23 #include <vtkRenderWindowInteractor.h>
24 #include <vtkTransform.h>
25 
26 namespace fwRenderVTK
27 {
28 
29 IAdaptor::IAdaptor() noexcept :
30  m_vtkPipelineModified(true),
31  m_rendererId("default"),
32  m_pickerId(""),
33  // by default no Picker
34  m_transformId(""),
35  // by default no Transform
36  m_propCollection( vtkPropCollection::New() )
37 {
38 }
39 
41 {
42  m_propCollection->Delete();
43 }
44 
45 //------------------------------------------------------------------------------
46 
48 {
49  const ConfigType config = this->getConfigTree();
50  this->setPickerId(config.get<std::string>("config.<xmlattr>.picker", ""));
51  this->setRendererId(config.get<std::string>("config.<xmlattr>.renderer", ""));
52  this->setTransformId(config.get<std::string>("config.<xmlattr>.transform", ""));
53 }
54 
55 //------------------------------------------------------------------------------
56 
58 {
59  if(m_renderService.expired())
60  {
61  // retrieve the SRender service associated to this adaptor
62  const auto servicesVector = ::fwServices::OSR::getServices("::fwRenderVTK::SRender");
63 
64  const auto& registry = ::fwRenderVTK::registry::getAdaptorRegistry();
65 
66  const std::string adaptorID = this->getID();
67  SLM_ASSERT("The service '" + adaptorID + "' is not found in the VTK adaptor registry. Make sure the service is "
68  "declared in a SRender.", registry.find(adaptorID) != registry.end());
69  const auto& renderServiceId = registry.at(adaptorID);
70 
71  auto result =
72  std::find_if(servicesVector.begin(), servicesVector.end(),
73  [renderServiceId](const ::fwServices::IService::sptr& srv)
74  {
75  return srv->getID() == renderServiceId;
76  });
77  SLM_ASSERT("Can't find '" + renderServiceId + "' SRender service.", result != servicesVector.end());
78 
79  ::fwRenderVTK::SRender::sptr renderService = ::fwRenderVTK::SRender::dynamicCast(*result);
80  m_renderService = renderService;
81  }
82 }
83 
84 //------------------------------------------------------------------------------
85 
86 void IAdaptor::setRenderService( SRender::sptr service)
87 {
89  SLM_ASSERT("service not instanced", service);
90 
91  m_renderService = service;
92 }
93 
94 //------------------------------------------------------------------------------
95 
96 void IAdaptor::setRendererId(SRender::RendererIdType newID)
97 {
98  m_rendererId = newID;
99 }
100 
101 //------------------------------------------------------------------------------
102 
104 {
105  m_vtkPipelineModified = true;
106 }
107 
108 //------------------------------------------------------------------------------
109 
111 {
112  return (m_renderService.lock()->getRenderMode() == SRender::RenderMode::AUTO);
113 }
114 
115 //------------------------------------------------------------------------------
116 
118 {
121  && this->getRenderService()->isShownOnScreen()
122  && m_vtkPipelineModified && this->getAutoRender() )
123  {
124  if ( !this->getRenderService()->getPendingRenderRequest())
125  {
126  this->getRenderService()->setPendingRenderRequest(true);
127  this->getRenderService()->slot(SRender::s_RENDER_SLOT)->asyncRun();
128  }
129  m_vtkPipelineModified = false;
130  }
131 }
132 
133 //------------------------------------------------------------------------------
134 
135 SRender::RendererIdType IAdaptor::getRendererId() const
136 {
137  return m_rendererId;
138 }
139 
140 //------------------------------------------------------------------------------
141 
142 SRender::sptr IAdaptor:: getRenderService() const
143 {
144  SLM_ASSERT("SRender service is not initialized, initialize() method must be called first",
145  !m_renderService.expired());
146  return m_renderService.lock();
147 }
148 
149 //------------------------------------------------------------------------------
150 
151 vtkRenderer* IAdaptor::getRenderer()
152 {
153  return m_renderService.lock()->getRenderer(m_rendererId);
154 }
155 
156 //------------------------------------------------------------------------------
157 
158 void IAdaptor::setPickerId(SRender::PickerIdType newID)
159 {
160  m_pickerId = newID;
161 }
162 
163 //------------------------------------------------------------------------------
164 
165 SRender::PickerIdType IAdaptor::getPickerId() const
166 {
167  return m_pickerId;
168 }
169 
170 //------------------------------------------------------------------------------
171 
172 vtkAbstractPropPicker* IAdaptor::getPicker(std::string pickerId)
173 {
174  if (pickerId.empty())
175  {
176  pickerId = m_pickerId;
177  }
178  return m_renderService.lock()->getPicker(pickerId);
179 }
180 
181 //------------------------------------------------------------------------------
182 
183 void IAdaptor::setTransformId(SRender::VtkObjectIdType newID)
184 {
185  m_transformId = newID;
186 }
187 
188 //------------------------------------------------------------------------------
189 
190 SRender::VtkObjectIdType IAdaptor::getTransformId() const
191 {
192  return m_transformId;
193 }
194 
195 //------------------------------------------------------------------------------
196 
197 vtkTransform* IAdaptor::getTransform()
198 {
199  SLM_ASSERT("Transform id must be defined", !m_transformId.empty());
200  return m_renderService.lock()->getOrAddVtkTransform(m_transformId);
201 }
202 
203 //------------------------------------------------------------------------------
204 
205 vtkObject* IAdaptor::getVtkObject(const SRender::VtkObjectIdType& objectId) const
206 {
207  if (!objectId.empty())
208  {
209  return m_renderService.lock()->getVtkObject(objectId);
210  }
211  return nullptr;
212 }
213 
214 //------------------------------------------------------------------------------
215 
216 vtkRenderWindowInteractor* IAdaptor::getInteractor()
217 {
218  return this->getRenderer()->GetRenderWindow()->GetInteractor();
219 }
220 
221 //------------------------------------------------------------------------------
222 
223 IAdaptor::sptr IAdaptor::getAssociatedAdaptor(vtkProp* prop, int depth)
224 {
225  IAdaptor::sptr srv;
226 
227  if (prop)
228  {
229  if ( m_propCollection->IsItemPresent(prop) )
230  {
231  srv = this->getSptr();
232  }
233  else
234  {
235  IAdaptor::sptr res;
236  auto subServices = this->getRegisteredServices();
237  for( auto& service : subServices)
238  {
239  auto adaptor = ::fwRenderVTK::IAdaptor::dynamicCast(service.lock());
240  if(adaptor)
241  {
242  res = adaptor->getAssociatedAdaptor(prop, depth - 1 );
243  if (res)
244  {
245  break;
246  }
247  }
248  }
249  srv = ( res && depth == 0 ) ? this->getSptr() : res;
250  }
251  }
252  return srv;
253 }
254 
255 //------------------------------------------------------------------------------
256 
257 void IAdaptor::registerProp(vtkProp* prop)
258 {
259  getProps(m_propCollection, prop);
260 }
261 
262 //------------------------------------------------------------------------------
263 
264 void IAdaptor::getProps(vtkPropCollection* propc, vtkProp* prop)
265 {
266  int initSize = propc->GetNumberOfItems();
267 
268  prop->GetActors(propc);
269  prop->GetActors2D(propc);
270  prop->GetVolumes(propc);
271 
272  if (initSize == propc->GetNumberOfItems())
273  {
274  propc->AddItem(prop);
275  }
276 }
277 
278 //------------------------------------------------------------------------------
279 
280 void IAdaptor::getAllSubProps(vtkPropCollection* propc, int depth)
281 {
282  vtkProp* prop;
283 
284  m_propCollection->InitTraversal();
285  while ( (prop = m_propCollection->GetNextProp()) )
286  {
287  getProps(propc, prop);
288  }
289 
290  if(depth != 0)
291  {
292  auto subServices = this->getRegisteredServices();
293  for( const auto& service : subServices)
294  {
295  auto adaptor = ::fwRenderVTK::IAdaptor::dynamicCast(service.lock());
296  if(adaptor)
297  {
298  adaptor->getAllSubProps( propc, depth - 1 );
299  }
300  }
301  }
302 }
303 
304 //------------------------------------------------------------------------------
305 
307 {
308  m_propCollection->RemoveAllItems();
309 }
310 
311 //------------------------------------------------------------------------------
312 
313 void IAdaptor::addToRenderer(vtkProp* prop)
314 {
315  this->registerProp(prop);
316  this->getRenderer()->AddViewProp(prop);
317  this->setVtkPipelineModified();
318 }
319 
320 //------------------------------------------------------------------------------
321 
322 void IAdaptor::addToPicker(vtkProp* prop, std::string pickerId)
323 {
324  OSLM_ASSERT("Picker '"<< pickerId << "' undefined.", this->getPicker(pickerId));
325  this->getPicker(pickerId)->AddPickList(prop);
326  this->setVtkPipelineModified();
327 }
328 
329 //------------------------------------------------------------------------------
330 
331 void IAdaptor::removeFromPicker(vtkProp* prop, std::string pickerId)
332 {
333  OSLM_ASSERT("Picker '"<< pickerId << "' undefined.", this->getPicker(pickerId));
334  this->getPicker(pickerId)->DeletePickList(prop);
335  this->setVtkPipelineModified();
336 }
337 
338 //------------------------------------------------------------------------------
339 
341 {
342  vtkPropCollection* propc = m_propCollection;
343  vtkProp* prop;
344 
345  propc->InitTraversal();
346  while ( (prop = propc->GetNextProp()) )
347  {
348  this->getRenderer()->RemoveViewProp(prop);
349  }
350  this->unregisterProps();
351  this->setVtkPipelineModified();
352 }
353 
354 } // namespace fwRenderVTK
FWRENDERVTK_API vtkRenderWindowInteractor * getInteractor()
Returns the render interactor.
Contains fwAtomsFilter::registry details.
FWRENDERVTK_API void addToRenderer(vtkProp *prop)
Adds the vtkProp to the renderer.
FWRENDERVTK_API vtkObject * getVtkObject(const SRender::VtkObjectIdType &objectId) const
Returns the vtk object defined by &#39;objectId&#39; in the vtk scene.
FWRENDERVTK_API void addToPicker(vtkProp *prop, std::string pickerId="")
Adds the vtkProp to the picking list.
#define OSLM_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:310
FWRENDERVTK_API vtkRenderer * getRenderer()
Returns the renderer.
FWRENDERVTK_API void setRendererId(SRender::RendererIdType newID)
Sets the renderer identifier.
FWRENDERVTK_API void configureParams()
Parse the xml configuration for renderer, picker and transform.
FWRENDERVTK_API IAdaptor::sptr getAssociatedAdaptor(vtkProp *prop, int depth)
Returns the adaptor associated to the vtkProp.
FWRENDERVTK_API IAdaptor() noexcept
constructor
FWRENDERVTK_API bool getAutoRender() const
Returns true if the service automatically triggers the rendering.
FWRENDERVTK_API vtkAbstractPropPicker * getPicker(std::string pickerId="")
Get the picker.
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 ...
FWRENDERVTK_API void setTransformId(SRender::VtkObjectIdType newID)
Sets the identifier of the transform used by this adaptor.
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.
FWRENDERVTK_API void setRenderService(SRender::sptr service)
Sets the assiciated render service.
FWRENDERVTK_API vtkTransform * getTransform()
Returns the transform used by this adaptor.
FWRENDERVTK_API void unregisterProps()
Unregisters all the vtkProp.
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_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 void getAllSubProps(vtkPropCollection *propc, int depth=-1)
Gets all the vtkProp associated to this adaptor.
FWRENDERVTK_API SRender::VtkObjectIdType getTransformId() const
Returns the identifier of the transform used by this adaptor.
FWRENDERVTK_API void removeFromPicker(vtkProp *prop, std::string pickerId="")
Removes the vtkProp to the picking list.
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).
FWRENDERVTK_API void registerProp(vtkProp *prop)
Registers the vtkProp.
The namespace fwRenderVTK contains classes for rendering with VTK.
FWSERVICES_API GlobalStatus getStatus() const noexcept
Return the global process status.
Definition: IService.cpp:376
static FWRENDERVTK_API void getProps(vtkPropCollection *outPropc, vtkProp *inProp)
Get all the vtkProp associated to the inProp (actors, actors2D, volumes)
FWRENDERVTK_API void setPickerId(SRender::PickerIdType newID)
Sets the identifier of the picker used by this adaptor.
virtual FWRENDERVTK_API ~IAdaptor() noexcept
destructor
FWSERVICES_API ConfigType getConfigTree() const
Return the configuration, in an boost property tree.
Definition: IService.cpp:247