fw4spl
SPlaneInteractor.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2017.
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/SPlaneInteractor.hpp"
10 
11 #include <fwCom/Signal.hpp>
12 #include <fwCom/Signal.hxx>
13 #include <fwCom/Signals.hpp>
14 
15 #include <fwData/Plane.hpp>
16 
17 #include <fwMath/PlaneFunctions.hpp>
18 
19 #include <fwServices/macros.hpp>
20 
21 #include <vtkCommand.h>
22 #include <vtkRenderWindowInteractor.h>
23 
25 
26 namespace visuVTKAdaptor
27 {
28 
29 static const ::fwServices::IService::KeyType s_PLANE_INOUT = "plane";
30 
31 class SPlaneInteractorCallback : public vtkCommand
32 {
33 public:
34  //------------------------------------------------------------------------------
35 
36  static SPlaneInteractorCallback* New()
37  {
38  return new SPlaneInteractorCallback();
39  }
40 
42  {
43  }
44 
46  {
47  }
48 
49  //------------------------------------------------------------------------------
50 
51  virtual void Execute( vtkObject* caller, unsigned long eventId, void*)
52  {
53  if ( eventId == vtkCommand::KeyPressEvent)
54  {
55  vtkRenderWindowInteractor* rwi = vtkRenderWindowInteractor::SafeDownCast(caller);
56  SLM_ASSERT("invalid vtkRenderWindowInteractor", rwi);
57  SLM_ASSERT("invalid m_adaptor", m_adaptor);
58 
59  char* keySym = rwi->GetKeySym();
60 
61  if (std::string(keySym) == "space")
62  {
63  m_adaptor->switchPlaneNormal();
64  }
65  else if (std::string(keySym) == "Escape" || std::string(keySym) == "Tab")
66  {
67  m_adaptor->deselectPlane();
68  }
69  }
70  else if (eventId == vtkCommand::MouseWheelForwardEvent)
71  {
72  SetAbortFlag(1);
73  m_adaptor->pushPlane(1);
74  }
75  else if (eventId == vtkCommand::MouseWheelBackwardEvent)
76  {
77  SetAbortFlag(1);
78  m_adaptor->pushPlane(-1);
79  }
80  }
81 
82  //------------------------------------------------------------------------------
83 
84  void setAdaptor( SPlaneInteractor::sptr adaptor)
85  {
86  m_adaptor = adaptor;
87  }
88 
89 protected:
90  SPlaneInteractor::sptr m_adaptor;
91 
92 };
93 
94 //------------------------------------------------------------------------------
95 
96 SPlaneInteractor::SPlaneInteractor() noexcept :
97  m_vtkObserver(nullptr),
98  m_priority(1.f)
99 {
100 }
101 
102 //------------------------------------------------------------------------------
103 
104 SPlaneInteractor::~SPlaneInteractor() noexcept
105 {
106 }
107 
108 //------------------------------------------------------------------------------
109 
111 {
112  this->configureParams();
113 }
114 
115 //------------------------------------------------------------------------------
116 
118 {
119  this->initialize();
120 
121  if (this->getInOut< ::fwData::Plane >(s_PLANE_INOUT))
122  {
123  SPlaneInteractorCallback* observer = SPlaneInteractorCallback::New();
124  observer->setAdaptor( SPlaneInteractor::dynamicCast(this->getSptr()) );
125 
126  m_vtkObserver = observer;
127 
128  this->getInteractor()->AddObserver(vtkCommand::KeyPressEvent, m_vtkObserver, m_priority);
129  this->getInteractor()->AddObserver(vtkCommand::KeyReleaseEvent, m_vtkObserver, m_priority);
130  this->getInteractor()->AddObserver(vtkCommand::MouseWheelForwardEvent, m_vtkObserver, m_priority);
131  this->getInteractor()->AddObserver(vtkCommand::MouseWheelBackwardEvent, m_vtkObserver, m_priority);
132  }
133 }
134 
135 //------------------------------------------------------------------------------
136 
138 {
139 }
140 
141 //------------------------------------------------------------------------------
142 
144 {
145  if(m_vtkObserver)
146  {
147  this->getInteractor()->RemoveObservers(vtkCommand::KeyPressEvent, m_vtkObserver);
148  this->getInteractor()->RemoveObservers(vtkCommand::KeyReleaseEvent, m_vtkObserver);
149  this->getInteractor()->RemoveObservers(vtkCommand::MouseWheelForwardEvent, m_vtkObserver);
150  this->getInteractor()->RemoveObservers(vtkCommand::MouseWheelBackwardEvent, m_vtkObserver);
151  m_vtkObserver->Delete();
152  m_vtkObserver = nullptr;
153  }
154 }
155 
156 //------------------------------------------------------------------------------
157 
159 {
160  ::fwData::Plane::sptr plane = this->getInOut< ::fwData::Plane >(s_PLANE_INOUT);
161 
162  if (plane)
163  {
164  ::fwData::Point::sptr pt0 = plane->getPoints()[0];
165  ::fwData::Point::sptr pt1 = plane->getPoints()[1];
166  ::fwData::Point::sptr pt2 = plane->getPoints()[2];
167  if ( pt0 && pt1 && pt2 )
168  {
169  plane->setValue(pt0, pt2, pt1);
170 
172  sig->asyncEmit();
173  }
174  this->setVtkPipelineModified();
175  }
176 }
177 
178 //------------------------------------------------------------------------------
179 
180 void SPlaneInteractor::pushPlane(double factor)
181 {
182  ::fwData::Plane::sptr plane = this->getInOut< ::fwData::Plane >(s_PLANE_INOUT);
183  if (plane)
184  {
185  ::fwData::Point::sptr pt0 = plane->getPoints()[0];
186  ::fwData::Point::sptr pt1 = plane->getPoints()[1];
187  ::fwData::Point::sptr pt2 = plane->getPoints()[2];
188 
189  fwPlane planeDesc;
190  ::fwMath::setValues(planeDesc, pt0->getCoord(), pt1->getCoord(), pt2->getCoord());
191 
192  fwVec3d normal = ::fwMath::getNormal(planeDesc);
193  if ( pt0 && pt1 && pt2 )
194  {
195  fwVec3d vec0 = pt0->getCoord();
196  fwVec3d vec1 = pt1->getCoord();
197  fwVec3d vec2 = pt2->getCoord();
198 
199  vec0 = vec0 + normal*factor;
200  vec1 = vec1 + normal*factor;
201  vec2 = vec2 + normal*factor;
202 
203  pt0->setCoord(vec0);
204  pt1->setCoord(vec1);
205  pt2->setCoord(vec2);
206 
207  plane->setValue(pt0, pt1, pt2);
208  ::fwMath::setValues(planeDesc, pt0->getCoord(), pt1->getCoord(), pt2->getCoord());
209  normal = ::fwMath::getNormal(planeDesc);
210 
211  ::fwData::Object::ModifiedSignalType::sptr sig;
213  sig->asyncEmit();
214 
216  sig->asyncEmit();
217 
219  sig->asyncEmit();
220 
221  this->setVtkPipelineModified();
222  }
223  }
224 }
225 
226 //------------------------------------------------------------------------------
227 
229 {
230  ::fwData::Plane::csptr plane = this->getInOut< ::fwData::Plane >(s_PLANE_INOUT);
231  if (plane)
232  {
234  sig->asyncEmit(false);
235  }
236 }
237 
238 } //namespace visuVTKAdaptor
239 
240 #endif // ANDROID
VISUVTKADAPTOR_API void updating() override
Perform some computations according to object (this service is attached to) attribute values and its ...
The namespace visuVTKAdaptor contains the list of adaptors available for the generic scene...
VISUVTKADAPTOR_API void configuring() override
Configure the service before starting. Apply the configuration to service.
VISUVTKADAPTOR_API void stopping() override
Uninitialize the service activity. The stop() method is always invoked before destroying a service...
VISUVTKADAPTOR_API void switchPlaneNormal()
Switch plane normal.
This service allows to interact with the plane (switch normal, deselect, move along the normal) ...
VISUVTKADAPTOR_API void starting() override
Initialize the service activity.
#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
Base class for each data object.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_MODIFIED_SIG
Key in m_signals map of signal m_sigModified.
VISUVTKADAPTOR_API void deselectPlane()
Deselect the plane.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_SELECTED_SIG
Signal emitted when plane is selected/deselected.
Definition: Plane.hpp:75
VISUVTKADAPTOR_API void pushPlane(double factor)
Move the plane along the normal.