Debian Codegraph @

vcg/wrap/io_trimesh/export_off.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
26 $Log: export_off.h,v $
27 Revision 1.14 2008/04/15 10:38:12 cignoni
28 added suppor of color in OFF format.
29
30 Revision 1.13 2007/11/06 10:58:25 cignoni
31 Changed the return value to the standard 0 in case of success and notzero for failures
32
33 Revision 1.12 2007/03/12 16:40:17 tarini
34 Texture coord name change! "TCoord" and "Texture" are BAD. "TexCoord" is GOOD.
35
36 Revision 1.11 2006/12/07 00:37:58 cignoni
37 Corrected bug in the management of deleted vertices
38
39 ****************************************************************************/
40
41 /**
42 @name Save in OFF format
43 */
44 //@{
45
46 #ifndef __VCGLIB_EXPORT_OFF
47 #define __VCGLIB_EXPORT_OFF
48
49 #include <stdio.h>
50 #include <wrap/io_trimesh/io_mask.h>
51
52
53 namespace vcg {
54 namespace tri {
55 namespace io {
56 template <class SaveMeshType>
57 class ExporterOFF
58 {
59
60 public:
61 typedef typename SaveMeshType::VertexPointer VertexPointer;
62 typedef typename SaveMeshType::ScalarType ScalarType;
63 typedef typename SaveMeshType::VertexType VertexType;
64 typedef typename SaveMeshType::FaceType FaceType;
65 typedef typename SaveMeshType::FacePointer FacePointer;
66 typedef typename SaveMeshType::VertexIterator VertexIterator;
67 typedef typename SaveMeshType::FaceIterator FaceIterator;
68
69 static int Save(SaveMeshType &m, const char * filename, int mask=0 )
70 {
71 vcg::face::Pos<FaceType> he;
72 vcg::face::Pos<FaceType> hei;
73 FILE * fpout = fopen(filename,"w");
74 if(fpout==NULL) return 1; // 1 is the error code for cant'open, see the ErrorMsg function
75
76
77
78 if( m.HasPerVertexNormal() && (mask & io::Mask::IOM_VERTNORMAL)) fprintf(fpout,"N");
79 if( tri::HasPerVertexColor(m) && (mask & io::Mask::IOM_VERTCOLOR)) fprintf(fpout,"C");
80 if( tri::HasPerVertexTexCoord(m) && (mask & io::Mask::IOM_VERTTEXCOORD)) fprintf(fpout,"ST");
81 fprintf(fpout,"OFF\n");
82 fprintf(fpout,"%d %d 0\n", m.vn, m.fn); // note that as edge number we simply write zero
83 typename SaveMeshType::FaceIterator fi;
84
85 //vertices
86 int j;
87 std::vector<int> FlagV;
88 VertexPointer vp;
89 VertexIterator vi;
90 for(j=0,vi=m.vert.begin();vi!=m.vert.end();++vi)
91 {
92 vp=&(*vi);
93 FlagV.push_back(vp->UberFlags()); // Salva in ogni caso flag del vertice
94 if( ! vp->IsD() )
95 { // ***** ASCII *****
96
97 fprintf(fpout,"%g %g %g " ,vp->P()[0],vp->P()[1],vp->P()[2]);
98 if( tri::HasPerVertexColor(m) && (mask & io::Mask::IOM_VERTCOLOR) )
99 fprintf(fpout,"%d %d %d %d ",vp->C()[0],vp->C()[1],vp->C()[2],vp->C()[3] );
100
101 if( m.HasPerVertexNormal() && (mask & io::Mask::IOM_VERTNORMAL) )
102 fprintf(fpout,"%g %g %g ", vp->N()[0],vp->N()[1],vp->N()[2]);
103
104 if( m.HasPerVertexTexCoord() && (mask & io::Mask::IOM_VERTTEXCOORD) )
105 fprintf(fpout,"%g %g ",vp->T().u(),vp->T().v());
106
107 fprintf(fpout,"\n");
108
109
110 vp->UberFlags()=j; // Trucco! Nascondi nei flags l'indice del vertice non deletato!
111 j++;
112 }
113 }
114
115 assert(j==m.vn);
116 FacePointer fp;
117 // int vv[3];
118
119 int fcnt=0;
120 for(j=0,fi=m.face.begin();fi!=m.face.end();++fi)
121 {
122 fp=&(*fi);
123 if( ! fp->IsD() )
124 { fcnt++;
125
126
127 fprintf(fpout,"3 %d %d %d\n",
128 fp->cV(0)->UberFlags(), fp->cV(1)->UberFlags(), fp->cV(2)->UberFlags() );
129 }
130 }
131
132
133 fclose(fpout);
134 // Recupera i flag originali
135 for(j=0,vi=m.vert.begin();vi!=m.vert.end();++vi)
136 (*vi).UberFlags()=FlagV[j++];
137
138 return 0;
139 }
140
141 static const char *ErrorMsg(int error)
142 {
143 static std::vector<std::string> off_error_msg;
144 if(off_error_msg.empty())
145 {
146 off_error_msg.resize(2 );
147 off_error_msg[0]="No errors";
148 off_error_msg[1]="Can't open file";
149 }
150
151 if(error>1 || error<0) return "Unknown error";
152 else return off_error_msg[error].c_str();
153 }
154 /*
155 returns mask of capability one define with what are the saveable information of the format.
156 */
157 static int GetExportMaskCapability()
158 {
159 int capability = 0;
160 capability |= vcg::tri::io::Mask::IOM_VERTCOORD;
161 capability |= vcg::tri::io::Mask::IOM_VERTCOLOR;
162 capability |= vcg::tri::io::Mask::IOM_FACEINDEX;
163 return capability;
164 }
165
166 }; // end class
167 } // end namespace tri
168 } // end namespace io
169 } // end namespace vcg
170 //@}
171 #endif