Debian Codegraph @

vcg/wrap/io_trimesh/import_raw.h

raw symbols SCIP
1 /****************************************************************************
2 * VCGLib o o *
3 * Visual and Computer Graphics Library o o *
4 * _ O _ *
5 * Copyright(C) 2004 \/)\/ *
6 * Visual Computing Lab /\/| *
7 * ISTI - Italian National Research Council | *
8 * \ *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
20 * for more details. *
21 * *
22 ****************************************************************************/
23
24 /****************************************************************************
25 History
26
27 $Log: import_raw.h,v $
28 Revision 1.3 2006/12/04 12:01:23 spinelli
29 add method for automatic hole filling....
30
31 Revision 1.2 2005/05/09 12:29:55 callieri
32 added line cleaning to eliminate all separators, added a rough triangulation scheme.
33
34 Revision 1.1 2005/05/06 13:58:26 callieri
35 First working version (callieri)
36
37
38 ****************************************************************************/
39
40 #ifndef __VCGLIB_IMPORT_RAW
41 #define __VCGLIB_IMPORT_RAW
42
43 #include <stdio.h>
44 #include <fstream>
45 #include <iostream>
46
47 namespace vcg {
48 namespace tri {
49 namespace io {
50
51 /**
52 This class encapsulate a filter for importing raw format pointcloud.
53 there exists many raw formats. each one with a particular sintax even if they only contains
54
55 */
56 template <class MESH_TYPE>
57 class ImporterRAW
58 {
59 public:
60
61 typedef typename MESH_TYPE::VertexPointer VertexPointer;
62 typedef typename MESH_TYPE::ScalarType ScalarType;
63 typedef typename MESH_TYPE::VertexType VertexType;
64 typedef typename MESH_TYPE::FaceType FaceType;
65 typedef typename MESH_TYPE::VertexIterator VertexIterator;
66 typedef typename MESH_TYPE::FaceIterator FaceIterator;
67
68 // max token number
69 #define RAW_MAX_TOKEN_LINE_DESCRIPTOR 32
70
71
72 enum RAWError {
73 E_NOERROR, // 0
74 // Error open
75 E_CANTOPEN, // 1
76 E_UNESPECTEDEOF, // 2
77 // error line descriptor
78 E_INVALIDLINEDESC, // 3
79 // error line parsing
80 E_LINEERROR, // 4
81 // wrong number of points
82 E_WRONGPOINTNUM // 5
83 };
84
85 static const char *ErrorMsg(int error)
86 {
87 static const char * raw_error_msg[] =
88 {
89 "No errors",
90 "Can't open file",
91 "Premature End of file",
92 "Invalid line Descriptor",
93 "Error parsing a line",
94 "Point number different from expected"
95 };
96
97 if(error>2 || error<0) return "Unknown error";
98 else return stl_error_msg[error];
99 };
100
101 // line format is a string describing which data is stored for every data line
102 // PX PY PZ posizione
103 // NX NY NZ normale
104 // CR CG CB colore
105 // RF riflettanza (qualita')
106 //
107 // the string is parsed to know how many value are contained in each line
108 // and which is the order. the result is a number (how many) and a vector
109 // describing the order
110 //
111 //
112 // during reading a data structure is used to store intermediate values
113 // it is basically an array of float
114 //
115 // float linebuffer[]
116 //[0][1][2][3][4][5][6][7][8][9]
117 // p p p n n n c c c r
118 // x y z x y z r g b f
119 //
120 // given the number of tokens and the order vector it is possible to scan a line using the command
121 //
122 // for(...n 0->tokennumber...)
123 // fscanf(fp,"%f", &linebuffer[tokenorder[n]])
124
125 static int Parselinedescription(const char * linedesc, int &tokennumber, int *order)
126 {
127 int ii;
128 char tok[3];
129 int index;
130
131 // controllo lunghezza
132 // se non e' multiplo di 3 allora e' errato
133 int len = strlen(linedesc) + 1;
134 if(len%3 != 0)
135 return E_INVALIDLINEDESC;
136
137 index=0;
138 tok[2] = '\0';
139 tokennumber = 0;
140 for(ii=0; ii<RAW_MAX_TOKEN_LINE_DESCRIPTOR; ii++)
141 order[ii] = -1;
142
143 while(index <= (len-3))
144 {
145 tok[0] = linedesc[index ];
146 tok[1] = linedesc[index+1];
147
148 if(strcmp(tok,"PX") == 0) // pos x
149 {order[tokennumber] = 0; tokennumber++;}
150 else if (strcmp(tok,"PY") == 0) // pos y
151 {order[tokennumber] = 1; tokennumber++;}
152 else if (strcmp(tok,"PZ") == 0) // pos z
153 {order[tokennumber] = 2; tokennumber++;}
154 else if (strcmp(tok,"NX") == 0) // norm x
155 {order[tokennumber] = 3; tokennumber++;}
156 else if (strcmp(tok,"NY") == 0) // norm y
157 {order[tokennumber] = 4; tokennumber++;}
158 else if (strcmp(tok,"NZ") == 0) // norm z
159 {order[tokennumber] = 5; tokennumber++;}
160 else if (strcmp(tok,"CR") == 0) // col r
161 {order[tokennumber] = 6; tokennumber++;}
162 else if (strcmp(tok,"CG") == 0) // col g
163 {order[tokennumber] = 7; tokennumber++;}
164 else if (strcmp(tok,"CB") == 0) // col b
165 {order[tokennumber] = 8; tokennumber++;}
166 else if (strcmp(tok,"RF") == 0) // rifl
167 {order[tokennumber] = 9; tokennumber++;}
168 else // nessuno dei suddetti... errore
169 { return E_INVALIDLINEDESC; }
170
171 index +=3;
172 }
173
174 return E_NOERROR;
175 }
176
177
178 // function to skip a line
179 static void Skipline(FILE *fp)
180 {
181 char buf;
182
183 fread(&(buf),sizeof(char),1,fp);
184
185 while(buf != '\n')
186 fread(&(buf),sizeof(char),1,fp);
187 }
188
189 // function to parse the line read from the raw file
190 // all characters besides numerals,dot,minus,plus
191 // if e is found between numbers it's ketpt (12e4)
192 static int Parseline(int tokennumber, int *tokenorder, char *rawline, float *linebuffer)
193 {
194 int linelen;
195 int ii;
196 bool change;
197 int foundtok;
198
199 // length
200 linelen = strlen(rawline);
201
202 // cleaning the line
203 for(ii=0;ii<(linelen-1);)
204 {
205 change = true;
206
207 if(isdigit(rawline[ii])) // is a number
208 change = false;
209 else if(rawline[ii]==' ') // is a space
210 change = false;
211 else if((rawline[ii]=='-') || (rawline[ii]=='+') || (rawline[ii]=='-') || (rawline[ii]=='.')) // is + - .
212 change = false;
213 else if(rawline[ii]=='e') // is e... is perhaps an exponential ?
214 {
215 if((ii>0) || (ii<(linelen-2))) // if it's at the begin or the end of line, it's not an exponential
216 if(isdigit(rawline[ii-1])) // a number before it
217 if(isdigit(rawline[ii+1]) || (rawline[ii+1]=='+') || (rawline[ii+1]=='-')) // after it a number a plus or a minus
218 change = false;
219 }
220
221 if(change)
222 rawline[ii++] = ' '; // then change it to ' '
223 else
224 ii++;
225
226 }
227 rawline[linelen] = '\0';
228
229 // now parsing the line
230 foundtok = 0;
231 ii = 0;
232 while((foundtok<tokennumber)&&(ii<linelen))
233 {
234 //find the next token begin
235 while(rawline[ii] == ' ')
236 ii++;
237
238 foundtok++;
239 sscanf(&(rawline[ii]),"%f", &(linebuffer[tokenorder[foundtok-1]]));
240
241 // going after the token
242 while((rawline[ii] != ' ')&&(rawline[ii] != '\0'))
243 ii++;
244 }
245
246 if(foundtok<tokennumber)
247 return E_LINEERROR;
248 else
249 return E_NOERROR;
250 }
251
252
253 static int fillHoles( MESH_TYPE &m, size_t rowCount, size_t colCount)
254 {
255
256 for (size_t i = 1; i<colCount-1; ++i)
257 for (size_t j = 1; j<rowCount-1; ++j)
258 {
259
260 size_t ind = (i) + ((j) * colCount);
261 size_t indL = (i-1) + ((j) * colCount);
262 size_t indR = (i+1) + ((j) * colCount);
263 size_t indT = (i) + ((j-1) * colCount);
264 size_t indB = (i) + ((j+1) * colCount);
265
266 if ((m.vert[ind].P() == vcg::Point3<MESH_TYPE::ScalarType>(0,0,0)) &&
267 (m.vert[indL].P() != vcg::Point3<MESH_TYPE::ScalarType>(0,0,0)) &&
268 (m.vert[indR].P() != vcg::Point3<MESH_TYPE::ScalarType>(0,0,0)) &&
269 (m.vert[indT].P() != vcg::Point3<MESH_TYPE::ScalarType>(0,0,0)) &&
270 (m.vert[indB].P() != vcg::Point3<MESH_TYPE::ScalarType>(0,0,0)) )
271 {
272 m.vert[ind].P() = ( m.vert[indL].P() + m.vert[indR].P() + m.vert[indT].P() + m.vert[indB].P() ) * 0.25;
273
274 }
275
276
277 }
278
279 //vcg::tri::io::ExporterPLY<MESH_TYPE>::Save( hm, "hole.ply" );
280 return 1;
281 }
282
283 /*!
284 * Standard call for reading a mesh
285 * \param m the destination mesh
286 * \param filename the name of the file to read from
287 * \param triangulate if true, the mesh will be triangulated, otherwise only points will be stored
288 * \param lineskip number of lines to be skipped at the begin of the file
289 * \return the operation result
290 */
291 static int Open( MESH_TYPE &m, const char * filename, bool triangulate=false, int lineskip = 0, const char * linedesc = "PX PY PZ")
292 {
293 int ii;
294 int ret;
295
296
297 FILE *fp;
298 int rownumber;
299 int colnumber;
300
301 // line description
302 int tokennumber;
303 int tokenorder[RAW_MAX_TOKEN_LINE_DESCRIPTOR];
304
305 // line read from file, to be parsed
306 char rawline[512];
307
308
309
310 //line data buffer
311 float linebuffer[10];
312 // fill buffer with standard values
313 linebuffer[0] = linebuffer[1] = linebuffer[2] = 0.0;
314 linebuffer[3] = linebuffer[4] = linebuffer[5] = 1.0;
315 linebuffer[6] = 0.0; linebuffer[7] = 1.0; linebuffer[8] = 0.0;
316 linebuffer[9] = 1.0;
317
318
319 fp = fopen(filename, "r");
320
321 if(fp == NULL)
322 {
323 return E_CANTOPEN;
324 }
325
326 // skip initial lines
327 for(ii=0; ii<lineskip; ii++) Skipline(fp);
328
329 // in raw files with indication of rows and columns it's also possible to triangulate points
330 // after the skipped lines there should be the number of row and columns
331 if(triangulate)
332 {
333 fscanf(fp,"%i", &(rownumber));
334 fscanf(fp,"%i\n", &(colnumber));
335 }
336
337 // parsing line description
338 ret = Parselinedescription(linedesc, tokennumber, tokenorder);
339 if(ret)
340 return ret;
341
342 m.Clear();
343 m.vert.reserve( rownumber * colnumber );
344 int line = 0;
345 size_t rowCounter = 0;
346 size_t colCounter = 0;
347 while(!feof(fp))
348 {
349 /**/
350 //read a new line
351
352 ii=0;
353 memset( rawline, 0, 512);
354 fread(&(rawline[ii++]),sizeof(char),1,fp);
355 while( (rawline[ii-1] != '\n') && (ii<512) )
356 {
357 fread(&(rawline[ii++]),sizeof(char),1,fp);
358 }
359 rawline[ii-1] = '\0';
360 line++;
361 if(strlen(rawline) >0) // empty line, just skip
362 {
363
364 ret = Parseline(tokennumber, tokenorder, rawline, linebuffer);
365 if(ret)
366 return ret;
367
368 /*
369 // old code reading directly fom file stream
370 for(ii=0; ii<tokennumber; ii++)
371 fscanf(fp,"%f", &(linebuffer[tokenorder[ii]]));
372 */
373
374 // new vertex
375 VertexType nv;
376
377 // store the position
378 nv.P()[0] = linebuffer[0];
379 nv.P()[1] = linebuffer[1];
380 nv.P()[2] = linebuffer[2];
381
382 // store the normal
383 if(m.HasPerVertexNormal())
384 {
385 nv.N()[0] = linebuffer[3];
386 nv.N()[1] = linebuffer[4];
387 nv.N()[2] = linebuffer[5];
388 }
389
390 // store the color
391 if(m.HasPerVertexColor())
392 {
393 nv.C()[0] = linebuffer[6];
394 nv.C()[1] = linebuffer[7];
395 nv.C()[2] = linebuffer[8];
396 }
397
398 // store the reflectance
399 if(m.HasPerVertexQuality())
400 {
401 nv.Q() = linebuffer[9];
402 }
403
404 m.vert.push_back(nv);
405
406
407
408 } // end if zero length
409 }
410 m.vn = m.vert.size();
411 if(triangulate) fillHoles(m, rownumber, colnumber);
412
413 // update model point number
414
415
416 // now generate the triangles
417 if(triangulate)
418 {
419 int rr,cc;
420 //if(m.vn != (rownumber * colnumber)) return E_WRONGPOINTNUM;
421
422 int trinum = (rownumber-1) * (colnumber-1) * 2;
423
424 FaceIterator fi=Allocator<MESH_TYPE>::AddFaces(m,trinum);
425
426 m.fn = trinum;
427 for(cc=0; cc<colnumber-1; cc++)
428 for(rr=0; rr<rownumber-1; rr++)
429 {
430 // upper tri
431 (*fi).V(2) = &(m.vert[(cc ) + ((rr ) * colnumber)]);
432 (*fi).V(1) = &(m.vert[(cc+1) + ((rr ) * colnumber)]);
433 (*fi).V(0) = &(m.vert[(cc ) + ((rr+1) * colnumber)]);
434
435
436 fi++;
437
438 // lower tri
439 (*fi).V(2) = &(m.vert[(cc+1) + ((rr ) * colnumber)]);
440 (*fi).V(1) = &(m.vert[(cc+1) + ((rr+1) * colnumber)]);
441 (*fi).V(0) = &(m.vert[(cc ) + ((rr+1) * colnumber)]);
442
443
444 fi++;
445
446 }
447
448
449 }
450
451 fclose(fp);
452 return E_NOERROR;
453 }
454
455 }; // end class
456 } // end Namespace tri
457 } // end Namespace io
458 } // end Namespace vcg
459
460 #endif