-
Notifications
You must be signed in to change notification settings - Fork 4
/
msh_ply.h
2724 lines (2459 loc) · 82.1 KB
/
msh_ply.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
==============================================================================
Licensing information can be found at the end of the file.
==============================================================================
MSH_PLY.H v1.01
A single header library for reading and writing ply files.
To use the library you simply add:
#define MSH_PLY_IMPLEMENTATION
#include "msh_ply.h"
==============================================================================
API DOCUMENTATION
Customization
-------------------
'msh_ply.h' performs dynamic memory allocation. You have an option to provide alternate memory
allocation functions, by defining following macros prior to inclusion of this file:
- MSH_PLY_MALLOC
- MSH_PLY_REALLOC
- MSH_PLY_FREE
You have an option to specify whether you need only encoding/decoding parts of the library.
#define MSH_PLY_ENCODER_ONLY - only pull in writing functionality
#define MSH_PLY_DECODER_ONLY - only pull in reading functionality
msh_ply_open
-------------------
msh_ply_t* msh_ply_open( const char* filename, const char* mode );
Creates a new ply file handle and returns pointer to it. 'filename' is the path to the ply file
and 'mode' describes in what mode file should be used:
'r' or 'rb' - read
'w' - write ASCII
'wb' - write binary(will write endianness based on your system)
Note that this does not perform any reading / writing.
msh_ply_add_descriptor
-------------------
int32_t msh_ply_add_descriptor( msh_ply_t *pf, msh_ply_desc_t *desc );
Adds a data descriptor 'desc' to ply file object 'pf'. Descriptors provide information that is
requested from ply file object. See example for more details. Returns 0 on success and error
code on failure.
msh_ply_read
-------------------
int32_t msh_ply_read( msh_ply_t* pf );
Performs reading of ply file described by 'pf'. Should be called after adding descriptors.
Returns 0 on success and error code on failure.
msh_ply_write
-------------------
int32_t msh_ply_write( msh_ply_t* pf );
Performs writing of ply file described by 'pf'. Should be called after adding descriptors.
Returns 0 on success and error code on failure.
msh_ply_parse_header
-------------------
int32_t msh_ply_parse_header( msh_ply_t* pf );
Parses just the header of the file. Needs to be called to programatically check what
contents file has, but will not read any of the actual data. Returns 0 on success and error
code on failure.
msh_ply_has_properties
-------------------
bool msh_ply_has_properties( const msh_ply_t* pf, const msh_ply_desc_t* desc );
Returns true if the ply file 'pf' contains properties contained in descriptor 'desc'. This is
the preffered method to check whether some property exist in ply file 'pf'.
Can only be called after header has been parsed!
msh_ply_find_element
-------------------
msh_ply_element_t* msh_ply_find_element( const msh_ply_t* pf, const char* element_name );
Returns a pointer to element if the element of given name has been found in 'pf'. Returns NULL
otherwise. Can only be called after header has been parsed!
msh_ply_find_property
-------------------
msh_ply_property_t* msh_ply_find_property( const msh_ply_element_t* el, const char* property_name );
Returns a pointer to property if the property of given name has been found in 'pf'.
Returns NULL otherwise. Can be called after header has been parsed!
msh_ply_close
-------------------
void msh_ply_close( msh_ply_t* pf );
Closes the ply file 'pf'.
msh_ply_error_msg
-------------------
const char* msh_ply_error_msg( int32_t err );
Get pointer to a error message string from the error code.
void msh_ply_print_header( msh_ply_t* pf );
-------------------
void msh_ply_print_header( msh_ply_t* pf );
Pretty print of the ply file header.
==============================================================================
EXAMPLES:
This library usage focuses around the concept of descriptors. User describes his/her/their
data with a set of descriptors. Descriptors tell us what element and properties is
the data describing, what is the data type, as well as storing pointer to actual data.
Once descriptors are prepared they can be added to each of ply files and written out.
System for reading is exactly the same, but you'd be describing requested data.
Note that ply file does not own pointers to the data, so you will need to delete these
separately. Additionally, every function returns an error code, which can be turned
into string message using "msh_ply_error_msg( ply_err_t err )" function. Not used
below for clarity.
------------------------------
Writing example:
typedef struct TriMesh
{
Vec3f* vertices;
Vec3i* faces;
int32_t n_vertices;
int32_t n_faces;
} TriMesh_t;
TriMesh_t mesh = {0};
your_function_to_initialize_mesh( &mesh );
msh_ply_desc_t descriptors[2];
descriptors[0] = { .element_name = "vertex",
.property_names = (const char*[]){"x", "y", "z"},
.num_properties = 3,
.data_type = MSH_PLY_FLOAT,
.data = &mesh->vertices,
.data_count = &mesh->n_vertices };
descriptors[1] = { .element_name = "face",
.property_names = (const char*[]){"vertex_indices"},
.num_properties = 1,
.data_type = MSH_PLY_INT32,
.list_type = MSH_PLY_UINT8,
.data = &mesh->faces,
.data_count = &mesh->n_faces,
.list_size_hint = 3 };
// Create new ply file
msh_ply_t* ply_file = msh_ply_open( filenames[i], "wb" );
// Add descriptors to ply file
msh_ply_add_descriptor( ply_file, &descriptors[0] );
msh_ply_add_descriptor( ply_file, &descriptors[1] );
// Write data to disk
msh_ply_write( ply_file );
// Close ply file
msh_ply_close( ply_file );
------------------------------
Reading example:
typedef struct TriMesh
{
Vec3f* vertices;
Vec3i* faces;
int32_t n_vertices;
int32_t n_faces;
} TriMesh_t;
TriMesh_t* meshes = NULL;
int32_t n_meshes = 10;
for (int32_t i = 0; i < n_meshes; ++i)
{
meshes[i].vertices = NULL;
meshes[i].faces = NULL;
meshes[i].n_faces = 0;
meshes[i].n_vertices = 0;
}
msh_ply_desc_t descriptors[2];
descriptors[0] = { .element_name = "vertex",
.property_names = (const char*[]){"x", "y", "z"},
.num_properties = 3,
.data_type = MSH_PLY_FLOAT };
descriptors[1] = { .element_name = "vertex",
.property_names = (const char*[]){"vertex_indices"},
.num_properties = 1,
.data_type = MSH_PLY_INT32,
.list_type = MSH_PLY_UINT8,
.list_size_hint = 3 };
for (int32_t i = 0; i < n_meshes; ++i)
{
// Create new ply file
msh_ply_t* ply_file = msh_ply_open( filenames[i], "rb" );
// Add data to descriptors
descriptors[0].data = &meshes[i].vertices;
descriptors[0].data_count = &meshes[i].n_vertices;
descriptors[1].data = &meshes[i].faces;
descriptors[1].data_count = &meshes[i].n_faces;
// Add descriptors to ply file
msh_ply_add_descriptor( ply_file, &descriptors[0] );
msh_ply_add_descriptor( ply_file, &descriptors[1] );
// Read the file
msh_ply_read( ply_file );
// Close ply file
msh_ply_close( ply_file );
}
==============================================================================
DEPENDENCIES
This file requires following c stdlib headers:
- assert.h
- stdlib.h
- stdint.h
- string.h
- stdio.h
- stdbool.h
Note that this file will not pull them in automatically to prevent pulling same
files multiple time. If you do not like this behaviour and want this file to
pull in c headers, simply define following before including the library:
#define MSH_PLY_INCLUDE_LIBC_HEADERS
==============================================================================
AUTHORS:
Maciej Halber
CREDITS:
Inspiration for single header ply reader: tinyply by ddiakopoulos
Dynamic array based on stb.h by Sean T. Barrett
==============================================================================
TODOs:
[ ] Support buffering -> read data in chunks, then serve it out, rather than continuously fread.
[ ] Add independence from c stdlib (like stdio etc.) -> Read from memory / Pass pointers to fopen?
[ ] Better ascii support - check Vilya Harvey's miniply
[ ] Getting raw data for the list property - Add different function.
[ ] Check for duplicates in user-provided descriptor set.
[ ] Writing optimization
[ ] Profile lucy writing
[ ] Error reporting
[ ] Revise when errors are reported
[ ] Just have a switch statement instead of static array
[ ] Write C++ support(?)
[ ] Add static function definition macro
==============================================================================
REFERENCES:
[1] stretchy_buffer https://github.com/nothings/stb/blob/master/stretchy_buffer.h
*/
#ifndef MSH_PLY
#define MSH_PLY
#if defined(MSH_PLY_INCLUDE_LIBC_HEADERS)
#include <assert.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include <stddef.h>
#include <sys/stat.h>
#endif
#ifndef MSH_PLY_MALLOC
#define MSH_PLY_MALLOC(x) malloc((x))
#endif
#ifndef MSH_PLY_REALLOC
#define MSH_PLY_REALLOC(x, y) realloc((x), (y))
#endif
#ifndef MSH_PLY_FREE
#define MSH_PLY_FREE(x) free((x))
#endif
#ifdef __cplusplus
extern "C" {
#endif
#if defined(_MSC_VER)
#define MSH_PLY_INLINE __forceinline
#else
#define MSH_PLY_INLINE __attribute__((always_inline, unused)) inline
#endif
#ifndef MSH_PLY_DEF
#ifdef MSH_PLY_STATIC
#define MSH_PLY_DEF static
#else
#define MSH_PLY_DEF extern
#endif
#endif
#ifndef MSH_PLY_PRIVATE
#define MSH_PLY_PRIVATE static
#endif
#define MSH_PLY_MAX(a, b) ((a) > (b) ? (a) : (b))
#define MSH_PLY_MAX_STR_LEN 1024
#define MSH_PLY_MAX_REQ_PROPERTIES 32
#define MSH_PLY_MAX_PROPERTIES 128
#define MSH_PLY_MAX_LIST_ELEMENTS 1024
typedef struct msh_ply_property msh_ply_property_t;
typedef struct msh_ply_element msh_ply_element_t;
typedef struct msh_ply_desc msh_ply_desc_t;
typedef struct msh_ply_file msh_ply_t;
typedef enum msh_ply_type_id
{
MSH_PLY_INVALID,
MSH_PLY_INT8,
MSH_PLY_UINT8,
MSH_PLY_INT16,
MSH_PLY_UINT16,
MSH_PLY_INT32,
MSH_PLY_UINT32,
MSH_PLY_FLOAT,
MSH_PLY_DOUBLE,
MSH_PLY_N_TYPES
} msh_ply_type_id_t;
typedef enum msh_ply_format
{
MSH_PLY_ASCII = 0,
MSH_PLY_LITTLE_ENDIAN,
MSH_PLY_BIG_ENDIAN
} msh_ply_format_t;
struct msh_ply_desc
{
char* element_name;
const char** property_names;
int16_t num_properties;
msh_ply_type_id_t data_type;
msh_ply_type_id_t list_type;
void* data;
void* list_data;
int32_t* data_count;
uint8_t list_size_hint;
};
MSH_PLY_DEF msh_ply_t* msh_ply_open(const char* filename, const char* mode);
MSH_PLY_DEF void msh_ply_close(msh_ply_t* pf);
MSH_PLY_DEF int32_t msh_ply_add_descriptor(msh_ply_t* pf, msh_ply_desc_t* desc);
MSH_PLY_DEF int32_t msh_ply_parse_header(msh_ply_t* pf);
MSH_PLY_DEF bool msh_ply_has_properties(const msh_ply_t* pf,
const msh_ply_desc_t* desc);
MSH_PLY_DEF msh_ply_element_t* msh_ply_find_element(const msh_ply_t* pf,
const char* element_name);
MSH_PLY_DEF msh_ply_property_t*
msh_ply_find_property(const msh_ply_element_t* el, const char* property_name);
MSH_PLY_DEF const char* msh_ply_error_msg(int32_t err);
MSH_PLY_DEF void msh_ply_print_header(msh_ply_t* pf);
MSH_PLY_DEF int32_t msh_ply_add_property_to_element(msh_ply_t* pf,
const msh_ply_desc_t* desc);
MSH_PLY_DEF int32_t msh_ply_get_property_from_element(msh_ply_t* pf,
msh_ply_desc_t* desc);
#ifndef MSH_PLY_ENCODER_ONLY
MSH_PLY_DEF int32_t msh_ply_read(msh_ply_t* pf);
#endif
#ifndef MSH_PLY_DECODER_ONLY
MSH_PLY_DEF int32_t msh_ply_write(msh_ply_t* pf);
#endif
#ifdef __cplusplus
}
#endif
#endif /* MSH_PLY */
////////////////////////////////////////////////////////////////////////////////
// IMPLEMENTATION
////////////////////////////////////////////////////////////////////////////////
#ifdef MSH_PLY_IMPLEMENTATION
////////////////////////////////////////////////////////////////////////////////
// THIS IS A SIMPLIFIED VERSION OF MSH_ARRAY
#ifdef __cplusplus
extern "C" {
#endif
typedef struct msh_ply_array_header
{
size_t len;
size_t cap;
} msh_ply_array_hdr_t;
#define msh_ply_array(T) T*
MSH_PLY_PRIVATE void* msh_ply__array_grow(const void* array, size_t new_len, size_t elem_size);
#define msh_ply_array__grow_formula(x) ((2 * (x) + 5))
#define msh_ply_array__hdr(a) \
((msh_ply_array_hdr_t*)((char*)(a) - sizeof(msh_ply_array_hdr_t)))
#define msh_ply_array_len(a) ((a) ? (msh_ply_array__hdr((a))->len) : 0)
#define msh_ply_array_cap(a) ((a) ? (msh_ply_array__hdr((a))->cap) : 0)
#define msh_ply_array_front(a) ((a) ? (a) : NULL)
#define msh_ply_array_back(a) \
(msh_ply_array_len((a)) ? ((a) + msh_ply_array_len((a)) - 1) : NULL)
#define msh_ply_array_free(a) \
((a) ? (MSH_PLY_FREE(msh_ply_array__hdr(a)), (a) = NULL) : 0)
#define msh_ply_array_fit(a, n) \
((n) <= msh_ply_array_cap(a) \
? (0) \
: (*(void**)&(a) = msh_ply__array_grow((a), (n), sizeof(*(a)))))
#define msh_ply_array_push(a, ...) \
(msh_ply_array_fit((a), 1 + msh_ply_array_len((a))), \
(a)[msh_ply_array__hdr(a)->len++] = (__VA_ARGS__))
#ifdef __cplusplus
}
#endif
struct msh_ply_property
{
int32_t list_count;
int16_t byte_size;
int32_t offset;
int32_t stride;
int16_t list_byte_size;
int32_t list_offset;
int32_t list_stride;
msh_ply_type_id_t type;
msh_ply_type_id_t list_type;
int32_t total_count;
size_t total_byte_size;
// Data storage
char name[32];
void* data;
void* list_data;
};
struct msh_ply_element
{
char name[64];
int32_t count;
msh_ply_array(msh_ply_property_t) properties;
long file_anchor;
void* data;
size_t data_size;
};
struct msh_ply_file
{
int32_t valid;
int32_t format;
int32_t format_version;
msh_ply_array(msh_ply_element_t) elements;
msh_ply_array(msh_ply_desc_t*) descriptors;
FILE* _fp;
int32_t _header_size;
int32_t _system_format;
int32_t _parsed;
};
enum msh_ply_err
{
MSH_PLY_NO_ERR = 0,
MSH_PLY_INVALID_FILE_ERR = 1,
MSH_PLY_INVALID_FORMAT_ERR = 2,
MSH_PLY_FILE_OPEN_ERR = 3,
MSH_PLY_FILE_NOT_OPEN_ERR = 4,
MSH_PLY_LINE_PARSE_ERR = 5,
MSH_PLY_FORMAT_CMD_ERR = 6,
MSH_PLY_ELEMENT_CMD_ERR = 7,
MSH_PLY_PROPERTY_CMD_ERR = 8,
MSH_PLY_ELEMENT_NOT_FOUND_ERR = 9,
MSH_PLY_PROPERTY_NOT_FOUND_ERR = 10,
MSH_PLY_BINARY_PARSE_ERR = 11,
MSH_PLY_CONFLICTING_NUMBER_OF_ELEMENTS_ERR = 12,
MSH_PLY_UNRECOGNIZED_CMD_ERR = 13,
MSH_PLY_NO_REQUESTS = 14,
MSH_PLY_NULL_DESCRIPTOR_ERR = 15,
MSH_PLY_NULL_ELEMENT_NAME_ERR = 16,
MSH_PLY_NULL_PROPERTY_NAMES_ERR = 17,
MSH_PLY_NULL_PROPERTY_NAME_ERR = 18,
MSH_PLY_NO_REQUESTED_PROPERTIES_ERR = 19,
MSH_PLY_NULL_DATA_PTR_ERR = 20,
MSH_PLY_NULL_DATA_COUNT_PTR_ERR = 21,
MSH_PLY_INVALID_DATA_TYPE_ERR = 22,
MSH_PLY_INVALID_LIST_TYPE_ERR = 23,
MSH_PLY_ASCII_FILE_READ_ERR = 24,
MSH_PLY_ASCII_FILE_EOF_ERR = 25,
MSH_PLY_READ_REQUIRED_PROPERTY_IS_MISSING = 26,
MSH_PLY_WRITE_REQUIRED_PROPERTY_IS_MISSING = 27,
MSH_PLY_NUM_OF_ERRORS
};
static const char* msh_ply_error_msgs[MSH_PLY_NUM_OF_ERRORS] = {
"MSH_PLY: No errors.",
"MSH_PLY: Invalid PLY file.",
"MSH_PLY: Invalid PLY file: Invalid format in ply file.",
"MSH_PLY: Could not open file.",
"MSH_PLY: File was not open. Please open file first.",
"MSH_PLY: Invalid PLY file: Could not parse line in a header.",
"MSH_PLY: Invalid PLY file: Format of ply command is invalid.",
"MSH_PLY: Invalid PLY file: Format of element command is invalid.",
"MSH_PLY: Invalid PLY file: Format of property command is invalid.",
"MSH_PLY: Could not find requested element in the input ply file.",
"MSH_PLY: Could not find requested property in the input ply file.",
"MSH_PLY: Error reading binary file.",
"MSH_PLY: Number of requested elements does not match the descriptor.",
"MSH_PLY: Invalid PLY file: Unrecognized command in a ply file.",
"MSH_PLY: There is no requests for data to be read or writen. Aborting "
"operation.",
"MSH_PLY: Invalid descriptor: Desciptor pointer is NULL.",
"MSH_PLY: Invalid descriptor: Element name pointer is NULL.",
"MSH_PLY: Invalid descriptor: Property names pointer is NULL.",
"MSH_PLY: Invalid descriptor: One of the property names is NULL.",
"MSH_PLY: Invalid descriptor: Number of requested properties is zero or less "
"than zero.",
"MSH_PLY: Invalid descriptor: Data pointer is NULL.",
"MSH_PLY: Invalid descriptor: Data Count pointer is NULL.",
"MSH_PLY: Invalid descriptor: Incorrect data type.",
"MSH_PLY: Invalid descriptor: Incorrect list type. List type cannot be float "
"or double.",
"MSH_PLY: Error reading ASCII PLY file.",
"MSH_PLY: Reached EOF when reading ASCII PLY file."
"MSH_PLY: When reading file, the required property not found in the input "
"file",
"MSH_PLY: When write file, the required property not found in the input file",
};
MSH_PLY_DEF const char*
msh_ply_error_msg(int32_t err)
{
return msh_ply_error_msgs[err];
}
MSH_PLY_PRIVATE void*
msh_ply__array_grow(const void* array, size_t new_len, size_t elem_size)
{
size_t old_cap = msh_ply_array_cap(array);
size_t new_cap = (size_t)msh_ply_array__grow_formula(old_cap);
new_cap = (size_t)MSH_PLY_MAX(new_cap, MSH_PLY_MAX(new_len, 16));
size_t new_size = sizeof(msh_ply_array_hdr_t) + new_cap * elem_size;
msh_ply_array_hdr_t* new_hdr;
if (array)
{
new_hdr = (msh_ply_array_hdr_t*)MSH_PLY_REALLOC(msh_ply_array__hdr(array),
new_size);
}
else
{
new_hdr = (msh_ply_array_hdr_t*)MSH_PLY_MALLOC(new_size);
new_hdr->len = 0;
}
new_hdr->cap = new_cap;
return (void*)((char*)new_hdr + sizeof(msh_ply_array_hdr_t));
}
MSH_PLY_PRIVATE msh_ply_property_t
msh_ply__property_zero_init()
{
msh_ply_property_t pr =
{0, 0, 0, 0, 0, 0, 0, MSH_PLY_INVALID, MSH_PLY_INVALID, 0, 0, {0}, 0, 0};
return pr;
}
MSH_PLY_PRIVATE msh_ply_element_t
msh_ply__element_zero_init()
{
msh_ply_element_t el = {{0}, 0, 0, 0, 0, 0};
return el;
}
MSH_PLY_DEF msh_ply_element_t*
msh_ply_find_element(const msh_ply_t* pf, const char* element_name)
{
msh_ply_element_t* el = NULL;
for (size_t i = 0; i < msh_ply_array_len(pf->elements); ++i)
{
if (!strcmp(element_name, pf->elements[i].name))
{
el = &pf->elements[i];
break;
}
}
return el;
}
MSH_PLY_DEF bool
msh_ply_has_properties(const msh_ply_t* pf, const msh_ply_desc_t* desc)
{
msh_ply_element_t* el = msh_ply_find_element(pf, desc->element_name);
if (!el) { return false; }
for (int32_t i = 0; i < desc->num_properties; ++i)
{
const char* property_name = desc->property_names[i];
bool found = false;
for (size_t j = 0; j < msh_ply_array_len(el->properties); ++j)
{
if (!strcmp(el->properties[j].name, property_name))
{
found = true;
break;
}
}
if (!found) { return false; }
}
return true;
}
MSH_PLY_DEF msh_ply_property_t*
msh_ply_find_property(const msh_ply_element_t* el, const char* property_name)
{
msh_ply_property_t* pr = NULL;
for (size_t i = 0; i < msh_ply_array_len(el->properties); ++i)
{
if (!strcmp(el->properties[i].name, property_name))
{
pr = &el->properties[i];
break;
}
}
return pr;
}
MSH_PLY_PRIVATE int32_t
msh_ply__validate_descriptor(const msh_ply_desc_t* desc)
{
if (!desc) { return MSH_PLY_NULL_DESCRIPTOR_ERR; }
if (!desc->element_name) { return MSH_PLY_NULL_ELEMENT_NAME_ERR; }
if (!desc->property_names) { return MSH_PLY_NULL_PROPERTY_NAMES_ERR; }
if (desc->num_properties <= 0) { return MSH_PLY_NO_REQUESTED_PROPERTIES_ERR; }
if (!desc->data) { return MSH_PLY_NULL_DATA_PTR_ERR; }
if (!desc->data_count) { return MSH_PLY_NULL_DATA_COUNT_PTR_ERR; }
if (desc->data_type <= MSH_PLY_INVALID || desc->data_type >= MSH_PLY_N_TYPES)
{
return MSH_PLY_INVALID_DATA_TYPE_ERR;
}
if (desc->list_type < MSH_PLY_INVALID || desc->list_type >= MSH_PLY_FLOAT)
{
return MSH_PLY_INVALID_LIST_TYPE_ERR;
}
for (int32_t i = 0; i < desc->num_properties; ++i)
{
if (!desc->property_names[i]) { return MSH_PLY_NULL_PROPERTY_NAME_ERR; }
}
return MSH_PLY_NO_ERR;
}
MSH_PLY_DEF int32_t
msh_ply_add_descriptor(msh_ply_t* pf, msh_ply_desc_t* desc)
{
if (!pf) { return MSH_PLY_FILE_NOT_OPEN_ERR; }
int32_t desc_err = msh_ply__validate_descriptor(desc);
if (desc_err) { return desc_err; }
msh_ply_array_push(pf->descriptors, desc);
return MSH_PLY_NO_ERR;
}
MSH_PLY_PRIVATE MSH_PLY_INLINE void
msh_ply__swap_bytes(uint8_t* buffer, int32_t type_size, int32_t count)
{
for (int32_t i = 0; i < count; ++i)
{
int32_t offset = i * type_size;
for (int32_t j = 0; j < type_size >> 1; ++j)
{
uint8_t temp = buffer[offset + j];
buffer[offset + j] = buffer[offset + type_size - 1 - j];
buffer[offset + type_size - 1 - j] = temp;
}
}
}
MSH_PLY_PRIVATE MSH_PLY_INLINE int32_t
msh_ply__get_data_as_int(void* data, int32_t type, int8_t swap_endianness)
{
int32_t retval = 0;
switch (type)
{
case MSH_PLY_INT8:
retval = ((char*)data)[0];
break;
case MSH_PLY_INT16:
{
retval = ((int16_t*)data)[0];
if (swap_endianness) msh_ply__swap_bytes((uint8_t*)&retval, 2, 1);
break;
}
case MSH_PLY_INT32:
{
retval = ((int32_t*)data)[0];
if (swap_endianness) msh_ply__swap_bytes((uint8_t*)&retval, 4, 1);
break;
}
case MSH_PLY_UINT8:
retval = ((uint8_t*)data)[0];
break;
case MSH_PLY_UINT16:
{
retval = ((uint16_t*)data)[0];
if (swap_endianness) msh_ply__swap_bytes((uint8_t*)&retval, 2, 1);
break;
}
case MSH_PLY_UINT32:
{
retval = ((uint32_t*)data)[0];
if (swap_endianness) msh_ply__swap_bytes((uint8_t*)&retval, 4, 1);
break;
}
default:
retval = 0;
break;
}
return retval;
}
// TODO(maciej): Static table instead of switch statement?
MSH_PLY_PRIVATE MSH_PLY_INLINE int16_t
msh_ply__type_to_byte_size(msh_ply_type_id_t type)
{
int16_t retval = 0;
switch (type)
{
case MSH_PLY_INT8:
{
retval = 1;
break;
}
case MSH_PLY_INT16:
{
retval = 2;
break;
}
case MSH_PLY_INT32:
{
retval = 4;
break;
}
case MSH_PLY_UINT8:
{
retval = 1;
break;
}
case MSH_PLY_UINT16:
{
retval = 2;
break;
}
case MSH_PLY_UINT32:
{
retval = 4;
break;
}
case MSH_PLY_FLOAT:
{
retval = 4;
break;
}
case MSH_PLY_DOUBLE:
{
retval = 8;
break;
}
default:
{
retval = 0;
break;
}
}
return retval;
}
// TODO(maciej): Static table instead of switch statement?
MSH_PLY_PRIVATE MSH_PLY_INLINE void
msh_ply__property_type_to_string(msh_ply_type_id_t type, char** string)
{
switch (type)
{
case MSH_PLY_INT8:
{
*string = (char*)"char";
break;
}
case MSH_PLY_INT16:
{
*string = (char*)"short";
break;
}
case MSH_PLY_INT32:
{
*string = (char*)"int";
break;
}
case MSH_PLY_UINT8:
{
*string = (char*)"uchar";
break;
}
case MSH_PLY_UINT16:
{
*string = (char*)"ushort";
break;
}
case MSH_PLY_UINT32:
{
*string = (char*)"uint";
break;
}
case MSH_PLY_FLOAT:
{
*string = (char*)"float";
break;
}
case MSH_PLY_DOUBLE:
{
*string = (char*)"double";
break;
}
default:
{
break;
}
}
}
//TODO(maciej): String interning?
MSH_PLY_PRIVATE MSH_PLY_INLINE void
msh_ply__string_to_property_type(char* type_str,
msh_ply_type_id_t* pr_type,
int16_t* pr_size)
{
if (!strcmp("int8", type_str) || !strcmp("char", type_str))
{
*pr_type = MSH_PLY_INT8;
*pr_size = 1;
}
else if (!strcmp("uint8", type_str) || !strcmp("uchar", type_str))
{
*pr_type = MSH_PLY_UINT8;
*pr_size = 1;
}
else if (!strcmp("int16", type_str) || !strcmp("short", type_str))
{
*pr_type = MSH_PLY_INT16;
*pr_size = 2;
}
else if (!strcmp("uint16", type_str) || !strcmp("ushort", type_str))
{
*pr_type = MSH_PLY_UINT16;
*pr_size = 2;
}
else if (!strcmp("int32", type_str) || !strcmp("int", type_str))
{
*pr_type = MSH_PLY_INT32;
*pr_size = 4;
}
else if (!strcmp("uint32", type_str) || !strcmp("uint", type_str))
{
*pr_type = MSH_PLY_UINT32;
*pr_size = 4;
}
else if (!strcmp("float32", type_str) || !strcmp("float", type_str))
{
*pr_type = MSH_PLY_FLOAT;
*pr_size = 4;
}
else if (!strcmp("float64", type_str) || !strcmp("double", type_str))
{
*pr_type = MSH_PLY_DOUBLE;
*pr_size = 8;
}
else
{
*pr_type = MSH_PLY_INVALID;
*pr_size = 0;
}
}
#define MSH_PLY__COPY_DATA_AS_TYPE(dst, src, T) \
do { \
T* dst_ptr = (T*)(dst); \
T* src_ptr = (T*)(src); \
for (int32_t c = 0; c < a; c++) \
{ \
*(dst_ptr) = *(src_ptr); \
dst_ptr++; \
src_ptr++; \
*(dst_ptr) = *(src_ptr); \
dst_ptr++; \
src_ptr++; \
*(dst_ptr) = *(src_ptr); \
dst_ptr++; \
src_ptr++; \
} \
for (int32_t c = 0; c < b; c++) \
{ \
*(dst_ptr) = *(src_ptr); \
dst_ptr++; \
src_ptr++; \
} \
} while (0)
MSH_PLY_PRIVATE void
msh_ply__data_assign(void* dst, void* src, int32_t type, int32_t count)
{
int32_t a = count / 3;
int32_t b = count % 3;
switch (type)
{
case MSH_PLY_UINT8:
{
MSH_PLY__COPY_DATA_AS_TYPE(dst, src, uint8_t);
break;
}
case MSH_PLY_UINT16:
{
MSH_PLY__COPY_DATA_AS_TYPE(dst, src, uint16_t);
break;
}
case MSH_PLY_UINT32:
{
MSH_PLY__COPY_DATA_AS_TYPE(dst, src, uint32_t);
break;
}
case MSH_PLY_INT8:
{
MSH_PLY__COPY_DATA_AS_TYPE(dst, src, int8_t);
break;
}
case MSH_PLY_INT16:
{
MSH_PLY__COPY_DATA_AS_TYPE(dst, src, int16_t);
break;
}
case MSH_PLY_INT32:
{
MSH_PLY__COPY_DATA_AS_TYPE(dst, src, int32_t);
break;
}
case MSH_PLY_FLOAT:
{
MSH_PLY__COPY_DATA_AS_TYPE(dst, src, float);
break;
}
case MSH_PLY_DOUBLE:
{
MSH_PLY__COPY_DATA_AS_TYPE(dst, src, double);
break;
}
default:
break;
}
}
#undef MSH_PLY__COPY_DATA_AS_TYPE
#ifndef MSH_PLY_ENCODER_ONLY
MSH_PLY_PRIVATE int32_t
msh_ply__parse_ply_cmd(char* line, msh_ply_t* pf)
{
char cmd_str[MSH_PLY_MAX_STR_LEN];
if (sscanf(line, "%s", cmd_str))
{
if (!strcmp(cmd_str, "ply"))
{
pf->valid = true;
return MSH_PLY_NO_ERR;
}
}