File: export_vrml.h

package info (click to toggle)
meshlab 2020.09%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 45,124 kB
  • sloc: cpp: 400,238; ansic: 31,952; javascript: 1,578; sh: 387; yacc: 238; lex: 139; python: 86; makefile: 29
file content (324 lines) | stat: -rw-r--r-- 9,932 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/****************************************************************************
* VCGLib                                                            o o     *
* Visual and Computer Graphics Library                            o     o   *
*                                                                _   O  _   *
* Copyright(C) 2004-2016                                           \/)\/    *
* Visual Computing Lab                                            /\/|      *
* ISTI - Italian National Research Council                           |      *
*                                                                    \      *
* All rights reserved.                                                      *
*                                                                           *
* This program is free software; you can redistribute it and/or modify      *   
* it under the terms of the GNU General Public License as published by      *
* the Free Software Foundation; either version 2 of the License, or         *
* (at your option) any later version.                                       *
*                                                                           *
* This program is distributed in the hope that it will be useful,           *
* but WITHOUT ANY WARRANTY; without even the implied warranty of            *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
* for more details.                                                         *
*                                                                           *
****************************************************************************/
/****************************************************************************
History
$Log: not supported by cvs2svn $
Revision 1.8  2008/02/21 17:47:29  cignoni
corrected texture saving. Still broken the multitexturing

Revision 1.7  2008/02/21 17:23:57  cignoni
Corrected various bug, involving spurious commas, and pervertex color saved as per wedge color.

Revision 1.6  2007/06/12 10:15:35  cignoni
Very important change. No more scaling and translation in the saved file!

Revision 1.5  2007/03/20 16:47:49  cignoni
Update to the new texture syntax

Revision 1.4  2006/11/21 19:22:53  e_cerisoli
Added Comments for documentation

****************************************************************************/

#ifndef __VCGLIB_EXPORT_WRL
#define __VCGLIB_EXPORT_WRL

#include <stdio.h>
#include <wrap/io_trimesh/io_mask.h>

namespace vcg {
	namespace tri {
		namespace io {

			template <class SaveMeshType>
			/** 
			This class encapsulate a filter for save vrml meshes.
			*/
			class ExporterWRL
			{
			public:
				typedef typename SaveMeshType::VertexPointer VertexPointer;
				typedef typename SaveMeshType::ScalarType ScalarType;
				typedef typename SaveMeshType::VertexType VertexType;
				typedef typename SaveMeshType::FaceType FaceType;
				typedef typename SaveMeshType::VertexIterator VertexIterator;
				typedef typename SaveMeshType::FaceIterator FaceIterator;
				
