fw4spl
SVectorField.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/SVectorField.hpp"
8 
9 #include <fwData/Boolean.hpp>
10 #include <fwData/Color.hpp>
11 #include <fwData/Image.hpp>
12 #include <fwData/String.hpp>
13 #include <fwData/TransferFunction.hpp>
14 
15 #include <fwDataTools/fieldHelper/Image.hpp>
16 #include <fwDataTools/fieldHelper/MedicalImageHelpers.hpp>
17 
18 #include <fwRenderVTK/vtk/fwVtkWindowLevelLookupTable.hpp>
19 
20 #include <fwServices/macros.hpp>
21 
22 #include <fwVtkIO/helper/TransferFunction.hpp>
23 #include <fwVtkIO/vtk.hpp>
24 
25 #include <vtkActor.h>
26 #include <vtkArrowSource.h>
27 #include <vtkAssignAttribute.h>
28 #include <vtkGlyph3D.h>
29 #include <vtkGlyph3DMapper.h>
30 #include <vtkGlyphSource2D.h>
31 #include <vtkImageData.h>
32 #include <vtkMaskPoints.h>
33 #include <vtkPointData.h>
34 #include <vtkPolyDataMapper.h>
35 
36 namespace visuVTKAdaptor
37 {
38 
39 fwServicesRegisterMacro(::fwRenderVTK::IAdaptor, ::visuVTKAdaptor::SVectorField);
40 
41 static const ::fwServices::IService::KeyType s_IMAGE_INPUT = "image";
42 
43 //------------------------------------------------------------------------------
44 
45 SVectorField::SVectorField() noexcept
46 {
47  m_imageData = vtkSmartPointer<vtkImageData>::New();
48 }
49 
50 //------------------------------------------------------------------------------
51 
52 SVectorField::~SVectorField() noexcept
53 {
54 }
55 
56 //------------------------------------------------------------------------------
57 
59 {
60  this->initialize();
61  this->updating();
62 }
63 
64 //------------------------------------------------------------------------------
65 
67 {
68  this->destroyPipeline();
69 }
70 
71 //------------------------------------------------------------------------------
72 
74 {
75  ::fwData::Image::csptr image = this->getInput< ::fwData::Image >(s_IMAGE_INPUT);
77 
78  if (imageIsValid && image->getNumberOfComponents() == 3)
79  {
80  this->buildPipeline();
81  }
82  else
83  {
84  this->destroyPipeline();
85  }
86 }
87 
88 //------------------------------------------------------------------------------
89 
91 {
92  this->configureParams();
93 }
94 
95 //------------------------------------------------------------------------------
96 
97 void SVectorField::buildPipeline( )
98 {
99  ::fwData::Image::csptr image = this->getInput< ::fwData::Image >(s_IMAGE_INPUT);
100 
101  ::fwVtkIO::toVTKImage(image, m_imageData);
102 
103  vtkSmartPointer<vtkArrowSource> arrowSource = vtkSmartPointer<vtkArrowSource>::New();
104 
105  m_arrowSource = arrowSource;
106 
107  vtkSmartPointer<vtkMaskPoints> ptMask = vtkSmartPointer<vtkMaskPoints>::New();
108 
109  ptMask->SetInputData(m_imageData);
110  ptMask->SetOnRatio(1);
111  ptMask->RandomModeOn();
112 
113 // #define USE_GPU_GLYPH
114 #ifndef USE_GPU_GLYPH
115  ptMask->SetMaximumNumberOfPoints(5000);
116 
117  vtkSmartPointer<vtkGlyph3D> glyphFilter = vtkSmartPointer<vtkGlyph3D>::New();
118  glyphFilter->SetSourceConnection(m_arrowSource->GetOutputPort());
119 
120  glyphFilter->ScalingOn();
121 
122  glyphFilter->SetVectorModeToUseVector();
123 
124  glyphFilter->OrientOn();
125 
126  glyphFilter->SetInputConnection(ptMask->GetOutputPort());
127 
128  glyphFilter->SetInputArrayToProcess( 1, m_imageData->GetInformation());
129 
130  vtkSmartPointer<vtkPolyDataMapper> vectorMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
131  vectorMapper->SetInputConnection(glyphFilter->GetOutputPort());
132 #else
133  ptMask->SetMaximumNumberOfPoints(15000);
134 
135  vtkSmartPointer<vtkGlyph3DMapper> vectorMapper = vtkSmartPointer<vtkGlyph3DMapper>::New();
136  vectorMapper->SetOrientationArray(vtkDataSetAttributes::SCALARS);
137  vectorMapper->SetSourceConnection(m_arrowSource->GetOutputPort());
138  vectorMapper->OrientOn();
139  vectorMapper->SetInputConnection(ptMask->GetOutputPort());
140 
141  SLM_DEBUG("USE_GPU");
142 #endif
143 
144  vtkSmartPointer<vtkActor> vectorActor = vtkSmartPointer<vtkActor>::New();
145  vectorActor->SetMapper(vectorMapper);
146 
147  this->addToRenderer(vectorActor);
148 
149  this->setVtkPipelineModified();
150  this->requestRender();
151 }
152 
153 //------------------------------------------------------------------------------
154 
155 void SVectorField::destroyPipeline( )
156 {
158  this->setVtkPipelineModified();
159  this->requestRender();
160 }
161 
162 //------------------------------------------------------------------------------
163 
165 {
166  KeyConnectionsMap connections;
167  connections.push(s_IMAGE_INPUT, ::fwData::Image::s_MODIFIED_SIG, s_UPDATE_SLOT);
168  connections.push(s_IMAGE_INPUT, ::fwData::Image::s_BUFFER_MODIFIED_SIG, s_UPDATE_SLOT);
169 
170  return connections;
171 }
172 
173 //------------------------------------------------------------------------------
174 
175 } //namespace visuVTKAdaptor
virtual VISUVTKADAPTOR_API KeyConnectionsMap getAutoConnections() const override
Returns proposals to connect service slots to associated object signals, this method is used for obj/...
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...
FWRENDERVTK_API void configureParams()
Parse the xml configuration for renderer, picker and transform.
VISUVTKADAPTOR_API void updating() override
Perform some computations according to object (this service is attached to) attribute values and its ...
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 ...
#define SLM_DEBUG(message)
Definition: spyLog.hpp:239
FWRENDERVTK_API void removeAllPropFromRenderer()
Removes all the vtkProp from the renderer.
VISUVTKADAPTOR_API void starting() override
Initialize the service activity.
VISUVTKADAPTOR_API void stopping() override
Uninitialize the service activity. The stop() method is always invoked before destroying a service...
VISUVTKADAPTOR_API void configuring() override
Configure the service before starting. Apply the configuration to service.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_BUFFER_MODIFIED_SIG
Type of signal when image&#39;s buffer is added.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_MODIFIED_SIG
Key in m_signals map of signal m_sigModified.
static FWDATATOOLS_API bool checkImageValidity(::fwData::Image::csptr _pImg)
Check if the image is valid.
FWRENDERVTK_API void initialize()
Initialize the adaptor with the associated render service. (must be call in starting).
This adaptor displays vector field contains in a fwData::Image.
static FWSERVICES_APIconst::fwCom::Slots::SlotKeyType s_UPDATE_SLOT
Slot to call start method.
Definition: IService.hpp:177