MorphoGraphX
PlyFile.hpp
1 //
2 // This file is part of MorphoGraphX - http://www.MorphoGraphX.org
3 // Copyright (C) 2012-2015 Richard S. Smith and collaborators.
4 //
5 // If you use MorphoGraphX in your work, please cite:
6 // http://dx.doi.org/10.7554/eLife.05864
7 //
8 // MorphoGraphX is free software, and is licensed under under the terms of the
9 // GNU General (GPL) Public License version 2.0, http://www.gnu.org/licenses.
10 //
11 #ifndef PLYFILE_HPP
12 #define PLYFILE_HPP
13 
14 #include <Config.hpp>
15 
16 #include <QHash>
17 #include <QList>
18 #include <QString>
19 #include <QStringList>
20 #include <stdint.h>
21 #include <vector>
22 
23 class QFile;
24 
25 #define FORALL_PLY_TYPES(macro) \
26  macro(int8_t); \
27  macro(uint8_t); \
28  macro(int16_t); \
29  macro(uint16_t); \
30  macro(int32_t); \
31  macro(uint32_t); \
32  macro(float); \
33  macro(double)
34 
35 #define FORALL_PLY_TYPEIDS(macro) \
36  macro(int8_t, PlyFile::CHAR); \
37  macro(uint8_t, PlyFile::UCHAR); \
38  macro(int16_t, PlyFile::SHORT); \
39  macro(uint16_t, PlyFile::USHORT); \
40  macro(int32_t, PlyFile::INT); \
41  macro(uint32_t, PlyFile::UINT); \
42  macro(float, PlyFile::FLOAT); \
43  macro(double, PlyFile::DOUBLE)
44 
45 namespace mgx {
46 namespace util {
47 
65 struct PlyFile {
69  enum TYPE {
70  CHAR,
74  INT,
75  UINT,
79  INVALID_TYPE = NB_TYPES
80  };
81 
82 #define INLINE_TYPE_ASSOC(T, TYPEID) \
83  static inline TYPE getType(const T &) { return TYPEID; \
84  }
85  FORALL_PLY_TYPEIDS(INLINE_TYPE_ASSOC)
86 #undef INLINE_TYPE_ASSOC
87 
91  static const unsigned int typeSizes[NB_TYPES];
95  static char const* const typeNames[NB_TYPES + 1];
96 
105  };
106 
110  static char const* const formatNames[4];
111 
112  struct Element;
113 
119  struct Property {
123  enum KIND {
126  };
138  Property(const QString& name, Element* el = 0);
139 
143  ~Property();
144 
150  void allocate(size_t size);
151 
155  void deallocate();
156 
160  template <typename T> std::vector<std::vector<T> >* list()
161  {
162  if(_kind != LIST)
163  return 0;
164  if(getType(T()) != _memType)
165  return 0;
166  return (std::vector<std::vector<T> >*)_content;
167  }
168 
173  template <typename T> std::vector<T>* value()
174  {
175  if(_kind != VALUE)
176  return 0;
177  if(getType(T()) != _memType)
178  return 0;
179  return (std::vector<T>*)_content;
180  }
181 
185  template <typename T> const std::vector<std::vector<T> >* list() const
186  {
187  if(_kind != LIST)
188  return 0;
189  if(getType(T()) != _memType)
190  return 0;
191  return (const std::vector<std::vector<T> >*)_content;
192  }
193 
198  template <typename T> const std::vector<T>* value() const
199  {
200  if(_kind != VALUE)
201  return 0;
202  if(getType(T()) != _memType)
203  return 0;
204  return (const std::vector<T>*)_content;
205  }
206 
210  bool error(const QString& str) const;
211 
215  const QString& name() const {
216  return _name;
217  }
218 
222  TYPE fileType() const {
223  return _fileType;
224  }
225 
229  TYPE memType() const {
230  return _memType;
231  }
232 
236  TYPE sizeType() const {
237  return _sizeType;
238  }
239 
243  KIND kind() const {
244  return _kind;
245  }
246 
250  size_t size() const {
251  return _size;
252  }
253 
259  bool rename(const QString& n);
260 
264  void setFileType(TYPE ft) {
265  _fileType = ft;
266  }
267 
273  void setMemType(TYPE mt);
274 
278  void setSizeType(TYPE st) {
279  _sizeType = st;
280  }
281 
287  void setKind(KIND k); // Changing the kind after allocation looses all data!
288 
293  return _parent;
294  }
295 
299  const Element* parent() const {
300  return _parent;
301  }
302 
306  bool setParent(Element* parent);
307 
315  void resize(size_t s);
316 
317 protected:
318  QString _name; // Name of the property
319  TYPE _fileType; // type of the elements on the file
320  TYPE _memType; // type of the elements in memory (INVALID_TYPE if the property should not be loaded)
321  TYPE _sizeType; // type used to hold the number of elements (if any)
322  KIND _kind; // Single value or list (later .. vector?)
323  Element* _parent; // Element containing the property (if any)
324  void* _content; // Content of the property, if allocated
325  size_t _size; // Number of elements
326  };
327 
333  struct Element {
346  Element(const QString& name, PlyFile* parent = 0);
347 
351  ~Element();
352 
356  void allocate();
357 
363  void clear();
364 
368  size_t nbProperties() const {
369  return _properties.size();
370  }
371 
375  size_t size() const {
376  return _nbElements;
377  }
378 
384  void resize(size_t n);
385 
389  QStringList properties() const;
390 
394  Property* property(size_t pos);
398  const Property* property(size_t pos) const;
402  Property* property(const QString& name);
406  const Property* property(const QString& name) const;
407 
415  Property* createValue(const QString& name, TYPE file, TYPE mem = INVALID_TYPE);
424  Property* createList(const QString& name, TYPE size, TYPE file, TYPE mem = INVALID_TYPE);
425 
429  bool error(const QString& str) const;
430 
434  bool hasProperty(const QString& name) const;
435 
439  bool attach(Property* prop);
440 
444  bool detach(Property* prop);
445 
449  Property* detach(const QString& name);
450 
454  const QString& name() const {
455  return _name;
456  }
457 
461  bool rename(const QString& n);
462 
466  bool allocated() const {
467  return _allocated;
468  }
469 
473  bool setParent(PlyFile* p);
478  return _parent;
479  }
483  const PlyFile* parent() const {
484  return _parent;
485  }
486 
491  void _rename_prop(Property* prop, const QString& new_name); // Don't call this yourself!
496  void _attach(Property* prop);
501  void _detach(Property* prop);
502 
503 protected:
504  QString _name;
505  size_t _nbElements;
506  QList<Property*> _properties;
507  QHash<QString, int> _property_map;
508  PlyFile* _parent;
509  bool _allocated;
510  };
511 
515  PlyFile();
516 
520  void clear();
521 
526 
530  bool validate();
531 
535  void allocate();
536 
541  return _format;
542  }
547  {
548  if(f != UNSPECIFIED_FORMAT) {
549  _format = f;
550  return true;
551  }
552  return false;
553  }
554 
558  bool setVersion(QString version);
562  const QString& version() const {
563  return _version;
564  }
565 
569  Element* element(size_t idx);
573  const Element* element(size_t idx) const;
577  Element* element(const QString& name);
581  const Element* element(const QString& name) const;
585  Element* createElement(const QString& name, size_t nb_elements);
586 
591  return current_element;
592  }
596  bool hasElement(const QString& name) const;
597 
601  size_t nbElements() const {
602  return _elements.size();
603  }
604 
608  bool attach(Element* el);
609 
613  bool detach(Element* el);
614 
620  bool parseHeader(const QString& filename);
621 
628  bool parseContent(); // Parse the content of the file whose header has been parsed last
629 
634  bool error(const QString err) const;
635 
639  bool save(const QString& filename) const;
640 
646  qint64 contentPosition() const {
647  return _contentPosition;
648  }
649 
653  bool isValid() const {
654  return is_valid;
655  }
659  operator bool() const { return is_valid; }
660 
664  const QStringList& comments() const {
665  return _comments;
666  }
667 
673  void addComment(QString line);
674 
678  void clearComments() {
679  _comments.clear();
680  }
681 
686  void _attach(Element* el);
691  void _detach(Element* el);
692 
693 protected:
694  bool parseAsciiContent(QFile& f);
695  bool parseBinaryContent(QFile& f, bool little_endian);
696 
697  bool readFormat(const QStringList& fields);
698  bool readElement(const QStringList& fields);
699  bool readProperty(const QStringList& fields);
700 
701  bool writeHeader(QFile& f) const;
702  bool writeAsciiContent(QFile& f) const;
703  bool writeBinaryContent(QFile& f, bool little_endian) const;
704 
705  TYPE parseType(QString typeName) const;
706 
707  QList<Element*> _elements;
708  QHash<QString, int> _element_map;
709 
710  Element* current_element;
711 
712  QString filename;
713  int line_nb;
714 
715  FORMAT_TYPES _format;
716  QString _version;
717  QStringList _comments;
718  int _version_major, _version_minor;
719  bool is_valid;
720  qint64 _contentPosition;
721 };
722 } // namespace util
723 } // namespace mgx
724 #endif // PLYFILE_HPP
Class representing an element.
Definition: PlyFile.hpp:333
std::vector< T > * value()
Return a pointer to the values help by the property as long as the property is a value and the type i...
Definition: PlyFile.hpp:173
The content is written in ASCII.
Definition: PlyFile.hpp:102
Element * createElement(const QString &name, size_t nb_elements)
Create a element name with nb_elements items.
const Element * parent() const
Get the element containing the property, if any.
Definition: PlyFile.hpp:299
bool setVersion(QString version)
Set the format version, checking it&#39;s validity.
size_t nbElements() const
Return the number of elements in the file.
Definition: PlyFile.hpp:601
qint64 contentPosition() const
Get the position of the content in a file.
Definition: PlyFile.hpp:646
TYPE
Enumeration for the possible types of the properties.
Definition: PlyFile.hpp:69
TYPE fileType() const
File type of the property value.
Definition: PlyFile.hpp:222
16 bits unsigned integer
Definition: PlyFile.hpp:73
Element * currentElement()
Get the current element (i.e.
Definition: PlyFile.hpp:590
bool save(const QString &filename) const
Save the file to filename.
void setSizeType(TYPE st)
Change the file type of the size of the property.
Definition: PlyFile.hpp:278
Class representing a property in an element.
Definition: PlyFile.hpp:119
The content is written in binary with little endian representation of numbers.
Definition: PlyFile.hpp:103
bool hasElement(const QString &name) const
Check if the element name exists.
size_t size() const
Number of items in the element.
Definition: PlyFile.hpp:375
void setMemType(TYPE mt)
Change the memory type of the property.
const std::vector< T > * value() const
Return a pointer to the values help by the property as long as the property is a value and the type i...
Definition: PlyFile.hpp:198
void allocate(size_t size)
Allocate the memory for the property, as long as memType is not INVALID_TYPE.
const QString & name() const
Name of the element.
Definition: PlyFile.hpp:454
bool attach(Element *el)
Attach an element to the file.
Element * element(size_t idx)
Access an element by index.
64 bits floating point
Definition: PlyFile.hpp:77
Property(const QString &name, Element *el=0)
Constructor of a property.
size_t size() const
Size of the property, that is the number of elements stored in it.
Definition: PlyFile.hpp:250
PlyFile * parent()
Get the parent of the element.
Definition: PlyFile.hpp:477
bool init(FORMAT_TYPES format=BINARY_LITTLE_ENDIAN, const QString &version="1.0")
Initialize the file with a format and a version.
const PlyFile * parent() const
Get the parent of the element.
Definition: PlyFile.hpp:483
static const unsigned int typeSizes[NB_TYPES]
Array holding the size in byte of the various types.
Definition: PlyFile.hpp:91
bool parseHeader(const QString &filename)
Parse the head of a PLY file.
FORMAT_TYPES
Enumeration of the possible file formats.
Definition: PlyFile.hpp:100
bool validate()
Validate the content.
KIND kind() const
Kind of the property.
Definition: PlyFile.hpp:243
const QString & name() const
Name of the property.
Definition: PlyFile.hpp:215
const QStringList & comments() const
See the comments currently defined.
Definition: PlyFile.hpp:664
The property holds a variable number of values per element.
Definition: PlyFile.hpp:125
void clearComments()
Remove all comments.
Definition: PlyFile.hpp:678
8 bits signed integer
Definition: PlyFile.hpp:70
This namespace contains all the API of MorphoGraphX.
Definition: Assert.hpp:26
Number of types, also used to mark an invalid type.
Definition: PlyFile.hpp:78
16 bits signed integer
Definition: PlyFile.hpp:72
32 bits signed integer
Definition: PlyFile.hpp:74
TYPE sizeType() const
File type of the size of the property list.
Definition: PlyFile.hpp:236
TYPE memType() const
Memory type of the property value.
Definition: PlyFile.hpp:229
bool error(const QString &str) const
Print an error on the standard output and return false.
32 bits floating point
Definition: PlyFile.hpp:76
PlyFile()
Create a new file.
bool allocated() const
Return true if the element has been allocated.
Definition: PlyFile.hpp:466
8 bits unsigned integer
Definition: PlyFile.hpp:71
static char const *const formatNames[4]
Array of C-string representation of the formats.
Definition: PlyFile.hpp:110
void addComment(QString line)
Add a comment.
The content is written in binary with big endian representation of numbers.
Definition: PlyFile.hpp:104
~Property()
The destructor takes charge to detach the property from any element that contains it...
32 bits unsigned integer
Definition: PlyFile.hpp:75
const std::vector< std::vector< T > > * list() const
Return a pointer to the list help by the property as long as the property is a list and the type is c...
Definition: PlyFile.hpp:185
The format is not yet specified.
Definition: PlyFile.hpp:101
void resize(size_t s)
Resize the property.
Element * parent()
Get the element containing the property, if any.
Definition: PlyFile.hpp:292
FORMAT_TYPES format() const
Format of the file.
Definition: PlyFile.hpp:540
Class representing the content of a PLY file.
Definition: PlyFile.hpp:65
bool setParent(Element *parent)
Change the parent, only if the new parent doesn&#39;t already have a property with the same name...
const QString & version() const
Version fo the format.
Definition: PlyFile.hpp:562
void deallocate()
Free the memory occupied by the property.
static char const *const typeNames[NB_TYPES+1]
Array of C-string representation of the types.
Definition: PlyFile.hpp:95
void setKind(KIND k)
Change the kind of the property.
bool rename(const QString &n)
Change the name of the property, only if the new name doesn&#39;t conflict with one of the other properti...
bool isValid() const
Check if the last call to PlyFile::validate was successful of not.
Definition: PlyFile.hpp:653
bool detach(Element *el)
Detach an element from the file.
void setFileType(TYPE ft)
Change the file type of the property.
Definition: PlyFile.hpp:264
size_t nbProperties() const
Number of properties in the element.
Definition: PlyFile.hpp:368
std::vector< std::vector< T > > * list()
Return a pointer to the list help by the property as long as the property is a list and the type is c...
Definition: PlyFile.hpp:160
bool setFormat(FORMAT_TYPES f)
Set the file format, checking the validity of the argument.
Definition: PlyFile.hpp:546
KIND
Kind of a property.
Definition: PlyFile.hpp:123
bool parseContent()
Parse the content of the current file.
void clear()
Remove any element from the file, and reset version number and format.
The property holds a single value per element.
Definition: PlyFile.hpp:124