iMSTK
Interactive Medical Simulation Toolkit
imstkSelectEnclosedPoints.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 "imstkSelectEnclosedPoints.h"
8 #include "imstkGeometryUtilities.h"
9 #include "imstkLogger.h"
10 #include "imstkSurfaceMesh.h"
11 #include "imstkVecDataArray.h"
12 
13 #include <vtkPolyData.h>
14 #include <vtkSelectEnclosedPoints.h>
15 
16 namespace imstk
17 {
18 SelectEnclosedPoints::SelectEnclosedPoints()
19 {
21  setRequiredInputType<SurfaceMesh>(0);
22  setRequiredInputType<PointSet>(1);
23 
25  setOutput(std::make_shared<PointSet>());
26 }
27 
28 void
29 SelectEnclosedPoints::setInputMesh(std::shared_ptr<SurfaceMesh> inputMesh)
30 {
31  setInput(inputMesh, 0);
32 }
33 
34 void
35 SelectEnclosedPoints::setInputPoints(std::shared_ptr<PointSet> inputPts)
36 {
37  setInput(inputPts, 1);
38 }
39 
40 std::shared_ptr<PointSet>
41 SelectEnclosedPoints::getOutputPoints() const
42 {
43  return std::static_pointer_cast<PointSet>(getOutput(0));
44 }
45 
46 void
48 {
49  std::shared_ptr<SurfaceMesh> inputSurfaceMesh = std::dynamic_pointer_cast<SurfaceMesh>(getInput(0));
50  std::shared_ptr<PointSet> inputPointSet = std::dynamic_pointer_cast<PointSet>(getInput(1));
51  m_IsInsideMask = nullptr;
52 
53  if (inputSurfaceMesh == nullptr || inputPointSet == nullptr)
54  {
55  LOG(WARNING) << "Missing inputs";
56  return;
57  }
58 
59  vtkNew<vtkSelectEnclosedPoints> filter;
60  filter->SetInputData(GeometryUtils::copyToVtkPointSet(inputPointSet));
61  filter->SetSurfaceData(GeometryUtils::copyToVtkPolyData(inputSurfaceMesh));
62  filter->SetTolerance(m_Tolerance);
63  filter->SetInsideOut(m_InsideOut);
64  filter->Update();
65 
66  vtkSmartPointer<vtkPointSet> vtkResults = vtkPointSet::SafeDownCast(filter->GetOutput());
67 
68  if (m_UsePruning)
69  {
70  std::shared_ptr<VecDataArray<double, 3>> points = std::make_shared<VecDataArray<double, 3>>();
71  points->reserve(vtkResults->GetNumberOfPoints());
72  for (vtkIdType i = 0; i < vtkResults->GetNumberOfPoints(); i++)
73  {
74  if (filter->IsInside(i))
75  {
76  double pt[3];
77  vtkResults->GetPoint(i, pt);
78  points->push_back(Vec3d(pt[0], pt[1], pt[2]));
79  }
80  }
81  points->squeeze();
82 
83  std::shared_ptr<PointSet> results = std::make_shared<PointSet>();
84  results->initialize(points);
85  setOutput(results, 0);
86  }
87  else
88  {
89  m_IsInsideMask = std::make_shared<DataArray<unsigned char>>(vtkResults->GetNumberOfPoints());
90  DataArray<unsigned char>& mask = *m_IsInsideMask;
91  for (vtkIdType i = 0; i < vtkResults->GetNumberOfPoints(); i++)
92  {
93  mask[i] = filter->IsInside(i);
94  }
95  }
96 }
97 } // namespace imstk
Base class for all geometries represented by discrete points and elements The pointsets follow a pipe...
Definition: imstkPointSet.h:25
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 setNumOutputPorts(const size_t numPorts)
Get/Set the amount of output ports.
std::shared_ptr< Geometry > getOutput(size_t port=0) const
Returns output geometry given port, returns nullptr if doesn&#39;t exist.
void requestUpdate() override
Users can implement this for the logic to be run.
void setInput(std::shared_ptr< Geometry > inputGeometry, size_t port=0)
Set the input at the port.
vtkSmartPointer< vtkPointSet > copyToVtkPointSet(std::shared_ptr< PointSet > imstkMesh)
Converts imstk point set into a vtk polydata.
Represents a set of triangles & vertices via an array of Vec3d double vertices & Vec3i integer indice...
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.
Simple dynamic array implementation that also supports event posting and viewing/facade.