| 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 | #include "ply.h" |
|---|
| 42 | #include"typedefs.h" |
|---|
| 43 | |
|---|
| 44 | #include <stdio.h> |
|---|
| 45 | #include <stdlib.h> |
|---|
| 46 | #include <math.h> |
|---|
| 47 | #include <string.h> |
|---|
| 48 | |
|---|
| 49 | #include <osg/Math> |
|---|
| 50 | |
|---|
| 51 | #if defined(_MSC_VER) && defined(OSG_DISABLE_MSVC_WARNINGS) |
|---|
| 52 | #pragma warning( disable : 4996 ) |
|---|
| 53 | #endif |
|---|
| 54 | |
|---|
| 55 | #ifdef WIN32 |
|---|
| 56 | # ifndef LITTLE_ENDIAN |
|---|
| 57 | # define LITTLE_ENDIAN |
|---|
| 58 | # endif |
|---|
| 59 | #endif |
|---|
| 60 | |
|---|
| 61 | const char *type_names[] = { |
|---|
| 62 | "invalid", |
|---|
| 63 | "char", "short", "int", |
|---|
| 64 | "uchar", "ushort", "uint", |
|---|
| 65 | "float", "double", "float32", "uint8", "int32" |
|---|
| 66 | }; |
|---|
| 67 | |
|---|
| 68 | int ply_type_size[] = { |
|---|
| 69 | 0, 1, 2, 4, 1, 2, 4, 4, 8, 4, 1, 4 |
|---|
| 70 | }; |
|---|
| 71 | |
|---|
| 72 | #define NO_OTHER_PROPS -1 |
|---|
| 73 | |
|---|
| 74 | #define DONT_STORE_PROP 0 |
|---|
| 75 | #define STORE_PROP 1 |
|---|
| 76 | |
|---|
| 77 | #define OTHER_PROP 0 |
|---|
| 78 | #define NAMED_PROP 1 |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | int equal_strings(const char *, const char *); |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | PlyElement *find_element(PlyFile *, const char *); |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | PlyProperty *find_property(PlyElement *, const char *, int *); |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | void write_scalar_type (FILE *, int); |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | char **get_words(FILE *, int *, char **); |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | void write_binary_item(PlyFile *, int, unsigned int, double, int); |
|---|
| 98 | void write_ascii_item(FILE *, int, unsigned int, double, int); |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | void add_element(PlyFile *, char **, int); |
|---|
| 102 | void add_property(PlyFile *, char **, int); |
|---|
| 103 | void add_comment(PlyFile *, char *); |
|---|
| 104 | void add_obj_info(PlyFile *, char *); |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | void copy_property(PlyProperty *, PlyProperty *); |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | void store_item(char *, int, int, unsigned int, double); |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | void get_stored_item( void *, int, int *, unsigned int *, double *); |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | double get_item_value(char *, int); |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | void get_ascii_item(char *, int, int *, unsigned int *, double *); |
|---|
| 120 | void get_binary_item(PlyFile *, int, int *, unsigned int *, double *); |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | void ascii_get_element(PlyFile *, char *); |
|---|
| 124 | void binary_get_element(PlyFile *, char *); |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | char *my_alloc(int, int, const char *); |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | void swap2Bytes( void* ptr ) |
|---|
| 134 | { |
|---|
| 135 | unsigned char* bytes = (unsigned char*)ptr; |
|---|
| 136 | unsigned short* result = (unsigned short*)ptr; |
|---|
| 137 | |
|---|
| 138 | *result = (bytes[0]<<8) | bytes[1]; |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | void swap4Bytes( void* ptr ) |
|---|
| 142 | { |
|---|
| 143 | unsigned char* bytes = (unsigned char*)ptr; |
|---|
| 144 | unsigned int* result = (unsigned int*)ptr; |
|---|
| 145 | |
|---|
| 146 | *result = (bytes[0]<<24) | (bytes[1]<<16) | (bytes[2]<<8) | bytes[3]; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | void swap8Bytes( void* ptr ) |
|---|
| 150 | { |
|---|
| 151 | unsigned char* bytes = (unsigned char*)ptr; |
|---|
| 152 | unsigned long long* result = (unsigned long long*)ptr; |
|---|
| 153 | |
|---|
| 154 | *result = ((unsigned long long)(bytes[0])) << 56 | |
|---|
| 155 | ((unsigned long long)(bytes[1])) << 48 | |
|---|
| 156 | ((unsigned long long)(bytes[2])) << 40 | |
|---|
| 157 | ((unsigned long long)(bytes[3])) << 32 | |
|---|
| 158 | ((unsigned long long)(bytes[4])) << 24 | |
|---|
| 159 | ((unsigned long long)(bytes[5])) << 16 | |
|---|
| 160 | ((unsigned long long)(bytes[6])) << 8 | |
|---|
| 161 | bytes[7]; |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | #ifdef LITTLE_ENDIAN |
|---|
| 167 | void swap2LE( void* ) {} |
|---|
| 168 | void swap2LE( short* ) {} |
|---|
| 169 | void swap2LE( unsigned short* ) {} |
|---|
| 170 | void swap4LE( void* ) {} |
|---|
| 171 | void swap4LE( int* ) {} |
|---|
| 172 | void swap4LE( unsigned int* ) {} |
|---|
| 173 | void swap4LE( float* ) {} |
|---|
| 174 | void swap8LE( void* ) {} |
|---|
| 175 | void swap8LE( long long* ) {} |
|---|
| 176 | void swap8LE( unsigned long long* ) {} |
|---|
| 177 | void swap8LE( double* ) {} |
|---|
| 178 | |
|---|
| 179 | void swap2BE( void* ptr ) { swap2Bytes(ptr); } |
|---|
| 180 | void swap2BE( short* ptr ) { swap2Bytes(ptr); } |
|---|
| 181 | void swap2BE( unsigned short* ptr ) { swap2Bytes(ptr); } |
|---|
| 182 | void swap4BE( void* ptr ) { swap4Bytes(ptr); } |
|---|
| 183 | void swap4BE( int* ptr ) { swap4Bytes(ptr); } |
|---|
| 184 | void swap4BE( unsigned int* ptr ) { swap4Bytes(ptr); } |
|---|
| 185 | void swap4BE( float* ptr ) { swap4Bytes(ptr); } |
|---|
| 186 | void swap8BE( long long* ptr ) { swap8Bytes(ptr); } |
|---|
| 187 | void swap8BE( void* ptr ) { swap8Bytes(ptr); } |
|---|
| 188 | void swap8BE( unsigned long long* ptr ) { swap8Bytes(ptr); } |
|---|
| 189 | void swap8BE( double* ptr ) { swap8Bytes(ptr); } |
|---|
| 190 | |
|---|
| 191 | #else // LITTLE_ENDIAN |
|---|
| 192 | |
|---|
| 193 | void swap2LE( void* ptr ) { swap2Bytes(ptr); } |
|---|
| 194 | void swap2LE( short* ptr ) { swap2Bytes(ptr); } |
|---|
| 195 | void swap2LE( unsigned short* ptr ) { swap2Bytes(ptr); } |
|---|
| 196 | void swap4LE( void* ptr ) { swap4Bytes(ptr); } |
|---|
| 197 | void swap4LE( int* ptr ) { swap4Bytes(ptr); } |
|---|
| 198 | void swap4LE( unsigned int* ptr ) { swap4Bytes(ptr); } |
|---|
| 199 | void swap4LE( float* ptr ) { swap4Bytes(ptr); } |
|---|
| 200 | void swap8LE( long long* ptr ) { swap8Bytes(ptr); } |
|---|
| 201 | void swap8LE( void* ptr ) { swap8Bytes(ptr); } |
|---|
| 202 | void swap8LE( unsigned long long* ptr ) { swap8Bytes(ptr); } |
|---|
| 203 | void swap8LE( double* ptr ) { swap8Bytes(ptr); } |
|---|
| 204 | |
|---|
| 205 | void swap2BE( void* ) {} |
|---|
| 206 | void swap2BE( short* ) {} |
|---|
| 207 | void swap2BE( unsigned short* ) {} |
|---|
| 208 | void swap4BE( void* ) {} |
|---|
| 209 | void swap4BE( int* ) {} |
|---|
| 210 | void swap4BE( unsigned int* ) {} |
|---|
| 211 | void swap4BE( float* ) {} |
|---|
| 212 | void swap8BE( void* ) {} |
|---|
| 213 | void swap8BE( long long* ) {} |
|---|
| 214 | void swap8BE( unsigned long long* ) {} |
|---|
| 215 | void swap8BE( double* ) {} |
|---|
| 216 | |
|---|
| 217 | #endif // LITTLE_ENDIAN |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | |
|---|
| 231 | |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | PlyFile *ply_write( |
|---|
| 239 | FILE *fp, |
|---|
| 240 | int nelems, |
|---|
| 241 | const char **elem_names, |
|---|
| 242 | int file_type |
|---|
| 243 | ) |
|---|
| 244 | { |
|---|
| 245 | int i; |
|---|
| 246 | PlyFile *plyfile; |
|---|
| 247 | PlyElement *elem; |
|---|
| 248 | |
|---|
| 249 | |
|---|
| 250 | if (fp == NULL) |
|---|
| 251 | return (NULL); |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | plyfile = (PlyFile *) myalloc (sizeof (PlyFile)); |
|---|
| 256 | plyfile->file_type = file_type; |
|---|
| 257 | plyfile->num_comments = 0; |
|---|
| 258 | plyfile->num_obj_info = 0; |
|---|
| 259 | plyfile->nelems = nelems; |
|---|
| 260 | plyfile->version = 1.0; |
|---|
| 261 | plyfile->fp = fp; |
|---|
| 262 | plyfile->other_elems = NULL; |
|---|
| 263 | |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | plyfile->elems = (PlyElement **) myalloc (sizeof (PlyElement *) * nelems); |
|---|
| 267 | for (i = 0; i < nelems; i++) { |
|---|
| 268 | elem = (PlyElement *) myalloc (sizeof (PlyElement)); |
|---|
| 269 | plyfile->elems[i] = elem; |
|---|
| 270 | elem->name = strdup (elem_names[i]); |
|---|
| 271 | elem->num = 0; |
|---|
| 272 | elem->nprops = 0; |
|---|
| 273 | } |
|---|
| 274 | |
|---|
| 275 | |
|---|
| 276 | return (plyfile); |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | |
|---|
| 283 | |
|---|
| 284 | |
|---|
| 285 | |
|---|
| 286 | |
|---|
| 287 | |
|---|
| 288 | |
|---|
| 289 | |
|---|
| 290 | |
|---|
| 291 | |
|---|
| 292 | |
|---|
| 293 | |
|---|
| 294 | PlyFile *ply_open_for_writing( |
|---|
| 295 | char *filename, |
|---|
| 296 | int nelems, |
|---|
| 297 | const char **elem_names, |
|---|
| 298 | int file_type, |
|---|
| 299 | float *version |
|---|
| 300 | ) |
|---|
| 301 | { |
|---|
| 302 | PlyFile *plyfile; |
|---|
| 303 | char *name; |
|---|
| 304 | FILE *fp; |
|---|
| 305 | |
|---|
| 306 | |
|---|
| 307 | |
|---|
| 308 | name = (char *) myalloc (sizeof (char) * |
|---|
| 309 | (static_cast<int>(strlen (filename)) + 5)); |
|---|
| 310 | strcpy (name, filename); |
|---|
| 311 | if (strlen (name) < 4 || |
|---|
| 312 | strcmp (name + strlen (name) - 4, ".ply") != 0) |
|---|
| 313 | strcat (name, ".ply"); |
|---|
| 314 | |
|---|
| 315 | |
|---|
| 316 | |
|---|
| 317 | fp = fopen (name, "wb"); |
|---|
| 318 | free (name); |
|---|
| 319 | if (fp == NULL) { |
|---|
| 320 | return (NULL); |
|---|
| 321 | } |
|---|
| 322 | |
|---|
| 323 | |
|---|
| 324 | |
|---|
| 325 | plyfile = ply_write (fp, nelems, elem_names, file_type); |
|---|
| 326 | |
|---|
| 327 | |
|---|
| 328 | if (plyfile == NULL) |
|---|
| 329 | return (NULL); |
|---|
| 330 | |
|---|
| 331 | |
|---|
| 332 | *version = plyfile->version; |
|---|
| 333 | |
|---|
| 334 | |
|---|
| 335 | return (plyfile); |
|---|
| 336 | } |
|---|
| 337 | |
|---|
| 338 | |
|---|
| 339 | |
|---|
| 340 | |
|---|
| 341 | |
|---|
| 342 | |
|---|
| 343 | |
|---|
| 344 | |
|---|
| 345 | |
|---|
| 346 | |
|---|
| 347 | |
|---|
| 348 | |
|---|
| 349 | |
|---|
| 350 | |
|---|
| 351 | void ply_describe_element( |
|---|
| 352 | PlyFile *plyfile, |
|---|
| 353 | const char *elem_name, |
|---|
| 354 | int nelems, |
|---|
| 355 | int nprops, |
|---|
| 356 | PlyProperty *prop_list |
|---|
| 357 | ) |
|---|
| 358 | { |
|---|
| 359 | int i; |
|---|
| 360 | PlyElement *elem; |
|---|
| 361 | PlyProperty *prop; |
|---|
| 362 | |
|---|
| 363 | |
|---|
| 364 | elem = find_element (plyfile, elem_name); |
|---|
| 365 | if (elem == NULL) { |
|---|
| 366 | char error[100]; |
|---|
| 367 | sprintf (error, "ply_describe_element: can't find element '%s'\n",elem_name); |
|---|
| 368 | throw ply::MeshException( error ); |
|---|
| 369 | } |
|---|
| 370 | |
|---|
| 371 | elem->num = nelems; |
|---|
| 372 | |
|---|
| 373 | |
|---|
| 374 | |
|---|
| 375 | elem->nprops = nprops; |
|---|
| 376 | elem->props = (PlyProperty **) myalloc (sizeof (PlyProperty *) * nprops); |
|---|
| 377 | elem->store_prop = (char *) myalloc (sizeof (char) * nprops); |
|---|
| 378 | |
|---|
| 379 | for (i = 0; i < nprops; i++) { |
|---|
| 380 | prop = (PlyProperty *) myalloc (sizeof (PlyProperty)); |
|---|
| 381 | elem->props[i] = prop; |
|---|
| 382 | elem->store_prop[i] = NAMED_PROP; |
|---|
| 383 | copy_property (prop, &prop_list[i]); |
|---|
| 384 | } |
|---|
| 385 | } |
|---|
| 386 | |
|---|
| 387 | |
|---|
| 388 | |
|---|
| 389 | |
|---|
| 390 | |
|---|
| 391 | |
|---|
| 392 | |
|---|
| 393 | |
|---|
| 394 | |
|---|
| 395 | |
|---|
| 396 | |
|---|
| 397 | void ply_describe_property( |
|---|
| 398 | PlyFile *plyfile, |
|---|
| 399 | const char *elem_name, |
|---|
| 400 | PlyProperty *prop |
|---|
| 401 | ) |
|---|
| 402 | { |
|---|
| 403 | PlyElement *elem; |
|---|
| 404 | PlyProperty *elem_prop; |
|---|
| 405 | |
|---|
| 406 | |
|---|
| 407 | elem = find_element (plyfile, elem_name); |
|---|
| 408 | if (elem == NULL) { |
|---|
| 409 | fprintf(stderr, "ply_describe_property: can't find element '%s'\n", |
|---|
| 410 | elem_name); |
|---|
| 411 | return; |
|---|
| 412 | } |
|---|
| 413 | |
|---|
| 414 | |
|---|
| 415 | |
|---|
| 416 | if (elem->nprops == 0) { |
|---|
| 417 | elem->props = (PlyProperty **) myalloc (sizeof (PlyProperty *)); |
|---|
| 418 | elem->store_prop = (char *) myalloc (sizeof (char)); |
|---|
| 419 | elem->nprops = 1; |
|---|
| 420 | } |
|---|
| 421 | else { |
|---|
| 422 | elem->nprops++; |
|---|
| 423 | elem->props = (PlyProperty **) |
|---|
| 424 | realloc (elem->props, sizeof (PlyProperty *) * elem->nprops); |
|---|
| 425 | elem->store_prop = (char *) |
|---|
| 426 | realloc (elem->store_prop, sizeof (char) * elem->nprops); |
|---|
| 427 | } |
|---|
| 428 | |
|---|
| 429 | |
|---|
| 430 | elem->other_offset = 0; |
|---|
| 431 | elem_prop = (PlyProperty *) myalloc (sizeof (PlyProperty)); |
|---|
| 432 | elem->props[elem->nprops - 1] = elem_prop; |
|---|
| 433 | elem->store_prop[elem->nprops - 1] = NAMED_PROP; |
|---|
| 434 | copy_property (elem_prop, prop); |
|---|
| 435 | } |
|---|
| 436 | |
|---|
| 437 | |
|---|
| 438 | |
|---|
| 439 | |
|---|
| 440 | |
|---|
| 441 | |
|---|
| 442 | |
|---|
| 443 | void ply_describe_other_properties( |
|---|
| 444 | PlyFile *plyfile, |
|---|
| 445 | PlyOtherProp *other, |
|---|
| 446 | int offset |
|---|
| 447 | ) |
|---|
| 448 | { |
|---|
| 449 | int i; |
|---|
| 450 | PlyElement *elem; |
|---|
| 451 | PlyProperty *prop; |
|---|
| 452 | |
|---|
| 453 | |
|---|
| 454 | elem = find_element (plyfile, other->name); |
|---|
| 455 | if (elem == NULL) { |
|---|
| 456 | fprintf(stderr, "ply_describe_other_properties: can't find element '%s'\n", |
|---|
| 457 | other->name); |
|---|
| 458 | return; |
|---|
| 459 | } |
|---|
| 460 | |
|---|
| 461 | |
|---|
| 462 | |
|---|
| 463 | if (elem->nprops == 0) { |
|---|
| 464 | elem->props = (PlyProperty **) |
|---|
| 465 | myalloc (sizeof (PlyProperty *) * other->nprops); |
|---|
| 466 | elem->store_prop = (char *) myalloc (sizeof (char) * other->nprops); |
|---|
| 467 | elem->nprops = 0; |
|---|
| 468 | } |
|---|
| 469 | else { |
|---|
| 470 | int newsize; |
|---|
| 471 | newsize = elem->nprops + other->nprops; |
|---|
| 472 | elem->props = (PlyProperty **) |
|---|
| 473 | realloc (elem->props, sizeof (PlyProperty *) * newsize); |
|---|
| 474 | elem->store_prop = (char *) |
|---|
| 475 | realloc (elem->store_prop, sizeof (char) * newsize); |
|---|
| 476 | } |
|---|
| 477 | |
|---|
| 478 | |
|---|
| 479 | |
|---|
| 480 | for (i = 0; i < other->nprops; i++) { |
|---|
| 481 | prop = (PlyProperty *) myalloc (sizeof (PlyProperty)); |
|---|
| 482 | copy_property (prop, other->props[i]); |
|---|
| 483 | elem->props[elem->nprops] = prop; |
|---|
| 484 | elem->store_prop[elem->nprops] = OTHER_PROP; |
|---|
| 485 | elem->nprops++; |
|---|
| 486 | } |
|---|
| 487 | |
|---|
| 488 | |
|---|
| 489 | elem->other_size = other->size; |
|---|
| 490 | elem->other_offset = offset; |
|---|
| 491 | } |
|---|
| 492 | |
|---|
| 493 | |
|---|
| 494 | |
|---|
| 495 | |
|---|
| 496 | |
|---|
| 497 | |
|---|
| 498 | |
|---|
| 499 | |
|---|
| 500 | |
|---|
| 501 | |
|---|
| 502 | |
|---|
| 503 | void ply_element_count( |
|---|
| 504 | PlyFile *plyfile, |
|---|
| 505 | const char *elem_name, |
|---|
| 506 | int nelems |
|---|
| 507 | ) |
|---|
| 508 | { |
|---|
| 509 | PlyElement *elem; |
|---|
| 510 | |
|---|
| 511 | |
|---|
| 512 | elem = find_element (plyfile, elem_name); |
|---|
| 513 | if (elem == NULL) { |
|---|
| 514 | char error[100]; |
|---|
| 515 | sprintf (error, "ply_element_count: can't find element '%s'\n",elem_name); |
|---|
| 516 | throw ply::MeshException( error ); |
|---|
| 517 | } |
|---|
| 518 | |
|---|
| 519 | elem->num = nelems; |
|---|
| 520 | } |
|---|
| 521 | |
|---|
| 522 | |
|---|
| 523 | |
|---|
| 524 | |
|---|
| 525 | |
|---|
| 526 | |
|---|
| 527 | |
|---|
| 528 | |
|---|
| 529 | |
|---|
| 530 | |
|---|
| 531 | void ply_header_complete(PlyFile *plyfile) |
|---|
| 532 | { |
|---|
| 533 | int i,j; |
|---|
| 534 | FILE *fp = plyfile->fp; |
|---|
| 535 | PlyElement *elem; |
|---|
| 536 | PlyProperty *prop; |
|---|
| 537 | |
|---|
| 538 | fprintf (fp, "ply\n"); |
|---|
| 539 | |
|---|
| 540 | switch (plyfile->file_type) { |
|---|
| 541 | case PLY_ASCII: |
|---|
| 542 | fprintf (fp, "format ascii 1.0\n"); |
|---|
| 543 | break; |
|---|
| 544 | case PLY_BINARY_BE: |
|---|
| 545 | fprintf (fp, "format binary_big_endian 1.0\n"); |
|---|
| 546 | break; |
|---|
| 547 | case PLY_BINARY_LE: |
|---|
| 548 | fprintf (fp, "format binary_little_endian 1.0\n"); |
|---|
| 549 | break; |
|---|
| 550 | default: |
|---|
| 551 | char error[100]; |
|---|
| 552 | sprintf (error, "ply_header_complete: bad file type = %d\n", |
|---|
| 553 | plyfile->file_type); |
|---|
| 554 | throw ply::MeshException( error ); |
|---|
| 555 | } |
|---|
| 556 | |
|---|
| 557 | |
|---|
| 558 | |
|---|
| 559 | for (i = 0; i < plyfile->num_comments; i++) |
|---|
| 560 | fprintf (fp, "comment %s\n", plyfile->comments[i]); |
|---|
| 561 | |
|---|
| 562 | |
|---|
| 563 | |
|---|
| 564 | for (i = 0; i < plyfile->num_obj_info; i++) |
|---|
| 565 | fprintf (fp, "obj_info %s\n", plyfile->obj_info[i]); |
|---|
| 566 | |
|---|
| 567 | |
|---|
| 568 | |
|---|
| 569 | for (i = 0; i < plyfile->nelems; i++) { |
|---|
| 570 | |
|---|
| 571 | elem = plyfile->elems[i]; |
|---|
| 572 | fprintf (fp, "element %s %d\n", elem->name, elem->num); |
|---|
| 573 | |
|---|
| 574 | |
|---|
| 575 | for (j = 0; j < elem->nprops; j++) { |
|---|
| 576 | prop = elem->props[j]; |
|---|
| 577 | if (prop->is_list) { |
|---|
| 578 | fprintf (fp, "property list "); |
|---|
| 579 | write_scalar_type (fp, prop->count_external); |
|---|
| 580 | fprintf (fp, " "); |
|---|
| 581 | write_scalar_type (fp, prop->external_type); |
|---|
| 582 | fprintf (fp, " %s\n", prop->name); |
|---|
| 583 | } |
|---|
| 584 | else { |
|---|
| 585 | fprintf (fp, "property "); |
|---|
| 586 | write_scalar_type (fp, prop->external_type); |
|---|
| 587 | fprintf (fp, " %s\n", prop->name); |
|---|
| 588 | } |
|---|
| 589 | } |
|---|
| 590 | } |
|---|
| 591 | |
|---|
| 592 | fprintf (fp, "end_header\n"); |
|---|
| 593 | } |
|---|
| 594 | |
|---|
| 595 | |
|---|
| 596 | |
|---|
| 597 | |
|---|
| 598 | |
|---|
| 599 | |
|---|
| 600 | |
|---|
| 601 | |
|---|
| 602 | |
|---|
| 603 | |
|---|
| 604 | |
|---|
| 605 | void ply_put_element_setup(PlyFile *plyfile, const char *elem_name) |
|---|
| 606 | { |
|---|
| 607 | PlyElement *elem; |
|---|
| 608 | |
|---|
| 609 | elem = find_element (plyfile, elem_name); |
|---|
| 610 | if (elem == NULL) { |
|---|
| 611 | char error[100]; |
|---|
| 612 | sprintf (error, "ply_elements_setup: can't find element '%s'\n", elem_name); |
|---|
| 613 | throw ply::MeshException( error ); |
|---|
| 614 | } |
|---|
| 615 | |
|---|
| 616 | plyfile->which_elem = elem; |
|---|
| 617 | } |
|---|
| 618 | |
|---|
| 619 | |
|---|
| 620 | |
|---|
| 621 | |
|---|
| 622 | |
|---|
| 623 | |
|---|
| 624 | |
|---|
| 625 | |
|---|
| 626 | |
|---|
| 627 | |
|---|
| 628 | |
|---|
| 629 | |
|---|
| 630 | void ply_put_element(PlyFile *plyfile, void *elem_ptr) |
|---|
| 631 | { |
|---|
| 632 | int j, k; |
|---|
| 633 | FILE *fp = plyfile->fp; |
|---|
| 634 | PlyElement *elem; |
|---|
| 635 | PlyProperty *prop; |
|---|
| 636 | char *elem_data,*item; |
|---|
| 637 | char **item_ptr; |
|---|
| 638 | int list_count; |
|---|
| 639 | int item_size; |
|---|
| 640 | int int_val; |
|---|
| 641 | unsigned int uint_val; |
|---|
| 642 | double double_val; |
|---|
| 643 | char **other_ptr; |
|---|
| 644 | |
|---|
| 645 | elem = plyfile->which_elem; |
|---|
| 646 | elem_data = (char *)elem_ptr; |
|---|
| 647 | other_ptr = (char **) (((char *) elem_ptr) + elem->other_offset); |
|---|
| 648 | |
|---|
| 649 | |
|---|
| 650 | |
|---|
| 651 | if (plyfile->file_type == PLY_ASCII) { |
|---|
| 652 | |
|---|
| 653 | |
|---|
| 654 | |
|---|
| 655 | |
|---|
| 656 | for (j = 0; j < elem->nprops; j++) { |
|---|
| 657 | prop = elem->props[j]; |
|---|
| 658 | if (elem->store_prop[j] == OTHER_PROP) |
|---|
| 659 | elem_data = *other_ptr; |
|---|
| 660 | else |
|---|
| 661 | elem_data = (char *)elem_ptr; |
|---|
| 662 | if (prop->is_list) { |
|---|
| 663 | item = elem_data + prop->count_offset; |
|---|
| 664 | get_stored_item ((void *) item, prop->count_internal, |
|---|
| 665 | &int_val, &uint_val, &double_val); |
|---|
| 666 | write_ascii_item (fp, int_val, uint_val, double_val, |
|---|
| 667 | prop->count_external); |
|---|
| 668 | list_count = uint_val; |
|---|
| 669 | item_ptr = (char **) (elem_data + prop->offset); |
|---|
| 670 | item = item_ptr[0]; |
|---|
| 671 | item_size = ply_type_size[prop->internal_type]; |
|---|
| 672 | for (k = 0; k < list_count; k++) { |
|---|
| 673 | get_stored_item ((void *) item, prop->internal_type, |
|---|
| 674 | &int_val, &uint_val, &double_val); |
|---|
| 675 | write_ascii_item (fp, int_val, uint_val, double_val, |
|---|
| 676 | prop->external_type); |
|---|
| 677 | item += item_size; |
|---|
| 678 | } |
|---|
| 679 | } |
|---|
| 680 | else { |
|---|
| 681 | item = elem_data + prop->offset; |
|---|
| 682 | get_stored_item ((void *) item, prop->internal_type, |
|---|
| 683 | &int_val, &uint_val, &double_val); |
|---|
| 684 | write_ascii_item (fp, int_val, uint_val, double_val, |
|---|
| 685 | prop->external_type); |
|---|
| 686 | } |
|---|
| 687 | } |
|---|
| 688 | |
|---|
| 689 | fprintf (fp, "\n"); |
|---|
| 690 | } |
|---|
| 691 | else { |
|---|
| 692 | |
|---|
| 693 | |
|---|
| 694 | |
|---|
| 695 | |
|---|
| 696 | for (j = 0; j < elem->nprops; j++) { |
|---|
| 697 | prop = elem->props[j]; |
|---|
| 698 | if (elem->store_prop[j] == OTHER_PROP) |
|---|
| 699 | elem_data = *other_ptr; |
|---|
| 700 | else |
|---|
| 701 | elem_data = (char *)elem_ptr; |
|---|
| 702 | if (prop->is_list) { |
|---|
| 703 | item = elem_data + prop->count_offset; |
|---|
| 704 | item_size = ply_type_size[prop->count_internal]; |
|---|
| 705 | get_stored_item ((void *) item, prop->count_internal, |
|---|
| 706 | &int_val, &uint_val, &double_val); |
|---|
| 707 | write_binary_item (plyfile, int_val, uint_val, double_val, |
|---|
| 708 | prop->count_external); |
|---|
| 709 | list_count = uint_val; |
|---|
| 710 | item_ptr = (char **) (elem_data + prop->offset); |
|---|
| 711 | item = item_ptr[0]; |
|---|
| 712 | item_size = ply_type_size[prop->internal_type]; |
|---|
| 713 | for (k = 0; k < list_count; k++) { |
|---|
| 714 | get_stored_item ((void *) item, prop->internal_type, |
|---|
| 715 | &int_val, &uint_val, &double_val); |
|---|
| 716 | write_binary_item (plyfile, int_val, uint_val, double_val, |
|---|
| 717 | prop->external_type); |
|---|
| 718 | item += item_size; |
|---|
| 719 | } |
|---|
| 720 | } |
|---|
| 721 | else { |
|---|
| 722 | item = elem_data + prop->offset; |
|---|
| 723 | item_size = ply_type_size[prop->internal_type]; |
|---|
| 724 | get_stored_item ((void *) item, prop->internal_type, |
|---|
| 725 | &int_val, &uint_val, &double_val); |
|---|
| 726 | write_binary_item (plyfile, int_val, uint_val, double_val, |
|---|
| 727 | prop->external_type); |
|---|
| 728 | } |
|---|
| 729 | } |
|---|
| 730 | |
|---|
| 731 | } |
|---|
| 732 | } |
|---|
| 733 | |
|---|
| 734 | |
|---|
| 735 | |
|---|
| 736 | |
|---|
| 737 | |
|---|
| 738 | |
|---|
| 739 | |
|---|
| 740 | |
|---|
| 741 | |
|---|
| 742 | |
|---|
| 743 | void ply_put_comment(PlyFile *plyfile, const char *comment) |
|---|
| 744 | { |
|---|
| 745 | |
|---|
| 746 | if (plyfile->num_comments == 0) |
|---|
| 747 | { |
|---|
| 748 | plyfile->comments = (char **) myalloc (sizeof (char *)); |
|---|
| 749 | } |
|---|
| 750 | else |
|---|
| 751 | { |
|---|
| 752 | plyfile->comments = (char **) realloc (plyfile->comments, |
|---|
| 753 | sizeof (char *) * (plyfile->num_comments + 1)); |
|---|
| 754 | } |
|---|
| 755 | |
|---|
| 756 | |
|---|
| 757 | plyfile->comments[plyfile->num_comments] = strdup (comment); |
|---|
| 758 | plyfile->num_comments++; |
|---|
| 759 | } |
|---|
| 760 | |
|---|
| 761 | |
|---|
| 762 | |
|---|
| 763 | |
|---|
| 764 | |
|---|
| 765 | |
|---|
| 766 | |
|---|
| 767 | |
|---|
| 768 | |
|---|
| 769 | |
|---|
| 770 | |
|---|
| 771 | void ply_put_obj_info(PlyFile *plyfile, const char *obj_info) |
|---|
| 772 | { |
|---|
| 773 | |
|---|
| 774 | if (plyfile->num_obj_info == 0) |
|---|
| 775 | { |
|---|
| 776 | plyfile->obj_info = (char **) myalloc (sizeof (char *)); |
|---|
| 777 | } |
|---|
| 778 | else |
|---|
| 779 | { |
|---|
| 780 | plyfile->obj_info = (char **) realloc (plyfile->obj_info, |
|---|
| 781 | sizeof (char *) * (plyfile->num_obj_info + 1)); |
|---|
| 782 | } |
|---|
| 783 | |
|---|
| 784 | |
|---|
| 785 | plyfile->obj_info[plyfile->num_obj_info] = strdup (obj_info); |
|---|
| 786 | plyfile->num_obj_info++; |
|---|
| 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 | PlyFile *ply_read(FILE *fp, int *nelems, char ***elem_names) |
|---|
| 814 | { |
|---|
| 815 | int i,j; |
|---|
| 816 | PlyFile *plyfile; |
|---|
| 817 | int nwords; |
|---|
| 818 | char **words; |
|---|
| 819 | char **elist; |
|---|
| 820 | PlyElement *elem; |
|---|
| 821 | char *orig_line; |
|---|
| 822 | |
|---|
| 823 | |
|---|
| 824 | if (fp == NULL) |
|---|
| 825 | return (NULL); |
|---|
| 826 | |
|---|
| 827 | |
|---|
| 828 | |
|---|
| 829 | plyfile = (PlyFile *) myalloc (sizeof (PlyFile)); |
|---|
| 830 | plyfile->nelems = 0; |
|---|
| 831 | plyfile->comments = NULL; |
|---|
| 832 | plyfile->num_comments = 0; |
|---|
| 833 | plyfile->obj_info = NULL; |
|---|
| 834 | plyfile->num_obj_info = 0; |
|---|
| 835 | plyfile->fp = fp; |
|---|
| 836 | plyfile->other_elems = NULL; |
|---|
| 837 | |
|---|
| 838 | |
|---|
| 839 | |
|---|
| 840 | words = get_words (plyfile->fp, &nwords, &orig_line); |
|---|
| 841 | if (!words || !equal_strings (words[0], "ply")) |
|---|
| 842 | return (NULL); |
|---|
| 843 | |
|---|
| 844 | while (words) { |
|---|
| 845 | |
|---|
| 846 | |
|---|
| 847 | |
|---|
| 848 | if (equal_strings (words[0], "format")) { |
|---|
| 849 | if (nwords != 3) |
|---|
| 850 | return (NULL); |
|---|
| 851 | if (equal_strings (words[1], "ascii")) |
|---|
| 852 | plyfile->file_type = PLY_ASCII; |
|---|
| 853 | else if (equal_strings (words[1], "binary_big_endian")) |
|---|
| 854 | plyfile->file_type = PLY_BINARY_BE; |
|---|
| 855 | else if (equal_strings (words[1], "binary_little_endian")) |
|---|
| 856 | plyfile->file_type = PLY_BINARY_LE; |
|---|
| 857 | else |
|---|
| 858 | { |
|---|
| 859 | free (words); |
|---|
| 860 | return (NULL); |
|---|
| 861 | } |
|---|
| 862 | plyfile->version = osg::asciiToDouble (words[2]); |
|---|
| 863 | } |
|---|
| 864 | else if (equal_strings (words[0], "element")) |
|---|
| 865 | add_element (plyfile, words, nwords); |
|---|
| 866 | else if (equal_strings (words[0], "property")) |
|---|
| 867 | add_property (plyfile, words, nwords); |
|---|
| 868 | else if (equal_strings (words[0], "comment")) |
|---|
| 869 | add_comment (plyfile, orig_line); |
|---|
| 870 | else if (equal_strings (words[0], "obj_info")) |
|---|
| 871 | add_obj_info (plyfile, orig_line); |
|---|
| 872 | else if (equal_strings (words[0], "end_header")) |
|---|
| 873 | { |
|---|
| 874 | free (words); |
|---|
| 875 | break; |
|---|
| 876 | } |
|---|
| 877 | |
|---|
| 878 | |
|---|
| 879 | free (words); |
|---|
| 880 | |
|---|
| 881 | words = get_words (plyfile->fp, &nwords, &orig_line); |
|---|
| 882 | } |
|---|
| 883 | |
|---|
| 884 | |
|---|
| 885 | |
|---|
| 886 | |
|---|
| 887 | |
|---|
| 888 | for (i = 0; i < plyfile->nelems; i++) { |
|---|
| 889 | elem = plyfile->elems[i]; |
|---|
| 890 | elem->store_prop = (char *) myalloc (sizeof (char) * elem->nprops); |
|---|
| 891 | for (j = 0; j < elem->nprops; j++) |
|---|
| 892 | elem->store_prop[j] = DONT_STORE_PROP; |
|---|
| 893 | elem->other_offset = NO_OTHER_PROPS; |
|---|
| 894 | } |
|---|
| 895 | |
|---|
| 896 | |
|---|
| 897 | |
|---|
| 898 | elist = (char **) myalloc (sizeof (char *) * plyfile->nelems); |
|---|
| 899 | for (i = 0; i < plyfile->nelems; i++) |
|---|
| 900 | elist[i] = strdup (plyfile->elems[i]->name); |
|---|
| 901 | |
|---|
| 902 | *elem_names = elist; |
|---|
| 903 | *nelems = plyfile->nelems; |
|---|
| 904 | |
|---|
| 905 | |
|---|
| 906 | |
|---|
| 907 | return (plyfile); |
|---|
| 908 | } |
|---|
| 909 | |
|---|
| 910 | |
|---|
| 911 | |
|---|
| 912 | |
|---|
| 913 | |
|---|
| 914 | |
|---|
| 915 | |
|---|
| 916 | |
|---|
| 917 | |
|---|
| 918 | |
|---|
| 919 | |
|---|
| 920 | |
|---|
| 921 | |
|---|
| 922 | |
|---|
| 923 | |
|---|
| 924 | |
|---|
| 925 | PlyFile *ply_open_for_reading( |
|---|
| 926 | char *filename, |
|---|
| 927 | int *nelems, |
|---|
| 928 | char ***elem_names, |
|---|
| 929 | int *file_type, |
|---|
| 930 | float *version |
|---|
| 931 | ) |
|---|
| 932 | { |
|---|
| 933 | FILE *fp; |
|---|
| 934 | PlyFile *plyfile; |
|---|
| 935 | char *name; |
|---|
| 936 | |
|---|
| 937 | |
|---|
| 938 | |
|---|
| 939 | name = (char *) myalloc (sizeof (char) * |
|---|
| 940 | (static_cast<int>(strlen (filename) + 5))); |
|---|
| 941 | strcpy (name, filename); |
|---|
| 942 | if (strlen (name) < 4 || |
|---|
| 943 | strcmp (name + strlen (name) - 4, ".ply") != 0) |
|---|
| 944 | strcat (name, ".ply"); |
|---|
| 945 | |
|---|
| 946 | |
|---|
| 947 | |
|---|
| 948 | fp = fopen (name, "rb"); |
|---|
| 949 | free(name); |
|---|
| 950 | if (fp == NULL) |
|---|
| 951 | return (NULL); |
|---|
| 952 | |
|---|
| 953 | |
|---|
| 954 | |
|---|
| 955 | plyfile = ply_read (fp, nelems, elem_names); |
|---|
| 956 | |
|---|
| 957 | if(!plyfile) |
|---|
| 958 | { |
|---|
| 959 | std::cout<<"Ply File Error : Could not read file"<<std::endl; |
|---|
| 960 | return NULL; |
|---|
| 961 | } |
|---|
| 962 | |
|---|
| 963 | |
|---|
| 964 | |
|---|
| 965 | *file_type = plyfile->file_type; |
|---|
| 966 | *version = plyfile->version; |
|---|
| 967 | |
|---|
| 968 | |
|---|
| 969 | |
|---|
| 970 | return (plyfile); |
|---|
| 971 | } |
|---|
| 972 | |
|---|
| 973 | |
|---|
| 974 | |
|---|
| 975 | |
|---|
| 976 | |
|---|
| 977 | |
|---|
| 978 | |
|---|
| 979 | |
|---|
| 980 | |
|---|
| 981 | |
|---|
| 982 | |
|---|
| 983 | |
|---|
| 984 | |
|---|
| 985 | |
|---|
| 986 | |
|---|
| 987 | PlyProperty **ply_get_element_description( |
|---|
| 988 | PlyFile *plyfile, |
|---|
| 989 | char *elem_name, |
|---|
| 990 | int *nelems, |
|---|
| 991 | int *nprops |
|---|
| 992 | ) |
|---|
| 993 | { |
|---|
| 994 | int i; |
|---|
| 995 | PlyElement *elem; |
|---|
| 996 | PlyProperty *prop; |
|---|
| 997 | PlyProperty **prop_list; |
|---|
| 998 | |
|---|
| 999 | |
|---|
| 1000 | elem = find_element (plyfile, elem_name); |
|---|
| 1001 | if (elem == NULL) |
|---|
| 1002 | return (NULL); |
|---|
| 1003 | |
|---|
| 1004 | *nelems = elem->num; |
|---|
| 1005 | *nprops = elem->nprops; |
|---|
| 1006 | |
|---|
| 1007 | |
|---|
| 1008 | prop_list = (PlyProperty **) myalloc (sizeof (PlyProperty *) * elem->nprops); |
|---|
| 1009 | for (i = 0; i < elem->nprops; i++) { |
|---|
| 1010 | prop = (PlyProperty *) myalloc (sizeof (PlyProperty)); |
|---|
| 1011 | copy_property (prop, elem->props[i]); |
|---|
| 1012 | prop_list[i] = prop; |
|---|
| 1013 | } |
|---|
| 1014 | |
|---|
| 1015 | |
|---|
| 1016 | return (prop_list); |
|---|
| 1017 | } |
|---|
| 1018 | |
|---|
| 1019 | |
|---|
| 1020 | |
|---|
| 1021 | |
|---|
| 1022 | |
|---|
| 1023 | |
|---|
| 1024 | |
|---|
| 1025 | |
|---|
| 1026 | |
|---|
| 1027 | |
|---|
| 1028 | |
|---|
| 1029 | |
|---|
| 1030 | |
|---|
| 1031 | void ply_get_element_setup( PlyFile *plyfile, char *elem_name, int nprops, |
|---|
| 1032 | PlyProperty *prop_list ) |
|---|
| 1033 | { |
|---|
| 1034 | int i; |
|---|
| 1035 | PlyElement *elem; |
|---|
| 1036 | PlyProperty *prop; |
|---|
| 1037 | int index; |
|---|
| 1038 | |
|---|
| 1039 | |
|---|
| 1040 | elem = find_element (plyfile, elem_name); |
|---|
| 1041 | plyfile->which_elem = elem; |
|---|
| 1042 | |
|---|
| 1043 | |
|---|
| 1044 | for (i = 0; i < nprops; i++) |
|---|
| 1045 | { |
|---|
| 1046 | |
|---|
| 1047 | prop = find_property (elem, prop_list[i].name, &index); |
|---|
| 1048 | if (prop == NULL) |
|---|
| 1049 | { |
|---|
| 1050 | fprintf ( stderr, |
|---|
| 1051 | "Warning: Can't find property '%s' in element '%s'\n", |
|---|
| 1052 | prop_list[i].name, elem_name ); |
|---|
| 1053 | continue; |
|---|
| 1054 | } |
|---|
| 1055 | |
|---|
| 1056 | |
|---|
| 1057 | prop->internal_type = prop_list[i].internal_type; |
|---|
| 1058 | prop->offset = prop_list[i].offset; |
|---|
| 1059 | prop->count_internal = prop_list[i].count_internal; |
|---|
| 1060 | prop->count_offset = prop_list[i].count_offset; |
|---|
| 1061 | |
|---|
| 1062 | |
|---|
| 1063 | elem->store_prop[index] = STORE_PROP; |
|---|
| 1064 | } |
|---|
| 1065 | } |
|---|
| 1066 | |
|---|
| 1067 | |
|---|
| 1068 | |
|---|
| 1069 | |
|---|
| 1070 | |
|---|
| 1071 | |
|---|
| 1072 | |
|---|
| 1073 | |
|---|
| 1074 | |
|---|
| 1075 | |
|---|
| 1076 | |
|---|
| 1077 | |
|---|
| 1078 | |
|---|
| 1079 | |
|---|
| 1080 | void ply_get_property( |
|---|
| 1081 | PlyFile *plyfile, |
|---|
| 1082 | const char *elem_name, |
|---|
| 1083 | PlyProperty *prop |
|---|
| 1084 | ) |
|---|
| 1085 | { |
|---|
| 1086 | PlyElement *elem; |
|---|
| 1087 | PlyProperty *prop_ptr; |
|---|
| 1088 | int index; |
|---|
| 1089 | |
|---|
| 1090 | |
|---|
| 1091 | elem = find_element (plyfile, elem_name); |
|---|
| 1092 | plyfile->which_elem = elem; |
|---|
| 1093 | |
|---|
| 1094 | |
|---|
| 1095 | |
|---|
| 1096 | prop_ptr = find_property (elem, prop->name, &index); |
|---|
| 1097 | if (prop_ptr == NULL) { |
|---|
| 1098 | fprintf (stderr, "Warning: Can't find property '%s' in element '%s'\n", |
|---|
| 1099 | prop->name, elem_name); |
|---|
| 1100 | return; |
|---|
| 1101 | } |
|---|
| 1102 | prop_ptr->internal_type = prop->internal_type; |
|---|
| 1103 | prop_ptr->offset = prop->offset; |
|---|
| 1104 | prop_ptr->count_internal = prop->count_internal; |
|---|
| 1105 | prop_ptr->count_offset = prop->count_offset; |
|---|
| 1106 | |
|---|
| 1107 | |
|---|
| 1108 | elem->store_prop[index] = STORE_PROP; |
|---|
| 1109 | } |
|---|
| 1110 | |
|---|
| 1111 | |
|---|
| 1112 | |
|---|
| 1113 | |
|---|
| 1114 | |
|---|
| 1115 | |
|---|
| 1116 | |
|---|
| 1117 | |
|---|
| 1118 | |
|---|
| 1119 | |
|---|
| 1120 | |
|---|
| 1121 | |
|---|
| 1122 | void ply_get_element(PlyFile *plyfile, void *elem_ptr) |
|---|
| 1123 | { |
|---|
| 1124 | if (plyfile->file_type == PLY_ASCII) |
|---|
| 1125 | ascii_get_element (plyfile, (char *) elem_ptr); |
|---|
| 1126 | else |
|---|
| 1127 | binary_get_element (plyfile, (char *) elem_ptr); |
|---|
| 1128 | } |
|---|
| 1129 | |
|---|
| 1130 | |
|---|
| 1131 | |
|---|
| 1132 | |
|---|
| 1133 | |
|---|
| 1134 | |
|---|
| 1135 | |
|---|
| 1136 | |
|---|
| 1137 | |
|---|
| 1138 | |
|---|
| 1139 | |
|---|
| 1140 | |
|---|
| 1141 | |
|---|
| 1142 | char **ply_get_comments(PlyFile *plyfile, int *num_comments) |
|---|
| 1143 | { |
|---|
| 1144 | *num_comments = plyfile->num_comments; |
|---|
| 1145 | return (plyfile->comments); |
|---|
| 1146 | } |
|---|
| 1147 | |
|---|
| 1148 | |
|---|
| 1149 | |
|---|
| 1150 | |
|---|
| 1151 | |
|---|
| 1152 | |
|---|
| 1153 | |
|---|
| 1154 | |
|---|
| 1155 | |
|---|
| 1156 | |
|---|
| 1157 | |
|---|
| 1158 | |
|---|
| 1159 | |
|---|
| 1160 | |
|---|
| 1161 | char **ply_get_obj_info(PlyFile *plyfile, int *num_obj_info) |
|---|
| 1162 | { |
|---|
| 1163 | *num_obj_info = plyfile->num_obj_info; |
|---|
| 1164 | return (plyfile->obj_info); |
|---|
| 1165 | } |
|---|
| 1166 | |
|---|
| 1167 | |
|---|
| 1168 | |
|---|
| 1169 | |
|---|
| 1170 | |
|---|
| 1171 | |
|---|
| 1172 | |
|---|
| 1173 | |
|---|
| 1174 | |
|---|
| 1175 | |
|---|
| 1176 | |
|---|
| 1177 | |
|---|
| 1178 | |
|---|
| 1179 | void setup_other_props(PlyFile *, PlyElement *elem) |
|---|
| 1180 | { |
|---|
| 1181 | int i; |
|---|
| 1182 | PlyProperty *prop; |
|---|
| 1183 | int size = 0; |
|---|
| 1184 | int type_size; |
|---|
| 1185 | |
|---|
| 1186 | |
|---|
| 1187 | |
|---|
| 1188 | |
|---|
| 1189 | |
|---|
| 1190 | for (type_size = 8; type_size > 0; type_size /= 2) { |
|---|
| 1191 | |
|---|
| 1192 | |
|---|
| 1193 | |
|---|
| 1194 | |
|---|
| 1195 | for (i = 0; i < elem->nprops; i++) { |
|---|
| 1196 | |
|---|
| 1197 | |
|---|
| 1198 | if (elem->store_prop[i]) |
|---|
| 1199 | continue; |
|---|
| 1200 | |
|---|
| 1201 | prop = elem->props[i]; |
|---|
| 1202 | |
|---|
| 1203 | |
|---|
| 1204 | prop->internal_type = prop->external_type; |
|---|
| 1205 | prop->count_internal = prop->count_external; |
|---|
| 1206 | |
|---|
| 1207 | |
|---|
| 1208 | if (prop->is_list) { |
|---|
| 1209 | |
|---|
| 1210 | |
|---|
| 1211 | if (type_size == sizeof (void *)) { |
|---|
| 1212 | prop->offset = size; |
|---|
| 1213 | size += sizeof (void *); |
|---|
| 1214 | } |
|---|
| 1215 | |
|---|
| 1216 | |
|---|
| 1217 | if (type_size == ply_type_size[prop->count_external]) { |
|---|
| 1218 | prop->count_offset = size; |
|---|
| 1219 | size += ply_type_size[prop->count_external]; |
|---|
| 1220 | } |
|---|
| 1221 | } |
|---|
| 1222 | |
|---|
| 1223 | else if (type_size == ply_type_size[prop->external_type]) { |
|---|
| 1224 | prop->offset = size; |
|---|
| 1225 | size += ply_type_size[prop->external_type]; |
|---|
| 1226 | } |
|---|
| 1227 | } |
|---|
| 1228 | |
|---|
| 1229 | } |
|---|
| 1230 | |
|---|
| 1231 | |
|---|
| 1232 | elem->other_size = size; |
|---|
| 1233 | } |
|---|
| 1234 | |
|---|
| 1235 | |
|---|
| 1236 | |
|---|
| 1237 | |
|---|
| 1238 | |
|---|
| 1239 | |
|---|
| 1240 | |
|---|
| 1241 | |
|---|
| 1242 | |
|---|
| 1243 | |
|---|
| 1244 | |
|---|
| 1245 | |
|---|
| 1246 | |
|---|
| 1247 | |
|---|
| 1248 | |
|---|
| 1249 | |
|---|
| 1250 | PlyOtherProp *ply_get_other_properties( |
|---|
| 1251 | PlyFile *plyfile, |
|---|
| 1252 | char *elem_name, |
|---|
| 1253 | int offset |
|---|
| 1254 | ) |
|---|
| 1255 | { |
|---|
| 1256 | int i; |
|---|
| 1257 | PlyElement *elem; |
|---|
| 1258 | PlyOtherProp *other; |
|---|
| 1259 | PlyProperty *prop; |
|---|
| 1260 | int nprops; |
|---|
| 1261 | |
|---|
| 1262 | |
|---|
| 1263 | elem = find_element (plyfile, elem_name); |
|---|
| 1264 | if (elem == NULL) { |
|---|
| 1265 | fprintf (stderr, "ply_get_other_properties: Can't find element '%s'\n", |
|---|
| 1266 | elem_name); |
|---|
| 1267 | return (NULL); |
|---|
| 1268 | } |
|---|
| 1269 | |
|---|
| 1270 | |
|---|
| 1271 | plyfile->which_elem = elem; |
|---|
| 1272 | |
|---|
| 1273 | |
|---|
| 1274 | elem->other_offset = offset; |
|---|
| 1275 | |
|---|
| 1276 | |
|---|
| 1277 | setup_other_props (plyfile, elem); |
|---|
| 1278 | |
|---|
| 1279 | |
|---|
| 1280 | other = (PlyOtherProp *) myalloc (sizeof (PlyOtherProp)); |
|---|
| 1281 | other->name = strdup (elem_name); |
|---|
| 1282 | #if 0 |
|---|
| 1283 | if (elem->other_offset == NO_OTHER_PROPS) { |
|---|
| 1284 | other->size = 0; |
|---|
| 1285 | other->props = NULL; |
|---|
| 1286 | other->nprops = 0; |
|---|
| 1287 | return (other); |
|---|
| 1288 | } |
|---|
| 1289 | #endif |
|---|
| 1290 | other->size = elem->other_size; |
|---|
| 1291 | other->props = (PlyProperty **) myalloc (sizeof(PlyProperty) * elem->nprops); |
|---|
| 1292 | |
|---|
| 1293 | |
|---|
| 1294 | nprops = 0; |
|---|
| 1295 | for (i = 0; i < elem->nprops; i++) { |
|---|
| 1296 | if (elem->store_prop[i]) |
|---|
| 1297 | continue; |
|---|
| 1298 | prop = (PlyProperty *) myalloc (sizeof (PlyProperty)); |
|---|
| 1299 | copy_property (prop, elem->props[i]); |
|---|
| 1300 | other->props[nprops] = prop; |
|---|
| 1301 | nprops++; |
|---|
| 1302 | } |
|---|
| 1303 | other->nprops = nprops; |
|---|
| 1304 | |
|---|
| 1305 | #if 1 |
|---|
| 1306 | |
|---|
| 1307 | if (other->nprops == 0) { |
|---|
| 1308 | elem->other_offset = NO_OTHER_PROPS; |
|---|
| 1309 | } |
|---|
| 1310 | #endif |
|---|
| 1311 | |
|---|
| 1312 | |
|---|
| 1313 | return (other); |
|---|
| 1314 | } |
|---|
| 1315 | |
|---|
| 1316 | |
|---|
| 1317 | |
|---|
| 1318 | |
|---|
| 1319 | |
|---|
| 1320 | |
|---|
| 1321 | |
|---|
| 1322 | |
|---|
| 1323 | |
|---|
| 1324 | |
|---|
| 1325 | |
|---|
| 1326 | |
|---|
| 1327 | |
|---|
| 1328 | |
|---|
| 1329 | |
|---|
| 1330 | |
|---|
| 1331 | |
|---|
| 1332 | |
|---|
| 1333 | |
|---|
| 1334 | |
|---|
| 1335 | |
|---|
| 1336 | |
|---|
| 1337 | |
|---|
| 1338 | |
|---|
| 1339 | PlyOtherElems *ply_get_other_element ( |
|---|
| 1340 | PlyFile *plyfile, |
|---|
| 1341 | char *elem_name, |
|---|
| 1342 | int elem_count |
|---|
| 1343 | ) |
|---|
| 1344 | { |
|---|
| 1345 | int i; |
|---|
| 1346 | PlyElement *elem; |
|---|
| 1347 | PlyOtherElems *other_elems; |
|---|
| 1348 | OtherElem *other; |
|---|
| 1349 | |
|---|
| 1350 | |
|---|
| 1351 | elem = find_element (plyfile, elem_name); |
|---|
| 1352 | if (elem == NULL) { |
|---|
| 1353 | char error[100]; |
|---|
| 1354 | sprintf (error, "ply_get_other_element: can't find element '%s'\n", elem_name); |
|---|
| 1355 | throw ply::MeshException( error ); |
|---|
| 1356 | } |
|---|
| 1357 | |
|---|
| 1358 | |
|---|
| 1359 | |
|---|
| 1360 | |
|---|
| 1361 | if (plyfile->other_elems == NULL) { |
|---|
| 1362 | plyfile->other_elems = (PlyOtherElems *) myalloc (sizeof (PlyOtherElems)); |
|---|
| 1363 | other_elems = plyfile->other_elems; |
|---|
| 1364 | other_elems->other_list = (OtherElem *) myalloc (sizeof (OtherElem)); |
|---|
| 1365 | other = &(other_elems->other_list[0]); |
|---|
| 1366 | other_elems->num_elems = 1; |
|---|
| 1367 | } |
|---|
| 1368 | else { |
|---|
| 1369 | other_elems = plyfile->other_elems; |
|---|
| 1370 | other_elems->other_list = (OtherElem *) realloc (other_elems->other_list, |
|---|
| 1371 | sizeof (OtherElem) * other_elems->num_elems + 1); |
|---|
| 1372 | other = &(other_elems->other_list[other_elems->num_elems]); |
|---|
| 1373 | other_elems->num_elems++; |
|---|
| 1374 | } |
|---|
| 1375 | |
|---|
| 1376 | |
|---|
| 1377 | other->elem_count = elem_count; |
|---|
| 1378 | |
|---|
| 1379 | |
|---|
| 1380 | other->elem_name = strdup (elem_name); |
|---|
| 1381 | |
|---|
| 1382 | |
|---|
| 1383 | other->other_data = (OtherData **) |
|---|
| 1384 | malloc (sizeof (OtherData *) * other->elem_count); |
|---|
| 1385 | |
|---|
| 1386 | |
|---|
| 1387 | other->other_props = ply_get_other_properties (plyfile, elem_name, |
|---|
| 1388 | offsetof(OtherData,other_props)); |
|---|
| 1389 | |
|---|
| 1390 | |
|---|
| 1391 | for (i = 0; i < other->elem_count; i++) { |
|---|
| 1392 | |
|---|
| 1393 | other->other_data[i] = (OtherData *) malloc (sizeof (OtherData)); |
|---|
| 1394 | ply_get_element (plyfile, (void *) other->other_data[i]); |
|---|
| 1395 | } |
|---|
| 1396 | |
|---|
| 1397 | |
|---|
| 1398 | return (other_elems); |
|---|
| 1399 | } |
|---|
| 1400 | |
|---|
| 1401 | |
|---|
| 1402 | |
|---|
| 1403 | |
|---|
| 1404 | |
|---|
| 1405 | |
|---|
| 1406 | |
|---|
| 1407 | |
|---|
| 1408 | |
|---|
| 1409 | |
|---|
| 1410 | |
|---|
| 1411 | void ply_describe_other_elements ( |
|---|
| 1412 | PlyFile *plyfile, |
|---|
| 1413 | PlyOtherElems *other_elems |
|---|
| 1414 | ) |
|---|
| 1415 | { |
|---|
| 1416 | int i; |
|---|
| 1417 | OtherElem *other; |
|---|
| 1418 | |
|---|
| 1419 | |
|---|
| 1420 | if (other_elems == NULL) |
|---|
| 1421 | return; |
|---|
| 1422 | |
|---|
| 1423 | |
|---|
| 1424 | plyfile->other_elems = other_elems; |
|---|
| 1425 | |
|---|
| 1426 | |
|---|
| 1427 | |
|---|
| 1428 | for (i = 0; i < other_elems->num_elems; i++) { |
|---|
| 1429 | other = &(other_elems->other_list[i]); |
|---|
| 1430 | ply_element_count (plyfile, other->elem_name, other->elem_count); |
|---|
| 1431 | ply_describe_other_properties (plyfile, other->other_props, |
|---|
| 1432 | offsetof(OtherData,other_props)); |
|---|
| 1433 | } |
|---|
| 1434 | } |
|---|
| 1435 | |
|---|
| 1436 | |
|---|
| 1437 | |
|---|
| 1438 | |
|---|
| 1439 | |
|---|
| 1440 | |
|---|
| 1441 | |
|---|
| 1442 | |
|---|
| 1443 | |
|---|
| 1444 | void ply_put_other_elements (PlyFile *plyfile) |
|---|
| 1445 | { |
|---|
| 1446 | int i,j; |
|---|
| 1447 | OtherElem *other; |
|---|
| 1448 | |
|---|
| 1449 | |
|---|
| 1450 | if (plyfile->other_elems == NULL) |
|---|
| 1451 | return; |
|---|
| 1452 | |
|---|
| 1453 | |
|---|
| 1454 | |
|---|
| 1455 | for (i = 0; i < plyfile->other_elems->num_elems; i++) { |
|---|
| 1456 | |
|---|
| 1457 | other = &(plyfile->other_elems->other_list[i]); |
|---|
| 1458 | ply_put_element_setup (plyfile, other->elem_name); |
|---|
| 1459 | |
|---|
| 1460 | |
|---|
| 1461 | for (j = 0; j < other->elem_count; j++) |
|---|
| 1462 | ply_put_element (plyfile, (void *) other->other_data[j]); |
|---|
| 1463 | } |
|---|
| 1464 | } |
|---|
| 1465 | |
|---|
| 1466 | |
|---|
| 1467 | |
|---|
| 1468 | |
|---|
| 1469 | |
|---|
| 1470 | |
|---|
| 1471 | |
|---|
| 1472 | |
|---|
| 1473 | |
|---|
| 1474 | void ply_free_other_elements (PlyOtherElems *) |
|---|
| 1475 | { |
|---|
| 1476 | |
|---|
| 1477 | } |
|---|
| 1478 | |
|---|
| 1479 | |
|---|
| 1480 | |
|---|
| 1481 | |
|---|
| 1482 | |
|---|
| 1483 | |
|---|
| 1484 | |
|---|
| 1485 | |
|---|
| 1486 | |
|---|
| 1487 | |
|---|
| 1488 | |
|---|
| 1489 | |
|---|
| 1490 | |
|---|
| 1491 | |
|---|
| 1492 | |
|---|
| 1493 | |
|---|
| 1494 | void ply_close(PlyFile *plyfile) |
|---|
| 1495 | { |
|---|
| 1496 | |
|---|
| 1497 | |
|---|
| 1498 | |
|---|
| 1499 | fclose (plyfile->fp); |
|---|
| 1500 | |
|---|
| 1501 | int i, j; |
|---|
| 1502 | PlyElement *elem; |
|---|
| 1503 | for (i=0; i<plyfile->nelems; i++) |
|---|
| 1504 | { |
|---|
| 1505 | elem = plyfile->elems[i]; |
|---|
| 1506 | if ( elem->name ) {free(elem->name);} |
|---|
| 1507 | for (j=0; j<elem->nprops; j++) |
|---|
| 1508 | { |
|---|
| 1509 | if ( elem->props[j]->name ) {free(const_cast<char *>(elem->props[j]->name));} |
|---|
| 1510 | free (elem->props[j]); |
|---|
| 1511 | } |
|---|
| 1512 | free (elem->props); |
|---|
| 1513 | free (elem->store_prop); |
|---|
| 1514 | free (elem); |
|---|
| 1515 | } |
|---|
| 1516 | free(plyfile->elems); |
|---|
| 1517 | |
|---|
| 1518 | for (i=0; i<plyfile->num_comments; i++) |
|---|
| 1519 | { |
|---|
| 1520 | free (plyfile->comments[i]); |
|---|
| 1521 | } |
|---|
| 1522 | free (plyfile->comments); |
|---|
| 1523 | |
|---|
| 1524 | for (i=0; i<plyfile->num_obj_info; i++) |
|---|
| 1525 | { |
|---|
| 1526 | free (plyfile->obj_info[i]); |
|---|
| 1527 | } |
|---|
| 1528 | free (plyfile->obj_info); |
|---|
| 1529 | |
|---|
| 1530 | free (plyfile); |
|---|
| 1531 | } |
|---|
| 1532 | |
|---|
| 1533 | |
|---|
| 1534 | |
|---|
| 1535 | |
|---|
| 1536 | |
|---|
| 1537 | |
|---|
| 1538 | |
|---|
| 1539 | |
|---|
| 1540 | |
|---|
| 1541 | |
|---|
| 1542 | |
|---|
| 1543 | |
|---|
| 1544 | |
|---|
| 1545 | void ply_get_info(PlyFile *ply, float *version, int *file_type) |
|---|
| 1546 | { |
|---|
| 1547 | if (ply == NULL) |
|---|
| 1548 | return; |
|---|
| 1549 | |
|---|
| 1550 | *version = ply->version; |
|---|
| 1551 | *file_type = ply->file_type; |
|---|
| 1552 | } |
|---|
| 1553 | |
|---|
| 1554 | |
|---|
| 1555 | |
|---|
| 1556 | |
|---|
| 1557 | |
|---|
| 1558 | |
|---|
| 1559 | int equal_strings(const char *s1, const char *s2) |
|---|
| 1560 | { |
|---|
| 1561 | while (*s1 && *s2) |
|---|
| 1562 | if (*s1++ != *s2++) |
|---|
| 1563 | return (0); |
|---|
| 1564 | |
|---|
| 1565 | if (*s1 != *s2) |
|---|
| 1566 | return (0); |
|---|
| 1567 | else |
|---|
| 1568 | return (1); |
|---|
| 1569 | } |
|---|
| 1570 | |
|---|
| 1571 | |
|---|
| 1572 | |
|---|
| 1573 | |
|---|
| 1574 | |
|---|
| 1575 | |
|---|
| 1576 | |
|---|
| 1577 | |
|---|
| 1578 | |
|---|
| 1579 | |
|---|
| 1580 | |
|---|
| 1581 | |
|---|
| 1582 | |
|---|
| 1583 | PlyElement *find_element(PlyFile *plyfile, const char *element) |
|---|
| 1584 | { |
|---|
| 1585 | int i; |
|---|
| 1586 | |
|---|
| 1587 | for (i = 0; i < plyfile->nelems; i++) |
|---|
| 1588 | if (equal_strings (element, plyfile->elems[i]->name)) |
|---|
| 1589 | return (plyfile->elems[i]); |
|---|
| 1590 | |
|---|
| 1591 | return (NULL); |
|---|
| 1592 | } |
|---|
| 1593 | |
|---|
| 1594 | |
|---|
| 1595 | |
|---|
| 1596 | |
|---|
| 1597 | |
|---|
| 1598 | |
|---|
| 1599 | |
|---|
| 1600 | |
|---|
| 1601 | |
|---|
| 1602 | |
|---|
| 1603 | |
|---|
| 1604 | |
|---|
| 1605 | |
|---|
| 1606 | |
|---|
| 1607 | PlyProperty *find_property(PlyElement *elem, const char *prop_name, int *index) |
|---|
| 1608 | { |
|---|
| 1609 | int i; |
|---|
| 1610 | |
|---|
| 1611 | for( i = 0; i < elem->nprops; i++) |
|---|
| 1612 | if (equal_strings (prop_name, elem->props[i]->name)) |
|---|
| 1613 | { |
|---|
| 1614 | *index = i; |
|---|
| 1615 | return (elem->props[i]); |
|---|
| 1616 | } |
|---|
| 1617 | |
|---|
| 1618 | *index = -1; |
|---|
| 1619 | return (NULL); |
|---|
| 1620 | } |
|---|
| 1621 | |
|---|
| 1622 | |
|---|
| 1623 | |
|---|
| 1624 | |
|---|
| 1625 | |
|---|
| 1626 | |
|---|
| 1627 | |
|---|
| 1628 | |
|---|
| 1629 | |
|---|
| 1630 | |
|---|
| 1631 | void ascii_get_element(PlyFile *plyfile, char *elem_ptr) |
|---|
| 1632 | { |
|---|
| 1633 | int j,k; |
|---|
| 1634 | PlyElement *elem; |
|---|
| 1635 | PlyProperty *prop; |
|---|
| 1636 | char **words; |
|---|
| 1637 | int nwords; |
|---|
| 1638 | int which_word; |
|---|
| 1639 | char *elem_data,*item=0; |
|---|
| 1640 | char *item_ptr; |
|---|
| 1641 | int item_size=0; |
|---|
| 1642 | int int_val; |
|---|
| 1643 | unsigned int uint_val; |
|---|
| 1644 | double double_val; |
|---|
| 1645 | int list_count; |
|---|
| 1646 | int store_it; |
|---|
| 1647 | char **store_array; |
|---|
| 1648 | char *orig_line; |
|---|
| 1649 | char *other_data=0; |
|---|
| 1650 | int other_flag; |
|---|
| 1651 | |
|---|
| 1652 | |
|---|
| 1653 | elem = plyfile->which_elem; |
|---|
| 1654 | |
|---|
| 1655 | |
|---|
| 1656 | |
|---|
| 1657 | if (elem->other_offset != NO_OTHER_PROPS) { |
|---|
| 1658 | char **ptr; |
|---|
| 1659 | other_flag = 1; |
|---|
| 1660 | |
|---|
| 1661 | other_data = (char *) myalloc (elem->other_size); |
|---|
| 1662 | |
|---|
| 1663 | ptr = (char **) (elem_ptr + elem->other_offset); |
|---|
| 1664 | *ptr = other_data; |
|---|
| 1665 | } |
|---|
| 1666 | else |
|---|
| 1667 | other_flag = 0; |
|---|
| 1668 | |
|---|
| 1669 | |
|---|
| 1670 | |
|---|
| 1671 | words = get_words (plyfile->fp, &nwords, &orig_line); |
|---|
| 1672 | if (words == NULL) { |
|---|
| 1673 | char error[100]; |
|---|
| 1674 | sprintf (error, "ply_get_element: unexpected end of file\n"); |
|---|
| 1675 | throw ply::MeshException( error ); |
|---|
| 1676 | } |
|---|
| 1677 | |
|---|
| 1678 | which_word = 0; |
|---|
| 1679 | |
|---|
| 1680 | for (j = 0; j < elem->nprops; j++) { |
|---|
| 1681 | |
|---|
| 1682 | prop = elem->props[j]; |
|---|
| 1683 | store_it = (elem->store_prop[j] | other_flag); |
|---|
| 1684 | |
|---|
| 1685 | |
|---|
| 1686 | if (elem->store_prop[j]) |
|---|
| 1687 | elem_data = elem_ptr; |
|---|
| 1688 | else |
|---|
| 1689 | elem_data = other_data; |
|---|
| 1690 | |
|---|
| 1691 | if (prop->is_list) { |
|---|
| 1692 | |
|---|
| 1693 | |
|---|
| 1694 | get_ascii_item (words[which_word++], prop->count_external, |
|---|
| 1695 | &int_val, &uint_val, &double_val); |
|---|
| 1696 | if (store_it) { |
|---|
| 1697 | item = elem_data + prop->count_offset; |
|---|
| 1698 | store_item(item, prop->count_internal, int_val, uint_val, double_val); |
|---|
| 1699 | } |
|---|
| 1700 | |
|---|
| 1701 | |
|---|
| 1702 | list_count = int_val; |
|---|
| 1703 | item_size = ply_type_size[prop->internal_type]; |
|---|
| 1704 | store_array = (char **) (elem_data + prop->offset); |
|---|
| 1705 | |
|---|
| 1706 | if (list_count == 0) { |
|---|
| 1707 | if (store_it) |
|---|
| 1708 | *store_array = NULL; |
|---|
| 1709 | } |
|---|
| 1710 | else { |
|---|
| 1711 | if (store_it) { |
|---|
| 1712 | item_ptr = (char *) myalloc (sizeof (char) * item_size * list_count); |
|---|
| 1713 | item = item_ptr; |
|---|
| 1714 | *store_array = item_ptr; |
|---|
| 1715 | } |
|---|
| 1716 | |
|---|
| 1717 | |
|---|
| 1718 | for (k = 0; k < list_count; k++) { |
|---|
| 1719 | get_ascii_item (words[which_word++], prop->external_type, |
|---|
| 1720 | &int_val, &uint_val, &double_val); |
|---|
| 1721 | if (store_it) { |
|---|
| 1722 | store_item (item, prop->internal_type, |
|---|
| 1723 | int_val, uint_val, double_val); |
|---|
| 1724 | item += item_size; |
|---|
| 1725 | } |
|---|
| 1726 | } |
|---|
| 1727 | } |
|---|
| 1728 | |
|---|
| 1729 | } |
|---|
| 1730 | else { |
|---|
| 1731 | get_ascii_item (words[which_word++], prop->external_type, |
|---|
| 1732 | &int_val, &uint_val, &double_val); |
|---|
| 1733 | if (store_it) { |
|---|
| 1734 | item = elem_data + prop->offset; |
|---|
| 1735 | store_item (item, prop->internal_type, int_val, uint_val, double_val); |
|---|
| 1736 | } |
|---|
| 1737 | } |
|---|
| 1738 | |
|---|
| 1739 | } |
|---|
| 1740 | |
|---|
| 1741 | free (words); |
|---|
| 1742 | } |
|---|
| 1743 | |
|---|
| 1744 | |
|---|
| 1745 | |
|---|
| 1746 | |
|---|
| 1747 | |
|---|
| 1748 | |
|---|
| 1749 | |
|---|
| 1750 | |
|---|
| 1751 | |
|---|
| 1752 | |
|---|
| 1753 | void binary_get_element(PlyFile *plyfile, char *elem_ptr) |
|---|
| 1754 | { |
|---|
| 1755 | int j,k; |
|---|
| 1756 | PlyElement *elem; |
|---|
| 1757 | PlyProperty *prop; |
|---|
| 1758 | |
|---|
| 1759 | char *elem_data,*item=0; |
|---|
| 1760 | char *item_ptr; |
|---|
| 1761 | int item_size=0; |
|---|
| 1762 | int int_val; |
|---|
| 1763 | unsigned int uint_val; |
|---|
| 1764 | double double_val; |
|---|
| 1765 | int list_count; |
|---|
| 1766 | int store_it; |
|---|
| 1767 | char **store_array; |
|---|
| 1768 | char *other_data=0; |
|---|
| 1769 | int other_flag; |
|---|
| 1770 | |
|---|
| 1771 | |
|---|
| 1772 | elem = plyfile->which_elem; |
|---|
| 1773 | |
|---|
| 1774 | |
|---|
| 1775 | |
|---|
| 1776 | if (elem->other_offset != NO_OTHER_PROPS) { |
|---|
| 1777 | char **ptr; |
|---|
| 1778 | other_flag = 1; |
|---|
| 1779 | |
|---|
| 1780 | other_data = (char *) myalloc (elem->other_size); |
|---|
| 1781 | |
|---|
| 1782 | ptr = (char **) (elem_ptr + elem->other_offset); |
|---|
| 1783 | *ptr = other_data; |
|---|
| 1784 | } |
|---|
| 1785 | else |
|---|
| 1786 | other_flag = 0; |
|---|
| 1787 | |
|---|
| 1788 | |
|---|
| 1789 | |
|---|
| 1790 | for (j = 0; j < elem->nprops; j++) { |
|---|
| 1791 | |
|---|
| 1792 | prop = elem->props[j]; |
|---|
| 1793 | store_it = (elem->store_prop[j] | other_flag); |
|---|
| 1794 | |
|---|
| 1795 | |
|---|
| 1796 | if (elem->store_prop[j]) |
|---|
| 1797 | elem_data = elem_ptr; |
|---|
| 1798 | else |
|---|
| 1799 | elem_data = other_data; |
|---|
| 1800 | |
|---|
| 1801 | if (prop->is_list) { |
|---|
| 1802 | |
|---|
| 1803 | |
|---|
| 1804 | get_binary_item (plyfile, prop->count_external, |
|---|
| 1805 | &int_val, &uint_val, &double_val); |
|---|
| 1806 | if (store_it) { |
|---|
| 1807 | item = elem_data + prop->count_offset; |
|---|
| 1808 | store_item(item, prop->count_internal, int_val, uint_val, double_val); |
|---|
| 1809 | } |
|---|
| 1810 | |
|---|
| 1811 | |
|---|
| 1812 | list_count = int_val; |
|---|
| 1813 | |
|---|
| 1814 | |
|---|
| 1815 | |
|---|
| 1816 | |
|---|
| 1817 | if (store_it) { |
|---|
| 1818 | item_size = ply_type_size[prop->internal_type]; |
|---|
| 1819 | } |
|---|
| 1820 | store_array = (char **) (elem_data + prop->offset); |
|---|
| 1821 | if (list_count == 0) { |
|---|
| 1822 | if (store_it) |
|---|
| 1823 | *store_array = NULL; |
|---|
| 1824 | } |
|---|
| 1825 | else { |
|---|
| 1826 | if (store_it) { |
|---|
| 1827 | item_ptr = (char *) myalloc (sizeof (char) * item_size * list_count); |
|---|
| 1828 | item = item_ptr; |
|---|
| 1829 | *store_array = item_ptr; |
|---|
| 1830 | } |
|---|
| 1831 | |
|---|
| 1832 | |
|---|
| 1833 | for (k = 0; k < list_count; k++) { |
|---|
| 1834 | get_binary_item (plyfile, prop->external_type, |
|---|
| 1835 | &int_val, &uint_val, &double_val); |
|---|
| 1836 | if (store_it) { |
|---|
| 1837 | store_item (item, prop->internal_type, |
|---|
| 1838 | int_val, uint_val, double_val); |
|---|
| 1839 | item += item_size; |
|---|
| 1840 | } |
|---|
| 1841 | } |
|---|
| 1842 | } |
|---|
| 1843 | |
|---|
| 1844 | } |
|---|
| 1845 | else { |
|---|
| 1846 | get_binary_item (plyfile, prop->external_type, |
|---|
| 1847 | &int_val, &uint_val, &double_val); |
|---|
| 1848 | if (store_it) { |
|---|
| 1849 | item = elem_data + prop->offset; |
|---|
| 1850 | store_item (item, prop->internal_type, int_val, uint_val, double_val); |
|---|
| 1851 | } |
|---|
| 1852 | } |
|---|
| 1853 | |
|---|
| 1854 | } |
|---|
| 1855 | } |
|---|
| 1856 | |
|---|
| 1857 | |
|---|
| 1858 | |
|---|
| 1859 | |
|---|
| 1860 | |
|---|
| 1861 | |
|---|
| 1862 | |
|---|
| 1863 | |
|---|
| 1864 | |
|---|
| 1865 | |
|---|
| 1866 | void write_scalar_type (FILE *fp, int code) |
|---|
| 1867 | { |
|---|
| 1868 | |
|---|
| 1869 | |
|---|
| 1870 | if (code <= PLY_START_TYPE || code >= PLY_END_TYPE) { |
|---|
| 1871 | char error[100]; |
|---|
| 1872 | sprintf (error, "write_scalar_type: bad data code = %d\n", code); |
|---|
| 1873 | throw ply::MeshException( error ); |
|---|
| 1874 | } |
|---|
| 1875 | |
|---|
| 1876 | |
|---|
| 1877 | |
|---|
| 1878 | fprintf (fp, "%s", type_names[code]); |
|---|
| 1879 | } |
|---|
| 1880 | |
|---|
| 1881 | |
|---|
| 1882 | |
|---|
| 1883 | |
|---|
| 1884 | |
|---|
| 1885 | |
|---|
| 1886 | |
|---|
| 1887 | |
|---|
| 1888 | |
|---|
| 1889 | |
|---|
| 1890 | |
|---|
| 1891 | |
|---|
| 1892 | |
|---|
| 1893 | |
|---|
| 1894 | |
|---|
| 1895 | |
|---|
| 1896 | |
|---|
| 1897 | char **get_words(FILE *fp, int *nwords, char **orig_line) |
|---|
| 1898 | { |
|---|
| 1899 | #define BIG_STRING 4096 |
|---|
| 1900 | static char str[BIG_STRING]; |
|---|
| 1901 | static char str_copy[BIG_STRING]; |
|---|
| 1902 | char **words; |
|---|
| 1903 | int max_words = 10; |
|---|
| 1904 | int num_words = 0; |
|---|
| 1905 | char *ptr,*ptr2; |
|---|
| 1906 | char *result; |
|---|
| 1907 | |
|---|
| 1908 | |
|---|
| 1909 | result = fgets (str, BIG_STRING, fp); |
|---|
| 1910 | if (result == NULL) { |
|---|
| 1911 | *nwords = 0; |
|---|
| 1912 | *orig_line = NULL; |
|---|
| 1913 | return (NULL); |
|---|
| 1914 | } |
|---|
| 1915 | |
|---|
| 1916 | words = (char **) myalloc (sizeof (char *) * max_words); |
|---|
| 1917 | |
|---|
| 1918 | |
|---|
| 1919 | |
|---|
| 1920 | |
|---|
| 1921 | |
|---|
| 1922 | str[BIG_STRING-2] = ' '; |
|---|
| 1923 | str[BIG_STRING-1] = '\0'; |
|---|
| 1924 | |
|---|
| 1925 | for (ptr = str, ptr2 = str_copy; *ptr != '\0'; ptr++, ptr2++) { |
|---|
| 1926 | *ptr2 = *ptr; |
|---|
| 1927 | if (*ptr == '\t') { |
|---|
| 1928 | *ptr = ' '; |
|---|
| 1929 | *ptr2 = ' '; |
|---|
| 1930 | } |
|---|
| 1931 | else if (*ptr == '\n') { |
|---|
| 1932 | *ptr = ' '; |
|---|
| 1933 | *ptr2 = '\0'; |
|---|
| 1934 | break; |
|---|
| 1935 | } |
|---|
| 1936 | } |
|---|
| 1937 | |
|---|
| 1938 | |
|---|
| 1939 | |
|---|
| 1940 | ptr = str; |
|---|
| 1941 | while (*ptr != '\0') { |
|---|
| 1942 | |
|---|
| 1943 | |
|---|
| 1944 | while (*ptr == ' ') |
|---|
| 1945 | ptr++; |
|---|
| 1946 | |
|---|
| 1947 | |
|---|
| 1948 | if (*ptr == '\0') |
|---|
| 1949 | break; |
|---|
| 1950 | |
|---|
| 1951 | |
|---|
| 1952 | if (num_words >= max_words) { |
|---|
| 1953 | max_words += 10; |
|---|
| 1954 | words = (char **) realloc (words, sizeof (char *) * max_words); |
|---|
| 1955 | } |
|---|
| 1956 | words[num_words++] = ptr; |
|---|
| 1957 | |
|---|
| 1958 | |
|---|
| 1959 | while (*ptr != ' ') |
|---|
| 1960 | ptr++; |
|---|
| 1961 | |
|---|
| 1962 | |
|---|
| 1963 | *ptr++ = '\0'; |
|---|
| 1964 | } |
|---|
| 1965 | |
|---|
| 1966 | |
|---|
| 1967 | *nwords = num_words; |
|---|
| 1968 | *orig_line = str_copy; |
|---|
| 1969 | return (words); |
|---|
| 1970 | } |
|---|
| 1971 | |
|---|
| 1972 | |
|---|
| 1973 | |
|---|
| 1974 | |
|---|
| 1975 | |
|---|
| 1976 | |
|---|
| 1977 | |
|---|
| 1978 | |
|---|
| 1979 | |
|---|
| 1980 | |
|---|
| 1981 | |
|---|
| 1982 | |
|---|
| 1983 | |
|---|
| 1984 | double get_item_value(char *item, int type) |
|---|
| 1985 | { |
|---|
| 1986 | unsigned char *puchar; |
|---|
| 1987 | char *pchar; |
|---|
| 1988 | short int *pshort; |
|---|
| 1989 | unsigned short int *pushort; |
|---|
| 1990 | int *pint; |
|---|
| 1991 | unsigned int *puint; |
|---|
| 1992 | float *pfloat; |
|---|
| 1993 | double *pdouble; |
|---|
| 1994 | int int_value; |
|---|
| 1995 | unsigned int uint_value; |
|---|
| 1996 | double double_value; |
|---|
| 1997 | |
|---|
| 1998 | switch (type) { |
|---|
| 1999 | case PLY_CHAR: |
|---|
| 2000 | pchar = (char *) item; |
|---|
| 2001 | int_value = *pchar; |
|---|
| 2002 | return ((double) int_value); |
|---|
| 2003 | case PLY_UCHAR: |
|---|
| 2004 | case PLY_UINT8: |
|---|
| 2005 | puchar = (unsigned char *) item; |
|---|
| 2006 | int_value = *puchar; |
|---|
| 2007 | return ((double) int_value); |
|---|
| 2008 | case PLY_SHORT: |
|---|
| 2009 | pshort = (short int *) item; |
|---|
| 2010 | int_value = *pshort; |
|---|
| 2011 | return ((double) int_value); |
|---|
| 2012 | case PLY_USHORT: |
|---|
| 2013 | pushort = (unsigned short int *) item; |
|---|
| 2014 | int_value = *pushort; |
|---|
| 2015 | return ((double) int_value); |
|---|
| 2016 | case PLY_INT: |
|---|
| 2017 | case PLY_INT32: |
|---|
| 2018 | pint = (int *) item; |
|---|
| 2019 | int_value = *pint; |
|---|
| 2020 | return ((double) int_value); |
|---|
| 2021 | case PLY_UINT: |
|---|
| 2022 | puint = (unsigned int *) item; |
|---|
| 2023 | uint_value = *puint; |
|---|
| 2024 | return ((double) uint_value); |
|---|
| 2025 | case PLY_FLOAT: |
|---|
| 2026 | case PLY_FLOAT32: |
|---|
| 2027 | pfloat = (float *) item; |
|---|
| 2028 | double_value = *pfloat; |
|---|
| 2029 | return (double_value); |
|---|
| 2030 | case PLY_DOUBLE: |
|---|
| 2031 | pdouble = (double *) item; |
|---|
| 2032 | double_value = *pdouble; |
|---|
| 2033 | return (double_value); |
|---|
| 2034 | } |
|---|
| 2035 | fprintf (stderr, "get_item_value: bad type = %d\n", type); |
|---|
| 2036 | return 0; |
|---|
| 2037 | } |
|---|
| 2038 | |
|---|
| 2039 | |
|---|
| 2040 | |
|---|
| 2041 | |
|---|
| 2042 | |
|---|
| 2043 | |
|---|
| 2044 | |
|---|
| 2045 | |
|---|
| 2046 | |
|---|
| 2047 | |
|---|
| 2048 | |
|---|
| 2049 | |
|---|
| 2050 | |
|---|
| 2051 | void write_binary_item(PlyFile *plyfile, |
|---|
| 2052 | int int_val, |
|---|
| 2053 | unsigned int uint_val, |
|---|
| 2054 | double double_val, |
|---|
| 2055 | int type |
|---|
| 2056 | ) |
|---|
| 2057 | { |
|---|
| 2058 | FILE *fp = plyfile->fp; |
|---|
| 2059 | unsigned char uchar_val; |
|---|
| 2060 | char char_val; |
|---|
| 2061 | unsigned short ushort_val; |
|---|
| 2062 | short short_val; |
|---|
| 2063 | float float_val; |
|---|
| 2064 | |
|---|
| 2065 | switch (type) { |
|---|
| 2066 | case PLY_CHAR: |
|---|
| 2067 | char_val = int_val; |
|---|
| 2068 | fwrite (&char_val, 1, 1, fp); |
|---|
| 2069 | break; |
|---|
| 2070 | case PLY_SHORT: |
|---|
| 2071 | short_val = int_val; |
|---|
| 2072 | if( plyfile->file_type == PLY_BINARY_BE ) |
|---|
| 2073 | swap2BE(&short_val); |
|---|
| 2074 | else |
|---|
| 2075 | swap2LE(&short_val); |
|---|
| 2076 | fwrite (&short_val, 2, 1, fp); |
|---|
| 2077 | break; |
|---|
| 2078 | case PLY_INT: |
|---|
| 2079 | case PLY_INT32: |
|---|
| 2080 | if( plyfile->file_type == PLY_BINARY_BE ) |
|---|
| 2081 | { |
|---|
| 2082 | swap4BE(&int_val); |
|---|
| 2083 | } |
|---|
| 2084 | else |
|---|
| 2085 | { |
|---|
| 2086 | swap4LE(&int_val); |
|---|
| 2087 | } |
|---|
| 2088 | fwrite (&int_val, 4, 1, fp); |
|---|
| 2089 | break; |
|---|
| 2090 | case PLY_UCHAR: |
|---|
| 2091 | case PLY_UINT8: |
|---|
| 2092 | uchar_val = uint_val; |
|---|
| 2093 | fwrite (&uchar_val, 1, 1, fp); |
|---|
| 2094 | break; |
|---|
| 2095 | case PLY_USHORT: |
|---|
| 2096 | if( plyfile->file_type == PLY_BINARY_BE ) |
|---|
| 2097 | { |
|---|
| 2098 | swap2BE(&ushort_val); |
|---|
| 2099 | } |
|---|
| 2100 | else |
|---|
| 2101 | { |
|---|
| 2102 | swap2LE(&ushort_val); |
|---|
| 2103 | } |
|---|
| 2104 | ushort_val = uint_val; |
|---|
| 2105 | fwrite (&ushort_val, 2, 1, fp); |
|---|
| 2106 | break; |
|---|
| 2107 | case PLY_UINT: |
|---|
| 2108 | if( plyfile->file_type == PLY_BINARY_BE ) |
|---|
| 2109 | { |
|---|
| 2110 | swap4BE(&uint_val); |
|---|
| 2111 | } |
|---|
| 2112 | else |
|---|
| 2113 | { |
|---|
| 2114 | swap4LE(&uint_val); |
|---|
| 2115 | } |
|---|
| 2116 | fwrite (&uint_val, 4, 1, fp); |
|---|
| 2117 | break; |
|---|
| 2118 | case PLY_FLOAT: |
|---|
| 2119 | case PLY_FLOAT32: |
|---|
| 2120 | float_val = double_val; |
|---|
| 2121 | if( plyfile->file_type == PLY_BINARY_BE ) |
|---|
| 2122 | { |
|---|
| 2123 | swap4BE(&float_val); |
|---|
| 2124 | } |
|---|
| 2125 | else |
|---|
| 2126 | { |
|---|
| 2127 | swap4LE(&float_val); |
|---|
| 2128 | } |
|---|
| 2129 | fwrite (&float_val, 4, 1, fp); |
|---|
| 2130 | break; |
|---|
| 2131 | case PLY_DOUBLE: |
|---|
| 2132 | if( plyfile->file_type == PLY_BINARY_BE ) |
|---|
| 2133 | { |
|---|
| 2134 | swap8BE(&double_val); |
|---|
| 2135 | } |
|---|
| 2136 | else |
|---|
| 2137 | { |
|---|
| 2138 | swap8LE(&double_val); |
|---|
| 2139 | } |
|---|
| 2140 | fwrite (&double_val, 8, 1, fp); |
|---|
| 2141 | break; |
|---|
| 2142 | default: |
|---|
| 2143 | char error[100]; |
|---|
| 2144 | sprintf (error, "write_binary_item: bad type = %d\n", type); |
|---|
| 2145 | throw ply::MeshException( error ); |
|---|
| 2146 | } |
|---|
| 2147 | } |
|---|
| 2148 | |
|---|
| 2149 | |
|---|
| 2150 | |
|---|
| 2151 | |
|---|
| 2152 | |
|---|
| 2153 | |
|---|
| 2154 | |
|---|
| 2155 | |
|---|
| 2156 | |
|---|
| 2157 | |
|---|
| 2158 | |
|---|
| 2159 | |
|---|
| 2160 | |
|---|
| 2161 | void write_ascii_item( |
|---|
| 2162 | FILE *fp, |
|---|
| 2163 | int int_val, |
|---|
| 2164 | unsigned int uint_val, |
|---|
| 2165 | double double_val, |
|---|
| 2166 | int type |
|---|
| 2167 | ) |
|---|
| 2168 | { |
|---|
| 2169 | switch (type) { |
|---|
| 2170 | case PLY_CHAR: |
|---|
| 2171 | case PLY_SHORT: |
|---|
| 2172 | case PLY_INT: |
|---|
| 2173 | case PLY_INT32: |
|---|
| 2174 | fprintf (fp, "%d ", int_val); |
|---|
| 2175 | break; |
|---|
| 2176 | case PLY_UCHAR: |
|---|
| 2177 | case PLY_UINT8: |
|---|
| 2178 | case PLY_USHORT: |
|---|
| 2179 | case PLY_UINT: |
|---|
| 2180 | fprintf (fp, "%u ", uint_val); |
|---|
| 2181 | break; |
|---|
| 2182 | case PLY_FLOAT: |
|---|
| 2183 | case PLY_FLOAT32: |
|---|
| 2184 | case PLY_DOUBLE: |
|---|
| 2185 | fprintf (fp, "%g ", double_val); |
|---|
| 2186 | break; |
|---|
| 2187 | default: |
|---|
| 2188 | char error[100]; |
|---|
| 2189 | sprintf (error, "write_ascii_item: bad type = %d\n", type); |
|---|
| 2190 | throw ply::MeshException( error ); |
|---|
| 2191 | } |
|---|
| 2192 | } |
|---|
| 2193 | |
|---|
| 2194 | |
|---|
| 2195 | |
|---|
| 2196 | |
|---|
| 2197 | |
|---|
| 2198 | |
|---|
| 2199 | |
|---|
| 2200 | |
|---|
| 2201 | |
|---|
| 2202 | |
|---|
| 2203 | |
|---|
| 2204 | |
|---|
| 2205 | |
|---|
| 2206 | |
|---|
| 2207 | |
|---|
| 2208 | void get_stored_item( |
|---|
| 2209 | void *ptr, |
|---|
| 2210 | int type, |
|---|
| 2211 | int *int_val, |
|---|
| 2212 | unsigned int *uint_val, |
|---|
| 2213 | double *double_val |
|---|
| 2214 | ) |
|---|
| 2215 | { |
|---|
| 2216 | switch (type) { |
|---|
| 2217 | case PLY_CHAR: |
|---|
| 2218 | *int_val = *((char *) ptr); |
|---|
| 2219 | *uint_val = *int_val; |
|---|
| 2220 | *double_val = *int_val; |
|---|
| 2221 | break; |
|---|
| 2222 | case PLY_UCHAR: |
|---|
| 2223 | case PLY_UINT8: |
|---|
| 2224 | *uint_val = *((unsigned char *) ptr); |
|---|
| 2225 | *int_val = *uint_val; |
|---|
| 2226 | *double_val = *uint_val; |
|---|
| 2227 | break; |
|---|
| 2228 | case PLY_SHORT: |
|---|
| 2229 | *int_val = *((short int *) ptr); |
|---|
| 2230 | *uint_val = *int_val; |
|---|
| 2231 | *double_val = *int_val; |
|---|
| 2232 | break; |
|---|
| 2233 | case PLY_USHORT: |
|---|
| 2234 | *uint_val = *((unsigned short int *) ptr); |
|---|
| 2235 | *int_val = *uint_val; |
|---|
| 2236 | *double_val = *uint_val; |
|---|
| 2237 | break; |
|---|
| 2238 | case PLY_INT: |
|---|
| 2239 | case PLY_INT32: |
|---|
| 2240 | *int_val = *((int *) ptr); |
|---|
| 2241 | *uint_val = *int_val; |
|---|
| 2242 | *double_val = *int_val; |
|---|
| 2243 | break; |
|---|
| 2244 | case PLY_UINT: |
|---|
| 2245 | *uint_val = *((unsigned int *) ptr); |
|---|
| 2246 | *int_val = *uint_val; |
|---|
| 2247 | *double_val = *uint_val; |
|---|
| 2248 | break; |
|---|
| 2249 | case PLY_FLOAT: |
|---|
| 2250 | case PLY_FLOAT32: |
|---|
| 2251 | *double_val = *((float *) ptr); |
|---|
| 2252 | *int_val = (int) *double_val; |
|---|
| 2253 | *uint_val = (unsigned int) *double_val; |
|---|
| 2254 | break; |
|---|
| 2255 | case PLY_DOUBLE: |
|---|
| 2256 | *double_val = *((double *) ptr); |
|---|
| 2257 | *int_val = (int) *double_val; |
|---|
| 2258 | *uint_val = (unsigned int) *double_val; |
|---|
| 2259 | break; |
|---|
| 2260 | default: |
|---|
| 2261 | char error[100]; |
|---|
| 2262 | sprintf (error, "get_stored_item: bad type = %d\n", type); |
|---|
| 2263 | throw ply::MeshException( error ); |
|---|
| 2264 | } |
|---|
| 2265 | } |
|---|
| 2266 | |
|---|
| 2267 | |
|---|
| 2268 | |
|---|
| 2269 | |
|---|
| 2270 | |
|---|
| 2271 | |
|---|
| 2272 | |
|---|
| 2273 | |
|---|
| 2274 | |
|---|
| 2275 | |
|---|
| 2276 | |
|---|
| 2277 | |
|---|
| 2278 | |
|---|
| 2279 | |
|---|
| 2280 | |
|---|
| 2281 | |
|---|
| 2282 | void get_binary_item( |
|---|
| 2283 | PlyFile *plyfile, |
|---|
| 2284 | int type, |
|---|
| 2285 | int *int_val, |
|---|
| 2286 | unsigned int *uint_val, |
|---|
| 2287 | double *double_val |
|---|
| 2288 | ) |
|---|
| 2289 | { |
|---|
| 2290 | char c[8]; |
|---|
| 2291 | void *ptr; |
|---|
| 2292 | |
|---|
| 2293 | ptr = (void *) c; |
|---|
| 2294 | size_t result = 0; |
|---|
| 2295 | |
|---|
| 2296 | switch (type) { |
|---|
| 2297 | case PLY_CHAR: |
|---|
| 2298 | result = fread (ptr, 1, 1, plyfile->fp); |
|---|
| 2299 | if(result < 1) |
|---|
| 2300 | { |
|---|
| 2301 | throw ply::MeshException( "Error in reading PLY file." |
|---|
| 2302 | "fread not succeeded." ); |
|---|
| 2303 | } |
|---|
| 2304 | *int_val = *((char *) ptr); |
|---|
| 2305 | *uint_val = *int_val; |
|---|
| 2306 | *double_val = *int_val; |
|---|
| 2307 | break; |
|---|
| 2308 | case PLY_UCHAR: |
|---|
| 2309 | case PLY_UINT8: |
|---|
| 2310 | result = fread (ptr, 1, 1, plyfile->fp); |
|---|
| 2311 | if(result < 1) |
|---|
| 2312 | { |
|---|
| 2313 | throw ply::MeshException( "Error in reading PLY file." |
|---|
| 2314 | "fread not succeeded." ); |
|---|
| 2315 | } |
|---|
| 2316 | *uint_val = *((unsigned char *) ptr); |
|---|
| 2317 | *int_val = *uint_val; |
|---|
| 2318 | *double_val = *uint_val; |
|---|
| 2319 | break; |
|---|
| 2320 | case PLY_SHORT: |
|---|
| 2321 | result = fread (ptr, 2, 1, plyfile->fp); |
|---|
| 2322 | if(result < 1 ) |
|---|
| 2323 | { |
|---|
| 2324 | throw ply::MeshException( "Error in reading PLY file." |
|---|
| 2325 | "fread not succeeded." ); |
|---|
| 2326 | } |
|---|
| 2327 | if( plyfile->file_type == PLY_BINARY_BE ) |
|---|
| 2328 | { |
|---|
| 2329 | swap2BE(ptr); |
|---|
| 2330 | } |
|---|
| 2331 | else |
|---|
| 2332 | { |
|---|
| 2333 | swap2LE(ptr); |
|---|
| 2334 | } |
|---|
| 2335 | *int_val = *((short int *) ptr); |
|---|
| 2336 | *uint_val = *int_val; |
|---|
| 2337 | *double_val = *int_val; |
|---|
| 2338 | break; |
|---|
| 2339 | case PLY_USHORT: |
|---|
| 2340 | result = fread (ptr, 2, 1, plyfile->fp); |
|---|
| 2341 | if(result < 1) |
|---|
| 2342 | { |
|---|
| 2343 | throw ply::MeshException( "Error in reading PLY file." |
|---|
| 2344 | "fread not succeeded." ); |
|---|
| 2345 | } |
|---|
| 2346 | if( plyfile->file_type == PLY_BINARY_BE ) |
|---|
| 2347 | { |
|---|
| 2348 | swap2BE(ptr); |
|---|
| 2349 | } |
|---|
| 2350 | else |
|---|
| 2351 | { |
|---|
| 2352 | swap2LE(ptr); |
|---|
| 2353 | } |
|---|
| 2354 | *uint_val = *((unsigned short int *) ptr); |
|---|
| 2355 | *int_val = *uint_val; |
|---|
| 2356 | *double_val = *uint_val; |
|---|
| 2357 | break; |
|---|
| 2358 | case PLY_INT: |
|---|
| 2359 | case PLY_INT32: |
|---|
| 2360 | result = fread (ptr, 4, 1, plyfile->fp); |
|---|
| 2361 | if(result < 1) |
|---|
| 2362 | { |
|---|
| 2363 | throw ply::MeshException( "Error in reading PLY file." |
|---|
| 2364 | "fread not succeeded." ); |
|---|
| 2365 | } |
|---|
| 2366 | if( plyfile->file_type == PLY_BINARY_BE ) |
|---|
| 2367 | { |
|---|
| 2368 | swap4BE(ptr); |
|---|
| 2369 | } |
|---|
| 2370 | else |
|---|
| 2371 | { |
|---|
| 2372 | swap4LE(ptr); |
|---|
| 2373 | } |
|---|
| 2374 | *int_val = *((int *) ptr); |
|---|
| 2375 | *uint_val = *int_val; |
|---|
| 2376 | *double_val = *int_val; |
|---|
| 2377 | break; |
|---|
| 2378 | case PLY_UINT: |
|---|
| 2379 | result = fread (ptr, 4, 1, plyfile->fp); |
|---|
| 2380 | if(result < 1) |
|---|
| 2381 | { |
|---|
| 2382 | throw ply::MeshException( "Error in reading PLY file." |
|---|
| 2383 | "fread not succeeded." ); |
|---|
| 2384 | } |
|---|
| 2385 | if( plyfile->file_type == PLY_BINARY_BE ) |
|---|
| 2386 | { |
|---|
| 2387 | swap4BE(ptr); |
|---|
| 2388 | } |
|---|
| 2389 | else |
|---|
| 2390 | { |
|---|
| 2391 | swap4LE(ptr); |
|---|
| 2392 | } |
|---|
| 2393 | *uint_val = *((unsigned int *) ptr); |
|---|
| 2394 | *int_val = *uint_val; |
|---|
| 2395 | *double_val = *uint_val; |
|---|
| 2396 | break; |
|---|
| 2397 | case PLY_FLOAT: |
|---|
| 2398 | case PLY_FLOAT32: |
|---|
| 2399 | result = fread (ptr, 4, 1, plyfile->fp); |
|---|
| 2400 | if(result < 1) |
|---|
| 2401 | { |
|---|
| 2402 | throw ply::MeshException( "Error in reading PLY file." |
|---|
| 2403 | "fread not succeeded." ); |
|---|
| 2404 | } |
|---|
| 2405 | if( plyfile->file_type == PLY_BINARY_BE ) |
|---|
| 2406 | { |
|---|
| 2407 | swap4BE(ptr); |
|---|
| 2408 | } |
|---|
| 2409 | else |
|---|
| 2410 | { |
|---|
| 2411 | swap4LE(ptr); |
|---|
| 2412 | } |
|---|
| 2413 | *double_val = *((float *) ptr); |
|---|
| 2414 | *int_val = (int) *double_val; |
|---|
| 2415 | *uint_val = (unsigned int) *double_val; |
|---|
| 2416 | break; |
|---|
| 2417 | case PLY_DOUBLE: |
|---|
| 2418 | result = fread (ptr, 8, 1, plyfile->fp); |
|---|
| 2419 | if(result < 1) |
|---|
| 2420 | { |
|---|
| 2421 | throw ply::MeshException( "Error in reading PLY file." |
|---|
| 2422 | "fread not succeeded." ); |
|---|
| 2423 | } |
|---|
| 2424 | if( plyfile->file_type == PLY_BINARY_BE ) |
|---|
| 2425 | { |
|---|
| 2426 | swap8BE(ptr); |
|---|
| 2427 | } |
|---|
| 2428 | else |
|---|
| 2429 | { |
|---|
| 2430 | swap8LE(ptr); |
|---|
| 2431 | } |
|---|
| 2432 | *double_val = *((double *) ptr); |
|---|
| 2433 | *int_val = (int) *double_val; |
|---|
| 2434 | *uint_val = (unsigned int) *double_val; |
|---|
| 2435 | break; |
|---|
| 2436 | default: |
|---|
| 2437 | char error[100]; |
|---|
| 2438 | sprintf (error, "get_binary_item: bad type = %d\n", type); |
|---|
| 2439 | throw ply::MeshException( error ); |
|---|
| 2440 | } |
|---|
| 2441 | } |
|---|
| 2442 | |
|---|
| 2443 | |
|---|
| 2444 | |
|---|
| 2445 | |
|---|
| 2446 | |
|---|
| 2447 | |
|---|
| 2448 | |
|---|
| 2449 | |
|---|
| 2450 | |
|---|
| 2451 | |
|---|
| 2452 | |
|---|
| 2453 | |
|---|
| 2454 | |
|---|
| 2455 | |
|---|
| 2456 | |
|---|
| 2457 | |
|---|
| 2458 | void get_ascii_item( |
|---|
| 2459 | char *word, |
|---|
| 2460 | int type, |
|---|
| 2461 | int *int_val, |
|---|
| 2462 | unsigned int *uint_val, |
|---|
| 2463 | double *double_val |
|---|
| 2464 | ) |
|---|
| 2465 | { |
|---|
| 2466 | switch (type) { |
|---|
| 2467 | case PLY_CHAR: |
|---|
| 2468 | case PLY_UCHAR: |
|---|
| 2469 | case PLY_UINT8: |
|---|
| 2470 | case PLY_SHORT: |
|---|
| 2471 | case PLY_USHORT: |
|---|
| 2472 | case PLY_INT: |
|---|
| 2473 | case PLY_INT32: |
|---|
| 2474 | *int_val = atoi (word); |
|---|
| 2475 | *uint_val = *int_val; |
|---|
| 2476 | *double_val = *int_val; |
|---|
| 2477 | break; |
|---|
| 2478 | |
|---|
| 2479 | case PLY_UINT: |
|---|
| 2480 | *uint_val = strtoul (word, (char **) NULL, 10); |
|---|
| 2481 | *int_val = *uint_val; |
|---|
| 2482 | *double_val = *uint_val; |
|---|
| 2483 | break; |
|---|
| 2484 | |
|---|
| 2485 | case PLY_FLOAT: |
|---|
| 2486 | case PLY_FLOAT32: |
|---|
| 2487 | case PLY_DOUBLE: |
|---|
| 2488 | *double_val = osg::asciiToDouble(word); |
|---|
| 2489 | *int_val = (int) *double_val; |
|---|
| 2490 | *uint_val = (unsigned int) *double_val; |
|---|
| 2491 | break; |
|---|
| 2492 | |
|---|
| 2493 | default: |
|---|
| 2494 | char error[100]; |
|---|
| 2495 | sprintf (error, "get_ascii_item: bad type = %d\n", type); |
|---|
| 2496 | throw ply::MeshException( error ); |
|---|
| 2497 | } |
|---|
| 2498 | } |
|---|
| 2499 | |
|---|
| 2500 | |
|---|
| 2501 | |
|---|
| 2502 | |
|---|
| 2503 | |
|---|
| 2504 | |
|---|
| 2505 | |
|---|
| 2506 | |
|---|
| 2507 | |
|---|
| 2508 | |
|---|
| 2509 | |
|---|
| 2510 | |
|---|
| 2511 | |
|---|
| 2512 | |
|---|
| 2513 | |
|---|
| 2514 | |
|---|
| 2515 | void store_item ( |
|---|
| 2516 | char *item, |
|---|
| 2517 | int type, |
|---|
| 2518 | int int_val, |
|---|
| 2519 | unsigned int uint_val, |
|---|
| 2520 | double double_val |
|---|
| 2521 | ) |
|---|
| 2522 | { |
|---|
| 2523 | unsigned char *puchar; |
|---|
| 2524 | short int *pshort; |
|---|
| 2525 | unsigned short int *pushort; |
|---|
| 2526 | int *pint; |
|---|
| 2527 | unsigned int *puint; |
|---|
| 2528 | float *pfloat; |
|---|
| 2529 | double *pdouble; |
|---|
| 2530 | |
|---|
| 2531 | switch (type) { |
|---|
| 2532 | case PLY_CHAR: |
|---|
| 2533 | *item = int_val; |
|---|
| 2534 | break; |
|---|
| 2535 | case PLY_UCHAR: |
|---|
| 2536 | case PLY_UINT8: |
|---|
| 2537 | puchar = (unsigned char *) item; |
|---|
| 2538 | *puchar = uint_val; |
|---|
| 2539 | break; |
|---|
| 2540 | case PLY_SHORT: |
|---|
| 2541 | pshort = (short *) item; |
|---|
| 2542 | *pshort = int_val; |
|---|
| 2543 | break; |
|---|
| 2544 | case PLY_USHORT: |
|---|
| 2545 | pushort = (unsigned short *) item; |
|---|
| 2546 | *pushort = uint_val; |
|---|
| 2547 | break; |
|---|
| 2548 | case PLY_INT: |
|---|
| 2549 | case PLY_INT32: |
|---|
| 2550 | pint = (int *) item; |
|---|
| 2551 | *pint = int_val; |
|---|
| 2552 | break; |
|---|
| 2553 | case PLY_UINT: |
|---|
| 2554 | puint = (unsigned int *) item; |
|---|
| 2555 | *puint = uint_val; |
|---|
| 2556 | break; |
|---|
| 2557 | case PLY_FLOAT: |
|---|
| 2558 | case PLY_FLOAT32: |
|---|
| 2559 | pfloat = (float *) item; |
|---|
| 2560 | *pfloat = double_val; |
|---|
| 2561 | break; |
|---|
| 2562 | case PLY_DOUBLE: |
|---|
| 2563 | pdouble = (double *) item; |
|---|
| 2564 | *pdouble = double_val; |
|---|
| 2565 | break; |
|---|
| 2566 | default: |
|---|
| 2567 | char error[100]; |
|---|
| 2568 | sprintf (error, "store_item: bad type = %d\n", type); |
|---|
| 2569 | throw ply::MeshException( error ); |
|---|
| 2570 | } |
|---|
| 2571 | } |
|---|
| 2572 | |
|---|
| 2573 | |
|---|
| 2574 | |
|---|
| 2575 | |
|---|
| 2576 | |
|---|
| 2577 | |
|---|
| 2578 | |
|---|
| 2579 | |
|---|
| 2580 | |
|---|
| 2581 | |
|---|
| 2582 | |
|---|
| 2583 | void add_element (PlyFile *plyfile, char **words, int) |
|---|
| 2584 | { |
|---|
| 2585 | PlyElement *elem; |
|---|
| 2586 | |
|---|
| 2587 | |
|---|
| 2588 | elem = (PlyElement *) myalloc (sizeof (PlyElement)); |
|---|
| 2589 | elem->name = strdup (words[1]); |
|---|
| 2590 | elem->num = atoi (words[2]); |
|---|
| 2591 | elem->nprops = 0; |
|---|
| 2592 | |
|---|
| 2593 | |
|---|
| 2594 | if (plyfile->nelems == 0) |
|---|
| 2595 | plyfile->elems = (PlyElement **) myalloc (sizeof (PlyElement *)); |
|---|
| 2596 | else |
|---|
| 2597 | plyfile->elems = (PlyElement **) realloc (plyfile->elems, |
|---|
| 2598 | sizeof (PlyElement *) * (plyfile->nelems + 1)); |
|---|
| 2599 | |
|---|
| 2600 | |
|---|
| 2601 | plyfile->elems[plyfile->nelems] = elem; |
|---|
| 2602 | plyfile->nelems++; |
|---|
| 2603 | } |
|---|
| 2604 | |
|---|
| 2605 | |
|---|
| 2606 | |
|---|
| 2607 | |
|---|
| 2608 | |
|---|
| 2609 | |
|---|
| 2610 | |
|---|
| 2611 | |
|---|
| 2612 | |
|---|
| 2613 | |
|---|
| 2614 | |
|---|
| 2615 | |
|---|
| 2616 | int get_prop_type(char *type_name) |
|---|
| 2617 | { |
|---|
| 2618 | int i; |
|---|
| 2619 | |
|---|
| 2620 | for (i = PLY_START_TYPE + 1; i < PLY_END_TYPE; i++) |
|---|
| 2621 | if (equal_strings (type_name, type_names[i])) |
|---|
| 2622 | return (i); |
|---|
| 2623 | |
|---|
| 2624 | |
|---|
| 2625 | return (0); |
|---|
| 2626 | } |
|---|
| 2627 | |
|---|
| 2628 | |
|---|
| 2629 | |
|---|
| 2630 | |
|---|
| 2631 | |
|---|
| 2632 | |
|---|
| 2633 | |
|---|
| 2634 | |
|---|
| 2635 | |
|---|
| 2636 | |
|---|
| 2637 | |
|---|
| 2638 | void add_property (PlyFile *plyfile, char **words, int ) |
|---|
| 2639 | { |
|---|
| 2640 | PlyProperty *prop; |
|---|
| 2641 | PlyElement *elem; |
|---|
| 2642 | |
|---|
| 2643 | |
|---|
| 2644 | |
|---|
| 2645 | prop = (PlyProperty *) myalloc (sizeof (PlyProperty)); |
|---|
| 2646 | |
|---|
| 2647 | if (equal_strings (words[1], "list")) { |
|---|
| 2648 | prop->count_external = get_prop_type (words[2]); |
|---|
| 2649 | prop->external_type = get_prop_type (words[3]); |
|---|
| 2650 | prop->name = strdup (words[4]); |
|---|
| 2651 | prop->is_list = 1; |
|---|
| 2652 | } |
|---|
| 2653 | else { |
|---|
| 2654 | prop->external_type = get_prop_type (words[1]); |
|---|
| 2655 | prop->name = strdup (words[2]); |
|---|
| 2656 | prop->is_list = 0; |
|---|
| 2657 | } |
|---|
| 2658 | |
|---|
| 2659 | |
|---|
| 2660 | |
|---|
| 2661 | elem = plyfile->elems[plyfile->nelems - 1]; |
|---|
| 2662 | |
|---|
| 2663 | if (elem->nprops == 0) |
|---|
| 2664 | elem->props = (PlyProperty **) myalloc (sizeof (PlyProperty *)); |
|---|
| 2665 | else |
|---|
| 2666 | elem->props = (PlyProperty **) realloc (elem->props, |
|---|
| 2667 | sizeof (PlyProperty *) * (elem->nprops + 1)); |
|---|
| 2668 | |
|---|
| 2669 | elem->props[elem->nprops] = prop; |
|---|
| 2670 | elem->nprops++; |
|---|
| 2671 | } |
|---|
| 2672 | |
|---|
| 2673 | |
|---|
| 2674 | |
|---|
| 2675 | |
|---|
| 2676 | |
|---|
| 2677 | |
|---|
| 2678 | |
|---|
| 2679 | |
|---|
| 2680 | |
|---|
| 2681 | |
|---|
| 2682 | void add_comment (PlyFile *plyfile, char *line) |
|---|
| 2683 | { |
|---|
| 2684 | int i; |
|---|
| 2685 | |
|---|
| 2686 | |
|---|
| 2687 | i = 7; |
|---|
| 2688 | while (line[i] == ' ' || line[i] == '\t') |
|---|
| 2689 | i++; |
|---|
| 2690 | |
|---|
| 2691 | ply_put_comment (plyfile, &line[i]); |
|---|
| 2692 | } |
|---|
| 2693 | |
|---|
| 2694 | |
|---|
| 2695 | |
|---|
| 2696 | |
|---|
| 2697 | |
|---|
| 2698 | |
|---|
| 2699 | |
|---|
| 2700 | |
|---|
| 2701 | |
|---|
| 2702 | |
|---|
| 2703 | void add_obj_info (PlyFile *plyfile, char *line) |
|---|
| 2704 | { |
|---|
| 2705 | int i; |
|---|
| 2706 | |
|---|
| 2707 | |
|---|
| 2708 | i = 8; |
|---|
| 2709 | while (line[i] == ' ' || line[i] == '\t') |
|---|
| 2710 | i++; |
|---|
| 2711 | |
|---|
| 2712 | ply_put_obj_info (plyfile, &line[i]); |
|---|
| 2713 | } |
|---|
| 2714 | |
|---|
| 2715 | |
|---|
| 2716 | |
|---|
| 2717 | |
|---|
| 2718 | |
|---|
| 2719 | |
|---|
| 2720 | void copy_property(PlyProperty *dest, PlyProperty *src) |
|---|
| 2721 | { |
|---|
| 2722 | dest->name = strdup (src->name); |
|---|
| 2723 | dest->external_type = src->external_type; |
|---|
| 2724 | dest->internal_type = src->internal_type; |
|---|
| 2725 | dest->offset = src->offset; |
|---|
| 2726 | |
|---|
| 2727 | dest->is_list = src->is_list; |
|---|
| 2728 | dest->count_external = src->count_external; |
|---|
| 2729 | dest->count_internal = src->count_internal; |
|---|
| 2730 | dest->count_offset = src->count_offset; |
|---|
| 2731 | } |
|---|
| 2732 | |
|---|
| 2733 | |
|---|
| 2734 | |
|---|
| 2735 | |
|---|
| 2736 | |
|---|
| 2737 | |
|---|
| 2738 | |
|---|
| 2739 | |
|---|
| 2740 | |
|---|
| 2741 | |
|---|
| 2742 | |
|---|
| 2743 | char *my_alloc(int size, int lnum, const char *fname) |
|---|
| 2744 | { |
|---|
| 2745 | char *ptr; |
|---|
| 2746 | |
|---|
| 2747 | ptr = (char *) malloc (size); |
|---|
| 2748 | |
|---|
| 2749 | if (ptr == 0) |
|---|
| 2750 | fprintf( stderr, "Memory allocation bombed on line %d in %s\n", |
|---|
| 2751 | lnum, fname); |
|---|
| 2752 | |
|---|
| 2753 | return (ptr); |
|---|
| 2754 | } |
|---|