Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
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
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
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
00102 ctmSave(context, filename);
00103 err=ctmGetError(context);
00104 if(err) return err;
00105
00106 ctmFreeContext(context);
00107 return err;
00108 }
00109
00110
00111
00112 static int GetExportMaskCapability()
00113 {
00114 int capability = 0;
00115
00116
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 }
00130 }
00131 }
00132 #endif // EXPORT_CTM_H