opensurgsim
|
Wrapper for the C .ply file parser This class wraps the main functionality for the original C .ply file parser at http://paulbourke.net/dataformats/ply/ it enables customization of the parsing process either through a delegate class or through executing the requestElement and requestProperty functions. More...
#include <PlyReader.h>
Classes | |
struct | Data |
Public Types | |
enum | Type { TYPE_INVALID = 0, TYPE_CHAR, TYPE_SHORT, TYPE_INT, TYPE_UNSIGNED_CHAR, TYPE_UNSIGNED_SHORT, TYPE_UNSIGNED_INT, TYPE_FLOAT, TYPE_DOUBLE, TYPE_COUNT } |
Values that represent the data type/size of requested data. | |
typedef std::function< void *(const std::string &, size_t)> | StartElementCallbackType |
The callback that is being used to indicate the start of an element, the parameters that are passed into the callback are the name of the element that is being processed and the number of elements that will be processed. More... | |
typedef std::function< void(const std::string &)> | StandardCallbackType |
The callback that is used for the processing and the end of processing, the parameter passed is the name of the element that is being processed. More... | |
Public Member Functions | |
PlyReader (const std::string &filename) | |
Constructor. More... | |
virtual | ~PlyReader () |
Destructor. | |
bool | isValid () const |
Query if this object is valid. More... | |
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. More... | |
bool | requestScalarProperty (const std::string &elementName, const std::string &propertyName, int dataType, int dataOffset) |
Request a scalar property for parsing. More... | |
bool | requestListProperty (const std::string &elementName, const std::string &propertyName, int dataType, int dataOffset, int countType, int countOffset) |
Request a list property for parsing. More... | |
bool | hasElement (const std::string &elementName) const |
Query if this elementName is in the .ply file. More... | |
bool | hasProperty (const std::string &elementName, const std::string &propertyName) const |
Query if 'elementName' has the given property. More... | |
bool | isScalar (const std::string &elementName, const std::string &propertyName) const |
Query if the property of the give element is scalar. More... | |
bool | parseWithDelegate (std::shared_ptr< PlyReaderDelegate > delegate) |
Sets a delegate for parsing and then parse the file. More... | |
void | setStartParseFileCallback (std::function< void(void)> startParseFileCallback) |
Register callback to be called at the begining of parseFile. | |
void | setEndParseFileCallback (std::function< void(void)> endParseFileCallback) |
Register callback to be called at the end of parseFile. | |
bool | setDelegate (std::shared_ptr< PlyReaderDelegate > delegate) |
Sets a delegate for parsing. More... | |
void | parseFile () |
Parse the file. | |
Friends | |
class | PlyReaderTests |
Wrapper for the C .ply file parser This class wraps the main functionality for the original C .ply file parser at http://paulbourke.net/dataformats/ply/ it enables customization of the parsing process either through a delegate class or through executing the requestElement and requestProperty functions.
The file needs to be a valid .ply file, either ascii or binary, for the reader to be a valid reader.
A ply file consists of a header description followed by the data, a header might look like this ply format ascii 1.0 { ascii/binary, format version number } comment made by Greg Turk { comments keyword specified, like all lines } comment this file is a cube element vertex 8 { define "vertex" element, 8 of them in file } property float x { vertex contains float "x" coordinate } property float y { y coordinate is also a vertex property } property float z { z coordinate, too } element face 6 { there are 6 "face" elements in the file } property list uchar int vertex_indices { "vertex_indices" is a list of ints } end_header { delimits the end of the header }
As you can see there are elements with properties, there can be multiple elements and multiple properties per element. An element can have scalar and/or list properties. To work correctly users will have to preallocate the memory that will be used by the parser to deposit the information from the file, and set offsets to the correct value to match the expected locations in the preallocated receiving memory.
The constructor for the PlyReader accepts the file name of the file, it will make sure the the file exists and is actually a .ply file, after the constructor has executed isValid() should be true. At this time all the information in the header is available and users of the reader can tell the reader which elements and which properties are of interest using the requestElement() and requestProperty() functions.
Parsing is accomplished via a set of callback functions. There are three kinds of callback function each with a different responsibility.
The PlyReaderDelegate interface should be used to create classes that encapsulate the needed callbacks and their processing.
typedef std::function<void (const std::string&)> SurgSim::DataStructures::PlyReader::StandardCallbackType |
The callback that is used for the processing and the end of processing, the parameter passed is the name of the element that is being processed.
typedef std::function<void* (const std::string&, size_t)> SurgSim::DataStructures::PlyReader::StartElementCallbackType |
The callback that is being used to indicate the start of an element, the parameters that are passed into the callback are the name of the element that is being processed and the number of elements that will be processed.
The callback needs to return allocated memory that is used by the reader to deposit the read information
|
explicit |
Constructor.
filename | Filename of the .ply file. |
bool SurgSim::DataStructures::PlyReader::hasElement | ( | const std::string & | elementName | ) | const |
Query if this elementName is in the .ply file.
elementName | Name of the element. |
bool SurgSim::DataStructures::PlyReader::hasProperty | ( | const std::string & | elementName, |
const std::string & | propertyName | ||
) | const |
Query if 'elementName' has the given property.
elementName | Name of the element. |
propertyName | Name of the property. |
bool SurgSim::DataStructures::PlyReader::isScalar | ( | const std::string & | elementName, |
const std::string & | propertyName | ||
) | const |
Query if the property of the give element is scalar.
elementName | Name of the element. |
propertyName | Name of the property. |
bool SurgSim::DataStructures::PlyReader::isValid | ( | ) | const |
Query if this object is valid.
bool SurgSim::DataStructures::PlyReader::parseWithDelegate | ( | std::shared_ptr< PlyReaderDelegate > | delegate | ) |
Sets a delegate for parsing and then parse the file.
delegate | The delegate. |
bool SurgSim::DataStructures::PlyReader::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.
elementName | Name of the element that is needed. |
startElementCallback | The callback to be used when the element is first encountered. |
processElementCallback | The callback to be used when one element of this type is read. |
endElementCallback | The callback to be used when all of the elements of this type have been read. |
bool SurgSim::DataStructures::PlyReader::requestListProperty | ( | const std::string & | elementName, |
const std::string & | propertyName, | ||
int | dataType, | ||
int | dataOffset, | ||
int | countType, | ||
int | countOffset | ||
) |
Request a list property for parsing.
Use this for when you want the information from a list property from the .ply file. The item in your data structure should be a pointer of the type of data that you want, the reader will allocate the needed space and deposit all the items in the list in this space.
elementName | Name of the element that contains this property. |
propertyName | Name of the property that you want stored. |
dataType | The type of the data that should be stored. |
dataOffset | The offset of the data in your data structure where the data should be stored. |
countType | The type of the number of element that should be stored. |
countOffset | The offset for storing the count. |
bool SurgSim::DataStructures::PlyReader::requestScalarProperty | ( | const std::string & | elementName, |
const std::string & | propertyName, | ||
int | dataType, | ||
int | dataOffset | ||
) |
Request a scalar property for parsing.
Use this for when you want the information from a scalar property from the .ply file. With this call you register the type that you want for storing the data and the offset in the data structure where the information should be stored. The data actually comes from the startElementCallback that was supplied in the previous call
elementName | Name of the element that contains this property. |
propertyName | Name of the property that you want stored. |
dataType | The type of the data that should be stored. |
dataOffset | The offset of the data in your data structure where the data should be stored. |
bool SurgSim::DataStructures::PlyReader::setDelegate | ( | std::shared_ptr< PlyReaderDelegate > | delegate | ) |
Sets a delegate for parsing.
delegate | The delegate. |