TriangleMeshPlyReaderDelegate-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_TRIANGLEMESHPLYREADERDELEGATE_INL_H
17 #define SURGSIM_DATASTRUCTURES_TRIANGLEMESHPLYREADERDELEGATE_INL_H
18 
19 template <class M>
21  m_mesh(std::make_shared<M>())
22 {
23 
24 }
25 
26 template <class M>
28  m_mesh(mesh)
29 {
30  SURGSIM_ASSERT(mesh != nullptr) << "The mesh cannot be null.";
31  mesh->clear();
32 }
33 
34 template <class M>
36 {
37  return m_mesh;
38 }
39 
40 template <class M>
42 {
43  // Vertex processing
44  reader->requestElement("vertex",
46  std::placeholders::_1, std::placeholders::_2),
47  std::bind(&TriangleMeshPlyReaderDelegate::processVertex, this, std::placeholders::_1),
48  std::bind(&TriangleMeshPlyReaderDelegate::endVertices, this, std::placeholders::_1));
49  reader->requestScalarProperty("vertex", "x", PlyReader::TYPE_DOUBLE, offsetof(VertexData, x));
50  reader->requestScalarProperty("vertex", "y", PlyReader::TYPE_DOUBLE, offsetof(VertexData, y));
51  reader->requestScalarProperty("vertex", "z", PlyReader::TYPE_DOUBLE, offsetof(VertexData, z));
52 
53  // Normal processing
54  m_hasTextureCoordinates = reader->hasProperty("vertex", "s") && reader->hasProperty("vertex", "t");
55 
57  {
58  reader->requestScalarProperty("vertex", "s", PlyReader::TYPE_DOUBLE, offsetof(VertexData, s));
59  reader->requestScalarProperty("vertex", "t", PlyReader::TYPE_DOUBLE, offsetof(VertexData, t));
60  }
61 
62  // Face Processing
63  reader->requestElement("face",
65  std::placeholders::_1, std::placeholders::_2),
66  std::bind(&TriangleMeshPlyReaderDelegate::processFace, this, std::placeholders::_1),
67  std::bind(&TriangleMeshPlyReaderDelegate::endFaces, this, std::placeholders::_1));
68  reader->requestListProperty("face", "vertex_indices",
70  offsetof(FaceData, indices),
72  offsetof(FaceData, edgeCount));
73 
75 
76  return true;
77 }
78 
79 template <class M>
81 {
82  bool result = true;
83 
84  // Shortcut test if one fails ...
85  result = result && reader.hasProperty("vertex", "x");
86  result = result && reader.hasProperty("vertex", "y");
87  result = result && reader.hasProperty("vertex", "z");
88  result = result && reader.hasProperty("face", "vertex_indices");
89  result = result && !reader.isScalar("face", "vertex_indices");
90 
91  return result;
92 }
93 
94 template <class M>
96  const std::string& elementName,
97  size_t vertexCount)
98 {
100  m_vertexData.overrun2 = 0l;
101  return &m_vertexData;
102 }
103 
104 template <class M>
106 {
107  typename M::VertexType vertex(SurgSim::Math::Vector3d(m_vertexData.x, m_vertexData.y, m_vertexData.z));
108  m_mesh->addVertex(vertex);
109 }
110 
111 template <class M>
113 {
115  "There was an overrun while reading the vertex structures, it is likely that data " <<
116  "has become corrupted.";
117 }
118 
119 template <class M>
121  const std::string& elementName,
122  size_t faceCount)
123 {
124  m_faceData.overrun = 0l;
125  return &m_faceData;
126 }
127 
128 template <class M>
130 {
131  SURGSIM_ASSERT(m_faceData.edgeCount == 3) << "Can only process triangle meshes.";
132  std::copy(m_faceData.indices, m_faceData.indices + 3, m_indices.begin());
133 
134  typename M::TriangleType triangle(m_indices);
135  m_mesh->addTriangle(triangle);
136 }
137 
138 template <class M>
140 {
142  << "There was an overrun while reading the face structures, it is likely that data "
143  << "has become corrupted.";
144 }
145 
146 template <class M>
148 {
149  m_mesh->update();
150 }
151 
152 template <class M>
154 {
156 }
157 
158 
159 #endif
std::shared_ptr< MeshType > getMesh()
Gets the mesh.
Definition: TriangleMeshPlyReaderDelegate-inl.h:35
bool hasTextureCoordinates()
Definition: TriangleMeshPlyReaderDelegate-inl.h:153
virtual void processVertex(const std::string &elementName)
Callback function to process one vertex.
Definition: TriangleMeshPlyReaderDelegate-inl.h:105
void endFaces(const std::string &elementName)
Callback function to finalize processing of faces.
Definition: TriangleMeshPlyReaderDelegate-inl.h:139
Internal structure, the receiver for data from the "vertex" element Provide space for standard ply ve...
Definition: TriangleMeshPlyReaderDelegate.h:99
virtual bool registerDelegate(PlyReader *reader) override
Registers the delegate with the reader, overridden from.
Definition: TriangleMeshPlyReaderDelegate-inl.h:41
double x
Definition: TriangleMeshPlyReaderDelegate.h:101
STL namespace.
double z
Definition: TriangleMeshPlyReaderDelegate.h:103
#define SURGSIM_ASSERT(condition)
Assert that condition is true.
Definition: Assert.h:77
std::array< size_t, 3 > m_indices
Definition: TriangleMeshPlyReaderDelegate.h:122
int64_t overrun
Used to check for buffer overruns.
Definition: TriangleMeshPlyReaderDelegate.h:115
bool m_hasTextureCoordinates
Set to true if s/t coordinates are found in the .ply file.
Definition: TriangleMeshPlyReaderDelegate.h:126
void endVertices(const std::string &elementName)
Callback function to finalize processing of vertices.
Definition: TriangleMeshPlyReaderDelegate-inl.h:112
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:20
void endFile()
Callback function to finalize processing of the mesh.
Definition: TriangleMeshPlyReaderDelegate-inl.h:147
string(TOUPPER ${DEVICE}DEVICE_UPPER_CASE) option(BUILD_DEVICE_ $
Definition: CMakeLists.txt:35
bool hasProperty(std::string elementName, std::string propertyName) const
Query if &#39;elementName&#39; has the given property.
Definition: PlyReader.cpp:331
bool requestScalarProperty(std::string elementName, std::string propertyName, int dataType, int dataOffset)
Request a scalar property for parsing.
Definition: PlyReader.cpp:118
struct SurgSim::DataStructures::TriangleMeshPlyReaderDelegate::VertexData m_vertexData
int64_t overrun2
Used to check for buffer overruns.
Definition: TriangleMeshPlyReaderDelegate.h:107
Internal structure, the received for data from the "face" element.
Definition: TriangleMeshPlyReaderDelegate.h:111
unsigned int edgeCount
Definition: TriangleMeshPlyReaderDelegate.h:113
void processFace(const std::string &elementName)
Callback function to process one face.
Definition: TriangleMeshPlyReaderDelegate-inl.h:129
std::shared_ptr< MeshType > m_mesh
The mesh that will be created.
Definition: TriangleMeshPlyReaderDelegate.h:119
bool requestElement(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:94
bool requestListProperty(std::string elementName, std::string propertyName, int dataType, int dataOffset, int countType, int countOffset)
Request a list property for parsing.
Definition: PlyReader.cpp:123
bool isScalar(std::string elementName, std::string propertyName) const
Query if the property of the give element is scalar.
Definition: PlyReader.cpp:345
void * beginVertices(const std::string &elementName, size_t vertexCount)
Callback function, begin the processing of vertices.
Definition: TriangleMeshPlyReaderDelegate-inl.h:95
struct SurgSim::DataStructures::TriangleMeshPlyReaderDelegate::FaceData m_faceData
unsigned int * indices
Definition: TriangleMeshPlyReaderDelegate.h:114
void setEndParseFileCallback(std::function< void(void)> endParseFileCallback)
Register callback to be called at the end of parseFile.
Definition: PlyReader.cpp:136
Eigen::Matrix< double, 3, 1 > Vector3d
A 3D vector of doubles.
Definition: Vector.h:56
virtual bool fileIsAcceptable(const PlyReader &reader) override
Check whether this file is acceptable to the delegate, overridden from.
Definition: TriangleMeshPlyReaderDelegate-inl.h:80
int64_t overrun1
Used to check for buffer overruns.
Definition: TriangleMeshPlyReaderDelegate.h:104
double y
Definition: TriangleMeshPlyReaderDelegate.h:102
void * beginFaces(const std::string &elementName, size_t faceCount)
Callback function, begin the processing of faces.
Definition: TriangleMeshPlyReaderDelegate-inl.h:120