fw4spl
SPlane.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/SPlane.hpp"
10 
11 #include "visuVTKAdaptor/SPoint.hpp"
12 
13 #include <fwCom/Signal.hpp>
14 #include <fwCom/Signal.hxx>
15 #include <fwCom/Slot.hpp>
16 #include <fwCom/Slot.hxx>
17 #include <fwCom/Slots.hpp>
18 #include <fwCom/Slots.hxx>
19 
20 #include <fwData/Color.hpp>
21 #include <fwData/Plane.hpp>
22 
23 #include <fwMath/IntrasecTypes.hpp>
24 #include <fwMath/PlaneFunctions.hpp>
25 
26 #include <fwRenderVTK/vtk/Helpers.hpp>
27 #include <fwRenderVTK/vtk/MarkedSphereHandleRepresentation.hpp>
28 
29 #include <fwServices/macros.hpp>
30 #include <fwServices/op/Add.hpp>
31 
32 #include <vtkActor.h>
33 #include <vtkPlaneCollection.h>
34 #include <vtkPolyDataMapper.h>
35 #include <vtkPropCollection.h>
36 #include <vtkProperty.h>
37 #include <vtkRenderer.h>
38 #include <vtkRenderWindowInteractor.h>
39 
40 fwServicesRegisterMacro( ::fwRenderVTK::IAdaptor, ::visuVTKAdaptor::SPlane, ::fwData::Plane );
41 
42 namespace visuVTKAdaptor
43 {
44 
45 static const ::fwCom::Slots::SlotKeyType s_UPDATE_POINTS_SLOT = "updatePoints";
46 static const ::fwCom::Slots::SlotKeyType s_START_INTERACTION_SLOT = "startInteraction";
47 static const ::fwCom::Slots::SlotKeyType s_SELECT_PLANE_SLOT = "selectPlane";
48 
49 const ::fwCom::Signals::SignalKeyType SPlane::s_INTERACTION_STARTED_SIG = "interactionStarted";
50 
51 const ::fwServices::IService::KeyType SPlane::s_PLANE_INOUT = "plane";
52 
53 //------------------------------------------------------------------------------
54 
55 SPlane::SPlane() noexcept :
56  m_vtkPlane(nullptr),
57  m_actorPlan(nullptr),
58  m_vtkImplicitPlane(nullptr),
59  m_vtkPlaneCollection(nullptr)
60 {
61  newSlot(s_UPDATE_POINTS_SLOT, &SPlane::updatePoints, this);
62  newSlot(s_START_INTERACTION_SLOT, &SPlane::startInteraction, this);
63  newSlot(s_SELECT_PLANE_SLOT, &SPlane::selectPlane, this);
64 
65  newSignal< InteractionStartedSignalType >(s_INTERACTION_STARTED_SIG);
66 }
67 
68 //------------------------------------------------------------------------------
69 
70 SPlane::~SPlane() noexcept
71 {
72 }
73 
74 //------------------------------------------------------------------------------
75 
77 {
78  this->configureParams();
79 
80  const ConfigType config = this->getConfigTree().get_child("config.<xmlattr>");
81 
82  m_planeCollectionId = config.get("planecollection", "");
83 }
84 
85 //------------------------------------------------------------------------------
86 
88 {
89  this->initialize();
90 
91  ::fwData::Plane::csptr plane = this->getInOut< ::fwData::Plane >(s_PLANE_INOUT);
92  SLM_ASSERT("Plane is missing", plane);
93 
94  for( const ::fwData::Point::sptr& point : plane->getPoints() )
95  {
96  // create the srv configuration for objects auto-connection
97  auto servicePoint = this->registerService< ::fwRenderVTK::IAdaptor >("::visuVTKAdaptor::SPoint");
98  servicePoint->registerInOut(point, SPoint::s_POINT_INOUT, true);
99 
100  servicePoint->setRenderService(this->getRenderService());
101  servicePoint->setRendererId( this->getRendererId() );
102  servicePoint->setPickerId( this->getPickerId() );
103  servicePoint->start();
104 
105  m_connections.connect(point, ::fwData::Object::s_MODIFIED_SIG, this->getSptr(), s_UPDATE_POINTS_SLOT);
106  m_connections.connect(servicePoint, ::visuVTKAdaptor::SPoint::s_INTERACTION_STARTED_SIG,
107  this->getSptr(), s_START_INTERACTION_SLOT);
108  }
109 
110  if (!m_planeCollectionId.empty())
111  {
112  m_vtkPlaneCollection = vtkPlaneCollection::SafeDownCast(this->getVtkObject(m_planeCollectionId));
113  }
114 
115  if (m_vtkPlaneCollection)
116  {
117  m_vtkImplicitPlane = vtkPlane::New();
118  m_vtkPlaneCollection->AddItem(m_vtkImplicitPlane);
119  }
120 
121  this->setVtkPipelineModified();
122  this->updating();
123 }
124 
125 //------------------------------------------------------------------------------
126 
128 {
129  ::fwData::Plane::csptr plane = this->getInOut< ::fwData::Plane >(s_PLANE_INOUT);
130  SLM_ASSERT("Plane is missing", plane);
131 
132  ::fwData::Point::csptr pt0 = plane->getPoints()[0];
133  ::fwData::Point::csptr pt1 = plane->getPoints()[1];
134  ::fwData::Point::csptr pt2 = plane->getPoints()[2];
135 
136  fwPlane planeDesc;
137  ::fwMath::setValues(planeDesc, pt0->getCoord(), pt1->getCoord(), pt2->getCoord());
138 
139  if(m_vtkImplicitPlane)
140  {
141  fwVec3d normal = ::fwMath::getNormal(planeDesc);
142 
143  m_vtkImplicitPlane->SetOrigin(pt0->getCoord()[0], pt0->getCoord()[1], pt0->getCoord()[2]);
144  m_vtkImplicitPlane->SetNormal(normal[0], normal[1], normal[2]);
145  m_vtkImplicitPlane->Modified();
146  }
147  this->setVtkPipelineModified();
148  this->requestRender();
149 }
150 
151 //------------------------------------------------------------------------------
152 
154 {
155  this->updating();
156 
157  ::fwData::Plane::csptr plane = this->getInOut< ::fwData::Plane >(s_PLANE_INOUT);
158  SLM_ASSERT("Plane is missing", plane);
159 
161  {
162  ::fwCom::Connection::Blocker block(sig->getConnection(m_slotUpdate));
163  sig->asyncEmit();
164  }
165 }
166 
167 //------------------------------------------------------------------------------
168 
170 {
171  ::fwData::Plane::sptr plane = this->getInOut< ::fwData::Plane >(s_PLANE_INOUT);
172  SLM_ASSERT("Plane is missing", plane);
173 
174  this->selectPlane(true);
175 
176  auto sig = this->signal< InteractionStartedSignalType >( s_INTERACTION_STARTED_SIG);
177  sig->asyncEmit(plane);
178 }
179 
180 //------------------------------------------------------------------------------
181 
183 {
184  if (m_vtkPlaneCollection && m_vtkImplicitPlane)
185  {
186  m_vtkPlaneCollection->RemoveItem(m_vtkImplicitPlane);
187  m_vtkImplicitPlane->Delete();
188  m_vtkImplicitPlane = 0;
189  }
190 
191  m_connections.disconnect();
192 
193  this->unregisterServices();
195  this->requestRender();
196 }
197 
198 //------------------------------------------------------------------------------
199 
200 void SPlane::setVtkPlaneCollection( vtkObject* col )
201 {
202  if (m_vtkPlaneCollection != col)
203  {
204  if (m_vtkPlaneCollection)
205  {
206  if (m_vtkImplicitPlane)
207  {
208  m_vtkPlaneCollection->RemoveItem(m_vtkImplicitPlane);
209  }
210  m_vtkPlaneCollection = 0;
211  }
212 
213  if ( col )
214  {
215  m_vtkPlaneCollection = vtkPlaneCollection::SafeDownCast(col);
216  if (m_vtkImplicitPlane)
217  {
218  m_vtkPlaneCollection->AddItem(m_vtkImplicitPlane);
219  }
220  }
221  }
222  this->setVtkPipelineModified();
223  this->requestRender();
224 }
225 
226 //------------------------------------------------------------------------------
227 
228 void SPlane::selectPlane(bool select)
229 {
230  auto subServices = this->getRegisteredServices();
231  for (const ::fwServices::IService::wptr& adaptor: subServices)
232  {
233  auto servicePoint = ::visuVTKAdaptor::SPoint::dynamicCast(adaptor.lock());
234  if(servicePoint)
235  {
236  if(select)
237  {
238  servicePoint->setColor(1., 0., 0.);
239  }
240  else
241  {
242  servicePoint->setColor(1., 1., 1.);
243  }
244  }
245  }
246  this->setVtkPipelineModified();
247  this->requestRender();
248 }
249 
250 //------------------------------------------------------------------------------
251 
253 {
254  KeyConnectionsMap connections;
255  connections.push(s_PLANE_INOUT, ::fwData::Plane::s_MODIFIED_SIG, s_UPDATE_SLOT);
256  connections.push(s_PLANE_INOUT, ::fwData::Plane::s_SELECTED_SIG, s_SELECT_PLANE_SLOT);
257 
258  return connections;
259 }
260 
261 //------------------------------------------------------------------------------
262 
263 } //namespace visuVTKAdaptor
264 
265 #endif //ANDROID
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.
FWCOM_API void disconnect()
Disconnect all registered connections and clear m_connections.
This class defines a plane defined by tree points.
Definition: Plane.hpp:25
Class allowing to block a Connection.
Definition: Connection.hpp:20
void startInteraction()
Re-notify "startInteraction" for this service.
Definition: SPlane.cpp:169
The namespace visuVTKAdaptor contains the list of adaptors available for the generic scene...
void updatePoints()
Update the plane from the points and notify planes is modified.
Definition: SPlane.cpp:153
VISUVTKADAPTOR_API void configuring() override
Configure the service before starting. Apply the configuration to service.
Definition: SPlane.cpp:76
FWRENDERVTK_API void configureParams()
Parse the xml configuration for renderer, picker and transform.
static VISUVTKADAPTOR_APIconst::fwCom::Signals::SignalKeyType s_INTERACTION_STARTED_SIG
Type of signal when point interaction is started.
Definition: SPoint.hpp:81
VISUVTKADAPTOR_API void stopping() override
Uninitialize the service activity. The stop() method is always invoked before destroying a service...
Definition: SPlane.cpp:182
static VISUVTKADAPTOR_APIconst::fwCom::Signals::SignalKeyType s_INTERACTION_STARTED_SIG
Type of signal when plane interaction is started (store current plane)
Definition: SPlane.hpp:84
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 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.
void selectPlane(bool select)
Changes points color (red is selected, else white)
Definition: SPlane.cpp:228
UpdateSlotType::sptr m_slotUpdate
Slot to call update method.
Definition: IService.hpp:690
virtual VISUVTKADAPTOR_API KeyConnectionsMap getAutoConnections() const override
Returns proposals to connect service slots to associated object signals, this method is used for obj/...
Definition: SPlane.cpp:252
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
Represents a Plane that can be interacted with 3 points.
Definition: SPlane.hpp:63
VISUVTKADAPTOR_API void starting() override
Initialize the service activity.
Definition: SPlane.cpp:87
VISUVTKADAPTOR_API void updating() override
Perform some computations according to object (this service is attached to) attribute values and its ...
Definition: SPlane.cpp:127
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).
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_SELECTED_SIG
Signal emitted when plane is selected/deselected.
Definition: Plane.hpp:75
FWCOM_API void connect(const ::fwCom::HasSignals::csptr &hasSignals,::fwCom::Signals::SignalKeyType signalKey, const ::fwCom::HasSlots::csptr &hasSlots,::fwCom::Slots::SlotKeyType slotKey)
Connect signal to slot, and register this new connection in m_connections.
static FWSERVICES_APIconst::fwCom::Slots::SlotKeyType s_UPDATE_SLOT
Slot to call start method.
Definition: IService.hpp:177
FWSERVICES_API ConfigType getConfigTree() const
Return the configuration, in an boost property tree.
Definition: IService.cpp:247