61 #define PLY_BINARY_BE 2 62 #define PLY_BINARY_LE 3 154 0, 1, 2, 4, 1, 2, 4, 4, 8
168 vector<string>
get_words(FILE *,
string&);
184 void store_item(
char *,
int,
int,
unsigned int,
double);
193 void get_ascii_item(
const char *,
int,
int *,
unsigned int *,
double *);
194 int get_binary_item(FILE *,
int,
int,
int *,
unsigned int *,
double *);
221 const vector<string> & elem_names,
238 plyfile->
elems.resize( elem_names.size() );
239 for (
size_t i = 0; i < elem_names.size(); i++)
241 plyfile->
elems[i].name = elem_names[i];
265 const vector<string> &elem_names,
282 plyfile =
ply_write (fp, elem_names, file_type);
308 const string &elem_name,
310 vector<PlyProperty> &prop_list
316 throw std::runtime_error(
format(
"ply_describe_element: can't find element '%s'",elem_name.c_str()));
322 const size_t nprops = prop_list.size();
323 elem->
props.resize(nprops);
326 for (
size_t i = 0; i < nprops; i++)
328 elem->
props[i] = prop_list[i];
345 const char *elem_name,
352 fprintf(stderr,
"ply_describe_property: can't find element '%s'\n",
358 elem->
props.push_back(*prop);
374 const string &elem_name,
385 throw std::runtime_error(
format(
"ply_element_count: can't find element '%s'",elem_name.c_str()));
401 FILE *fp = plyfile->
fp;
407 fprintf (fp,
"format ascii 1.0\n");
410 fprintf (fp,
"format binary_big_endian 1.0\n");
413 fprintf (fp,
"format binary_little_endian 1.0\n");
416 throw std::runtime_error(
format(
"ply_header_complete: bad file type = %d",plyfile->
file_type));
421 for (
size_t i = 0; i < plyfile->
comments.size(); i++)
426 for (
size_t i = 0; i < plyfile->
obj_info.size(); i++)
431 for (
size_t i = 0; i < plyfile->
elems.size(); i++)
437 for (
size_t j = 0; j < elem->
props.size(); j++) {
440 fprintf (fp,
"property list ");
473 throw std::runtime_error(
format(
"ply_elements_setup: can't find element '%s'", elem_name.c_str()));
491 FILE *fp = plyfile->
fp;
492 char *elem_data,*item;
496 unsigned int uint_val;
501 elem_data = (
char*)elem_ptr;
502 other_ptr = (
char **) (((
char *) elem_ptr) + elem->
other_offset);
511 for (
size_t j = 0; j < elem->
props.size(); j++)
515 elem_data = *other_ptr;
517 elem_data = (
char*)elem_ptr;
521 &int_val, &uint_val, &double_val);
524 const size_t list_count = uint_val;
525 item_ptr = (
char **) (elem_data + prop->
offset);
528 for (
size_t k = 0; k < list_count; k++)
531 &int_val, &uint_val, &double_val);
538 item = elem_data + prop->
offset;
540 &int_val, &uint_val, &double_val);
553 for (
size_t j = 0; j < elem->
props.size(); j++)
557 elem_data = *other_ptr;
559 elem_data = (
char*)elem_ptr;
564 &int_val, &uint_val, &double_val);
567 const size_t list_count = uint_val;
568 item_ptr = (
char **) (elem_data + prop->
offset);
571 for (
size_t k = 0; k < list_count; k++) {
573 &int_val, &uint_val, &double_val);
580 item = elem_data + prop->
offset;
583 &int_val, &uint_val, &double_val);
603 plyfile->
comments.push_back(comment);
618 plyfile->
obj_info.push_back(obj_info);
655 vector<string> words =
get_words (plyfile->
fp, orig_line);
657 if (words.empty() || words[0]!=
"ply" )
660 while (!words.empty()) {
664 if (words[0]==
"format")
666 if (words.size() != 3)
668 if (words[1]==
"ascii")
670 else if (words[1]==
"binary_big_endian")
672 else if (words[1]==
"binary_little_endian")
676 plyfile->
version = atof (words[2].c_str());
679 else if (words[0]==
"element")
681 else if (words[0]==
"property")
683 else if (words[0]==
"comment")
685 else if (words[0]==
"obj_info")
687 else if (words[0]==
"end_header")
696 for (
size_t i = 0; i < plyfile->
elems.size(); i++) {
705 for (
size_t i = 0; i < plyfile->
elems.size(); i++)
706 elem_names.push_back(plyfile->
elems[i].name);
729 const char *filename,
730 vector<string> &elem_names,
740 fp =
fopen (filename,
"r");
746 plyfile =
ply_read (fp, elem_names);
776 const string &elem_name,
784 return vector<PlyProperty>();
787 nprops = elem->
props.size();
809 const string &elem_name,
824 if (prop_ptr == NULL) {
825 fprintf (stderr,
"Warning: Can't find property '%s' in element '%s'\n",
826 prop->
name.c_str(), elem_name.c_str());
950 for (
size_t i = 0; i < plyfile->
elems.size(); i++)
951 if ( element == plyfile->
elems[i].name)
952 return &plyfile->
elems[i];
972 for (
size_t i = 0; i < elem->
props.size(); i++)
973 if (
string(prop_name)==elem->
props[i].name ) {
975 return &elem->
props[i];
994 char *elem_data,*item=NULL;
998 unsigned int uint_val;
1003 char *other_data=NULL;
1015 other_data = (
char *) malloc (elem->
other_size);
1025 vector<string> words =
get_words (plyfile->
fp, orig_line);
1028 throw std::runtime_error(
format(
"ply_get_element: unexpected end of file"));
1032 for (
size_t j = 0; j < elem->
props.size(); j++)
1035 store_it = (elem->
store_prop[j] | other_flag);
1039 elem_data = elem_ptr;
1041 elem_data = other_data;
1047 &int_val, &uint_val, &double_val);
1054 list_count = int_val;
1056 store_array = (
char **) (elem_data + prop->
offset);
1058 if (list_count == 0) {
1060 *store_array = NULL;
1064 item_ptr = (
char *) malloc (
sizeof (
char) * item_size * list_count);
1066 *store_array = item_ptr;
1070 for (
int k = 0; k < list_count; k++) {
1072 &int_val, &uint_val, &double_val);
1075 int_val, uint_val, double_val);
1084 &int_val, &uint_val, &double_val);
1086 item = elem_data + prop->
offset;
1106 FILE *fp = plyfile->
fp;
1107 char *elem_data,*item=NULL;
1111 unsigned int uint_val;
1116 char *other_data=NULL;
1130 other_data = (
char *) malloc (elem->
other_size);
1140 for (
size_t j = 0; j < elem->
props.size(); j++)
1143 store_it = (elem->
store_prop[j] | other_flag);
1147 elem_data = elem_ptr;
1149 elem_data = other_data;
1155 &int_val, &uint_val, &double_val))
1158 fprintf(stderr,
"RPly::binary_get_element: Error reading binary file!\n");
1167 list_count = int_val;
1175 store_array = (
char **) (elem_data + prop->
offset);
1176 if (list_count == 0) {
1178 *store_array = NULL;
1182 item_ptr = (
char *) malloc (
sizeof (
char) * item_size * list_count);
1184 *store_array = item_ptr;
1188 for (
int k = 0; k < list_count; k++) {
1190 &int_val, &uint_val, &double_val))
1193 fprintf(stderr,
"RPly::binary_get_element: Error reading binary file!\n");
1198 int_val, uint_val, double_val);
1207 &int_val, &uint_val, &double_val))
1210 fprintf(stderr,
"RPly::binary_get_element: Error reading binary file!\n");
1214 item = elem_data + prop->
offset;
1236 throw std::runtime_error(
format(
"write_scalar_type: bad data code = %d",
code));
1261 #define BIG_STRING 4096 1264 vector<string> words;
1270 if (result == NULL) {
1295 unsigned char *puchar;
1298 unsigned short int *pushort;
1300 unsigned int *puint;
1304 unsigned int uint_value;
1305 double double_value;
1309 pchar = (
char *) item;
1311 return ((
double) int_value);
1313 puchar = (
unsigned char *) item;
1314 int_value = *puchar;
1315 return ((
double) int_value);
1317 pshort = (
short int *) item;
1318 int_value = *pshort;
1319 return ((
double) int_value);
1321 pushort = (
unsigned short int *) item;
1322 int_value = *pushort;
1323 return ((
double) int_value);
1325 pint = (
int *) item;
1327 return ((
double) int_value);
1329 puint = (
unsigned int *) item;
1330 uint_value = *puint;
1331 return ((
double) uint_value);
1333 pfloat = (
float *) item;
1334 double_value = *pfloat;
1335 return (double_value);
1337 pdouble = (
double *) item;
1338 double_value = *pdouble;
1339 return (double_value);
1341 throw std::runtime_error(
format(
"get_item_value: bad type = %d",
type));
1360 unsigned int uint_val,
1365 unsigned char uchar_val;
1367 unsigned short ushort_val;
1374 fwrite (&char_val, 1, 1, fp);
1377 short_val = int_val;
1378 fwrite (&short_val, 2, 1, fp);
1381 fwrite (&int_val, 4, 1, fp);
1384 uchar_val = uint_val;
1385 fwrite (&uchar_val, 1, 1, fp);
1388 ushort_val = uint_val;
1389 fwrite (&ushort_val, 2, 1, fp);
1392 fwrite (&uint_val, 4, 1, fp);
1395 float_val = double_val;
1396 fwrite (&float_val, 4, 1, fp);
1399 fwrite (&double_val, 8, 1, fp);
1402 throw std::runtime_error(
format(
"write_binary_item: bad type = %d",
type));
1421 unsigned int uint_val,
1435 fprintf (fp,
"%u ", uint_val);
1439 fprintf (fp,
"%g ", double_val);
1442 throw std::runtime_error(
format(
"write_ascii_item: bad type = %d",
type));
1467 unsigned int *uint_val,
1473 *int_val = *((
char *) ptr);
1474 *uint_val = *int_val;
1475 *double_val = *int_val;
1478 *uint_val = *((
unsigned char *) ptr);
1479 *int_val = *uint_val;
1480 *double_val = *uint_val;
1483 *int_val = *((
short int *) ptr);
1484 *uint_val = *int_val;
1485 *double_val = *int_val;
1488 *uint_val = *((
unsigned short int *) ptr);
1489 *int_val = *uint_val;
1490 *double_val = *uint_val;
1493 *int_val = *((
int *) ptr);
1494 *uint_val = *int_val;
1495 *double_val = *int_val;
1498 *uint_val = *((
unsigned int *) ptr);
1499 *int_val = *uint_val;
1500 *double_val = *uint_val;
1503 *double_val = *((
float *) ptr);
1504 *int_val = *double_val;
1505 *uint_val = *double_val;
1508 *double_val = *((
double *) ptr);
1509 *int_val = *double_val;
1510 *uint_val = *double_val;
1513 throw std::runtime_error(
format(
"get_stored_item: bad type = %d",
type));
1539 unsigned int *uint_val,
1550 if (fread (ptr, 1, 1, fp)!=1)
return 0;
1551 *int_val = *((
char *) ptr);
1552 *uint_val = *int_val;
1553 *double_val = *int_val;
1556 if (fread (ptr, 1, 1, fp)!=1)
return 0;
1557 *uint_val = *((
unsigned char *) ptr);
1558 *int_val = *uint_val;
1559 *double_val = *uint_val;
1562 if (fread (ptr, 2, 1, fp)!=1)
return 0;
1563 *int_val = *((
short int *) ptr);
1564 *uint_val = *int_val;
1565 *double_val = *int_val;
1568 if (fread (ptr, 2, 1, fp)!=1)
return 0;
1569 *uint_val = *((
unsigned short int *) ptr);
1570 *int_val = *uint_val;
1571 *double_val = *uint_val;
1574 if (fread (ptr, 4, 1, fp)!=1)
return 0;
1575 *int_val = *((
int *) ptr);
1576 *uint_val = *int_val;
1577 *double_val = *int_val;
1580 if (fread (ptr, 4, 1, fp)!=1)
return 0;
1581 *uint_val = *((
unsigned int *) ptr);
1582 *int_val = *uint_val;
1583 *double_val = *uint_val;
1586 if (fread (ptr, 4, 1, fp)!=1)
return 0;
1587 *double_val = *((
float *) ptr);
1588 *int_val = *double_val;
1589 *uint_val = *double_val;
1592 if (fread (ptr, 8, 1, fp)!=1)
return 0;
1593 *double_val = *((
double *) ptr);
1594 *int_val = *double_val;
1595 *uint_val = *double_val;
1598 throw std::runtime_error(
format(
"get_binary_item: bad type = %d",
type));
1603 #if MRPT_IS_BIG_ENDIAN 1611 int int_val2 = *int_val;
1612 unsigned int uint_val2 = *uint_val;
1613 double double_val2 = *double_val;
1641 unsigned int *uint_val,
1651 *int_val = atoi (word);
1652 *uint_val = *int_val;
1653 *double_val = *int_val;
1657 *uint_val = strtoul (word, (
char **) NULL, 10);
1658 *int_val = *uint_val;
1659 *double_val = *uint_val;
1664 *double_val = atof (word);
1665 *int_val = (int) *double_val;
1666 *uint_val = (
unsigned int) *double_val;
1670 throw std::runtime_error(
format(
"get_ascii_item: bad type = %d",
type));
1693 unsigned int uint_val,
1697 unsigned char *puchar;
1699 unsigned short int *pushort;
1701 unsigned int *puint;
1710 puchar = (
unsigned char *) item;
1714 pshort = (
short *) item;
1718 pushort = (
unsigned short *) item;
1719 *pushort = uint_val;
1722 pint = (
int *) item;
1726 puint = (
unsigned int *) item;
1730 pfloat = (
float *) item;
1731 *pfloat = double_val;
1734 pdouble = (
double *) item;
1735 *pdouble = double_val;
1738 throw std::runtime_error(
format(
"store_item: bad type = %d",
type));
1758 elem->
name = words[1];
1759 elem->
num = atoi (words[2].c_str());
1807 if (words[1]==
"list") {
1810 prop->
name = words[4];
1815 prop->
name = words[2];
1893 bool PLY_Importer::loadFromPlyFile(
1901 vector<string> elist;
1909 for (
size_t i = 0; i < elist.size(); i++)
1912 const string &elem_name = elist[i];
1913 int num_elems=0, nprops=0;
1922 if (
"vertex"==elem_name)
1929 this->PLY_import_set_vertex_count(num_elems);
1930 for (
int j = 0; j < num_elems; j++)
1941 this->PLY_import_set_vertex(j,xyz, &col);
1947 this->PLY_import_set_vertex(j,xyz, &col);
1951 this->PLY_import_set_vertex(j,xyz);
1964 vector<string> strs;
1972 vector<string> strs;
1984 catch(std::exception &e)
1993 bool PLY_Exporter::saveToPlyFile(
1995 bool save_in_binary,
2002 vector<string> elem_names;
2003 elem_names.push_back(
string(
"vertex"));
2004 elem_names.push_back(
string(
"face"));
2019 #if MRPT_IS_BIG_ENDIAN 2029 const size_t nverts = this->PLY_export_get_vertex_count();
2030 const size_t nfaces = this->PLY_export_get_face_count();
2038 this->PLY_export_get_vertex(0,pt,pt_has_color,pt_color);
2054 for (
size_t k=0;k<file_comments.
size();k++)
2057 for (
size_t k=0;k<file_obj_info.
size();k++)
2067 for (
size_t i = 0; i < nverts; i++)
2072 this->PLY_export_get_vertex(i,pt,pt_has_color,pt_color);
2080 ver.
intensity = (1.0f/3.0f)*(pt_color.
R+pt_color.
G+pt_color.
B);
2103 catch(std::exception &e)
#define offsetof(_structure, _field)
const char DONT_STORE_PROP
void ply_put_obj_info(PlyFile *plyfile, const string &obj_info)
FILE BASE_IMPEXP * fopen(const char *fileName, const char *mode) MRPT_NO_THROWS
An OS-independent version of fopen.
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 BASE_IMPEXP void BASE_IMPEXP 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)
int BASE_IMPEXP fprintf(FILE *fil, const char *format,...) MRPT_NO_THROWS MRPT_printf_format_check(2
An OS-independent version of fprintf.
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)
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)
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 BASE_IMPEXP trim(const std::string &str)
Removes leading and trailing spaces.
vector< PlyElement > elems
struct PlyElement PlyElement
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 BASE_IMPEXP tokenize(const std::string &inString, const std::string &inDelimiters, std::deque< std::string > &outTokens, bool skipBlankTokens=true) MRPT_NO_THROWS
Tokenizes a string according to a set of delimiting characters.
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)