Debian Codegraph @

vcg/wrap/io_trimesh/import_asc.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 ****************************************************************************/
29
30 #ifndef __VCGLIB_IMPORT_ASC
31 #define __VCGLIB_IMPORT_ASC
32
33 #include <stdio.h>
34 #include <fstream>
35 #include <iostream>
36 #include <vcg/complex/trimesh/allocate.h>
37 #include <vcg/complex/trimesh/create/platonic.h>
38 #include <vcg/complex/trimesh/clean.h>
39
40 namespace vcg {
41 namespace tri {
42 namespace io {
43
44 /**
45 This class encapsulate a filter for importing raw format pointcloud.
46 there exists many raw formats. each one with a particular sintax even if they only contains
47
48 */
49 template <class MESH_TYPE>
50 class ImporterASC
51 {
52 public:
53
54 typedef typename MESH_TYPE::VertexPointer VertexPointer;
55 typedef typename MESH_TYPE::ScalarType ScalarType;
56 typedef typename MESH_TYPE::VertexType VertexType;
57 typedef typename MESH_TYPE::FaceType FaceType;
58 typedef typename MESH_TYPE::VertexIterator VertexIterator;
59 typedef typename MESH_TYPE::FaceIterator FaceIterator;
60
61 enum RAWError {
62 E_NOERROR, // 0
63 // Error open
64 E_CANTOPEN, // 1
65 E_UNESPECTEDEOF, // 2
66 };
67
68 static const char *ErrorMsg(int error)
69 {
70 static const char * raw_error_msg[] =
71 {
72 "No errors",
73 "Can't open file",
74 "Premature End of file",
75 };
76
77 if(error>2 || error<0) return "Unknown error";
78 else return raw_error_msg[error];
79 };
80
81 /*!
82 * Standard call for reading a mesh
83 * \param m the destination mesh
84 * \param filename the name of the file to read from
85 * \param triangulate if true, the mesh will be triangulated, otherwise only points will be stored
86 * \param lineskip number of lines to be skipped at the begin of the file
87 * \return the operation result
88 */
89 static int Open( MESH_TYPE &m, const char * filename, CallBackPos *cb=0,bool triangulate=false)
90 {
91 FILE *fp;
92 fp = fopen(filename, "r");
93 if(fp == NULL)
94 {
95 return E_CANTOPEN;
96 }
97 long currentPos = ftell(fp);
98 fseek(fp,0L,SEEK_END);
99 long fileLen = ftell(fp);
100 fseek(fp,currentPos,SEEK_SET);
101
102 m.Clear();
103
104 Point3f pp;
105 int cnt=0;
106 int ret;
107 /* Read a single facet from an ASCII .STL file */
108 while(!feof(fp))
109 {
110 if(cb && (++cnt)%1000) cb( (ftell(fp)*100)/fileLen, "ASC Mesh Loading");
111 if(feof(fp)) break;
112 ret=fscanf(fp, "%f, %f, %f\n", &pp.X(), &pp.Y(), &pp.Z());
113 if(ret!=3) return E_UNESPECTEDEOF;
114 VertexIterator vi=Allocator<MESH_TYPE>::AddVertices(m,1);
115 (*vi).P().Import(pp);
116 }
117
118 fclose(fp);
119 // now try to triangulate.
120 // search for the first jump
121 float baseY = m.vert[0].P().Y();
122 int i;
123 for(i=1;i<m.vert.size();++i)
124 {
125 if(m.vert[i].P().Y()!= baseY) break;
126 }
127 cnt=m.vert.size();
128 qDebug("Grid is %i x %i = %i (%i) ",i,cnt/i,i* (cnt/i),cnt);
129 tri::FaceGrid(m,i,cnt/i);
130 tri::Clean<CMeshO>::FlipMesh(m);
131 return E_NOERROR;
132 }
133
134 }; // end class
135 } // end Namespace tri
136 } // end Namespace io
137 } // end Namespace vcg
138
139 #endif