iMSTK
Interactive Medical Simulation Toolkit
imstkSurfaceMesh.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 "imstkCellMesh.h"
10 #include "imstkMacros.h"
11 
12 #include <array>
13 #include <unordered_set>
14 
15 namespace imstk
16 {
23 {
24  Vec3d position;
25  Vec3d normal;
26 };
27 } // namespace imstk
28 
29 namespace imstk
30 {
34 struct TriCell
35 {
36  std::array<std::uint32_t, 3> vertexIds;
37 
38  TriCell(std::uint32_t id0, std::uint32_t id1, std::uint32_t id2)
39  {
40  vertexIds[0] = id0;
41  vertexIds[1] = id1;
42  vertexIds[2] = id2;
43  if (vertexIds[0] > vertexIds[1])
44  {
45  std::swap(vertexIds[0], vertexIds[1]);
46  }
47  if (vertexIds[1] > vertexIds[2])
48  {
49  std::swap(vertexIds[1], vertexIds[2]);
50  }
51  if (vertexIds[0] > vertexIds[1])
52  {
53  std::swap(vertexIds[0], vertexIds[1]);
54  }
55  }
56 
57  // Test true equivalence
58  bool operator==(const TriCell& other) const
59  {
60  // Only works if sorted
61  return (vertexIds[0] == other.vertexIds[0] && vertexIds[1] == other.vertexIds[1]
62  && vertexIds[2] == other.vertexIds[2]);
63  }
64 };
65 } // namespace imstk
66 
67 namespace std
68 {
76 template<>
77 struct hash<imstk::TriCell>
78 {
79  // A 128 int could garuntee no collisions but its harder to find support for
80  std::size_t operator()(const imstk::TriCell& k) const
81  {
82  using std::size_t;
83  using std::hash;
84 
85  // Assuming sorted
86  const std::size_t r =
87  imstk::symCantor(static_cast<size_t>(k.vertexIds[0]), static_cast<size_t>(k.vertexIds[1]));
88  return imstk::symCantor(r, static_cast<size_t>(k.vertexIds[2]));
89  }
90 };
96 template<> struct less<imstk::NormalGroup>
97 {
98  bool operator()(const imstk::NormalGroup& group1,
99  const imstk::NormalGroup& group2) const
100  {
101  if (group1.position != group2.position)
102  {
103  return (group1.position.x() < group2.position.x());
104  }
105 
106  if (group1.normal != group2.normal)
107  {
108  return (group1.normal.x() < group2.normal.x());
109  }
110 
111  return false;
112  }
113 };
114 } // namespace std
115 
116 namespace imstk
117 {
124 class SurfaceMesh : public CellMesh<3>
125 {
126 public:
127  SurfaceMesh() = default;
128  ~SurfaceMesh() override = default;
129 
130  IMSTK_TYPE_NAME(SurfaceMesh)
131 
132 
133  void initialize(std::shared_ptr<VecDataArray<double, 3>> vertices,
137  std::shared_ptr<VecDataArray<int, 3>> triangleIndices,
138  const bool computeDerivedData = false);
139 
144  void initialize(std::shared_ptr<VecDataArray<double, 3>> vertices,
145  std::shared_ptr<VecDataArray<int, 3>> triangleIndices,
146  std::shared_ptr<VecDataArray<double, 3>> normals,
147  const bool computeDerivedData = false);
148 
152  void computeTrianglesNormals();
153 
158  void computeTriangleTangents();
159 
163  void computeVertexNormals();
164 
168  void computeVertexTangents();
169 
173  Vec3d computeBarycentricWeights(const int tetId, const Vec3d& pos) const override;
174 
180  void optimizeForDataLocality();
181 
185  void flipNormals();
186 
190  void correctWindingOrder();
191 
195  void computeUVSeamVertexGroups();
196 
200  double getVolume() override;
201 
202  int getNumTriangles() const { return getNumCells(); }
203  void setTriangleIndices(std::shared_ptr<VecDataArray<int, 3>> indices) { setCells(indices); }
204  std::shared_ptr<VecDataArray<int, 3>> getTriangleIndices() const { return getCells(); }
205 
210  std::unique_ptr<SurfaceMesh> clone()
211  {
212  return std::unique_ptr<SurfaceMesh>(cloneImplementation());
213  }
214 
215 protected:
216  std::map<NormalGroup, std::shared_ptr<std::vector<size_t>>> m_UVSeamVertexGroups;
217 
218 private:
219  SurfaceMesh* cloneImplementation() const;
220 };
221 } // namespace imstk
Utility for triangle comparison.
Compound Geometry.
std::unique_ptr< SurfaceMesh > clone()
Polymorphic clone, hides the declaration in superclass return own type.
Returns a hash value for a PointEntry.
Represents a set of triangles & vertices via an array of Vec3d double vertices & Vec3i integer indice...
bool operator==(const Color &color_lhs, const Color &color_rhs)
Comparison operator.
Definition: imstkColor.cpp:310
Abstract template base class for all meshes that have homogenous cell types. This class allows templa...
Definition: imstkCellMesh.h:23
Helper class for indentifying duplicate points.
This method is defined to allow for the map to be properly indexed by Texture objects.