import_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 
00024 #ifndef __VCGLIB_IMPORT_CTM
00025 #define __VCGLIB_IMPORT_CTM
00026 #include <openctm.h>
00027 #include <wrap/callback.h>
00028 #include <wrap/io_trimesh/io_mask.h>
00029 
00030 // lib3ds headers
00031 
00032 
00033 
00034 namespace vcg {
00035 namespace tri {
00036 namespace io {
00037 
00042 template <class OpenMeshType>
00043 class ImporterCTM
00044 {
00045 public:
00046 
00047 typedef typename OpenMeshType::CoordType CoordType;
00048 typedef typename OpenMeshType::VertexPointer VertexPointer;
00049 typedef typename OpenMeshType::ScalarType ScalarType;
00050 typedef typename OpenMeshType::VertexType VertexType;
00051 typedef typename OpenMeshType::FaceType FaceType;
00052 typedef typename OpenMeshType::VertexIterator VertexIterator;
00053 typedef typename OpenMeshType::FaceIterator FaceIterator;
00054 
00055 enum CTMError {
00056     // Successfull opening
00057   E_NOERROR,                                                            // 0
00058     // Opening Errors
00059   E_CANTOPEN,                                                           // 1
00060   E_UNESPECTEDEOF,                                      // 2
00061   E_ABORTED,                                                            // 3
00062 
00063   E_NO_VERTEX,                                                  // 4
00064   E_NO_FACE,                                                            // 5
00065   E_LESS_THAN_3VERTINFACE,      // 6
00066   E_BAD_VERT_INDEX,                                     // 7
00067   E_BAD_TEX_VERT_INDEX                  // 8
00068 };
00069 
00070 static const char* ErrorMsg(int error)
00071 {
00072   static const char* _3ds_error_msg[] =
00073   {
00074     "No errors",                                                                                                // 0
00075     "Can't open file",                                                                  // 1
00076     "Premature End of file",                                            // 2
00077     "File opening aborted",                                                     // 3
00078     "No vertex field found",                                            // 4
00079     "No face field found",                                                      // 5
00080     "Face with less than 3 vertices",           // 6
00081     "Bad vertex index in face",                                 // 7
00082     "Bad texture index in face"                                 // 8
00083   };
00084 
00085   if(error>8 || error<0) return "Unknown error";
00086   else return _3ds_error_msg[error];
00087 };
00088 
00089 
00090 static int Open( OpenMeshType &m, const char * filename, int &loadmask, CallBackPos */*cb*/=0)
00091 {
00092     CTMcontext context;
00093 
00094     // Create a new importer context
00095     context = ctmNewContext(CTM_IMPORT);
00096     // Load the OpenCTM file
00097     ctmLoad(context, filename);
00098     if(ctmGetError(context) == CTM_NONE)
00099     {
00100     // Access the mesh data
00101     CTMuint vertCount = ctmGetInteger(context, CTM_VERTEX_COUNT);
00102     const CTMfloat *vertices = ctmGetFloatArray(context, CTM_VERTICES);
00103     CTMuint triCount = ctmGetInteger(context, CTM_TRIANGLE_COUNT);
00104     const CTMuint *indices = ctmGetIntegerArray(context, CTM_INDICES);
00105     // Deal with the mesh (e.g. transcode it to our // internal representation) // ...
00106 
00107     // Extract colors
00108     m.Clear();
00109     Allocator<OpenMeshType>::AddVertices(m, vertCount);
00110     for(unsigned int i=0;i<vertCount;++i)
00111         m.vert[i].P()=CoordType(vertices[i*3+0],vertices[i*3+1],vertices[i*3+2]);
00112 
00113     CTMenum colorAttrib = ctmGetNamedAttribMap(context,"Color");
00114     if(colorAttrib != CTM_NONE)
00115     {
00116       const CTMfloat *colors = ctmGetFloatArray(context,colorAttrib);
00117       for(unsigned int i=0;i<vertCount;++i)
00118           m.vert[i].C()=Color4b(colors[i*4+0]*255,colors[i*4+1]*255,colors[i*4+2]*255,colors[i*4+3]*255);
00119       loadmask |= Mask::IOM_VERTCOLOR;
00120     }
00121 
00122     CTMenum qualityAttrib = ctmGetNamedAttribMap(context,"Quality");
00123     if(qualityAttrib != CTM_NONE)
00124     {
00125       const CTMfloat *qualities = ctmGetFloatArray(context,colorAttrib);
00126       for(unsigned int i=0;i<vertCount;++i)
00127           m.vert[i].Q()=qualities[i*4+0];
00128       loadmask |= Mask::IOM_VERTQUALITY;
00129     }
00130 
00131     if(triCount==1)
00132     {
00133       if(indices[0]==0 && indices[1]==0 && indices[2]==0)
00134         triCount=0;
00135     }
00136     Allocator<OpenMeshType>::AddFaces(m, triCount);
00137     for(unsigned int i=0;i<triCount;++i)
00138     {
00139         m.face[i].V(0)=&m.vert[indices[i*3+0]];
00140         m.face[i].V(1)=&m.vert[indices[i*3+1]];
00141         m.face[i].V(2)=&m.vert[indices[i*3+2]];
00142     }
00143     // Free the context
00144     ctmFreeContext(context);
00145     }
00146 
00147     int result = E_NOERROR;
00148     return result;
00149 } // end of Open
00150 
00151 
00152 }; // end class
00153 } // end Namespace tri
00154 } // end Namespace io
00155 } // end Namespace vcg
00156 
00157 #endif  // ndef __VCGLIB_IMPORT_3DS


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