All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PlyReader.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_PLYREADER_H
17 #define SURGSIM_DATASTRUCTURES_PLYREADER_H
18 
19 #include <string>
20 #include <functional>
21 #include <memory>
22 #include <unordered_map>
23 #include <vector>
24 
25 namespace SurgSim
26 {
27 namespace DataStructures
28 {
29 
30 class PlyReaderDelegate;
31 
83 
84 
85 class PlyReader
86 {
87 public:
89  enum Type
90  {
101  };
102 
107  typedef std::function<void* (const std::string&, size_t)> StartElementCallbackType;
108 
111  typedef std::function<void (const std::string&)> StandardCallbackType;
112 
115  explicit PlyReader(std::string filename);
116 
118  virtual ~PlyReader();
119 
122  bool isValid() const;
123 
132  bool requestElement(std::string elementName,
133  std::function<void* (const std::string&, size_t)> startElementCallback,
134  std::function<void (const std::string&)> processElementCallback,
135  std::function<void (const std::string&)> endElementCallback);
136 
150  bool requestScalarProperty(std::string elementName, std::string propertyName, int dataType, int dataOffset);
151 
166  bool requestListProperty(std::string elementName,
167  std::string propertyName,
168  int dataType, int dataOffset,
169  int countType, int countOffset);
170 
174  bool hasElement(std::string elementName) const;
175 
180  bool hasProperty(std::string elementName, std::string propertyName) const;
181 
186  bool isScalar(std::string elementName, std::string propertyName) const;
187 
191  bool parseWithDelegate(std::shared_ptr<PlyReaderDelegate> delegate);
192 
194  void setStartParseFileCallback(std::function<void (void)> startParseFileCallback);
195 
197  void setEndParseFileCallback(std::function<void (void)> endParseFileCallback);
198 
202  bool setDelegate(std::shared_ptr<PlyReaderDelegate> delegate);
203 
205  void parseFile();
206 
207 private:
208  friend class PlyReaderTests;
209 
220  bool requestProperty(std::string elementName,
221  std::string propertyName,
222  int dataType, int dataOffset,
223  int countType, int countOffset);
224 
226  std::string m_filename;
227 
230  {
231  std::string propertyName;
232  int dataType;
234  int countType;
236  };
237 
239  struct ElementInfo
240  {
241  std::string name;
245  std::vector<PropertyInfo> requestedProperties;
246  };
247 
248  std::unordered_map<std::string, ElementInfo> m_requestedElements;
249 
252  struct Data;
253  std::unique_ptr<Data> m_data;
255 
257  std::shared_ptr<PlyReaderDelegate> m_delegate;
258 
260  std::function<void(void)> m_startParseFileCallback;
261 
263  std::function<void(void)> m_endParseFileCallback;
264 };
265 
266 } // DataStructures
267 } // SurgSim
268 
269 #endif // SURGSIM_DATASTRUCTURES_PLYREADER_H
void parseFile()
Parse the file.
Definition: PlyReader.cpp:210
virtual ~PlyReader()
Destructor.
Definition: PlyReader.cpp:74
bool hasElement(std::string elementName) const
Query if this elementName is in the .ply file.
Definition: PlyReader.cpp:324
Information about the property on the .ply file.
Definition: PlyReader.h:229
void setStartParseFileCallback(std::function< void(void)> startParseFileCallback)
Register callback to be called at the begining of parseFile.
Definition: PlyReader.cpp:131
std::shared_ptr< PlyReaderDelegate > m_delegate
The delegate.
Definition: PlyReader.h:257
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 n...
Definition: PlyReader.h:111
std::unique_ptr< Data > m_data
Definition: PlyReader.h:252
int dataType
Type of the receiving data.
Definition: PlyReader.h:232
std::string propertyName
Name of the property.
Definition: PlyReader.h:231
Definition: PlyReader.cpp:34
Wrapper for the C .ply file parser This class wraps the main functionality for the original C ...
Definition: PlyReader.h:85
int dataOffset
Location for the receiving data.
Definition: PlyReader.h:233
bool hasProperty(std::string elementName, std::string propertyName) const
Query if 'elementName' has the given property.
Definition: PlyReader.cpp:331
Type
Values that represent the data type/size of requested data.
Definition: PlyReader.h:89
StandardCallbackType processElementCallback
Callback to be used for each processed element.
Definition: PlyReader.h:243
bool setDelegate(std::shared_ptr< PlyReaderDelegate > delegate)
Sets a delegate for parsing.
Definition: PlyReader.cpp:195
std::string m_filename
The name of the .ply file.
Definition: PlyReader.h:226
int countOffset
For lists, location of the receiving data for the count.
Definition: PlyReader.h:235
bool requestScalarProperty(std::string elementName, std::string propertyName, int dataType, int dataOffset)
Request a scalar property for parsing.
Definition: PlyReader.cpp:118
friend class PlyReaderTests
Definition: PlyReader.h:208
bool requestProperty(std::string elementName, std::string propertyName, int dataType, int dataOffset, int countType, int countOffset)
Generic Internal function to handle list and scalar properties, see requestScalarProperty() and reque...
Definition: PlyReader.cpp:141
std::function< void(void)> m_startParseFileCallback
Callback to be executed at the start of 'parseFile'.
Definition: PlyReader.h:260
std::unordered_map< std::string, ElementInfo > m_requestedElements
Definition: PlyReader.h:248
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 i...
Definition: PlyReader.h:107
StartElementCallbackType startElementCallback
Callback to be used when the element is first encountered.
Definition: PlyReader.h:242
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 isValid() const
Query if this object is valid.
Definition: PlyReader.cpp:89
std::vector< PropertyInfo > requestedProperties
All the properties that are wanted.
Definition: PlyReader.h:245
std::string name
Name of the element.
Definition: PlyReader.h:241
bool isScalar(std::string elementName, std::string propertyName) const
Query if the property of the give element is scalar.
Definition: PlyReader.cpp:345
std::function< void(void)> m_endParseFileCallback
Callback to be executed at the end of 'parseFile'.
Definition: PlyReader.h:263
Information about the element in the .ply file.
Definition: PlyReader.h:239
void setEndParseFileCallback(std::function< void(void)> endParseFileCallback)
Register callback to be called at the end of parseFile.
Definition: PlyReader.cpp:136
bool parseWithDelegate(std::shared_ptr< PlyReaderDelegate > delegate)
Sets a delegate for parsing and then parse the file.
Definition: PlyReader.cpp:314
PlyReader(std::string filename)
Constructor.
Definition: PlyReader.cpp:60
StandardCallbackType endElementCallback
Callback to be used after all the elements have been processed.
Definition: PlyReader.h:244
int countType
For lists, type of the receiving data for the count of listelements.
Definition: PlyReader.h:234