1 #ifndef _SUPER4PCS_IO_PLY_H 2 #define _SUPER4PCS_IO_PLY_H 8 BINARY_LITTLE_ENDIAN_1,
12 static const unsigned int MAX_COMMENT_SIZE = 256;
16 readHeader (
const char *filename,
17 unsigned int & numOfVertices,
18 unsigned int & numOfFaces,
20 unsigned int & numOfVertexProperties,
26 ifstream in (filename, std::ios_base::in | std::ios_base::binary);
28 cerr <<
"(PLY) error opening file" << endl;
32 numOfVertexProperties = 0;
37 string current, currentelement;
39 if (current !=
"ply"){
40 cerr <<
"(PLY) not a PLY file" << endl;
45 while (current !=
"end_header") {
46 if (current ==
"format") {
48 if (current ==
"binary_big_endian") {
51 format = BINARY_BIG_ENDIAN_1;
53 cerr <<
"(PLY) error parsing header - bad binary big endian version" << endl;
56 }
else if (current ==
"binary_little_endian") {
59 format = BINARY_LITTLE_ENDIAN_1;
61 cerr <<
"(PLY) error parsing header - bad binary little endian version" << endl;
64 }
else if (current ==
"ascii") {
69 cerr <<
"(PLY) error parsing header - bad ascii version" << endl;
73 cerr <<
"(PLY) error parsing header (format)" << endl;
76 }
else if (current ==
"element") {
78 if (current ==
"vertex"){
79 currentelement = current;
82 else if (current ==
"face"){
83 currentelement = current;
87 cerr <<
"(PLY) ignoring unknown element " << current << endl;
90 }
else if (currentelement !=
"" && current ==
"property") {
92 if (current ==
"float" || current ==
"double") {
93 numOfVertexProperties++;
96 else if (current ==
"uchar") {
97 numOfVertexProperties++;
101 else if (current ==
"list") {
106 cerr <<
"(PLY) error parsing header (property)" << endl;
109 }
else if ( (current ==
"comment") || (current.find(
"obj_info") != std::string::npos) ) {
110 char comment[MAX_COMMENT_SIZE];
111 in.getline (comment, MAX_COMMENT_SIZE);
121 unsigned int headerSize = in.tellg ();
129 bigLittleEndianSwap (T * v,
unsigned int numOfElements)
131 char * tmp = (
char*)v;
132 for (
unsigned int j = 0; j < numOfElements; j++){
133 unsigned int offset = 4*j;
134 char c = tmp[offset];
135 tmp[offset] = tmp[offset+3];
138 tmp[offset+1] = tmp[offset+2];
144 almostsafefread (
void * ptr,
size_t size,
size_t count, FILE * stream ){
145 size_t result = fread (ptr,size,count,stream);
146 return (result == count);
149 template<
int targetCount,
typename... Args>
151 almostsafefscanf ( FILE * stream,
const char * format, Args&&... args ){
152 int count = fscanf (stream,format, std::forward<Args>(args)...);
153 return (count == targetCount);
158 readBinary1Body (
const std::string & filename,
159 unsigned int headerSize,
160 unsigned int numOfVertices,
161 unsigned int numOfFaces,
162 unsigned int numOfVertexProperties,
165 std::vector<GlobalRegistration::Point3D>& vertex,
166 std::vector<typename GlobalRegistration::Point3D::VectorType>& normal,
167 std::vector<tripple>& face )
172 FILE * in = fopen (filename.c_str (),
"r");
174 cerr <<
"(PLY) error opening file" << endl;
179 for (
unsigned int i = 0; i < headerSize; i++) {
180 almostsafefread (&c, 1, 1, in);
188 float * v =
new float[numOfVertexProperties];
189 unsigned char rgb_buff [4];
191 for (
unsigned int i = 0; i < numOfVertices && !feof (in); i++) {
192 if (numOfVertexProperties==10){
193 almostsafefread (v, 4, 6, in);
194 almostsafefread (rgb_buff,
sizeof(
unsigned char), 4, in);
195 }
else if (numOfVertexProperties==9){
196 almostsafefread (v, 4, 6, in);
197 almostsafefread (rgb_buff,
sizeof(
unsigned char), 3, in);
198 }
else if (numOfVertexProperties==6 && haveColor){
199 almostsafefread (v, 4, 3, in);
200 almostsafefread (rgb_buff,
sizeof(
unsigned char), 3, in);
201 }
else if (numOfVertexProperties==7 ){
202 almostsafefread (v, 4, 3, in);
203 almostsafefread (rgb_buff,
sizeof(
unsigned char), 4, in);
206 almostsafefread (v, 4, numOfVertexProperties, in);
207 if (bigEndian ==
true)
208 bigLittleEndianSwap (v, numOfVertexProperties);
209 vertex.emplace_back( v[0],v[1],v[2] );
211 if (numOfVertexProperties == 6){
213 rgb << rgb_buff[0], rgb_buff[1], rgb_buff[2];
214 vertex.back().set_rgb(rgb);
216 n << v[3], v[4], v[5];
217 normal.push_back (n);
218 vertex.back().set_normal(n);
220 }
else if (numOfVertexProperties == 7){
221 rgb << rgb_buff[0], rgb_buff[1], rgb_buff[2];
222 vertex.back().set_rgb(rgb);
223 }
else if (numOfVertexProperties == 9 || numOfVertexProperties == 10){
224 n << v[3], v[4], v[5];
225 rgb << rgb_buff[0], rgb_buff[1], rgb_buff[2];
226 normal.push_back (n);
227 vertex.back().set_normal(n);
228 vertex.back().set_rgb(rgb);
233 if (numOfFaces != 0){
235 cerr <<
"(PLY) incomplete file" << endl;
242 for (
unsigned int i = 0; i < numOfFaces && !feof (in); i++) {
245 almostsafefread (&polygonSize, 1, 1, in);
246 almostsafefread (f, 4, 3, in);
247 if (bigEndian ==
true)
248 bigLittleEndianSwap (f, 3);
249 face.emplace_back(f[0],f[1],f[2]);
258 readASCII1Body (
const std::string & filename,
259 unsigned int headerSize,
260 unsigned int numOfVertices,
261 unsigned int numOfFaces,
262 unsigned int numOfVertexProperties,
264 std::vector<GlobalRegistration::Point3D>& vertex,
265 std::vector<typename GlobalRegistration::Point3D::VectorType>& normal,
266 std::vector<tripple>& face )
271 FILE * in = fopen (filename.c_str (),
"r");
273 cerr <<
"(PLY) error opening file" << endl;
278 for (
unsigned int i = 0; i < headerSize; i++) {
279 almostsafefread (&c, 1, 1, in);
287 unsigned int rgb_buff [4];
288 for (
unsigned int i = 0; i < numOfVertices && !feof (in); i++) {
289 std::vector<float> v(numOfVertexProperties);
291 if (numOfVertexProperties==10){
292 for (
unsigned int j = 0; j < 6; j++)
293 almostsafefscanf<1> (in,
"%f", &v[j]);
294 for (
unsigned int j = 0; j < 4; j++)
295 almostsafefscanf<1> (in,
"%i", &rgb_buff[j]);
297 else if (numOfVertexProperties==9){
298 for (
unsigned int j = 0; j < 6; j++)
299 almostsafefscanf<1> (in,
"%f", &v[j]);
300 for (
unsigned int j = 0; j < 3; j++)
301 almostsafefscanf<1> (in,
"%i", &rgb_buff[j]);
302 }
else if (numOfVertexProperties==6 && haveColor){
303 for (
unsigned int j = 0; j < 3; j++)
304 almostsafefscanf<1> (in,
"%f", &v[j]);
305 for (
unsigned int j = 0; j < 3; j++)
306 almostsafefscanf<1> (in,
"%i", &rgb_buff[j]);
307 }
else if (numOfVertexProperties==7){
308 for (
unsigned int j = 0; j < 3; j++)
309 almostsafefscanf<1> (in,
"%f", &v[j]);
310 for (
unsigned int j = 0; j < 4; j++)
311 almostsafefscanf<1> (in,
"%i", &rgb_buff[j]);
314 for (
unsigned int j = 0; j < numOfVertexProperties; j++)
315 almostsafefscanf<1> (in,
"%f", &v[j]);
317 vertex.emplace_back( v[0],v[1],v[2] );
319 if (numOfVertexProperties == 6){
321 rgb << rgb_buff[0], rgb_buff[1], rgb_buff[2];
322 vertex.back().set_rgb(rgb);
325 n << v[3], v[4], v[5];
326 normal.push_back (n);
327 vertex.back().set_normal(n);
329 }
else if (numOfVertexProperties == 7){
330 rgb << rgb_buff[0], rgb_buff[1], rgb_buff[2];
331 vertex.back().set_rgb(rgb);
332 }
else if (numOfVertexProperties == 9 || numOfVertexProperties == 10){
333 n << v[3], v[4], v[5];
334 rgb << rgb_buff[0], rgb_buff[1], rgb_buff[2];
335 normal.push_back (n);
336 vertex.back().set_normal(n);
337 vertex.back().set_rgb(rgb);
341 if (numOfFaces != 0){
343 cerr <<
"(PLY) incomplete file" << endl;
350 for (
unsigned int i = 0; i < numOfFaces && !feof (in); i++) {
353 almostsafefscanf<4> (in,
"%d %d %d %d", &polygonSize, &f[0], &f[1], &f[2]);
354 face.emplace_back(f[0],f[1],f[2]);
Eigen::Matrix< Scalar, 3, 1 > VectorType
Definition: shared4pcs.h:64