Easy3D 2.6.1
Loading...
Searching...
No Matches
ply_reader_writer.h
1/********************************************************************
2 * Copyright (C) 2015 Liangliang Nan <liangliang.nan@gmail.com>
3 * https://3d.bk.tudelft.nl/liangliang/
4 *
5 * This file is part of Easy3D. If it is useful in your research/work,
6 * I would be grateful if you show your appreciation by citing it:
7 * ------------------------------------------------------------------
8 * Liangliang Nan.
9 * Easy3D: a lightweight, easy-to-use, and efficient C++ library
10 * for processing and rendering 3D data.
11 * Journal of Open Source Software, 6(64), 3255, 2021.
12 * ------------------------------------------------------------------
13 *
14 * Easy3D is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License Version 3
16 * as published by the Free Software Foundation.
17 *
18 * Easy3D is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ********************************************************************/
26
27#ifndef EASY3D_FILEIO_PLY_READER_WRITER_H
28#define EASY3D_FILEIO_PLY_READER_WRITER_H
29
30
31#include <string>
32#include <vector>
33
34#include <easy3d/core/types.h>
35
36// Todo: check happly, a header-only implementation of the .ply file format.
37// https://github.com/nmwsharp/happly
38
39namespace easy3d {
40
45 namespace io {
46
53 template <typename VT>
54 class GenericProperty : public std::vector<VT> {
55 public:
61 explicit GenericProperty(const std::string &prop_name = "", const std::vector<VT> &values = std::vector<VT>())
62 : std::vector<VT>(values), name(prop_name) {}
63 std::string name;
64 };
65
72
77 struct Element {
83 explicit Element(const std::string &elem_name, std::size_t n_instances = 0) : name(elem_name),
84 num_instances(n_instances) {}
85
86 std::string name;
87 std::size_t num_instances;
88
89 std::vector<Vec3Property> vec3_properties;
90 std::vector<Vec2Property> vec2_properties;
91 std::vector<FloatProperty> float_properties;
92 std::vector<IntProperty> int_properties;
93 std::vector<FloatListProperty> float_list_properties;
94 std::vector<IntListProperty> int_list_properties;
95
100 std::string property_statistics() const;
101 };
102
103
110 class PlyReader
111 {
112 public:
113 PlyReader() = default;
114 ~PlyReader();
115
122 bool read(const std::string& file_name, std::vector<Element>& elements);
132 static std::size_t num_instances(const std::string& file_name, const std::string& element_name);
133
134 private:
135 // Collect all elements stored as general properties (in list_properties_ and value_properties_).
136 // Meanwhile, convert the "list" intermediate representation into the user requested format.
137 void collect_elements(std::vector<Element>& elements) const;
138
139 private:
140 // For simpler code, it is possible to save all data as properties of type PLY_LIST with value type double.
141 // This can allow us to use a single callback function to handle all the properties. However, the
142 // performance is not optimal. Thus, I process list properties and value properties separately.
143 struct PlyProperty {
144 int orig_value_type; // e.g., PLY_INT, PLY_FLOAT
145 };
146
147 struct ListProperty : PlyProperty, GenericProperty< std::vector<double> > {
148 ListProperty(const std::string &elem_name, const std::string &prop_name)
149 : GenericProperty<std::vector<double> >(prop_name), element_name(elem_name) {}
150 std::string element_name;
151 };
152
153 struct ValueProperty : PlyProperty, GenericProperty<double> {
154 ValueProperty(const std::string &elem_name, const std::string &prop_name)
155 : GenericProperty<double>(prop_name), element_name(elem_name) {}
156 std::string element_name;
157 };
158
159 std::vector< ListProperty* > list_properties_;
160 std::vector< ValueProperty* > value_properties_;
161 };
162
163
170 class PlyWriter {
171 public:
180 static bool write(
181 const std::string &file_name,
182 const std::vector<Element> &elements,
183 const std::string &comment = "",
184 bool binary = false
185 );
186
187 };
188
193 bool is_big_endian();
194
195 } // namespace io
196
197} // namespace easy3d
198
199#endif // EASY3D_FILEIO_PLY_READER_WRITER_H
200
Generic property.
Definition ply_reader_writer.h:54
GenericProperty(const std::string &prop_name="", const std::vector< VT > &values=std::vector< VT >())
Constructor to initialize the property with a name and values.
Definition ply_reader_writer.h:61
std::string name
Definition ply_reader_writer.h:63
bool read(const std::string &file_name, std::vector< Element > &elements)
Reads a PLY file and stores the model as a set of elements.
static std::size_t num_instances(const std::string &file_name, const std::string &element_name)
A quick check of the number of instances of a type of element.
A general purpose PLY file writer.
Definition ply_reader_writer.h:170
static bool write(const std::string &file_name, const std::vector< Element > &elements, const std::string &comment="", bool binary=false)
Saves a model stored as a set of elements to a file.
File input/output functionalities.
Definition graph_io.h:60
GenericProperty< vec2 > Vec2Property
Property for 2D vectors.
Definition ply_reader_writer.h:67
GenericProperty< int > IntProperty
Property for scalar fields of integer values.
Definition ply_reader_writer.h:69
GenericProperty< float > FloatProperty
Property for scalar fields of float values.
Definition ply_reader_writer.h:68
GenericProperty< std::vector< float > > FloatListProperty
Property for properties of a list of float values.
Definition ply_reader_writer.h:70
GenericProperty< vec3 > Vec3Property
Property for 3D vectors.
Definition ply_reader_writer.h:66
GenericProperty< std::vector< int > > IntListProperty
Property for properties of a list of integer values.
Definition ply_reader_writer.h:71
bool is_big_endian()
Returns the endianness of the system.
Definition ply_reader_writer.cpp:695
Definition collider.cpp:182
std::vector< Vec2Property > vec2_properties
Properties for "texcoord".
Definition ply_reader_writer.h:90
std::vector< Vec3Property > vec3_properties
Properties for "point", "normal", "color", and vector fields.
Definition ply_reader_writer.h:89
std::vector< FloatListProperty > float_list_properties
Properties for a list of float values.
Definition ply_reader_writer.h:93
std::vector< FloatProperty > float_properties
Properties for scalar fields of float values.
Definition ply_reader_writer.h:91
std::vector< IntProperty > int_properties
Properties for scalar fields of integer values.
Definition ply_reader_writer.h:92
std::string name
The name of the element, e.g., "vertex", "face", "edge".
Definition ply_reader_writer.h:86
std::vector< IntListProperty > int_list_properties
Properties for a list of integer values.
Definition ply_reader_writer.h:94
Element(const std::string &elem_name, std::size_t n_instances=0)
Constructor to initialize the element with a name and number of instances.
Definition ply_reader_writer.h:83
std::string property_statistics() const
Returns a string representation of the property statistics.
std::size_t num_instances
The number of instances of the element.
Definition ply_reader_writer.h:87