iMSTK
Interactive Medical Simulation Toolkit
imstkMeshCut.h
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 #pragma once
8 
9 #include "imstkGeometryAlgorithm.h"
10 #include "imstkMath.h"
11 
12 #include <map>
13 #include <unordered_set>
14 #include <set>
15 
16 namespace imstk
17 {
18 class AbstractCellMesh;
19 class AnalyticalGeometry;
20 class Geometry;
21 class SurfaceMesh;
22 template<typename T, int N> class VecDataArray;
23 
24 struct CutData
25 {
26  public:
27  Vec3d cutCoords[2];
28  Vec3d initCoords[2];
29  int cellId = -1; // Id of the cell (line, triangle, ...)
30  int ptIds[2] = { -1, -1 };
31  int cutType = 0;
32 };
33 
41 class MeshCut : public GeometryAlgorithm
42 {
43 protected:
44  MeshCut();
45 
46 public:
47  ~MeshCut() override = default;
48 
49  void requestUpdate() override;
50 
51  std::shared_ptr<std::map<int, int>> getCutVertMap() { return m_CutVertMap; }
52 
53  imstkGetMacro(CutData, std::shared_ptr<std::vector<CutData>>);
54  imstkSetMacro(CutData, std::shared_ptr<std::vector<CutData>>);
55 
56  imstkGetMacro(CutGeometry, std::shared_ptr<Geometry>);
57  imstkSetMacro(CutGeometry, std::shared_ptr<Geometry>);
58 
59  imstkGetMacro(Epsilon, double);
60  imstkSetMacro(Epsilon, double);
61 
62  imstkGetMacro(RemoveConstraintVertices, std::shared_ptr<std::unordered_set<size_t>>);
63  imstkGetMacro(AddConstraintVertices, std::shared_ptr<std::unordered_set<size_t>>);
64 
65 protected:
69  virtual std::shared_ptr<std::vector<CutData>> generateCutData(
70  std::shared_ptr<Geometry> cuttingGeom,
71  std::shared_ptr<AbstractCellMesh> geomToCut) = 0;
72 
79  virtual void refinement(
80  std::shared_ptr<AbstractCellMesh> outputGeom,
81  std::map<int, bool>& cutVerts) = 0;
82 
89  virtual void splitVerts(
90  std::shared_ptr<AbstractCellMesh> outputGeom,
91  std::map<int, bool>& cutVerts,
92  std::shared_ptr<Geometry> cuttingGeom) = 0;
93 
98  int ptBoundarySign(const Vec3d& pt, std::shared_ptr<Geometry> geometry);
99 
100  template<int N>
101  bool vertexOnBoundary(std::shared_ptr<VecDataArray<int, N>> cells,
102  std::set<int>& cellSet)
103  {
104  std::set<int> nonRepeatNeighborVerts;
105  for (const auto& cellId : cellSet)
106  {
107  for (int i = 0; i < N; i++)
108  {
109  const int ptId = (*cells)[cellId][i];
110  if (nonRepeatNeighborVerts.find(ptId) != nonRepeatNeighborVerts.end())
111  {
112  nonRepeatNeighborVerts.erase(ptId);
113  }
114  else
115  {
116  nonRepeatNeighborVerts.insert(ptId);
117  }
118  }
119  }
120  return (nonRepeatNeighborVerts.size() >= 2);
121  }
122 
123  bool pointProjectionInSurface(const Vec3d& pt, std::shared_ptr<SurfaceMesh> surface);
124 
125  std::shared_ptr<std::vector<CutData>> m_CutData = nullptr;
126 
127  std::shared_ptr<std::map<int, int>> m_CutVertMap =
128  std::make_shared<std::map<int, int>>();
129 
130  std::shared_ptr<Geometry> m_CutGeometry = nullptr;
131 
132  std::shared_ptr<std::unordered_set<size_t>> m_RemoveConstraintVertices = nullptr;
133  std::shared_ptr<std::unordered_set<size_t>> m_AddConstraintVertices = nullptr;
134 
135  double m_Epsilon = 0.001;
136 };
137 } // namespace imstk
Compound Geometry.
Simple dynamic array implementation that also supports event posting and viewing/facade.
Abstract base class for geometry algorithms. GeometryAlgorithms take N input geometries and produce N...
Base abstract class for discrete cut algorithms.
Definition: imstkMeshCut.h:41