export_ctm.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) 2011                                                \/)\/    *
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 #ifndef EXPORT_CTM_H
00024 #define EXPORT_CTM_H
00025 
00026 namespace vcg {
00027     namespace tri {
00028         namespace io {
00029             template <class SaveMeshType>
00030             class ExporterCTM
00031             {
00032 
00033             public:
00034                 typedef typename SaveMeshType::VertexPointer VertexPointer;
00035                 typedef typename SaveMeshType::FaceIterator FaceIterator;
00036 
00037                 static int Save(SaveMeshType &m, const char * filename, int mask=0, bool lossLessFlag=false, float relativePrecision=0.0001)
00038                 {
00039                     tri::Allocator<SaveMeshType>::CompactVertexVector(m);
00040                     tri::Allocator<SaveMeshType>::CompactFaceVector(m);
00041                     CTMuint aVertCount=m.vn;
00042                     CTMuint aTriCount=m.fn;
00043                     std::vector<CTMfloat> aVertices(aVertCount*3);
00044                     std::vector<CTMfloat> aColors(aVertCount*4);
00045                     std::vector<CTMfloat> aQuality(aVertCount*4);
00046                     std::vector<CTMuint> aIndices(aTriCount*3);
00047                     int err;
00048                     CTMcontext context;
00049                     // Create a new exporter context
00050                     context = ctmNewContext(CTM_EXPORT);
00051                     if(lossLessFlag) ctmCompressionMethod(context, CTM_METHOD_MG1);
00052                     else {
00053                       ctmCompressionMethod(context, CTM_METHOD_MG2);
00054                       ctmVertexPrecision(context, relativePrecision);
00055                     }
00056                     // Prepare vertex and faces
00057                     for(unsigned int i=0;i<aVertCount;++i)
00058                     {
00059                         aVertices[i*3+0]=m.vert[i].P()[0];
00060                         aVertices[i*3+1]=m.vert[i].P()[1];
00061                         aVertices[i*3+2]=m.vert[i].P()[2];
00062                     }
00063                     for(unsigned int i=0;i<aTriCount;++i)
00064                     {
00065                         aIndices[i*3+0]=m.face[i].V(0)-&*m.vert.begin();
00066                         aIndices[i*3+1]=m.face[i].V(1)-&*m.vert.begin();
00067                         aIndices[i*3+2]=m.face[i].V(2)-&*m.vert.begin();
00068                     }
00069                     if(aTriCount==0)
00070                     {
00071                       aIndices.resize(3,0);
00072                       aTriCount=1;
00073                     }
00074 
00075                     // Define our mesh representation to OpenCTM
00076                     ctmDefineMesh(context, &*aVertices.begin(), aVertCount, &*aIndices.begin(), aTriCount, NULL);
00077                     err=ctmGetError(context);
00078                     if(err) return err;
00079                     if( tri::HasPerVertexColor(m)   && (mask & io::Mask::IOM_VERTCOLOR))
00080                     {
00081                         aColors.resize(aVertCount*4);
00082                         for(unsigned int i=0;i<aVertCount;++i)
00083                         {
00084                             aColors[i*4+0]=(float)(m.vert[i].C()[0])/255.0f;
00085                             aColors[i*4+1]=(float)(m.vert[i].C()[1])/255.0f;
00086                             aColors[i*4+2]=(float)(m.vert[i].C()[2])/255.0f;
00087                             aColors[i*4+3]=(float)(m.vert[i].C()[3])/255.0f;
00088                         }
00089                         ctmAddAttribMap(context,&aColors[0], "Color");
00090                     }
00091                     if( tri::HasPerVertexQuality(m)   && (mask & io::Mask::IOM_VERTQUALITY))
00092                     {
00093                         aQuality.resize(aVertCount*4,0);
00094                         for(unsigned int i=0;i<aVertCount;++i)
00095                         {
00096                             aQuality[i*4+0]=m.vert[i].Q();
00097                         }
00098                         ctmAddAttribMap(context,&aQuality[0], "Quality");
00099                     }
00100 
00101                     // Save the OpenCTM file
00102                     ctmSave(context, filename);
00103                     err=ctmGetError(context);
00104                     if(err) return err;
00105                     // Free the context
00106                     ctmFreeContext(context);
00107                     return err;
00108                 }
00109                 /*
00110             returns mask of capability one define with what are the saveable information of the format.
00111         */
00112                 static int GetExportMaskCapability()
00113                 {
00114                     int capability = 0;
00115 
00116                     //vert
00117                     capability |= vcg::tri::io::Mask::IOM_VERTCOORD;
00118                     capability |= vcg::tri::io::Mask::IOM_VERTQUALITY;
00119                     capability |= vcg::tri::io::Mask::IOM_VERTCOLOR;
00120                     return capability;
00121                 }
00122 
00123                 static const char *ErrorMsg(int error)
00124                 {
00125                     if(error==0) return "Ok, no errors";
00126                     else return ctmErrorString((CTMenum)error);
00127                 }
00128             };
00129         } // end namespace io
00130     } // end namespace tri
00131 } // end namespace vcg
00132 #endif // EXPORT_CTM_H


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