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
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
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
00057 E_NOERROR,
00058
00059 E_CANTOPEN,
00060 E_UNESPECTEDEOF,
00061 E_ABORTED,
00062
00063 E_NO_VERTEX,
00064 E_NO_FACE,
00065 E_LESS_THAN_3VERTINFACE,
00066 E_BAD_VERT_INDEX,
00067 E_BAD_TEX_VERT_INDEX
00068 };
00069
00070 static const char* ErrorMsg(int error)
00071 {
00072 static const char* _3ds_error_msg[] =
00073 {
00074 "No errors",
00075 "Can't open file",
00076 "Premature End of file",
00077 "File opening aborted",
00078 "No vertex field found",
00079 "No face field found",
00080 "Face with less than 3 vertices",
00081 "Bad vertex index in face",
00082 "Bad texture index in face"
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 *=0)
00091 {
00092 CTMcontext context;
00093
00094
00095 context = ctmNewContext(CTM_IMPORT);
00096
00097 ctmLoad(context, filename);
00098 if(ctmGetError(context) == CTM_NONE)
00099 {
00100
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
00106
00107
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
00144 ctmFreeContext(context);
00145 }
00146
00147 int result = E_NOERROR;
00148 return result;
00149 }
00150
00151
00152 };
00153 }
00154 }
00155 }
00156
00157 #endif // ndef __VCGLIB_IMPORT_3DS