Debian Codegraph @

src/thirdparty/PoissonRecon/PlyFile.inl

raw
1 /*
2
3 The interface routines for reading and writing PLY polygon files.
4
5 Greg Turk, February 1994
6
7 ---------------------------------------------------------------
8
9 A PLY file contains a single polygonal _object_.
10
11 An object is composed of lists of _elements_. Typical elements are
12 vertices, faces, edges and materials.
13
14 Each type of element for a given object has one or more _properties_
15 associated with the element type. For instance, a vertex element may
16 have as properties the floating-point values x,y,z and the three unsigned
17 chars representing red, green and blue.
18
19 ---------------------------------------------------------------
20
21 Copyright (c) 2020, Georgia Institute of Technology
22 All rights reserved.
23
24 Redistribution and use in source and binary forms, with or without
25 modification, are permitted provided that the following conditions are met:
26
27 Redistributions of source code must retain the above copyright notice, this
28 list of conditions and the following disclaimer. Redistributions in binary form
29 must reproduce the above copyright notice, this list of conditions and the
30 following disclaimer in the documentation and/or other materials provided with
31 the distribution.
32
33 Neither the name of the Georgia Institute of Technology nor the names of its
34 contributors may be used to endorse or promote products derived from this
35 software without specific prior written permission.
36
37 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
38 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED
39 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
41 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
42 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
44 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
45 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
46 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47
48 */
49
50 static const char *type_names[] =
51 {
52 "invalid",
53 "char",
54 "short",
55 "int",
56 "longlong",
57 "uchar",
58 "ushort",
59 "uint",
60 "ulonglong",
61 "float",
62 "double",
63 "int8", // character 1
64 "uint8", // unsigned character 1
65 "int16", // short integer 2
66 "uint16", // unsigned short integer 2
67 "int32", // integer 4
68 "uint32", // unsigned integer 4
69 "int64", // integer 8
70 "uint64", // unsigned integer 8
71 "float32", // single-precision float 4
72 "float64", // double-precision float 8
73 };
74
75 static const int ply_type_size[] =
76 {
77 0,
78 1,
79 2,
80 4,
81 8,
82 1,
83 2,
84 4,
85 8,
86 4,
87 8,
88 1,
89 1,
90 2,
91 2,
92 4,
93 4,
94 8,
95 8,
96 4,
97 8
98 };
99
100 typedef union
101 {
102 int int_value;
103 char byte_values[sizeof(int)];
104 } endian_test_type;
105
106
107 static int native_binary_type = -1;
108 static int types_checked = 0;
109
110 static const int no_other_props = -1;
111
112 static const int dont_store_prop = 0;
113 static const int store_prop = 1;
114
115 static const int other_prop = 0;
116 static const int named_prop = 1;
117
118
119 /* write to a file the word describing a PLY file data type */
120 inline void write_scalar_type( FILE * , int );
121
122 /* read a line from a file and break it up into separate words */
123 inline std::vector< std::string > get_words( FILE * , char ** );
124
125 /* write to a file the word describing a PLY file data type */
126 inline void write_scalar_type( FILE * , int );
127
128 /* write an item to a file */
129 inline void write_binary_item( FILE * , int , int , unsigned int , long long , unsigned long long , double , int );
130 inline void write_ascii_item ( FILE * , int , unsigned int , long long , unsigned long long , double , int );
131
132 /* store a value into where a pointer and a type specify */
133 inline void store_item( void * , int , int , unsigned int , long long , unsigned long long , double );
134
135 /* return the value of a stored item */
136 inline void get_stored_item( void * , int , int & , unsigned int & , long long & , unsigned long long & , double & );
137
138 /* return the value stored in an item, given ptr to it and its type */
139 inline double get_item_value( const void * , int );
140
141 /* get binary or ascii item and store it according to ptr and type */
142 inline void get_ascii_item( const std::string & , int , int & , unsigned int & , long long & , unsigned long long & , double & );
143 inline void get_binary_item( FILE * , int , int , int & , unsigned int & , long long & , unsigned long long & , double & );
144
145 /* byte ordering */
146 inline void get_native_binary_type();
147 inline void swap_bytes( void * , int );
148
149 inline void check_types( void );
150
151 inline int get_prop_type( const std::string &type_name );
152 inline void setup_other_props( PlyElement *elem );
153
154 /*************/
155 /* Writing */
156 /*************/
157
158
159 /******************************************************************************
160 Given a file pointer, get ready to write PLY data to the file.
161
162 Entry:
163 fp - the given file pointer
164 nelems - number of elements in object
165 elem_names - list of element names
166 file_type - file type, either ascii or binary
167
168 Exit:
169 returns a pointer to a PlyFile, used to refer to this file, or NULL if error
170 ******************************************************************************/
171
172 PlyFile *PlyFile::_Write( FILE *fp , const std::vector< std::string > &elem_names , int file_type )
173 {
174 /* check for NULL file pointer */
175 if( fp==NULL ) return NULL;
176
177 if( native_binary_type==-1 ) get_native_binary_type();
178 if( !types_checked ) check_types();
179
180 /* create a record for this object */
181
182 PlyFile *plyfile = new PlyFile( fp );
183 if( file_type==PLY_BINARY_NATIVE ) plyfile->file_type = native_binary_type;
184 else plyfile->file_type = file_type;
185
186 /* tuck aside the names of the elements */
187 plyfile->elems.resize( elem_names.size() );
188 for( int i=0 ; i<elem_names.size() ; i++ )
189 {
190 plyfile->elems[i].name = elem_names[i];
191 plyfile->elems[i].num = 0;
192 }
193
194 /* return pointer to the file descriptor */
195 return plyfile;
196 }
197
198
199 /******************************************************************************
200 Open a polygon file for writing.
201
202 Entry:
203 filename - name of file to read from
204 nelems - number of elements in object
205 elem_names - list of element names
206 file_type - file type, either ascii or binary
207
208 Exit:
209 version - version number of PLY file
210 returns a file identifier, used to refer to this file, or NULL if error
211 ******************************************************************************/
212
213 PlyFile *PlyFile::Write( const std::string &filename , const std::vector< std::string > &elem_names , int file_type , float &version )
214 {
215 /* tack on the extension .ply, if necessary */
216 std::string name = filename;
217 if( name.length()<4 || name.substr( name.length()-4 )!=".ply" ) name += ".ply";
218
219 /* open the file for writing */
220 FILE *fp = fopen( name.c_str() , "wb" );
221 if( fp==NULL ) return NULL;
222
223 /* create the actual PlyFile structure */
224 PlyFile *plyfile = _Write( fp , elem_names , file_type );
225
226 /* say what PLY file version number we're writing */
227 version = plyfile->version;
228
229 /* return pointer to the file descriptor */
230 return plyfile;
231 }
232
233
234 /******************************************************************************
235 Describe an element, including its properties and how many will be written
236 to the file.
237
238 Entry:
239 elem_name - name of element that information is being specified about
240 nelems - number of elements of this type to be written
241 nprops - number of properties contained in the element
242 prop_list - list of properties
243 ******************************************************************************/
244
245 void PlyFile::describe_element( const std::string &elem_name , size_t nelems , int nprops , const PlyProperty *prop_list )
246 {
247 /* look for appropriate element */
248 PlyElement *elem = find_element( elem_name );
249 if( elem==NULL ) MK_THROW( "Can't find element '" , elem_name , "'" );
250
251 elem->num = nelems;
252
253 /* copy the list of properties */
254 elem->props.resize( nprops );
255 for( int i=0 ; i<nprops ; i++ ) elem->props[i] = PlyStoredProperty( prop_list[i] , named_prop );
256 }
257
258
259 /******************************************************************************
260 Describe a property of an element.
261
262 Entry:
263 elem_name - name of element that information is being specified about
264 prop - the new property
265 ******************************************************************************/
266
267 void PlyFile::describe_property( const std::string &elem_name , const PlyProperty *prop )
268 {
269 /* look for appropriate element */
270 PlyElement *elem = find_element( elem_name );
271 if( elem == NULL )
272 {
273 MK_WARN( "Can't find element '" , elem_name , "'" );
274 return;
275 }
276
277 elem->props.push_back( PlyStoredProperty( *prop , named_prop ) );
278 }
279
280
281 /******************************************************************************
282 Describe what the "other" properties are that are to be stored, and where
283 they are in an element.
284 ******************************************************************************/
285
286 void PlyFile::describe_other_properties( const PlyOtherProp &other , int offset )
287 {
288 /* look for appropriate element */
289 PlyElement *elem = find_element( other.name );
290 if( elem==NULL )
291 {
292 MK_WARN( "Can't find element '" , other.name , "'" );
293 return;
294 }
295
296 elem->props.reserve( elem->props.size() + other.props.size() );
297 for( int i=0 ; i<other.props.size() ; i++ ) elem->props.push_back( PlyStoredProperty( other.props[i] , other_prop ) );
298
299 /* save other info about other properties */
300 elem->other_size = other.size;
301 elem->other_offset = offset;
302 }
303
304
305 /******************************************************************************
306 State how many of a given element will be written.
307
308 Entry:
309 elem_name - name of element that information is being specified about
310 nelems - number of elements of this type to be written
311 ******************************************************************************/
312 void PlyFile::element_count( const std::string &elem_name , size_t nelems )
313 {
314 /* look for appropriate element */
315 PlyElement *elem = find_element( elem_name );
316 if( elem==NULL ) MK_THROW( "Can't find element '" , elem_name , "'" );
317
318 elem->num = nelems;
319 }
320
321
322 /******************************************************************************
323 Signal that we've described everything a PLY file's header and that the
324 header should be written to the file.
325 ******************************************************************************/
326
327 void PlyFile::header_complete( void )
328 {
329 fprintf( fp , "ply\n" );
330 switch( file_type )
331 {
332 case PLY_ASCII: fprintf( fp , "format ascii 1.0\n" ) ; break;
333 case PLY_BINARY_BE: fprintf( fp , "format binary_big_endian 1.0\n" ) ; break;
334 case PLY_BINARY_LE: fprintf( fp , "format binary_little_endian 1.0\n" ) ; break;
335 default: MK_THROW( "Bad file type: " , file_type );
336 }
337
338 /* write out the comments */
339 for( int i=0 ; i<comments.size() ; i++ ) fprintf( fp , "comment %s\n" , comments[i].c_str() );
340
341 /* write out object information */
342 for( int i=0 ; i<obj_info.size() ; i++ ) fprintf( fp , "obj_info %s\n" , obj_info[i].c_str() );
343
344 /* write out information about each element */
345 for( int i=0 ; i<elems.size() ; i++ )
346 {
347 fprintf( fp , "element %s %llu\n" , elems[i].name.c_str() , (unsigned long long)elems[i].num );
348
349 for( int j=0 ; j<elems[i].props.size() ; j++ )
350 {
351 if( elems[i].props[j].prop.is_list )
352 {
353 fprintf( fp , "property list " );
354 write_scalar_type( fp , elems[i].props[j].prop.count_external );
355 fprintf( fp , " " );
356 write_scalar_type( fp , elems[i].props[j].prop.external_type );
357 fprintf( fp , " %s\n", elems[i].props[j].prop.name.c_str() );
358 }
359 else
360 {
361 fprintf( fp , "property " );
362 write_scalar_type( fp , elems[i].props[j].prop.external_type );
363 fprintf( fp , " %s\n", elems[i].props[j].prop.name.c_str() );
364 }
365 }
366 }
367
368 fprintf( fp , "end_header\n" );
369 }
370
371
372 /******************************************************************************
373 Specify which elements are going to be written. This should be called
374 before a call to the routine ply_put_element().
375
376 Entry:
377 elem_name - name of element we're talking about
378 ******************************************************************************/
379
380 void PlyFile::put_element_setup( const std::string &elem_name )
381 {
382 PlyElement *elem = find_element( elem_name );
383 if( elem==NULL ) MK_THROW( "Can't find element '" , elem_name , "'" );
384 which_elem = elem;
385 }
386
387
388 /******************************************************************************
389 Write an element to the file. This routine assumes that we're
390 writing the type of element specified in the last call to the routine
391 ply_put_element_setup().
392
393 Entry:
394 elem_ptr - pointer to the element
395 ******************************************************************************/
396
397 void PlyFile::put_element( void *elem_ptr )
398 {
399 char *elem_data,*item;
400 char **item_ptr;
401 int list_count;
402 int item_size;
403 int int_val;
404 unsigned int uint_val;
405 long long longlong_val;
406 unsigned long long ulonglong_val;
407 double double_val;
408 char **other_ptr;
409
410 PlyElement *elem = which_elem;
411 elem_data = (char *)elem_ptr;
412 other_ptr = (char **) (((char *) elem_ptr) + elem->other_offset);
413
414 /* write out either to an ascii or binary file */
415
416 if( file_type==PLY_ASCII ) /* write an ascii file */
417 {
418 /* write out each property of the element */
419 for( int j=0 ; j<elem->props.size() ; j++ )
420 {
421 if( elem->props[j].store==other_prop ) elem_data = *other_ptr;
422 else elem_data = (char *)elem_ptr;
423 if( elem->props[j].prop.is_list )
424 {
425 item = elem_data + elem->props[j].prop.count_offset;
426 get_stored_item( (void *)item , elem->props[j].prop.count_internal , int_val , uint_val , longlong_val , ulonglong_val , double_val );
427 write_ascii_item( fp , int_val , uint_val , longlong_val , ulonglong_val , double_val , elem->props[j].prop.count_external );
428 list_count = uint_val;
429 item_ptr = (char **)( elem_data + elem->props[j].prop.offset );
430 item = item_ptr[0];
431 item_size = ply_type_size[ elem->props[j].prop.internal_type ];
432 for( int k=0 ; k<list_count ; k++ )
433 {
434 get_stored_item( (void *)item , elem->props[j].prop.internal_type , int_val , uint_val , longlong_val , ulonglong_val , double_val );
435 write_ascii_item( fp , int_val , uint_val , longlong_val , ulonglong_val , double_val , elem->props[j].prop.external_type );
436 item += item_size;
437 }
438 }
439 else
440 {
441 item = elem_data + elem->props[j].prop.offset;
442 get_stored_item( (void *)item , elem->props[j].prop.internal_type , int_val , uint_val , longlong_val , ulonglong_val , double_val );
443 write_ascii_item( fp , int_val , uint_val , longlong_val , ulonglong_val , double_val , elem->props[j].prop.external_type );
444 }
445 }
446 fprintf( fp , "\n" );
447 }
448 else /* write a binary file */
449 {
450 /* write out each property of the element */
451 for( int j=0 ; j<elem->props.size() ; j++ )
452 {
453 if (elem->props[j].store==other_prop ) elem_data = *other_ptr;
454 else elem_data = (char *)elem_ptr;
455 if( elem->props[j].prop.is_list )
456 {
457 item = elem_data + elem->props[j].prop.count_offset;
458 item_size = ply_type_size[ elem->props[j].prop.count_internal ];
459 get_stored_item( (void *)item , elem->props[j].prop.count_internal , int_val , uint_val , longlong_val , ulonglong_val , double_val );
460 write_binary_item( fp , file_type , int_val , uint_val , longlong_val , ulonglong_val , double_val , elem->props[j].prop.count_external );
461 list_count = uint_val;
462 item_ptr = (char **)( elem_data + elem->props[j].prop.offset );
463 item = item_ptr[0];
464 item_size = ply_type_size[ elem->props[j].prop.internal_type ];
465 for( int k=0 ; k<list_count ; k++ )
466 {
467 get_stored_item( (void *)item , elem->props[j].prop.internal_type , int_val , uint_val , longlong_val , ulonglong_val , double_val );
468 write_binary_item( fp , file_type , int_val , uint_val , longlong_val , ulonglong_val , double_val , elem->props[j].prop.external_type );
469 item += item_size;
470 }
471 }
472 else
473 {
474 item = elem_data + elem->props[j].prop.offset;
475 item_size = ply_type_size[ elem->props[j].prop.internal_type ];
476 get_stored_item( (void *)item , elem->props[j].prop.internal_type , int_val , uint_val , longlong_val , ulonglong_val , double_val );
477 write_binary_item( fp , file_type , int_val , uint_val , longlong_val , ulonglong_val , double_val , elem->props[j].prop.external_type );
478 }
479 }
480 }
481 }
482
483
484 /******************************************************************************
485 Specify a comment that will be written in the header.
486
487 Entry:
488 comment - the comment to be written
489 ******************************************************************************/
490
491 void PlyFile::put_comment( const std::string &comment ){ comments.push_back( comment ); }
492
493
494 /******************************************************************************
495 Specify a piece of object information (arbitrary text) that will be written
496 in the header.
497
498 Entry:
499 obj_info - the text information to be written
500 ******************************************************************************/
501
502 void PlyFile::put_obj_info( const std::string &obj_info ){ this->obj_info.push_back( obj_info ); }
503
504
505 /*************/
506 /* Reading */
507 /*************/
508
509
510
511 /******************************************************************************
512 Given a file pointer, get ready to read PLY data from the file.
513
514 Entry:
515 fp - the given file pointer
516
517 Exit:
518 nelems - number of elements in object
519 elem_names - list of element names
520 returns a pointer to a PlyFile, used to refer to this file, or NULL if error
521 ******************************************************************************/
522
523 PlyFile *PlyFile::_Read( FILE *fp , std::vector< std::string > &elem_names )
524 {
525 char *orig_line;
526 /* check for NULL file pointer */
527 if( fp==NULL ) return NULL;
528
529 if( native_binary_type==-1 ) get_native_binary_type();
530 if( !types_checked ) check_types();
531
532 /* create record for this object */
533 std::vector< std::string > words;
534 PlyFile *plyfile = new PlyFile( fp );
535
536 /* read and parse the file's header */
537 words = get_words( plyfile->fp , &orig_line );
538 if( !words.size() || words[0]!="ply" ) return NULL;
539 while( words.size() )
540 {
541 /* parse words */
542 if( words[0]=="format" )
543 {
544 if( words.size()!=3 ) return NULL;
545 if ( words[1]=="ascii" ) plyfile->file_type = PLY_ASCII;
546 else if( words[1]=="binary_big_endian" ) plyfile->file_type = PLY_BINARY_BE;
547 else if( words[1]=="binary_little_endian" ) plyfile->file_type = PLY_BINARY_LE;
548 else return NULL;
549 plyfile->version = (float)atof( words[2].c_str() );
550 }
551 else if( words[0]=="element" ) plyfile->add_element ( words );
552 else if( words[0]=="property" ) plyfile->add_property( words );
553 else if( words[0]=="comment" ) plyfile->add_comment ( orig_line );
554 else if( words[0]=="obj_info" ) plyfile->add_obj_info( orig_line );
555 else if( words[0]=="end_header" ) break;
556
557 words = get_words( plyfile->fp , &orig_line );
558 }
559
560 /* create tags for each property of each element, to be used */
561 /* later to say whether or not to store each property for the user */
562 for( int i=0 ; i<plyfile->elems.size() ; i++ )
563 {
564 for( int j=0 ; j<plyfile->elems[i].props.size() ; j++ ) plyfile->elems[i].props[j].store = dont_store_prop;
565 plyfile->elems[i].other_offset = no_other_props; /* no "other" props by default */
566 }
567
568 /* set return values about the elements */
569 elem_names.resize( plyfile->elems.size() );
570 for( int i=0 ; i<elem_names.size() ; i++ ) elem_names[i] = plyfile->elems[i].name;
571
572 /* return a pointer to the file's information */
573 return plyfile;
574 }
575
576
577 /******************************************************************************
578 Open a polygon file for reading.
579
580 Entry:
581 filename - name of file to read from
582
583 Exit:
584 nelems - number of elements in object
585 elem_names - list of element names
586 file_type - file type, either ascii or binary
587 version - version number of PLY file
588 returns a file identifier, used to refer to this file, or NULL if error
589 ******************************************************************************/
590
591 PlyFile *PlyFile::Read( const std::string &filename , std::vector< std::string > &elem_names , int &file_type , float &version )
592 {
593 /* tack on the extension .ply, if necessary */
594 std::string name = filename;
595 if( name.length()<4 || name.substr( name.length()-4 )!=".ply" ) name += ".ply";
596
597 /* open the file for reading */
598 FILE *fp = fopen( name.c_str() , "rb" );
599 if( fp==NULL ) return NULL;
600
601 /* create the PlyFile data structure */
602 PlyFile *plyfile = _Read( fp , elem_names );
603
604 /* determine the file type and version */
605 file_type = plyfile->file_type;
606 version = plyfile->version;
607
608 /* return a pointer to the file's information */
609 return plyfile;
610 }
611
612
613 /******************************************************************************
614 Get information about a particular element.
615
616 Entry:
617 elem_name - name of element to get information about
618
619 Exit:
620 nelems - number of elements of this type in the file
621 nprops - number of properties
622 returns a list of properties, or NULL if the file doesn't contain that elem
623 ******************************************************************************/
624
625 std::vector< PlyProperty > PlyFile::get_element_description( const std::string &elem_name , size_t &nelems )
626 {
627 std::vector< PlyProperty > prop_list;
628
629 /* find information about the element */
630 PlyElement *elem = find_element( elem_name );
631 if( elem==NULL ) return prop_list;
632 nelems = elem->num;
633
634 /* make a copy of the element's property list */
635 prop_list.resize( elem->props.size() );
636 for( int i=0 ; i<elem->props.size() ; i++ ) prop_list[i] = elem->props[i].prop;
637
638 /* return this duplicate property list */
639 return prop_list;
640 }
641
642 /******************************************************************************
643 Specify which properties of an element are to be returned. This should be
644 called before a call to the routine ply_get_element().
645
646 Entry:
647 elem_name - which element we're talking about
648 nprops - number of properties
649 prop_list - list of properties
650 ******************************************************************************/
651
652 void PlyFile::get_element_setup( const std::string &elem_name , int nprops , PlyProperty *prop_list )
653 {
654 /* find information about the element */
655 PlyElement *elem = find_element( elem_name );
656 which_elem = elem;
657
658 /* deposit the property information into the element's description */
659 for( int i=0 ; i<nprops ; i++ )
660 {
661 /* look for actual property */
662 int index;
663 PlyProperty *prop = elem->find_property( prop_list[i].name , index );
664 if( prop==NULL )
665 {
666 MK_WARN( "Can't find property '" , prop_list[i].name , "' in element '" , elem_name , "'" );
667 continue;
668 }
669
670 /* store its description */
671 prop->internal_type = prop_list[i].internal_type;
672 prop->offset = prop_list[i].offset;
673 prop->count_internal = prop_list[i].count_internal;
674 prop->count_offset = prop_list[i].count_offset;
675
676 /* specify that the user wants this property */
677 elem->props[index].store = store_prop;
678 }
679 }
680
681
682 /******************************************************************************
683 Specify a property of an element that is to be returned. This should be
684 called (usually multiple times) before a call to the routine ply_get_element().
685 This routine should be used in preference to the less flexible old routine
686 called ply_get_element_setup().
687
688 Entry:
689 elem_name - which element we're talking about
690 prop - property to add to those that will be returned
691 ******************************************************************************/
692
693 int PlyFile::get_property( const std::string &elem_name , const PlyProperty *prop )
694 {
695 /* find information about the element */
696 PlyElement *elem = find_element( elem_name );
697 which_elem = elem;
698
699 /* deposit the property information into the element's description */
700 int index;
701 PlyProperty *prop_ptr = elem->find_property( prop->name , index );
702 if( prop_ptr==NULL ) return 0;
703 prop_ptr->internal_type = prop->internal_type;
704 prop_ptr->offset = prop->offset;
705 prop_ptr->count_internal = prop->count_internal;
706 prop_ptr->count_offset = prop->count_offset;
707
708 /* specify that the user wants this property */
709 elem->props[index].store = store_prop;
710
711 return 1;
712 }
713
714
715 /******************************************************************************
716 Read one element from the file. This routine assumes that we're reading
717 the type of element specified in the last call to the routine
718 ply_get_element_setup().
719
720 Entry:
721 elem_ptr - pointer to location where the element information should be put
722 ******************************************************************************/
723
724 void PlyFile::get_element( void *elem_ptr )
725 {
726 if( file_type==PLY_ASCII ) _ascii_get_element( elem_ptr );
727 else _binary_get_element( elem_ptr );
728 }
729
730 /******************************************************************************
731 Extract the comments from the header information of a PLY file.
732
733 Exit:
734 num_comments - number of comments returned
735 returns a pointer to a list of comments
736 ******************************************************************************/
737
738 std::vector< std::string > &PlyFile::get_comments( void ){ return comments; }
739
740 /******************************************************************************
741 Extract the object information (arbitrary text) from the header information
742 of a PLY file.
743
744 Exit:
745 num_obj_info - number of lines of text information returned
746 returns a pointer to a list of object info lines
747 ******************************************************************************/
748 std::vector< std::string > &PlyFile::get_obj_info( void ){ return obj_info; }
749
750 /******************************************************************************
751 Make ready for "other" properties of an element-- those properties that
752 the user has not explicitly asked for, but that are to be stashed away
753 in a special structure to be carried along with the element's other
754 information.
755
756 Entry:
757 elem - element for which we want to save away other properties
758 ******************************************************************************/
759
760 void setup_other_props( PlyElement *elem )
761 {
762 int size = 0;
763
764 /* Examine each property in decreasing order of size. */
765 /* We do this so that all data types will be aligned by */
766 /* word, half-word, or whatever within the structure. */
767
768 for( int type_size=8 ; type_size>0 ; type_size/=2 )
769 {
770
771 /* add up the space taken by each property, and save this information */
772 /* away in the property descriptor */
773 for( int i=0 ; i<elem->props.size() ; i++ )
774 {
775 /* don't bother with properties we've been asked to store explicitly */
776 if( elem->props[i].store ) continue;
777 PlyProperty &prop = elem->props[i].prop;
778
779 /* internal types will be same as external */
780 prop.internal_type = prop.external_type;
781 prop.count_internal = prop.count_external;
782
783 /* check list case */
784 if( prop.is_list )
785 {
786 /* pointer to list */
787 if( type_size==sizeof(void *) )
788 {
789 prop.offset = size;
790 size += sizeof( void * ); /* always use size of a pointer here */
791 }
792
793 /* count of number of list elements */
794 if( type_size==ply_type_size[ prop.count_external ] )
795 {
796 prop.count_offset = size;
797 size += ply_type_size[ prop.count_external ];
798 }
799 }
800 /* not list */
801 else if( type_size==ply_type_size[ prop.external_type ] )
802 {
803 prop.offset = size;
804 size += ply_type_size[ prop.external_type ];
805 }
806 }
807 }
808
809 /* save the size for the other_props structure */
810 elem->other_size = size;
811 }
812
813
814 /******************************************************************************
815 Specify that we want the "other" properties of an element to be tucked
816 away within the user's structure. The user needn't be concerned for how
817 these properties are stored.
818
819 Entry:
820 elem_name - name of element that we want to store other_props in
821 offset - offset to where other_props will be stored inside user's structure
822
823 Exit:
824 returns pointer to structure containing description of other_props
825 ******************************************************************************/
826
827 bool PlyFile::set_other_properties( const std::string &elem_name , int offset , PlyOtherProp &other )
828 {
829 /* find information about the element */
830 PlyElement *elem = find_element( elem_name );
831 if( elem==NULL )
832 {
833 MK_WARN( "Can't find element '" , elem_name , "'" );
834 return false;
835 }
836
837 /* remember that this is the "current" element */
838 which_elem = elem;
839
840 /* save the offset to where to store the other_props */
841 elem->other_offset = offset;
842
843 /* place the appropriate pointers, etc. in the element's property list */
844 setup_other_props( elem );
845
846 /* create structure for describing other_props */
847 other.size = elem->other_size;
848 other.props.reserve( elem->props.size() );
849 for( int i=0 ; i<elem->props.size() ; i++ ) if( !elem->props[i].store ) other.props.push_back( elem->props[i].prop );
850
851 /* set other_offset pointer appropriately if there are NO other properties */
852 if( !other.props.size() ) elem->other_offset = no_other_props;
853 return true;
854 }
855
856 /*************************/
857 /* Other Element Stuff */
858 /*************************/
859
860
861
862
863 /******************************************************************************
864 Grab all the data for an element that a user does not want to explicitly
865 read in.
866
867 Entry:
868 elem_name - name of element whose data is to be read in
869 elem_count - number of instances of this element stored in the file
870
871 Exit:
872 returns pointer to ALL the "other" element data for this PLY file
873 ******************************************************************************/
874
875 PlyOtherElems *PlyFile::get_other_element( std::string &elem_name , size_t elem_count )
876 {
877 /* look for appropriate element */
878 PlyElement *elem = find_element( elem_name );
879 if( elem==NULL ) MK_THROW( "Can't find element '" , elem_name , "'" );
880
881 if( other_elems==NULL ) other_elems = new PlyOtherElems();
882 other_elems->other_list.resize( other_elems->other_list.size()+1 );
883 OtherElem *other = &other_elems->other_list.back();
884
885 /* save name of element */
886 other->elem_name = elem_name;
887
888 /* create a list to hold all the current elements */
889 other->other_data.resize( elem_count );
890
891 /* set up for getting elements */
892 set_other_properties( elem_name , offsetof( OtherData , other_props ) , other->other_props );
893
894 /* grab all these elements */
895 for( int i=0 ; i<other->other_data.size() ; i++ )
896 {
897 /* grab and element from the file */
898 get_element( (void *)&other->other_data[i] );
899 }
900
901 /* return pointer to the other elements data */
902 return other_elems;
903 }
904
905
906 /******************************************************************************
907 Pass along a pointer to "other" elements that we want to save in a given
908 PLY file. These other elements were presumably read from another PLY file.
909
910 Entry:
911 other_elems - info about other elements that we want to store
912 ******************************************************************************/
913
914 void PlyFile::describe_other_elements( PlyOtherElems *other_elems )
915 {
916 /* ignore this call if there is no other element */
917 if( other_elems==NULL ) return;
918
919 /* save pointer to this information */
920 this->other_elems = other_elems;
921
922 /* describe the other properties of this element */
923 /* store them in the main element list as elements with
924 only other properties */
925
926 elems.reserve( elems.size() + other_elems->other_list.size() );
927 for( int i=0 ; i<other_elems->other_list.size() ; i++ )
928 {
929 PlyElement elem;
930 elem.name = other_elems->other_list[i].elem_name;
931 elem.num = other_elems->other_list[i].other_data.size();
932 elem.props.resize(0);
933 describe_other_properties( other_elems->other_list[i].other_props , offsetof( OtherData , other_props ) );
934 elems.push_back( elem );
935 }
936 }
937
938
939 /******************************************************************************
940 Write out the "other" elements specified for this PLY file.
941 ******************************************************************************/
942
943 void PlyFile::put_other_elements( void )
944 {
945 OtherElem *other;
946
947 /* make sure we have other elements to write */
948 if( other_elems==NULL ) return;
949
950 /* write out the data for each "other" element */
951 for( int i=0 ; i<other_elems->other_list.size() ; i++ )
952 {
953 other = &(other_elems->other_list[i]);
954 put_element_setup( other->elem_name );
955
956 /* write out each instance of the current element */
957 for( int j=0 ; j<other->other_data.size() ; j++ ) put_element( (void *)&other->other_data[j] );
958 }
959 }
960
961 /*******************/
962 /* Miscellaneous */
963 /*******************/
964
965 /******************************************************************************
966 Get version number and file type of a PlyFile.
967
968 Exit:
969 version - version of the file
970 file_type - PLY_ASCII, PLY_BINARY_BE, or PLY_BINARY_LE
971 ******************************************************************************/
972
973 void PlyFile::get_info( float &version, int &file_type ){ version = this->version , file_type = this->file_type; }
974
975 /******************************************************************************
976 Find an element from the element list of a given PLY object.
977
978 Entry:
979 element - name of element we're looking for
980
981 Exit:
982 returns the element, or NULL if not found
983 ******************************************************************************/
984
985 PlyElement *PlyFile::find_element( const std::string &element )
986 {
987 for( int i=0 ; i<elems.size() ; i++ ) if( element==elems[i].name ) return &elems[i];
988 return NULL;
989 }
990
991
992 /******************************************************************************
993 Find a property in the list of properties of a given element.
994
995 Entry:
996 elem - pointer to element in which we want to find the property
997 prop_name - name of property to find
998
999 Exit:
1000 index - index to position in list
1001 returns a pointer to the property, or NULL if not found
1002 ******************************************************************************/
1003
1004 PlyProperty *PlyElement::find_property( const std::string &prop_name , int &index )
1005 {
1006 for( int i=0 ; i<props.size() ; i++ ) if( prop_name==props[i].prop.name ){ index = i ; return &props[i].prop; }
1007 index = -1;
1008 return NULL;
1009 }
1010
1011 /******************************************************************************
1012 Read an element from an ascii file.
1013
1014 Entry:
1015 elem_ptr - pointer to element
1016 ******************************************************************************/
1017
1018 void PlyFile::_ascii_get_element( void *elem_ptr )
1019 {
1020 std::vector< std::string > words;
1021 PlyElement *elem;
1022 int which_word;
1023 void *elem_data , *item=NULL;
1024 char *item_ptr;
1025 int item_size;
1026 int int_val;
1027 unsigned int uint_val;
1028 long long longlong_val;
1029 unsigned long long ulonglong_val;
1030 double double_val;
1031 int list_count;
1032 int store_it;
1033 char **store_array;
1034 char *orig_line;
1035 char *other_data=NULL;
1036 int other_flag;
1037
1038 /* the kind of element we're reading currently */
1039 elem = which_elem;
1040
1041 /* do we need to setup for other_props? */
1042 if( elem->other_offset!=no_other_props )
1043 {
1044 char **ptr;
1045 other_flag = 1;
1046 /* make room for other_props */
1047 other_data = (char *)malloc( elem->other_size );
1048 /* store pointer in user's structure to the other_props */
1049 ptr = (char **) ( (char*)elem_ptr + elem->other_offset);
1050 *ptr = other_data;
1051 }
1052 else other_flag = 0;
1053
1054 /* read in the element */
1055 words = get_words( fp , &orig_line );
1056 if( !words.size() ) MK_THROW( "Unexpected end of file" );
1057
1058 which_word = 0;
1059
1060 for( int j=0 ; j<elem->props.size() ; j++ )
1061 {
1062 PlyProperty &prop = elem->props[j].prop;
1063 store_it = (elem->props[j].store | other_flag);
1064
1065 /* store either in the user's structure or in other_props */
1066 if( elem->props[j].store ) elem_data = elem_ptr;
1067 else elem_data = other_data;
1068
1069 if( prop.is_list ) /* a list */
1070 {
1071 /* get and store the number of items in the list */
1072 get_ascii_item( words[which_word++] , prop.count_external , int_val , uint_val , longlong_val , ulonglong_val , double_val );
1073 if( store_it )
1074 {
1075 item = (char *)elem_data + prop.count_offset;
1076 store_item( item , prop.count_internal , int_val , uint_val , longlong_val , ulonglong_val , double_val );
1077 }
1078
1079 /* allocate space for an array of items and store a ptr to the array */
1080 list_count = int_val;
1081 item_size = ply_type_size[ prop.internal_type ];
1082 store_array = (char **)( (char *)elem_data + prop.offset );
1083
1084 if( list_count==0 )
1085 {
1086 if( store_it ) *store_array = NULL;
1087 }
1088 else
1089 {
1090 if( store_it )
1091 {
1092 item_ptr = (char *) malloc (sizeof (char) * item_size * list_count);
1093 item = item_ptr;
1094 *store_array = item_ptr;
1095 }
1096
1097 /* read items and store them into the array */
1098 for( int k=0 ; k<list_count ; k++ )
1099 {
1100 get_ascii_item( words[which_word++] , prop.external_type , int_val , uint_val , longlong_val , ulonglong_val , double_val );
1101 if( store_it )
1102 {
1103 store_item( item , prop.internal_type , int_val , uint_val , longlong_val , ulonglong_val , double_val );
1104 item = (char *)item + item_size;
1105 }
1106 }
1107 }
1108 }
1109 else /* not a list */
1110 {
1111 get_ascii_item( words[which_word++] , prop.external_type , int_val , uint_val , longlong_val , ulonglong_val , double_val );
1112 if( store_it )
1113 {
1114 item = (char *)elem_data + prop.offset;
1115 store_item( item , prop.internal_type , int_val , uint_val , longlong_val , ulonglong_val , double_val );
1116 }
1117 }
1118 }
1119 }
1120
1121 /******************************************************************************
1122 Read an element from a binary file.
1123
1124 Entry:
1125 elem_ptr - pointer to an element
1126 ******************************************************************************/
1127
1128 void PlyFile::_binary_get_element( void *elem_ptr )
1129 {
1130 PlyElement *elem;
1131 void *elem_data , *item=NULL;
1132 char *item_ptr;
1133 int item_size;
1134 int int_val;
1135 unsigned int uint_val;
1136 long long longlong_val;
1137 unsigned long long ulonglong_val;
1138 double double_val;
1139 int list_count;
1140 int store_it;
1141 char **store_array;
1142 char *other_data=NULL;
1143 int other_flag;
1144
1145 /* the kind of element we're reading currently */
1146 elem = which_elem;
1147
1148 /* do we need to setup for other_props? */
1149 if( elem->other_offset!=no_other_props )
1150 {
1151 char **ptr;
1152 other_flag = 1;
1153 /* make room for other_props */
1154 other_data = (char *) malloc (elem->other_size);
1155 /* store pointer in user's structure to the other_props */
1156 ptr = (char **) ((char *)elem_ptr + elem->other_offset);
1157 *ptr = other_data;
1158 }
1159 else other_flag = 0;
1160
1161 /* read in a number of elements */
1162
1163 for( int j=0 ; j<elem->props.size() ; j++ )
1164 {
1165 PlyProperty &prop = elem->props[j].prop;
1166 store_it = ( elem->props[j].store | other_flag );
1167
1168 /* store either in the user's structure or in other_props */
1169 if( elem->props[j].store ) elem_data = elem_ptr;
1170 else elem_data = other_data;
1171
1172 if( prop.is_list ) /* a list */
1173 {
1174 /* get and store the number of items in the list */
1175 get_binary_item( fp , file_type , prop.count_external , int_val, uint_val , longlong_val , ulonglong_val , double_val );
1176 if( store_it )
1177 {
1178 item = (char *)elem_data + prop.count_offset;
1179 store_item( item , prop.count_internal , int_val , uint_val , longlong_val , ulonglong_val , double_val );
1180 }
1181
1182 /* allocate space for an array of items and store a ptr to the array */
1183 list_count = int_val;
1184 item_size = ply_type_size[ prop.internal_type ];
1185 store_array = (char **) ((char *)elem_data + prop.offset);
1186 if( list_count==0 )
1187 {
1188 if( store_it ) *store_array = NULL;
1189 }
1190 else
1191 {
1192 if( store_it )
1193 {
1194 item_ptr = (char *)malloc(sizeof (char) * item_size * list_count);
1195 item = item_ptr;
1196 *store_array = item_ptr;
1197 }
1198
1199 /* read items and store them into the array */
1200 for( int k=0 ; k<list_count ; k++ )
1201 {
1202 get_binary_item( fp , file_type , prop.external_type , int_val , uint_val , longlong_val , ulonglong_val , double_val );
1203 if( store_it )
1204 {
1205 store_item( item , prop.internal_type , int_val , uint_val , longlong_val , ulonglong_val , double_val );
1206 item = (char *)item + item_size;
1207 }
1208 }
1209 }
1210 }
1211 else /* not a list */
1212 {
1213 get_binary_item( fp , file_type , prop.external_type , int_val , uint_val , longlong_val , ulonglong_val , double_val );
1214 if( store_it )
1215 {
1216 item = (char *)elem_data + prop.offset;
1217 store_item( item , prop.internal_type , int_val , uint_val , longlong_val , ulonglong_val , double_val );
1218 }
1219 }
1220 }
1221 }
1222
1223
1224 /******************************************************************************
1225 Write to a file the word that represents a PLY data type.
1226
1227 Entry:
1228 fp - file pointer
1229 code - code for type
1230 ******************************************************************************/
1231
1232 void write_scalar_type( FILE *fp , int code )
1233 {
1234 /* make sure this is a valid code */
1235 if( code<=PLY_START_TYPE || code>=PLY_END_TYPE ) MK_THROW( "Bad data code: " , code );
1236
1237 /* write the code to a file */
1238 fprintf( fp , "%s" , type_names[code] );
1239 }
1240
1241 /******************************************************************************
1242 Reverse the order in an array of bytes. This is the conversion from big
1243 endian to little endian and vice versa
1244
1245 Entry:
1246 bytes - array of bytes to reverse (in place)
1247 num_bytes - number of bytes in array
1248 ******************************************************************************/
1249
1250 void swap_bytes( void *bytes , int num_bytes )
1251 {
1252 char *chars = (char *)bytes;
1253
1254 for( int i=0 ; i<num_bytes/2 ; i++ )
1255 {
1256 char temp = chars[i];
1257 chars[i] = chars[(num_bytes-1)-i];
1258 chars[(num_bytes-1)-i] = temp;
1259 }
1260 }
1261
1262 /******************************************************************************
1263 Find out if this machine is big endian or little endian
1264
1265 Exit:
1266 set global variable, native_binary_type =
1267 either PLY_BINARY_BE or PLY_BINARY_LE
1268
1269 ******************************************************************************/
1270
1271 void get_native_binary_type( void )
1272 {
1273 endian_test_type test;
1274
1275 test.int_value = 0;
1276 test.int_value = 1;
1277 if ( test.byte_values[0]==1 ) native_binary_type = PLY_BINARY_LE;
1278 else if( test.byte_values[sizeof(int)-1] == 1) native_binary_type = PLY_BINARY_BE;
1279 else MK_THROW( "Couldn't determine machine endianness" );
1280 }
1281
1282 /******************************************************************************
1283 Verify that all the native types are the sizes we need
1284
1285
1286 ******************************************************************************/
1287
1288 void check_types()
1289 {
1290 if( (ply_type_size[PLY_CHAR] != sizeof(char)) ||
1291 (ply_type_size[PLY_SHORT] != sizeof(short)) ||
1292 (ply_type_size[PLY_INT] != sizeof(int)) ||
1293 (ply_type_size[PLY_LONGLONG] != sizeof(long long)) ||
1294 (ply_type_size[PLY_UCHAR] != sizeof(unsigned char)) ||
1295 (ply_type_size[PLY_USHORT] != sizeof(unsigned short)) ||
1296 (ply_type_size[PLY_UINT] != sizeof(unsigned int)) ||
1297 (ply_type_size[PLY_ULONGLONG] != sizeof(unsigned long long)) ||
1298 (ply_type_size[PLY_FLOAT] != sizeof(float)) ||
1299 (ply_type_size[PLY_DOUBLE] != sizeof(double)))
1300 MK_THROW( "Type sizes do not match built-in types" );
1301
1302 types_checked = 1;
1303 }
1304
1305 /******************************************************************************
1306 Get a text line from a file and break it up into words.
1307
1308 IMPORTANT: The calling routine call "free" on the returned pointer once
1309 finished with it.
1310
1311 Entry:
1312 fp - file to read from
1313
1314 Exit:
1315 nwords - number of words returned
1316 orig_line - the original line of characters
1317 returns a list of words from the line, or NULL if end-of-file
1318 ******************************************************************************/
1319
1320 std::vector< std::string > get_words( FILE *fp , char **orig_line )
1321 {
1322 static const unsigned int BigString = 4096;
1323 static char str[BigString];
1324 static char str_copy[BigString];
1325 std::vector< std::string > words;
1326 int max_words = 10;
1327 int num_words = 0;
1328 char *ptr , *ptr2;
1329 char *result;
1330
1331 /* read in a line */
1332 result = fgets( str , BigString , fp );
1333 if( result==NULL )
1334 {
1335 *orig_line = NULL;
1336 return words;
1337 }
1338 /* convert line-feed and tabs into spaces */
1339 /* (this guarentees that there will be a space before the */
1340 /* null character at the end of the string) */
1341
1342 str[BigString-2] = ' ';
1343 str[BigString-1] = '\0';
1344
1345 for( ptr=str , ptr2=str_copy ; *ptr!='\0' ; ptr++ , ptr2++ )
1346 {
1347 *ptr2 = *ptr;
1348 // Added line here to manage carriage returns
1349 if( *ptr == '\t' || *ptr == '\r' )
1350 {
1351 *ptr = ' ';
1352 *ptr2 = ' ';
1353 }
1354 else if( *ptr=='\n' )
1355 {
1356 *ptr = ' ';
1357 *ptr2 = '\0';
1358 break;
1359 }
1360 }
1361
1362 /* find the words in the line */
1363
1364 ptr = str;
1365 while( *ptr!='\0' )
1366 {
1367 /* jump over leading spaces */
1368 while( *ptr==' ' ) ptr++;
1369
1370 /* break if we reach the end */
1371 if( *ptr=='\0' ) break;
1372
1373 char *_ptr = ptr;
1374
1375 /* jump over non-spaces */
1376 while( *ptr!=' ' ) ptr++;
1377
1378 /* place a null character here to mark the end of the word */
1379 *ptr++ = '\0';
1380
1381 /* save pointer to beginning of word */
1382 words.push_back( _ptr );
1383 }
1384
1385 /* return the list of words */
1386 *orig_line = str_copy;
1387 return words;
1388 }
1389
1390 /******************************************************************************
1391 Return the value of an item, given a pointer to it and its type.
1392
1393 Entry:
1394 item - pointer to item
1395 type - data type that "item" points to
1396
1397 Exit:
1398 returns a double-precision float that contains the value of the item
1399 ******************************************************************************/
1400
1401 double get_item_value( const void *item , int type )
1402 {
1403 switch( type )
1404 {
1405 case PLY_CHAR:
1406 case PLY_INT_8: return (double)*(const char *)item;
1407 case PLY_UCHAR:
1408 case PLY_UINT_8: return (double)*(const unsigned char *)item;
1409 case PLY_SHORT:
1410 case PLY_INT_16: return (double)*(const short int *)item;
1411 case PLY_USHORT:
1412 case PLY_UINT_16: return (double)*(const unsigned short int *)item;
1413 case PLY_INT:
1414 case PLY_INT_32: return (double)*(const int *)item;
1415 case PLY_LONGLONG:
1416 case PLY_INT_64: return (double)*(const long long *)item;
1417 case PLY_UINT:
1418 case PLY_UINT_32: return (double)*(const unsigned int *)item;
1419 case PLY_ULONGLONG:
1420 case PLY_UINT_64: return (double)*(const unsigned long long *)item;
1421 case PLY_FLOAT:
1422 case PLY_FLOAT_32: return (double)*(const float *)item;
1423 case PLY_DOUBLE:
1424 case PLY_FLOAT_64: return (double)*(const double *)item;
1425 default: MK_THROW( "Bad type: " , type );
1426 }
1427 return 0;
1428 }
1429
1430
1431 /******************************************************************************
1432 Write out an item to a file as raw binary bytes.
1433
1434 Entry:
1435 fp - file to write to
1436 int_val - integer version of item
1437 uint_val - unsigned integer version of item
1438 double_val - double-precision float version of item
1439 type - data type to write out
1440 ******************************************************************************/
1441
1442 void write_binary_item( FILE *fp , int file_type , int int_val , unsigned int uint_val , long long longlong_val , unsigned long long ulonglong_val , double double_val , int type )
1443 {
1444 unsigned char uchar_val;
1445 char char_val;
1446 unsigned short ushort_val;
1447 short short_val;
1448 float float_val;
1449 void *value = NULL;
1450
1451 switch (type) {
1452 case PLY_CHAR:
1453 case PLY_INT_8:
1454 char_val = char(int_val);
1455 value = &char_val;
1456 break;
1457 case PLY_SHORT:
1458 case PLY_INT_16:
1459 short_val = short(int_val);
1460 value = &short_val;
1461 break;
1462 case PLY_INT:
1463 case PLY_INT_32:
1464 value = &int_val;
1465 break;
1466 case PLY_LONGLONG:
1467 case PLY_INT_64:
1468 value = &longlong_val;
1469 break;
1470 case PLY_UCHAR:
1471 case PLY_UINT_8:
1472 uchar_val = (unsigned char)(uint_val);
1473 value = &uchar_val;
1474 break;
1475 case PLY_USHORT:
1476 case PLY_UINT_16:
1477 ushort_val = (unsigned short)(uint_val);
1478 value = &ushort_val;
1479 break;
1480 case PLY_UINT:
1481 case PLY_UINT_32:
1482 value = &uint_val;
1483 break;
1484 case PLY_ULONGLONG:
1485 case PLY_UINT_64:
1486 value = &ulonglong_val;
1487 break;
1488 case PLY_FLOAT:
1489 case PLY_FLOAT_32:
1490 float_val = (float)double_val;
1491 value = &float_val;
1492 break;
1493 case PLY_DOUBLE:
1494 case PLY_FLOAT_64:
1495 value = &double_val;
1496 break;
1497 default: MK_THROW( "Bad type: " , type );
1498 }
1499
1500
1501 if( (file_type!=native_binary_type) && (ply_type_size[type]>1) ) swap_bytes( (char *)value , ply_type_size[type] );
1502 if( fwrite( value , ply_type_size[type] , 1 , fp )!=1 ) MK_THROW( "Failed to write binary item" );
1503 }
1504
1505
1506 /******************************************************************************
1507 Write out an item to a file as ascii characters.
1508
1509 Entry:
1510 fp - file to write to
1511 int_val - integer version of item
1512 uint_val - unsigned integer version of item
1513 double_val - double-precision float version of item
1514 type - data type to write out
1515 ******************************************************************************/
1516
1517 void write_ascii_item( FILE *fp , int int_val , unsigned int uint_val , long long longlong_val , unsigned long long ulonglong_val , double double_val , int type )
1518 {
1519 switch (type)
1520 {
1521 case PLY_CHAR:
1522 case PLY_INT_8:
1523 case PLY_SHORT:
1524 case PLY_INT_16:
1525 case PLY_INT:
1526 case PLY_INT_32:
1527 if( fprintf( fp , "%d " , int_val )<=0 ) MK_THROW( "fprintf() failed -- aborting" );
1528 break;
1529 case PLY_LONGLONG:
1530 case PLY_INT_64:
1531 if( fprintf( fp , "%lld " , longlong_val )<=0 ) MK_THROW( "fprintf() failed -- aborting" );
1532 break;
1533 case PLY_UCHAR:
1534 case PLY_UINT_8:
1535 case PLY_USHORT:
1536 case PLY_UINT_16:
1537 case PLY_UINT:
1538 case PLY_UINT_32:
1539 if( fprintf( fp , "%u " , uint_val )<=0 ) MK_THROW( "fprintf() failed -- aborting" );
1540 break;
1541 case PLY_ULONGLONG:
1542 case PLY_UINT_64:
1543 if( fprintf( fp , "%llu " , ulonglong_val )<=0 ) MK_THROW( "fprintf() failed -- aborting" );
1544 break;
1545 case PLY_FLOAT:
1546 case PLY_FLOAT_32:
1547 case PLY_DOUBLE:
1548 case PLY_FLOAT_64:
1549 if( fprintf( fp , "%g " , double_val )<=0 ) MK_THROW( "fprintf() failed -- aborting" );
1550 break;
1551 default: MK_THROW( "Bad type: " , type );
1552 }
1553 }
1554
1555 /******************************************************************************
1556 Get the value of an item that is in memory, and place the result
1557 into an integer, an unsigned integer and a double.
1558
1559 Entry:
1560 ptr - pointer to the item
1561 type - data type supposedly in the item
1562
1563 Exit:
1564 int_val - integer value
1565 uint_val - unsigned integer value
1566 double_val - double-precision floating point value
1567 ******************************************************************************/
1568
1569 void get_stored_item( void *ptr , int type , int &int_val , unsigned int &uint_val , long long &longlong_val , unsigned long long &ulonglong_val , double &double_val )
1570 {
1571 switch( type )
1572 {
1573 case PLY_CHAR:
1574 case PLY_INT_8:
1575 int_val = *((char *) ptr);
1576 uint_val = int_val;
1577 double_val = int_val;
1578 longlong_val = (long long)int_val;
1579 ulonglong_val = (unsigned long long)int_val;
1580 break;
1581 case PLY_UCHAR:
1582 case PLY_UINT_8:
1583 uint_val = *((unsigned char *) ptr);
1584 int_val = uint_val;
1585 double_val = uint_val;
1586 longlong_val = (long long)uint_val;
1587 ulonglong_val = (unsigned long long)uint_val;
1588 break;
1589 case PLY_SHORT:
1590 case PLY_INT_16:
1591 int_val = *((short int *) ptr);
1592 uint_val = int_val;
1593 double_val = int_val;
1594 longlong_val = (long long)int_val;
1595 ulonglong_val = (unsigned long long)int_val;
1596 break;
1597 case PLY_USHORT:
1598 case PLY_UINT_16:
1599 uint_val = *((unsigned short int *) ptr);
1600 int_val = uint_val;
1601 double_val = uint_val;
1602 longlong_val = (long long)uint_val;
1603 ulonglong_val = (unsigned long long)uint_val;
1604 break;
1605 case PLY_INT:
1606 case PLY_INT_32:
1607 int_val = *((int *) ptr);
1608 uint_val = int_val;
1609 double_val = int_val;
1610 longlong_val = (long long)int_val;
1611 ulonglong_val = (unsigned long long)int_val;
1612 break;
1613 case PLY_UINT:
1614 case PLY_UINT_32:
1615 uint_val = *((unsigned int *) ptr);
1616 int_val = uint_val;
1617 double_val = uint_val;
1618 longlong_val = (long long)uint_val;
1619 ulonglong_val = (unsigned long long)uint_val;
1620 break;
1621 case PLY_LONGLONG:
1622 case PLY_INT_64:
1623 longlong_val = *((long long *) ptr);
1624 ulonglong_val = (unsigned long long)longlong_val;
1625 int_val = (int)longlong_val;
1626 uint_val = (unsigned int)longlong_val;
1627 double_val = (double)longlong_val;
1628 break;
1629 case PLY_ULONGLONG:
1630 case PLY_UINT_64:
1631 ulonglong_val = *((unsigned long long *) ptr);
1632 longlong_val = (long long)ulonglong_val;
1633 int_val = (int)ulonglong_val;
1634 uint_val = (unsigned int)ulonglong_val;
1635 double_val = (double)ulonglong_val;
1636 break;
1637 case PLY_FLOAT:
1638 case PLY_FLOAT_32:
1639 double_val = *((float *) ptr);
1640 int_val = (int)double_val;
1641 uint_val = (unsigned int)double_val;
1642 longlong_val = (long long)double_val;
1643 ulonglong_val = (unsigned long long)double_val;
1644 break;
1645 case PLY_DOUBLE:
1646 case PLY_FLOAT_64:
1647 double_val = *((double *) ptr);
1648 int_val = (int)double_val;
1649 uint_val = (unsigned int)double_val;
1650 longlong_val = (long long)double_val;
1651 ulonglong_val = (unsigned long long)double_val;
1652 break;
1653 default: MK_THROW( "Bad type: " , type );
1654 }
1655 }
1656
1657 /******************************************************************************
1658 Get the value of an item from a binary file, and place the result
1659 into an integer, an unsigned integer and a double.
1660
1661 Entry:
1662 fp - file to get item from
1663 type - data type supposedly in the word
1664
1665 Exit:
1666 int_val - integer value
1667 uint_val - unsigned integer value
1668 double_val - double-precision floating point value
1669 ******************************************************************************/
1670
1671 void get_binary_item( FILE *fp , int file_type , int type , int &int_val , unsigned int &uint_val , long long &longlong_val , unsigned long long &ulonglong_val , double &double_val )
1672 {
1673 // Use memcpy instead of type-punning pointer casts to read from the
1674 // buffer. The original code cast char* to int*/float*/etc. which
1675 // violates C++ strict-aliasing rules. At -O3 GCC exploits this and
1676 // may reorder or eliminate the loads, returning garbage.
1677 // Note: -Wstrict-aliasing does not catch this because the cast goes
1678 // through a void* intermediate which defeats the heuristic checker.
1679 //
1680 // For the float/double cases we also avoid direct casts from
1681 // floating-point to unsigned integer types, which are UB when the
1682 // value is negative or out-of-range. Instead we convert through
1683 // long long first (well-defined truncation from double for values
1684 // within ±2^63) and then use the well-defined signed-to-unsigned
1685 // integer conversion.
1686 char c[8];
1687
1688 if( fread( c , ply_type_size[type] , 1 , fp )!=1 ) MK_THROW( "fread() failed -- aborting: " , std::string( type_names[type] ) );
1689 if( ( file_type!=native_binary_type ) && ( ply_type_size[type]>1 ) ) swap_bytes( c , ply_type_size[type] );
1690
1691 switch( type )
1692 {
1693 case PLY_CHAR:
1694 case PLY_INT_8:
1695 int_val = (signed char)c[0];
1696 uint_val = int_val;
1697 longlong_val = int_val;
1698 ulonglong_val = int_val;
1699 double_val = int_val;
1700 break;
1701 case PLY_UCHAR:
1702 case PLY_UINT_8:
1703 uint_val = (unsigned char)c[0];
1704 int_val = uint_val;
1705 longlong_val = int_val;
1706 ulonglong_val = int_val;
1707 double_val = uint_val;
1708 break;
1709 case PLY_SHORT:
1710 case PLY_INT_16:
1711 { short v; memcpy( &v , c , sizeof(v) ); int_val = v; }
1712 uint_val = int_val;
1713 longlong_val = int_val;
1714 ulonglong_val = int_val;
1715 double_val = int_val;
1716 break;
1717 case PLY_USHORT:
1718 case PLY_UINT_16:
1719 { unsigned short v; memcpy( &v , c , sizeof(v) ); uint_val = v; }
1720 int_val = uint_val;
1721 longlong_val = int_val;
1722 ulonglong_val = int_val;
1723 double_val = uint_val;
1724 break;
1725 case PLY_INT:
1726 case PLY_INT_32:
1727 memcpy( &int_val , c , sizeof(int_val) );
1728 uint_val = int_val;
1729 longlong_val = int_val;
1730 ulonglong_val = int_val;
1731 double_val = int_val;
1732 break;
1733 case PLY_UINT:
1734 case PLY_UINT_32:
1735 memcpy( &uint_val , c , sizeof(uint_val) );
1736 int_val = uint_val;
1737 longlong_val = int_val;
1738 ulonglong_val = int_val;
1739 double_val = uint_val;
1740 break;
1741 case PLY_LONGLONG:
1742 case PLY_INT_64:
1743 memcpy( &longlong_val , c , sizeof(longlong_val) );
1744 ulonglong_val = (unsigned long long)longlong_val;
1745 int_val = (int)longlong_val;
1746 uint_val = (unsigned int)longlong_val;
1747 double_val = (double)longlong_val;
1748 break;
1749 case PLY_ULONGLONG:
1750 case PLY_UINT_64:
1751 memcpy( &ulonglong_val , c , sizeof(ulonglong_val) );
1752 longlong_val = (long long)ulonglong_val;
1753 int_val = (int)ulonglong_val;
1754 uint_val = (unsigned int)ulonglong_val;
1755 double_val = (double)ulonglong_val;
1756 break;
1757 case PLY_FLOAT:
1758 case PLY_FLOAT_32:
1759 { float v; memcpy( &v , c , sizeof(v) ); double_val = v; }
1760 // Clamp to avoid undefined behavior with an out of range double.
1761 longlong_val = static_cast<long long>(std::clamp(double_val, static_cast<double>(std::numeric_limits<long long>::min()), static_cast<double>(std::numeric_limits<long long>::max())));
1762 int_val = (int)longlong_val;
1763 uint_val = (unsigned int)longlong_val;
1764 ulonglong_val = (unsigned long long)longlong_val;
1765 break;
1766 case PLY_DOUBLE:
1767 case PLY_FLOAT_64:
1768 memcpy( &double_val , c , sizeof(double_val) );
1769 // Clamp to avoid undefined behavior with an out of range double.
1770 longlong_val = static_cast<long long>(std::clamp(double_val, static_cast<double>(std::numeric_limits<long long>::min()), static_cast<double>(std::numeric_limits<long long>::max())));
1771 int_val = (int)longlong_val;
1772 uint_val = (unsigned int)longlong_val;
1773 ulonglong_val = (unsigned long long)longlong_val;
1774 break;
1775 default: MK_THROW( "Bad type: " , type );
1776 }
1777 }
1778
1779 /******************************************************************************
1780 Extract the value of an item from an ascii word, and place the result
1781 into an integer, an unsigned integer and a double.
1782
1783 Entry:
1784 word - word to extract value from
1785 type - data type supposedly in the word
1786
1787 Exit:
1788 int_val - integer value
1789 uint_val - unsigned integer value
1790 double_val - double-precision floating point value
1791 ******************************************************************************/
1792 void get_ascii_item( const std::string &word , int type , int &int_val , unsigned int &uint_val , long long &longlong_val , unsigned long long &ulonglong_val , double &double_val )
1793 {
1794 switch( type )
1795 {
1796 case PLY_CHAR:
1797 case PLY_INT_8:
1798 case PLY_UCHAR:
1799 case PLY_UINT_8:
1800 case PLY_SHORT:
1801 case PLY_INT_16:
1802 case PLY_USHORT:
1803 case PLY_UINT_16:
1804 case PLY_INT:
1805 case PLY_INT_32:
1806 int_val = atoi( word.c_str() );
1807 uint_val = (unsigned int)int_val;
1808 double_val = (double)int_val;
1809 longlong_val = (long long)int_val;
1810 ulonglong_val = (unsigned long long)int_val;
1811 break;
1812
1813 case PLY_UINT:
1814 case PLY_UINT_32:
1815 uint_val = strtol( word.c_str() , (char **)NULL , 10 );
1816 int_val = (int)uint_val;
1817 double_val = (double)uint_val;
1818 longlong_val = (long long)uint_val;
1819 ulonglong_val = (unsigned long long)uint_val;
1820 break;
1821 case PLY_LONGLONG:
1822 case PLY_INT_64:
1823 longlong_val = std::stoll( word.c_str() );
1824 ulonglong_val = (unsigned long long)longlong_val;
1825 int_val = (int)longlong_val;
1826 uint_val = (unsigned int)longlong_val;
1827 double_val = (double)longlong_val;
1828 break;
1829 case PLY_ULONGLONG:
1830 case PLY_UINT_64:
1831 ulonglong_val = std::stoull( word.c_str() );
1832 longlong_val = (long long)ulonglong_val;
1833 int_val = (int)ulonglong_val;
1834 uint_val = (unsigned int)ulonglong_val;
1835 double_val = (double)ulonglong_val;
1836 break;
1837 case PLY_FLOAT:
1838 case PLY_FLOAT_32:
1839 case PLY_DOUBLE:
1840 case PLY_FLOAT_64:
1841 double_val = atof( word.c_str() );
1842 int_val = (int)double_val;
1843 uint_val = (unsigned int)double_val;
1844 longlong_val = (long long)double_val;
1845 ulonglong_val = (unsigned long long)double_val;
1846 break;
1847 default: MK_THROW( "Bad type: " , type );
1848 }
1849 }
1850
1851 /******************************************************************************
1852 Store a value into a place being pointed to, guided by a data type.
1853
1854 Entry:
1855 item - place to store value
1856 type - data type
1857 int_val - integer version of value
1858 uint_val - unsigned integer version of value
1859 double_val - double version of value
1860
1861 Exit:
1862 item - pointer to stored value
1863 ******************************************************************************/
1864
1865 void store_item( void *item , int type , int int_val , unsigned int uint_val , long long longlong_val , unsigned long long ulonglong_val , double double_val )
1866 {
1867 switch( type )
1868 {
1869 case PLY_CHAR:
1870 case PLY_INT_8: *( char *)item = ( char) int_val ; break;
1871 case PLY_UCHAR:
1872 case PLY_UINT_8: *(unsigned char *)item = (unsigned char) uint_val ; break;
1873 case PLY_SHORT:
1874 case PLY_INT_16: *( short *)item = ( short) int_val ; break;
1875 case PLY_USHORT:
1876 case PLY_UINT_16: *(unsigned short *)item = (unsigned short) uint_val ; break;
1877 case PLY_INT:
1878 case PLY_INT_32: *( int *)item = ( int) int_val ; break;
1879 case PLY_UINT:
1880 case PLY_UINT_32: *(unsigned int *)item = (unsigned int) uint_val ; break;
1881 case PLY_LONGLONG:
1882 case PLY_INT_64: *( long long *)item = ( long long)longlong_val ; break;
1883 case PLY_ULONGLONG:
1884 case PLY_UINT_64: *(unsigned long long *)item = (unsigned long long)ulonglong_val ; break;
1885 case PLY_FLOAT:
1886 case PLY_FLOAT_32: *( float *)item = ( float)double_val ; break;
1887 case PLY_DOUBLE:
1888 case PLY_FLOAT_64: *( double *)item = ( double)double_val ; break;
1889 default: MK_THROW( "Bad type: " , type );
1890 }
1891 }
1892
1893
1894 /******************************************************************************
1895 Add an element to a PLY file descriptor.
1896
1897 Entry:
1898 plyfile - PLY file descriptor
1899 words - list of words describing the element
1900 nwords - number of words in the list
1901 ******************************************************************************/
1902
1903 void PlyFile::add_element( const std::vector< std::string > &words )
1904 {
1905 PlyElement elem;
1906
1907 /* set the new element */
1908 elem.name = words[1];
1909 elem.num = std::stoull( words[2] );
1910 elem.props.resize(0);
1911
1912 /* add the new element to the object's list */
1913 elems.push_back( elem );
1914 }
1915
1916 /******************************************************************************
1917 Return the type of a property, given the name of the property.
1918
1919 Entry:
1920 name - name of property type
1921
1922 Exit:
1923 returns integer code for property, or 0 if not found
1924 ******************************************************************************/
1925
1926 int get_prop_type( const std::string &type_name )
1927 {
1928 for( int i=PLY_START_TYPE+1 ; i<PLY_END_TYPE ; i++ ) if( type_name==type_names[i] ) return i;
1929
1930 /* if we get here, we didn't find the type */
1931 return 0;
1932 }
1933
1934 /******************************************************************************
1935 Add a property to a PLY file descriptor.
1936
1937 Entry:
1938 plyfile - PLY file descriptor
1939 words - list of words describing the property
1940 nwords - number of words in the list
1941 ******************************************************************************/
1942
1943 void PlyFile::add_property( const std::vector< std::string > &words )
1944 {
1945 PlyProperty prop;
1946 if( words[1]=="list" ) /* is a list */
1947 {
1948 prop.count_external = get_prop_type( words[2] );
1949 prop.external_type = get_prop_type( words[3]) ;
1950 prop.name = words[4];
1951 prop.is_list = 1;
1952 }
1953 else /* not a list */
1954 {
1955 prop.external_type = get_prop_type( words[1] );
1956 prop.name = words[2];
1957 prop.is_list = 0;
1958 }
1959
1960 /* add this property to the list of properties of the current element */
1961 elems.back().props.push_back( PlyStoredProperty( prop , dont_store_prop ) );
1962 }
1963
1964
1965 /******************************************************************************
1966 Add a comment to a PLY file descriptor.
1967
1968 Entry:
1969 plyfile - PLY file descriptor
1970 line - line containing comment
1971 ******************************************************************************/
1972
1973 void PlyFile::add_comment( const std::string &line )
1974 {
1975 /* skip over "comment" and leading spaces and tabs */
1976 int i = 7;
1977 while( line[i]==' ' || line[i] =='\t' ) i++;
1978
1979 put_comment( line.substr(i) );
1980 }
1981
1982
1983 /******************************************************************************
1984 Add a some object information to a PLY file descriptor.
1985
1986 Entry:
1987 plyfile - PLY file descriptor
1988 line - line containing text info
1989 ******************************************************************************/
1990
1991 void PlyFile::add_obj_info( const std::string &line )
1992 {
1993 /* skip over "obj_info" and leading spaces and tabs */
1994 int i = 8;
1995 while( line[i]==' ' || line[i]=='\t' ) i++;
1996 put_obj_info( line.substr(i) );
1997 }