iMSTK
Interactive Medical Simulation Toolkit
imstkCleanMesh.cpp
1 /*
2 ** This file is part of the Interactive Medical Simulation Toolkit (iMSTK)
3 ** iMSTK is distributed under the Apache License, Version 2.0.
4 ** See accompanying NOTICE for details.
5 */
6 
7 #include "imstkCleanMesh.h"
8 #include "imstkLineMesh.h"
9 #include "imstkSurfaceMesh.h"
10 #include "imstkLogger.h"
11 #include "imstkGeometryUtilities.h"
12 
13 #include <vtkCleanPolyData.h>
14 
15 namespace imstk
16 {
17 CleanMesh::CleanMesh()
18 {
20  setRequiredInputType<SurfaceMesh>(0);
21 
23  setOutput(std::make_shared<SurfaceMesh>());
24 }
25 
26 std::shared_ptr<SurfaceMesh>
27 CleanMesh::getOutputMesh() const
28 {
29  return std::dynamic_pointer_cast<SurfaceMesh>(getOutput(0));
30 }
31 
32 void
33 CleanMesh::setInputMesh(std::shared_ptr<SurfaceMesh> inputMesh)
34 {
35  setInput(inputMesh, 0);
36 }
37 
38 void
40 {
41  std::shared_ptr<PointSet> inputMesh = std::dynamic_pointer_cast<PointSet>(getInput(0));
42  if (inputMesh == nullptr)
43  {
44  LOG(WARNING) << "No inputMesh to clean";
45  return;
46  }
47  vtkSmartPointer<vtkPolyData> inputMeshVtk = nullptr;
48  if (auto lineMesh = std::dynamic_pointer_cast<LineMesh>(inputMesh))
49  {
50  inputMeshVtk = GeometryUtils::copyToVtkPolyData(lineMesh);
51  }
52  else if (auto surfMesh = std::dynamic_pointer_cast<SurfaceMesh>(inputMesh))
53  {
54  inputMeshVtk = GeometryUtils::copyToVtkPolyData(surfMesh);
55  }
56  else
57  {
58  LOG(WARNING) << "Unsupported mesh type";
59  return;
60  }
61 
62  vtkNew<vtkCleanPolyData> filter;
63  filter->SetInputData(inputMeshVtk);
64  filter->SetTolerance(m_Tolerance);
65  filter->SetAbsoluteTolerance(m_AbsoluteTolerance);
66  filter->SetToleranceIsAbsolute(m_UseAbsolute);
67  filter->Update();
68 
69  if (std::dynamic_pointer_cast<LineMesh>(inputMesh) != nullptr)
70  {
71  setOutput(GeometryUtils::copyToLineMesh(filter->GetOutput()));
72  }
73  else if (std::dynamic_pointer_cast<SurfaceMesh>(inputMesh) != nullptr)
74  {
75  setOutput(GeometryUtils::copyToSurfaceMesh(filter->GetOutput()));
76  }
77 }
78 } // namespace imstk
Base class for all geometries represented by discrete points and elements The pointsets follow a pipe...
Definition: imstkPointSet.h:25
std::shared_ptr< LineMesh > copyToLineMesh(vtkSmartPointer< vtkPolyData > vtkMesh)
Converts vtk polydata into a imstk surface mesh.
vtkSmartPointer< vtkPolyData > copyToVtkPolyData(std::shared_ptr< LineMesh > imstkMesh)
Converts imstk line mesh into a vtk polydata.
std::shared_ptr< Geometry > getInput(size_t port=0) const
Returns input geometry given port, returns nullptr if doesn&#39;t exist.
Compound Geometry.
void setInputMesh(std::shared_ptr< SurfaceMesh > inputMesh)
Required input, port 0.
void setNumOutputPorts(const size_t numPorts)
Get/Set the amount of output ports.
void requestUpdate() override
Users can implement this for the logic to be run.
std::shared_ptr< Geometry > getOutput(size_t port=0) const
Returns output geometry given port, returns nullptr if doesn&#39;t exist.
void setInput(std::shared_ptr< Geometry > inputGeometry, size_t port=0)
Set the input at the port.
std::shared_ptr< SurfaceMesh > copyToSurfaceMesh(vtkSmartPointer< vtkPolyData > vtkMesh)
Converts vtk polydata into a imstk surface mesh.
void setOutput(std::shared_ptr< Geometry > inputGeometry, const size_t port=0)
Set the output at the port.
void setNumInputPorts(const size_t numPorts)
Get/Set the amount of input ports.