Debian Codegraph @

vcg/wrap/io_trimesh/import_smf.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 History
25 $Log: import_smf.h,v $
26 Revision 1.5 2006/11/21 19:23:50 e_cerisoli
27 Added comments for documentation
28
29 ****************************************************************************/
30
31 #ifndef __VCGLIB_IMPORTER_SMF
32 #define __VCGLIB_IMPORTER_SMF
33
34 #include <stdio.h>
35 #include <vcg/space/point3.h>
36
37 namespace vcg {
38 namespace tri {
39 namespace io {
40 template<class OpenMeshType>
41 /**
42 This class encapsulate a filter for opening smf meshes.
43 */
44 class ImporterSMF
45 {
46 public:
47 typedef typename OpenMeshType::VertexPointer VertexPointer;
48 typedef typename OpenMeshType::ScalarType ScalarType;
49 typedef typename OpenMeshType::VertexType VertexType;
50 typedef typename OpenMeshType::FaceType FaceType;
51 typedef typename OpenMeshType::VertexIterator VertexIterator;
52 typedef typename OpenMeshType::FaceIterator FaceIterator;
53
54 // if it is binary there are 80 char of comment, the number fn of faces and then exactly fn*4*3 bytes.
55 enum {SMF_LABEL_SIZE=80};
56
57 class SMFFacet
58 {
59 public:
60 vcg::Point3f n;
61 vcg::Point3f v[3];
62 };
63
64 enum SMFError {
65 E_NOERROR, // 0
66 // Errori di open
67 E_CANTOPEN, // 1
68 E_UNESPECTEDEOF // 2
69 };
70
71 /// Standard call for knowing the meaning of an error code
72 static const char *ErrorMsg(int error)
73 {
74 static const char * smf_error_msg[] =
75 {
76 "No errors",
77 "Can't open file",
78 "Premature End of file",
79 };
80 if(error>2 || error<0) return "Unknown error";
81 else return smf_error_msg[error];
82 };
83 ///Standard call for reading a mesh
84 static int Open(OpenMeshType &m, const char * filename )
85 {
86 VertexType v;
87 FaceType f;
88 FILE *fp;
89 float x,y,z;
90 bool one = true;
91 std::map<int,VertexPointer> mv;
92 fp = fopen(filename,"r");
93 if(!fp) return -1;
94 char buf[1024];
95 while( fgets(buf,1024,fp) )
96 {
97 char *vf, *comm_pt;
98 if((comm_pt = strstr(buf,"#")) != NULL)
99 *comm_pt = '\0';
100 if( (vf = strstr(buf,"v")) != NULL )
101 {
102 sscanf(vf+1,"%f %f %f", &x, &y, &z);
103 v.P()[0] = x;
104 v.P()[1] = y;
105 v.P()[2] = z;
106 m.vert.push_back(v);
107 }
108 else if( (vf = strstr(buf,"f")) != NULL)
109 {
110 if(one)
111 {
112 VertexIterator vi;
113 int ind;
114 for(ind=1,vi=m.vert.begin(); vi!=m.vert.end(); ++vi,++ind)
115 mv[ind]=&*vi;
116 one = false;
117 }
118 int v1,v2,v3;
119 sscanf(vf+1,"%d %d %d", &v1, &v2, &v3);
120 f.V(0) = mv[v1];
121 f.V(1) = mv[v2];
122 f.V(2) = mv[v3];
123 m.face.push_back(f);
124 }
125 }
126 m.vn = m.vert.size();
127 m.fn = m.face.size();
128 return 0;
129 }
130 };
131 }// end of io
132 }// end of tri
133 }// end of vcg
134
135 #endif
136