Easy3D 2.5.3
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
43 namespace io {
44
49 template <typename VT>
50 class GenericProperty : public std::vector<VT> {
51 public:
52 explicit GenericProperty(const std::string &prop_name = "", const std::vector<VT> &values = std::vector<VT>())
53 : std::vector<VT>(values), name(prop_name) {}
54 std::string name;
55 };
56
63
66 struct Element {
67 explicit Element(const std::string &elem_name, std::size_t n_instances = 0) : name(elem_name),
68 num_instances(n_instances) {}
69
70 std::string name; // e.g., "vertex", "face", "edge"
71 std::size_t num_instances; // number of instances
72
73 std::vector<Vec3Property> vec3_properties; // for "point", "normal", "color", and vector fields
74 std::vector<Vec2Property> vec2_properties; // for "texcoord"
75 std::vector<FloatProperty> float_properties; // for scalar fields of float values
76 std::vector<IntProperty> int_properties; // for scalar fields of integer values
77 std::vector<FloatListProperty> float_list_properties; // for properties of a list of float values
78 std::vector<IntListProperty> int_list_properties; // for properties of a list of integer values
79
80 std::string property_statistics() const;
81 };
82
83
89 {
90 public:
91 PlyReader() = default;
92 ~PlyReader();
93
100 bool read(const std::string& file_name, std::vector<Element>& elements);
101
111 static std::size_t num_instances(const std::string& file_name, const std::string& element_name);
112
113 private:
114 // Collect all elements stored as general properties (in list_properties_ and value_properties_).
115 // Meanwhile, convert the "list" intermediate representation into the user requested format.
116 void collect_elements(std::vector<Element>& elements) const;
117
118 private:
119 // For simpler code, it is possible to save all data as properties of type PLY_LIST with value type double.
120 // This can allow us to use a single callback function to handle all the properties. However, the
121 // performance is not optimal. Thus, I process list properties and value properties separately.
122 struct PlyProperty {
123 int orig_value_type; // e.g., PLY_INT, PLY_FLOAT
124 };
125
126 struct ListProperty : PlyProperty, GenericProperty< std::vector<double> > {
127 ListProperty(const std::string &elem_name, const std::string &prop_name)
128 : GenericProperty<std::vector<double> >(prop_name), element_name(elem_name) {}
129 std::string element_name;
130 };
131
132 struct ValueProperty : PlyProperty, GenericProperty< double > {
133 ValueProperty(const std::string &elem_name, const std::string &prop_name)
134 : GenericProperty<double>(prop_name), element_name(elem_name) {}
135 std::string element_name;
136 };
137
138 std::vector< ListProperty* > list_properties_;
139 std::vector< ValueProperty* > value_properties_;
140 };
141
142
147 class PlyWriter {
148 public:
156 static bool write(
157 const std::string &file_name,
158 const std::vector<Element> &elements,
159 const std::string &comment = "",
160 bool binary = false
161 );
162
163 };
164
166 bool is_big_endian();
167
168 } // namespace io
169
170} // namespace easy3d
171
172#endif // EASY3D_FILEIO_PLY_READER_WRITER_H
173
Generic property.
Definition: ply_reader_writer.h:50
A general purpose PLY file reader.
Definition: ply_reader_writer.h:89
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. The typical use is to determine if a P...
A general purpose PLY file writer.
Definition: ply_reader_writer.h:147
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 file file_name.
bool is_big_endian()
returns endianness of the system.
Definition: ply_reader_writer.cpp:694
Definition: collider.cpp:182
Model element (e.g., faces, vertices, edges) with optional properties.
Definition: ply_reader_writer.h:66