19 #include <type_traits> 57 explicit PlyProperty(
const std::string& name) : _name(name) {}
60 : _name(name), _is_list(is_list)
76 const std::string&
name()
const 92 virtual std::string type_str()
const = 0;
98 std::ifstream& stream,
105 std::ofstream& stream,
110 bool _is_list =
false;
119 using value_type = double;
134 std::ifstream& stream,
138 std::ofstream& stream,
141 value_type get_ascii(std::ifstream& stream)
const;
143 value_type get_binary(std::ifstream& stream,
144 bool file_little_endian,
145 bool sys_little_endian)
const;
147 void put_ascii(std::ofstream& stream, value_type value)
const;
149 void put_binary(std::ofstream& stream,
151 bool file_little_endian,
152 bool sys_little_endian)
const;
161 using value_type = float;
176 std::ifstream& stream,
180 std::ofstream& stream,
183 value_type get_ascii(std::ifstream& stream)
const;
185 value_type get_binary(std::ifstream& stream,
186 bool file_little_endian,
187 bool sys_little_endian)
const;
189 void put_ascii(std::ofstream& stream, value_type value)
const;
191 void put_binary(std::ofstream& stream,
193 bool file_little_endian,
194 bool sys_little_endian)
const;
203 using value_type = int;
218 std::ifstream& stream,
222 std::ofstream& stream,
225 value_type get_ascii(std::ifstream& stream)
const;
227 value_type get_binary(std::ifstream& stream,
228 bool file_little_endian,
229 bool sys_little_endian)
const;
231 void put_ascii(std::ofstream& stream, value_type value)
const;
233 void put_binary(std::ofstream& stream,
235 bool file_little_endian,
236 bool sys_little_endian)
const;
245 using value_type = unsigned;
260 std::ifstream& stream,
264 std::ofstream& stream,
267 value_type get_ascii(std::ifstream& stream)
const;
269 value_type get_binary(std::ifstream& stream,
270 bool file_little_endian,
271 bool sys_little_endian)
const;
273 void put_ascii(std::ofstream& stream, value_type value)
const;
275 void put_binary(std::ofstream& stream,
277 bool file_little_endian,
278 bool sys_little_endian)
const;
287 using value_type = int;
302 std::ifstream& stream,
306 std::ofstream& stream,
309 value_type get_ascii(std::ifstream& stream)
const;
311 value_type get_binary(std::ifstream& stream,
312 bool file_little_endian,
313 bool sys_little_endian)
const;
315 void put_ascii(std::ofstream& stream, value_type value)
const;
317 void put_binary(std::ofstream& stream,
319 bool file_little_endian,
320 bool sys_little_endian)
const;
329 using value_type = unsigned;
344 std::ifstream& stream,
348 std::ofstream& stream,
351 value_type get_ascii(std::ifstream& stream)
const;
353 value_type get_binary(std::ifstream& stream,
354 bool file_little_endian,
355 bool sys_little_endian)
const;
357 void put_ascii(std::ofstream& stream, value_type value)
const;
359 void put_binary(std::ofstream& stream,
361 bool file_little_endian,
362 bool sys_little_endian)
const;
371 using value_type = int;
386 std::ifstream& stream,
390 std::ofstream& stream,
393 value_type get_ascii(std::ifstream& stream)
const;
395 value_type get_binary(std::ifstream& stream,
396 bool file_little_endian,
397 bool sys_little_endian)
const;
399 void put_ascii(std::ofstream& stream, value_type value)
const;
401 void put_binary(std::ofstream& stream,
403 bool file_little_endian,
404 bool sys_little_endian)
const;
413 using value_type = unsigned;
428 std::ifstream& stream,
432 std::ofstream& stream,
435 value_type get_ascii(std::ifstream& stream)
const;
437 value_type get_binary(std::ifstream& stream,
438 bool file_little_endian,
439 bool sys_little_endian)
const;
441 void put_ascii(std::ofstream& stream, value_type value)
const;
443 void put_binary(std::ofstream& stream,
445 bool file_little_endian,
446 bool sys_little_endian)
const;
464 using iterator_category = std::forward_iterator_tag;
468 using difference_type = int;
469 using unique_iter = std::vector<std::unique_ptr<PlyProperty>>
::iterator;
471 explicit iterator(unique_iter iter) : _iter(iter) {}
491 bool operator!=(
const iterator& rhs)
493 return _iter != rhs._iter;
506 using iterator_category = std::forward_iterator_tag;
510 using difference_type = int;
536 return _iter != rhs._iter;
547 : _name(name), _count(count)
560 std::copy(std::make_move_iterator(rhs._properties.begin()),
561 std::make_move_iterator(rhs._properties.end()),
562 std::back_inserter(_properties));
569 std::copy(std::make_move_iterator(rhs._properties.begin()),
570 std::make_move_iterator(rhs._properties.end()),
571 std::back_inserter(_properties));
578 const std::string&
name()
const 596 _properties.push_back(std::move(property));
604 return _properties[i].get();
612 return _properties[i].get();
620 return _properties.size();
628 return iterator(_properties.begin());
658 std::vector<std::unique_ptr<PlyProperty>> _properties;
718 _elements.push_back(std::move(element));
726 return _elements.size();
732 decltype(
auto) begin()
734 return _elements.cbegin();
740 decltype(
auto) begin()
const 742 return _elements.begin();
750 return _elements.end();
756 decltype(
auto) end()
const 758 return _elements.cend();
763 std::vector<PlyElement> _elements;
839 bool _sys_little_endian;
926 bool _sys_little_endian;
953 template<
int VN,
typename FloatType,
typename IndexType,
typename ColorType>
963 std::vector<FloatType>* normals =
nullptr,
964 std::vector<FloatType>* texcoords =
nullptr,
965 std::vector<IndexType>* indices =
nullptr,
966 std::vector<ColorType>* colors =
nullptr)
967 : _positions(positions), _normals(normals), _texcoords(texcoords),
968 _indices(indices), _colors(colors)
971 void on_read(
const PlyHeader& header)
override;
974 std::ifstream& stream,
978 std::ifstream& stream,
982 std::ifstream& stream,
986 std::ifstream& stream,
990 std::ifstream& stream,
994 template<
typename TPlyProperty>
995 void _store_float(
const TPlyProperty* property,
996 std::ifstream& stream,
999 template<
typename TPlyProperty>
1000 void _store_color(
const TPlyProperty* property,
1001 std::ifstream& stream,
1004 template<
typename TPlyProperty>
1005 void _store_indices(
const TPlyProperty* property,
1006 std::ifstream& stream,
1010 std::vector<FloatType>& _positions;
1011 std::vector<FloatType>* _normals =
nullptr;
1012 std::vector<FloatType>* _texcoords =
nullptr;
1013 std::vector<IndexType>* _indices =
nullptr;
1014 std::vector<ColorType>* _colors =
nullptr;
1041 template<
int VN,
typename FloatType,
typename IndexType,
typename ColorType>
1051 const std::vector<FloatType>* normals =
nullptr,
1052 const std::vector<FloatType>* texcoords =
nullptr,
1053 const std::vector<IndexType>* indices =
nullptr,
1054 const std::vector<ColorType>* colors =
nullptr)
1055 : _positions(positions), _normals(normals), _texcoords(texcoords),
1056 _indices(indices), _colors(colors)
1058 auto n = positions.size() / 3;
1059 if (_normals !=
nullptr && _normals->size() != n * 3) {
1060 throw std::invalid_argument(
1061 "Size of normals not compatible with positions");
1063 if (_texcoords !=
nullptr && _texcoords->size() != n * 2) {
1064 throw std::invalid_argument(
1065 "Size of texcoords not compatible with positions");
1067 if (_colors !=
nullptr) {
1068 if (_colors->size() == n * 3) {
1071 else if (_colors->size() == n * 4) {
1075 throw std::invalid_argument(
1076 "Size of colors not compatible with positions");
1079 if (_indices !=
nullptr && _indices->size() % VN != 0) {
1080 throw std::invalid_argument(
1081 "Size of indices not compatible with VN");
1085 void on_write()
override;
1090 std::ofstream& stream,
1094 std::ofstream& stream,
1098 std::ofstream& stream,
1102 std::ofstream& stream,
1106 std::ofstream& stream,
1110 template<
typename TPlyProperty>
1111 void _write_float(
const TPlyProperty* property,
1112 std::ofstream& stream,
1115 template<
typename TPlyProperty>
1116 void _write_color(
const TPlyProperty* property,
1117 std::ofstream& stream,
1120 template<
typename TPlyProperty>
1121 void _write_indices(
const TPlyProperty* property,
1122 std::ofstream& stream,
1126 const std::vector<FloatType>& _positions;
1127 const std::vector<FloatType>* _normals =
nullptr;
1128 const std::vector<FloatType>* _texcoords =
nullptr;
1129 const std::vector<IndexType>* _indices =
nullptr;
1130 const std::vector<ColorType>* _colors =
nullptr;
1131 bool _has_alpha =
false;
1203 void read_ply(
const std::string& filename,
1204 std::vector<FloatType>& positions,
1205 std::vector<FloatType>* normals,
1206 std::vector<FloatType>* texcoords,
1207 std::vector<IndexType>* indices,
1208 std::vector<ColorType>* colors);
1217 void write_ply(
const std::string& filename,
1272 void write_ply(
const std::string& filename,
1273 const std::vector<FloatType>& positions,
1274 const std::vector<FloatType>* normals,
1275 const std::vector<FloatType>* texcoords,
1276 const std::vector<IndexType>* indices,
1277 const std::vector<ColorType>* colors,
1283 #include "src/PlyIO.cpp" CommonPlyWriter(const std::vector< FloatType > &positions, const std::vector< FloatType > *normals=nullptr, const std::vector< FloatType > *texcoords=nullptr, const std::vector< IndexType > *indices=nullptr, const std::vector< ColorType > *colors=nullptr)
Constructor.
Definition: PlyIO.h:1050
std::string type_str() const override
Return the string for this type in the header.
Definition: PlyIO.h:380
const_iterator end() const
Return the end const_iterator of the property vector.
Definition: PlyIO.h:650
A ply writer for a common set of properties.
Definition: PlyIO.h:1042
virtual void read(const PlyUcharProperty *, std::ifstream &, PlyFormat)
Read a PlyUcharProperty.
Definition: PlyIO.h:836
A short ply property.
Definition: PlyIO.h:284
virtual void write(const PlyUcharProperty *, std::ofstream &, PlyFormat)
Write a PlyUcharProperty.
Definition: PlyIO.h:923
A double ply property.
Definition: PlyIO.h:116
Iterate through the properties in this element.
Definition: PlyIO.h:503
std::string type_str() const override
Return the string for this type in the header.
Definition: PlyIO.h:338
void add_property(std::unique_ptr< PlyProperty > property)
Add a property to this element.
Definition: PlyIO.h:594
bool is_list() const
Return if this property is a list property.
Definition: PlyIO.h:84
Ply element.
Definition: PlyIO.h:455
unsigned count() const
Return the instance count of this element in file.
Definition: PlyIO.h:586
virtual void write(const PlyIntProperty *, std::ofstream &, PlyFormat)
Write a PlyIntProperty.
Definition: PlyIO.h:898
std::string type_str() const override
Return the string for this type in the header.
Definition: PlyIO.h:254
std::string type_str() const override
Return the string for this type in the header.
Definition: PlyIO.h:212
std::string type_str() const override
Return the string for this type in the header.
Definition: PlyIO.h:170
virtual void on_write()
On writing the ply file.
Definition: PlyIO.h:872
virtual void write(const PlyCharProperty *, std::ofstream &, PlyFormat)
Write a PlyCharProperty.
Definition: PlyIO.h:918
PlyProperty * property(size_t i)
Get the property by index.
Definition: PlyIO.h:602
virtual void read(const PlyUshortProperty *, std::ifstream &, PlyFormat)
Read a PlyUshortProperty.
Definition: PlyIO.h:826
std::string type_str() const override
Return the string for this type in the header.
Definition: PlyIO.h:296
const PlyProperty * property(size_t i) const
Get the property by index.
Definition: PlyIO.h:610
virtual void read(const PlyDoubleProperty *, std::ifstream &, PlyFormat)
Read a PlyDoubleProperty.
Definition: PlyIO.h:801
An abstrct class for ply reader.
Definition: PlyIO.h:782
virtual void write(const PlyFloatProperty *, std::ofstream &, PlyFormat)
Write a PlyFloatProperty.
Definition: PlyIO.h:893
An unsigned int ply property.
Definition: PlyIO.h:242
std::string type_str() const override
Return the string for this type in the header.
Definition: PlyIO.h:128
virtual void on_read(const PlyHeader &)
On reading the header.
Definition: PlyIO.h:796
virtual void read(const PlyIntProperty *, std::ifstream &, PlyFormat)
Read a PlyIntProperty.
Definition: PlyIO.h:811
virtual void write(const PlyUintProperty *, std::ofstream &, PlyFormat)
Write a PlyUintProperty.
Definition: PlyIO.h:903
virtual void read(const PlyShortProperty *, std::ifstream &, PlyFormat)
Read a PlyShortProperty.
Definition: PlyIO.h:821
iterator end()
Return the end iterator of the property vector.
Definition: PlyIO.h:642
iterator begin()
Return the begin iterator of the property vector.
Definition: PlyIO.h:626
size_t n_props() const
Return the size of the property list.
Definition: PlyIO.h:618
An unsigned short ply property.
Definition: PlyIO.h:326
PlyHeader read_ply_header(const std::string &filename)
Read ply header.
Definition: PlyIO.cpp:1179
const std::string & name() const
Return the name of this property.
Definition: PlyIO.h:76
virtual PlyHeader generate_header(PlyFormat format) const
Generate the ply header.
Definition: PlyIO.h:879
virtual void write(const PlyDoubleProperty *, std::ofstream &, PlyFormat)
Write a PlyDoubleProperty.
Definition: PlyIO.h:888
A char ply property.
Definition: PlyIO.h:368
little endian binary mode.
void write_ply(const std::string &filename, const std::vector< FloatType > &positions, const std::vector< FloatType > *normals, const std::vector< FloatType > *texcoords, const std::vector< IndexType > *indices, const std::vector< ColorType > *colors, PlyFormat format=PlyFormat::ascii)
Write ply file using CommonPlyWriter.
Iterate through the properties in this element.
Definition: PlyIO.h:461
An abstract ply writer.
Definition: PlyIO.h:859
A float ply property.
Definition: PlyIO.h:158
virtual void write(const PlyShortProperty *, std::ofstream &, PlyFormat)
Write a PlyShortProperty.
Definition: PlyIO.h:908
virtual void write(const PlyUshortProperty *, std::ofstream &, PlyFormat)
Write a PlyUshortProperty.
Definition: PlyIO.h:913
virtual void read(const PlyCharProperty *, std::ifstream &, PlyFormat)
Read a PlyCharProperty.
Definition: PlyIO.h:831
const std::string & name() const
Return the name of the element.
Definition: PlyIO.h:578
A ply reader for a common set of properties.
Definition: PlyIO.h:954
CommonPlyReader(std::vector< FloatType > &positions, std::vector< FloatType > *normals=nullptr, std::vector< FloatType > *texcoords=nullptr, std::vector< IndexType > *indices=nullptr, std::vector< ColorType > *colors=nullptr)
Constructor.
Definition: PlyIO.h:962
An unsigned char ply property.
Definition: PlyIO.h:410
std::string type_str() const override
Return the string for this type in the header.
Definition: PlyIO.h:422
An int ply property.
Definition: PlyIO.h:200
Base class for ply property.
Definition: PlyIO.h:54
PlyElement(const std::string &name, unsigned count)
Create a PlyElement with name and instance count.
Definition: PlyIO.h:546
const_iterator begin() const
Return the begin const_iterator of the property vector.
Definition: PlyIO.h:634
PlyFormat
Ply file format.
Definition: PlyIO.h:35
void read_ply(const std::string &filename, std::vector< FloatType > &positions, std::vector< FloatType > *normals, std::vector< FloatType > *texcoords, std::vector< IndexType > *indices, std::vector< ColorType > *colors)
Read ply file using CommonPlyReader.
virtual void read(const PlyUintProperty *, std::ifstream &, PlyFormat)
Read a PlyUintProperty.
Definition: PlyIO.h:816
virtual void read(const PlyFloatProperty *, std::ifstream &, PlyFormat)
Read a PlyFloatProperty.
Definition: PlyIO.h:806