TriangleMeshPlyReaderDelegate-inl.h
Go to the documentation of this file.
// This file is a part of the OpenSurgSim project.
// Copyright 2013, SimQuest Solutions Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef SURGSIM_DATASTRUCTURES_TRIANGLEMESHPLYREADERDELEGATE_INL_H
#define SURGSIM_DATASTRUCTURES_TRIANGLEMESHPLYREADERDELEGATE_INL_H
#include <cstddef>
#include "SurgSim/Math/Vector.h"
template <class M>
SurgSim::DataStructures::TriangleMeshPlyReaderDelegate<M>::TriangleMeshPlyReaderDelegate() :
m_mesh(std::make_shared<M>()),
m_hasTextureCoordinates(false),
m_hasFaces(false),
m_hasEdges(false)
{
}
template <class M>
SurgSim::DataStructures::TriangleMeshPlyReaderDelegate<M>::TriangleMeshPlyReaderDelegate(std::shared_ptr<M> mesh) :
m_mesh(mesh),
m_hasTextureCoordinates(false),
m_hasFaces(false),
m_hasEdges(false)
{
SURGSIM_ASSERT(mesh != nullptr) << "The mesh cannot be null.";
mesh->clear();
}
template <class M>
std::shared_ptr<M> SurgSim::DataStructures::TriangleMeshPlyReaderDelegate<M>::getMesh()
{
return m_mesh;
}
template <class M>
bool SurgSim::DataStructures::TriangleMeshPlyReaderDelegate<M>::registerDelegate(PlyReader* reader)
{
// Vertex processing
reader->requestElement("vertex",
std::bind(&TriangleMeshPlyReaderDelegate::beginVertices, this,
std::placeholders::_1, std::placeholders::_2),
std::bind(&TriangleMeshPlyReaderDelegate::processVertex, this, std::placeholders::_1),
std::bind(&TriangleMeshPlyReaderDelegate::endVertices, this, std::placeholders::_1));
reader->requestScalarProperty("vertex", "x", PlyReader::TYPE_DOUBLE, offsetof(VertexData, x));
reader->requestScalarProperty("vertex", "y", PlyReader::TYPE_DOUBLE, offsetof(VertexData, y));
reader->requestScalarProperty("vertex", "z", PlyReader::TYPE_DOUBLE, offsetof(VertexData, z));
// Normal processing
m_hasTextureCoordinates = reader->hasProperty("vertex", "s") && reader->hasProperty("vertex", "t");
if (m_hasTextureCoordinates)
{
reader->requestScalarProperty("vertex", "s", PlyReader::TYPE_DOUBLE, offsetof(VertexData, s));
reader->requestScalarProperty("vertex", "t", PlyReader::TYPE_DOUBLE, offsetof(VertexData, t));
}
if (m_hasFaces)
{
// Face Processing
reader->requestElement("face",
std::bind(&TriangleMeshPlyReaderDelegate::beginFaces, this,
std::placeholders::_1, std::placeholders::_2),
std::bind(&TriangleMeshPlyReaderDelegate::processFace, this, std::placeholders::_1),
std::bind(&TriangleMeshPlyReaderDelegate::endFaces, this, std::placeholders::_1));
reader->requestListProperty("face", "vertex_indices",
PlyReader::TYPE_UNSIGNED_INT,
offsetof(ListData, indices),
PlyReader::TYPE_UNSIGNED_INT,
offsetof(ListData, count));
}
if (m_hasEdges)
{
// Edge Processing
reader->requestElement("1d_element",
std::bind(&TriangleMeshPlyReaderDelegate::beginEdges, this,
std::placeholders::_1, std::placeholders::_2),
std::bind(&TriangleMeshPlyReaderDelegate::processEdge, this, std::placeholders::_1),
std::bind(&TriangleMeshPlyReaderDelegate::endEdges, this, std::placeholders::_1));
reader->requestListProperty("1d_element", "vertex_indices",
PlyReader::TYPE_UNSIGNED_INT,
offsetof(ListData, indices),
PlyReader::TYPE_UNSIGNED_INT,
offsetof(ListData, count));
}
reader->setEndParseFileCallback(std::bind(&TriangleMeshPlyReaderDelegate::endFile, this));
return true;
}
template <class M>
bool SurgSim::DataStructures::TriangleMeshPlyReaderDelegate<M>::fileIsAcceptable(const PlyReader& reader)
{
bool result = true;
m_hasFaces = reader.hasProperty("face", "vertex_indices") &&
!reader.isScalar("face", "vertex_indices");
m_hasEdges = reader.hasProperty("1d_element", "vertex_indices") &&
!reader.isScalar("1d_element", "vertex_indices");
// Shortcut test if one fails ...
result = result && reader.hasProperty("vertex", "x");
result = result && reader.hasProperty("vertex", "y");
result = result && reader.hasProperty("vertex", "z");
result = result && (m_hasFaces || m_hasEdges);
return result;
}
template <class M>
void* SurgSim::DataStructures::TriangleMeshPlyReaderDelegate<M>::beginVertices(
const std::string& elementName,
size_t vertexCount)
{
m_vertexData.overrun1 = 0l;
m_vertexData.overrun2 = 0l;
return &m_vertexData;
}
template <class M>
void SurgSim::DataStructures::TriangleMeshPlyReaderDelegate<M>::processVertex(const std::string& elementName)
{
typename M::VertexType vertex(SurgSim::Math::Vector3d(m_vertexData.x, m_vertexData.y, m_vertexData.z));
m_mesh->addVertex(vertex);
}
template <class M>
void SurgSim::DataStructures::TriangleMeshPlyReaderDelegate<M>::endVertices(const std::string& elementName)
{
SURGSIM_ASSERT(m_vertexData.overrun1 == 0l && m_vertexData.overrun2 == 0l) <<
"There was an overrun while reading the vertex structures, it is likely that data " <<
"has become corrupted.";
}
template <class M>
void* SurgSim::DataStructures::TriangleMeshPlyReaderDelegate<M>::beginFaces(
const std::string& elementName,
size_t faceCount)
{
m_listData.overrun = 0l;
return &m_listData;
}
template <class M>
void SurgSim::DataStructures::TriangleMeshPlyReaderDelegate<M>::processFace(const std::string& elementName)
{
SURGSIM_ASSERT(m_listData.count == 3) << "Can only process triangle meshes.";
std::copy(m_listData.indices, m_listData.indices + 3, m_face.begin());
typename M::TriangleType triangle(m_face);
m_mesh->addTriangle(triangle);
}
template <class M>
void SurgSim::DataStructures::TriangleMeshPlyReaderDelegate<M>::endFaces(const std::string& elementName)
{
SURGSIM_ASSERT(m_listData.overrun == 0l)
<< "There was an overrun while reading the face structures, it is likely that data "
<< "has become corrupted.";
}
template <class M>
void SurgSim::DataStructures::TriangleMeshPlyReaderDelegate<M>::endFile()
{
m_mesh->update();
}
template <class M>
bool SurgSim::DataStructures::TriangleMeshPlyReaderDelegate<M>::hasTextureCoordinates()
{
return m_hasTextureCoordinates;
}
template <class M>
void* SurgSim::DataStructures::TriangleMeshPlyReaderDelegate<M>::beginEdges(const std::string& elementName,
size_t edgeCount)
{
m_listData.overrun = 0l;
return &m_listData;
}
template <class M>
void SurgSim::DataStructures::TriangleMeshPlyReaderDelegate<M>::processEdge(const std::string& elementName)
{
SURGSIM_ASSERT(m_listData.count == 2) << "Edges have to have 2 points.";
std::copy(m_listData.indices, m_listData.indices + 2, m_edge.begin());
typename M::EdgeType edge(m_edge);
m_mesh->addEdge(edge);
}
template <class M>
void SurgSim::DataStructures::TriangleMeshPlyReaderDelegate<M>::endEdges(const std::string& elementName)
{
SURGSIM_ASSERT(m_listData.overrun == 0l)
<< "There was an overrun while reading the face structures, it is likely that data "
<< "has become corrupted.";
}
#endif
SurgSim::DataStructures::TriangleMeshPlyReaderDelegate::getMesh
std::shared_ptr< MeshType > getMesh()
Gets the mesh.
Definition: TriangleMeshPlyReaderDelegate-inl.h:46
SurgSim::DataStructures::PlyReader::hasProperty
bool hasProperty(const std::string &elementName, const std::string &propertyName) const
Query if &#39;elementName&#39; has the given property.
Definition: PlyReader.cpp:350
SurgSim::DataStructures::TriangleMeshPlyReaderDelegate::hasTextureCoordinates
bool hasTextureCoordinates()
Definition: TriangleMeshPlyReaderDelegate-inl.h:189
SurgSim::DataStructures::TriangleMeshPlyReaderDelegate::processVertex
virtual void processVertex(const std::string &elementName)
Callback function to process one vertex.
Definition: TriangleMeshPlyReaderDelegate-inl.h:141
SurgSim::DataStructures::TriangleMeshPlyReaderDelegate::endFaces
void endFaces(const std::string &elementName)
Callback function to finalize processing of faces.
Definition: TriangleMeshPlyReaderDelegate-inl.h:175
SurgSim::DataStructures::TriangleMeshPlyReaderDelegate::registerDelegate
bool registerDelegate(PlyReader *reader) override
Registers the delegate with the reader, overridden from.
Definition: TriangleMeshPlyReaderDelegate-inl.h:52
SurgSim::DataStructures::PlyReader::requestScalarProperty
bool requestScalarProperty(const std::string &elementName, const std::string &propertyName, int dataType, int dataOffset)
Request a scalar property for parsing.
Definition: PlyReader.cpp:118
std
STL namespace.
SURGSIM_ASSERT
#define SURGSIM_ASSERT(condition)
Assert that condition is true.
Definition: Assert.h:77
SurgSim::DataStructures::TriangleMeshPlyReaderDelegate::processEdge
void processEdge(const std::string &elementName)
Definition: TriangleMeshPlyReaderDelegate-inl.h:204
SurgSim::DataStructures::TriangleMeshPlyReaderDelegate::endVertices
void endVertices(const std::string &elementName)
Callback function to finalize processing of vertices.
Definition: TriangleMeshPlyReaderDelegate-inl.h:148
SurgSim::DataStructures::PlyReader
Wrapper for the C .ply file parser This class wraps the main functionality for the original C ...
Definition: PlyReader.h:85
SurgSim::DataStructures::TriangleMeshPlyReaderDelegate::TriangleMeshPlyReaderDelegate
TriangleMeshPlyReaderDelegate()
Default constructor.
Definition: TriangleMeshPlyReaderDelegate-inl.h:24
SurgSim::DataStructures::TriangleMeshPlyReaderDelegate::endFile
void endFile()
Callback function to finalize processing of the mesh.
Definition: TriangleMeshPlyReaderDelegate-inl.h:183
SurgSim::DataStructures::PlyReader::requestListProperty
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:124
SurgSim::DataStructures::PlyReader::isScalar
bool isScalar(const std::string &elementName, const std::string &propertyName) const
Query if the property of the give element is scalar.
Definition: PlyReader.cpp:364
SurgSim::DataStructures::TriangleMeshPlyReaderDelegate::processFace
void processFace(const std::string &elementName)
Callback function to process one face.
Definition: TriangleMeshPlyReaderDelegate-inl.h:165
Vector.h
Definitions of small fixed-size vector types.
SurgSim::DataStructures::PlyReader::requestElement
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:94
SurgSim::DataStructures::TriangleMeshPlyReaderDelegate::beginVertices
void * beginVertices(const std::string &elementName, size_t vertexCount)
Callback function, begin the processing of vertices.
Definition: TriangleMeshPlyReaderDelegate-inl.h:131
SurgSim::DataStructures::PlyReader::setEndParseFileCallback
void setEndParseFileCallback(std::function< void(void)> endParseFileCallback)
Register callback to be called at the end of parseFile.
Definition: PlyReader.cpp:137
SurgSim::Math::Vector3d
Eigen::Matrix< double, 3, 1 > Vector3d
A 3D vector of doubles.
Definition: Vector.h:57
SurgSim::DataStructures::TriangleMeshPlyReaderDelegate::fileIsAcceptable
bool fileIsAcceptable(const PlyReader &reader) override
Check whether this file is acceptable to the delegate, overridden from.
Definition: TriangleMeshPlyReaderDelegate-inl.h:111
SurgSim::DataStructures::TriangleMeshPlyReaderDelegate::beginEdges
void * beginEdges(const std::string &elementName, size_t edgeCount)
Definition: TriangleMeshPlyReaderDelegate-inl.h:195
SurgSim::DataStructures::TriangleMeshPlyReaderDelegate::endEdges
void endEdges(const std::string &elementName)
Definition: TriangleMeshPlyReaderDelegate-inl.h:215
SurgSim::DataStructures::TriangleMeshPlyReaderDelegate::beginFaces
void * beginFaces(const std::string &elementName, size_t faceCount)
Callback function, begin the processing of faces.
Definition: TriangleMeshPlyReaderDelegate-inl.h:156