import_vmi.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 * VCGLib                                                            o o     *
00003 * Visual and Computer Graphics Library                            o     o   *
00004 *                                                                _   O  _   *
00005 * Copyright(C) 2004                                                \/)\/    *
00006 * Visual Computing Lab                                            /\/|      *
00007 * ISTI - Italian National Research Council                           |      *
00008 *                                                                    \      *
00009 * All rights reserved.                                                      *
00010 *                                                                           *
00011 * This program is Free software; you can redistribute it and/or modify      *
00012 * it under the terms of the GNU General Public License as published by      *
00013 * the Free Software Foundation; either version 2 of the License, or         *
00014 * (at your option) any later version.                                       *
00015 *                                                                           *
00016 * This program is distributed in the hope that it will be useful,           *
00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
00019 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
00020 * for more details.                                                         *
00021 *                                                                           *
00022 ****************************************************************************/
00023 
00024 /****************************************************************************
00025   History
00026 
00027  $Log: not supported by cvs2svn $
00028  Revision 1.1  2007/02/14 01:20:37  ganovelli
00029  working draft of VCG Mesh Image importer and exporter. Does not consider optional attributes. The mesh atributes are only vn and fn (no bbox, texture coordiantes)
00030 
00031 
00032 
00033 ****************************************************************************/
00034 
00035 #ifndef __VCGLIB_IMPORT_VMI
00036 #define __VCGLIB_IMPORT_VMI
00037 
00038 #include <wrap/io_trimesh/io_mask.h>
00039 #include <wrap/callback.h>
00040 /*
00041     VMI VCG Mesh Image.
00042     The vmi image file consists of a header containing the description of the vertex and face type,
00043     the length of vectors containing vertices of faces and the memory image of the object mesh as it is when
00044     passed to the function Save(SaveMeshType m)
00045     NOTE: THIS IS NOT A FILE FORMAT. IT IS ONLY USEFUL FOR DUMPING MESH IMAGES FOR DEBUG PURPOSE.
00046     Example of use: say you are running a time consuming mesh processing and you want to save intermediate
00047     state, but no file format support all the attributes you need in your vertex/face type.
00048     NOTE2: At the present if you add members to your TriMesh these will NOT be saved. More precisely, this file and
00049     import_vmi must be updated to reflect changes in vcg/complex/trimesh/base.h
00050 
00051 */
00052 
00053 namespace vcg {
00054 namespace tri {
00055 namespace io {
00056 
00057     template <int N> struct DummyType{ char placeholder[N]; };
00058 
00059     /* ------------------------- derivation chain for the vertex attribute ---------------------------*/
00060 
00063     template <class MeshType, class A, class T>
00064     struct Der:public T{
00065         typedef typename std::set<typename MeshType::PointerToAttribute >::iterator HWIte;
00066 
00067         template <int VoF>
00068         static void AddAttrib(MeshType &m, const char * name, unsigned int s, void * data){
00069             switch(VoF)
00070             {
00071             case 0: if(s == sizeof(A)){
00072                     typename MeshType::template PerVertexAttributeHandle<A> h = vcg::tri::Allocator<MeshType>:: template AddPerVertexAttribute<A>(m,name);
00073                     for(unsigned int i  = 0; i < m.vert.size(); ++i)
00074                         memcpy(&h[i], (void*) &((A*)data)[i],sizeof(A)); // we don't want the type conversion
00075                         }
00076                     else
00077                         T::template AddAttrib<0>(m,name,s,data);
00078                 break;
00079             case 1: if(s == sizeof(A)){
00080                     typename MeshType::template PerFaceAttributeHandle<A> h = vcg::tri::Allocator<MeshType>:: template AddPerFaceAttribute<A>(m,name);
00081                     for(unsigned int i  = 0; i < m.face.size(); ++i)
00082                         memcpy(&h[i], (void*) &((A*)data)[i],sizeof(A)); // we don't want the type conversion
00083                     }
00084                     else
00085                         T::template AddAttrib<0>(m,name,s,data);
00086                 break;
00087             case 2:
00088                 if(s == sizeof(A)){
00089                     typename MeshType::template PerMeshAttributeHandle<A> h = vcg::tri::Allocator<MeshType>:: template AddPerMeshAttribute<A>(m,name);
00090                         memcpy(&h(), (void*) ((A*)data),sizeof(A)); // we don't want the type conversion
00091                 }
00092                     else
00093                         T::template AddAttrib<2>(m,name,s,data);
00094                 break;
00095 
00096             default:break;
00097             }
00098         }
00099     };
00100 
00103     template <class MeshType, class A, class T>
00104     struct DerK:public T{
00105         typedef typename std::set<typename MeshType::PointerToAttribute >::iterator HWIte;
00106         template <int VoF>
00107         static void AddAttrib(MeshType &m, const char * name, unsigned int s, void * data){
00108             switch(VoF){
00109                     case 0:
00110                     if(s == sizeof(A)){
00111                         typename MeshType::template PerVertexAttributeHandle<A> h = vcg::tri::Allocator<MeshType>::template AddPerVertexAttribute<A>(m,name);
00112                         for(unsigned int i  = 0; i < m.vert.size(); ++i)
00113                             memcpy((void*) &(h[i]), (void*) &((A*)data)[i],sizeof(A)); // we don't want the type conversion
00114                     }
00115                     else
00116                         if(s < sizeof(A)){
00117                             // padding
00118                             int padd = sizeof(A) - s;
00119                             typename MeshType::template PerVertexAttributeHandle<A> h = vcg::tri::Allocator<MeshType>::template AddPerVertexAttribute<A>(m,name);
00120                             for(unsigned int i  = 0; i < m.vert.size(); ++i){
00121                                 char * dest =  &((char*)(&h[i]))[0];
00122                                 memcpy( (void *)dest , (void*) &((A*)data)[i],s); // we don't want the type conversion
00123                             }
00124                             typename MeshType::PointerToAttribute pa;
00125                             pa._name = std::string(name);
00126                             HWIte res = m.vert_attr.find(pa);
00127                             pa = *res;
00128                             m.vert_attr.erase(res);
00129                             pa._padding = padd;
00130                             std::pair<HWIte,bool > new_pa = m.vert_attr.insert(pa);
00131                             (void)new_pa;
00132                             assert(new_pa.second);
00133                         }
00134                         else
00135                             T::template AddAttrib<0>(m,name,s,data);
00136                     break;
00137                     case 1:
00138                         if(s == sizeof(A)){
00139                             typename MeshType::template PerVertexAttributeHandle<A> h = vcg::tri::Allocator<MeshType>::template AddPerVertexAttribute<A>(m,name);
00140                             for(unsigned int i  = 0; i < m.vert.size(); ++i)
00141                             memcpy((void*) &(h[i]), (void*) &((A*)data)[i],sizeof(A)); // we don't want the type conversion
00142                             }
00143                         else
00144                             if(s < sizeof(A)){
00145                                 // padding
00146                                 int padd = sizeof(A) - s;
00147                                 typename MeshType::template PerFaceAttributeHandle<A> h = vcg::tri::Allocator<MeshType>::template AddPerFaceAttribute<A>(m,name);
00148                                 for(unsigned int i  = 0; i < m.face.size(); ++i){
00149                                     char * dest =  &((char*)(&h[i]))[0];
00150                                     memcpy( (void *)dest , (void*) &((A*)data)[i],s); // we don't want the type conversion
00151                                 }
00152                                 typename MeshType::PointerToAttribute pa;
00153                                 pa._name = std::string(name);
00154                                 HWIte res = m.face_attr.find(pa);
00155                                 pa = *res;
00156                                 m.face_attr.erase(res);
00157                                 pa._padding = padd;
00158                                 std::pair<HWIte,bool > new_pa = m.face_attr.insert(pa);
00159                                 (void)new_pa;
00160                                 assert(new_pa.second);
00161                             }
00162                         else
00163                             T::template AddAttrib<1>(m,name,s,data);
00164                         break;
00165                     case 2:
00166                         if(s == sizeof(A)){
00167                             typename MeshType::template PerMeshAttributeHandle<A> h = vcg::tri::Allocator<MeshType>::template AddPerMeshAttribute<A>(m,name);
00168                                 memcpy((void*)&h(), (void*)((A*)data),sizeof(A)); // we don't want the type conversion
00169                             }
00170                         else
00171                             if(s < sizeof(A)){
00172                                 // padding
00173                                 int padd = sizeof(A) - s;
00174                                 typename MeshType::template PerMeshAttributeHandle<A> h = vcg::tri::Allocator<MeshType>::template AddPerMeshAttribute<A>(m,name);
00175                                 char * dest =  & ((char*)(&h()))[0];
00176                                 memcpy( (void *)dest , (void*)((A*)data),s); // we don't want the type conversion
00177 
00178                                 typename MeshType::PointerToAttribute pa;
00179                                 pa._name = std::string(name);
00180                                 HWIte res = m.mesh_attr.find(pa);
00181                                 pa = *res;
00182                                 m.mesh_attr.erase(res);
00183                                 pa._padding = padd;
00184                                 std::pair<HWIte,bool > new_pa = m.mesh_attr.insert(pa);
00185                                 (void)new_pa;
00186                                 assert(new_pa.second);
00187                             }
00188                         else
00189                             T::template AddAttrib<2>(m,name,s,data);
00190                         break;
00191                     default: assert(0);break;
00192             }
00193         }
00194     };
00195 
00199     template <class MeshType>   struct K        {
00200         template <int VoF>
00201         static void AddAttrib(MeshType &/*m*/, const char * /*name*/, unsigned int /*s*/, void * /*data*/){
00202             // if yohu got this your attribute is larger than   1048576. Honestly...
00203             assert(0);
00204         }
00205     };
00206 
00207     template <class MeshType, class B0 >                                                                                                                                                                                                                                struct K0       : public DerK<  MeshType, B0,   K<MeshType> > {};
00208     template <class MeshType, class B0, class B1 >                                                                                                                                                                                                              struct K1       : public DerK<  MeshType, B1,   K0<MeshType, B0> > {};
00209     template <class MeshType, class B0, class B1, class B2 >                                                                                                                                                                                    struct K2       : public DerK<  MeshType, B2,   K1<MeshType, B0, B1> > {};
00210     template <class MeshType, class B0, class B1, class B2,class B3>                                                                                                                                                                    struct K3       : public DerK<  MeshType, B3,   K2<MeshType, B0, B1, B2> > {};
00211     template <class MeshType, class B0, class B1, class B2,class B3,class B4>                                                                                                                                                   struct K4       : public DerK<  MeshType, B4,   K3<MeshType, B0, B1, B2, B3> > {};
00212     template <class MeshType, class B0, class B1, class B2,class B3,class B4,class B5>                                                                                                                                  struct K5       : public DerK<  MeshType, B5,   K4<MeshType, B0, B1, B2, B3, B4> > {};
00213     template <class MeshType, class B0, class B1, class B2,class B3,class B4,class B5,class B6>                                                                                                                 struct K6       : public DerK<  MeshType, B6,   K5<MeshType, B0, B1, B2, B3, B4, B5> > {};
00214     template <class MeshType, class B0, class B1, class B2,class B3,class B4,class B5,class B6,class B7>                                                                                                struct K7       : public DerK<  MeshType, B7,   K6<MeshType, B0, B1, B2, B3, B4, B5, B6> > {};
00215     template <class MeshType, class B0, class B1, class B2,class B3,class B4,class B5,class B6,class B7,class B8>                                                                               struct K8       : public DerK<  MeshType, B8,   K7<MeshType, B0, B1, B2, B3, B4, B5, B6, B7> > {};
00216     template <class MeshType, class B0, class B1, class B2,class B3,class B4,class B5,class B6,class B7,class B8,class B9>                                                              struct K9       : public DerK<  MeshType, B9,   K8<MeshType, B0, B1, B2, B3, B4, B5, B6, B7, B8> > {};
00217     template <class MeshType, class B0, class B1, class B2,class B3,class B4,class B5,class B6,class B7,class B8,class B9,class B10>                                    struct K10      : public DerK<  MeshType, B10,  K9<MeshType, B0, B1, B2, B3, B4, B5, B6, B7, B8, B9> > {};
00218     template <class MeshType, class B0, class B1, class B2,class B3,class B4,class B5,class B6,class B7,class B8,class B9,class B10,class B11>                  struct K11      : public DerK<  MeshType, B11,  K10<MeshType, B0, B1, B2, B3, B4, B5, B6, B7, B8, B9, B11 > > {};
00219     template <class MeshType, class B0, class B1, class B2,class B3,class B4,class B5,class B6,class B7,class B8,class B9,class B10,class B11,class B12>struct K12      : public DerK<  MeshType, B12,  K11<MeshType, B0, B1, B2, B3, B4, B5, B6, B7, B8, B9, B11, B12 > > {};
00220 
00221     template <class MeshType, class A0,
00222         class B0  = DummyType<1048576>,
00223         class B1  = DummyType<2048>,
00224         class B2  = DummyType<1024>,
00225         class B3  = DummyType<512>,
00226         class B4  = DummyType<256>,
00227         class B5  = DummyType<128>,
00228         class B6  = DummyType<64>,
00229         class B7  = DummyType<32>,
00230         class B8  = DummyType<16>,
00231         class B9  = DummyType<8>,
00232         class B10 = DummyType<4>,
00233         class B11 = DummyType<2>,
00234         class B12 = DummyType<1>
00235     >   struct C0               : public DerK<  MeshType, A0,    K12<MeshType, B0, B1, B2, B3, B4,B5,B6,B7,B8,B9,B10,B11,B12> > {};
00236 
00237     template <class MeshType, class A0, class A1>                                                                                       struct C1               : public Der<  MeshType, A1,    C0<MeshType, A0> > {};
00238     template <class MeshType, class A0, class A1, class A2>                                                                     struct C2               : public Der<  MeshType, A2,    C1<MeshType, A0, A1> > {};
00239     template <class MeshType, class A0, class A1, class A2,class A3>                                            struct C3               : public Der<  MeshType, A3,    C2<MeshType, A0, A1, A2> > {};
00240     template <class MeshType, class A0, class A1, class A2,class A3,class A4>                           struct AttrAll  : public Der<  MeshType, A4,    C3<MeshType, A0, A1, A2, A3> > {};
00241 
00242 
00243 
00244     template <class OpenMeshType,class A0 = long, class A1 = double, class A2 = int,class A3 = short, class A4 = char >
00245     class ImporterVMI: public AttrAll<OpenMeshType,A0,A1,A2,A3,A4>
00246     {
00247 
00248         static void ReadString(std::string & out){
00249             unsigned int l; Read(&l,4,1);
00250             char * buf = new char[l+1];
00251             Read(buf,1,l);buf[l]='\0';
00252             out = std::string(buf);
00253             delete [] buf;
00254             }
00255 
00256         static void ReadInt( unsigned int & i){ Read(&i,1,4);}
00257         static void ReadFloat( float & v){ Read(&v,1,sizeof(float));}
00258 
00259 
00260          static int   LoadVertexOcfMask( ){
00261             int mask  =0;
00262             std::string s;
00263 
00264             // vertex quality
00265             ReadString( s);
00266             if( s == std::string("HAS_VERTEX_QUALITY_OCF")) mask |= Mask::IOM_VERTQUALITY;
00267 
00268             // vertex color
00269             ReadString( s);
00270             if( s == std::string("HAS_VERTEX_COLOR_OCF"))  mask |= Mask::IOM_VERTCOLOR;
00271 
00272             // vertex normal
00273             ReadString( s);
00274             if( s == std::string("HAS_VERTEX_NORMAL_OCF")) mask |= Mask::IOM_VERTNORMAL;
00275 
00276             // vertex mark
00277             ReadString( s);
00278             //if( s == std::string("HAS_VERTEX_MARK_OCF"))  mask |=
00279 
00280             // vertex texcoord
00281             ReadString( s);
00282             if( s == std::string("HAS_VERTEX_TEXCOORD_OCF"))  mask |= Mask::IOM_VERTTEXCOORD;
00283 
00284             // vertex-face adjacency
00285             ReadString( s);
00286             //if( s == std::string("HAS_VERTEX_VFADJACENCY_OCF")) mask |=
00287 
00288             // vertex curvature
00289             ReadString( s);
00290             //if( s == std::string("HAS_VERTEX_CURVATURE_OCF"))  mask |=
00291 
00293             ReadString( s);
00294             //if( s == std::string("HAS_VERTEX_CURVATUREDIR_OCF"))  mask |=
00295 
00296             // vertex radius
00297             ReadString( s);
00298             if( s == std::string("HAS_VERTEX_RADIUS_OCF"))  mask |= Mask::IOM_VERTRADIUS;
00299 
00300             return mask;
00301        }
00302 
00303         template <typename MeshType, typename CONT>
00304                 struct LoadVertexOcf{
00305                         LoadVertexOcf(FILE* /*f*/,const CONT & /*vert*/){
00306                                 // do nothing, it is a std::vector
00307                         }
00308                 };
00309 
00310 
00311 
00312         template <typename MeshType>
00313         struct
00314         LoadVertexOcf<MeshType,vertex::vector_ocf<typename OpenMeshType::VertexType> >{
00315                         typedef typename OpenMeshType::VertexType VertexType;
00316             LoadVertexOcf( FILE * /*f*/, vertex::vector_ocf<typename OpenMeshType::VertexType> & vert){
00317             std::string s;
00318 
00319                 // vertex quality
00320                 ReadString( s);
00321                 if( s == std::string("HAS_VERTEX_QUALITY_OCF")) {
00322                         vert.EnableQuality();
00323                         Read((void*)&vert.QV[0],sizeof(typename VertexType::QualityType),vert.size() );
00324                 }
00325 
00326                 // vertex color
00327                 ReadString( s);
00328                 if( s == std::string("HAS_VERTEX_COLOR_OCF")) {
00329                         vert.EnableColor();
00330                         Read((void*)&vert.CV[0],sizeof(typename VertexType::ColorType),vert.size() );
00331                 }
00332 
00333                 // vertex normal
00334                 ReadString( s);
00335                 if( s == std::string("HAS_VERTEX_NORMAL_OCF")) {
00336                         vert.EnableNormal();
00337                         Read((void*)&vert.NV[0],sizeof(typename VertexType::NormalType),vert.size() );
00338                 }
00339 
00340                  // vertex mark
00341                 ReadString( s);
00342                 if( s == std::string("HAS_VERTEX_MARK_OCF")) {
00343                         vert.EnableMark();
00344                         Read((void*)&vert.MV[0],sizeof(typename VertexType::MarkType),vert.size() );
00345                 }
00346 
00347                 // vertex texcoord
00348                 ReadString( s);
00349                 if( s == std::string("HAS_VERTEX_TEXCOORD_OCF")) {
00350                         vert.EnableTexCoord();
00351                         Read((void*)&vert.TV[0],sizeof(typename VertexType::TexCoordType),vert.size() );
00352                 }
00353 
00354                 // vertex-face adjacency
00355                 ReadString( s);
00356                 if( s == std::string("HAS_VERTEX_VFADJACENCY_OCF")) {
00357                         vert.EnableVFAdjacency();
00358                         Read((void*)&vert.AV[0],sizeof(typename vertex::vector_ocf<VertexType>::VFAdjType),vert.size() );
00359                 }
00360 
00361                 // vertex curvature
00362                 ReadString( s);
00363                 if( s == std::string("HAS_VERTEX_CURVATURE_OCF")) {
00364                         vert.EnableCurvature();
00365                         Read((void*)&vert.CuV[0],sizeof(typename VertexType::CurvatureType),vert.size() );
00366                 }
00367 
00368                 // vertex curvature dir
00369                 ReadString( s);
00370                 if( s == std::string("HAS_VERTEX_CURVATUREDIR_OCF")) {
00371                         vert.EnableCurvatureDir();
00372                         Read((void*)&vert.CuDV[0],sizeof(typename VertexType::CurvatureDirType),vert.size() );
00373                 }
00374 
00375                 // vertex radius
00376                 ReadString( s);
00377                 if( s == std::string("HAS_VERTEX_RADIUS_OCF")) {
00378                         vert.EnableRadius();
00379                         Read((void*)&vert.RadiusV[0],sizeof(typename VertexType::RadiusType),vert.size() );
00380                 }
00381 
00382                         }
00383                 };
00384 
00385                 template <typename MeshType, typename CONT>
00386                 struct LoadFaceOcf{
00387                         LoadFaceOcf(const CONT & /* face */){
00388                                 // do nothing, it is a std::vector
00389                         }
00390                 };
00391 
00392 
00393                  static int  LoadFaceOcfMask( ){
00394                 int mask=0;
00395                     std::string s;
00396 
00397                 // face quality
00398                     ReadString( s);
00399                     if( s == std::string("HAS_FACE_QUALITY_OCF"))       mask    |=  Mask::IOM_FACEQUALITY;
00400 
00401                     // face color
00402                     ReadString( s);
00403                          if( s == std::string("HAS_FACE_COLOR_OCF"))            mask    |=   Mask::IOM_FACECOLOR;
00404 
00405                     // face normal
00406                     ReadString( s);
00407                     if( s == std::string("HAS_FACE_NORMAL_OCF"))  mask  |=  Mask::IOM_FACENORMAL;
00408 
00410                     ReadString( s);
00411                     //if( s == std::string("HAS_FACE_MARK_OCF")) mask   |=
00412 
00413                     // face wedgetexcoord
00414                     ReadString( s);
00415                     if( s == std::string("HAS_FACE_WEDGETEXCOORD_OCF")) mask    |= Mask::IOM_WEDGTEXCOORD;
00416 
00417 
00418                     // face-face adjacency
00419                     ReadString( s);
00420 //                    if( s == std::string("HAS_FACE_FFADJACENCY_OCF")) mask    |= */
00421 
00422                     // vertex-face adjacency
00423                     ReadString( s);
00424                     //if( s == std::string("HAS_FACE_VFADJACENCY_OCF")) mask    |=
00425 
00426                     // face WedgeColor
00427                     ReadString( s);
00428                     if( s == std::string("HAS_FACE_WEDGECOLOR_OCF")) mask       |=  Mask::IOM_WEDGCOLOR;
00429 
00430                     // face WedgeNormal
00431                     ReadString( s);
00432                     if( s == std::string("HAS_FACE_WEDGENORMAL_OCF")) mask      |=  Mask::IOM_WEDGNORMAL;
00433                     return mask;
00434             }
00435 
00436 
00437                 /* partial specialization for vector_ocf */
00438                 template <typename MeshType>
00439                                                                 struct LoadFaceOcf< MeshType, face::vector_ocf<typename OpenMeshType::FaceType> >{
00440                                                 typedef typename OpenMeshType::FaceType FaceType;
00441                         LoadFaceOcf( face::vector_ocf<FaceType> & face){
00442                                 std::string s;
00443 
00444                                 // face quality
00445                                 ReadString( s);
00446                                 if( s == std::string("HAS_FACE_QUALITY_OCF")) {
00447                                         face.EnableQuality();
00448                                         Read((void*)&face.QV[0],sizeof(typename FaceType::QualityType),face.size() );
00449                                 }
00450 
00451                                 // face color
00452                                 ReadString( s);
00453                                 if( s == std::string("HAS_FACE_COLOR_OCF")) {
00454                                         face.EnableColor();
00455                                         Read((void*)&face.CV[0],sizeof(typename FaceType::ColorType),face.size() );
00456                                 }
00457 
00458                                 // face normal
00459                                 ReadString( s);
00460                                 if( s == std::string("HAS_FACE_NORMAL_OCF")) {
00461                                         face.EnableNormal();
00462                                         Read((void*)&face.NV[0],sizeof(typename FaceType::NormalType),face.size() );
00463                                 }
00464 
00465                                 // face mark
00466                                 ReadString( s);
00467                                 if( s == std::string("HAS_FACE_MARK_OCF")) {
00468                                         face.EnableMark();
00469                                         Read((void*)&face.MV[0],sizeof(typename FaceType::MarkType),face.size() );
00470                                 }
00471 
00472                                 // face wedgetexcoord
00473                                 ReadString( s);
00474                                 if( s == std::string("HAS_FACE_WEDGETEXCOORD_OCF")) {
00475                                         face.EnableWedgeTexCoord();
00476                                         Read((void*)&face.WTV[0],sizeof(typename FaceType::WedgeTexCoordType),face.size() );
00477                                 }
00478 
00479 
00480                                 // face-face adjacency
00481                                 ReadString( s);
00482                                 if( s == std::string("HAS_FACE_FFADJACENCY_OCF")) {
00483                                         face.EnableFFAdjacency();
00484                                         Read((void*)&face.AF[0],sizeof(typename  face::vector_ocf<FaceType>::AdjTypePack),face.size() );
00485                                 }
00486 
00487                                 // vertex-face adjacency
00488                                 ReadString( s);
00489                                 if( s == std::string("HAS_FACE_VFADJACENCY_OCF")) {
00490                                         face.EnableVFAdjacency();
00491                                         Read((void*)&face.AV[0],sizeof(typename  face::vector_ocf<FaceType>::AdjTypePack),face.size() );
00492                                 }
00493 
00494                                 // face WedgeColor
00495                                 ReadString( s);
00496                                 if( s == std::string("HAS_FACE_WEDGECOLOR_OCF")) {
00497                                         face.EnableWedgeColor();
00498                                         Read((void*)&face.WCV[0],sizeof(typename  face::vector_ocf<FaceType>::WedgeColorTypePack),face.size() );
00499                                 }
00500 
00501                                 // face WedgeNormal
00502                                 ReadString( s);
00503                                 if( s == std::string("HAS_FACE_WEDGENORMAL_OCF")) {
00504                                         face.EnableWedgeNormal();
00505                                         Read((void*)&face.WNV[0],sizeof(typename  face::vector_ocf<FaceType>::WedgeNormalTypePack),face.size() );
00506                                 }
00507                         }
00508                 };
00509 
00510     static int  FaceMaskBitFromString(std::string s){
00511         if( s.find("Color",0) != std::string::npos )                    return Mask::IOM_FACECOLOR;                     else
00512         if( s.find("BitFlags",0) != std::string::npos )                 return Mask::IOM_FACEFLAGS;                     else
00513         if( s.find("VertexRef",0) != std::string::npos )                return Mask::IOM_FACEINDEX;                     else
00514         if( s.find("Normal",0) != std::string::npos )                   return Mask::IOM_FACENORMAL;            else
00515         if( s.find("Quality",0) != std::string::npos )                  return Mask::IOM_FACEQUALITY;           else
00516         if( s.find("Quality",0) != std::string::npos )                  return Mask::IOM_FACEQUALITY;           else
00517         if( s.find("WedgeColor",0) != std::string::npos )               return Mask::IOM_WEDGCOLOR;                     else
00518         if( s.find("WedgeNormal",0) != std::string::npos )              return Mask::IOM_WEDGNORMAL;            else
00519         if( s.find("WedgeTexCoord",0) != std::string::npos)             return Mask::IOM_WEDGTEXCOORD;          else
00520         return 0;
00521     }
00522     static int  VertexMaskBitFromString(std::string s){
00523         if( s.find("Color",0) != std::string::npos )            return Mask::IOM_VERTCOLOR;             else
00524         if( s.find("Coord",0) != std::string::npos )            return Mask::IOM_VERTCOORD;             else
00525         if( s.find("BitFlags",0) != std::string::npos )         return Mask::IOM_VERTFLAGS;             else
00526         if( s.find("Quality",0) != std::string::npos )          return Mask::IOM_VERTQUALITY;   else
00527         if( s.find("Normal",0) != std::string::npos )           return Mask::IOM_VERTNORMAL;    else
00528         if( s.find("TexCoord",0) != std::string::npos )         return Mask::IOM_VERTTEXCOORD;  else
00529         if( s.find("Radius",0) != std::string::npos )           return Mask::IOM_VERTRADIUS;    else
00530             return 0;
00531     }
00532 
00533 
00534         static FILE *& F(){static FILE * f; return f;}
00535 
00536 
00537         static void * Malloc(unsigned int n){ return (n)?malloc(n):0;}
00538         static void Free(void * ptr){ if(ptr) free (ptr);}
00539 
00540 
00541         typedef typename OpenMeshType::FaceType FaceType;
00542         typedef typename OpenMeshType::FaceContainer FaceContainer;
00543         typedef typename OpenMeshType::FaceIterator FaceIterator;
00544 
00545         typedef typename OpenMeshType::VertContainer VertContainer;
00546         typedef typename OpenMeshType::VertexIterator VertexIterator;
00547         typedef typename OpenMeshType::VertexType VertexType;
00548 
00549     public:
00550        enum VMIErrorCodes {
00551                 VMI_NO_ERROR = 0,
00552                 VMI_INCOMPATIBLE_VERTEX_TYPE,
00553                 VMI_INCOMPATIBLE_FACE_TYPE,
00554                 VMI_FAILED_OPEN
00555        };
00556 
00562         static const char* ErrorMsg(int message_code)
00563         {
00564             static const char* error_msg[] =
00565             {
00566                 "No errors",
00567                 "The file has a incompatible vertex signature",
00568                 "The file has a incompatible Face signature",
00569                 "General failure of the file opening"
00570             };
00571 
00572             if(message_code>4 || message_code<0)
00573                 return "Unknown error";
00574             else
00575                 return error_msg[message_code];
00576         };
00577 
00578         /* Read the info about the mesh. Note: in the header the bounding box is always written/readed
00579            as a vcg::Box3f, even if the scalar type is not float. The bounding box of the mesh will
00580            be set properly on loading.
00581           */
00582         static bool GetHeader(  std::vector<std::string>& fnameV,
00583                                 std::vector<std::string>& fnameF,
00584                                 unsigned int & vertSize,
00585                                 unsigned int &faceSize,
00586                                 vcg::Box3f & bbox,
00587                                 int & mask){
00588             std::string name;
00589             unsigned int nameFsize,nameVsize,i;
00590 
00591             ReadString( name); ReadInt( nameFsize);
00592 
00593             for(i=0; i < nameFsize; ++i)
00594                 {ReadString(  name);fnameF.push_back( name );mask |= FaceMaskBitFromString(name);}
00595             mask |= LoadFaceOcfMask();
00596 
00597             ReadString( name); ReadInt( faceSize);
00598             ReadString(  name); ReadInt( nameVsize);
00599 
00600             for(i=0; i < nameVsize; ++i)
00601                 {ReadString(  name) ;fnameV.push_back( name);mask |= VertexMaskBitFromString(name);}
00602             mask |= LoadVertexOcfMask();
00603 
00604             ReadString( name);
00605             ReadInt( vertSize);
00606 
00607             ReadString( name);
00608             float float_value;
00609             for(unsigned int i =0; i < 2; ++i){ReadFloat( float_value); bbox.min[i]=float_value;}
00610             for(unsigned int i =0; i < 2; ++i){ReadFloat( float_value); bbox.max[i]=float_value;}
00611 
00612             ReadString( name);
00613             assert(strstr( name.c_str(),"end_header")!=NULL);
00614             return true;
00615         }
00616 
00617 
00618         static bool GetHeader(const char * filename,std::vector<std::string>& nameV, std::vector<std::string>& nameF, unsigned int & vertSize, unsigned int &faceSize,vcg::Box3f & bbox,int & mask){
00619                 F() = fopen(filename,"rb");
00620                 bool res =  GetHeader(nameV, nameF, vertSize, faceSize,bbox,mask);
00621                 fclose(F());
00622                 return res;
00623     }
00624 
00625     public:
00626         static const char * & In_mem(){static const char *    in_mem; return in_mem;}
00627         static unsigned int & In_mode(){static unsigned int  in_mode = 0; return in_mode;}
00628 
00629 
00630         static unsigned int & pos(){static unsigned int  p = 0; return p;}
00631         static int Read_sim(const void * , size_t size, size_t count ){ pos() += size * count;return size * count; }
00632         static int Read_mem( void *dst , size_t size, size_t count ){ memcpy(dst,&In_mem()[pos()],size*count); pos() += size * count;return size * count; }
00633 
00634 
00635         static int Read( void * dst,  size_t size, size_t count){
00636             switch(In_mode()){
00637             case 0: return Read_mem(dst, size,count );  break;
00638             case 1: return fread(dst, size,count, F() ); break;
00639              }
00640             assert(0);
00641             return 0;
00642         }
00643 
00644 
00645         static bool LoadMask(const char * f, int & mask){
00646             std::vector<std::string>  nameV;
00647             std::vector<std::string>  nameF;
00648             unsigned int   vertSize, faceSize;
00649             vcg::Box3f bbox;
00650             F() = fopen(f,"rb");
00651             In_mode() = 1;
00652             GetHeader(nameV,nameF,vertSize, faceSize, bbox, mask);
00653             return true;
00654         }
00655 
00656         static bool LoadMaskFromMem(  const char * ptr, int & mask){
00657             std::vector<std::string>  nameV;
00658             std::vector<std::string>  nameF;
00659             unsigned int   vertSize, faceSize;
00660             vcg::Box3f bbox;
00661             In_mode() = 0;
00662             pos() = 0;
00663             In_mem() = ptr;
00664             GetHeader(nameV,nameF,vertSize, faceSize, bbox, mask);
00665             return true;
00666         }
00667 
00668         static int Open(OpenMeshType &m, const char * filename, int & mask,CallBackPos  * /*cb*/ = 0 )       {
00669             In_mode() = 1;
00670             F() = fopen(filename,"rb");
00671             if(!F()) return VMI_FAILED_OPEN;
00672             if(F()==NULL)       return 1; // 1 is the error code for cant'open, see the ErrorMsg function
00673             int res = Deserialize(m,mask);
00674             fclose(F());
00675             return  res;
00676         }
00677         static int ReadFromMem(  OpenMeshType &m, int & mask,char * ptr){
00678             In_mode() = 0;
00679             pos() = 0;
00680             In_mem() = ptr;
00681             return Deserialize(m,mask);
00682         }
00683 
00684         static int Deserialize(OpenMeshType &m, int & mask)
00685         {
00686             typedef typename OpenMeshType::VertexType VertexType;
00687             typedef typename OpenMeshType::FaceType FaceType;
00688             typename OpenMeshType::FaceIterator fi;
00689             typename OpenMeshType::VertexIterator vi;
00690 
00691             std::vector<std::string> nameF,nameV,fnameF,fnameV;
00692             unsigned int vertSize,faceSize;
00693 
00694             /* read the header */
00695       vcg::Box3f lbbox;
00696       GetHeader(fnameV, fnameF, vertSize, faceSize,lbbox,mask);
00697       m.bbox.Import(lbbox);
00698             /* read the mesh type */
00699             OpenMeshType::FaceType::Name(nameF);
00700             OpenMeshType::VertexType::Name(nameV);
00701 
00702             /* check if the type is the very same, otherwise return */
00703             if(fnameV != nameV) return VMI_INCOMPATIBLE_VERTEX_TYPE;
00704             if(fnameF != nameF) return VMI_INCOMPATIBLE_FACE_TYPE;
00705 
00706              void *  offsetV = 0,*offsetF = 0;
00707 
00708              if(vertSize!=0)
00709                 /* read the address of the first vertex */
00710                 Read(&offsetV,sizeof( void *),1 );
00711 
00712              if(faceSize!=0)
00713                 /* read the address of the first face */
00714                 Read(&offsetF,sizeof( void *),1 );
00715 
00716             /* read the object mesh */
00717             Read(&m.shot,sizeof(Shot<typename OpenMeshType::ScalarType>),1 );
00718             Read(&m.vn,sizeof(int),1 );
00719             Read(&m.fn,sizeof(int),1 );
00720             Read(&m.imark,sizeof(int),1 );
00721             Read(&m.bbox,sizeof(Box3<typename OpenMeshType::ScalarType>),1 );
00722             Read(&m.C(),sizeof(Color4b),1 );
00723 
00724 
00725             /* resize the vector of vertices */
00726             m.vert.resize(vertSize);
00727 
00728 
00729             size_t read = 0;
00730             /* load the vertices */
00731             if(vertSize>0){
00732                 read=Read((void*)& m.vert[0],sizeof(VertexType),vertSize );
00733                  LoadVertexOcf<OpenMeshType,VertContainer>(F(),m.vert);
00734             }
00735 
00736             read = 0;
00737             m.face.resize(faceSize);
00738             if(faceSize>0){
00739                 /* load the faces */
00740                 read = Read((void*)& m.face[0],sizeof(FaceType),faceSize );
00741                 LoadFaceOcf<OpenMeshType,FaceContainer>(m.face);
00742             }
00743 
00744 
00745             /* load the per vertex attributes */
00746             std::string _string,_trash;
00747             unsigned int n,sz;
00748 
00749             ReadString( _trash); ReadInt( n);
00750 
00751             for(size_t ia = 0 ; ia < n; ++ia){
00752                 ReadString(_trash); ReadString(_string);
00753                 ReadString(_trash); ReadInt(sz);
00754 
00755                 void * data = Malloc(sz*m.vert.size());
00756                 Read(data,sz,m.vert.size());
00757                 AttrAll<OpenMeshType,A0,A1,A2,A3,A4>::template AddAttrib<0>(m,_string.c_str(),sz,data);
00758                 Free(data);
00759             }
00760 
00761             /* load the per face attributes */
00762             ReadString(_trash); ReadInt( n);
00763             for(size_t ia = 0 ; ia < n; ++ia){
00764                 ReadString(_trash); ReadString( _string);
00765                 ReadString(_trash); ReadInt( sz);
00766                 void * data = Malloc(sz*m.face.size());
00767                 Read(data,sz,m.face.size() );
00768                 AttrAll<OpenMeshType,A0,A1,A2,A3,A4>::template AddAttrib<1>(m,_string.c_str(),sz,data);
00769                 Free(data);
00770             }
00771 
00772             /* load the per mesh attributes */
00773             ReadString( _trash); ReadInt( n);
00774             for(unsigned int ia = 0 ; ia < n; ++ia){
00775                 ReadString( _trash); ReadString( _string);
00776                 ReadString( _trash); ReadInt( sz);
00777                 void * data = Malloc(sz);
00778                 Read(data,1,sz );
00779                 AttrAll<OpenMeshType,A0,A1,A2,A3,A4>::template AddAttrib<2>(m,_string.c_str(),sz,data);
00780                 Free(data);
00781             }
00782 
00783             if(!m.face.empty()){
00784             if(FaceVectorHasVFAdjacency(m.face))
00785                 for(vi = m.vert.begin(); vi != m.vert.end(); ++vi){
00786                     (*vi).VFp() = (*vi).VFp()-(FaceType*)offsetF+ &m.face[0];
00787                     (*vi).VFp() = (*vi).VFp()-(FaceType*)offsetF+ &m.face[0];
00788                     (*vi).VFp() = (*vi).VFp()-(FaceType*)offsetF+ &m.face[0];
00789                 }
00790 
00791             if(FaceVectorHasFVAdjacency(m.face))
00792                 for(fi = m.face.begin(); fi != m.face.end(); ++fi){
00793                     (*fi).V(0) = (*fi).V(0)-(VertexType*)offsetV+ &m.vert[0];
00794                     (*fi).V(1) = (*fi).V(1)-(VertexType*)offsetV+ &m.vert[0];
00795                     (*fi).V(2) = (*fi).V(2)-(VertexType*)offsetV+ &m.vert[0];
00796                 }
00797 
00798             if(FaceVectorHasFFAdjacency(m.face))
00799                 for(fi = m.face.begin(); fi != m.face.end(); ++fi){
00800                     (*fi).FFp(0) = (*fi).FFp(0)-(FaceType*)offsetF+ &m.face[0];
00801                     (*fi).FFp(1) = (*fi).FFp(1)-(FaceType*)offsetF+ &m.face[0];
00802                     (*fi).FFp(2) = (*fi).FFp(2)-(FaceType*)offsetF+ &m.face[0];
00803                 }
00804 
00805             }
00806 
00807             return VMI_NO_ERROR; // zero is the standard (!) code of success
00808         }
00809 
00810     }; // end class
00811 
00812 
00813 } // end Namespace tri
00814 } // end Namespace io
00815 } // end Namespace vcg
00816 
00817 #endif


shape_reconstruction
Author(s): Roberto Martín-Martín
autogenerated on Sat Jun 8 2019 18:32:02