export_stl.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.8  2006/09/18 08:55:33  cignoni
00029 Corrected return value of save function (zero is no error)
00030 
00031 Revision 1.7  2006/01/30 13:43:59  cignoni
00032 Added GetExportMaskCapability
00033 
00034 Revision 1.6  2006/01/13 15:47:43  cignoni
00035 Uniformed return type to the style of Open. Now every export function returns 0 in case of success.
00036 
00037 Revision 1.5  2005/12/01 00:58:56  cignoni
00038 Added and removed typenames for gcc compiling...
00039 
00040 Revision 1.4  2004/10/28 00:52:45  cignoni
00041 Better Doxygen documentation
00042 
00043 Revision 1.3  2004/03/09 21:26:47  cignoni
00044 cr lf mismatch
00045 
00046 Revision 1.2  2004/03/03 15:35:53  cignoni
00047 Yet another cr lf mismatch
00048 
00049 Revision 1.3  2004/02/19 15:28:01  ponchio
00050 *** empty log message ***
00051 
00052 Revision 1.2  2004/02/13 02:18:57  cignoni
00053 Edited Comments and GPL license
00054 
00055 
00056 ****************************************************************************/
00057 
00058 #ifndef __VCGLIB_EXPORT_STL
00059 #define __VCGLIB_EXPORT_STL
00060 
00061 #include <stdio.h>
00062 
00063 namespace vcg {
00064 namespace tri {
00065 namespace io {
00066 
00071 template <class SaveMeshType>
00072 class ExporterSTL
00073 {
00074 public:
00075 typedef typename SaveMeshType::FaceType FaceType;
00076 typedef unsigned short CallBackSTLFaceAttribute(const SaveMeshType &m, const FaceType &f);
00077 
00078 static int Save(SaveMeshType &m, const char * filename, const int &mask, CallBackPos *)
00079 {
00080  return Save(m,filename,true,mask);
00081 }
00082 
00083 static int Save(SaveMeshType &m, const char * filename , bool binary =true, int mask=0, const char *objectname=0, bool magicsMode=0)
00084 {
00085   typedef typename SaveMeshType::FaceIterator FaceIterator;
00086     FILE *fp;
00087 
00088     fp = fopen(filename,"wb");
00089     if(fp==0)
00090         return 1;
00091 
00092     if(binary)
00093     {
00094         // Write Header
00095         char header[128]="VCG                                                                                                  ";
00096         if(objectname)  strncpy(header,objectname,80);
00097         if(magicsMode)
00098         {
00099           strncpy(header,"COLOR=XXX MATERIAL=AAA BBB CCC                                                                       ",80);
00100           for(int i=0;i<3;++i)
00101           {
00102             header[0x06+i]=0x7f;
00103             header[0x13+i]=0x7f;
00104             header[0x17+i]=0x7f;
00105             header[0x1b+i]=0x7f;
00106           }
00107         }
00108         fwrite(header,80,1,fp);
00109         // write number of facets
00110         fwrite(&m.fn,1,sizeof(int),fp);
00111         Point3f p;
00112         unsigned short attributes=0;
00113         for(FaceIterator fi=m.face.begin(); fi!=m.face.end(); ++fi) if( !(*fi).IsD() )
00114         {
00115             // For each triangle write the normal, the three coords and a short set to zero
00116             p.Import(vcg::TriangleNormal(*fi).Normalize());
00117             fwrite(p.V(),3,sizeof(float),fp);
00118 
00119             for(int k=0;k<3;++k){
00120                 p.Import((*fi).V(k)->P());
00121                 fwrite(p.V(),3,sizeof(float),fp);
00122             }
00123             if ((mask & Mask::IOM_FACECOLOR) && tri::HasPerFaceColor(m))
00124             {
00125               if(magicsMode) attributes = 32768 | vcg::Color4b::ToUnsignedR5G5B5(fi->C());
00126                         else attributes = 32768 | vcg::Color4b::ToUnsignedB5G5R5(fi->C());
00127             }
00128             fwrite(&attributes,1,sizeof(short),fp);
00129         }
00130     }
00131     else
00132     {
00133         if(objectname) fprintf(fp,"solid %s\n",objectname);
00134         else fprintf(fp,"solid vcg\n");
00135 
00136         Point3f p;
00137         FaceIterator fi;
00138         for(fi=m.face.begin(); fi!=m.face.end(); ++fi) if( !(*fi).IsD() )
00139         {
00140         // For each triangle write the normal, the three coords and a short set to zero
00141             p.Import(TriangleNormal(*fi).Normalize());
00142             fprintf(fp,"  facet normal %13e %13e %13e\n",p[0],p[1],p[2]);
00143             fprintf(fp,"    outer loop\n");
00144             for(int k=0;k<3;++k){
00145                 p.Import((*fi).V(k)->P());
00146                 fprintf(fp,"      vertex  %13e %13e %13e\n",p[0],p[1],p[2]);
00147             }
00148             fprintf(fp,"    endloop\n");
00149             fprintf(fp,"  endfacet\n");
00150         }
00151         fprintf(fp,"endsolid vcg\n");
00152     }
00153     fclose(fp);
00154     return 0;
00155 }
00156 static const char *ErrorMsg(int error)
00157 {
00158   static std::vector<std::string> stl_error_msg;
00159   if(stl_error_msg.empty())
00160   {
00161     stl_error_msg.resize(2 );
00162     stl_error_msg[0]="No errors";
00163       stl_error_msg[1]="Can't open file";
00164     }
00165 
00166   if(error>1 || error<0) return "Unknown error";
00167   else return stl_error_msg[error].c_str();
00168 };
00169 
00170 /*
00171     returns mask of capability one define with what are the saveable information of the format.
00172 */
00173 static int GetExportMaskCapability()
00174 {
00175     int capability = 0;
00176     capability |= vcg::tri::io::Mask::IOM_VERTCOORD;
00177     capability |= vcg::tri::io::Mask::IOM_FACEINDEX;
00178     capability |= vcg::tri::io::Mask::IOM_FACECOLOR;
00179     return capability;
00180 }
00181 
00182 
00183 }; // end class
00184 
00185 } // end Namespace tri
00186 } // end Namespace io
00187 } // end Namespace vcg
00188 
00189 
00190 
00191 #endif


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