fw4spl
SImage3DCursor.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 #include "visuVTKAdaptor/SImage3DCursor.hpp"
8 
9 #include <fwCom/Slot.hxx>
10 #include <fwCom/Slots.hxx>
11 
12 #include <fwData/Color.hpp>
13 #include <fwData/Float.hpp>
14 #include <fwData/Image.hpp>
15 
16 #include <fwServices/macros.hpp>
17 
18 #include <vtkActor.h>
19 #include <vtkPolyData.h>
20 #include <vtkPolyDataMapper.h>
21 #include <vtkProperty.h>
22 #include <vtkSphereSource.h>
23 #include <vtkTransform.h>
24 
25 fwServicesRegisterMacro( ::fwRenderVTK::IAdaptor, ::visuVTKAdaptor::SImage3DCursor );
26 
27 namespace visuVTKAdaptor
28 {
29 
30 //------------------------------------------------------------------------------
31 
32 static const ::fwCom::Slots::SlotKeyType s_UPDATE_SLICE_INDEX_SLOT = "updateSliceIndex";
33 static const ::fwCom::Slots::SlotKeyType s_UPDATE_SPHERE_SLOT = "updateSphere";
34 
35 static const ::fwServices::IService::KeyType s_IMAGE_INOUT = "image";
36 
37 //------------------------------------------------------------------------------
38 
39 SImage3DCursor::SImage3DCursor() noexcept
40 {
41  newSlot(s_UPDATE_SLICE_INDEX_SLOT, &SImage3DCursor::updateSliceIndex, this);
42  newSlot(s_UPDATE_SPHERE_SLOT, &SImage3DCursor::updateSphere, this);
43 }
44 
45 //------------------------------------------------------------------------------
46 
47 SImage3DCursor::~SImage3DCursor() noexcept
48 {
49 }
50 
51 //------------------------------------------------------------------------------
52 void SImage3DCursor::setVisibility( bool visibility )
53 {
54  m_cursorActor->SetVisibility(visibility);
55  this->setVtkPipelineModified();
56  this->updating();
57 }
58 
59 //------------------------------------------------------------------------------
60 
62 {
63  this->configureParams();
64 }
65 
66 //------------------------------------------------------------------------------
67 
69 {
70  this->initialize();
71 
72  m_cursorPolyData = vtkSmartPointer<vtkPolyData>::New();
73  m_cursorMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
74  m_cursorActor = vtkSmartPointer<vtkActor>::New();
75 
76  ::fwData::Image::sptr img = this->getInOut< ::fwData::Image >(s_IMAGE_INOUT);
77 
78  if ( img->getField("IMAGE3DCURSOR_RADIUS") && img->getField("IMAGE3DCURSOR_COLOR") )
79  {
80  ::fwData::Float::sptr radius = img->getField< ::fwData::Float >("IMAGE3DCURSOR_RADIUS");
81  ::fwData::Color::sptr color = img->getField< ::fwData::Color >("IMAGE3DCURSOR_COLOR");
82 
83  this->buildPolyData(radius->value());
84  m_cursorActor->GetProperty()->SetColor( color->red(), color->green(), color->blue());
85  }
86  else
87  {
88  this->buildPolyData();
89  m_cursorActor->GetProperty()->SetColor(.1, .6, 1.);
90  }
91 
92  m_cursorMapper->SetInputData( m_cursorPolyData );
93  m_cursorActor->SetMapper(m_cursorMapper);
94 
95  if(!this->getTransformId().empty())
96  {
97  m_cursorActor->SetUserTransform(this->getTransform());
98  }
99  this->addToRenderer(m_cursorActor);
100  this->updating();
101 }
102 
103 //------------------------------------------------------------------------------
104 
106 {
108  this->requestRender();
109  m_cursorPolyData = 0;
110  m_cursorMapper = 0;
111  m_cursorActor = 0;
112 }
113 
114 //------------------------------------------------------------------------------
115 
117 {
118  ::fwData::Image::sptr image = this->getInOut< ::fwData::Image >(s_IMAGE_INOUT);
119  this->updateImageInfos(image);
120  int index[3] = { *m_sagittalIndex, *m_frontalIndex, *m_axialIndex };
121  double center[3];
122  this->sliceIndexToWorld(index, center);
123  this->updateCursorPosition(center);
124  this->requestRender();
125 }
126 
127 //------------------------------------------------------------------------------
128 
130 {
131  ::fwData::Image::sptr image = this->getInOut< ::fwData::Image >(s_IMAGE_INOUT);
132  this->updateImageInfos(image);
133 }
134 
135 //-----------------------------------------------------------------------------
136 
137 void SImage3DCursor::updateSliceIndex(int axial, int frontal, int sagittal)
138 {
139  m_axialIndex->value() = axial;
140  m_frontalIndex->value() = frontal;
141  m_sagittalIndex->value() = sagittal;
142 
143  int index[3] = {sagittal, frontal, axial};
144  double center[3];
145  this->sliceIndexToWorld(index, center);
146  this->updateCursorPosition(center);
147  this->requestRender();
148 }
149 
150 //------------------------------------------------------------------------------
151 
152 void SImage3DCursor::updateSphere(::fwData::Color::sptr color, float radius)
153 {
154  ::fwData::Image::sptr img = this->getInOut< ::fwData::Image >(s_IMAGE_INOUT);
155 
156  m_cursorActor->GetProperty()->SetColor( color->red(), color->green(), color->blue());
157  this->buildPolyData(radius);
158 
159  m_cursorMapper->SetInputData( m_cursorPolyData );
160  this->setVtkPipelineModified();
161  this->requestRender();
162 }
163 
164 //------------------------------------------------------------------------------
165 
166 void SImage3DCursor::updateCursorPosition( double world[3] )
167 {
168  m_cursorActor->SetPosition(world);
169  this->setVtkPipelineModified();
170 }
171 
172 //------------------------------------------------------------------------------
173 
174 void SImage3DCursor::buildPolyData(float radius)
175 {
176  // point are stored Left,right,up,down
177  vtkSmartPointer<vtkSphereSource> polySource = vtkSmartPointer<vtkSphereSource>::New();
178  polySource->SetCenter(0.0, 0.0, 0.0);
179  polySource->SetRadius(radius);
180  polySource->SetPhiResolution(8);
181  polySource->SetThetaResolution(8);
182 
183  polySource->SetOutput(m_cursorPolyData);
184  polySource->Update();
185 }
186 
187 //------------------------------------------------------------------------------
188 
190 {
191  KeyConnectionsMap connections;
192  connections.push("image", ::fwData::Image::s_SLICE_INDEX_MODIFIED_SIG, s_UPDATE_SLICE_INDEX_SLOT );
193 
194  return connections;
195 }
196 
197 //-----------------------------------------------------------------------------
198 
199 } //namespace visuVTKAdaptor
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_SLICE_INDEX_MODIFIED_SIG
Type of signal when image&#39;s buffer is added.
virtual VISUVTKADAPTOR_API void starting() override
Initialize the service activity.
This class is a helper to define the connections of a service and its data.
Definition: IService.hpp:454
FWRENDERVTK_API void addToRenderer(vtkProp *prop)
Adds the vtkProp to the renderer.
The namespace visuVTKAdaptor contains the list of adaptors available for the generic scene...
::fwData::Integer::sptr m_axialIndex
Axial slice index.
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
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 removeAllPropFromRenderer()
Removes all the vtkProp from the renderer.
::fwData::Integer::sptr m_sagittalIndex
Sagittal slice index.
FWDATA_API::fwData::Object::sptr getField(const FieldNameType &name,::fwData::Object::sptr defaultValue=::fwData::Object::sptr()) const
Returns a pointer of corresponding field (null if non exist).
Add a 3D spatial marker represented by a sphere that identify the intersection point of the selected ...
virtual VISUVTKADAPTOR_API void updating() override
Perform some computations according to object (this service is attached to) attribute values and its ...
FWRENDERVTK_API vtkTransform * getTransform()
Returns the transform used by this adaptor.
This class defines color object.
FWDATATOOLS_API void sliceIndexToWorld(const int index[3], double world[3])
Convert from world coordinates system to image coordinates system.
FWDATATOOLS_API void updateImageInfos(::fwData::Image::sptr image)
Update the image information (slice index, min/max,...)
FWRENDERVTK_API SRender::VtkObjectIdType getTransformId() const
Returns the identifier of the transform used by this adaptor.
virtual VISUVTKADAPTOR_API KeyConnectionsMap getAutoConnections() const override
Returns proposals to connect service slots to associated object signals, this method is used for obj/...
::fwData::Integer::sptr m_frontalIndex
Frontal slice index.
virtual VISUVTKADAPTOR_API void configuring() override
Configure the service before starting. Apply the configuration to service.
FWRENDERVTK_API void initialize()
Initialize the adaptor with the associated render service. (must be call in starting).
virtual VISUVTKADAPTOR_API void swapping() override
Swap the service from associated object to another object.
This class contains an float value.
Definition: Float.hpp:25
virtual VISUVTKADAPTOR_API void stopping() override
Uninitialize the service activity. The stop() method is always invoked before destroying a service...