19#include "fast_float.h"
50static const char *
drop_plus(
const char *p,
const char *end)
52 if (p < end && *p ==
'+') {
58static const char *
parse_int(
const char *p,
const char *end,
int fallback,
int &dst)
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)) {
74 else if (type_size == 4) {
77 else if (type_size == 8) {
87 else if (type_size == 4) {
90 else if (type_size == 8) {
103 "PLY data type normalization factor table mismatch");
119 for (
int i = 0, n =
int(
element.properties.size()); i != n; i++) {
121 if (prop.
name == property) {
132 const fast_float::from_chars_result res = fast_float::from_chars(p, end, dst);
133 if (res.ec != std::errc()) {
146 return "Could not read row of ASCII property";
149 const char *p = line.
begin();
150 const char *end = line.
end();
160 for (
int j = 0; j < count && p < end; j++) {
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();
187 val = *
reinterpret_cast<int8_t *
>(
const_cast<uint8_t *
>(r_ptr));
191 val = *
const_cast<uint8_t *
>(r_ptr);
232 return "Vertex/Edge element contains list properties, this is not supported";
237 return "Could not read row of binary property";
243 for (
int i = 0, n =
int(
element.properties.size()); i != n; i++) {
251 for (
int i = 0, n =
int(
element.properties.size()); i != n; i++) {
259 return "Unknown binary ply format for vertex element";
273 int3 normal_index = {
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;
285 return "Vertex positions are not present in the file";
291 bool is_standard =
ELEM(
292 prop.
name,
"x",
"y",
"z",
"nx",
"ny",
"nz",
"red",
"green",
"blue",
"alpha",
"s",
"t");
303 custom_attr_indices.
append(prop_idx);
305 data->vertex_custom_attr.append(attr);
319 float4 color_norm = {1, 1, 1, 1};
335 for (
int i = 0; i <
element.count; i++) {
337 const char *error =
nullptr;
344 if (error !=
nullptr) {
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);
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;
362 colors4.w = value_vec[alpha_index] / color_norm.w;
367 data->vertex_colors.append(colors4);
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);
382 uvmap.x = value_vec[uv_index.x];
383 uvmap.y = value_vec[uv_index.y];
384 data->uv_coordinates.append(uvmap);
389 float value = value_vec[custom_attr_indices[ci]];
390 data->vertex_custom_attr[ci].data[i] = value;
446 if (prop_index < 0) {
449 if (prop_index < 0 &&
element.properties.size() == 1) {
452 if (prop_index < 0) {
453 return "Face element does not contain vertex indices property";
457 return "Face element vertex indices property must be a list";
464 for (
int i = 0; i <
element.count; i++) {
468 const char *p = line.
data();
469 const char *end = p + line.
size();
473 for (
int j = 0; j < prop_index; j++) {
480 for (
int k = 0; k < count && p < end; ++k) {
489 if (count < 1 || count > 255) {
490 return "Invalid face size, must be between 1 and 255";
495 CLOG_WARN(&
LOG,
"PLY Importer: ignoring face %i (%i vertices)", i, count);
500 const int64_t face_start =
data->face_vertices.size();
501 bool is_complete =
true;
502 for (
int j = 0; j < count; j++) {
513 CLOG_WARN(&
LOG,
"PLY Importer: ignoring face %i (missing vertex indices)", i);
514 data->face_vertices.resize(face_start);
517 data->face_sizes.append(count);
523 for (
int i = 0; i <
element.count; i++) {
527 for (
int j = 0; j < prop_index; j++) {
531 return "Could not read face property";
538 return "Could not read face size";
540 if (count < 1 || count > 255) {
541 return "Invalid face size, must be between 1 and 255";
545 return "Could not read face vertex indices";
550 CLOG_WARN(&
LOG,
"PLY Importer: ignoring face %i (%u vertices)", i, count);
553 ptr = scratch.
data();
557 for (
int j = 0; j < count; ++j) {
561 data->face_sizes.append(count);
565 for (
int j = prop_index + 1; j <
element.properties.size(); j++) {
569 return "Could not read face property";
583 return "Tristrips element should contain one row";
585 if (
element.properties.size() != 1) {
586 return "Tristrips element should contain one property";
590 return "Tristrips element property must be a list";
598 const char *p = line.
data();
599 const char *end = p + line.
size();
603 return "Invalid tristrips size";
606 for (
int j = 0; j < count; j++) {
609 return "Could not read tristrips";
615 return "Could not read tristrips";
628 return "Could not read tristrips size";
631 return "Invalid tristrips size";
634 return "Could not read tristrips";
638 ptr = scratch.
data();
642 for (
int j = 0; j < count; ++j) {
651 for (
size_t i = 0; i < strip.
size(); i++) {
652 if (strip[i] == -1) {
656 else if (i - start >= 2) {
657 int a = strip[i - 2],
b = strip[i - 1], c = strip[i];
659 if ((i - start) & 1) {
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);
681 if (prop_vertex1 < 0 || prop_vertex2 < 0) {
682 return "Edge element does not contain vertex1 and vertex2 properties";
696 for (
int i = 0; i <
element.count; i++) {
697 const char *error =
nullptr;
704 if (error !=
nullptr) {
709 data->edges.append(std::make_pair(index1, index2));
719 for (
int i = 0; i <
element.count; i++) {
722 return "Could not read element";
728 for (
int i = 0; i <
element.count; i++) {
731 return "Could not read element";
741 std::unique_ptr<PlyData>
data = std::make_unique<PlyData>();
743 bool got_vertex =
false, got_face =
false, got_tristrips =
false, got_edge =
false;
748 std::any_of(
element.properties.begin(),
751 return prop.type == PlyDataTypes::NONE;
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";
759 const char *error =
nullptr;
760 if (!is_known_element) {
763 if (has_unknown_type) {
764 data->warning =
"Unknown property type in element \"" +
element.name +
765 "\", skipping remaining elements";
769 data->warning =
"Could not read element \"" +
element.name +
770 "\", skipping remaining elements";
774 else if (
element.name ==
"vertex") {
777 data->error =
"Multiple vertex elements are not supported";
783 else if (
element.name ==
"face") {
787 else if (
element.name ==
"tristrips") {
789 got_tristrips =
true;
791 else if (
element.name ==
"edge") {
795 if (error !=
nullptr) {
799 if (got_vertex && got_face && got_tristrips && got_edge) {
#define CLOG_WARN(clg_ref,...)
#define BLI_assert_msg(a, msg)
constexpr const T * data() const
constexpr int64_t size() const
constexpr const T * end() const
constexpr const T * begin() const
constexpr bool is_empty() 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)
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)
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
static void endian_switch_array(uint8_t *ptr, int type_size, int size)
static const char * drop_plus(const char *p, const char *end)
VecBase< float, 4 > float4
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
VecBase< int32_t, 3 > int3
BMesh const char void * data
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)
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
static void endian_switch(uint8_t *ptr, int type_size)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
Vector< PlyProperty > properties