uvgVPCCenc 1.0.0
uvgVPCCenc is an open-source real-time V-PCC encoder library written in C++ from scratch.
Loading...
Searching...
No Matches
miniply.h
Go to the documentation of this file.
1/*
2MIT License
3
4Copyright (c) 2019 Vilya Harvey
5
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software, and to permit persons to whom the Software is
11furnished TODO(lf)so, subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22SOFTWARE.
23*/
24
25#ifndef MINIPLY_H
26#define MINIPLY_H
27
28#include <cstdarg>
29#include <cstdint>
30#include <cstdio>
31#include <string>
32#include <vector>
33
40
42namespace miniply {
43
44//
45// Constants
46//
47
48static constexpr uint32_t kInvalidIndex = 0xFFFFFFFFu;
49
50// Standard PLY element names
51extern const char* kPLYVertexElement; // "vertex"
52extern const char* kPLYFaceElement; // "face"
53
54//
55// PLY Parsing types
56//
57
58enum class PLYFileType {
59 ASCII,
60 Binary,
62};
63
64enum class PLYPropertyType {
65 Char,
66 UChar,
67 Short,
68 UShort,
69 Int,
70 UInt,
71 Float,
72 Double,
73
74 None,
75};
76
78 std::string name;
82 uint32_t offset = 0;
83 uint32_t stride = 0;
84
85 std::vector<uint8_t> listData;
86 std::vector<uint32_t> rowCount; // Entry `i` is the number of items (*not* the number of bytes) in row `i`.
87};
88
89struct PLYElement {
90 std::string name;
91 std::vector<PLYProperty> properties;
92 uint32_t count = 0;
93 bool fixedSize = true;
94 uint32_t rowStride = 0;
95
96 void calculate_offsets();
97
100 uint32_t find_property(const char* propName) const;
101
114 bool find_properties(uint32_t propIdxs[], uint32_t numIdxs, ...) const;
115
119 bool find_properties_va(uint32_t propIdxs[], uint32_t numIdxs, va_list names) const;
120
140 bool convert_list_to_fixed_size(uint32_t listPropIdx, uint32_t listSize, uint32_t newPropIdxs[]);
141};
142
144 public:
145 PLYReader(const char* filename);
146 ~PLYReader();
147
148 bool valid() const;
149 bool has_element() const;
150 const PLYElement* element() const;
151 bool load_element();
152 void next_element();
153
154 PLYFileType file_type() const;
155 int version_major() const;
156 int version_minor() const;
157 uint32_t num_elements() const;
158 uint32_t find_element(const char* name) const;
159 PLYElement* get_element(uint32_t idx);
160
162 bool element_is(const char* name) const;
163
165 uint32_t num_rows() const;
166
169 uint32_t find_property(const char* name) const;
170
172 bool find_properties(uint32_t propIdxs[], uint32_t numIdxs, ...) const;
173
194 bool extract_properties(const uint32_t propIdxs[], uint32_t numProps, PLYPropertyType destType, void* dest) const;
195
209 bool extract_properties_with_stride(const uint32_t propIdxs[], uint32_t numProps, PLYPropertyType destType, void* dest,
210 uint32_t destStride) const;
211
214 const uint32_t* get_list_counts(uint32_t propIdx) const;
215
220 uint32_t sum_of_list_counts(uint32_t propIdx) const;
221
222 const uint8_t* get_list_data(uint32_t propIdx) const;
223 bool extract_list_property(uint32_t propIdx, PLYPropertyType destType, void* dest) const;
224
225 uint32_t num_triangles(uint32_t propIdx) const;
226 bool requires_triangulation(uint32_t propIdx) const;
227 bool extract_triangles(uint32_t propIdx, const float pos[], uint32_t numVerts, PLYPropertyType destType, void* dest) const;
228
229 bool find_pos(uint32_t propIdxs[3]) const;
230 bool find_normal(uint32_t propIdxs[3]) const;
231 bool find_texcoord(uint32_t propIdxs[2]) const;
232 bool find_color(uint32_t propIdxs[3]) const;
233 bool find_indices(uint32_t propIdxs[1]) const;
234
235 private:
236 bool refill_buffer();
237 bool rewind_to_safe_char();
238 bool accept();
239 bool advance();
240 bool next_line();
241 bool match(const char* str);
242 bool which(const char* values[], uint32_t* index);
243 bool which_property_type(PLYPropertyType* type);
244 bool keyword(const char* kw);
245 bool identifier(char* dest, size_t destLen);
246
247 template <class T> // T must be a type compatible with uint32_t.
248 bool typed_which(const char* values[], T* index) {
249 return which(values, reinterpret_cast<uint32_t*>(index));
250 }
251
252 bool int_literal(int* value);
253 bool float_literal(float* value);
254 bool double_literal(double* value);
255
256 bool parse_elements();
257 bool parse_element();
258 bool parse_property(std::vector<PLYProperty>& properties);
259
260 bool load_fixed_size_element(PLYElement& elem);
261 bool load_variable_size_element(PLYElement& elem);
262
263 bool load_ascii_scalar_property(PLYProperty& prop, size_t& destIndex);
264 bool load_ascii_list_property(PLYProperty& prop);
265 bool load_binary_scalar_property(PLYProperty& prop, size_t& destIndex);
266 bool load_binary_list_property(PLYProperty& prop);
267 bool load_binary_scalar_property_big_endian(PLYProperty& prop, size_t& destIndex);
268 bool load_binary_list_property_big_endian(PLYProperty& prop);
269
270 bool ascii_value(PLYPropertyType propType, uint8_t value[8]);
271
272 private:
273 FILE* m_f = nullptr;
274 char* m_buf = nullptr;
275 const char* m_bufEnd = nullptr;
276 const char* m_pos = nullptr;
277 const char* m_end = nullptr;
278 bool m_inDataSection = false;
279 bool m_atEOF = false;
280 int64_t m_bufOffset = 0;
281
282 bool m_valid = false;
283
284 PLYFileType m_fileType = PLYFileType::ASCII;
285 int m_majorVersion = 0;
286 int m_minorVersion = 0;
287 std::vector<PLYElement> m_elements;
288
289 size_t m_currentElement = 0;
290 bool m_elementLoaded = false;
291 std::vector<uint8_t> m_elementData;
292
293 char* m_tmpBuf = nullptr;
294};
295
309uint32_t triangulate_polygon(uint32_t n, const float pos[], uint32_t numVerts, const int indices[], int dst[]);
310
311} // namespace miniply
312
313#endif // MINIPLY_H
Definition miniply.h:143
~PLYReader()
Definition miniply.cpp:591
uint32_t find_element(const char *name) const
Definition miniply.cpp:770
const PLYElement * element() const
Definition miniply.cpp:603
bool find_indices(uint32_t propIdxs[1]) const
Definition miniply.cpp:1192
bool find_color(uint32_t propIdxs[3]) const
Definition miniply.cpp:1188
bool extract_list_property(uint32_t propIdx, PLYPropertyType destType, void *dest) const
Definition miniply.cpp:1037
uint32_t find_property(const char *name) const
Definition miniply.cpp:786
uint32_t num_elements() const
Definition miniply.cpp:768
bool find_texcoord(uint32_t propIdxs[2]) const
Definition miniply.cpp:1183
bool load_element()
Definition miniply.cpp:608
uint32_t num_rows() const
Number of rows in the current element.
Definition miniply.cpp:784
void next_element()
Definition miniply.cpp:618
bool extract_properties(const uint32_t propIdxs[], uint32_t numProps, PLYPropertyType destType, void *dest) const
Definition miniply.cpp:799
bool element_is(const char *name) const
Check whether the current element has the given name.
Definition miniply.cpp:782
int version_major() const
Definition miniply.cpp:764
bool requires_triangulation(uint32_t propIdx) const
Definition miniply.cpp:1080
const uint32_t * get_list_counts(uint32_t propIdx) const
Definition miniply.cpp:1015
bool has_element() const
Definition miniply.cpp:601
uint32_t sum_of_list_counts(uint32_t propIdx) const
Definition miniply.cpp:1022
const uint8_t * get_list_data(uint32_t propIdx) const
Definition miniply.cpp:1030
PLYFileType file_type() const
Definition miniply.cpp:762
PLYElement * get_element(uint32_t idx)
Definition miniply.cpp:780
bool extract_triangles(uint32_t propIdx, const float pos[], uint32_t numVerts, PLYPropertyType destType, void *dest) const
Definition miniply.cpp:1095
int version_minor() const
Definition miniply.cpp:766
bool find_properties(uint32_t propIdxs[], uint32_t numIdxs,...) const
Equivalent to calling find_properties on the current element.
Definition miniply.cpp:788
bool extract_properties_with_stride(const uint32_t propIdxs[], uint32_t numProps, PLYPropertyType destType, void *dest, uint32_t destStride) const
Definition miniply.cpp:904
uint32_t num_triangles(uint32_t propIdx) const
Definition miniply.cpp:1064
bool find_normal(uint32_t propIdxs[3]) const
Definition miniply.cpp:1181
bool valid() const
Definition miniply.cpp:599
bool find_pos(uint32_t propIdxs[3]) const
Definition miniply.cpp:1179
The example application for uvgVPCCenc uses the external library miniply to parse the ....
Definition miniply.cpp:40
const char * kPLYFaceElement
Definition miniply.cpp:48
PLYPropertyType
Definition miniply.h:64
@ None
Special value used in Element::listCountType to indicate a non-list property.
uint32_t triangulate_polygon(uint32_t n, const float pos[], uint32_t numVerts, const int indices[], int dst[])
Definition miniply.cpp:1755
const char * kPLYVertexElement
Definition miniply.cpp:47
PLYFileType
Definition miniply.h:58
Definition miniply.h:89
bool find_properties_va(uint32_t propIdxs[], uint32_t numIdxs, va_list names) const
Definition miniply.cpp:483
bool fixedSize
true if there are only fixed-size properties in this element, i.e. no list properties.
Definition miniply.h:93
void calculate_offsets()
Definition miniply.cpp:444
uint32_t rowStride
The number of bytes from the start of one row to the start of the next, for this element.
Definition miniply.h:94
uint32_t find_property(const char *propName) const
Definition miniply.cpp:466
std::vector< PLYProperty > properties
Definition miniply.h:91
bool find_properties(uint32_t propIdxs[], uint32_t numIdxs,...) const
Definition miniply.cpp:475
std::string name
Name of this element.
Definition miniply.h:90
uint32_t count
The number of items in this element (e.g. the number of vertices if this is the vertex element).
Definition miniply.h:92
bool convert_list_to_fixed_size(uint32_t listPropIdx, uint32_t listSize, uint32_t newPropIdxs[])
Definition miniply.cpp:493
Definition miniply.h:77
uint32_t stride
Definition miniply.h:83
uint32_t offset
Byte offset from the start of the row.
Definition miniply.h:82
PLYPropertyType type
Type of the data. Must be set to a value other than None.
Definition miniply.h:79
std::string name
Definition miniply.h:78
PLYPropertyType countType
None indicates this is not a list type, otherwise it's the type for the list count.
Definition miniply.h:80
std::vector< uint32_t > rowCount
Definition miniply.h:86
std::vector< uint8_t > listData
Definition miniply.h:85