fw4spl
SMeshNormals.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 #include "visuVTKAdaptor/SMeshNormals.hpp"
8 
9 #include <fwCom/Slot.hpp>
10 #include <fwCom/Slot.hxx>
11 #include <fwCom/Slots.hpp>
12 #include <fwCom/Slots.hxx>
13 
14 #include <fwData/Mesh.hpp>
15 
16 #include <fwServices/macros.hpp>
17 
18 #include <fwVtkIO/helper/Mesh.hpp>
19 
20 #include <boost/assign/list_of.hpp>
21 
22 #include <vtkActor.h>
23 #include <vtkArrowSource.h>
24 #include <vtkCellCenters.h>
25 #include <vtkGlyph3D.h>
26 #include <vtkGlyphSource2D.h>
27 #include <vtkMaskPoints.h>
28 #include <vtkPolyDataMapper.h>
29 #include <vtkProperty.h>
30 
31 fwServicesRegisterMacro( ::fwRenderVTK::IAdaptor, ::visuVTKAdaptor::SMeshNormals);
32 
33 namespace visuVTKAdaptor
34 {
35 
36 const ::fwServices::IService::KeyType SMeshNormals::s_MESH_INPUT = "mesh";
37 
38 //------------------------------------------------------------------------------
39 
40 std::map< std::string, SMeshNormals::NormalRepresentation >
41 SMeshNormals::m_normalRepresentationConversion
42  = ::boost::assign::map_list_of(std::string("POINT"), POINT_NORMAL)
43  (std::string("CELL"), CELL_NORMAL)
44  (std::string("NONE"), NONE);
45 
46 //------------------------------------------------------------------------------
47 
48 const ::fwCom::Slots::SlotKeyType SMeshNormals::s_UPDATE_VERTEX_SLOT = "updateVertex";
49 const ::fwCom::Slots::SlotKeyType SMeshNormals::s_UPDATE_POINT_NORMALS_SLOT = "updatePointNormals";
50 const ::fwCom::Slots::SlotKeyType SMeshNormals::s_UPDATE_CELL_NORMALS_SLOT = "updateCellNormals";
51 const ::fwCom::Slots::SlotKeyType SMeshNormals::s_SHOW_POINT_NORMALS_SLOT = "showPointNormals";
52 const ::fwCom::Slots::SlotKeyType SMeshNormals::s_SHOW_CELL_NORMALS_SLOT = "showCellNormals";
53 const ::fwCom::Slots::SlotKeyType SMeshNormals::s_HIDE_NORMALS_SLOT = "hideNormals";
54 const ::fwCom::Slots::SlotKeyType SMeshNormals::s_UPDATE_NORMAL_MODE_SLOT = "updateNormalMode";
55 
56 //------------------------------------------------------------------------------
57 
58 SMeshNormals::SMeshNormals() noexcept :
59  m_normalRepresentation(CELL_NORMAL)
60 {
61  m_actor = vtkActor::New();
62 
70 }
71 
72 //------------------------------------------------------------------------------
73 
74 SMeshNormals::~SMeshNormals() noexcept
75 {
76  m_actor->Delete();
77  m_actor = 0;
78 }
79 
80 //------------------------------------------------------------------------------
81 
83 {
84  this->configureParams();
85 
86  const ConfigType config = this->getConfigTree().get_child("config.<xmlattr>");
87 
88  if(config.count("normal") )
89  {
90  const std::string normal = config.get<std::string>("normal");
91  SLM_ASSERT("Wrong normal representation '" + normal + "' (required POINT, CELL or NONE)",
92  m_normalRepresentationConversion.find(normal) != m_normalRepresentationConversion.end());
93 
94  m_normalRepresentation = m_normalRepresentationConversion[normal];
95  }
96 }
97 
98 //------------------------------------------------------------------------------
99 
101 {
102  this->initialize();
103  this->updating();
104  this->addToRenderer(this->getActor());
105  this->requestRender();
106 }
107 
108 //------------------------------------------------------------------------------
109 
111 {
112  this->updateMeshNormals();
113  this->requestRender();
114 }
115 
116 //------------------------------------------------------------------------------
117 
118 vtkActor* SMeshNormals::getActor() const
119 {
120  return m_actor;
121 }
122 
123 //------------------------------------------------------------------------------
124 
125 void SMeshNormals::setPolyData(vtkSmartPointer< vtkPolyData > polydata)
126 {
127  if (polydata)
128  {
129  m_polyData = polydata;
130  }
131 }
132 
133 //------------------------------------------------------------------------------
134 
135 void SMeshNormals::updateMeshNormals()
136 {
137  ::fwData::Mesh::csptr mesh = this->getInput< ::fwData::Mesh >(s_MESH_INPUT);
138  SLM_ASSERT("Missing mesh", mesh);
139 
140  if(m_normalRepresentation == NONE)
141  {
142  m_actor->SetVisibility( false );
143  }
144  else
145  {
146  m_polyData = vtkSmartPointer< vtkPolyData >::New();
147  ::fwVtkIO::helper::Mesh::toVTKMesh(mesh, m_polyData);
148 
149  vtkSmartPointer<vtkPolyDataAlgorithm> algo;
150  if(m_normalRepresentation == CELL_NORMAL)
151  {
152  algo = vtkSmartPointer<vtkCellCenters>::New();
153  }
154  else if(m_normalRepresentation == POINT_NORMAL)
155  {
156  vtkSmartPointer<vtkMaskPoints> ptMask = vtkSmartPointer<vtkMaskPoints>::New();
157  ptMask->SetOnRatio(1);
158  ptMask->RandomModeOn();
159  ptMask->SetMaximumNumberOfPoints(static_cast<vtkIdType>(mesh->getNumberOfPoints()));
160  algo = ptMask;
161  }
162 
163  algo->SetInputData(m_polyData);
164 
165  vtkSmartPointer<vtkGlyphSource2D> arrow = vtkSmartPointer<vtkGlyphSource2D>::New();
166  arrow->SetGlyphTypeToArrow();
167  arrow->FilledOff();
168 
169  vtkSmartPointer<vtkGlyph3D> glyph = vtkSmartPointer<vtkGlyph3D>::New();
170  glyph->SetInputConnection(algo->GetOutputPort());
171  glyph->SetSourceConnection(arrow->GetOutputPort());
172  glyph->SetVectorModeToUseNormal();
173  glyph->SetScaleModeToScaleByVector();
174  glyph->SetScaleFactor(10.0);
175 
176  vtkSmartPointer<vtkPolyDataMapper> glyphMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
177  glyphMapper->SetInputConnection(glyph->GetOutputPort());
178 
179  m_actor->SetVisibility( true );
180  this->getActor()->SetMapper(glyphMapper);
181  this->getActor()->GetProperty()->SetColor(1., 1., 1.);
182  }
183  this->setVtkPipelineModified();
184  this->requestRender();
185 }
186 
187 //------------------------------------------------------------------------------
188 
190 {
192  this->requestRender();
193 }
194 
195 //------------------------------------------------------------------------------
196 
198 {
199  KeyConnectionsMap connections;
200  connections.push( s_MESH_INPUT, ::fwData::Mesh::s_MODIFIED_SIG, s_UPDATE_SLOT);
201  connections.push( s_MESH_INPUT, ::fwData::Mesh::s_VERTEX_MODIFIED_SIG, s_UPDATE_VERTEX_SLOT);
203  connections.push( s_MESH_INPUT, ::fwData::Mesh::s_CELL_NORMALS_MODIFIED_SIG, s_UPDATE_CELL_NORMALS_SLOT);
204 
205  return connections;
206 }
207 
208 //------------------------------------------------------------------------------
209 
211 {
212  ::fwData::Mesh::csptr mesh = this->getInput< ::fwData::Mesh >(s_MESH_INPUT);
213  SLM_ASSERT("Missing mesh", mesh);
217  this->setVtkPipelineModified();
218  this->requestRender();
219 }
220 
221 //------------------------------------------------------------------------------
222 
224 {
225  ::fwData::Mesh::csptr mesh = this->getInput< ::fwData::Mesh >(s_MESH_INPUT);
226  SLM_ASSERT("Missing mesh", mesh);
228  this->setVtkPipelineModified();
229  this->requestRender();
230 }
231 
232 //------------------------------------------------------------------------------
233 
235 {
236  ::fwData::Mesh::csptr mesh = this->getInput< ::fwData::Mesh >(s_MESH_INPUT);
237  SLM_ASSERT("Missing mesh", mesh);
239  this->setVtkPipelineModified();
240  this->requestRender();
241 }
242 
243 //------------------------------------------------------------------------------
244 
246 {
247  m_normalRepresentation = POINT_NORMAL;
248  this->updateMeshNormals();
249 }
250 
251 //------------------------------------------------------------------------------
252 
254 {
255  m_normalRepresentation = CELL_NORMAL;
256  this->updateMeshNormals();
257 }
258 
259 //------------------------------------------------------------------------------
260 
262 {
263  m_normalRepresentation = NONE;
264  this->updateMeshNormals();
265 }
266 
267 //------------------------------------------------------------------------------
268 
269 void SMeshNormals::updateNormalMode(std::uint8_t mode)
270 {
271  switch (mode)
272  {
273  case 0:
274  {
275  this->hideNormals();
276  break;
277  }
278  case 1:
279  {
280  this->showPointNormals();
281  break;
282  }
283  case 2:
284  {
285  this->showCellNormals();
286  break;
287  }
288  default:
289  {
290  OSLM_ERROR("mode " << mode << " is not allowed");
291  break;
292  }
293  }
294 }
295 
296 //------------------------------------------------------------------------------
297 
298 } //namespace visuVTKAdaptor
This class is a helper to define the connections of a service and its data.
Definition: IService.hpp:454
This adaptor displays mesh normals.
FWRENDERVTK_API void addToRenderer(vtkProp *prop)
Adds the vtkProp to the renderer.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_POINT_NORMALS_MODIFIED_SIG
Key in m_signals map of signal m_sigPointNormalsModified.
void updateNormalMode(std::uint8_t mode)
Slot: used to update normal mode (0: none, 1: point, 2: cell)
The namespace visuVTKAdaptor contains the list of adaptors available for the generic scene...
static FWVTKIO_API void toVTKMesh(const ::fwData::Mesh::csptr &_mesh, vtkSmartPointer< vtkPolyData > _polyData)
Convert a ::fwData::Mesh::csptr to a vtkPolyData.
void updatePointNormals()
Slot: used to update point normals.
FWRENDERVTK_API void configureParams()
Parse the xml configuration for renderer, picker and transform.
static FWVTKIO_API void updatePolyDataPoints(vtkSmartPointer< vtkPolyData > polyDataDst, const ::fwData::Mesh::csptr &meshSrc)
Update a vtkPolyData with ::fwData::Mesh::sptr points.
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.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_CELL_NORMALS_MODIFIED_SIG
Key in m_signals map of signal m_sigCellNormalsModified.
static VISUVTKADAPTOR_APIconst::fwCom::Slots::SlotKeyType s_UPDATE_CELL_NORMALS_SLOT
normal mode (0: none, 1: point, 2: cell)
void updateCellNormals()
Slot: used to update cell normals.
void showPointNormals()
Slot: used to show point normals.
#define OSLM_ERROR(message)
Definition: spyLog.hpp:274
static VISUVTKADAPTOR_APIconst::fwCom::Slots::SlotKeyType s_UPDATE_NORMAL_MODE_SLOT
normal mode (0: none, 1: point, 2: cell)
static VISUVTKADAPTOR_APIconst::fwCom::Slots::SlotKeyType s_HIDE_NORMALS_SLOT
normal mode (0: none, 1: point, 2: cell)
#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 starting() override
Initialize the service activity.
VISUVTKADAPTOR_API void configuring() override
Configure the service before starting. Apply the configuration to service.
static FWVTKIO_API void updatePolyDataCellNormals(vtkSmartPointer< vtkPolyData > polyDataDst, const ::fwData::Mesh::csptr &meshSrc)
Update a vtkPolyData with cell normals of fwData::Mesh.
static VISUVTKADAPTOR_APIconst::fwCom::Slots::SlotKeyType s_SHOW_CELL_NORMALS_SLOT
normal mode (0: none, 1: point, 2: cell)
static VISUVTKADAPTOR_APIconst::fwCom::Slots::SlotKeyType s_UPDATE_VERTEX_SLOT
normal mode (0: none, 1: point, 2: cell)
VISUVTKADAPTOR_API void updating() override
Perform some computations according to object (this service is attached to) attribute values and its ...
void showCellNormals()
Slot: used to show cell normals.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_VERTEX_MODIFIED_SIG
Key in m_signals map of signal m_sigVertexModified.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_MODIFIED_SIG
Key in m_signals map of signal m_sigModified.
void updateVertex()
Slot: used to update mesh vertex.
static FWVTKIO_API void updatePolyDataPointNormals(vtkSmartPointer< vtkPolyData > polyDataDst, const ::fwData::Mesh::csptr &meshSrc)
Update a vtkPolyData with point normals of fwData::Mesh.
FWRENDERVTK_API void initialize()
Initialize the adaptor with the associated render service. (must be call in starting).
virtual VISUVTKADAPTOR_API KeyConnectionsMap getAutoConnections() const override
Returns proposals to connect service slots to associated object signals, this method is used for obj/...
static VISUVTKADAPTOR_APIconst::fwCom::Slots::SlotKeyType s_UPDATE_POINT_NORMALS_SLOT
normal mode (0: none, 1: point, 2: cell)
VISUVTKADAPTOR_API void stopping() override
Uninitialize the service activity. The stop() method is always invoked before destroying a service...
void hideNormals()
Slot: used to hide normals.
static FWSERVICES_APIconst::fwCom::Slots::SlotKeyType s_UPDATE_SLOT
Slot to call start method.
Definition: IService.hpp:177
static VISUVTKADAPTOR_APIconst::fwCom::Slots::SlotKeyType s_SHOW_POINT_NORMALS_SLOT
normal mode (0: none, 1: point, 2: cell)
FWSERVICES_API ConfigType getConfigTree() const
Return the configuration, in an boost property tree.
Definition: IService.cpp:247