export_gts.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 /* This code has been adapted from the GTS exporter and Gael's GTS exporter from Expe */
00025 
00030 
00031 #ifndef __VCGLIB_EXPORT_GTS
00032 #define __VCGLIB_EXPORT_GTS
00033 
00034 #include <QFile>
00035 #include <QTextStream>
00036 #include <map>
00037 #include <wrap/io_trimesh/io_mask.h>
00038 
00039 
00040 namespace vcg {
00041         namespace tri {
00042                 namespace io {
00043                         template <class SaveMeshType>
00044                         class ExporterGTS
00045                         {
00046 
00047                         public:
00048                                 typedef typename SaveMeshType::VertexPointer VertexPointer;
00049                                 typedef typename SaveMeshType::ScalarType ScalarType;
00050                                 typedef typename SaveMeshType::VertexType VertexType;
00051                                 typedef typename SaveMeshType::FaceType FaceType;
00052                                 typedef typename SaveMeshType::FacePointer FacePointer;
00053                                 typedef typename SaveMeshType::VertexIterator VertexIterator;
00054                                 typedef typename SaveMeshType::FaceIterator FaceIterator;
00055 
00056                                 static int Save(SaveMeshType &m, const char * filename, int mask=0 )
00057                                 {
00058                                         QFile device(filename);
00059                                         if (!device.open(QFile::WriteOnly))
00060                                                 return 1;
00061 
00062                                         QTextStream stream(&device);
00063 
00064                                         // update vertex indices
00065                                         std::vector<int> FlagV;
00066                                         VertexPointer  vp;
00067                                         VertexIterator vi;
00068                                         int j;
00069                                         for(j=0,vi=m.vert.begin(); vi!=m.vert.end(); ++vi)
00070                                         {
00071                                                 vp = &(*vi);
00072                                                 FlagV.push_back(vp->Flags());
00073                                                 if (!vp->IsD())
00074                                                 {
00075                                                         vp->Flags() = j;
00076                                                         j++;
00077                                                 }
00078                                         }
00079                                         assert(j==m.vn);
00080 
00081                                         // build the edge list, and count the number of edges
00082                                         typedef std::pair<int,int> Edge;
00083                                         typedef std::map<Edge,int> Edge2Index;
00084                                         Edge2Index edge2index;
00085 
00086                                         // loop over all edge's faces
00087                                         FacePointer fp;
00088                                         int edgeCount = 0;
00089                                         FaceIterator fi;
00090                                         for (j=0,fi=m.face.begin(); fi!=m.face.end(); ++fi)
00091                                         {
00092                                                 fp = &(*fi);
00093                                                 if (!fp->IsD() )
00094                                                 {
00095                                                         for (int k=0; k<3; ++k)
00096                                                         {
00097                                                                 int a = fp->cV(k)->Flags();
00098                                                                 int b = fp->cV((k+1)%3)->Flags();
00099                                                                 if (a>b)
00100                                                                         std::swap(a,b);
00101                                                                 Edge e(a,b);
00102                                                                 Edge2Index::iterator it = edge2index.find(e);
00103                                                                 if (it==edge2index.end())
00104                                                                 {
00105                                                                         edge2index[e] = edgeCount;
00106                                                                         edgeCount++;
00107                                                                 }
00108                                                         }
00109                                                 }
00110                                         }
00111 
00112                                         stream  << m.vn << " "
00113                                                                         << edgeCount << " "
00114                                                                         << m.fn << " "
00115                                                                         << "GtsSurface GtsFace GtsEdge GtsVertex\n";
00116 
00117                                         // export vertices
00118                                         for (vi=m.vert.begin();vi!=m.vert.end();++vi)
00119                                         {
00120                                                 vp=&(*vi);
00121                                                 if (!vp->IsD())
00122                                                 {
00123                                                         stream << vp->P()[0] << " " << vp->P()[1] << " " << vp->P()[2] << "\n";
00124                                                 }
00125                                         }
00126 
00127                                         // export the edges
00128                                         for (j=0,fi=m.face.begin(); fi!=m.face.end(); ++fi)
00129                                         {
00130                                                 fp = &(*fi);
00131                                                 if (!fp->IsD() )
00132                                                 {
00133                                                         for (int k=0; k<3; ++k)
00134                                                         {
00135                                                                 int a = fp->cV(k)->Flags();
00136                                                                 int b = fp->cV((k+1)%3)->Flags();
00137                                                                 if (a>b)
00138                                                                         std::swap(a,b);
00139                                                                 Edge e(a,b);
00140                                                                 Edge2Index::iterator it = edge2index.find(e);
00141                                                                 if (it!=edge2index.end())
00142                                                                 {
00143                                                                         stream << a+1 << " " << b+1 << "\n";
00144                                                                 }
00145                                                         }
00146                                                 }
00147                                         }
00148 
00149                                         // third pass to export the face to edge connectivity
00150                                         for (j=0,fi=m.face.begin(); fi!=m.face.end(); ++fi)
00151                                         {
00152                                                 fp = &(*fi);
00153                                                 if (!fp->IsD() )
00154                                                 {
00155                                                         for (int k=0; k<3; ++k)
00156                                                         {
00157                                                                 int a = fp->cV(k)->Flags();
00158                                                                 int b = fp->cV((k+1)%3)->Flags();
00159                                                                 if (a>b)
00160                                                                         std::swap(a,b);
00161                                                                 Edge e(a,b);
00162                                                                 Edge2Index::iterator it = edge2index.find(e);
00163                                                                 if (it!=edge2index.end())
00164                                                                         stream << it->second+1 << " ";
00165                                                                 else
00166                                                                         return 2; // internal error
00167                                                         }
00168                                                         stream << "\n";
00169                                                 }
00170                                         }
00171 
00172                                         // Recupera i flag originali
00173                                         for(j=0,vi=m.vert.begin();vi!=m.vert.end();++vi)
00174                                                 (*vi).Flags()=FlagV[j++];
00175 
00176                                         return 0;
00177                                 }
00178 
00179         static const char *ErrorMsg(int error)
00180         {
00181           static std::vector<std::string> off_error_msg;
00182           if(off_error_msg.empty())
00183           {
00184             off_error_msg.resize(2 );
00185             off_error_msg[0]="No errors";
00186                   off_error_msg[1]="Can't open file";
00187                                                 off_error_msg[2]="Internal error";
00188                                         }
00189 
00190           if(error>2 || error<0) return "Unknown error";
00191           else return off_error_msg[error].c_str();
00192         }
00193         /*
00194                 returns mask of capability one define with what are the saveable information of the format.
00195         */
00196         static int GetExportMaskCapability()
00197         {
00198                 int capability = 0;
00199                 capability |= vcg::tri::io::Mask::IOM_VERTCOORD;
00200           capability |= vcg::tri::io::Mask::IOM_FACEINDEX;
00201                 return capability;
00202         }
00203 
00204                         }; // end class
00205                 } // end namespace tri
00206         } // end namespace io
00207 } // end namespace vcg
00209 #endif


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