| 1 |
/* +------------------------------------------------------------------------+ |
| 2 |
| Mobile Robot Programming Toolkit (MRPT) | |
| 3 |
| https://www.mrpt.org/ | |
| 4 |
| | |
| 5 |
| Copyright (c) 2005-2026, Individual contributors, see AUTHORS file | |
| 6 |
| See: https://www.mrpt.org/Authors - All rights reserved. | |
| 7 |
| Released under BSD License. See: https://www.mrpt.org/License | |
| 8 |
+------------------------------------------------------------------------+ */ |
| 9 |
|
| 10 |
/* |
| 11 |
This file is heavily based on the RPly C library by Greg Turk (February 1994) |
| 12 |
It was modified, C++'ized and STL'ized by J.L.Blanco for MRPT (Jan-Feb 2011) |
| 13 |
|
| 14 |
Below follows the original copyright notes by Greg Turk: |
| 15 |
====================================================================== |
| 16 |
The interface routines for reading and writing PLY polygon files. |
| 17 |
|
| 18 |
Greg Turk, February 1994 |
| 19 |
|
| 20 |
--------------------------------------------------------------- |
| 21 |
|
| 22 |
A PLY file contains a single polygonal _object_. |
| 23 |
|
| 24 |
An object is composed of lists of _elements_. Typical elements are |
| 25 |
vertices, faces, edges and materials. |
| 26 |
|
| 27 |
Each type of element for a given object has one or more _properties_ |
| 28 |
associated with the element type. For instance, a vertex element may |
| 29 |
have as properties the floating-point values x,y,z and the three unsigned |
| 30 |
chars representing red, green and blue. |
| 31 |
|
| 32 |
--------------------------------------------------------------- |
| 33 |
|
| 34 |
Copyright (c) 1994 The Board of Trustees of The Leland Stanford |
| 35 |
Junior University. All rights reserved. |
| 36 |
|
| 37 |
Permission to use, copy, modify and distribute this software and its |
| 38 |
documentation for any purpose is hereby granted without fee, provided |
| 39 |
that the above copyright notice and this permission notice appear in |
| 40 |
all copies of this software and that you do not sell the software. |
| 41 |
|
| 42 |
THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND, |
| 43 |
EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY |
| 44 |
WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. |
| 45 |
|
| 46 |
*/ |
| 47 |
|
| 48 |
#include "opengl-precomp.h" // Precompiled headers |
| 49 |
// |
| 50 |
#include <mrpt/core/exceptions.h> |
| 51 |
#include <mrpt/core/reverse_bytes.h> |
| 52 |
#include <mrpt/opengl/PLY_import_export.h> |
| 53 |
#include <mrpt/system/string_utils.h> |
| 54 |
|
| 55 |
#include <array> |
| 56 |
#include <cstdio> |
| 57 |
#include <map> |
| 58 |
|
| 59 |
using namespace std; |
| 60 |
using namespace mrpt; |
| 61 |
using namespace mrpt::opengl; |
| 62 |
using namespace mrpt::math; |
| 63 |
using namespace mrpt::img; |
| 64 |
|
| 65 |
#define PLY_ASCII 1 /* ascii PLY file */ |
| 66 |
#define PLY_BINARY_BE 2 /* binary PLY file, big endian */ |
| 67 |
#define PLY_BINARY_LE 3 /* binary PLY file, little endian */ |
| 68 |
|
| 69 |
//#define PLY_OKAY 0 /* ply routine worked okay */ |
| 70 |
//#define PLY_ERROR -1 /* error in ply routine */ |
| 71 |
|
| 72 |
/* scalar data types supported by PLY format */ |
| 73 |
|
| 74 |
enum PLY_DATA_TYPE |
| 75 |
{ |
| 76 |
PLY_START_TYPE = 0, |
| 77 |
PLY_INVALID = 0, |
| 78 |
PLY_CHAR = 1, |
| 79 |
PLY_SHORT = 2, |
| 80 |
PLY_INT = 3, |
| 81 |
PLY_UCHAR = 4, |
| 82 |
PLY_USHORT = 5, |
| 83 |
PLY_UINT = 6, |
| 84 |
PLY_FLOAT = 7, |
| 85 |
PLY_DOUBLE = 8, |
| 86 |
PLY_END_TYPE = 9 |
| 87 |
}; |
| 88 |
|
| 89 |
//#define PLY_SCALAR 0 |
| 90 |
//#define PLY_LIST 1 |
| 91 |
|
| 92 |
const int NO_OTHER_PROPS = -1; |
| 93 |
|
| 94 |
const char DONT_STORE_PROP = 0; |
| 95 |
const char STORE_PROP = 1; |
| 96 |
|
| 97 |
const char OTHER_PROP = 0; |
| 98 |
const char NAMED_PROP = 1; |
| 99 |
|
| 100 |
struct PlyProperty |
| 101 |
{ /* description of a property */ |
| 102 |
|
| 103 |
PlyProperty() = default; |
| 104 |
|
| 105 |
PlyProperty( |
| 106 |
const char* Name, |
| 107 |
PLY_DATA_TYPE External_type, |
| 108 |
PLY_DATA_TYPE Internal_type, |
| 109 |
size_t Offset, |
| 110 |
bool Is_list = false, |
| 111 |
PLY_DATA_TYPE Count_external = PLY_INVALID, |
| 112 |
PLY_DATA_TYPE Count_internal = PLY_INVALID, |
| 113 |
size_t Count_offset = 0) : |
| 114 |
name(Name), |
| 115 |
external_type(External_type), |
| 116 |
internal_type(Internal_type), |
| 117 |
offset(Offset), |
| 118 |
count_external(Count_external), |
| 119 |
count_internal(Count_internal), |
| 120 |
count_offset(Count_offset) |
| 121 |
{ |
| 122 |
} |
| 123 |
|
| 124 |
std::string name; /* property name */ |
| 125 |
PLY_DATA_TYPE external_type = PLY_INVALID; /* file's data type */ |
| 126 |
PLY_DATA_TYPE internal_type = PLY_INVALID; /* program's data type */ |
| 127 |
|
| 128 |
size_t offset = 0; /* offset bytes of prop in a struct */ |
| 129 |
|
| 130 |
bool is_list = false; |
| 131 |
PLY_DATA_TYPE count_external = PLY_INVALID; /* file's count type */ |
| 132 |
PLY_DATA_TYPE count_internal = PLY_INVALID; /* program's count type */ |
| 133 |
size_t count_offset = 0; /* offset byte for list count */ |
| 134 |
}; |
| 135 |
|
| 136 |
struct PlyElement |
| 137 |
{ /* description of an element */ |
| 138 |
PlyElement() {} |
| 139 |
|
| 140 |
string name; /* element name */ |
| 141 |
int num = 0; /* number of elements in this object */ |
| 142 |
int size = 0; /* size of element (bytes) or -1 if variable */ |
| 143 |
vector<PlyProperty> props; /* list of properties in the file */ |
| 144 |
vector<char> store_prop; /* flags: property wanted by user? */ |
| 145 |
int other_offset = NO_OTHER_PROPS; /* offset to un-asked-for props, or -1 if none*/ |
| 146 |
int other_size = 0; /* size of other_props structure */ |
| 147 |
}; |
| 148 |
|
| 149 |
struct PlyFile |
| 150 |
{ /* description of PLY file */ |
| 151 |
PlyFile(FILE* _fp = nullptr) : fp(_fp) {} |
| 152 |
FILE* fp; /* file pointer */ |
| 153 |
int file_type{0}; /* ascii or binary */ |
| 154 |
float version{0}; /* version number of file */ |
| 155 |
vector<PlyElement> elems; /* list of elements */ |
| 156 |
vector<string> comments; /* list of comments */ |
| 157 |
vector<string> obj_info; /* list of object info items */ |
| 158 |
PlyElement* which_elem{nullptr}; /* which element we're currently writing */ |
| 159 |
}; |
| 160 |
|
| 161 |
using namespace std::string_literals; |
| 162 |
const std::map<std::string, PLY_DATA_TYPE> type_names = { |
| 163 |
{ "char"s, PLY_CHAR}, // |
| 164 |
|
| 165 |
{ "short"s, PLY_SHORT}, // |
| 166 |
{ "int16"s, PLY_SHORT}, // |
| 167 |
|
| 168 |
{ "int"s, PLY_INT}, // |
| 169 |
{ "int32"s, PLY_INT}, // |
| 170 |
|
| 171 |
{ "uchar"s, PLY_UCHAR}, // |
| 172 |
{ "uint8"s, PLY_UCHAR}, // |
| 173 |
|
| 174 |
{ "ushort"s, PLY_USHORT}, // |
| 175 |
{ "uint16"s, PLY_USHORT}, // |
| 176 |
|
| 177 |
{ "uint"s, PLY_UINT}, // |
| 178 |
{ "uint32"s, PLY_UINT}, // |
| 179 |
|
| 180 |
{ "float"s, PLY_FLOAT}, // |
| 181 |
{"float32"s, PLY_FLOAT}, // |
| 182 |
|
| 183 |
{ "double"s, PLY_DOUBLE}, // |
| 184 |
{"float64"s, PLY_DOUBLE}, // |
| 185 |
}; |
| 186 |
|
| 187 |
const std::map<PLY_DATA_TYPE, std::string> type_names_inv = { |
| 188 |
{ PLY_CHAR, "char"}, // |
| 189 |
{ PLY_SHORT, "short"}, // |
| 190 |
{ PLY_INT, "int"}, // |
| 191 |
{ PLY_UCHAR, "uchar"}, // |
| 192 |
{PLY_USHORT, "ushort"}, // |
| 193 |
{ PLY_UINT, "uint"}, // |
| 194 |
{ PLY_FLOAT, "float"s}, // |
| 195 |
{PLY_DOUBLE, "double"}, // |
| 196 |
}; |
| 197 |
|
| 198 |
const int ply_type_size[] = {0, 1, 2, 4, 1, 2, 4, 4, 8}; |
| 199 |
// PLY_CHAR = 1, |
| 200 |
// PLY_SHORT = 2, |
| 201 |
// PLY_INT = 3, |
| 202 |
// PLY_UCHAR = 4, |
| 203 |
// PLY_USHORT = 5, |
| 204 |
// PLY_UINT = 6, |
| 205 |
// PLY_FLOAT = 7, |
| 206 |
// PLY_DOUBLE = 8, |
| 207 |
|
| 208 |
/* find an element in a plyfile's list */ |
| 209 |
PlyElement* find_element(PlyFile*, const std::string& s); |
| 210 |
|
| 211 |
/* find a property in an element's list */ |
| 212 |
PlyProperty* find_property(PlyElement*, const std::string& s, int*); |
| 213 |
|
| 214 |
/* write to a file the word describing a PLY file data type */ |
| 215 |
void write_scalar_type(FILE*, PLY_DATA_TYPE); |
| 216 |
|
| 217 |
/* read a line from a file and break it up into separate words */ |
| 218 |
vector<string> get_words(FILE*, string&); |
| 219 |
|
| 220 |
/* write an item to a file */ |
| 221 |
void write_binary_item(FILE*, int, unsigned int, double, int); |
| 222 |
void write_ascii_item(FILE*, int, unsigned int, double, int); |
| 223 |
|
| 224 |
/* add information to a PLY file descriptor */ |
| 225 |
void add_element(PlyFile*, const vector<string>&); |
| 226 |
void add_property(PlyFile*, const vector<string>&); |
| 227 |
void add_comment(PlyFile*, const string&); |
| 228 |
void add_obj_info(PlyFile*, const string&); |
| 229 |
|
| 230 |
/* copy a property */ |
| 231 |
void copy_property(PlyProperty*, const PlyProperty*); |
| 232 |
|
| 233 |
/* store a value into where a pointer and a type specify */ |
| 234 |
void store_item(char*, int, int, unsigned int, double); |
| 235 |
|
| 236 |
/* return the value of a stored item */ |
| 237 |
void get_stored_item(void*, int, int*, unsigned int*, double*); |
| 238 |
|
| 239 |
/* return the value stored in an item, given ptr to it and its type */ |
| 240 |
double get_item_value(const char*, int); |
| 241 |
|
| 242 |
/* get binary or ascii item and store it according to ptr and type */ |
| 243 |
void get_ascii_item(const char*, int, int*, unsigned int*, double*); |
| 244 |
int get_binary_item(FILE*, int, int, int*, unsigned int*, |
| 245 |
double*); // return 0 on error |
| 246 |
|
| 247 |
/* get a bunch of elements from a file */ |
| 248 |
void ascii_get_element(PlyFile*, char*); |
| 249 |
void binary_get_element(PlyFile*, char*); |
| 250 |
|
| 251 |
/*************/ |
| 252 |
/* Writing */ |
| 253 |
/*************/ |
| 254 |
|
| 255 |
/****************************************************************************** |
| 256 |
Given a file pointer, get ready to write PLY data to the file. |
| 257 |
|
| 258 |
Entry: |
| 259 |
fp - the given file pointer |
| 260 |
nelems - number of elements in object |
| 261 |
elem_names - list of element names |
| 262 |
file_type - file type, either ascii or binary |
| 263 |
|
| 264 |
Exit: |
| 265 |
returns a pointer to a PlyFile, used to refer to this file, or nullptr if |
| 266 |
error |
| 267 |
******************************************************************************/ |
| 268 |
|
| 269 |
PlyFile* ply_write(FILE* fp, const vector<string>& elem_names, int file_type) |
| 270 |
{ |
| 271 |
/* check for nullptr file pointer */ |
| 272 |
if (fp == nullptr) return (nullptr); |
| 273 |
|
| 274 |
/* create a record for this object */ |
| 275 |
auto* plyfile = new PlyFile(fp); |
| 276 |
|
| 277 |
plyfile->file_type = file_type; |
| 278 |
plyfile->version = 1.0; |
| 279 |
// plyfile->other_elems = nullptr; |
| 280 |
|
| 281 |
/* tuck aside the names of the elements */ |
| 282 |
|
| 283 |
plyfile->elems.resize(elem_names.size()); |
| 284 |
for (size_t i = 0; i < elem_names.size(); i++) |
| 285 |
{ |
| 286 |
plyfile->elems[i].name = elem_names[i]; |
| 287 |
} |
| 288 |
|
| 289 |
/* return pointer to the file descriptor */ |
| 290 |
return (plyfile); |
| 291 |
} |
| 292 |
|
| 293 |
/****************************************************************************** |
| 294 |
Open a polygon file for writing. |
| 295 |
|
| 296 |
Entry: |
| 297 |
filename - name of file to read from |
| 298 |
nelems - number of elements in object |
| 299 |
elem_names - list of element names |
| 300 |
file_type - file type, either ascii or binary |
| 301 |
|
| 302 |
Exit: |
| 303 |
version - version number of PLY file |
| 304 |
returns a file identifier, used to refer to this file, or nullptr if error |
| 305 |
******************************************************************************/ |
| 306 |
|
| 307 |
PlyFile* ply_open_for_writing( |
| 308 |
const char* name, const vector<string>& elem_names, int file_type, float* version) |
| 309 |
{ |
| 310 |
PlyFile* plyfile; |
| 311 |
FILE* fp; |
| 312 |
|
| 313 |
/* open the file for writing */ |
| 314 |
|
| 315 |
fp = fopen(name, "w"); |
| 316 |
if (fp == nullptr) |
| 317 |
{ |
| 318 |
return (nullptr); |
| 319 |
} |
| 320 |
|
| 321 |
/* create the actual PlyFile structure */ |
| 322 |
|
| 323 |
plyfile = ply_write(fp, elem_names, file_type); |
| 324 |
if (plyfile == nullptr) return (nullptr); |
| 325 |
|
| 326 |
/* say what PLY file version number we're writing */ |
| 327 |
*version = plyfile->version; |
| 328 |
|
| 329 |
/* return pointer to the file descriptor */ |
| 330 |
return (plyfile); |
| 331 |
} |
| 332 |
|
| 333 |
/****************************************************************************** |
| 334 |
Describe an element, including its properties and how many will be written |
| 335 |
to the file. |
| 336 |
|
| 337 |
Entry: |
| 338 |
plyfile - file identifier |
| 339 |
elem_name - name of element that information is being specified about |
| 340 |
nelems - number of elements of this type to be written |
| 341 |
nprops - number of properties contained in the element |
| 342 |
prop_list - list of properties |
| 343 |
******************************************************************************/ |
| 344 |
|
| 345 |
void ply_describe_element( |
| 346 |
PlyFile* plyfile, const string& elem_name, int nelems, vector<PlyProperty>& prop_list) |
| 347 |
{ |
| 348 |
/* look for appropriate element */ |
| 349 |
PlyElement* elem = find_element(plyfile, elem_name); |
| 350 |
if (elem == nullptr) |
| 351 |
throw std::runtime_error( |
| 352 |
mrpt::format("ply_describe_element: can't find element '%s'", elem_name.c_str())); |
| 353 |
|
| 354 |
elem->num = nelems; |
| 355 |
|
| 356 |
/* copy the list of properties */ |
| 357 |
|
| 358 |
const size_t nprops = prop_list.size(); |
| 359 |
elem->props.resize(nprops); |
| 360 |
elem->store_prop.resize(nprops); |
| 361 |
|
| 362 |
for (size_t i = 0; i < nprops; i++) |
| 363 |
{ |
| 364 |
elem->props[i] = prop_list[i]; |
| 365 |
elem->store_prop[i] = NAMED_PROP; |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
/****************************************************************************** |
| 370 |
Describe a property of an element. |
| 371 |
|
| 372 |
Entry: |
| 373 |
plyfile - file identifier |
| 374 |
elem_name - name of element that information is being specified about |
| 375 |
prop - the new property |
| 376 |
******************************************************************************/ |
| 377 |
|
| 378 |
void ply_describe_property(PlyFile* plyfile, const char* elem_name, const PlyProperty* prop) |
| 379 |
{ |
| 380 |
/* look for appropriate element */ |
| 381 |
PlyElement* elem = find_element(plyfile, elem_name); |
| 382 |
if (elem == nullptr) |
| 383 |
{ |
| 384 |
fprintf(stderr, "ply_describe_property: can't find element '%s'\n", elem_name); |
| 385 |
return; |
| 386 |
} |
| 387 |
|
| 388 |
/* copy the new property */ |
| 389 |
elem->props.push_back(*prop); |
| 390 |
elem->store_prop.push_back(NAMED_PROP); |
| 391 |
} |
| 392 |
|
| 393 |
/****************************************************************************** |
| 394 |
State how many of a given element will be written. |
| 395 |
|
| 396 |
Entry: |
| 397 |
plyfile - file identifier |
| 398 |
elem_name - name of element that information is being specified about |
| 399 |
nelems - number of elements of this type to be written |
| 400 |
******************************************************************************/ |
| 401 |
|
| 402 |
void ply_element_count(PlyFile* plyfile, const string& elem_name, int nelems) |
| 403 |
{ |
| 404 |
// int i; |
| 405 |
PlyElement* elem; |
| 406 |
// PlyProperty *prop; |
| 407 |
|
| 408 |
/* look for appropriate element */ |
| 409 |
elem = find_element(plyfile, elem_name); |
| 410 |
if (elem == nullptr) |
| 411 |
throw std::runtime_error( |
| 412 |
mrpt::format("ply_element_count: can't find element '%s'", elem_name.c_str())); |
| 413 |
|
| 414 |
elem->num = nelems; |
| 415 |
} |
| 416 |
|
| 417 |
/****************************************************************************** |
| 418 |
Signal that we've described everything a PLY file's header and that the |
| 419 |
header should be written to the file. |
| 420 |
|
| 421 |
Entry: |
| 422 |
plyfile - file identifier |
| 423 |
******************************************************************************/ |
| 424 |
|
| 425 |
void ply_header_complete(PlyFile* plyfile) |
| 426 |
{ |
| 427 |
FILE* fp = plyfile->fp; |
| 428 |
|
| 429 |
fprintf(fp, "ply\n"); |
| 430 |
|
| 431 |
switch (plyfile->file_type) |
| 432 |
{ |
| 433 |
case PLY_ASCII: |
| 434 |
fprintf(fp, "format ascii 1.0\n"); |
| 435 |
break; |
| 436 |
case PLY_BINARY_BE: |
| 437 |
fprintf(fp, "format binary_big_endian 1.0\n"); |
| 438 |
break; |
| 439 |
case PLY_BINARY_LE: |
| 440 |
fprintf(fp, "format binary_little_endian 1.0\n"); |
| 441 |
break; |
| 442 |
default: |
| 443 |
throw std::runtime_error( |
| 444 |
mrpt::format("ply_header_complete: bad file type = %d", plyfile->file_type)); |
| 445 |
} |
| 446 |
|
| 447 |
/* write out the comments */ |
| 448 |
|
| 449 |
for (auto& comment : plyfile->comments) fprintf(fp, "comment %s\n", comment.c_str()); |
| 450 |
|
| 451 |
/* write out object information */ |
| 452 |
|
| 453 |
for (auto& i : plyfile->obj_info) fprintf(fp, "obj_info %s\n", i.c_str()); |
| 454 |
|
| 455 |
/* write out information about each element */ |
| 456 |
|
| 457 |
for (auto& i : plyfile->elems) |
| 458 |
{ |
| 459 |
const PlyElement* elem = &i; |
| 460 |
fprintf(fp, "element %s %d\n", elem->name.c_str(), elem->num); |
| 461 |
|
| 462 |
/* write out each property */ |
| 463 |
for (const auto& j : elem->props) |
| 464 |
{ |
| 465 |
const PlyProperty* prop = &j; |
| 466 |
if (prop->is_list) |
| 467 |
{ |
| 468 |
fprintf(fp, "property list "); |
| 469 |
write_scalar_type(fp, prop->count_external); |
| 470 |
fprintf(fp, " "); |
| 471 |
write_scalar_type(fp, prop->external_type); |
| 472 |
fprintf(fp, " %s\n", prop->name.c_str()); |
| 473 |
} |
| 474 |
else |
| 475 |
{ |
| 476 |
fprintf(fp, "property "); |
| 477 |
write_scalar_type(fp, prop->external_type); |
| 478 |
fprintf(fp, " %s\n", prop->name.c_str()); |
| 479 |
} |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
fprintf(fp, "end_header\n"); |
| 484 |
} |
| 485 |
|
| 486 |
/****************************************************************************** |
| 487 |
Specify which elements are going to be written. This should be called |
| 488 |
before a call to the routine ply_put_element(). |
| 489 |
|
| 490 |
Entry: |
| 491 |
plyfile - file identifier |
| 492 |
elem_name - name of element we're talking about |
| 493 |
******************************************************************************/ |
| 494 |
|
| 495 |
void ply_put_element_setup(PlyFile* plyfile, const string& elem_name) |
| 496 |
{ |
| 497 |
PlyElement* elem; |
| 498 |
|
| 499 |
elem = find_element(plyfile, elem_name); |
| 500 |
if (elem == nullptr) |
| 501 |
throw std::runtime_error( |
| 502 |
mrpt::format("ply_elements_setup: can't find element '%s'", elem_name.c_str())); |
| 503 |
|
| 504 |
plyfile->which_elem = elem; |
| 505 |
} |
| 506 |
|
| 507 |
/****************************************************************************** |
| 508 |
Write an element to the file. This routine assumes that we're |
| 509 |
writing the type of element specified in the last call to the routine |
| 510 |
ply_put_element_setup(). |
| 511 |
|
| 512 |
Entry: |
| 513 |
plyfile - file identifier |
| 514 |
elem_ptr - pointer to the element |
| 515 |
******************************************************************************/ |
| 516 |
|
| 517 |
void ply_put_element(PlyFile* plyfile, void* elem_ptr) |
| 518 |
{ |
| 519 |
FILE* fp = plyfile->fp; |
| 520 |
char *elem_data, *item; |
| 521 |
char** item_ptr; |
| 522 |
int item_size; |
| 523 |
int int_val; |
| 524 |
unsigned int uint_val; |
| 525 |
double double_val; |
| 526 |
char** other_ptr; |
| 527 |
|
| 528 |
PlyElement* elem = plyfile->which_elem; |
| 529 |
elem_data = (char*)elem_ptr; |
| 530 |
other_ptr = (char**)(((char*)elem_ptr) + elem->other_offset); |
| 531 |
|
| 532 |
/* write out either to an ascii or binary file */ |
| 533 |
|
| 534 |
if (plyfile->file_type == PLY_ASCII) |
| 535 |
{ |
| 536 |
/* write an ascii file */ |
| 537 |
|
| 538 |
/* write out each property of the element */ |
| 539 |
for (size_t j = 0; j < elem->props.size(); j++) |
| 540 |
{ |
| 541 |
const PlyProperty* prop = &elem->props[j]; |
| 542 |
if (elem->store_prop[j] == OTHER_PROP) |
| 543 |
elem_data = *other_ptr; |
| 544 |
else |
| 545 |
elem_data = (char*)elem_ptr; |
| 546 |
if (prop->is_list) |
| 547 |
{ |
| 548 |
item = elem_data + prop->count_offset; |
| 549 |
get_stored_item((void*)item, prop->count_internal, &int_val, &uint_val, &double_val); |
| 550 |
write_ascii_item(fp, int_val, uint_val, double_val, prop->count_external); |
| 551 |
const size_t list_count = uint_val; |
| 552 |
item_ptr = (char**)(elem_data + prop->offset); |
| 553 |
item = item_ptr[0]; |
| 554 |
item_size = ply_type_size[prop->internal_type]; |
| 555 |
for (size_t k = 0; k < list_count; k++) |
| 556 |
{ |
| 557 |
get_stored_item((void*)item, prop->internal_type, &int_val, &uint_val, &double_val); |
| 558 |
write_ascii_item(fp, int_val, uint_val, double_val, prop->external_type); |
| 559 |
item += item_size; |
| 560 |
} |
| 561 |
} |
| 562 |
else |
| 563 |
{ |
| 564 |
item = elem_data + prop->offset; |
| 565 |
get_stored_item((void*)item, prop->internal_type, &int_val, &uint_val, &double_val); |
| 566 |
write_ascii_item(fp, int_val, uint_val, double_val, prop->external_type); |
| 567 |
} |
| 568 |
} |
| 569 |
|
| 570 |
fprintf(fp, "\n"); |
| 571 |
} |
| 572 |
else |
| 573 |
{ |
| 574 |
/* write a binary file */ |
| 575 |
|
| 576 |
/* write out each property of the element */ |
| 577 |
for (size_t j = 0; j < elem->props.size(); j++) |
| 578 |
{ |
| 579 |
const PlyProperty* prop = &elem->props[j]; |
| 580 |
if (elem->store_prop[j] == OTHER_PROP) |
| 581 |
elem_data = *other_ptr; |
| 582 |
else |
| 583 |
elem_data = (char*)elem_ptr; |
| 584 |
if (prop->is_list) |
| 585 |
{ |
| 586 |
item = elem_data + prop->count_offset; |
| 587 |
item_size = ply_type_size[prop->count_internal]; |
| 588 |
get_stored_item((void*)item, prop->count_internal, &int_val, &uint_val, &double_val); |
| 589 |
write_binary_item(fp, int_val, uint_val, double_val, prop->count_external); |
| 590 |
const size_t list_count = uint_val; |
| 591 |
item_ptr = (char**)(elem_data + prop->offset); |
| 592 |
item = item_ptr[0]; |
| 593 |
item_size = ply_type_size[prop->internal_type]; |
| 594 |
for (size_t k = 0; k < list_count; k++) |
| 595 |
{ |
| 596 |
get_stored_item((void*)item, prop->internal_type, &int_val, &uint_val, &double_val); |
| 597 |
write_binary_item(fp, int_val, uint_val, double_val, prop->external_type); |
| 598 |
item += item_size; |
| 599 |
} |
| 600 |
} |
| 601 |
else |
| 602 |
{ |
| 603 |
item = elem_data + prop->offset; |
| 604 |
item_size = ply_type_size[prop->internal_type]; |
| 605 |
get_stored_item((void*)item, prop->internal_type, &int_val, &uint_val, &double_val); |
| 606 |
write_binary_item(fp, int_val, uint_val, double_val, prop->external_type); |
| 607 |
} |
| 608 |
} |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
/****************************************************************************** |
| 613 |
Specify a comment that will be written in the header. |
| 614 |
|
| 615 |
Entry: |
| 616 |
plyfile - file identifier |
| 617 |
comment - the comment to be written |
| 618 |
******************************************************************************/ |
| 619 |
|
| 620 |
void ply_put_comment(PlyFile* plyfile, const string& comment) |
| 621 |
{ |
| 622 |
plyfile->comments.push_back(comment); |
| 623 |
} |
| 624 |
|
| 625 |
/****************************************************************************** |
| 626 |
Specify a piece of object information (arbitrary text) that will be written |
| 627 |
in the header. |
| 628 |
|
| 629 |
Entry: |
| 630 |
plyfile - file identifier |
| 631 |
obj_info - the text information to be written |
| 632 |
******************************************************************************/ |
| 633 |
|
| 634 |
void ply_put_obj_info(PlyFile* plyfile, const string& obj_info) |
| 635 |
{ |
| 636 |
plyfile->obj_info.push_back(obj_info); |
| 637 |
} |
| 638 |
|
| 639 |
/*************/ |
| 640 |
/* Reading */ |
| 641 |
/*************/ |
| 642 |
|
| 643 |
/****************************************************************************** |
| 644 |
Given a file pointer, get ready to read PLY data from the file. |
| 645 |
|
| 646 |
Entry: |
| 647 |
fp - the given file pointer |
| 648 |
|
| 649 |
Exit: |
| 650 |
nelems - number of elements in object |
| 651 |
elem_names - list of element names |
| 652 |
returns a pointer to a PlyFile, used to refer to this file, or nullptr if |
| 653 |
error |
| 654 |
******************************************************************************/ |
| 655 |
|
| 656 |
PlyFile* ply_read(FILE* fp, vector<string>& elem_names) |
| 657 |
{ |
| 658 |
// int found_format = 0; |
| 659 |
|
| 660 |
/* check for nullptr file pointer */ |
| 661 |
if (fp == nullptr) return (nullptr); |
| 662 |
|
| 663 |
/* create record for this object */ |
| 664 |
auto* plyfile = new PlyFile(fp); |
| 665 |
|
| 666 |
/* read and parse the file's header */ |
| 667 |
string orig_line; |
| 668 |
vector<string> words = get_words(plyfile->fp, orig_line); |
| 669 |
|
| 670 |
if (words.empty() || words[0] != "ply") return nullptr; |
| 671 |
|
| 672 |
while (!words.empty()) |
| 673 |
{ |
| 674 |
/* parse words */ |
| 675 |
|
| 676 |
if (words[0] == "format") |
| 677 |
{ |
| 678 |
if (words.size() != 3) return (nullptr); |
| 679 |
if (words[1] == "ascii") |
| 680 |
plyfile->file_type = PLY_ASCII; |
| 681 |
else if (words[1] == "binary_big_endian") |
| 682 |
plyfile->file_type = PLY_BINARY_BE; |
| 683 |
else if (words[1] == "binary_little_endian") |
| 684 |
plyfile->file_type = PLY_BINARY_LE; |
| 685 |
else |
| 686 |
return (nullptr); |
| 687 |
plyfile->version = d2f(atof(words[2].c_str())); |
| 688 |
// found_format = 1; |
| 689 |
} |
| 690 |
else if (words[0] == "element") |
| 691 |
add_element(plyfile, words); |
| 692 |
else if (words[0] == "property") |
| 693 |
add_property(plyfile, words); |
| 694 |
else if (words[0] == "comment") |
| 695 |
add_comment(plyfile, orig_line); |
| 696 |
else if (words[0] == "obj_info") |
| 697 |
add_obj_info(plyfile, orig_line); |
| 698 |
else if (words[0] == "end_header") |
| 699 |
break; |
| 700 |
|
| 701 |
words = get_words(plyfile->fp, orig_line); |
| 702 |
} |
| 703 |
|
| 704 |
/* create tags for each property of each element, to be used */ |
| 705 |
/* later to say whether or not to store each property for the user */ |
| 706 |
|
| 707 |
for (auto& i : plyfile->elems) |
| 708 |
{ |
| 709 |
PlyElement* elem = &i; |
| 710 |
|
| 711 |
elem->store_prop.assign(elem->props.size(), DONT_STORE_PROP); |
| 712 |
elem->other_offset = NO_OTHER_PROPS; /* no "other" props by default */ |
| 713 |
} |
| 714 |
|
| 715 |
/* set return values about the elements */ |
| 716 |
elem_names.clear(); |
| 717 |
for (auto& elem : plyfile->elems) elem_names.push_back(elem.name); |
| 718 |
|
| 719 |
/* return a pointer to the file's information */ |
| 720 |
|
| 721 |
return (plyfile); |
| 722 |
} |
| 723 |
|
| 724 |
/****************************************************************************** |
| 725 |
Open a polygon file for reading. |
| 726 |
|
| 727 |
Entry: |
| 728 |
filename - name of file to read from |
| 729 |
|
| 730 |
Exit: |
| 731 |
nelems - number of elements in object |
| 732 |
elem_names - list of element names |
| 733 |
file_type - file type, either ascii or binary |
| 734 |
version - version number of PLY file |
| 735 |
returns a file identifier, used to refer to this file, or nullptr if error |
| 736 |
******************************************************************************/ |
| 737 |
|
| 738 |
PlyFile* ply_open_for_reading( |
| 739 |
const char* filename, vector<string>& elem_names, int* file_type, float* version) |
| 740 |
{ |
| 741 |
FILE* fp; |
| 742 |
PlyFile* plyfile; |
| 743 |
|
| 744 |
/* open the file for reading */ |
| 745 |
|
| 746 |
fp = fopen(filename, "r"); |
| 747 |
if (fp == nullptr) return (nullptr); |
| 748 |
|
| 749 |
/* create the PlyFile data structure */ |
| 750 |
|
| 751 |
plyfile = ply_read(fp, elem_names); |
| 752 |
|
| 753 |
/* determine the file type and version */ |
| 754 |
if (plyfile) |
| 755 |
{ |
| 756 |
*file_type = plyfile->file_type; |
| 757 |
*version = plyfile->version; |
| 758 |
} |
| 759 |
|
| 760 |
/* return a pointer to the file's information */ |
| 761 |
|
| 762 |
return (plyfile); |
| 763 |
} |
| 764 |
|
| 765 |
/****************************************************************************** |
| 766 |
Get information about a particular element. |
| 767 |
|
| 768 |
Entry: |
| 769 |
plyfile - file identifier |
| 770 |
elem_name - name of element to get information about |
| 771 |
|
| 772 |
Exit: |
| 773 |
nelems - number of elements of this type in the file |
| 774 |
nprops - number of properties |
| 775 |
returns a list of properties, or nullptr if the file doesn't contain that elem |
| 776 |
******************************************************************************/ |
| 777 |
|
| 778 |
vector<PlyProperty> ply_get_element_description( |
| 779 |
PlyFile* plyfile, const string& elem_name, int& nelems, int& nprops) |
| 780 |
{ |
| 781 |
/* find information about the element */ |
| 782 |
PlyElement* elem = find_element(plyfile, elem_name); |
| 783 |
if (elem == nullptr) return vector<PlyProperty>(); |
| 784 |
|
| 785 |
nelems = elem->num; |
| 786 |
nprops = elem->props.size(); |
| 787 |
|
| 788 |
/* make a copy of the element's property list */ |
| 789 |
return elem->props; |
| 790 |
} |
| 791 |
|
| 792 |
/****************************************************************************** |
| 793 |
Specify a property of an element that is to be returned. This should be |
| 794 |
called (usually multiple times) before a call to the routine ply_get_element(). |
| 795 |
This routine should be used in preference to the less flexible old routine |
| 796 |
called ply_get_element_setup(). |
| 797 |
|
| 798 |
Entry: |
| 799 |
plyfile - file identifier |
| 800 |
elem_name - which element we're talking about |
| 801 |
prop - property to add to those that will be returned |
| 802 |
******************************************************************************/ |
| 803 |
|
| 804 |
void ply_get_property(PlyFile* plyfile, const string& elem_name, const PlyProperty* prop) |
| 805 |
{ |
| 806 |
PlyElement* elem = nullptr; |
| 807 |
PlyProperty* prop_ptr = nullptr; |
| 808 |
int index; |
| 809 |
|
| 810 |
/* find information about the element */ |
| 811 |
elem = find_element(plyfile, elem_name); |
| 812 |
plyfile->which_elem = elem; |
| 813 |
|
| 814 |
/* deposit the property information into the element's description */ |
| 815 |
|
| 816 |
prop_ptr = find_property(elem, prop->name, &index); |
| 817 |
if (prop_ptr == nullptr) |
| 818 |
{ |
| 819 |
// "Warning: Can't find property '%s' in element '%s'\n", |
| 820 |
// prop->name.c_str(), elem_name.c_str() |
| 821 |
return; |
| 822 |
} |
| 823 |
prop_ptr->internal_type = prop->internal_type; |
| 824 |
prop_ptr->offset = prop->offset; |
| 825 |
prop_ptr->count_internal = prop->count_internal; |
| 826 |
prop_ptr->count_offset = prop->count_offset; |
| 827 |
|
| 828 |
/* specify that the user wants this property */ |
| 829 |
elem->store_prop[index] = STORE_PROP; |
| 830 |
} |
| 831 |
|
| 832 |
/****************************************************************************** |
| 833 |
Read one element from the file. This routine assumes that we're reading |
| 834 |
the type of element specified in the last call to the routine |
| 835 |
ply_get_element_setup(). |
| 836 |
|
| 837 |
Entry: |
| 838 |
plyfile - file identifier |
| 839 |
elem_ptr - pointer to location where the element information should be put |
| 840 |
******************************************************************************/ |
| 841 |
|
| 842 |
void ply_get_element(PlyFile* plyfile, void* elem_ptr) |
| 843 |
{ |
| 844 |
if (plyfile->file_type == PLY_ASCII) |
| 845 |
ascii_get_element(plyfile, (char*)elem_ptr); |
| 846 |
else |
| 847 |
binary_get_element(plyfile, (char*)elem_ptr); |
| 848 |
} |
| 849 |
|
| 850 |
/****************************************************************************** |
| 851 |
Extract the comments from the header information of a PLY file. |
| 852 |
|
| 853 |
Entry: |
| 854 |
plyfile - file identifier |
| 855 |
|
| 856 |
Exit: |
| 857 |
num_comments - number of comments returned |
| 858 |
returns a pointer to a list of comments |
| 859 |
******************************************************************************/ |
| 860 |
|
| 861 |
void ply_get_comments(PlyFile* plyfile, vector<string>& comments) { comments = plyfile->comments; } |
| 862 |
|
| 863 |
/****************************************************************************** |
| 864 |
Extract the object information (arbitrary text) from the header information |
| 865 |
of a PLY file. |
| 866 |
|
| 867 |
Entry: |
| 868 |
plyfile - file identifier |
| 869 |
|
| 870 |
Exit: |
| 871 |
num_obj_info - number of lines of text information returned |
| 872 |
returns a pointer to a list of object info lines |
| 873 |
******************************************************************************/ |
| 874 |
|
| 875 |
void ply_get_obj_info(PlyFile* plyfile, vector<string>& obj_info) { obj_info = plyfile->obj_info; } |
| 876 |
|
| 877 |
/*******************/ |
| 878 |
/* Miscellaneous */ |
| 879 |
/*******************/ |
| 880 |
|
| 881 |
/****************************************************************************** |
| 882 |
Close a PLY file. |
| 883 |
|
| 884 |
Entry: |
| 885 |
plyfile - identifier of file to close |
| 886 |
******************************************************************************/ |
| 887 |
|
| 888 |
void ply_close(PlyFile* plyfile) |
| 889 |
{ |
| 890 |
fclose(plyfile->fp); |
| 891 |
|
| 892 |
/* free up memory associated with the PLY file */ |
| 893 |
delete plyfile; |
| 894 |
} |
| 895 |
|
| 896 |
/****************************************************************************** |
| 897 |
Get version number and file type of a PlyFile. |
| 898 |
|
| 899 |
Entry: |
| 900 |
ply - pointer to PLY file |
| 901 |
|
| 902 |
Exit: |
| 903 |
version - version of the file |
| 904 |
file_type - PLY_ASCII, PLY_BINARY_BE, or PLY_BINARY_LE |
| 905 |
******************************************************************************/ |
| 906 |
|
| 907 |
void ply_get_info(PlyFile* ply, float* version, int* file_type) |
| 908 |
{ |
| 909 |
if (ply == nullptr) return; |
| 910 |
|
| 911 |
*version = ply->version; |
| 912 |
*file_type = ply->file_type; |
| 913 |
} |
| 914 |
|
| 915 |
/****************************************************************************** |
| 916 |
Find an element from the element list of a given PLY object. |
| 917 |
|
| 918 |
Entry: |
| 919 |
plyfile - file id for PLY file |
| 920 |
element - name of element we're looking for |
| 921 |
|
| 922 |
Exit: |
| 923 |
returns the element, or nullptr if not found |
| 924 |
******************************************************************************/ |
| 925 |
|
| 926 |
PlyElement* find_element(PlyFile* plyfile, const string& element) |
| 927 |
{ |
| 928 |
for (auto& elem : plyfile->elems) |
| 929 |
if (element == elem.name) return &elem; |
| 930 |
|
| 931 |
return (nullptr); |
| 932 |
} |
| 933 |
|
| 934 |
/****************************************************************************** |
| 935 |
Find a property in the list of properties of a given element. |
| 936 |
|
| 937 |
Entry: |
| 938 |
elem - pointer to element in which we want to find the property |
| 939 |
prop_name - name of property to find |
| 940 |
|
| 941 |
Exit: |
| 942 |
index - index to position in list |
| 943 |
returns a pointer to the property, or nullptr if not found |
| 944 |
******************************************************************************/ |
| 945 |
|
| 946 |
PlyProperty* find_property(PlyElement* elem, const std::string& prop_name, int* index) |
| 947 |
{ |
| 948 |
for (size_t i = 0; i < elem->props.size(); i++) |
| 949 |
if (string(prop_name) == elem->props[i].name) |
| 950 |
{ |
| 951 |
*index = i; |
| 952 |
return &elem->props[i]; |
| 953 |
} |
| 954 |
*index = -1; |
| 955 |
return (nullptr); |
| 956 |
} |
| 957 |
|
| 958 |
/****************************************************************************** |
| 959 |
Read an element from an ascii file. |
| 960 |
|
| 961 |
Entry: |
| 962 |
plyfile - file identifier |
| 963 |
elem_ptr - pointer to element |
| 964 |
******************************************************************************/ |
| 965 |
|
| 966 |
void ascii_get_element(PlyFile* plyfile, char* elem_ptr) |
| 967 |
{ |
| 968 |
int which_word; |
| 969 |
// FILE *fp = plyfile->fp; |
| 970 |
char *elem_data, *item = nullptr; |
| 971 |
char* item_ptr; |
| 972 |
int item_size; |
| 973 |
int int_val; |
| 974 |
unsigned int uint_val; |
| 975 |
double double_val; |
| 976 |
int list_count; |
| 977 |
int store_it; |
| 978 |
char** store_array; |
| 979 |
char* other_data = nullptr; |
| 980 |
int other_flag; |
| 981 |
|
| 982 |
/* the kind of element we're reading currently */ |
| 983 |
PlyElement* elem = plyfile->which_elem; |
| 984 |
|
| 985 |
/* do we need to setup for other_props? */ |
| 986 |
|
| 987 |
if (elem->other_offset != NO_OTHER_PROPS) |
| 988 |
{ |
| 989 |
char** ptr; |
| 990 |
other_flag = 1; |
| 991 |
/* make room for other_props */ |
| 992 |
other_data = (char*)malloc(elem->other_size); |
| 993 |
/* store pointer in user's structure to the other_props */ |
| 994 |
ptr = (char**)(elem_ptr + elem->other_offset); |
| 995 |
*ptr = other_data; |
| 996 |
} |
| 997 |
else |
| 998 |
other_flag = 0; |
| 999 |
|
| 1000 |
/* read in the element */ |
| 1001 |
string orig_line; |
| 1002 |
vector<string> words = get_words(plyfile->fp, orig_line); |
| 1003 |
|
| 1004 |
if (words.empty()) throw std::runtime_error("ply_get_element: unexpected end of file"); |
| 1005 |
|
| 1006 |
which_word = 0; |
| 1007 |
|
| 1008 |
for (size_t j = 0; j < elem->props.size(); j++) |
| 1009 |
{ |
| 1010 |
PlyProperty* prop = &elem->props[j]; |
| 1011 |
store_it = (elem->store_prop[j] | other_flag); |
| 1012 |
|
| 1013 |
/* store either in the user's structure or in other_props */ |
| 1014 |
if (elem->store_prop[j]) |
| 1015 |
elem_data = elem_ptr; |
| 1016 |
else |
| 1017 |
elem_data = other_data; |
| 1018 |
|
| 1019 |
if (prop->is_list) |
| 1020 |
{ /* a list */ |
| 1021 |
|
| 1022 |
/* get and store the number of items in the list */ |
| 1023 |
get_ascii_item( |
| 1024 |
words[which_word++].c_str(), prop->count_external, &int_val, &uint_val, &double_val); |
| 1025 |
if (store_it) |
| 1026 |
{ |
| 1027 |
item = elem_data + prop->count_offset; |
| 1028 |
store_item(item, prop->count_internal, int_val, uint_val, double_val); |
| 1029 |
} |
| 1030 |
|
| 1031 |
/* allocate space for an array of items and store a ptr to the array |
| 1032 |
*/ |
| 1033 |
list_count = int_val; |
| 1034 |
item_size = ply_type_size[prop->internal_type]; |
| 1035 |
store_array = (char**)(elem_data + prop->offset); |
| 1036 |
|
| 1037 |
if (list_count == 0) |
| 1038 |
{ |
| 1039 |
if (store_it) *store_array = nullptr; |
| 1040 |
} |
| 1041 |
else |
| 1042 |
{ |
| 1043 |
if (store_it) |
| 1044 |
{ |
| 1045 |
item_ptr = (char*)malloc(sizeof(char) * item_size * list_count); |
| 1046 |
item = item_ptr; |
| 1047 |
*store_array = item_ptr; |
| 1048 |
} |
| 1049 |
|
| 1050 |
/* read items and store them into the array */ |
| 1051 |
for (int k = 0; k < list_count; k++) |
| 1052 |
{ |
| 1053 |
get_ascii_item( |
| 1054 |
words[which_word++].c_str(), prop->external_type, &int_val, &uint_val, &double_val); |
| 1055 |
if (store_it) |
| 1056 |
{ |
| 1057 |
store_item(item, prop->internal_type, int_val, uint_val, double_val); |
| 1058 |
item += item_size; |
| 1059 |
} |
| 1060 |
} |
| 1061 |
} |
| 1062 |
} |
| 1063 |
else |
| 1064 |
{ /* not a list */ |
| 1065 |
get_ascii_item( |
| 1066 |
words[which_word++].c_str(), prop->external_type, &int_val, &uint_val, &double_val); |
| 1067 |
if (store_it) |
| 1068 |
{ |
| 1069 |
item = elem_data + prop->offset; |
| 1070 |
store_item(item, prop->internal_type, int_val, uint_val, double_val); |
| 1071 |
} |
| 1072 |
} |
| 1073 |
} |
| 1074 |
} |
| 1075 |
|
| 1076 |
/****************************************************************************** |
| 1077 |
Read an element from a binary file. |
| 1078 |
|
| 1079 |
Entry: |
| 1080 |
plyfile - file identifier |
| 1081 |
elem_ptr - pointer to an element |
| 1082 |
******************************************************************************/ |
| 1083 |
|
| 1084 |
void binary_get_element(PlyFile* plyfile, char* elem_ptr) |
| 1085 |
{ |
| 1086 |
FILE* fp = plyfile->fp; |
| 1087 |
char *elem_data, *item = nullptr; |
| 1088 |
char* item_ptr; |
| 1089 |
int item_size = 0; |
| 1090 |
int int_val; |
| 1091 |
unsigned int uint_val; |
| 1092 |
double double_val; |
| 1093 |
int list_count; |
| 1094 |
int store_it; |
| 1095 |
char** store_array; |
| 1096 |
char* other_data = nullptr; |
| 1097 |
int other_flag; |
| 1098 |
|
| 1099 |
int bin_file_type = plyfile->file_type; |
| 1100 |
|
| 1101 |
/* the kind of element we're reading currently */ |
| 1102 |
PlyElement* elem = plyfile->which_elem; |
| 1103 |
|
| 1104 |
/* do we need to setup for other_props? */ |
| 1105 |
|
| 1106 |
if (elem->other_offset != NO_OTHER_PROPS) |
| 1107 |
{ |
| 1108 |
char** ptr; |
| 1109 |
other_flag = 1; |
| 1110 |
/* make room for other_props */ |
| 1111 |
other_data = (char*)malloc(elem->other_size); |
| 1112 |
/* store pointer in user's structure to the other_props */ |
| 1113 |
ptr = (char**)(elem_ptr + elem->other_offset); |
| 1114 |
*ptr = other_data; |
| 1115 |
} |
| 1116 |
else |
| 1117 |
other_flag = 0; |
| 1118 |
|
| 1119 |
/* read in a number of elements */ |
| 1120 |
|
| 1121 |
for (size_t j = 0; j < elem->props.size(); j++) |
| 1122 |
{ |
| 1123 |
PlyProperty* prop = &elem->props[j]; |
| 1124 |
store_it = (elem->store_prop[j] | other_flag); |
| 1125 |
|
| 1126 |
/* store either in the user's structure or in other_props */ |
| 1127 |
if (elem->store_prop[j]) |
| 1128 |
elem_data = elem_ptr; |
| 1129 |
else |
| 1130 |
elem_data = other_data; |
| 1131 |
|
| 1132 |
if (prop->is_list) |
| 1133 |
{ /* a list */ |
| 1134 |
|
| 1135 |
/* get and store the number of items in the list */ |
| 1136 |
if (!get_binary_item( |
| 1137 |
fp, bin_file_type, prop->count_external, &int_val, &uint_val, &double_val)) |
| 1138 |
{ |
| 1139 |
// Error... |
| 1140 |
fprintf(stderr, "RPly::binary_get_element: Error reading binary file!\n"); |
| 1141 |
} |
| 1142 |
|
| 1143 |
if (store_it) |
| 1144 |
{ |
| 1145 |
item = elem_data + prop->count_offset; |
| 1146 |
store_item(item, prop->count_internal, int_val, uint_val, double_val); |
| 1147 |
} |
| 1148 |
|
| 1149 |
/* allocate space for an array of items and store a ptr to the array |
| 1150 |
*/ |
| 1151 |
list_count = int_val; |
| 1152 |
/* The "if" was added by Afra Zomorodian 8/22/95 |
| 1153 |
* so that zipper won't crash reading plies that have additional |
| 1154 |
* properties. |
| 1155 |
*/ |
| 1156 |
if (store_it) |
| 1157 |
{ |
| 1158 |
item_size = ply_type_size[prop->internal_type]; |
| 1159 |
} |
| 1160 |
store_array = (char**)(elem_data + prop->offset); |
| 1161 |
if (list_count == 0) |
| 1162 |
{ |
| 1163 |
if (store_it) *store_array = nullptr; |
| 1164 |
} |
| 1165 |
else |
| 1166 |
{ |
| 1167 |
if (store_it) |
| 1168 |
{ |
| 1169 |
item_ptr = (char*)malloc(sizeof(char) * item_size * list_count); |
| 1170 |
item = item_ptr; |
| 1171 |
*store_array = item_ptr; |
| 1172 |
} |
| 1173 |
|
| 1174 |
/* read items and store them into the array */ |
| 1175 |
for (int k = 0; k < list_count; k++) |
| 1176 |
{ |
| 1177 |
if (!get_binary_item( |
| 1178 |
fp, bin_file_type, prop->external_type, &int_val, &uint_val, &double_val)) |
| 1179 |
{ |
| 1180 |
// Error... |
| 1181 |
fprintf( |
| 1182 |
stderr, |
| 1183 |
"RPly::binary_get_element: Error reading binary " |
| 1184 |
"file!\n"); |
| 1185 |
} |
| 1186 |
|
| 1187 |
if (store_it) |
| 1188 |
{ |
| 1189 |
store_item(item, prop->internal_type, int_val, uint_val, double_val); |
| 1190 |
item += item_size; |
| 1191 |
} |
| 1192 |
} |
| 1193 |
} |
| 1194 |
} |
| 1195 |
else |
| 1196 |
{ /* not a list */ |
| 1197 |
if (!get_binary_item( |
| 1198 |
fp, bin_file_type, prop->external_type, &int_val, &uint_val, &double_val)) |
| 1199 |
{ |
| 1200 |
// Error... |
| 1201 |
fprintf(stderr, "RPly::binary_get_element: Error reading binary file!\n"); |
| 1202 |
} |
| 1203 |
|
| 1204 |
if (store_it) |
| 1205 |
{ |
| 1206 |
item = elem_data + prop->offset; |
| 1207 |
store_item(item, prop->internal_type, int_val, uint_val, double_val); |
| 1208 |
} |
| 1209 |
} |
| 1210 |
} |
| 1211 |
} |
| 1212 |
|
| 1213 |
/****************************************************************************** |
| 1214 |
Write to a file the word that represents a PLY data type. |
| 1215 |
|
| 1216 |
Entry: |
| 1217 |
fp - file pointer |
| 1218 |
code - code for type |
| 1219 |
******************************************************************************/ |
| 1220 |
|
| 1221 |
void write_scalar_type(FILE* fp, PLY_DATA_TYPE code) |
| 1222 |
{ |
| 1223 |
/* make sure this is a valid code */ |
| 1224 |
|
| 1225 |
if (code <= PLY_START_TYPE || code >= PLY_END_TYPE) |
| 1226 |
throw std::runtime_error(mrpt::format("write_scalar_type: bad data code = %d", code)); |
| 1227 |
|
| 1228 |
/* write the code to a file */ |
| 1229 |
|
| 1230 |
fprintf(fp, "%s", type_names_inv.at(code).c_str()); |
| 1231 |
} |
| 1232 |
|
| 1233 |
/****************************************************************************** |
| 1234 |
Get a text line from a file and break it up into words. |
| 1235 |
|
| 1236 |
IMPORTANT: The calling routine call "free" on the returned pointer once |
| 1237 |
finished with it. |
| 1238 |
|
| 1239 |
Entry: |
| 1240 |
fp - file to read from |
| 1241 |
|
| 1242 |
Exit: |
| 1243 |
nwords - number of words returned |
| 1244 |
orig_line - the original line of characters |
| 1245 |
returns a list of words from the line, or nullptr if end-of-file |
| 1246 |
******************************************************************************/ |
| 1247 |
|
| 1248 |
vector<string> get_words(FILE* fp, string& orig_line) |
| 1249 |
{ |
| 1250 |
#define BIG_STRING 4096 |
| 1251 |
char str[BIG_STRING]; |
| 1252 |
|
| 1253 |
vector<string> words; |
| 1254 |
|
| 1255 |
ASSERT_(fp != nullptr); |
| 1256 |
|
| 1257 |
/* read in a line */ |
| 1258 |
char* result = fgets(str, BIG_STRING, fp); |
| 1259 |
if (result == nullptr) |
| 1260 |
{ |
| 1261 |
orig_line = string(); |
| 1262 |
return words; |
| 1263 |
} |
| 1264 |
|
| 1265 |
orig_line = string(str); |
| 1266 |
mrpt::system::tokenize(orig_line, " \t\r\n", words); |
| 1267 |
|
| 1268 |
return words; |
| 1269 |
} |
| 1270 |
|
| 1271 |
/****************************************************************************** |
| 1272 |
Return the value of an item, given a pointer to it and its type. |
| 1273 |
|
| 1274 |
Entry: |
| 1275 |
item - pointer to item |
| 1276 |
type - data type that "item" points to |
| 1277 |
|
| 1278 |
Exit: |
| 1279 |
returns a double-precision float that contains the value of the item |
| 1280 |
******************************************************************************/ |
| 1281 |
|
| 1282 |
double get_item_value(char* item, int type) |
| 1283 |
{ |
| 1284 |
unsigned char* puchar; |
| 1285 |
char* pchar; |
| 1286 |
short int* pshort; |
| 1287 |
unsigned short int* pushort; |
| 1288 |
int* pint; |
| 1289 |
unsigned int* puint; |
| 1290 |
float* pfloat; |
| 1291 |
double* pdouble; |
| 1292 |
int int_value; |
| 1293 |
unsigned int uint_value; |
| 1294 |
double double_value; |
| 1295 |
|
| 1296 |
switch (type) |
| 1297 |
{ |
| 1298 |
case PLY_CHAR: |
| 1299 |
pchar = (char*)item; |
| 1300 |
int_value = *pchar; |
| 1301 |
return ((double)int_value); |
| 1302 |
case PLY_UCHAR: |
| 1303 |
puchar = (unsigned char*)item; |
| 1304 |
int_value = *puchar; |
| 1305 |
return ((double)int_value); |
| 1306 |
case PLY_SHORT: |
| 1307 |
pshort = (short int*)item; |
| 1308 |
int_value = *pshort; |
| 1309 |
return ((double)int_value); |
| 1310 |
case PLY_USHORT: |
| 1311 |
pushort = (unsigned short int*)item; |
| 1312 |
int_value = *pushort; |
| 1313 |
return ((double)int_value); |
| 1314 |
case PLY_INT: |
| 1315 |
pint = (int*)item; |
| 1316 |
int_value = *pint; |
| 1317 |
return ((double)int_value); |
| 1318 |
case PLY_UINT: |
| 1319 |
puint = (unsigned int*)item; |
| 1320 |
uint_value = *puint; |
| 1321 |
return ((double)uint_value); |
| 1322 |
case PLY_FLOAT: |
| 1323 |
pfloat = (float*)item; |
| 1324 |
double_value = *pfloat; |
| 1325 |
return (double_value); |
| 1326 |
case PLY_DOUBLE: |
| 1327 |
pdouble = (double*)item; |
| 1328 |
double_value = *pdouble; |
| 1329 |
return (double_value); |
| 1330 |
default: |
| 1331 |
throw std::runtime_error(mrpt::format("get_item_value: bad type = %d", type)); |
| 1332 |
} |
| 1333 |
} |
| 1334 |
|
| 1335 |
/****************************************************************************** |
| 1336 |
Write out an item to a file as raw binary bytes. |
| 1337 |
|
| 1338 |
Entry: |
| 1339 |
fp - file to write to |
| 1340 |
int_val - integer version of item |
| 1341 |
uint_val - unsigned integer version of item |
| 1342 |
double_val - double-precision float version of item |
| 1343 |
type - data type to write out |
| 1344 |
******************************************************************************/ |
| 1345 |
|
| 1346 |
void write_binary_item(FILE* fp, int int_val, unsigned int uint_val, double double_val, int type) |
| 1347 |
{ |
| 1348 |
unsigned char uchar_val; |
| 1349 |
char char_val; |
| 1350 |
unsigned short ushort_val; |
| 1351 |
short short_val; |
| 1352 |
float float_val; |
| 1353 |
|
| 1354 |
switch (type) |
| 1355 |
{ |
| 1356 |
case PLY_CHAR: |
| 1357 |
char_val = int_val; |
| 1358 |
fwrite(&char_val, 1, 1, fp); |
| 1359 |
break; |
| 1360 |
case PLY_SHORT: |
| 1361 |
short_val = int_val; |
| 1362 |
fwrite(&short_val, 2, 1, fp); |
| 1363 |
break; |
| 1364 |
case PLY_INT: |
| 1365 |
fwrite(&int_val, 4, 1, fp); |
| 1366 |
break; |
| 1367 |
case PLY_UCHAR: |
| 1368 |
uchar_val = uint_val; |
| 1369 |
fwrite(&uchar_val, 1, 1, fp); |
| 1370 |
break; |
| 1371 |
case PLY_USHORT: |
| 1372 |
ushort_val = uint_val; |
| 1373 |
fwrite(&ushort_val, 2, 1, fp); |
| 1374 |
break; |
| 1375 |
case PLY_UINT: |
| 1376 |
fwrite(&uint_val, 4, 1, fp); |
| 1377 |
break; |
| 1378 |
case PLY_FLOAT: |
| 1379 |
float_val = d2f(double_val); |
| 1380 |
fwrite(&float_val, 4, 1, fp); |
| 1381 |
break; |
| 1382 |
case PLY_DOUBLE: |
| 1383 |
fwrite(&double_val, 8, 1, fp); |
| 1384 |
break; |
| 1385 |
default: |
| 1386 |
throw std::runtime_error(mrpt::format("write_binary_item: bad type = %d", type)); |
| 1387 |
} |
| 1388 |
} |
| 1389 |
|
| 1390 |
/****************************************************************************** |
| 1391 |
Write out an item to a file as ascii characters. |
| 1392 |
|
| 1393 |
Entry: |
| 1394 |
fp - file to write to |
| 1395 |
int_val - integer version of item |
| 1396 |
uint_val - unsigned integer version of item |
| 1397 |
double_val - double-precision float version of item |
| 1398 |
type - data type to write out |
| 1399 |
******************************************************************************/ |
| 1400 |
|
| 1401 |
void write_ascii_item(FILE* fp, int int_val, unsigned int uint_val, double double_val, int type) |
| 1402 |
{ |
| 1403 |
switch (type) |
| 1404 |
{ |
| 1405 |
case PLY_CHAR: |
| 1406 |
case PLY_SHORT: |
| 1407 |
case PLY_INT: |
| 1408 |
fprintf(fp, "%d ", int_val); |
| 1409 |
break; |
| 1410 |
case PLY_UCHAR: |
| 1411 |
case PLY_USHORT: |
| 1412 |
case PLY_UINT: |
| 1413 |
fprintf(fp, "%u ", uint_val); |
| 1414 |
break; |
| 1415 |
case PLY_FLOAT: |
| 1416 |
case PLY_DOUBLE: |
| 1417 |
fprintf(fp, "%g ", double_val); |
| 1418 |
break; |
| 1419 |
default: |
| 1420 |
throw std::runtime_error(mrpt::format("write_ascii_item: bad type = %d", type)); |
| 1421 |
} |
| 1422 |
} |
| 1423 |
|
| 1424 |
/****************************************************************************** |
| 1425 |
Get the value of an item that is in memory, and place the result |
| 1426 |
into an integer, an unsigned integer and a double. |
| 1427 |
|
| 1428 |
Entry: |
| 1429 |
ptr - pointer to the item |
| 1430 |
type - data type supposedly in the item |
| 1431 |
|
| 1432 |
Exit: |
| 1433 |
int_val - integer value |
| 1434 |
uint_val - unsigned integer value |
| 1435 |
double_val - double-precision floating point value |
| 1436 |
******************************************************************************/ |
| 1437 |
|
| 1438 |
void get_stored_item(void* ptr, int type, int* int_val, unsigned int* uint_val, double* double_val) |
| 1439 |
{ |
| 1440 |
switch (type) |
| 1441 |
{ |
| 1442 |
case PLY_CHAR: |
| 1443 |
*int_val = *((char*)ptr); |
| 1444 |
*uint_val = *int_val; |
| 1445 |
*double_val = *int_val; |
| 1446 |
break; |
| 1447 |
case PLY_UCHAR: |
| 1448 |
*uint_val = *((unsigned char*)ptr); |
| 1449 |
*int_val = *uint_val; |
| 1450 |
*double_val = *uint_val; |
| 1451 |
break; |
| 1452 |
case PLY_SHORT: |
| 1453 |
*int_val = *((short int*)ptr); |
| 1454 |
*uint_val = *int_val; |
| 1455 |
*double_val = *int_val; |
| 1456 |
break; |
| 1457 |
case PLY_USHORT: |
| 1458 |
*uint_val = *((unsigned short int*)ptr); |
| 1459 |
*int_val = *uint_val; |
| 1460 |
*double_val = *uint_val; |
| 1461 |
break; |
| 1462 |
case PLY_INT: |
| 1463 |
*int_val = *((int*)ptr); |
| 1464 |
*uint_val = *int_val; |
| 1465 |
*double_val = *int_val; |
| 1466 |
break; |
| 1467 |
case PLY_UINT: |
| 1468 |
*uint_val = *((unsigned int*)ptr); |
| 1469 |
*int_val = *uint_val; |
| 1470 |
*double_val = *uint_val; |
| 1471 |
break; |
| 1472 |
case PLY_FLOAT: |
| 1473 |
*double_val = *((float*)ptr); |
| 1474 |
*int_val = static_cast<int>(*double_val); |
| 1475 |
*uint_val = static_cast<unsigned int>(*double_val); |
| 1476 |
break; |
| 1477 |
case PLY_DOUBLE: |
| 1478 |
*double_val = *((double*)ptr); |
| 1479 |
*int_val = static_cast<int>(*double_val); |
| 1480 |
*uint_val = static_cast<unsigned int>(*double_val); |
| 1481 |
break; |
| 1482 |
default: |
| 1483 |
throw std::runtime_error(mrpt::format("get_stored_item: bad type = %d", type)); |
| 1484 |
} |
| 1485 |
} |
| 1486 |
|
| 1487 |
/****************************************************************************** |
| 1488 |
Get the value of an item from a binary file, and place the result |
| 1489 |
into an integer, an unsigned integer and a double. |
| 1490 |
|
| 1491 |
Entry: |
| 1492 |
fp - file to get item from |
| 1493 |
type - data type supposedly in the word |
| 1494 |
|
| 1495 |
Exit: |
| 1496 |
int_val - integer value |
| 1497 |
uint_val - unsigned integer value |
| 1498 |
double_val - double-precision floating point value |
| 1499 |
|
| 1500 |
Return: 0: On error |
| 1501 |
******************************************************************************/ |
| 1502 |
|
| 1503 |
int get_binary_item( |
| 1504 |
FILE* fp, int bin_file_type, int type, int* int_val, unsigned int* uint_val, double* double_val) |
| 1505 |
{ |
| 1506 |
char c[8]; |
| 1507 |
void* ptr; |
| 1508 |
|
| 1509 |
ptr = (void*)c; |
| 1510 |
|
| 1511 |
switch (type) |
| 1512 |
{ |
| 1513 |
case PLY_CHAR: |
| 1514 |
if (fread(ptr, 1, 1, fp) != 1) return 0; |
| 1515 |
*int_val = *((char*)ptr); |
| 1516 |
*uint_val = *int_val; |
| 1517 |
*double_val = *int_val; |
| 1518 |
break; |
| 1519 |
case PLY_UCHAR: |
| 1520 |
if (fread(ptr, 1, 1, fp) != 1) return 0; |
| 1521 |
*uint_val = *((unsigned char*)ptr); |
| 1522 |
*int_val = *uint_val; |
| 1523 |
*double_val = *uint_val; |
| 1524 |
break; |
| 1525 |
case PLY_SHORT: |
| 1526 |
if (fread(ptr, 2, 1, fp) != 1) return 0; |
| 1527 |
*int_val = *((short int*)ptr); |
| 1528 |
*uint_val = *int_val; |
| 1529 |
*double_val = *int_val; |
| 1530 |
break; |
| 1531 |
case PLY_USHORT: |
| 1532 |
if (fread(ptr, 2, 1, fp) != 1) return 0; |
| 1533 |
*uint_val = *((unsigned short int*)ptr); |
| 1534 |
*int_val = *uint_val; |
| 1535 |
*double_val = *uint_val; |
| 1536 |
break; |
| 1537 |
case PLY_INT: |
| 1538 |
if (fread(ptr, 4, 1, fp) != 1) return 0; |
| 1539 |
*int_val = *((int*)ptr); |
| 1540 |
*uint_val = *int_val; |
| 1541 |
*double_val = *int_val; |
| 1542 |
break; |
| 1543 |
case PLY_UINT: |
| 1544 |
if (fread(ptr, 4, 1, fp) != 1) return 0; |
| 1545 |
*uint_val = *((unsigned int*)ptr); |
| 1546 |
*int_val = *uint_val; |
| 1547 |
*double_val = *uint_val; |
| 1548 |
break; |
| 1549 |
case PLY_FLOAT: |
| 1550 |
if (fread(ptr, 4, 1, fp) != 1) return 0; |
| 1551 |
*double_val = *((float*)ptr); |
| 1552 |
*int_val = static_cast<int>(*double_val); |
| 1553 |
*uint_val = static_cast<unsigned int>(*double_val); |
| 1554 |
break; |
| 1555 |
case PLY_DOUBLE: |
| 1556 |
if (fread(ptr, 8, 1, fp) != 1) return 0; |
| 1557 |
*double_val = *((double*)ptr); |
| 1558 |
*int_val = static_cast<int>(*double_val); |
| 1559 |
*uint_val = static_cast<unsigned int>(*double_val); |
| 1560 |
break; |
| 1561 |
default: |
| 1562 |
throw std::runtime_error(mrpt::format("get_binary_item: bad type = %d", type)); |
| 1563 |
} |
| 1564 |
|
| 1565 |
// Added by JL: |
| 1566 |
// If the Big/Little endian format in the file is different than the native |
| 1567 |
// format, do the conversion: |
| 1568 |
#if MRPT_IS_BIG_ENDIAN |
| 1569 |
const bool do_reverse = (bin_file_type == PLY_BINARY_LE); |
| 1570 |
#else |
| 1571 |
const bool do_reverse = (bin_file_type == PLY_BINARY_BE); |
| 1572 |
#endif |
| 1573 |
|
| 1574 |
if (do_reverse) |
| 1575 |
{ |
| 1576 |
int int_val2 = *int_val; |
| 1577 |
unsigned int uint_val2 = *uint_val; |
| 1578 |
double double_val2 = *double_val; |
| 1579 |
mrpt::reverseBytes(int_val2, *int_val); |
| 1580 |
mrpt::reverseBytes(uint_val2, *uint_val); |
| 1581 |
mrpt::reverseBytes(double_val2, *double_val); |
| 1582 |
} |
| 1583 |
|
| 1584 |
return 1; |
| 1585 |
} |
| 1586 |
|
| 1587 |
/****************************************************************************** |
| 1588 |
Extract the value of an item from an ascii word, and place the result |
| 1589 |
into an integer, an unsigned integer and a double. |
| 1590 |
|
| 1591 |
Entry: |
| 1592 |
word - word to extract value from |
| 1593 |
type - data type supposedly in the word |
| 1594 |
|
| 1595 |
Exit: |
| 1596 |
int_val - integer value |
| 1597 |
uint_val - unsigned integer value |
| 1598 |
double_val - double-precision floating point value |
| 1599 |
******************************************************************************/ |
| 1600 |
|
| 1601 |
void get_ascii_item( |
| 1602 |
const char* word, int type, int* int_val, unsigned int* uint_val, double* double_val) |
| 1603 |
{ |
| 1604 |
switch (type) |
| 1605 |
{ |
| 1606 |
case PLY_CHAR: |
| 1607 |
case PLY_UCHAR: |
| 1608 |
case PLY_SHORT: |
| 1609 |
case PLY_USHORT: |
| 1610 |
case PLY_INT: |
| 1611 |
*int_val = atoi(word); |
| 1612 |
*uint_val = *int_val; |
| 1613 |
*double_val = *int_val; |
| 1614 |
break; |
| 1615 |
|
| 1616 |
case PLY_UINT: |
| 1617 |
*uint_val = strtoul(word, (char**)nullptr, 10); |
| 1618 |
*int_val = *uint_val; |
| 1619 |
*double_val = *uint_val; |
| 1620 |
break; |
| 1621 |
|
| 1622 |
case PLY_FLOAT: |
| 1623 |
case PLY_DOUBLE: |
| 1624 |
*double_val = atof(word); |
| 1625 |
*int_val = (int)*double_val; |
| 1626 |
*uint_val = (unsigned int)*double_val; |
| 1627 |
break; |
| 1628 |
|
| 1629 |
default: |
| 1630 |
throw std::runtime_error(mrpt::format("get_ascii_item: bad type = %d", type)); |
| 1631 |
} |
| 1632 |
} |
| 1633 |
|
| 1634 |
/****************************************************************************** |
| 1635 |
Store a value into a place being pointed to, guided by a data type. |
| 1636 |
|
| 1637 |
Entry: |
| 1638 |
item - place to store value |
| 1639 |
type - data type |
| 1640 |
int_val - integer version of value |
| 1641 |
uint_val - unsigned integer version of value |
| 1642 |
double_val - double version of value |
| 1643 |
|
| 1644 |
Exit: |
| 1645 |
item - pointer to stored value |
| 1646 |
******************************************************************************/ |
| 1647 |
|
| 1648 |
void store_item(char* item, int type, int int_val, unsigned int uint_val, double double_val) |
| 1649 |
{ |
| 1650 |
unsigned char* puchar; |
| 1651 |
short int* pshort; |
| 1652 |
unsigned short int* pushort; |
| 1653 |
int* pint; |
| 1654 |
unsigned int* puint; |
| 1655 |
float* pfloat; |
| 1656 |
double* pdouble; |
| 1657 |
|
| 1658 |
switch (type) |
| 1659 |
{ |
| 1660 |
case PLY_CHAR: |
| 1661 |
*item = int_val; |
| 1662 |
break; |
| 1663 |
case PLY_UCHAR: |
| 1664 |
puchar = (unsigned char*)item; |
| 1665 |
*puchar = uint_val; |
| 1666 |
break; |
| 1667 |
case PLY_SHORT: |
| 1668 |
pshort = (short*)item; |
| 1669 |
*pshort = int_val; |
| 1670 |
break; |
| 1671 |
case PLY_USHORT: |
| 1672 |
pushort = (unsigned short*)item; |
| 1673 |
*pushort = uint_val; |
| 1674 |
break; |
| 1675 |
case PLY_INT: |
| 1676 |
pint = (int*)item; |
| 1677 |
*pint = int_val; |
| 1678 |
break; |
| 1679 |
case PLY_UINT: |
| 1680 |
puint = (unsigned int*)item; |
| 1681 |
*puint = uint_val; |
| 1682 |
break; |
| 1683 |
case PLY_FLOAT: |
| 1684 |
pfloat = (float*)item; |
| 1685 |
*pfloat = d2f(double_val); |
| 1686 |
break; |
| 1687 |
case PLY_DOUBLE: |
| 1688 |
pdouble = (double*)item; |
| 1689 |
*pdouble = double_val; |
| 1690 |
break; |
| 1691 |
default: |
| 1692 |
throw std::runtime_error(mrpt::format("store_item: bad type = %d", type)); |
| 1693 |
} |
| 1694 |
} |
| 1695 |
|
| 1696 |
/****************************************************************************** |
| 1697 |
Add an element to a PLY file descriptor. |
| 1698 |
|
| 1699 |
Entry: |
| 1700 |
plyfile - PLY file descriptor |
| 1701 |
words - list of words describing the element |
| 1702 |
nwords - number of words in the list |
| 1703 |
******************************************************************************/ |
| 1704 |
|
| 1705 |
void add_element(PlyFile* plyfile, const vector<string>& words) |
| 1706 |
{ |
| 1707 |
/* create the new element */ |
| 1708 |
plyfile->elems.emplace_back(); |
| 1709 |
|
| 1710 |
PlyElement* elem = &(*plyfile->elems.rbegin()); |
| 1711 |
elem->name = words[1]; |
| 1712 |
elem->num = atoi(words[2].c_str()); |
| 1713 |
} |
| 1714 |
|
| 1715 |
/****************************************************************************** |
| 1716 |
Return the type of a property, given the name of the property. |
| 1717 |
|
| 1718 |
Entry: |
| 1719 |
name - name of property type |
| 1720 |
|
| 1721 |
Exit: |
| 1722 |
returns integer code for property, or 0 if not found |
| 1723 |
******************************************************************************/ |
| 1724 |
|
| 1725 |
PLY_DATA_TYPE get_prop_type(const string& type_name) |
| 1726 |
{ |
| 1727 |
auto it = type_names.find(type_name); |
| 1728 |
if (it != type_names.end()) return it->second; |
| 1729 |
|
| 1730 |
THROW_EXCEPTION_FMT("Unknown PLY data type: '%s'", type_name.c_str()); |
| 1731 |
} |
| 1732 |
|
| 1733 |
/****************************************************************************** |
| 1734 |
Add a property to a PLY file descriptor. |
| 1735 |
|
| 1736 |
Entry: |
| 1737 |
plyfile - PLY file descriptor |
| 1738 |
words - list of words describing the property |
| 1739 |
nwords - number of words in the list |
| 1740 |
******************************************************************************/ |
| 1741 |
|
| 1742 |
void add_property(PlyFile* plyfile, const vector<string>& words) |
| 1743 |
{ |
| 1744 |
/* add this property to the list of properties of the current element */ |
| 1745 |
PlyElement* elem = &(*plyfile->elems.rbegin()); |
| 1746 |
|
| 1747 |
PlyProperty& prop = elem->props.emplace_back(); |
| 1748 |
|
| 1749 |
/* create the new property */ |
| 1750 |
|
| 1751 |
if (words[1] == "list") |
| 1752 |
{ /* is a list */ |
| 1753 |
prop.count_external = get_prop_type(words[2]); |
| 1754 |
prop.external_type = get_prop_type(words[3]); |
| 1755 |
prop.name = words[4]; |
| 1756 |
prop.is_list = true; |
| 1757 |
} |
| 1758 |
else |
| 1759 |
{ /* not a list */ |
| 1760 |
prop.external_type = get_prop_type(words[1]); |
| 1761 |
prop.name = words[2]; |
| 1762 |
prop.is_list = false; |
| 1763 |
} |
| 1764 |
} |
| 1765 |
|
| 1766 |
/****************************************************************************** |
| 1767 |
Add a comment to a PLY file descriptor. |
| 1768 |
|
| 1769 |
Entry: |
| 1770 |
plyfile - PLY file descriptor |
| 1771 |
line - line containing comment |
| 1772 |
******************************************************************************/ |
| 1773 |
|
| 1774 |
void add_comment(PlyFile* plyfile, const string& line) |
| 1775 |
{ |
| 1776 |
/* skip over "comment" and leading spaces and tabs */ |
| 1777 |
ply_put_comment(plyfile, mrpt::system::trim(line.substr(7))); |
| 1778 |
} |
| 1779 |
|
| 1780 |
/****************************************************************************** |
| 1781 |
Add a some object information to a PLY file descriptor. |
| 1782 |
|
| 1783 |
Entry: |
| 1784 |
plyfile - PLY file descriptor |
| 1785 |
line - line containing text info |
| 1786 |
******************************************************************************/ |
| 1787 |
|
| 1788 |
void add_obj_info(PlyFile* plyfile, const string& line) |
| 1789 |
{ |
| 1790 |
/* skip over "obj_info" and leading spaces and tabs */ |
| 1791 |
ply_put_obj_info(plyfile, mrpt::system::trim(line.substr(8))); |
| 1792 |
} |
| 1793 |
|
| 1794 |
/****************************************************************************** |
| 1795 |
Copy a property. |
| 1796 |
******************************************************************************/ |
| 1797 |
|
| 1798 |
void copy_property(PlyProperty* dest, const PlyProperty* src) { *dest = *src; } |
| 1799 |
const float VAL_NOT_SET = -1e10; |
| 1800 |
|
| 1801 |
struct TVertex |
| 1802 |
{ |
| 1803 |
float x = 0, y = 0, z = 0; |
| 1804 |
float r = 0, g = 0, b = 0; |
| 1805 |
float intensity = 0; |
| 1806 |
float timestamp = 0; |
| 1807 |
|
| 1808 |
void markAsNotSet() |
| 1809 |
{ |
| 1810 |
x = y = z = VAL_NOT_SET; |
| 1811 |
r = g = b = VAL_NOT_SET; |
| 1812 |
intensity = VAL_NOT_SET; |
| 1813 |
timestamp = VAL_NOT_SET; |
| 1814 |
} |
| 1815 |
}; |
| 1816 |
|
| 1817 |
const std::array<PlyProperty, 5> vert_props = { |
| 1818 |
/* list of property information for a vertex */ |
| 1819 |
// ... is_list count_external count_internal count_offset |
| 1820 |
PlyProperty("x", PLY_FLOAT, PLY_FLOAT, offsetof(TVertex, x)), |
| 1821 |
PlyProperty("y", PLY_FLOAT, PLY_FLOAT, offsetof(TVertex, y)), |
| 1822 |
PlyProperty("z", PLY_FLOAT, PLY_FLOAT, offsetof(TVertex, z)), |
| 1823 |
PlyProperty("intensity", PLY_FLOAT, PLY_FLOAT, offsetof(TVertex, intensity)), |
| 1824 |
PlyProperty("timestamp", PLY_FLOAT, PLY_FLOAT, offsetof(TVertex, timestamp)), |
| 1825 |
}; |
| 1826 |
|
| 1827 |
struct TFace |
| 1828 |
{ |
| 1829 |
float intensity; /* this user attaches intensity to faces */ |
| 1830 |
unsigned char nverts; /* number of vertex indices in list */ |
| 1831 |
int* verts; /* vertex index list */ |
| 1832 |
}; |
| 1833 |
|
| 1834 |
const PlyProperty face_props[] = { |
| 1835 |
/* list of property information for a vertex */ |
| 1836 |
{ "intensity", PLY_FLOAT, PLY_FLOAT, offsetof(TFace, intensity)}, |
| 1837 |
{ "vertex_indices", PLY_INT, PLY_INT, offsetof(TFace, verts), true, PLY_UCHAR, PLY_UCHAR, |
| 1838 |
offsetof(TFace, nverts)} |
| 1839 |
}; |
| 1840 |
|
| 1841 |
/* |
| 1842 |
Loads from a PLY file. |
| 1843 |
*/ |
| 1844 |
bool PLY_Importer::loadFromPlyFile( |
| 1845 |
const std::string& filename, |
| 1846 |
std::vector<std::string>* file_comments, |
| 1847 |
std::vector<std::string>* file_obj_info) |
| 1848 |
{ |
| 1849 |
try |
| 1850 |
{ |
| 1851 |
/* open a PLY file for reading */ |
| 1852 |
vector<string> elist; // element names |
| 1853 |
int file_type; |
| 1854 |
float version; |
| 1855 |
PlyFile* ply = ply_open_for_reading(filename.c_str(), elist, &file_type, &version); |
| 1856 |
|
| 1857 |
/* go through each kind of element that we learned is in the file */ |
| 1858 |
/* and read them */ |
| 1859 |
|
| 1860 |
for (const auto& elem_name : elist) |
| 1861 |
{ |
| 1862 |
/* get the description of the first element */ |
| 1863 |
int num_elems = 0, nprops = 0; |
| 1864 |
|
| 1865 |
// vector<PlyProperty> plist = |
| 1866 |
ply_get_element_description(ply, elem_name, num_elems, nprops); |
| 1867 |
|
| 1868 |
/* print the name of the element, for debugging */ |
| 1869 |
// printf ("element %s %d\n", elem_name, num_elems); |
| 1870 |
|
| 1871 |
/* if we're on vertex elements, read them in */ |
| 1872 |
if ("vertex" != elem_name) continue; |
| 1873 |
|
| 1874 |
/* set up for getting vertex elements */ |
| 1875 |
for (const auto& vert_prop : vert_props) ply_get_property(ply, elem_name, &vert_prop); |
| 1876 |
|
| 1877 |
/* grab all the vertex elements */ |
| 1878 |
this->PLY_import_set_vertex_count(num_elems); |
| 1879 |
for (int j = 0; j < num_elems; j++) |
| 1880 |
{ |
| 1881 |
TVertex pt; |
| 1882 |
pt.markAsNotSet(); |
| 1883 |
|
| 1884 |
/* grab an element from the file */ |
| 1885 |
ply_get_element(ply, reinterpret_cast<void*>(&pt)); |
| 1886 |
const TPoint3Df xyz(pt.x, pt.y, pt.z); |
| 1887 |
if (pt.intensity != VAL_NOT_SET) |
| 1888 |
{ // Grayscale |
| 1889 |
const TColorf col(pt.intensity, pt.intensity, pt.intensity); |
| 1890 |
this->PLY_import_set_vertex(j, xyz, &col); |
| 1891 |
} |
| 1892 |
else if (pt.r != VAL_NOT_SET && pt.g != VAL_NOT_SET && pt.b != VAL_NOT_SET) |
| 1893 |
{ // RGB |
| 1894 |
const TColorf col(pt.r, pt.g, pt.b); |
| 1895 |
this->PLY_import_set_vertex(j, xyz, &col); |
| 1896 |
} |
| 1897 |
else |
| 1898 |
{ // No color |
| 1899 |
this->PLY_import_set_vertex(j, xyz); |
| 1900 |
} |
| 1901 |
// timestamp? |
| 1902 |
if (pt.timestamp != VAL_NOT_SET) this->PLY_import_set_vertex_timestamp(j, pt.timestamp); |
| 1903 |
} |
| 1904 |
} |
| 1905 |
|
| 1906 |
// grab and print out the comments in the file |
| 1907 |
if (file_comments) |
| 1908 |
{ |
| 1909 |
vector<string> strs; |
| 1910 |
ply_get_comments(ply, strs); |
| 1911 |
*file_comments = std::vector<std::string>(strs); |
| 1912 |
} |
| 1913 |
|
| 1914 |
// grab and print out the object information |
| 1915 |
if (file_obj_info) |
| 1916 |
{ |
| 1917 |
vector<string> strs; |
| 1918 |
ply_get_obj_info(ply, strs); |
| 1919 |
*file_obj_info = std::vector<std::string>(strs); |
| 1920 |
} |
| 1921 |
|
| 1922 |
/* close the PLY file */ |
| 1923 |
ply_close(ply); |
| 1924 |
|
| 1925 |
// All OK: |
| 1926 |
m_ply_import_last_error = std::string(); |
| 1927 |
return true; |
| 1928 |
} |
| 1929 |
catch (const std::exception& e) |
| 1930 |
{ |
| 1931 |
// Return error: |
| 1932 |
m_ply_import_last_error = std::string(e.what()); |
| 1933 |
return false; |
| 1934 |
} |
| 1935 |
} |
| 1936 |
|
| 1937 |
bool PLY_Exporter::saveToPlyFile( |
| 1938 |
const std::string& filename, |
| 1939 |
bool save_in_binary, |
| 1940 |
const std::vector<std::string>& file_comments, |
| 1941 |
const std::vector<std::string>& file_obj_info) const |
| 1942 |
{ |
| 1943 |
try |
| 1944 |
{ |
| 1945 |
/* list of the kinds of elements in the user's object */ |
| 1946 |
vector<string> elem_names; |
| 1947 |
elem_names.emplace_back("vertex"); |
| 1948 |
elem_names.emplace_back("face"); |
| 1949 |
|
| 1950 |
/* create the vertex index lists for the faces */ |
| 1951 |
// for (i = 0; i < nfaces; i++) |
| 1952 |
// faces[i].verts = vert_ptrs[i]; |
| 1953 |
|
| 1954 |
/* open either a binary or ascii PLY file for writing */ |
| 1955 |
/* (the file will be called "test.ply" because the routines */ |
| 1956 |
/* enforce the .ply filename extension) */ |
| 1957 |
|
| 1958 |
float version; |
| 1959 |
PlyFile* ply = ply_open_for_writing( |
| 1960 |
filename.c_str(), elem_names, |
| 1961 |
save_in_binary ? |
| 1962 |
#if MRPT_IS_BIG_ENDIAN |
| 1963 |
PLY_BINARY_BE |
| 1964 |
#else |
| 1965 |
PLY_BINARY_LE |
| 1966 |
#endif |
| 1967 |
: PLY_ASCII, |
| 1968 |
&version); |
| 1969 |
|
| 1970 |
/* describe what properties go into the vertex and face elements */ |
| 1971 |
|
| 1972 |
const size_t nverts = this->PLY_export_get_vertex_count(); |
| 1973 |
const size_t nfaces = this->PLY_export_get_face_count(); |
| 1974 |
|
| 1975 |
if (nverts) |
| 1976 |
{ |
| 1977 |
// Find out if we have color: |
| 1978 |
TPoint3Df pt; |
| 1979 |
bool pt_has_color; |
| 1980 |
TColorf pt_color; |
| 1981 |
this->PLY_export_get_vertex(0, pt, pt_has_color, pt_color); |
| 1982 |
|
| 1983 |
ply_element_count(ply, "vertex", nverts); |
| 1984 |
ply_describe_property(ply, "vertex", &vert_props[0]); // x |
| 1985 |
ply_describe_property(ply, "vertex", &vert_props[1]); // y |
| 1986 |
ply_describe_property(ply, "vertex", &vert_props[2]); // z |
| 1987 |
|
| 1988 |
if (pt_has_color) ply_describe_property(ply, "vertex", &vert_props[3]); // intensity |
| 1989 |
} |
| 1990 |
|
| 1991 |
ply_element_count(ply, "face", nfaces); |
| 1992 |
for (const auto& face_prop : face_props) ply_describe_property(ply, "face", &face_prop); |
| 1993 |
|
| 1994 |
/* write a comment and an object information field */ |
| 1995 |
for (const auto& file_comment : file_comments) ply_put_comment(ply, file_comment.c_str()); |
| 1996 |
|
| 1997 |
for (const auto& k : file_obj_info) ply_put_obj_info(ply, k.c_str()); |
| 1998 |
|
| 1999 |
/* we have described exactly what we will put in the file, so */ |
| 2000 |
/* we are now done with the header info */ |
| 2001 |
ply_header_complete(ply); |
| 2002 |
|
| 2003 |
/* set up and write the vertex elements */ |
| 2004 |
ply_put_element_setup(ply, "vertex"); |
| 2005 |
for (size_t i = 0; i < nverts; i++) |
| 2006 |
{ |
| 2007 |
TPoint3Df pt; |
| 2008 |
bool pt_has_color; |
| 2009 |
TColorf pt_color; |
| 2010 |
this->PLY_export_get_vertex(i, pt, pt_has_color, pt_color); |
| 2011 |
|
| 2012 |
TVertex ver; |
| 2013 |
ver.x = pt.x; |
| 2014 |
ver.y = pt.y; |
| 2015 |
ver.z = pt.z; |
| 2016 |
|
| 2017 |
if (pt_has_color) |
| 2018 |
ver.intensity = (1.0f / 3.0f) * (pt_color.R + pt_color.G + pt_color.B); |
| 2019 |
else |
| 2020 |
ver.intensity = 0.5; |
| 2021 |
|
| 2022 |
ply_put_element(ply, (void*)&ver); |
| 2023 |
} |
| 2024 |
|
| 2025 |
/* set up and write the face elements */ |
| 2026 |
/* ply_put_element_setup (ply, "face"); |
| 2027 |
for (size_t i = 0; i < nfaces; i++) |
| 2028 |
{ |
| 2029 |
TFace face; |
| 2030 |
this->PLY_export_get_face(...); |
| 2031 |
ply_put_element (ply, (void *) &face); |
| 2032 |
}*/ |
| 2033 |
|
| 2034 |
/* close the PLY file */ |
| 2035 |
ply_close(ply); |
| 2036 |
|
| 2037 |
// All OK: |
| 2038 |
m_ply_export_last_error = std::string(); |
| 2039 |
return true; |
| 2040 |
} |
| 2041 |
catch (const std::exception& e) |
| 2042 |
{ |
| 2043 |
// Return error: |
| 2044 |
m_ply_export_last_error = std::string(e.what()); |
| 2045 |
return false; |
| 2046 |
} |
| 2047 |
} |