opensurgsim
TriangleMeshPlyReaderDelegate-inl.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_TRIANGLEMESHPLYREADERDELEGATE_INL_H
17 #define SURGSIM_DATASTRUCTURES_TRIANGLEMESHPLYREADERDELEGATE_INL_H
18 
19 #include <cstddef>
20 
21 #include "SurgSim/Math/Vector.h"
22 
23 template <class M>
25  m_mesh(std::make_shared<M>()),
26  m_hasTextureCoordinates(false),
27  m_hasFaces(false),
28  m_hasEdges(false)
29 
30 {
31 
32 }
33 
34 template <class M>
36  m_mesh(mesh),
37  m_hasTextureCoordinates(false),
38  m_hasFaces(false),
39  m_hasEdges(false)
40 {
41  SURGSIM_ASSERT(mesh != nullptr) << "The mesh cannot be null.";
42  mesh->clear();
43 }
44 
45 template <class M>
47 {
48  return m_mesh;
49 }
50 
51 template <class M>
53 {
54  // Vertex processing
55  reader->requestElement("vertex",
56  std::bind(&TriangleMeshPlyReaderDelegate::beginVertices, this,
57  std::placeholders::_1, std::placeholders::_2),
58  std::bind(&TriangleMeshPlyReaderDelegate::processVertex, this, std::placeholders::_1),
59  std::bind(&TriangleMeshPlyReaderDelegate::endVertices, this, std::placeholders::_1));
60  reader->requestScalarProperty("vertex", "x", PlyReader::TYPE_DOUBLE, offsetof(VertexData, x));
61  reader->requestScalarProperty("vertex", "y", PlyReader::TYPE_DOUBLE, offsetof(VertexData, y));
62  reader->requestScalarProperty("vertex", "z", PlyReader::TYPE_DOUBLE, offsetof(VertexData, z));
63 
64  // Normal processing
65  m_hasTextureCoordinates = reader->hasProperty("vertex", "s") && reader->hasProperty("vertex", "t");
66 
67  if (m_hasTextureCoordinates)
68  {
69  reader->requestScalarProperty("vertex", "s", PlyReader::TYPE_DOUBLE, offsetof(VertexData, s));
70  reader->requestScalarProperty("vertex", "t", PlyReader::TYPE_DOUBLE, offsetof(VertexData, t));
71  }
72 
73 
74  if (m_hasFaces)
75  {
76  // Face Processing
77  reader->requestElement("face",
78  std::bind(&TriangleMeshPlyReaderDelegate::beginFaces, this,
79  std::placeholders::_1, std::placeholders::_2),
80  std::bind(&TriangleMeshPlyReaderDelegate::processFace, this, std::placeholders::_1),
81  std::bind(&TriangleMeshPlyReaderDelegate::endFaces, this, std::placeholders::_1));
82  reader->requestListProperty("face", "vertex_indices",
83  PlyReader::TYPE_UNSIGNED_INT,
84  offsetof(ListData, indices),
85  PlyReader::TYPE_UNSIGNED_INT,
86  offsetof(ListData, count));
87  }
88 
89 
90  if (m_hasEdges)
91  {
92  // Edge Processing
93  reader->requestElement("1d_element",
94  std::bind(&TriangleMeshPlyReaderDelegate::beginEdges, this,
95  std::placeholders::_1, std::placeholders::_2),
96  std::bind(&TriangleMeshPlyReaderDelegate::processEdge, this, std::placeholders::_1),
97  std::bind(&TriangleMeshPlyReaderDelegate::endEdges, this, std::placeholders::_1));
98  reader->requestListProperty("1d_element", "vertex_indices",
99  PlyReader::TYPE_UNSIGNED_INT,
100  offsetof(ListData, indices),
101  PlyReader::TYPE_UNSIGNED_INT,
102  offsetof(ListData, count));
103  }
104 
105  reader->setEndParseFileCallback(std::bind(&TriangleMeshPlyReaderDelegate::endFile, this));
106 
107  return true;
108 }
109 
110 template <class M>
112 {
113  bool result = true;
114 
115  m_hasFaces = reader.hasProperty("face", "vertex_indices") &&
116  !reader.isScalar("face", "vertex_indices");
117 
118  m_hasEdges = reader.hasProperty("1d_element", "vertex_indices") &&
119  !reader.isScalar("1d_element", "vertex_indices");
120 
121  // Shortcut test if one fails ...
122  result = result && reader.hasProperty("vertex", "x");
123  result = result && reader.hasProperty("vertex", "y");
124  result = result && reader.hasProperty("vertex", "z");
125  result = result && (m_hasFaces || m_hasEdges);
126 
127  return result;
128 }
129 
130 template <class M>
132  const std::string& elementName,
133  size_t vertexCount)
134 {
135  m_vertexData.overrun1 = 0l;
136  m_vertexData.overrun2 = 0l;
137  return &m_vertexData;
138 }
139 
140 template <class M>
142 {
143  typename M::VertexType vertex(SurgSim::Math::Vector3d(m_vertexData.x, m_vertexData.y, m_vertexData.z));
144  m_mesh->addVertex(vertex);
145 }
146 
147 template <class M>
149 {
150  SURGSIM_ASSERT(m_vertexData.overrun1 == 0l && m_vertexData.overrun2 == 0l) <<
151  "There was an overrun while reading the vertex structures, it is likely that data " <<
152  "has become corrupted.";
153 }
154 
155 template <class M>
157  const std::string& elementName,
158  size_t faceCount)
159 {
160  m_listData.overrun = 0l;
161  return &m_listData;
162 }
163 
164 template <class M>
166 {
167  SURGSIM_ASSERT(m_listData.count == 3) << "Can only process triangle meshes.";
168  std::copy(m_listData.indices, m_listData.indices + 3, m_face.begin());
169 
170  typename M::TriangleType triangle(m_face);
171  m_mesh->addTriangle(triangle);
172 }
173 
174 template <class M>
176 {
177  SURGSIM_ASSERT(m_listData.overrun == 0l)
178  << "There was an overrun while reading the face structures, it is likely that data "
179  << "has become corrupted.";
180 }
181 
182 template <class M>
184 {
185  m_mesh->update();
186 }
187 
188 template <class M>
190 {
191  return m_hasTextureCoordinates;
192 }
193 
194 template <class M>
196  size_t edgeCount)
197 {
198  m_listData.overrun = 0l;
199  return &m_listData;
200 }
201 
202 
203 template <class M>
205 {
206  SURGSIM_ASSERT(m_listData.count == 2) << "Edges have to have 2 points.";
207  std::copy(m_listData.indices, m_listData.indices + 2, m_edge.begin());
208 
209  typename M::EdgeType edge(m_edge);
210  m_mesh->addEdge(edge);
211 }
212 
213 
214 template <class M>
216 {
217  SURGSIM_ASSERT(m_listData.overrun == 0l)
218  << "There was an overrun while reading the face structures, it is likely that data "
219  << "has become corrupted.";
220 }
221 
222 
223 #endif
std::shared_ptr< MeshType > getMesh()
Gets the mesh.
Definition: TriangleMeshPlyReaderDelegate-inl.h:46
bool hasProperty(const std::string &elementName, const std::string &propertyName) const
Query if &#39;elementName&#39; has the given property.
Definition: PlyReader.cpp:353
bool hasTextureCoordinates()
Definition: TriangleMeshPlyReaderDelegate-inl.h:189
virtual void processVertex(const std::string &elementName)
Callback function to process one vertex.
Definition: TriangleMeshPlyReaderDelegate-inl.h:141
void endFaces(const std::string &elementName)
Callback function to finalize processing of faces.
Definition: TriangleMeshPlyReaderDelegate-inl.h:175
bool registerDelegate(PlyReader *reader) override
Registers the delegate with the reader, overridden from.
Definition: TriangleMeshPlyReaderDelegate-inl.h:52
bool requestScalarProperty(const std::string &elementName, const std::string &propertyName, int dataType, int dataOffset)
Request a scalar property for parsing.
Definition: PlyReader.cpp:121
Definition: MockObjects.h:47
#define SURGSIM_ASSERT(condition)
Assert that condition is true.
Definition: Assert.h:77
Eigen::Matrix< double, 3, 1 > Vector3d
A 3D vector of doubles.
Definition: Vector.h:57
void endVertices(const std::string &elementName)
Callback function to finalize processing of vertices.
Definition: TriangleMeshPlyReaderDelegate-inl.h:148
Wrapper for the C .ply file parser This class wraps the main functionality for the original C ...
Definition: PlyReader.h:85
TriangleMeshPlyReaderDelegate()
Default constructor.
Definition: TriangleMeshPlyReaderDelegate-inl.h:24
void endFile()
Callback function to finalize processing of the mesh.
Definition: TriangleMeshPlyReaderDelegate-inl.h:183
Implementation of PlyReaderDelegate for simple triangle meshes.
Definition: TriangleMeshPlyReaderDelegate.h:33
bool requestListProperty(const std::string &elementName, const std::string &propertyName, int dataType, int dataOffset, int countType, int countOffset)
Request a list property for parsing.
Definition: PlyReader.cpp:127
bool isScalar(const std::string &elementName, const std::string &propertyName) const
Query if the property of the give element is scalar.
Definition: PlyReader.cpp:367
void processFace(const std::string &elementName)
Callback function to process one face.
Definition: TriangleMeshPlyReaderDelegate-inl.h:165
Definitions of small fixed-size vector types.
bool requestElement(const std::string &elementName, std::function< void *(const std::string &, size_t)> startElementCallback, std::function< void(const std::string &)> processElementCallback, std::function< void(const std::string &)> endElementCallback)
Request element to be processed during parsing.
Definition: PlyReader.cpp:97
void * beginVertices(const std::string &elementName, size_t vertexCount)
Callback function, begin the processing of vertices.
Definition: TriangleMeshPlyReaderDelegate-inl.h:131
void setEndParseFileCallback(std::function< void(void)> endParseFileCallback)
Register callback to be called at the end of parseFile.
Definition: PlyReader.cpp:140
bool fileIsAcceptable(const PlyReader &reader) override
Check whether this file is acceptable to the delegate, overridden from.
Definition: TriangleMeshPlyReaderDelegate-inl.h:111
void * beginFaces(const std::string &elementName, size_t faceCount)
Callback function, begin the processing of faces.
Definition: TriangleMeshPlyReaderDelegate-inl.h:156