60 #define PLY_BINARY_BE 2 61 #define PLY_BINARY_LE 3 123 : fp(_fp), file_type(0), version(0), which_elem(nullptr)
153 vector<string>
get_words(FILE*,
string&);
169 void store_item(
char*,
int,
int,
unsigned int,
double);
178 void get_ascii_item(
const char*,
int,
int*,
unsigned int*,
double*);
180 FILE*,
int,
int,
int*,
unsigned int*,
double*);
207 if (fp ==
nullptr)
return (
nullptr);
218 plyfile->
elems.resize(elem_names.size());
219 for (
size_t i = 0; i < elem_names.size(); i++)
221 plyfile->
elems[i].name = elem_names[i];
243 const char*
name,
const vector<string>& elem_names,
int file_type,
259 plyfile =
ply_write(fp, elem_names, file_type);
260 if (plyfile ==
nullptr)
return (
nullptr);
282 PlyFile* plyfile,
const string& elem_name,
int nelems,
283 vector<PlyProperty>& prop_list)
288 throw std::runtime_error(
290 "ply_describe_element: can't find element '%s'",
297 const size_t nprops = prop_list.size();
298 elem->
props.resize(nprops);
301 for (
size_t i = 0; i < nprops; i++)
303 elem->
props[i] = prop_list[i];
325 stderr,
"ply_describe_property: can't find element '%s'\n",
331 elem->
props.push_back(*prop);
353 throw std::runtime_error(
355 "ply_element_count: can't find element '%s'",
371 FILE* fp = plyfile->
fp;
378 fprintf(fp,
"format ascii 1.0\n");
381 fprintf(fp,
"format binary_big_endian 1.0\n");
384 fprintf(fp,
"format binary_little_endian 1.0\n");
387 throw std::runtime_error(
389 "ply_header_complete: bad file type = %d",
395 for (
size_t i = 0; i < plyfile->
comments.size(); i++)
400 for (
size_t i = 0; i < plyfile->
obj_info.size(); i++)
405 for (
size_t i = 0; i < plyfile->
elems.size(); i++)
411 for (
size_t j = 0; j < elem->
props.size(); j++)
449 throw std::runtime_error(
451 "ply_elements_setup: can't find element '%s'",
469 FILE* fp = plyfile->
fp;
470 char *elem_data, *item;
474 unsigned int uint_val;
479 elem_data = (
char*)elem_ptr;
480 other_ptr = (
char**)(((
char*)elem_ptr) + elem->
other_offset);
489 for (
size_t j = 0; j < elem->
props.size(); j++)
493 elem_data = *other_ptr;
495 elem_data = (
char*)elem_ptr;
504 const size_t list_count = uint_val;
505 item_ptr = (
char**)(elem_data + prop->
offset);
508 for (
size_t k = 0; k < list_count; k++)
520 item = elem_data + prop->
offset;
536 for (
size_t j = 0; j < elem->
props.size(); j++)
540 elem_data = *other_ptr;
542 elem_data = (
char*)elem_ptr;
552 const size_t list_count = uint_val;
553 item_ptr = (
char**)(elem_data + prop->
offset);
556 for (
size_t k = 0; k < list_count; k++)
568 item = elem_data + prop->
offset;
590 plyfile->
comments.push_back(comment);
604 plyfile->
obj_info.push_back(obj_info);
629 if (fp ==
nullptr)
return (
nullptr);
636 vector<string> words =
get_words(plyfile->
fp, orig_line);
638 if (words.empty() || words[0] !=
"ply")
return nullptr;
640 while (!words.empty())
644 if (words[0] ==
"format")
646 if (words.size() != 3)
return (
nullptr);
647 if (words[1] ==
"ascii")
649 else if (words[1] ==
"binary_big_endian")
651 else if (words[1] ==
"binary_little_endian")
655 plyfile->
version = atof(words[2].c_str());
658 else if (words[0] ==
"element")
660 else if (words[0] ==
"property")
662 else if (words[0] ==
"comment")
664 else if (words[0] ==
"obj_info")
666 else if (words[0] ==
"end_header")
675 for (
size_t i = 0; i < plyfile->
elems.size(); i++)
685 for (
size_t i = 0; i < plyfile->
elems.size(); i++)
686 elem_names.push_back(plyfile->
elems[i].name);
708 const char* filename, vector<string>& elem_names,
int* file_type,
716 fp =
fopen(filename,
"r");
717 if (fp ==
nullptr)
return (
nullptr);
749 PlyFile* plyfile,
const string& elem_name,
int& nelems,
int& nprops)
753 if (elem ==
nullptr)
return vector<PlyProperty>();
756 nprops = elem->
props.size();
788 if (prop_ptr ==
nullptr)
791 stderr,
"Warning: Can't find property '%s' in element '%s'\n",
792 prop->
name.c_str(), elem_name.c_str());
887 if (ply ==
nullptr)
return;
906 for (
size_t i = 0; i < plyfile->
elems.size(); i++)
907 if (element == plyfile->
elems[i].name)
return &plyfile->
elems[i];
927 for (
size_t i = 0; i < elem->
props.size(); i++)
928 if (
string(prop_name) == elem->
props[i].name)
931 return &elem->
props[i];
949 char *elem_data, *item =
nullptr;
953 unsigned int uint_val;
958 char* other_data =
nullptr;
981 vector<string> words =
get_words(plyfile->
fp, orig_line);
984 throw std::runtime_error(
985 format(
"ply_get_element: unexpected end of file"));
989 for (
size_t j = 0; j < elem->
props.size(); j++)
992 store_it = (elem->
store_prop[j] | other_flag);
996 elem_data = elem_ptr;
998 elem_data = other_data;
1006 &uint_val, &double_val);
1016 list_count = int_val;
1018 store_array = (
char**)(elem_data + prop->
offset);
1020 if (list_count == 0)
1022 if (store_it) *store_array =
nullptr;
1029 (
char*)malloc(
sizeof(
char) * item_size * list_count);
1031 *store_array = item_ptr;
1035 for (
int k = 0; k < list_count; k++)
1039 &int_val, &uint_val, &double_val);
1054 &uint_val, &double_val);
1057 item = elem_data + prop->
offset;
1075 FILE* fp = plyfile->
fp;
1076 char *elem_data, *item =
nullptr;
1080 unsigned int uint_val;
1085 char* other_data =
nullptr;
1100 other_data = (
char*)malloc(elem->
other_size);
1110 for (
size_t j = 0; j < elem->
props.size(); j++)
1113 store_it = (elem->
store_prop[j] | other_flag);
1117 elem_data = elem_ptr;
1119 elem_data = other_data;
1127 &uint_val, &double_val))
1132 "RPly::binary_get_element: Error reading binary file!\n");
1144 list_count = int_val;
1153 store_array = (
char**)(elem_data + prop->
offset);
1154 if (list_count == 0)
1156 if (store_it) *store_array =
nullptr;
1163 (
char*)malloc(
sizeof(
char) * item_size * list_count);
1165 *store_array = item_ptr;
1169 for (
int k = 0; k < list_count; k++)
1173 &uint_val, &double_val))
1178 "RPly::binary_get_element: Error reading binary " 1195 fp, bin_file_type, prop->
external_type, &int_val, &uint_val,
1201 "RPly::binary_get_element: Error reading binary file!\n");
1206 item = elem_data + prop->
offset;
1227 throw std::runtime_error(
1228 format(
"write_scalar_type: bad data code = %d",
code));
1252 #define BIG_STRING 4096 1255 vector<string> words;
1261 if (result ==
nullptr)
1286 unsigned char* puchar;
1289 unsigned short int* pushort;
1291 unsigned int* puint;
1295 unsigned int uint_value;
1296 double double_value;
1301 pchar = (
char*)item;
1303 return ((
double)int_value);
1305 puchar = (
unsigned char*)item;
1306 int_value = *puchar;
1307 return ((
double)int_value);
1309 pshort = (
short int*)item;
1310 int_value = *pshort;
1311 return ((
double)int_value);
1313 pushort = (
unsigned short int*)item;
1314 int_value = *pushort;
1315 return ((
double)int_value);
1319 return ((
double)int_value);
1321 puint = (
unsigned int*)item;
1322 uint_value = *puint;
1323 return ((
double)uint_value);
1325 pfloat = (
float*)item;
1326 double_value = *pfloat;
1327 return (double_value);
1329 pdouble = (
double*)item;
1330 double_value = *pdouble;
1331 return (double_value);
1333 throw std::runtime_error(
1334 format(
"get_item_value: bad type = %d",
type));
1350 FILE* fp,
int int_val,
unsigned int uint_val,
double double_val,
int type)
1352 unsigned char uchar_val;
1354 unsigned short ushort_val;
1362 fwrite(&char_val, 1, 1, fp);
1365 short_val = int_val;
1366 fwrite(&short_val, 2, 1, fp);
1369 fwrite(&int_val, 4, 1, fp);
1372 uchar_val = uint_val;
1373 fwrite(&uchar_val, 1, 1, fp);
1376 ushort_val = uint_val;
1377 fwrite(&ushort_val, 2, 1, fp);
1380 fwrite(&uint_val, 4, 1, fp);
1383 float_val = double_val;
1384 fwrite(&float_val, 4, 1, fp);
1387 fwrite(&double_val, 8, 1, fp);
1390 throw std::runtime_error(
1391 format(
"write_binary_item: bad type = %d",
type));
1407 FILE* fp,
int int_val,
unsigned int uint_val,
double double_val,
int type)
1423 fprintf(fp,
"%g ", double_val);
1426 throw std::runtime_error(
1427 format(
"write_ascii_item: bad type = %d",
type));
1446 void* ptr,
int type,
int* int_val,
unsigned int* uint_val,
1452 *int_val = *((
char*)ptr);
1453 *uint_val = *int_val;
1454 *double_val = *int_val;
1457 *uint_val = *((
unsigned char*)ptr);
1458 *int_val = *uint_val;
1459 *double_val = *uint_val;
1462 *int_val = *((
short int*)ptr);
1463 *uint_val = *int_val;
1464 *double_val = *int_val;
1467 *uint_val = *((
unsigned short int*)ptr);
1468 *int_val = *uint_val;
1469 *double_val = *uint_val;
1472 *int_val = *((
int*)ptr);
1473 *uint_val = *int_val;
1474 *double_val = *int_val;
1477 *uint_val = *((
unsigned int*)ptr);
1478 *int_val = *uint_val;
1479 *double_val = *uint_val;
1482 *double_val = *((
float*)ptr);
1483 *int_val = *double_val;
1484 *uint_val = *double_val;
1487 *double_val = *((
double*)ptr);
1488 *int_val = *double_val;
1489 *uint_val = *double_val;
1492 throw std::runtime_error(
1493 format(
"get_stored_item: bad type = %d",
type));
1514 FILE* fp,
int bin_file_type,
int type,
int* int_val,
unsigned int* uint_val,
1525 if (fread(ptr, 1, 1, fp) != 1)
return 0;
1526 *int_val = *((
char*)ptr);
1527 *uint_val = *int_val;
1528 *double_val = *int_val;
1531 if (fread(ptr, 1, 1, fp) != 1)
return 0;
1532 *uint_val = *((
unsigned char*)ptr);
1533 *int_val = *uint_val;
1534 *double_val = *uint_val;
1537 if (fread(ptr, 2, 1, fp) != 1)
return 0;
1538 *int_val = *((
short int*)ptr);
1539 *uint_val = *int_val;
1540 *double_val = *int_val;
1543 if (fread(ptr, 2, 1, fp) != 1)
return 0;
1544 *uint_val = *((
unsigned short int*)ptr);
1545 *int_val = *uint_val;
1546 *double_val = *uint_val;
1549 if (fread(ptr, 4, 1, fp) != 1)
return 0;
1550 *int_val = *((
int*)ptr);
1551 *uint_val = *int_val;
1552 *double_val = *int_val;
1555 if (fread(ptr, 4, 1, fp) != 1)
return 0;
1556 *uint_val = *((
unsigned int*)ptr);
1557 *int_val = *uint_val;
1558 *double_val = *uint_val;
1561 if (fread(ptr, 4, 1, fp) != 1)
return 0;
1562 *double_val = *((
float*)ptr);
1563 *int_val = *double_val;
1564 *uint_val = *double_val;
1567 if (fread(ptr, 8, 1, fp) != 1)
return 0;
1568 *double_val = *((
double*)ptr);
1569 *int_val = *double_val;
1570 *uint_val = *double_val;
1573 throw std::runtime_error(
1574 format(
"get_binary_item: bad type = %d",
type));
1580 #if MRPT_IS_BIG_ENDIAN 1588 int int_val2 = *int_val;
1589 unsigned int uint_val2 = *uint_val;
1590 double double_val2 = *double_val;
1614 const char* word,
int type,
int* int_val,
unsigned int* uint_val,
1624 *int_val = atoi(word);
1625 *uint_val = *int_val;
1626 *double_val = *int_val;
1630 *uint_val = strtoul(word, (
char**)
nullptr, 10);
1631 *int_val = *uint_val;
1632 *double_val = *uint_val;
1637 *double_val = atof(word);
1638 *int_val = (int)*double_val;
1639 *uint_val = (
unsigned int)*double_val;
1643 throw std::runtime_error(
1644 format(
"get_ascii_item: bad type = %d",
type));
1663 char* item,
int type,
int int_val,
unsigned int uint_val,
double double_val)
1665 unsigned char* puchar;
1667 unsigned short int* pushort;
1669 unsigned int* puint;
1679 puchar = (
unsigned char*)item;
1683 pshort = (
short*)item;
1687 pushort = (
unsigned short*)item;
1688 *pushort = uint_val;
1695 puint = (
unsigned int*)item;
1699 pfloat = (
float*)item;
1700 *pfloat = double_val;
1703 pdouble = (
double*)item;
1704 *pdouble = double_val;
1707 throw std::runtime_error(
format(
"store_item: bad type = %d",
type));
1726 elem->
name = words[1];
1727 elem->
num = atoi(words[2].c_str());
1771 if (words[1] ==
"list")
1775 prop->
name = words[4];
1781 prop->
name = words[2];
1855 bool PLY_Importer::loadFromPlyFile(
1862 vector<string> elist;
1871 for (
size_t i = 0; i < elist.size(); i++)
1874 const string& elem_name = elist[i];
1875 int num_elems = 0, nprops = 0;
1884 if (
"vertex" == elem_name)
1892 this->PLY_import_set_vertex_count(num_elems);
1893 for (
int j = 0; j < num_elems; j++)
1906 this->PLY_import_set_vertex(j, xyz, &col);
1913 this->PLY_import_set_vertex(j, xyz, &col);
1917 this->PLY_import_set_vertex(j, xyz);
1930 vector<string> strs;
1938 vector<string> strs;
1950 catch (std::exception& e)
1958 bool PLY_Exporter::saveToPlyFile(
1965 vector<string> elem_names;
1966 elem_names.push_back(
string(
"vertex"));
1967 elem_names.push_back(
string(
"face"));
1979 filename.c_str(), elem_names, save_in_binary ?
1980 #if MRPT_IS_BIG_ENDIAN 1990 const size_t nverts = this->PLY_export_get_vertex_count();
1991 const size_t nfaces = this->PLY_export_get_face_count();
1999 this->PLY_export_get_vertex(0, pt, pt_has_color, pt_color);
2016 for (
size_t k = 0; k < file_comments.
size(); k++)
2019 for (
size_t k = 0; k < file_obj_info.
size(); k++)
2028 for (
size_t i = 0; i < nverts; i++)
2033 this->PLY_export_get_vertex(i, pt, pt_has_color, pt_color);
2042 (1.0f / 3.0f) * (pt_color.
R + pt_color.
G + pt_color.
B);
2065 catch (std::exception& e)
#define offsetof(_structure, _field)
const char DONT_STORE_PROP
void ply_put_obj_info(PlyFile *plyfile, const string &obj_info)
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
void get_ascii_item(const char *, int, int *, unsigned int *, double *)
vector< string > comments
vector< PlyProperty > props
PlyElement * find_element(PlyFile *, const std::string &s)
int void fclose(FILE *f)
An OS-independent version of fclose.
const PlyProperty vert_props[]
vector< char > store_prop
void ply_get_obj_info(PlyFile *plyfile, vector< string > &obj_info)
struct PlyProperty PlyProperty
PlyFile * ply_open_for_writing(const char *name, const vector< string > &elem_names, int file_type, float *version)
int get_binary_item(FILE *, int, int, int *, unsigned int *, double *)
void add_property(PlyFile *, const vector< string > &)
const int ply_type_size[]
A class for storing a list of text lines.
This base provides a set of functions for maths stuff.
Lightweight 3D point (float version).
double get_item_value(const char *, int)
PlyFile(FILE *_fp=nullptr)
void ply_put_element_setup(PlyFile *plyfile, const string &elem_name)
void add_comment(PlyFile *, const string &)
void ply_describe_element(PlyFile *plyfile, const string &elem_name, int nelems, vector< PlyProperty > &prop_list)
const PlyProperty face_props[]
GLsizei const GLchar ** string
void ply_describe_property(PlyFile *plyfile, const char *elem_name, const PlyProperty *prop)
size_t size() const
Returns the number of text lines in the list.
PlyFile * ply_read(FILE *fp, vector< string > &elem_names)
void reverseBytes(const T &v_in, T &v_out)
Reverse the order of the bytes of a given type (useful for transforming btw little/big endian) ...
void add_element(PlyFile *, const vector< string > &)
int get_prop_type(const string &type_name)
int fprintf(FILE *fil, const char *format,...) noexcept MRPT_printf_format_check(2
An OS-independent version of fprintf.
void tokenize(const std::string &inString, const std::string &inDelimiters, std::deque< std::string > &outTokens, bool skipBlankTokens=true) noexcept
Tokenizes a string according to a set of delimiting characters.
const std::string type_names[]
vector< PlyProperty > ply_get_element_description(PlyFile *plyfile, const string &elem_name, int &nelems, int &nprops)
void add_obj_info(PlyFile *, const string &)
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void write_binary_item(FILE *, int, unsigned int, double, int)
void ply_close(PlyFile *plyfile)
void ply_put_comment(PlyFile *plyfile, const string &comment)
void ply_get_element(PlyFile *plyfile, void *elem_ptr)
void write_scalar_type(FILE *, int)
void write_ascii_item(FILE *, int, unsigned int, double, int)
PlyFile * ply_write(FILE *fp, const vector< string > &elem_names, int file_type)
PlyProperty * find_property(PlyElement *, const std::string &s, int *)
void get_stored_item(void *, int, int *, unsigned int *, double *)
GLuint const GLchar * name
A RGB color - floats in the range [0,1].
void ascii_get_element(PlyFile *, char *)
void copy_property(PlyProperty *, const PlyProperty *)
vector< string > get_words(FILE *, string &)
void binary_get_element(PlyFile *, char *)
std::string trim(const std::string &str)
Removes leading and trailing spaces.
vector< PlyElement > elems
struct PlyElement PlyElement
FILE * fopen(const char *fileName, const char *mode) noexcept
An OS-independent version of fopen.
void ply_get_property(PlyFile *plyfile, const string &elem_name, const PlyProperty *prop)
GLenum GLsizei GLenum format
void store_item(char *, int, int, unsigned int, double)
void ply_header_complete(PlyFile *plyfile)
void ply_get_info(PlyFile *ply, float *version, int *file_type)
GLuint GLuint GLsizei GLenum type
vector< string > obj_info
void ply_element_count(PlyFile *plyfile, const string &elem_name, int nelems)
void ply_get_comments(PlyFile *plyfile, vector< string > &comments)
PlyFile * ply_open_for_reading(const char *filename, vector< string > &elem_names, int *file_type, float *version)
void ply_put_element(PlyFile *plyfile, void *elem_ptr)