Go to the documentation of this file.
16 #ifndef SURGSIM_DATASTRUCTURES_TRIANGLEMESH_INL_H
17 #define SURGSIM_DATASTRUCTURES_TRIANGLEMESH_INL_H
24 namespace DataStructures
27 template <
class VertexData,
class EdgeData,
class TriangleData>
32 template <
class VertexData,
class EdgeData,
class TriangleData>
37 m_edges(other.getEdges()),
38 m_triangles(other.getTriangles()),
39 m_freeTriangles(other.m_freeTriangles)
43 template <
class VertexData,
class EdgeData,
class TriangleData>
44 template <
class V,
class E,
class T>
60 if (!triangle.isValid)
68 template <
class VertexData,
class EdgeData,
class TriangleData>
73 template <
class VertexData,
class EdgeData,
class TriangleData>
80 template <
class VertexData,
class EdgeData,
class TriangleData>
83 m_edges.push_back(edge);
84 return m_edges.size() - 1;
87 template <
class VertexData,
class EdgeData,
class TriangleData>
94 if (m_freeTriangles.empty())
96 m_triangles.push_back(triangle);
97 result = m_triangles.size() - 1;
101 result = m_freeTriangles.back();
102 m_freeTriangles.pop_back();
103 m_triangles[result] = triangle;
109 template <
class VertexData,
class EdgeData,
class TriangleData>
112 return m_edges.size();
115 template <
class VertexData,
class EdgeData,
class TriangleData>
118 return m_triangles.size() - m_freeTriangles.size();
121 template <
class VertexData,
class EdgeData,
class TriangleData>
122 const std::vector<typename TriangleMesh<VertexData, EdgeData, TriangleData>::EdgeType>&
128 template <
class VertexData,
class EdgeData,
class TriangleData>
129 std::vector<typename TriangleMesh<VertexData, EdgeData, TriangleData>::EdgeType>&
135 template <
class VertexData,
class EdgeData,
class TriangleData>
136 const std::vector<typename TriangleMesh<VertexData, EdgeData, TriangleData>::TriangleType>&
142 template <
class VertexData,
class EdgeData,
class TriangleData>
143 std::vector<typename TriangleMesh<VertexData, EdgeData, TriangleData>::TriangleType>&
149 template <
class VertexData,
class EdgeData,
class TriangleData>
156 template <
class VertexData,
class EdgeData,
class TriangleData>
163 template <
class VertexData,
class EdgeData,
class TriangleData>
164 std::array<SurgSim::Math::Vector3d, 2>
168 std::array<SurgSim::Math::Vector3d, 2> result =
177 template <
class VertexData,
class EdgeData,
class TriangleData>
181 auto const& triangle = m_triangles[id];
183 <<
"Attempted to access invalid or deleted triangle " <<
id <<
" have " << getNumTriangles();
187 template <
class VertexData,
class EdgeData,
class TriangleData>
191 auto& triangle = m_triangles[id];
196 template <
class VertexData,
class EdgeData,
class TriangleData>
199 auto& triangle = m_triangles[id];
203 m_freeTriangles.push_back(
id);
207 template <
class VertexData,
class EdgeData,
class TriangleData>
208 std::array<SurgSim::Math::Vector3d, 3>
211 auto& ids = getTriangle(
id).verticesId;
212 std::array<SurgSim::Math::Vector3d, 3> result =
222 template <
class VertexData,
class EdgeData,
class TriangleData>
231 for (
typename std::vector<EdgeType>::const_iterator it = m_edges.begin(); it != m_edges.end(); ++it)
233 for (
int vertexId = 0; vertexId < 2; vertexId++)
243 for (
typename std::vector<TriangleType>::const_iterator it = m_triangles.begin(); it != m_triangles.end(); ++it)
245 for (
int vertexId = 0; vertexId < 3; vertexId++)
258 template <
class VertexData,
class EdgeData,
class TriangleData>
264 template <
class VertexData,
class EdgeData,
class TriangleData>
268 m_freeTriangles.clear();
271 template <
class VertexData,
class EdgeData,
class TriangleData>
279 template <
class VertexData,
class EdgeData,
class TriangleData>
286 <<
"'" << fileName <<
"' is an invalid .ply file.";
291 auto delegate = std::make_shared<TriangleMeshPlyReaderDelegate<MeshType>>(this->shared_from_this());
295 <<
"The input file '" << fileName <<
"' does not have the property required by triangle mesh.";
302 template <
class VertexData,
class EdgeData,
class TriangleData>
310 template <
class VertexData,
class EdgeData,
class TriangleData>
317 std::swap(
m_edges, other.m_edges);
321 template <
class VertexData,
class EdgeData,
class TriangleData>
332 template <
class VertexData,
class EdgeData,
class TriangleData>
339 std::swap(m_triangles, other.m_triangles);
340 std::swap(m_edges, other.m_edges);
341 std::swap(m_freeTriangles, other.m_freeTriangles);
345 template <
class VertexData,
class EdgeData,
class TriangleData>
348 std::fstream out(fileName, std::ios::out);
352 out <<
"ply" << std::endl;
353 out <<
"format ascii 1.0" << std::endl;
354 out <<
"comment Created by OpenSurgSim, www.opensurgsim.org" << std::endl;
355 out <<
"element vertex " << getNumVertices() << std::endl;
356 out <<
"property float x\nproperty float y\nproperty float z" << std::endl;
357 out <<
"element face " << getNumTriangles() << std::endl;
358 out <<
"property list uchar uint vertex_indices" << std::endl;
359 out <<
"end_header" << std::endl;
361 for (
const auto& vertex : getVertices())
363 out << vertex.position[0] <<
" " << vertex.position[1] <<
" " << vertex.position[2] << std::endl;
366 for (
const auto& tri : getTriangles())
368 out <<
"3 " << tri.verticesId[0] <<
" " << tri.verticesId[1] <<
" " << tri.verticesId[2] << std::endl;
374 <<
"There was a problem writing " << fileName;
382 <<
"Could not open " << fileName <<
" for writing.";
392 #endif // SURGSIM_DATASTRUCTURES_TRIANGLEMESH_INL_H
#define SURGSIM_ASSERT(condition)
Assert that condition is true.
Definition: Assert.h:77
TriangleMesh< VertexData, EdgeData, TriangleData > & operator=(const TriangleMesh< VertexData, EdgeData, TriangleData > &other)
Copy Assignment.
Definition: TriangleMesh-inl.h:322
Basic class for storing Triangle Meshes, handling basic vertex, edge, and triangle functionality.
Definition: TriangleMesh.h:64
bool doLoad(const std::string &fileName) override
Derived classes will overwrite this method to do actual loading.
Definition: TriangleMesh-inl.h:280
bool parseWithDelegate(std::shared_ptr< PlyReaderDelegate > delegate)
Sets a delegate for parsing and then parse the file.
Definition: PlyReader.cpp:333
size_t getNumTriangles() const
Get the number of triangles.
Definition: TriangleMesh-inl.h:116
bool isValid() const
Test if the TriangleMesh is valid (valid vertex Ids used in all MeshElements)
Definition: TriangleMesh-inl.h:223
const std::vector< EdgeType > & getEdges() const
Retrieve all edges.
Definition: TriangleMesh-inl.h:123
EdgeType & getEdge(size_t id)
Retrieve a specific edge (non const version)
Definition: TriangleMesh-inl.h:158
size_t getNumEdges() const
Get the number of edges.
Definition: TriangleMesh-inl.h:110
Base class for mesh structures, handling basic vertex functionality.
Definition: Vertices.h:52
std::vector< TriangleType > m_triangles
Triangles.
Definition: TriangleMesh.h:235
void removeTriangle(size_t id)
Marks a triangle as invalid, the triangle cannot be accessed via getTriangle anymore.
Definition: TriangleMesh-inl.h:197
int numVertices
Definition: TestCube.cpp:25
const VertexType & getVertex(size_t id) const
Returns the specified vertex.
Definition: Vertices-inl.h:105
Element structure for meshes.
Definition: MeshElement.h:45
std::array< SurgSim::Math::Vector3d, 3 > getTrianglePositions(size_t id) const
Returns an array of the triangle's vertices' positions.
Definition: TriangleMesh-inl.h:209
size_t addTriangle(const TriangleType &triangle)
Adds a triangle to the mesh.
Definition: TriangleMesh-inl.h:88
const std::vector< TriangleType > & getTriangles() const
Retrieve all triangles.
Definition: TriangleMesh-inl.h:137
#define SURGSIM_LOG_WARNING(logger)
Logs a message to the specified logger at the WARNING level.
Definition: LogMacros.h:96
virtual void doClearEdges()
Remove all edges from the mesh.
Definition: TriangleMesh-inl.h:259
Definition: CompoundShapeToGraphics.cpp:30
virtual ~TriangleMesh()
Destructor.
Definition: TriangleMesh-inl.h:69
The convenience header that provides the entirety of the logging API.
IdType verticesId
Element vertices.
Definition: MeshElement.h:78
MeshElement< 3, TriangleData > TriangleType
Triangle type for convenience (Ids of the 3 vertices)
Definition: TriangleMesh.h:69
void save(const std::string &fileName)
Save the triangle mesh in the ply format.
Definition: TriangleMesh-inl.h:346
static std::shared_ptr< Logger > getDefaultLogger()
Get default logger.
Definition: Logger.h:116
const EdgeType & getEdge(size_t id) const
Retrieve a specific edge.
Definition: TriangleMesh-inl.h:151
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-inl.h:184
virtual void doClearTriangles()
Remove all triangles from the mesh.
Definition: TriangleMesh-inl.h:265
Wrapper for the C .ply file parser This class wraps the main functionality for the original C ....
Definition: PlyReader.h:86
std::vector< EdgeType > m_edges
Edges.
Definition: TriangleMesh.h:232
std::vector< size_t > m_freeTriangles
List of indices of deleted triangles, to be reused when another triangle is added.
Definition: TriangleMesh.h:238
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
std::string getClassName() const override
Support serialization with a classname.
Definition: TriangleMesh-inl.h:74
virtual void doClear()
Clear mesh to return to an empty state (no vertices, no edges, no triangles).
Definition: TriangleMesh-inl.h:303
size_t getNumVertices() const
Returns the number of vertices in this mesh.
Definition: Vertices-inl.h:99
SurgSim::Math::Vector3d position
Position of the vertex.
Definition: Vertex.h:82
TriangleMesh()
Constructor. The mesh is initially empty (no vertices, no edges, no triangles).
Definition: TriangleMesh-inl.h:28
MeshElement< 2, EdgeData > EdgeType
Edge type for convenience (Ids of the 2 vertices)
Definition: TriangleMesh.h:67
#define SURGSIM_LOG_SEVERE(logger)
Logs a message to the specified logger at the SEVERE level.
Definition: LogMacros.h:106
string(TOUPPER ${DEVICE} DEVICE_UPPER_CASE) option(BUILD_DEVICE_$
Definition: CMakeLists.txt:38
bool isValid() const
Query if this object is valid.
Definition: PlyReader.cpp:89
const TriangleType & getTriangle(size_t id) const
Retrieve a specific triangle.
Definition: TriangleMesh-inl.h:179
bool isValid
Is this a valid element.
Definition: MeshElement.h:84
std::array< SurgSim::Math::Vector3d, 2 > getEdgePositions(size_t id) const
Returns an array of the edge's vertices' positions.
Definition: TriangleMesh-inl.h:165
Vertices< VertexData > & operator=(const Vertices< V > &other)
Assignment when the template data is a different type In this case, no data will be copied.
Definition: Vertices-inl.h:47
size_t addEdge(const EdgeType &edge)
Adds an edge to the mesh.
Definition: TriangleMesh-inl.h:81