00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #ifndef __VCGLIB_EXPORT_VMI
00036 #define __VCGLIB_EXPORT_VMI
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051 #include <vcg/simplex/face/component.h>
00052 #include <vcg/simplex/face/component_ocf.h>
00053 #include <vcg/simplex/vertex/component.h>
00054 #include <vcg/simplex/vertex/component_ocf.h>
00055
00056 namespace vcg {
00057 namespace tri {
00058 namespace io {
00059
00060 template <int N> struct PlaceHolderType{ char A[N];};
00061
00062
00063 template <class SaveMeshType>
00064 class ExporterVMI
00065 {
00066
00067 static void WriteString(FILE *f,const char * in) { unsigned int l = strlen(in); fwrite(&l,4,1,f); fwrite(in,1,l,f);}
00068 static void WriteInt(FILE *f,const unsigned int i) { fwrite(&i,1,4,f);}
00069
00070 static void WriteFloat(FILE *f,const float v) { fwrite(&v,1,sizeof(float),f);}
00071
00072
00073 template <typename OpenMeshType,typename CONT>
00074 struct SaveVertexOcf{
00075 SaveVertexOcf(FILE*f, const CONT & , bool only_header){
00076
00077 if(only_header){
00078 WriteString(f,"NOT_HAS_VERTEX_QUALITY_OCF");
00079 WriteString(f,"NOT_HAS_VERTEX_COLOR_OCF");
00080 WriteString(f,"NOT_HAS_VERTEX_NORMAL_OCF");
00081 WriteString(f,"NOT_HAS_VERTEX_MARK_OCF");
00082 WriteString(f,"NOT_HAS_VERTEX_TEXCOORD_OCF");
00083 WriteString(f,"NOT_HAS_VERTEX_VFADJACENCY_OCF");
00084 WriteString(f,"NOT_HAS_VERTEX_CURVATURE_OCF");
00085 WriteString(f,"NOT_HAS_VERTEX_CURVATUREDIR_OCF");
00086 WriteString(f,"NOT_HAS_VERTEX_RADIUS_OCF");
00087 }
00088 }
00089 };
00090
00091
00092 template <typename MeshType>
00093 struct SaveVertexOcf<MeshType, vertex::vector_ocf<typename MeshType::VertexType> >{
00094 typedef typename MeshType::VertexType VertexType;
00095 SaveVertexOcf(FILE * f,const vertex::vector_ocf<VertexType> & vert, bool only_header){
00096
00097 if( VertexType::HasQualityOcf() && vert.IsQualityEnabled()){
00098 WriteString(f,"HAS_VERTEX_QUALITY_OCF");
00099 if(!only_header) fwrite(&vert.QV[0],sizeof(typename VertexType::QualityType),vert.size(),f);
00100 }else WriteString(f,"NOT_HAS_VERTEX_QUALITY_OCF");
00101
00102 if( VertexType::HasColorOcf() && vert.IsColorEnabled()){
00103 WriteString(f,"HAS_VERTEX_COLOR_OCF");
00104 if(!only_header) fwrite(&vert.CV[0],sizeof(typename VertexType::ColorType),vert.size(),f);
00105 }else WriteString(f,"NOT_HAS_VERTEX_COLOR_OCF");
00106
00107 if( VertexType::HasNormalOcf() && vert.IsNormalEnabled()){
00108 WriteString(f,"HAS_VERTEX_NORMAL_OCF");
00109 if(!only_header) fwrite(&vert.NV[0],sizeof(typename VertexType::NormalType),vert.size(),f);
00110 }else WriteString(f,"NOT_HAS_VERTEX_NORMAL_OCF");
00111
00112 if( VertexType::HasMarkOcf() && vert.IsMarkEnabled()){
00113 WriteString(f,"HAS_VERTEX_MARK_OCF");
00114 if(!only_header) fwrite(&vert.MV[0],sizeof(typename VertexType::MarkType),vert.size(),f);
00115 }else WriteString(f,"NOT_HAS_VERTEX_MARK_OCF");
00116
00117 if( VertexType::HasTexCoordOcf() && vert.IsTexCoordEnabled()){
00118 WriteString(f,"HAS_VERTEX_TEXCOORD_OCF");
00119 if(!only_header) fwrite(&vert.TV[0],sizeof(typename VertexType::TexCoordType),vert.size(),f);
00120 }else WriteString(f,"NOT_HAS_VERTEX_TEXCOORD_OCF");
00121
00122 if( VertexType::HasVFAdjacencyOcf() && vert.IsVFAdjacencyEnabled()){
00123 WriteString(f,"HAS_VERTEX_VFADJACENCY_OCF");
00124 if(!only_header) fwrite(&vert.AV[0],sizeof(typename vertex::vector_ocf<VertexType>::VFAdjType),vert.size(),f);
00125 }else WriteString(f,"NOT_HAS_VERTEX_VFADJACENCY_OCF");
00126
00127 if( VertexType::HasCurvatureOcf() && vert.IsCurvatureEnabled()){
00128 WriteString(f,"HAS_VERTEX_CURVATURE_OCF");
00129 if(!only_header) fwrite(&vert.CuV[0],sizeof(typename VertexType::CurvatureType),vert.size(),f);
00130 }else WriteString(f,"NOT_HAS_VERTEX_CURVATURE_OCF");
00131
00132 if( VertexType::HasCurvatureDirOcf() && vert.IsCurvatureDirEnabled()){
00133 WriteString(f,"HAS_VERTEX_CURVATUREDIR_OCF");
00134 if(!only_header) fwrite(&vert.CuDV[0],sizeof(typename VertexType::CurvatureDirType),vert.size(),f);
00135 }else WriteString(f,"NOT_HAS_VERTEX_CURVATUREDIR_OCF");
00136
00137 if( VertexType::HasRadiusOcf() && vert.IsRadiusEnabled()){
00138 WriteString(f,"HAS_VERTEX_RADIUS_OCF");
00139 if(!only_header) fwrite(&vert.RadiusV[0],sizeof(typename VertexType::RadiusType),vert.size(),f);
00140 }else WriteString(f,"NOT_HAS_VERTEX_RADIUS_OCF");
00141
00142 }
00143 };
00144
00145
00146
00147 template <typename MeshType,typename CONT>
00148 struct SaveFaceOcf{
00149 SaveFaceOcf(FILE * f,const CONT & , bool only_header){
00150
00151 if(only_header){
00152 WriteString(f,"NOT_HAS_FACE_QUALITY_OCF");
00153 WriteString(f,"NOT_HAS_FACE_COLOR_OCF");
00154 WriteString(f,"NOT_HAS_FACE_NORMAL_OCF");
00155 WriteString(f,"NOT_HAS_FACE_MARK_OCF");
00156 WriteString(f,"NOT_HAS_FACE_WEDGETEXCOORD_OCF");
00157 WriteString(f,"NOT_HAS_FACE_FFADJACENCY_OCF");
00158 WriteString(f,"NOT_HAS_FACE_VFADJACENCY_OCF");
00159 WriteString(f,"NOT_HAS_FACE_WEDGECOLOR_OCF");
00160 WriteString(f,"NOT_HAS_FACE_WEDGENORMAL_OCF");
00161 }
00162 }
00163 };
00164
00165
00166 template <typename MeshType>
00167 struct SaveFaceOcf< MeshType, face::vector_ocf<typename MeshType::FaceType> >{
00168 typedef typename MeshType::FaceType FaceType;
00169 SaveFaceOcf(FILE * f,const face::vector_ocf<FaceType> & face, bool only_header){
00170
00171 if( FaceType::HasFaceQualityOcf() && face.IsQualityEnabled()){
00172 WriteString(f,"HAS_FACE_QUALITY_OCF");
00173 if(!only_header) fwrite(&face.QV[0],sizeof(typename FaceType::QualityType),face.size(),f);
00174 }else WriteString(f,"NOT_HAS_FACE_QUALITY_OCF");
00175
00176 if( FaceType::HasFaceColorOcf() && face.IsColorEnabled()){
00177 WriteString(f,"HAS_FACE_COLOR_OCF");
00178 if(!only_header) fwrite(&face.CV[0],sizeof(typename FaceType::ColorType),face.size(),f);
00179 }else WriteString(f,"NOT_HAS_FACE_COLOR_OCF");
00180
00181 if( FaceType::HasFaceNormalOcf() && face.IsNormalEnabled()){
00182 WriteString(f,"HAS_FACE_NORMAL_OCF");
00183 if(!only_header) fwrite(&face.NV[0],sizeof(typename FaceType::NormalType),face.size(),f);
00184 }else WriteString(f,"NOT_HAS_FACE_NORMAL_OCF");
00185
00186 if( FaceType::HasFaceMarkOcf() && face.IsMarkEnabled()){
00187 WriteString(f,"HAS_FACE_MARK_OCF");
00188 if(!only_header) fwrite(&face.MV[0],sizeof(typename FaceType::MarkType),face.size(),f);
00189 }else WriteString(f,"NOT_HAS_FACE_MARK_OCF");
00190
00191 if( FaceType::HasWedgeTexCoordOcf() && face.IsWedgeTexEnabled()){
00192 WriteString(f,"HAS_FACE_WEDGETEXCOORD_OCF");
00193 if(!only_header) fwrite(&face.WTV[0],sizeof(typename FaceType::WedgeTexCoordType),face.size(),f);
00194 }else WriteString(f,"NOT_HAS_FACE_WEDGETEXCOORD_OCF");
00195
00196 if( FaceType::HasFFAdjacencyOcf() && face.IsFFAdjacencyEnabled()){
00197 WriteString(f,"HAS_FACE_FFADJACENCY_OCF");
00198 if(!only_header) fwrite(&face.AF[0],sizeof(typename face::vector_ocf<FaceType>::AdjTypePack),face.size(),f);
00199 }else WriteString(f,"NOT_HAS_FACE_FFADJACENCY_OCF");
00200
00201 if( FaceType::HasVFAdjacencyOcf() && face.IsVFAdjacencyEnabled()){
00202 WriteString(f,"HAS_FACE_VFADJACENCY_OCF");
00203 if(!only_header) fwrite(&face.AV[0],sizeof(typename face::vector_ocf<FaceType>::AdjTypePack),face.size(),f);
00204 }else WriteString(f,"NOT_HAS_FACE_VFADJACENCY_OCF");
00205
00206 if( FaceType::HasWedgeColorOcf() && face.IsWedgeColorEnabled()){
00207 WriteString(f,"HAS_FACE_WEDGECOLOR_OCF");
00208 if(!only_header) fwrite(&face.WCV[0],sizeof(typename face::vector_ocf<FaceType>::WedgeColorTypePack),face.size(),f);
00209 }else WriteString(f,"NOT_HAS_FACE_WEDGECOLOR_OCF");
00210
00211 if( FaceType::HasWedgeNormalOcf() && face.IsWedgeNormalEnabled()){
00212 WriteString(f,"HAS_FACE_WEDGENORMAL_OCF");
00213 if(!only_header) fwrite(&face.WNV[0],sizeof(typename face::vector_ocf<FaceType>::WedgeNormalTypePack),face.size(),f);
00214 }else WriteString(f,"NOT_HAS_FACE_WEDGENORMAL_OCF");
00215 }
00216 };
00217
00218
00219
00220 static FILE *& F(){static FILE * f; return f;}
00221
00222 typedef typename SaveMeshType::FaceContainer FaceContainer;
00223 typedef typename SaveMeshType::FaceIterator FaceIterator;
00224 typedef typename SaveMeshType::VertContainer VertContainer;
00225 typedef typename SaveMeshType::VertexIterator VertexIterator;
00226 typedef typename SaveMeshType::VertexType VertexType;
00227 typedef typename SaveMeshType::FaceType FaceType;
00228 typedef SimpleTempDataBase<typename SaveMeshType::VertContainer> STDBv;
00229 typedef SimpleTempDataBase<typename SaveMeshType::FaceContainer> STDBf;
00230
00231
00232
00233
00234
00235 public:
00236 static int Save(const SaveMeshType &m,const char * filename){
00237 unsigned int i;
00238 unsigned int vertSize,faceSize;
00239 F() = fopen(filename,"wb");
00240 if(F()==NULL) return 1;
00241 std::vector<std::string> nameF,nameV;
00242 SaveMeshType::FaceType::Name(nameF);
00243 SaveMeshType::VertexType::Name(nameV);
00244 vertSize = m.vert.size();
00245 faceSize = m.face.size();
00246
00247
00248 WriteString(F(),"FACE_TYPE");
00249 WriteInt(F(),nameF.size());
00250
00251 for(i=0; i < nameF.size(); ++i) WriteString(F(),nameF[i].c_str());
00252 SaveFaceOcf<SaveMeshType,FaceContainer>(F(),m.face,true);
00253 WriteString(F(),"SIZE_VECTOR_FACES");
00254 WriteInt(F(), faceSize );
00255
00256 WriteString(F(),"VERTEX_TYPE");
00257 WriteInt(F(),nameV.size());
00258
00259 for(i=0; i < nameV.size(); ++i) WriteString(F(),nameV[i].c_str());
00260 SaveVertexOcf<SaveMeshType,VertContainer>(F(),m.vert,true);
00261
00262 WriteString(F(),"SIZE_VECTOR_VERTS");
00263 WriteInt(F(),vertSize);
00264
00265 WriteString(F(),"BOUNDING_BOX");
00266 float float_value;
00267 for(unsigned int i =0; i < 2; ++i){float_value = m.bbox.min[i]; WriteFloat(F(),float_value);}
00268 for(unsigned int i =0; i < 2; ++i){float_value = m.bbox.max[i]; WriteFloat(F(),float_value);}
00269
00270 WriteString(F(),"end_header");
00271
00272
00273 if(vertSize!=0){
00274 size_t offsetV = (size_t) &m.vert[0];
00275
00276 fwrite(&offsetV,sizeof(size_t),1,F());
00277 }
00278
00279 if(faceSize!=0){
00280 size_t offsetF= ( size_t) &m.face[0];
00281
00282 fwrite(&offsetF,sizeof( size_t),1,F());
00283 }
00284
00285 fwrite(&m.shot,sizeof(Shot<typename SaveMeshType::ScalarType>),1,F());
00286 fwrite(&m.vn,sizeof(int),1,F());
00287 fwrite(&m.fn,sizeof(int),1,F());
00288 fwrite(&m.imark,sizeof(int),1,F());
00289 fwrite(&m.bbox,sizeof(Box3<typename SaveMeshType::ScalarType>),1,F());
00290 fwrite(&m.C(),sizeof(Color4b),1,F());
00291
00292 unsigned int written;
00293
00294
00295 if(vertSize!=0){
00296
00297 written = fwrite((void*)&m.vert[0],sizeof(typename SaveMeshType::VertexType),m.vert.size(),F());
00298 assert(written==m.vert.size());
00299 SaveVertexOcf<SaveMeshType,VertContainer>(F(),m.vert,false);
00300 }
00301
00302 if(faceSize!=0){
00303
00304 written = fwrite((void*)&m.face[0],sizeof(typename SaveMeshType::FaceType),faceSize,F());
00305 assert(written==m.face.size());
00306
00307 SaveFaceOcf<SaveMeshType,FaceContainer>(F(),m.face,false);
00308
00309 }
00310
00311
00312
00313
00314 typename std::set< typename SaveMeshType::PointerToAttribute>::const_iterator ai;
00315
00316
00317
00318 {
00319 typename std::set< typename SaveMeshType::PointerToAttribute>::const_iterator ai;
00320 unsigned int n_named_attr = 0;
00321 for(ai = m.vert_attr.begin(); ai != m.vert_attr.end(); ++ai) n_named_attr+=!(*ai)._name.empty();
00322
00323 WriteString(F(),"N_PER_VERTEX_ATTRIBUTES"); WriteInt (F(),n_named_attr);
00324 for(ai = m.vert_attr.begin(); ai != m.vert_attr.end(); ++ai)
00325 if(!(*ai)._name.empty())
00326 {
00327 STDBv * stdb = (STDBv *) (*ai)._handle;
00328
00329 WriteString(F(),"PER_VERTEX_ATTR_NAME");
00330 WriteString(F(),(*ai)._name.c_str() );
00331
00332 WriteString(F(),"PER_VERTEX_ATTR_SIZE");
00333 WriteInt(F(),stdb->SizeOf());
00334
00335 fwrite(stdb->DataBegin(),m.vert.size(),stdb->SizeOf(),F());
00336 }
00337 }
00338
00339
00340 {
00341 typename std::set< typename SaveMeshType::PointerToAttribute>::const_iterator ai;
00342 unsigned int n_named_attr = 0;
00343 for(ai = m.face_attr.begin(); ai != m.face_attr.end(); ++ai) n_named_attr+=!(*ai)._name.empty();
00344
00345 WriteString(F(),"N_PER_FACE_ATTRIBUTES");
00346 WriteInt (F(),n_named_attr);
00347
00348 for(ai = m.face_attr.begin(); ai != m.face_attr.end(); ++ai)
00349 if(!(*ai)._name.empty())
00350 {
00351 STDBf * stdb = (STDBf *) (*ai)._handle;
00352
00353 WriteString(F(),"PER_FACE_ATTR_NAME");
00354 WriteString(F(),(*ai)._name.c_str());
00355
00356 WriteString(F(),"PER_FACE_ATTR_SIZE");
00357 WriteInt(F(),stdb->SizeOf());
00358
00359 fwrite(stdb->DataBegin(),m.face.size(),stdb->SizeOf(),F());
00360 }
00361 }
00362
00364 {
00365 typename std::set< typename SaveMeshType::PointerToAttribute>::const_iterator ai;
00366 unsigned int n_named_attr = 0;
00367 for(ai = m.mesh_attr.begin(); ai != m.mesh_attr.end(); ++ai) n_named_attr+=!(*ai)._name.empty();
00368 WriteString(F(),"N_PER_MESH_ATTRIBUTES"); WriteInt(F(),n_named_attr);
00369 for(ai = m.mesh_attr.begin(); ai != m.mesh_attr.end(); ++ai)
00370 if(!(*ai)._name.empty())
00371 {
00372 AttributeBase * handle = (AttributeBase *) (*ai)._handle ;
00373
00374 WriteString(F(),"PER_MESH_ATTR_NAME");
00375 WriteString(F(),(*ai)._name.c_str());
00376
00377 WriteString(F(),"PER_MESH_ATTR_SIZE");
00378 WriteInt(F(),handle->SizeOf());
00379
00380 fwrite(handle->DataBegin(),1,handle->SizeOf(),F());
00381 }
00382 }
00383
00384
00385 fclose(F());
00386 return 0;
00387 }
00388 static const char *ErrorMsg(int error)
00389 {
00390 static std::vector<std::string> off_error_msg;
00391 if(off_error_msg.empty())
00392 {
00393 off_error_msg.resize(2 );
00394 off_error_msg[0]="No errors";
00395 off_error_msg[1]="Can't open file";
00396 }
00397
00398 if(error>1 || error<0) return "Unknown error";
00399 else return off_error_msg[error].c_str();
00400 }
00401 };
00402
00403 }
00404 }
00405 }
00406
00407 #endif