				///Standard call for saving a mesh
				static int Save(SaveMeshType &m, const char * filename, const int &mask, CallBackPos * /*cb=0*/)
				{					
					FILE *fp;
					fp = fopen(filename,"wb");
					if(fp==NULL)
						return 1;

					// Header					
					fprintf(fp,
						 "#VRML V2.0 utf8\n"  
   	 					"\n" 	 					
						"# Generated by VCGLIB, (C)Copyright 1999-2001 VCG, IEI-CNR\n"
						"\n"						
						"NavigationInfo {\n"						
						"	type [ \"EXAMINE\", \"ANY\" ]\n"						
						"}\n"						
						);					

					// Tranche principale
					//double ss = 8.0/m.bbox.Diag();					
					
					fprintf(fp,
					"Transform {\n"
					"  scale %g %g %g\n"
					"  translation %g %g %g\n"
					"  children\n"
					"  [\n"
					,1.0,1.0,1.0,
					 0.0,0.0,0.0);
//					,ss,ss,ss
//					,  -m.bbox.Center()[0]*ss,  -m.bbox.Center()[1]*ss,-3-m.bbox.Center()[2]*ss	);

					// Start Shape
					fprintf(fp,
					"    Shape\n"
					"    {\n"
					"      geometry IndexedFaceSet\n"
					"      {\n"
					"        creaseAngle .5\n"
					"        solid FALSE\n"
					"        coord Coordinate\n"
					"        {\n"
					"          point\n"
					"          ["
					);
					FaceIterator fi;	
					VertexIterator vi;	
					std::map<VertexPointer,int> index;
					int ind;

					// Vertici
					for(ind=0,vi=m.vert.begin(); vi!=m.vert.end(); ++vi, ++ind)	
					if(!(*vi).IsD())
						{
							if(vi!=m.vert.begin()) fprintf(fp,", ");
							if(ind%4==0) fprintf(fp, "\n            " );
							fprintf(fp, "%g %g %g" ,(*vi).P()[0] ,(*vi).P()[1] ,(*vi).P()[2] 	);
							index[&*vi] = ind;
						}
					fprintf(fp,"\n"
								"          ]\n"
								"        }\n"
								);
					
					
					if( HasPerVertexColor(m) && (mask & vcg::tri::io::Mask::IOM_VERTCOLOR))
					{
						fprintf(fp,
							"        color Color\n"
							"        {\n"
							"          color\n"
							"          ["
							);
						for(ind=0,vi=m.vert.begin();vi!=m.vert.end();++vi,++ind)
						if(!(*vi).IsD())
						{
							if(vi!=m.vert.begin()) fprintf(fp,", ");
							float r = float(vi->C()[0])/255;
							float g = float(vi->C()[1])/255;
							float b = float(vi->C()[2])/255;

							if(ind%4==0)
								fprintf(fp,"\n            ");
							fprintf(fp,"%g %g %g",r,g,b);
						}
						fprintf(fp,	
							"\n"
							"          ]\n"							
							"        }\n"
							);
					}
					else if( HasPerWedgeColor(m) &&  (mask & vcg::tri::io::Mask::IOM_WEDGCOLOR ))
					{						
						fprintf(fp,
							"        color Color\n"	
							"        {\n"							
							"          color\n"							
							"          ["							);
						for(ind=0,fi=m.face.begin();fi!=m.face.end();++fi,++ind)
						if(!(*fi).IsD())
						{
							if(fi!=m.face.begin()) fprintf(fp,", ");
							if(ind%4==0) fprintf(fp,"\n            ");
								for(int z=0;z<3;++z)							
								{								
									if(z!=0) fprintf(fp,", ");
									float r = float(fi->WC(z)[0])/255;
									float g = float(fi->WC(z)[1])/255;
									float b = float(fi->WC(z)[2])/255;
									fprintf(fp,"%g %g %g",r,g,b);
								}
						}						
						fprintf(fp,							
							"\n"							
							"          ]\n"							
							"        }\n"						
							"        colorIndex\n"							
							"        ["							);						
						int nn = 0;						
						for(ind=0,fi=m.face.begin(); fi!=m.face.end(); ++fi,++ind)
						if(!(*fi).IsD())
						{
							//if(fi!=m.face.begin()) fprintf(fp,", ");
							if(ind%4==0) fprintf(fp,"\n          ");							
									fprintf(fp,"%i",nn++);								
									fprintf(fp,"%i",nn++);								
									fprintf(fp,"%i",nn++);								
							fprintf(fp,"-1");						
						}						
						fprintf(fp,							
							"\n"							
							"       ]\n"							
							);					
					}
					// NOTE MULTITEXTURE WRL SAVING DO NOT WORK!
					else if (HasPerWedgeTexCoord(m) &&(mask &  vcg::tri::io::Mask::IOM_WEDGTEXCOORD))
					{
						fprintf(fp,
							"\n"
							"        texCoord TextureCoordinate\n"
							"        {\n"
							"          point\n"
							"          [\n"
							);
						for(ind=0,fi=m.face.begin(); fi!=m.face.end(); ++fi,++ind)
						if(!(*fi).IsD())
							{
								//if(fi!=m.face.begin()) fprintf(fp,", ");
								if(ind%4==0) fprintf(fp,"\n          ");
								for (int j = 0; j < 3; j++)
										fprintf(fp,"%g %g ",fi->WT(j).u(),fi->WT(j).v());
							}
						fprintf(fp,
							"\n"
							"          ]\n"
							"        }\n"
							"        texCoordIndex\n"
							"        [\n"
							);
						int nn = 0;
						for(ind=0,fi=m.face.begin(); fi!=m.face.end(); ++fi,++ind)
							if(!(*fi).IsD())
							{
								//if(fi!=m.face.begin()) fprintf(fp,", ");
								if(ind%4==0) fprintf(fp,"\n          ");								
								for (int j = 0; j < 3; j++)
									fprintf(fp,"%d ",nn++);
								fprintf(fp,"-1 ");
							}
						fprintf(fp,
							"\n"
							"        ]\n"
							);
					}
					fprintf(fp,
							"        coordIndex\n"
							"        ["
							);
					// Facce
					for(ind=0,fi=m.face.begin(); fi!=m.face.end(); ++fi,++ind)
					if(!(*fi).IsD())
					{
						if(fi!=m.face.begin()) fprintf(fp,", ");
						if(ind%6==0) fprintf(fp,"\n          ");								
						for (int j = 0; j < 3; j++)
							fprintf(fp,"%i,",index[(*fi).V(j)]);
						
						fprintf(fp,"-1");	
					}
					fprintf(fp,
							"\n"
							"        ]\n"
							"      }\n"
							"      appearance Appearance\n"
							"      {\n"						
							"        material Material\n"
							"        {\n"
							"	       ambientIntensity 0.2\n"
							"	       diffuseColor 0.9 0.9 0.9\n"
							"	       specularColor .1 .1 .1\n"
							"	       shininess .5\n"
							"        }\n"
							);
					if(m.textures.size())
					{
						fprintf(fp,
							"        texture ImageTexture { url \"%s\" }\n"
							,m.textures[0].c_str()
							);
					}
					fprintf(fp,
						"      }\n"
						"    }\n"
						"  ]\n"
						"}\n"
						);
					int result = 0;
					if (ferror(fp)) result = 2;
					fclose(fp);
					return result;
	}
	///Returns mask of capability one define with what are the saveable information of the format.
	static int GetExportMaskCapability()
	{
		int capability = 0;

		//vert
		capability |= Mask::IOM_VERTCOLOR;
		
		//wedg
		capability |= Mask::IOM_WEDGTEXCOORD;
		capability |= Mask::IOM_WEDGCOLOR;

		return capability;
	}

	/// Standard call for knowing the meaning of an error code
	static const char *ErrorMsg(int error)
	{
		static std::vector<std::string> wrl_error_msg;
		if(wrl_error_msg.empty())
		{
			wrl_error_msg.resize(3);
			wrl_error_msg[0] = "No errors";
			wrl_error_msg[1] = "Can't open file";
			wrl_error_msg[1] = "Output Stream error";
		}
		if(error>2 || error<0) return "Unknown error";
		else return wrl_error_msg[error].c_str();
		}

}; // end class
} // end Namespace io
} // end Namespace tri
} // end Namespace vcg

#endif