Blender V5.3
ply_import_data.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "ply_import_data.hh"
10#include "ply_data.hh"
11#include "ply_import_buffer.hh"
12
14#include "BKE_attribute.hh"
15
16#include "BLI_endian_switch.hh"
17#include "BLI_string_ref.hh"
18
19#include "fast_float.h"
20
21#include <charconv>
22
23#include "CLG_log.h"
24
25namespace blender {
26
27static CLG_LogRef LOG = {"io.ply"};
28
29static bool is_whitespace(char c)
30{
31 return c <= ' ';
32}
33
34static const char *drop_whitespace(const char *p, const char *end)
35{
36 while (p < end && is_whitespace(*p)) {
37 ++p;
38 }
39 return p;
40}
41
42static const char *drop_non_whitespace(const char *p, const char *end)
43{
44 while (p < end && !is_whitespace(*p)) {
45 ++p;
46 }
47 return p;
48}
49
50static const char *drop_plus(const char *p, const char *end)
51{
52 if (p < end && *p == '+') {
53 ++p;
54 }
55 return p;
56}
57
58static const char *parse_int(const char *p, const char *end, int fallback, int &dst)
59{
60 p = drop_whitespace(p, end);
61 p = drop_plus(p, end);
62 std::from_chars_result res = std::from_chars(p, end, dst);
63 if (ELEM(res.ec, std::errc::invalid_argument, std::errc::result_out_of_range)) {
64 dst = fallback;
65 }
66 return res.ptr;
67}
68
69static void endian_switch(uint8_t *ptr, int type_size)
70{
71 if (type_size == 2) {
72 BLI_endian_switch_uint16(reinterpret_cast<uint16_t *>(ptr));
73 }
74 else if (type_size == 4) {
75 BLI_endian_switch_uint32(reinterpret_cast<uint32_t *>(ptr));
76 }
77 else if (type_size == 8) {
78 BLI_endian_switch_uint64(reinterpret_cast<uint64_t *>(ptr));
79 }
80}
81
82static void endian_switch_array(uint8_t *ptr, int type_size, int size)
83{
84 if (type_size == 2) {
85 BLI_endian_switch_uint16_array(reinterpret_cast<uint16_t *>(ptr), size);
86 }
87 else if (type_size == 4) {
88 BLI_endian_switch_uint32_array(reinterpret_cast<uint32_t *>(ptr), size);
89 }
90 else if (type_size == 8) {
91 BLI_endian_switch_uint64_array(reinterpret_cast<uint64_t *>(ptr), size);
92 }
93}
94
95namespace io::ply {
96
97static const int data_type_size[] = {0, 1, 1, 2, 2, 4, 4, 4, 8};
98static_assert(std::size(data_type_size) == PLY_TYPE_COUNT, "PLY data type size table mismatch");
99
100static const float data_type_normalizer[] = {
101 1.0f, 127.0f, 255.0f, 32767.0f, 65535.0f, float(INT_MAX), float(UINT_MAX), 1.0f, 1.0f};
102static_assert(std::size(data_type_normalizer) == PLY_TYPE_COUNT,
103 "PLY data type normalization factor table mismatch");
104
106{
107 stride = 0;
108 for (PlyProperty &p : properties) {
109 if (p.count_type != PlyDataTypes::NONE) {
110 stride = 0;
111 return;
112 }
113 stride += data_type_size[p.type];
114 }
115}
116
117static int get_index(const PlyElement &element, StringRef property)
118{
119 for (int i = 0, n = int(element.properties.size()); i != n; i++) {
120 const PlyProperty &prop = element.properties[i];
121 if (prop.name == property) {
122 return i;
123 }
124 }
125 return -1;
126}
127
128/* Parse float or double, with 0 as fallback for invalid values. */
129template<typename T> static const char *parse_ascii_value(const char *p, const char *end, T &dst)
130{
131 p = drop_plus(drop_whitespace(p, end), end);
132 const fast_float::from_chars_result res = fast_float::from_chars(p, end, dst);
133 if (res.ec != std::errc()) {
134 dst = T(0);
135 }
136 return res.ptr;
137}
138
139template<typename T>
140static const char *parse_row_ascii(PlyReadBuffer &file,
141 const PlyElement &element,
142 Vector<T> &r_values)
143{
144 Span<char> line = file.read_line();
145 if (line.is_empty()) {
146 return "Could not read row of ASCII property";
147 }
148
149 const char *p = line.begin();
150 const char *end = line.end();
151 for (const int64_t i : element.properties.index_range()) {
152 if (element.properties[i].count_type == PlyDataTypes::NONE) {
153 p = parse_ascii_value(p, end, r_values[i]);
154 continue;
155 }
156
157 /* Skip list values, which are not supported for vertices and edges. */
158 int count = 0;
159 p = parse_int(p, end, 0, count);
160 for (int j = 0; j < count && p < end; j++) {
161 p = drop_non_whitespace(drop_whitespace(p, end), end);
162 }
163 r_values[i] = T(0);
164 }
165 return nullptr;
166}
167
168/* Convert floating point to T. Values that don't fit an integer type or that are
169 * NaN become the maximum value, so they fail later count and index checks. */
170template<typename T, typename F> static T float_to_value(const F value)
171{
172 if constexpr (std::is_integral_v<T>) {
173 if (!(value >= F(std::numeric_limits<T>::min()) && value < F(std::numeric_limits<T>::max()))) {
174 return std::numeric_limits<T>::max();
175 }
176 }
177 return T(value);
178}
179
180template<typename T> static T get_binary_value(PlyDataTypes type, const uint8_t *&r_ptr)
181{
182 T val = 0;
183 switch (type) {
184 case NONE:
185 break;
186 case CHAR:
187 val = *reinterpret_cast<int8_t *>(const_cast<uint8_t *>(r_ptr));
188 r_ptr += 1;
189 break;
190 case UCHAR:
191 val = *const_cast<uint8_t *>(r_ptr);
192 r_ptr += 1;
193 break;
194 case SHORT:
195 val = *reinterpret_cast<int16_t *>(const_cast<uint8_t *>(r_ptr));
196 r_ptr += 2;
197 break;
198 case USHORT:
199 val = *reinterpret_cast<uint16_t *>(const_cast<uint8_t *>(r_ptr));
200 r_ptr += 2;
201 break;
202 case INT:
203 val = *reinterpret_cast<int32_t *>(const_cast<uint8_t *>(r_ptr));
204 r_ptr += 4;
205 break;
206 case UINT:
207 val = *reinterpret_cast<uint32_t *>(const_cast<uint8_t *>(r_ptr));
208 r_ptr += 4;
209 break;
210 case FLOAT:
211 val = float_to_value<T>(*reinterpret_cast<float *>(const_cast<uint8_t *>(r_ptr)));
212 r_ptr += 4;
213 break;
214 case DOUBLE:
215 val = float_to_value<T>(*reinterpret_cast<double *>(const_cast<uint8_t *>(r_ptr)));
216 r_ptr += 8;
217 break;
218 default:
219 BLI_assert_msg(false, "Unknown property type");
220 }
221 return val;
222}
223
224template<typename T>
225static const char *parse_row_binary(PlyReadBuffer &file,
226 const PlyHeader &header,
227 const PlyElement &element,
228 Vector<uint8_t> &r_scratch,
229 Vector<T> &r_values)
230{
231 if (element.stride == 0) {
232 return "Vertex/Edge element contains list properties, this is not supported";
233 }
234 BLI_assert(r_scratch.size() == element.stride);
235 BLI_assert(r_values.size() == element.properties.size());
236 if (!file.read_bytes(r_scratch.data(), r_scratch.size())) {
237 return "Could not read row of binary property";
238 }
239
240 const uint8_t *ptr = r_scratch.data();
241 if (header.type == PlyFormatType::BINARY_LE) {
242 /* Little endian: just read/convert the values. */
243 for (int i = 0, n = int(element.properties.size()); i != n; i++) {
244 const PlyProperty &prop = element.properties[i];
245 T val = get_binary_value<T>(prop.type, ptr);
246 r_values[i] = val;
247 }
248 }
249 else if (header.type == PlyFormatType::BINARY_BE) {
250 /* Big endian: read, switch endian, convert the values. */
251 for (int i = 0, n = int(element.properties.size()); i != n; i++) {
252 const PlyProperty &prop = element.properties[i];
253 endian_switch(const_cast<uint8_t *>(ptr), data_type_size[prop.type]);
254 T val = get_binary_value<T>(prop.type, ptr);
255 r_values[i] = val;
256 }
257 }
258 else {
259 return "Unknown binary ply format for vertex element";
260 }
261 return nullptr;
262}
263
264static const char *load_vertex_element(PlyReadBuffer &file,
265 const PlyHeader &header,
266 const PlyElement &element,
267 PlyData *data)
268{
269 /* Figure out vertex component indices. */
270 int3 vertex_index = {get_index(element, "x"), get_index(element, "y"), get_index(element, "z")};
271 int3 color_index = {
272 get_index(element, "red"), get_index(element, "green"), get_index(element, "blue")};
273 int3 normal_index = {
274 get_index(element, "nx"), get_index(element, "ny"), get_index(element, "nz")};
275 int2 uv_index = {get_index(element, "s"), get_index(element, "t")};
276 int alpha_index = get_index(element, "alpha");
277
278 bool has_vertex = vertex_index.x >= 0 && vertex_index.y >= 0 && vertex_index.z >= 0;
279 bool has_color = color_index.x >= 0 && color_index.y >= 0 && color_index.z >= 0;
280 bool has_normal = normal_index.x >= 0 && normal_index.y >= 0 && normal_index.z >= 0;
281 bool has_uv = uv_index.x >= 0 && uv_index.y >= 0;
282 bool has_alpha = alpha_index >= 0;
283
284 if (!has_vertex) {
285 return "Vertex positions are not present in the file";
286 }
287
288 Vector<int64_t> custom_attr_indices;
289 for (const int64_t prop_idx : element.properties.index_range()) {
290 const PlyProperty &prop = element.properties[prop_idx];
291 bool is_standard = ELEM(
292 prop.name, "x", "y", "z", "nx", "ny", "nz", "red", "green", "blue", "alpha", "s", "t");
293 if (is_standard || prop.count_type != PlyDataTypes::NONE) {
294 continue;
295 }
296 /* Skip names that are empty or reserved for internal attributes. */
297 if (prop.name.empty() || !bke::allow_procedural_attribute_access(prop.name) ||
299 {
300 continue;
301 }
302
303 custom_attr_indices.append(prop_idx);
304 PlyCustomAttribute attr(prop.name, element.count);
305 data->vertex_custom_attr.append(attr);
306 }
307
308 data->vertices.reserve(element.count);
309 if (has_color) {
310 data->vertex_colors.reserve(element.count);
311 }
312 if (has_normal) {
313 data->vertex_normals.reserve(element.count);
314 }
315 if (has_uv) {
316 data->uv_coordinates.reserve(element.count);
317 }
318
319 float4 color_norm = {1, 1, 1, 1};
320 if (has_color) {
321 color_norm.x = data_type_normalizer[element.properties[color_index.x].type];
322 color_norm.y = data_type_normalizer[element.properties[color_index.y].type];
323 color_norm.z = data_type_normalizer[element.properties[color_index.z].type];
324 }
325 if (has_alpha) {
326 color_norm.w = data_type_normalizer[element.properties[alpha_index].type];
327 }
328
329 Vector<float> value_vec(element.properties.size());
330 Vector<uint8_t> scratch;
331 if (header.type != PlyFormatType::ASCII) {
332 scratch.resize(element.stride);
333 }
334
335 for (int i = 0; i < element.count; i++) {
336
337 const char *error = nullptr;
338 if (header.type == PlyFormatType::ASCII) {
339 error = parse_row_ascii(file, element, value_vec);
340 }
341 else {
342 error = parse_row_binary(file, header, element, scratch, value_vec);
343 }
344 if (error != nullptr) {
345 return error;
346 }
347
348 /* Vertex coord */
349 float3 vertex3;
350 vertex3.x = value_vec[vertex_index.x];
351 vertex3.y = value_vec[vertex_index.y];
352 vertex3.z = value_vec[vertex_index.z];
353 data->vertices.append(vertex3);
354
355 /* Vertex color */
356 if (has_color) {
357 float4 colors4;
358 colors4.x = value_vec[color_index.x] / color_norm.x;
359 colors4.y = value_vec[color_index.y] / color_norm.y;
360 colors4.z = value_vec[color_index.z] / color_norm.z;
361 if (has_alpha) {
362 colors4.w = value_vec[alpha_index] / color_norm.w;
363 }
364 else {
365 colors4.w = 1.0f;
366 }
367 data->vertex_colors.append(colors4);
368 }
369
370 /* If normals */
371 if (has_normal) {
372 float3 normals3;
373 normals3.x = value_vec[normal_index.x];
374 normals3.y = value_vec[normal_index.y];
375 normals3.z = value_vec[normal_index.z];
376 data->vertex_normals.append(normals3);
377 }
378
379 /* If uv */
380 if (has_uv) {
381 float2 uvmap;
382 uvmap.x = value_vec[uv_index.x];
383 uvmap.y = value_vec[uv_index.y];
384 data->uv_coordinates.append(uvmap);
385 }
386
387 /* Custom attributes */
388 for (const int64_t ci : custom_attr_indices.index_range()) {
389 float value = value_vec[custom_attr_indices[ci]];
390 data->vertex_custom_attr[ci].data[i] = value;
391 }
392 }
393 return nullptr;
394}
395
397 const PlyProperty &prop,
398 Vector<uint8_t> &scratch,
399 bool big_endian,
400 uint32_t &r_count)
401{
402 scratch.resize(8);
403 if (!file.read_bytes(scratch.data(), data_type_size[prop.count_type])) {
404 return false;
405 }
406 const uint8_t *ptr = scratch.data();
407 if (big_endian) {
408 endian_switch(const_cast<uint8_t *>(ptr), data_type_size[prop.count_type]);
409 }
410 r_count = get_binary_value<uint32_t>(prop.count_type, ptr);
411 return true;
412}
413
414/* Read list values into scratch, returns false on unexpected end of file. */
416 const PlyProperty &prop,
417 const uint32_t count,
418 Vector<uint8_t> &scratch)
419{
420 scratch.resize(int64_t(count) * data_type_size[prop.type]);
421 return file.read_bytes(scratch.data(), scratch.size());
422}
423
424static bool skip_property(PlyReadBuffer &file,
425 const PlyProperty &prop,
426 Vector<uint8_t> &scratch,
427 bool big_endian)
428{
429 if (prop.count_type == PlyDataTypes::NONE) {
430 scratch.resize(8);
431 return file.read_bytes(scratch.data(), data_type_size[prop.type]);
432 }
433 uint32_t count;
434 if (!read_list_count(file, prop, scratch, big_endian, count)) {
435 return false;
436 }
437 return read_list_values(file, prop, count, scratch);
438}
439
440static const char *load_face_element(PlyReadBuffer &file,
441 const PlyHeader &header,
442 const PlyElement &element,
443 PlyData *data)
444{
445 int prop_index = get_index(element, "vertex_indices");
446 if (prop_index < 0) {
447 prop_index = get_index(element, "vertex_index");
448 }
449 if (prop_index < 0 && element.properties.size() == 1) {
450 prop_index = 0;
451 }
452 if (prop_index < 0) {
453 return "Face element does not contain vertex indices property";
454 }
455 const PlyProperty &prop = element.properties[prop_index];
456 if (prop.count_type == PlyDataTypes::NONE) {
457 return "Face element vertex indices property must be a list";
458 }
459
460 data->face_vertices.reserve(int64_t(element.count) * 3);
461 data->face_sizes.reserve(element.count);
462
463 if (header.type == PlyFormatType::ASCII) {
464 for (int i = 0; i < element.count; i++) {
465 /* Read line */
466 Span<char> line = file.read_line();
467
468 const char *p = line.data();
469 const char *end = p + line.size();
470 int count = 0;
471
472 /* Skip any properties before vertex indices. */
473 for (int j = 0; j < prop_index; j++) {
474 p = drop_whitespace(p, end);
475 if (element.properties[j].count_type == PlyDataTypes::NONE) {
476 p = drop_non_whitespace(p, end);
477 }
478 else {
479 p = parse_int(p, end, 0, count);
480 for (int k = 0; k < count && p < end; ++k) {
481 p = drop_whitespace(p, end);
482 p = drop_non_whitespace(p, end);
483 }
484 }
485 }
486
487 /* Parse vertex indices list. */
488 p = parse_int(p, end, 0, count);
489 if (count < 1 || count > 255) {
490 return "Invalid face size, must be between 1 and 255";
491 }
492 /* Previous python based importer was accepting faces with fewer
493 * than 3 vertices, and silently dropping them. */
494 if (count < 3) {
495 CLOG_WARN(&LOG, "PLY Importer: ignoring face %i (%i vertices)", i, count);
496 continue;
497 }
498
499 /* Skip faces with fewer indices than their size, instead of using index 0. */
500 const int64_t face_start = data->face_vertices.size();
501 bool is_complete = true;
502 for (int j = 0; j < count; j++) {
503 p = drop_whitespace(p, end);
504 if (p == end) {
505 is_complete = false;
506 break;
507 }
508 int index;
509 p = parse_int(p, end, 0, index);
510 data->face_vertices.append(index);
511 }
512 if (!is_complete) {
513 CLOG_WARN(&LOG, "PLY Importer: ignoring face %i (missing vertex indices)", i);
514 data->face_vertices.resize(face_start);
515 continue;
516 }
517 data->face_sizes.append(count);
518 }
519 }
520 else {
521 Vector<uint8_t> scratch(64);
522
523 for (int i = 0; i < element.count; i++) {
524 const uint8_t *ptr;
525
526 /* Skip any properties before vertex indices. */
527 for (int j = 0; j < prop_index; j++) {
528 if (!skip_property(
529 file, element.properties[j], scratch, header.type == PlyFormatType::BINARY_BE))
530 {
531 return "Could not read face property";
532 }
533 }
534
535 /* Read vertex indices list. */
536 uint32_t count;
537 if (!read_list_count(file, prop, scratch, header.type == PlyFormatType::BINARY_BE, count)) {
538 return "Could not read face size";
539 }
540 if (count < 1 || count > 255) {
541 return "Invalid face size, must be between 1 and 255";
542 }
543
544 if (!read_list_values(file, prop, count, scratch)) {
545 return "Could not read face vertex indices";
546 }
547 /* Previous python based importer was accepting faces with fewer
548 * than 3 vertices, and silently dropping them. */
549 if (count < 3) {
550 CLOG_WARN(&LOG, "PLY Importer: ignoring face %i (%u vertices)", i, count);
551 }
552 else {
553 ptr = scratch.data();
554 if (header.type == PlyFormatType::BINARY_BE) {
555 endian_switch_array(const_cast<uint8_t *>(ptr), data_type_size[prop.type], count);
556 }
557 for (int j = 0; j < count; ++j) {
559 data->face_vertices.append(index);
560 }
561 data->face_sizes.append(count);
562 }
563
564 /* Skip any properties after vertex indices. */
565 for (int j = prop_index + 1; j < element.properties.size(); j++) {
566 if (!skip_property(
567 file, element.properties[j], scratch, header.type == PlyFormatType::BINARY_BE))
568 {
569 return "Could not read face property";
570 }
571 }
572 }
573 }
574 return nullptr;
575}
576
577static const char *load_tristrips_element(PlyReadBuffer &file,
578 const PlyHeader &header,
579 const PlyElement &element,
580 PlyData *data)
581{
582 if (element.count != 1) {
583 return "Tristrips element should contain one row";
584 }
585 if (element.properties.size() != 1) {
586 return "Tristrips element should contain one property";
587 }
588 const PlyProperty &prop = element.properties[0];
589 if (prop.count_type == PlyDataTypes::NONE) {
590 return "Tristrips element property must be a list";
591 }
592
593 Vector<int> strip;
594
595 if (header.type == PlyFormatType::ASCII) {
596 Span<char> line = file.read_line();
597
598 const char *p = line.data();
599 const char *end = p + line.size();
600 int count = 0;
601 p = parse_int(p, end, 0, count);
602 if (count < 0) {
603 return "Invalid tristrips size";
604 }
605
606 for (int j = 0; j < count; j++) {
607 p = drop_whitespace(p, end);
608 if (p == end) {
609 return "Could not read tristrips";
610 }
611 int index;
612 const char *next = parse_int(p, end, 0, index);
613 if (next == p) {
614 /* Not a number, can't advance properly so stop. */
615 return "Could not read tristrips";
616 }
617 p = next;
618 strip.append(index);
619 }
620 }
621 else {
622 Vector<uint8_t> scratch(64);
623
624 const uint8_t *ptr;
625
626 uint32_t count;
627 if (!read_list_count(file, prop, scratch, header.type == PlyFormatType::BINARY_BE, count)) {
628 return "Could not read tristrips size";
629 }
630 if (count > INT_MAX) {
631 return "Invalid tristrips size";
632 }
633 if (!read_list_values(file, prop, count, scratch)) {
634 return "Could not read tristrips";
635 }
636
637 strip.resize(count);
638 ptr = scratch.data();
639 if (header.type == PlyFormatType::BINARY_BE) {
640 endian_switch_array(const_cast<uint8_t *>(ptr), data_type_size[prop.type], count);
641 }
642 for (int j = 0; j < count; ++j) {
643 int index = get_binary_value<int>(prop.type, ptr);
644 strip[j] = index;
645 }
646 }
647
648 /* Decode triangle strip (with possible -1 restart indices) into faces. */
649 size_t start = 0;
650
651 for (size_t i = 0; i < strip.size(); i++) {
652 if (strip[i] == -1) {
653 /* Restart strip. */
654 start = i + 1;
655 }
656 else if (i - start >= 2) {
657 int a = strip[i - 2], b = strip[i - 1], c = strip[i];
658 /* Flip odd triangles. */
659 if ((i - start) & 1) {
660 std::swap(a, b);
661 }
662 /* Add triangle if it's not degenerate. */
663 if (a != b && a != c && b != c) {
664 data->face_vertices.append(a);
665 data->face_vertices.append(b);
666 data->face_vertices.append(c);
667 data->face_sizes.append(3);
668 }
669 }
670 }
671 return nullptr;
672}
673
674static const char *load_edge_element(PlyReadBuffer &file,
675 const PlyHeader &header,
676 const PlyElement &element,
677 PlyData *data)
678{
679 int prop_vertex1 = get_index(element, "vertex1");
680 int prop_vertex2 = get_index(element, "vertex2");
681 if (prop_vertex1 < 0 || prop_vertex2 < 0) {
682 return "Edge element does not contain vertex1 and vertex2 properties";
683 }
684
685 data->edges.reserve(element.count);
686
687 /* Double to represent all int and uint indices exactly, float has only 24 bits.
688 * TODO: Ideally should parse as int directly, but would need to account for
689 * other edge data that may be float. */
690 Vector<double> value_vec(element.properties.size());
691 Vector<uint8_t> scratch;
692 if (header.type != PlyFormatType::ASCII) {
693 scratch.resize(element.stride);
694 }
695
696 for (int i = 0; i < element.count; i++) {
697 const char *error = nullptr;
698 if (header.type == PlyFormatType::ASCII) {
699 error = parse_row_ascii(file, element, value_vec);
700 }
701 else {
702 error = parse_row_binary(file, header, element, scratch, value_vec);
703 }
704 if (error != nullptr) {
705 return error;
706 }
707 const int index1 = float_to_value<int>(value_vec[prop_vertex1]);
708 const int index2 = float_to_value<int>(value_vec[prop_vertex2]);
709 data->edges.append(std::make_pair(index1, index2));
710 }
711 return nullptr;
712}
713
714static const char *skip_element(PlyReadBuffer &file,
715 const PlyHeader &header,
716 const PlyElement &element)
717{
718 if (header.type == PlyFormatType::ASCII) {
719 for (int i = 0; i < element.count; i++) {
720 /* Empty lines are valid rows for elements without properties. */
721 if (!file.try_read_line()) {
722 return "Could not read element";
723 }
724 }
725 }
726 else {
727 Vector<uint8_t> scratch(64);
728 for (int i = 0; i < element.count; i++) {
729 for (const PlyProperty &prop : element.properties) {
730 if (!skip_property(file, prop, scratch, header.type == PlyFormatType::BINARY_BE)) {
731 return "Could not read element";
732 }
733 }
734 }
735 }
736 return nullptr;
737}
738
739std::unique_ptr<PlyData> import_ply_data(PlyReadBuffer &file, PlyHeader &header)
740{
741 std::unique_ptr<PlyData> data = std::make_unique<PlyData>();
742
743 bool got_vertex = false, got_face = false, got_tristrips = false, got_edge = false;
744 for (const PlyElement &element : header.elements) {
745 /* Binary rows can't be read with unknown property types, as their size is unknown.
746 * Elements without rows read nothing, so they don't matter. */
747 const bool has_unknown_type = header.type != PlyFormatType::ASCII && element.count > 0 &&
748 std::any_of(element.properties.begin(),
749 element.properties.end(),
750 [](const PlyProperty &prop) {
751 return prop.type == PlyDataTypes::NONE;
752 });
753 const bool is_known_element = ELEM(element.name, "vertex", "face", "tristrips", "edge");
754 if (has_unknown_type && is_known_element) {
755 data->error = "Unknown property type";
756 return data;
757 }
758
759 const char *error = nullptr;
760 if (!is_known_element) {
761 /* Elements that are skipped don't affect the geometry. When they can't be
762 * read, stop reading and keep the elements read so far. */
763 if (has_unknown_type) {
764 data->warning = "Unknown property type in element \"" + element.name +
765 "\", skipping remaining elements";
766 break;
767 }
768 if (skip_element(file, header, element) != nullptr) {
769 data->warning = "Could not read element \"" + element.name +
770 "\", skipping remaining elements";
771 break;
772 }
773 }
774 else if (element.name == "vertex") {
775 if (got_vertex) {
776 /* Vertex arrays are sized per element, so multiple vertex elements would mismatch. */
777 data->error = "Multiple vertex elements are not supported";
778 return data;
779 }
780 error = load_vertex_element(file, header, element, data.get());
781 got_vertex = true;
782 }
783 else if (element.name == "face") {
784 error = load_face_element(file, header, element, data.get());
785 got_face = true;
786 }
787 else if (element.name == "tristrips") {
788 error = load_tristrips_element(file, header, element, data.get());
789 got_tristrips = true;
790 }
791 else if (element.name == "edge") {
792 error = load_edge_element(file, header, element, data.get());
793 got_edge = true;
794 }
795 if (error != nullptr) {
796 data->error = error;
797 return data;
798 }
799 if (got_vertex && got_face && got_tristrips && got_edge) {
800 /* We have parsed all the elements we'd need, skip the rest. */
801 break;
802 }
803 }
804
805 return data;
806}
807
808} // namespace io::ply
809} // namespace blender
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
#define ELEM(...)
#define BLI_assert_msg(a, msg)
constexpr const T * data() const
Definition BLI_span.hh:219
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr const T * end() const
Definition BLI_span.hh:228
constexpr const T * begin() const
Definition BLI_span.hh:224
constexpr bool is_empty() const
Definition BLI_span.hh:264
int64_t size() const
void append(const T &value)
IndexRange index_range() const
void resize(const int64_t new_size)
std::optional< Span< char > > try_read_line()
bool read_bytes(void *dst, size_t size)
#define INT_MAX
#define UINT_MAX
Definition hash_md5.cc:46
#define T
#define F
bool attribute_name_is_anonymous(const StringRef name)
bool allow_procedural_attribute_access(StringRef attribute_name)
static const char * load_face_element(PlyReadBuffer &file, const PlyHeader &header, const PlyElement &element, PlyData *data)
static const char * load_edge_element(PlyReadBuffer &file, const PlyHeader &header, const PlyElement &element, PlyData *data)
static const char * parse_row_binary(PlyReadBuffer &file, const PlyHeader &header, const PlyElement &element, Vector< uint8_t > &r_scratch, Vector< T > &r_values)
static const char * load_vertex_element(PlyReadBuffer &file, const PlyHeader &header, const PlyElement &element, PlyData *data)
static bool read_list_count(PlyReadBuffer &file, const PlyProperty &prop, Vector< uint8_t > &scratch, bool big_endian, uint32_t &r_count)
static bool skip_property(PlyReadBuffer &file, const PlyProperty &prop, Vector< uint8_t > &scratch, bool big_endian)
static const char * load_tristrips_element(PlyReadBuffer &file, const PlyHeader &header, const PlyElement &element, PlyData *data)
static int get_index(const PlyElement &element, StringRef property)
std::unique_ptr< PlyData > import_ply_data(PlyReadBuffer &file, PlyHeader &header)
static const int data_type_size[]
static const char * parse_row_ascii(PlyReadBuffer &file, const PlyElement &element, Vector< T > &r_values)
static const float data_type_normalizer[]
static bool read_list_values(PlyReadBuffer &file, const PlyProperty &prop, const uint32_t count, Vector< uint8_t > &scratch)
static const char * parse_ascii_value(const char *p, const char *end, T &dst)
static T get_binary_value(PlyDataTypes type, const uint8_t *&r_ptr)
static const char * skip_element(PlyReadBuffer &file, const PlyHeader &header, const PlyElement &element)
static T float_to_value(const F value)
const char * drop_non_whitespace(const char *p, const char *end)
const char * parse_int(const char *p, const char *end, int fallback, int &dst, bool skip_space)
const char * drop_whitespace(const char *p, const char *end)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static const char * drop_non_whitespace(const char *p, const char *end)
unsigned __int64 uint64_t
Definition BKE_text.h:91
static void endian_switch_array(uint8_t *ptr, int type_size, int size)
__int64 int64_t
Definition BKE_text.h:90
static const char * drop_plus(const char *p, const char *end)
VecBase< float, 4 > float4
unsigned short uint16_t
Definition BKE_text.h:80
unsigned int uint32_t
Definition BKE_text.h:81
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
VecBase< int32_t, 3 > int3
BMesh const char void * data
unsigned char uint8_t
Definition BKE_text.h:79
static const char * drop_whitespace(const char *p, const char *end)
static bool is_whitespace(char c)
BLI_INLINE void BLI_endian_switch_uint16(unsigned short *val) ATTR_NONNULL(1)
BLI_INLINE void BLI_endian_switch_uint32(unsigned int *val) ATTR_NONNULL(1)
BLI_INLINE void BLI_endian_switch_uint64(uint64_t *val) ATTR_NONNULL(1)
void BLI_endian_switch_uint16_array(unsigned short *val, int size) ATTR_NONNULL(1)
static const char * parse_int(const char *p, const char *end, int fallback, int &dst)
signed int int32_t
Definition BKE_text.h:78
static CLG_LogRef LOG
Definition logging.cpp:12
void BLI_endian_switch_uint32_array(unsigned int *val, int size) ATTR_NONNULL(1)
VecBase< float, 3 > float3
void BLI_endian_switch_uint64_array(uint64_t *val, int size) ATTR_NONNULL(1)
ATTR_WARN_UNUSED_RESULT const void * element
signed char int8_t
Definition BKE_text.h:76
signed short int16_t
Definition BKE_text.h:77
static void endian_switch(uint8_t *ptr, int type_size)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
Vector< PlyProperty > properties
Definition ply_data.hh:54
Vector< PlyElement > elements
Definition ply_data.hh:61