fw4spl
SPointList.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 #ifndef ANDROID
8 
9 #include "visuVTKAdaptor/SPointList.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/PointList.hpp>
18 #include <fwData/Reconstruction.hpp>
19 
20 #include <fwServices/macros.hpp>
21 #include <fwServices/op/Add.hpp>
22 
23 #include <boost/function.hpp>
24 
25 #include <vtkActor.h>
26 #include <vtkCubeSource.h>
27 #include <vtkPolyDataMapper.h>
28 
29 #include <algorithm>
30 #include <functional>
31 #include <iterator>
32 
33 fwServicesRegisterMacro( ::fwRenderVTK::IAdaptor, ::visuVTKAdaptor::SPointList);
34 
35 namespace visuVTKAdaptor
36 {
37 
38 static const ::fwCom::Slots::SlotKeyType s_ADD_POINT_SLOT = "addPoint";
39 static const ::fwCom::Slots::SlotKeyType s_UPDATE_SPLINE_SLOT = "updateSpline";
40 
41 const ::fwServices::IService::KeyType SPointList::s_POINTLIST_INPUT = "pointList";
42 
43 //------------------------------------------------------------------------------
44 
45 SPointList::SPointList() noexcept :
46  m_radius(7.0),
47  m_interaction(true)
48 {
49  newSlot(s_ADD_POINT_SLOT, &SPointList::addPoint, this);
50  newSlot(s_UPDATE_SPLINE_SLOT, &SPointList::updateSpline, this);
51 
52  m_ptColor = ::fwData::Color::New();
53 }
54 
55 //------------------------------------------------------------------------------
56 
57 SPointList::~SPointList() noexcept
58 {
59 }
60 //------------------------------------------------------------------------------
61 
63 {
64  this->configureParams();
65 
66  const ConfigType config = this->getConfigTree().get_child("config.<xmlattr>");
67 
68  const std::string hexaColor = config.get<std::string>("color", "");
69  m_ptColor = ::fwData::Color::New();
70  if (!hexaColor.empty())
71  {
72  m_ptColor->setRGBA(hexaColor);
73  }
74 
75  const std::string radius = config.get<std::string>("radius", "");
76  if(!radius.empty())
77  {
78  m_radius = std::stod(radius);
79  }
80 
81  const std::string interaction = config.get<std::string>("interaction", "");
82 
83  if (!interaction.empty())
84  {
85  SLM_FATAL_IF("value for 'interaction' must be 'on' or 'off', actual: " + interaction,
86  interaction != "on" && interaction != "off");
87 
88  m_interaction = (interaction == "on");
89  }
90 }
91 
92 //------------------------------------------------------------------------------
93 
95 {
96  this->initialize();
97 
98  m_oldWeakPointList.clear();
99 
100  m_weakPointList = this->getWeakPointList();
101 
102  this->updating();
103 }
104 
105 //------------------------------------------------------------------------------
106 
108 {
109  WeakPointListType points = this->getNewPoints();
110  this->createServices( points );
111  this->requestRender();
112 }
113 
114 //----------------------------------------------------------------------------------------------------------------
115 
116 void SPointList::addPoint(::fwData::Point::sptr /*point*/)
117 {
118  m_oldWeakPointList = m_weakPointList;
119  m_weakPointList = this->getWeakPointList();
120  this->setVtkPipelineModified();
121  this->updating();
122 }
123 
124 //----------------------------------------------------------------------------------------------------------------
125 
126 void SPointList::updateSpline()
127 {
128  this->stopping();
129  this->updating();
130  this->setVtkPipelineModified();
131 }
132 
133 //------------------------------------------------------------------------------
134 
136 {
137  m_oldWeakPointList.clear();
138  m_weakPointList.clear();
139  this->unregisterServices();
140 }
141 
142 //------------------------------------------------------------------------------
143 
144 void SPointList::createServices(WeakPointListType& wPtList)
145 {
146  for( ::fwData::Point::wptr wpt : wPtList )
147  {
148  SLM_ASSERT("Point Expired", !wpt.expired());
149 
150  ::fwData::Point::sptr pt = wpt.lock();
151 
152  // create the srv configuration for objects auto-connection
153  auto pointAdaptor = this->registerService< ::visuVTKAdaptor::SPoint >("::visuVTKAdaptor::SPoint");
154  pointAdaptor->registerInOut(pt, SPoint::s_POINT_INOUT, true);
155 
156  SLM_ASSERT("Bad cast of IAdaptor to Point", pointAdaptor);
157 
158  pointAdaptor->setRenderService(this->getRenderService());
159  pointAdaptor->setRendererId( this->getRendererId() );
160  pointAdaptor->setPickerId( this->getPickerId() );
161  pointAdaptor->start();
162 
163  pointAdaptor->setColor(m_ptColor->red(), m_ptColor->green(), m_ptColor->blue(), m_ptColor->alpha());
164  pointAdaptor->setRadius(m_radius);
165  pointAdaptor->setInteraction(m_interaction);
166  }
167 }
168 
169 //------------------------------------------------------------------------------
170 
171 SPointList::WeakPointListType SPointList::getWeakPointList()
172 {
173  ::fwData::PointList::csptr ptList = this->getInput< ::fwData::PointList >(s_POINTLIST_INPUT);
174  SLM_ASSERT("'pointList' is not defined.", ptList);
175 
176  WeakPointListType weakList;
177 
178  std::copy(ptList->getPoints().begin(), ptList->getPoints().end(), std::back_inserter(weakList));
179 
180  return weakList;
181 }
182 
183 //------------------------------------------------------------------------------
184 
185 SPointList::WeakPointListType SPointList::getNewPoints()
186 {
187  WeakPointListType newPoints;
188 
189  bool isFound;
190  for(::fwData::Point::wptr point : m_weakPointList)
191  {
192  isFound = false;
193  for(::fwData::Point::wptr oldPoint : m_oldWeakPointList)
194  {
195  isFound = (point.lock() == oldPoint.lock());
196  if(isFound)
197  {
198  break;
199  }
200  }
201  if(!isFound)
202  {
203  newPoints.push_back(point);
204  }
205  }
206  return newPoints;
207 }
208 
209 //------------------------------------------------------------------------------
210 
212 {
213  KeyConnectionsMap connections;
214  connections.push(s_POINTLIST_INPUT, ::fwData::PointList::s_MODIFIED_SIG, s_UPDATE_SPLINE_SLOT);
215  connections.push(s_POINTLIST_INPUT, ::fwData::PointList::s_POINT_ADDED_SIG, s_ADD_POINT_SLOT);
216 
217  return connections;
218 }
219 
220 //------------------------------------------------------------------------------
221 
222 void SPointList::setRadius(const double radius)
223 {
224  m_radius = radius;
225 }
226 
227 //------------------------------------------------------------------------------
228 
229 void SPointList::setColor(const fwData::Color::sptr ptColor)
230 {
231  m_ptColor = ptColor;
232 }
233 
234 //------------------------------------------------------------------------------
235 
236 void SPointList::setInteraction(const bool interaction)
237 {
238  m_interaction = interaction;
239 }
240 
241 //------------------------------------------------------------------------------
242 
243 } //namespace visuVTKAdaptor
244 
245 #endif // ANDROID
Adaptor to display a point list.
Definition: SPointList.hpp:52
This class is a helper to define the connections of a service and its data.
Definition: IService.hpp:454
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.
FWRENDERVTK_API void requestRender()
notify a render request iff vtkPipeline is modified
virtual VISUVTKADAPTOR_API KeyConnectionsMap getAutoConnections() const override
Returns proposals to connect service slots to associated object signals, this method is used for obj/...
Definition: SPointList.cpp:211
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 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_POINT_ADDED_SIG
Signal emitted when a Point is added.
VISUVTKADAPTOR_API void updating() override
Perform some computations according to object (this service is attached to) attribute values and its ...
Definition: SPointList.cpp:107
VISUVTKADAPTOR_API void starting() override
Initialize the service activity.
Definition: SPointList.cpp:94
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
VISUVTKADAPTOR_API void configuring() override
Configure the service before starting. Apply the configuration to service.
Definition: SPointList.cpp:62
#define SLM_FATAL_IF(message, cond)
Definition: spyLog.hpp:287
VISUVTKADAPTOR_API void stopping() override
Uninitialize the service activity. The stop() method is always invoked before destroying a service...
Definition: SPointList.cpp:135
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 ConfigType getConfigTree() const
Return the configuration, in an boost property tree.
Definition: IService.cpp:247