All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TriangleMeshBase-inl.h
Go to the documentation of this file.
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_TRIANGLEMESHBASE_INL_H
17 #define SURGSIM_DATASTRUCTURES_TRIANGLEMESHBASE_INL_H
18 
20 
21 namespace SurgSim
22 {
23 namespace DataStructures
24 {
25 
26 template <class VertexData, class EdgeData, class TriangleData>
28 {
29 }
30 
31 template <class VertexData, class EdgeData, class TriangleData>
32 template <class VertexDataSource, class EdgeDataSource, class TriangleDataSource>
35 {
36  for (size_t iVertex = 0; iVertex < mesh.getNumVertices(); ++iVertex)
37  {
39  addVertex(vertexData);
40  }
41  for (size_t iEdge = 0; iEdge < mesh.getNumEdges(); ++iEdge)
42  {
43  EdgeType edgeData((mesh.getEdge(iEdge)).verticesId, EdgeData());
44  addEdge(edgeData);
45  }
46 
47  auto& sourceTriangles = mesh.getTriangles();
48  size_t index = 0;
49  m_triangles.reserve(sourceTriangles.size());
50  for (auto sourceTriangle : sourceTriangles)
51  {
52  TriangleType triangleData(sourceTriangle.verticesId, TriangleData());
53  addTriangle(triangleData);
54  if (!sourceTriangle.isValid)
55  {
56  m_freeTriangles.push_back(index);
57  }
58  ++index;
59  }
60 }
61 
62 template <class VertexData, class EdgeData, class TriangleData>
64 {
65 }
66 
67 template <class VertexData, class EdgeData, class TriangleData>
69 {
70  m_edges.push_back(edge);
71  return m_edges.size() - 1;
72 }
73 
74 template <class VertexData, class EdgeData, class TriangleData>
76 {
77  size_t result;
78 
79  SURGSIM_ASSERT(triangle.isValid) << "Cannot insert invalid triangle into mesh.";
80 
81  if (m_freeTriangles.empty())
82  {
83  m_triangles.push_back(triangle);
84  result = m_triangles.size() - 1;
85  }
86  else
87  {
88  result = m_freeTriangles.back();
89  m_freeTriangles.pop_back();
90  m_triangles[result] = triangle;
91  }
92 
93  return result;
94 }
95 
96 template <class VertexData, class EdgeData, class TriangleData>
98 {
99  return m_edges.size();
100 }
101 
102 template <class VertexData, class EdgeData, class TriangleData>
104 {
105  return m_triangles.size() - m_freeTriangles.size();
106 }
107 
108 template <class VertexData, class EdgeData, class TriangleData>
109 const std::vector<typename TriangleMeshBase<VertexData, EdgeData, TriangleData>::EdgeType>&
111 {
112  return m_edges;
113 }
114 
115 template <class VertexData, class EdgeData, class TriangleData>
116 std::vector<typename TriangleMeshBase<VertexData, EdgeData, TriangleData>::EdgeType>&
118 {
119  return m_edges;
120 }
121 
122 template <class VertexData, class EdgeData, class TriangleData>
123 const std::vector<typename TriangleMeshBase<VertexData, EdgeData, TriangleData>::TriangleType>&
125 {
126  return m_triangles;
127 }
128 
129 template <class VertexData, class EdgeData, class TriangleData>
130 std::vector<typename TriangleMeshBase<VertexData, EdgeData, TriangleData>::TriangleType>&
132 {
133  return m_triangles;
134 }
135 
136 template <class VertexData, class EdgeData, class TriangleData>
139 {
140  return m_edges[id];
141 }
142 
143 template <class VertexData, class EdgeData, class TriangleData>
146 {
147  return m_edges[id];
148 }
149 
150 template <class VertexData, class EdgeData, class TriangleData>
153 {
154  auto const& triangle = m_triangles[id];
155  SURGSIM_ASSERT(triangle.isValid == true) << "Attempted to access invalid or deleted triangle.";
156  return triangle;
157 }
158 
159 template <class VertexData, class EdgeData, class TriangleData>
162 {
163  auto& triangle = m_triangles[id];
164  SURGSIM_ASSERT(triangle.isValid == true) << "Attempted to access invalid or deleted triangle.";
165  return triangle;
166 }
167 
168 template <class VertexData, class EdgeData, class TriangleData>
170 {
171  auto& triangle = m_triangles[id];
172  SURGSIM_ASSERT(triangle.isValid) << "This triangle has already been removed.";
173  triangle.isValid = false;
174  m_freeTriangles.push_back(id);
175 }
176 
177 template <class VertexData, class EdgeData, class TriangleData>
178 std::array<SurgSim::Math::Vector3d, 3>
180 {
181  auto& ids = getTriangle(id).verticesId;
182  std::array<SurgSim::Math::Vector3d, 3> result
183  = {{
187  }
188  };
189 
190  return result;
191 }
192 
193 template <class VertexData, class EdgeData, class TriangleData>
195 {
198 
200 
201  // Test edges validity
202  for (typename std::vector<EdgeType>::const_iterator it = m_edges.begin(); it != m_edges.end(); ++it)
203  {
204  for (int vertexId = 0; vertexId < 2; vertexId++)
205  {
206  if (it->verticesId[vertexId] >= numVertices)
207  {
208  return false;
209  }
210  }
211  }
212 
213  // Test triangles validity
214  for (typename std::vector<TriangleType>::const_iterator it = m_triangles.begin(); it != m_triangles.end(); ++it)
215  {
216  for (int vertexId = 0; vertexId < 3; vertexId++)
217  {
218  if (it->verticesId[vertexId] >= numVertices)
219  {
220  return false;
221  }
222  }
223  }
224 
225  return true;
226 }
227 
228 
229 template <class VertexData, class EdgeData, class TriangleData>
231 {
232  m_edges.clear();
233 }
234 
235 template <class VertexData, class EdgeData, class TriangleData>
237 {
238  m_triangles.clear();
239  m_freeTriangles.clear();
240 }
241 
242 template <class VertexData, class EdgeData, class TriangleData>
244 {
245  const TriangleMeshBase& triangleMesh = static_cast<const TriangleMeshBase&>(mesh);
246  return Vertices<VertexData>::isEqual(triangleMesh) && m_edges == triangleMesh.getEdges() &&
247  m_triangles == triangleMesh.getTriangles();
248 }
249 
250 template <class VertexData, class EdgeData, class TriangleData>
252 {
253  doClearTriangles();
254  doClearEdges();
255  this->doClearVertices();
256 }
257 
258 
259 
260 }; // namespace DataStructures
261 }; // namespace SurgSim
262 
263 #endif // SURGSIM_DATASTRUCTURES_TRIANGLEMESHBASE_INL_H
SurgSim::Math::Vector3d position
Position of the vertex.
Definition: Vertex.h:56
bool isValid() const
Test if the TriangleMeshBase is valid (valid vertex Ids used in all MeshElements) ...
Definition: TriangleMeshBase-inl.h:194
size_t addEdge(const EdgeType &edge)
Adds an edge to the mesh.
Definition: TriangleMeshBase-inl.h:68
void removeTriangle(size_t id)
Marks a triangle as invalid, the triangle cannot be accessed via getTriangle anymore.
Definition: TriangleMeshBase-inl.h:169
size_t getNumVertices() const
Returns the number of vertices in this mesh.
Definition: Vertices.h:91
double vertexData[]
Definition: TestCube.cpp:27
virtual void doClearEdges()
Remove all edges from the mesh.
Definition: TriangleMeshBase-inl.h:230
unsigned int triangleData[]
Definition: TestCube.cpp:118
const VertexType & getVertex(size_t id) const
Returns the specified vertex.
Definition: Vertices.h:97
#define SURGSIM_ASSERT(condition)
Assert that condition is true.
Definition: Assert.h:77
virtual bool isEqual(const Vertices &mesh) const
Internal comparison of meshes of the same type: returns true if equal, false if not equal...
Definition: Vertices.h:178
const std::vector< TriangleType > & getTriangles() const
Retrieve all triangles.
Definition: TriangleMeshBase-inl.h:124
Vertex structure for meshes.
Definition: Vertex.h:44
virtual ~TriangleMeshBase()
Destructor.
Definition: TriangleMeshBase-inl.h:63
int numVertices
Definition: TestCube.cpp:25
size_t getNumEdges() const
Get the number of edges.
Definition: TriangleMeshBase-inl.h:97
TriangleMeshBase()
Constructor. The mesh is initially empty (no vertices, no edges, no triangles).
Definition: TriangleMeshBase-inl.h:27
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: TriangleMeshBase-inl.h:243
std::array< SurgSim::Math::Vector3d, 3 > getTrianglePositions(size_t id) const
Returns an array of the triangle's vertices' positions.
Definition: TriangleMeshBase-inl.h:179
const EdgeType & getEdge(size_t id) const
Retrieve a specific edge.
Definition: TriangleMeshBase-inl.h:138
Basic class for storing Triangle Meshes, handling basic vertex, edge, and triangle functionality...
Definition: TriangleMeshBase.h:58
size_t getNumTriangles() const
Get the number of triangles.
Definition: TriangleMeshBase-inl.h:103
virtual void doClearTriangles()
Remove all triangles from the mesh.
Definition: TriangleMeshBase-inl.h:236
virtual void doClear()
Clear mesh to return to an empty state (no vertices, no edges, no triangles).
Definition: TriangleMeshBase-inl.h:251
size_t addTriangle(const TriangleType &triangle)
Adds a triangle to the mesh.
Definition: TriangleMeshBase-inl.h:75
Element structure for meshes.
Definition: MeshElement.h:44
const TriangleType & getTriangle(size_t id) const
Retrieve a specific triangle.
Definition: TriangleMeshBase-inl.h:152
const SurgSim::Math::Vector3d & getVertexPosition(size_t id) const
Returns the position of a vertex.
Definition: Vertices.h:128
Base class for mesh structures, handling basic vertex functionality.
Definition: Vertices.h:50
bool isValid
Is this a valid element.
Definition: MeshElement.h:73
const std::vector< EdgeType > & getEdges() const
Retrieve all edges.
Definition: TriangleMeshBase-inl.h:110