export_vrml.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 History
00025 $Log: not supported by cvs2svn $
00026 Revision 1.8  2008/02/21 17:47:29  cignoni
00027 corrected texture saving. Still broken the multitexturing
00028 
00029 Revision 1.7  2008/02/21 17:23:57  cignoni
00030 Corrected various bug, involving spurious commas, and pervertex color saved as per wedge color.
00031 
00032 Revision 1.6  2007/06/12 10:15:35  cignoni
00033 Very important change. No more scaling and translation in the saved file!
00034 
00035 Revision 1.5  2007/03/20 16:47:49  cignoni
00036 Update to the new texture syntax
00037 
00038 Revision 1.4  2006/11/21 19:22:53  e_cerisoli
00039 Added Comments for documentation
00040 
00041 ****************************************************************************/
00042 
00043 #ifndef __VCGLIB_EXPORT_WRL
00044 #define __VCGLIB_EXPORT_WRL
00045 
00046 #include <stdio.h>
00047 #include <wrap/io_trimesh/io_mask.h>
00048 
00049 namespace vcg {
00050         namespace tri {
00051                 namespace io {
00052 
00053                         template <class SaveMeshType>
00057                         class ExporterWRL
00058                         {
00059                         public:
00060                                 typedef typename SaveMeshType::VertexPointer VertexPointer;
00061                                 typedef typename SaveMeshType::ScalarType ScalarType;
00062                                 typedef typename SaveMeshType::VertexType VertexType;
00063                                 typedef typename SaveMeshType::FaceType FaceType;
00064                                 typedef typename SaveMeshType::VertexIterator VertexIterator;
00065                                 typedef typename SaveMeshType::FaceIterator FaceIterator;
00066                                 
00068                                 static int Save(SaveMeshType &m, const char * filename, const int &mask, CallBackPos */*cb=0*/)
00069                                 {                                       
00070                                         FILE *fp;
00071                                         fp = fopen(filename,"wb");
00072                                         if(fp==NULL)
00073                                                 return 1;
00074 
00075                                         // Header                                       
00076                                         fprintf(fp,
00077                                                  "#VRML V2.0 utf8\n"  
00078                                                 "\n"                                            
00079                                                 "# Generated by VCGLIB, (C)Copyright 1999-2001 VCG, IEI-CNR\n"
00080                                                 "\n"                                            
00081                                                 "NavigationInfo {\n"                                            
00082                                                 "       type [ \"EXAMINE\", \"ANY\" ]\n"                                                
00083                                                 "}\n"                                           
00084                                                 );                                      
00085 
00086                                         // Tranche principale
00087                                         //double ss = 8.0/m.bbox.Diag();                                        
00088                                         
00089                                         fprintf(fp,
00090                                         "Transform {\n"
00091                                         "  scale %g %g %g\n"
00092                                         "  translation %g %g %g\n"
00093                                         "  children\n"
00094                                         "  [\n"
00095                                         ,1.0,1.0,1.0,
00096                                          0.0,0.0,0.0);
00097 //                                      ,ss,ss,ss
00098 //                                      ,  -m.bbox.Center()[0]*ss,  -m.bbox.Center()[1]*ss,-3-m.bbox.Center()[2]*ss     );
00099 
00100                                         // Start Shape
00101                                         fprintf(fp,
00102                                         "    Shape\n"
00103                                         "    {\n"
00104                                         "      geometry IndexedFaceSet\n"
00105                                         "      {\n"
00106                                         "        creaseAngle .5\n"
00107                                         "        solid FALSE\n"
00108                                         "        coord Coordinate\n"
00109                                         "        {\n"
00110                                         "          point\n"
00111                                         "          ["
00112                                         );
00113                                         FaceIterator fi;        
00114                                         VertexIterator vi;      
00115                                         std::map<VertexPointer,int> index;
00116                                         int ind;
00117 
00118                                         // Vertici
00119                                         for(ind=0,vi=m.vert.begin(); vi!=m.vert.end(); ++vi, ++ind)     
00120                                         if(!(*vi).IsD())
00121                                                 {
00122                                                         if(vi!=m.vert.begin()) fprintf(fp,", ");
00123                                                         if(ind%4==0) fprintf(fp, "\n            " );
00124                                                         fprintf(fp, "%g %g %g" ,(*vi).P()[0] ,(*vi).P()[1] ,(*vi).P()[2]        );
00125                                                         index[&*vi] = ind;
00126                                                 }
00127                                         fprintf(fp,"\n"
00128                                                                 "          ]\n"
00129                                                                 "        }\n"
00130                                                                 );
00131                                         
00132                                         
00133                                         if( HasPerVertexColor(m) && (mask & vcg::tri::io::Mask::IOM_VERTCOLOR))
00134                                         {
00135                                                 fprintf(fp,
00136                                                         "        color Color\n"
00137                                                         "        {\n"
00138                                                         "          color\n"
00139                                                         "          ["
00140                                                         );
00141                                                 for(ind=0,vi=m.vert.begin();vi!=m.vert.end();++vi,++ind)
00142                                                 if(!(*vi).IsD())
00143                                                 {
00144                                                         if(vi!=m.vert.begin()) fprintf(fp,", ");
00145                                                         float r = float(vi->C()[0])/255;
00146                                                         float g = float(vi->C()[1])/255;
00147                                                         float b = float(vi->C()[2])/255;
00148 
00149                                                         if(ind%4==0)
00150                                                                 fprintf(fp,"\n            ");
00151                                                         fprintf(fp,"%g %g %g",r,g,b);
00152                                                 }
00153                                                 fprintf(fp,     
00154                                                         "\n"
00155                                                         "          ]\n"                                                 
00156                                                         "        }\n"
00157                                                         );
00158                                         }
00159                                         else if( HasPerWedgeColor(m) &&  (mask & vcg::tri::io::Mask::IOM_WEDGCOLOR ))
00160                                         {                                               
00161                                                 fprintf(fp,
00162                                                         "        color Color\n" 
00163                                                         "        {\n"                                                   
00164                                                         "          color\n"                                                     
00165                                                         "          ["                                                   );
00166                                                 for(ind=0,fi=m.face.begin();fi!=m.face.end();++fi,++ind)
00167                                                 if(!(*fi).IsD())
00168                                                 {
00169                                                         if(fi!=m.face.begin()) fprintf(fp,", ");
00170                                                         if(ind%4==0) fprintf(fp,"\n            ");
00171                                                                 for(int z=0;z<3;++z)                                                    
00172                                                                 {                                                               
00173                                                                         if(z!=0) fprintf(fp,", ");
00174                                                                         float r = float(fi->WC(z)[0])/255;
00175                                                                         float g = float(fi->WC(z)[1])/255;
00176                                                                         float b = float(fi->WC(z)[2])/255;
00177                                                                         fprintf(fp,"%g %g %g",r,g,b);
00178                                                                 }
00179                                                 }                                               
00180                                                 fprintf(fp,                                                     
00181                                                         "\n"                                                    
00182                                                         "          ]\n"                                                 
00183                                                         "        }\n"                                           
00184                                                         "        colorIndex\n"                                                  
00185                                                         "        ["                                                     );                                              
00186                                                 int nn = 0;                                             
00187                                                 for(ind=0,fi=m.face.begin(); fi!=m.face.end(); ++fi,++ind)
00188                                                 if(!(*fi).IsD())
00189                                                 {
00190                                                         //if(fi!=m.face.begin()) fprintf(fp,", ");
00191                                                         if(ind%4==0) fprintf(fp,"\n          ");                                                        
00192                                                                         fprintf(fp,"%i",nn++);                                                          
00193                                                                         fprintf(fp,"%i",nn++);                                                          
00194                                                                         fprintf(fp,"%i",nn++);                                                          
00195                                                         fprintf(fp,"-1");                                               
00196                                                 }                                               
00197                                                 fprintf(fp,                                                     
00198                                                         "\n"                                                    
00199                                                         "       ]\n"                                                    
00200                                                         );                                      
00201                                         }
00202                                         // NOTE MULTITEXTURE WRL SAVING DO NOT WORK!
00203                                         else if (HasPerWedgeTexCoord(m) &&(mask &  vcg::tri::io::Mask::IOM_WEDGTEXCOORD))
00204                                         {
00205                                                 fprintf(fp,
00206                                                         "\n"
00207                                                         "        texCoord TextureCoordinate\n"
00208                                                         "        {\n"
00209                                                         "          point\n"
00210                                                         "          [\n"
00211                                                         );
00212                                                 for(ind=0,fi=m.face.begin(); fi!=m.face.end(); ++fi,++ind)
00213                                                 if(!(*fi).IsD())
00214                                                         {
00215                                                                 //if(fi!=m.face.begin()) fprintf(fp,", ");
00216                                                                 if(ind%4==0) fprintf(fp,"\n          ");
00217                                                                 for (int j = 0; j < 3; j++)
00218                                                                                 fprintf(fp,"%g %g ",fi->WT(j).u(),fi->WT(j).v());
00219                                                         }
00220                                                 fprintf(fp,
00221                                                         "\n"
00222                                                         "          ]\n"
00223                                                         "        }\n"
00224                                                         "        texCoordIndex\n"
00225                                                         "        [\n"
00226                                                         );
00227                                                 int nn = 0;
00228                                                 for(ind=0,fi=m.face.begin(); fi!=m.face.end(); ++fi,++ind)
00229                                                         if(!(*fi).IsD())
00230                                                         {
00231                                                                 //if(fi!=m.face.begin()) fprintf(fp,", ");
00232                                                                 if(ind%4==0) fprintf(fp,"\n          ");                                                                
00233                                                                 for (int j = 0; j < 3; j++)
00234                                                                         fprintf(fp,"%d ",nn++);
00235                                                                 fprintf(fp,"-1 ");
00236                                                         }
00237                                                 fprintf(fp,
00238                                                         "\n"
00239                                                         "        ]\n"
00240                                                         );
00241                                         }
00242                                         fprintf(fp,
00243                                                         "        coordIndex\n"
00244                                                         "        ["
00245                                                         );
00246                                         // Facce
00247                                         for(ind=0,fi=m.face.begin(); fi!=m.face.end(); ++fi,++ind)
00248                                         if(!(*fi).IsD())
00249                                         {
00250                                                 if(fi!=m.face.begin()) fprintf(fp,", ");
00251                                                 if(ind%6==0) fprintf(fp,"\n          ");                                                                
00252                                                 for (int j = 0; j < 3; j++)
00253                                                         fprintf(fp,"%i,",index[(*fi).V(j)]);
00254                                                 
00255                                                 fprintf(fp,"-1");       
00256                                         }
00257                                         fprintf(fp,
00258                                                         "\n"
00259                                                         "        ]\n"
00260                                                         "      }\n"
00261                                                         "      appearance Appearance\n"
00262                                                         "      {\n"                                             
00263                                                         "        material Material\n"
00264                                                         "        {\n"
00265                                                         "              ambientIntensity 0.2\n"
00266                                                         "              diffuseColor 0.9 0.9 0.9\n"
00267                                                         "              specularColor .1 .1 .1\n"
00268                                                         "              shininess .5\n"
00269                                                         "        }\n"
00270                                                         );
00271                                         if(m.textures.size())
00272                                         {
00273                                                 fprintf(fp,
00274                                                         "        texture ImageTexture { url \"%s\" }\n"
00275                                                         ,m.textures[0].c_str()
00276                                                         );
00277                                         }
00278                                         fprintf(fp,
00279                                                 "      }\n"
00280                                                 "    }\n"
00281                                                 "  ]\n"
00282                                                 "}\n"
00283                                                 );
00284                                         fclose(fp);
00285                                         return 0;
00286         }
00288         static int GetExportMaskCapability()
00289         {
00290                 int capability = 0;
00291 
00292                 //vert
00293                 capability |= Mask::IOM_VERTCOLOR;
00294                 
00295                 //wedg
00296                 capability |= Mask::IOM_WEDGTEXCOORD;
00297                 capability |= Mask::IOM_WEDGCOLOR;
00298 
00299                 return capability;
00300         }
00301 
00303         static const char *ErrorMsg(int error)
00304         {
00305                 static std::vector<std::string> wrl_error_msg;
00306                 if(wrl_error_msg.empty())
00307                 {
00308                         wrl_error_msg.resize(2 );
00309                         wrl_error_msg[0]="No errors";
00310                         wrl_error_msg[1]="Can't open file";
00311                 }
00312                 if(error>1 || error<0) return "Unknown error";
00313                 else return wrl_error_msg[error].c_str();
00314                 }
00315 
00316 }; // end class
00317 } // end Namespace io
00318 } // end Namespace tri
00319 } // end Namespace vcg
00320 
00321 #endif


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