fw4spl
SMeshCreation.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2016.
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 "opVTKMesh/action/SMeshCreation.hpp"
8 
9 #include <fwCom/Signal.hpp>
10 #include <fwCom/Signal.hxx>
11 #include <fwCom/Signals.hpp>
12 
13 #include <fwData/Image.hpp>
14 #include <fwData/Mesh.hpp>
15 
16 #include <fwServices/macros.hpp>
17 
18 #include <fwTools/fwID.hpp>
19 
20 #include <fwVtkIO/helper/Mesh.hpp>
21 #include <fwVtkIO/vtk.hpp>
22 
23 #include <vtkDecimatePro.h>
24 #include <vtkDiscreteMarchingCubes.h>
25 #include <vtkGeometryFilter.h>
26 #include <vtkImageData.h>
27 #include <vtkPolyDataMapper.h>
28 #include <vtkSmartPointer.h>
29 #include <vtkThreshold.h>
30 #include <vtkWindowedSincPolyDataFilter.h>
31 
32 
33 namespace opVTKMesh
34 {
35 
36 namespace action
37 {
38 
39 //-----------------------------------------------------------------------------
40 
41 fwServicesRegisterMacro( ::fwGui::IActionSrv, ::opVTKMesh::action::SMeshCreation );
42 
43 //-----------------------------------------------------------------------------
44 
45 SMeshCreation::SMeshCreation() noexcept :
46  m_reduction(0)
47 {
48 }
49 
50 //-----------------------------------------------------------------------------
51 
52 SMeshCreation::~SMeshCreation() noexcept
53 {
54 }
55 
56 //-----------------------------------------------------------------------------
57 
59 {
61  this->actionServiceStarting();
62 }
63 
64 //-----------------------------------------------------------------------------
65 
67 {
69  this->actionServiceStopping();
70 }
71 
72 //-----------------------------------------------------------------------------
73 
75 {
77  this->initialize();
78 
79  if (m_configuration->findConfigurationElement("percentReduction") &&
80  m_configuration->findConfigurationElement("percentReduction")->hasAttribute("value"))
81  {
82  std::string reduce = m_configuration->findConfigurationElement("percentReduction")->getExistingAttributeValue(
83  "value");
84  m_reduction = boost::lexical_cast<unsigned int>(reduce);
85  }
86 
87  OSLM_INFO( "Reduction value = " << m_reduction);
88 }
89 
90 //-----------------------------------------------------------------------------
91 
93 {
95 
97  auto pImage = this->getInput< ::fwData::Image >("image");
98  SLM_ASSERT("'image' key not found", pImage);
99  auto pMesh = this->getInOut< ::fwData::Mesh >("mesh");
100  SLM_ASSERT("'mesh' key not found", pMesh);
101 
103 
104  // vtk img
105  vtkSmartPointer< vtkImageData > vtkImage = vtkSmartPointer< vtkImageData >::New();
106  ::fwVtkIO::toVTKImage( pImage, vtkImage );
107 
108  // contour filter
109  vtkSmartPointer< vtkDiscreteMarchingCubes > contourFilter = vtkSmartPointer< vtkDiscreteMarchingCubes >::New();
110  contourFilter->SetInputData(vtkImage);
111  contourFilter->SetValue(0, 255);
112  contourFilter->ComputeScalarsOn();
113  contourFilter->ComputeNormalsOn();
114  contourFilter->Update();
115 
116  // smooth filter
117  vtkSmartPointer< vtkWindowedSincPolyDataFilter > smoothFilter =
118  vtkSmartPointer< vtkWindowedSincPolyDataFilter >::New();
119  smoothFilter->SetInputConnection(contourFilter->GetOutputPort());
120  smoothFilter->SetNumberOfIterations( 50 );
121  smoothFilter->BoundarySmoothingOn();
122  smoothFilter->SetPassBand ( 0.1 );
123  smoothFilter->SetFeatureAngle(120.0);
124  smoothFilter->SetEdgeAngle(90);
125  smoothFilter->FeatureEdgeSmoothingOn();
126  smoothFilter->Update();
127 
128 
129  // Get polyData
130  vtkSmartPointer< vtkPolyData > polyData;
131 
132  // decimate filter
133  unsigned int reduction = m_reduction;
134  if( reduction > 0 )
135  {
136  vtkSmartPointer< vtkDecimatePro > decimate = vtkSmartPointer< vtkDecimatePro >::New();
137  decimate->SetInputConnection( smoothFilter->GetOutputPort() );
138  decimate->SetTargetReduction( reduction/100.0 );
139  decimate->PreserveTopologyOff();
140  decimate->SplittingOn();
141  decimate->BoundaryVertexDeletionOn();
142  decimate->SetSplitAngle( 120 );
143  decimate->Update();
144  polyData = decimate->GetOutput();
145  OSLM_TRACE("final GetNumberOfCells = " << polyData->GetNumberOfCells());
146  ::fwVtkIO::helper::Mesh::fromVTKMesh( polyData, pMesh);
147  }
148  else
149  {
150  polyData = smoothFilter->GetOutput();
151  OSLM_TRACE("final GetNumberOfCells = " << polyData->GetNumberOfCells());
152  ::fwVtkIO::helper::Mesh::fromVTKMesh( polyData, pMesh);
153  }
154 
156  ::fwData::Object::ModifiedSignalType::sptr sig;
158  {
159  ::fwCom::Connection::Blocker block(sig->getConnection(m_slotUpdate));
160  sig->asyncEmit();
161  }
162 }
163 
164 //-----------------------------------------------------------------------------
165 
166 } // namespace action
167 } // namespace opVTKMesh
168 
FWGUI_API void actionServiceStarting()
Method called when the action service is starting.
Definition: IActionSrv.cpp:160
Class allowing to block a Connection.
Definition: Connection.hpp:20
#define SLM_TRACE_FUNC()
Trace contextual function signature.
Definition: spyLog.hpp:329
FWGUI_API void actionServiceStopping()
Method called when the action service is stopping.
Definition: IActionSrv.cpp:153
OPVTKMESH_API void stopping() override
Uninitialize the service activity. The stop() method is always invoked before destroying a service...
The namespace opVTKMesh contains an implementation of a VTK mesher.
#define OSLM_INFO(message)
Definition: spyLog.hpp:252
#define OSLM_TRACE(message)
Definition: spyLog.hpp:230
Defines the service interface managing the menu items.
Definition: IActionSrv.hpp:24
UpdateSlotType::sptr m_slotUpdate
Slot to call update method.
Definition: IService.hpp:690
OPVTKMESH_API void configuring() override
Configure the service.
#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
FWGUI_API void initialize()
Initialize the action.
Definition: IActionSrv.cpp:69
OPVTKMESH_API void updating() override
Process the mesh creation from the image.
::fwRuntime::ConfigurationElement::sptr m_configuration
Configuration element used to configure service internal state using a generic XML like structure TOD...
Definition: IService.hpp:670
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_MODIFIED_SIG
Key in m_signals map of signal m_sigModified.
Action to create a mesh from an image using VTK library.
static FWVTKIO_API void fromVTKMesh(vtkSmartPointer< vtkPolyData > _polyData,::fwData::Mesh::sptr _mesh)
Convert a vtkPolyData to a ::fwData::Mesh::sptr.
OPVTKMESH_API void starting() override
Initialize the service activity.