opensurgsim
TriangleMesh.h
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2013, SimQuest Solutions Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef SURGSIM_DATASTRUCTURES_TRIANGLEMESH_H
17 #define SURGSIM_DATASTRUCTURES_TRIANGLEMESH_H
18 
19 #include <array>
20 #include <memory>
21 
22 #include "SurgSim/DataStructures/EmptyData.h"
23 #include "SurgSim/DataStructures/MeshElement.h"
24 #include "SurgSim/DataStructures/NormalData.h"
25 #include "SurgSim/DataStructures/TriangleMeshPlyReaderDelegate.h"
26 #include "SurgSim/DataStructures/Vertices.h"
27 #include "SurgSim/Framework/Asset.h"
28 
29 namespace SurgSim
30 {
31 namespace DataStructures
32 {
33 
61 template <class VertexData, class EdgeData, class TriangleData>
62 class TriangleMesh : public Vertices<VertexData>, public SurgSim::Framework::Asset,
63  public std::enable_shared_from_this<TriangleMesh<VertexData, EdgeData, TriangleData>>
64 {
65 public:
70 
72  TriangleMesh();
73 
77 
84  template <class V, class E, class T>
85  explicit TriangleMesh(const TriangleMesh<V, E, T>& other);
86 
88  virtual ~TriangleMesh();
89 
92  TriangleMesh(TriangleMesh&& other);
93 
98 
103 
104 
105  std::string getClassName() const override;
106 
114  size_t addEdge(const EdgeType& edge);
115 
123  size_t addTriangle(const TriangleType& triangle);
124 
127  size_t getNumEdges() const;
128 
133  size_t getNumTriangles() const;
134 
137  const std::vector<EdgeType>& getEdges() const;
138 
141  std::vector<EdgeType>& getEdges();
142 
149  const std::vector<TriangleType>& getTriangles() const;
150 
157  std::vector<TriangleType>& getTriangles();
158 
162  const EdgeType& getEdge(size_t id) const;
163 
167  EdgeType& getEdge(size_t id);
168 
172  std::array<SurgSim::Math::Vector3d, 2> getEdgePositions(size_t id) const;
173 
178  const TriangleType& getTriangle(size_t id) const;
179 
184  TriangleType& getTriangle(size_t id);
185 
191  void removeTriangle(size_t id);
192 
196  std::array<SurgSim::Math::Vector3d, 3> getTrianglePositions(size_t id) const;
197 
200  bool isValid() const;
201 
204  void save(const std::string& fileName);
205 
206 protected:
208  virtual void doClearEdges();
209 
211  virtual void doClearTriangles();
212 
218  virtual bool isEqual(const Vertices<VertexData>& mesh) const;
219 
220  bool doLoad(const std::string& fileName) override;
221 
223 
224  static std::string m_className;
225 
226 private:
227 
229  virtual void doClear();
230 
232  std::vector<EdgeType> m_edges;
233 
235  std::vector<TriangleType> m_triangles;
236 
238  std::vector<size_t> m_freeTriangles;
239 
240 public:
241  // Dependent name resolution for inherited functions and typenames from templates
242  using typename Vertices<VertexData>::VertexType;
246 
247 };
248 
250 
251 }; // namespace DataStructures
252 }; // namespace SurgSim
253 
254 #include "SurgSim/DataStructures/TriangleMesh-inl.h"
255 
256 #endif // SURGSIM_DATASTRUCTURES_TRIANGLEMESH_H
Wraps glewInit() to separate the glew opengl definitions from the osg opengl definitions only imgui n...
Definition: AddRandomSphereBehavior.cpp:36
This class is used to facilitate file loading.
Definition: Asset.h:39
const TriangleType & getTriangle(size_t id) const
Retrieve a specific triangle.
Definition: TriangleMesh-inl.h:179
size_t addTriangle(const TriangleType &triangle)
Adds a triangle to the mesh.
Definition: TriangleMesh-inl.h:88
std::array< SurgSim::Math::Vector3d, 2 > getEdgePositions(size_t id) const
Returns an array of the edge&#39;s vertices&#39; positions.
Definition: TriangleMesh-inl.h:165
bool isValid() const
Test if the TriangleMesh is valid (valid vertex Ids used in all MeshElements)
Definition: TriangleMesh-inl.h:223
virtual void doClearTriangles()
Remove all triangles from the mesh.
Definition: TriangleMesh-inl.h:265
Vertex structure for meshes.
Definition: Vertex.h:44
void removeTriangle(size_t id)
Marks a triangle as invalid, the triangle cannot be accessed via getTriangle anymore.
Definition: TriangleMesh-inl.h:197
const std::vector< TriangleType > & getTriangles() const
Retrieve all triangles.
Definition: TriangleMesh-inl.h:137
virtual bool isEqual(const Vertices< VertexData > &mesh) const
Internal comparison of meshes of the same type: returns true if equal, false if not equal...
Definition: TriangleMesh-inl.h:272
virtual void doClearEdges()
Remove all edges from the mesh.
Definition: TriangleMesh-inl.h:259
std::array< SurgSim::Math::Vector3d, 3 > getTrianglePositions(size_t id) const
Returns an array of the triangle&#39;s vertices&#39; positions.
Definition: TriangleMesh-inl.h:209
void save(const std::string &fileName)
Save the triangle mesh in the ply format.
Definition: TriangleMesh-inl.h:346
TriangleMesh()
Constructor. The mesh is initially empty (no vertices, no edges, no triangles).
Definition: TriangleMesh-inl.h:28
MeshElement< 3, TriangleData > TriangleType
Triangle type for convenience (Ids of the 3 vertices)
Definition: TriangleMesh.h:69
virtual ~TriangleMesh()
Destructor.
Definition: TriangleMesh-inl.h:69
const EdgeType & getEdge(size_t id) const
Retrieve a specific edge.
Definition: TriangleMesh-inl.h:151
TriangleMesh< VertexData, EdgeData, TriangleData > & operator=(const TriangleMesh< VertexData, EdgeData, TriangleData > &other)
Copy Assignment.
Definition: TriangleMesh-inl.h:322
MeshElement< 2, EdgeData > EdgeType
Edge type for convenience (Ids of the 2 vertices)
Definition: TriangleMesh.h:67
size_t addEdge(const EdgeType &edge)
Adds an edge to the mesh.
Definition: TriangleMesh-inl.h:81
Basic class for storing Triangle Meshes, handling basic vertex, edge, and triangle functionality...
Definition: TriangleMesh.h:62
Element structure for meshes.
Definition: MeshElement.h:44
const std::vector< EdgeType > & getEdges() const
Retrieve all edges.
Definition: TriangleMesh-inl.h:123
size_t getNumTriangles() const
Get the number of triangles.
Definition: TriangleMesh-inl.h:116
Base class for mesh structures, handling basic vertex functionality.
Definition: Vertices.h:51
bool doLoad(const std::string &fileName) override
Derived classes will overwrite this method to do actual loading.
Definition: TriangleMesh-inl.h:280
std::string getClassName() const override
Support serialization with a classname.
Definition: TriangleMesh-inl.h:74
size_t getNumEdges() const
Get the number of edges.
Definition: TriangleMesh-inl.h:110