Debian Codegraph @

vcg/wrap/io_trimesh/export_stl.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: export_stl.h,v $
28 Revision 1.9 2006/11/30 22:49:32 cignoni
29 Added save with (unused) callback
30
31 Revision 1.8 2006/09/18 08:55:33 cignoni
32 Corrected return value of save function (zero is no error)
33
34 Revision 1.7 2006/01/30 13:43:59 cignoni
35 Added GetExportMaskCapability
36
37 Revision 1.6 2006/01/13 15:47:43 cignoni
38 Uniformed return type to the style of Open. Now every export function returns 0 in case of success.
39
40 Revision 1.5 2005/12/01 00:58:56 cignoni
41 Added and removed typenames for gcc compiling...
42
43 Revision 1.4 2004/10/28 00:52:45 cignoni
44 Better Doxygen documentation
45
46 Revision 1.3 2004/03/09 21:26:47 cignoni
47 cr lf mismatch
48
49 Revision 1.2 2004/03/03 15:35:53 cignoni
50 Yet another cr lf mismatch
51
52 Revision 1.3 2004/02/19 15:28:01 ponchio
53 *** empty log message ***
54
55 Revision 1.2 2004/02/13 02:18:57 cignoni
56 Edited Comments and GPL license
57
58
59 ****************************************************************************/
60
61 #ifndef __VCGLIB_EXPORT_STL
62 #define __VCGLIB_EXPORT_STL
63
64 #include <stdio.h>
65
66 namespace vcg {
67 namespace tri {
68 namespace io {
69
70 /**
71 This class encapsulate a filter for opening stl (sterolitograpy) meshes.
72 The stl format is quite simple and rather un-flexible. It just stores, in ascii or binary the, unindexed, geometry of the faces.
73 */
74 template <class SaveMeshType>
75 class ExporterSTL
76 {
77 public:
78 static int Save(SaveMeshType &m, const char * filename, const int &mask, CallBackPos *cb=0)
79 {
80 return Save(m,filename,true);
81 }
82
83 static int Save(SaveMeshType &m, const char * filename , bool binary =true, const char *objectname=0)
84 {
85 typedef typename SaveMeshType::FaceIterator FaceIterator;
86 FILE *fp;
87
88 fp = fopen(filename,"wb");
89 if(fp==0)
90 return 1;
91
92 if(binary)
93 {
94 // Write Header
95 char *header="VCG ";
96 if(objectname) strncpy(header,objectname,80);
97 fwrite(header,80,1,fp);
98 // write number of facets
99 fwrite(&m.fn,1,sizeof(int),fp);
100 Point3f p;
101 unsigned short attributes=0;
102
103 FaceIterator fi;
104 for(fi=m.face.begin(); fi!=m.face.end(); ++fi) if( !(*fi).IsD() )
105 {
106 // For each triangle write the normal, the three coords and a short set to zero
107 p.Import(vcg::NormalizedNormal(*fi));
108 fwrite(p.V(),3,sizeof(float),fp);
109
110 for(int k=0;k<3;++k){
111 p.Import((*fi).V(k)->P());
112 fwrite(p.V(),3,sizeof(float),fp);
113 }
114 fwrite(&attributes,1,sizeof(short),fp);
115 }
116 }
117 else
118 {
119 if(objectname) fprintf(fp,"solid %s\n",objectname);
120 else fprintf(fp,"solid vcg\n");
121
122 Point3f p;
123 FaceIterator fi;
124 for(fi=m.face.begin(); fi!=m.face.end(); ++fi) if( !(*fi).IsD() )
125 {
126 // For each triangle write the normal, the three coords and a short set to zero
127 p.Import(vcg::NormalizedNormal(*fi));
128 fprintf(fp," facet normal %13e %13e %13e\n",p[0],p[1],p[2]);
129 fprintf(fp," outer loop\n");
130 for(int k=0;k<3;++k){
131 p.Import((*fi).V(k)->P());
132 fprintf(fp," vertex %13e %13e %13e\n",p[0],p[1],p[2]);
133 }
134 fprintf(fp," endloop\n");
135 fprintf(fp," endfacet\n");
136 }
137 fprintf(fp,"endsolid vcg\n");
138 }
139 fclose(fp);
140 return 0;
141 }
142 static const char *ErrorMsg(int error)
143 {
144 static std::vector<std::string> stl_error_msg;
145 if(stl_error_msg.empty())
146 {
147 stl_error_msg.resize(2 );
148 stl_error_msg[0]="No errors";
149 stl_error_msg[1]="Can't open file";
150 }
151
152 if(error>1 || error<0) return "Unknown error";
153 else return stl_error_msg[error].c_str();
154 };
155
156 /*
157 returns mask of capability one define with what are the saveable information of the format.
158 */
159 static int GetExportMaskCapability()
160 {
161 int capability = 0;
162 capability |= vcg::tri::io::Mask::IOM_VERTCOORD;
163 capability |= vcg::tri::io::Mask::IOM_FACEINDEX;
164 return capability;
165 }
166
167
168 }; // end class
169
170 } // end Namespace tri
171 } // end Namespace io
172 } // end Namespace vcg
173
174
175
176 #endif