| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#ifndef __VCGLIB_IMPORT_CTM |
| 25 |
#define __VCGLIB_IMPORT_CTM |
| 26 |
#include <openctm.h> |
| 27 |
#include <wrap/callback.h> |
| 28 |
#include <wrap/io_trimesh/io_mask.h> |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
namespace vcg { |
| 35 |
namespace tri { |
| 36 |
namespace io { |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
template <class OpenMeshType> |
| 43 |
class ImporterCTM |
| 44 |
{ |
| 45 |
public: |
| 46 |
|
| 47 |
typedef typename OpenMeshType::CoordType CoordType; |
| 48 |
typedef typename OpenMeshType::VertexPointer VertexPointer; |
| 49 |
typedef typename OpenMeshType::ScalarType ScalarType; |
| 50 |
typedef typename OpenMeshType::VertexType VertexType; |
| 51 |
typedef typename OpenMeshType::FaceType FaceType; |
| 52 |
typedef typename OpenMeshType::VertexIterator VertexIterator; |
| 53 |
typedef typename OpenMeshType::FaceIterator FaceIterator; |
| 54 |
|
| 55 |
enum CTMError { |
| 56 |
|
| 57 |
E_NOERROR, |
| 58 |
|
| 59 |
E_CANTOPEN, |
| 60 |
E_UNESPECTEDEOF, |
| 61 |
E_ABORTED, |
| 62 |
|
| 63 |
E_NO_VERTEX, |
| 64 |
E_NO_FACE, |
| 65 |
E_LESS_THAN_3VERTINFACE, |
| 66 |
E_BAD_VERT_INDEX, |
| 67 |
E_BAD_TEX_VERT_INDEX |
| 68 |
}; |
| 69 |
|
| 70 |
static const char* ErrorMsg(int error) |
| 71 |
{ |
| 72 |
static const char* _3ds_error_msg[] = |
| 73 |
{ |
| 74 |
"No errors", |
| 75 |
"Can't open file", |
| 76 |
"Premature End of file", |
| 77 |
"File opening aborted", |
| 78 |
"No vertex field found", |
| 79 |
"No face field found", |
| 80 |
"Face with less than 3 vertices", |
| 81 |
"Bad vertex index in face", |
| 82 |
"Bad texture index in face" |
| 83 |
}; |
| 84 |
|
| 85 |
if(error>8 || error<0) return "Unknown error"; |
| 86 |
else return _3ds_error_msg[error]; |
| 87 |
}; |
| 88 |
|
| 89 |
|
| 90 |
static int Open( OpenMeshType &m, const char * filename, int &loadmask, CallBackPos * =0) |
| 91 |
{ |
| 92 |
CTMcontext context; |
| 93 |
|
| 94 |
|
| 95 |
context = ctmNewContext(CTM_IMPORT); |
| 96 |
|
| 97 |
ctmLoad(context, filename); |
| 98 |
if(ctmGetError(context) == CTM_NONE) |
| 99 |
{ |
| 100 |
|
| 101 |
CTMuint vertCount = ctmGetInteger(context, CTM_VERTEX_COUNT); |
| 102 |
const CTMfloat *vertices = ctmGetFloatArray(context, CTM_VERTICES); |
| 103 |
CTMuint triCount = ctmGetInteger(context, CTM_TRIANGLE_COUNT); |
| 104 |
const CTMuint *indices = ctmGetIntegerArray(context, CTM_INDICES); |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
m.Clear(); |
| 109 |
Allocator<OpenMeshType>::AddVertices(m, vertCount); |
| 110 |
for(unsigned int i=0;i<vertCount;++i) |
| 111 |
m.vert[i].P()=CoordType(vertices[i*3+0],vertices[i*3+1],vertices[i*3+2]); |
| 112 |
|
| 113 |
CTMenum colorAttrib = ctmGetNamedAttribMap(context,"Color"); |
| 114 |
if(colorAttrib != CTM_NONE) |
| 115 |
{ |
| 116 |
const CTMfloat *colors = ctmGetFloatArray(context,colorAttrib); |
| 117 |
for(unsigned int i=0;i<vertCount;++i) |
| 118 |
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); |
| 119 |
loadmask |= Mask::IOM_VERTCOLOR; |
| 120 |
} |
| 121 |
|
| 122 |
CTMenum qualityAttrib = ctmGetNamedAttribMap(context,"Quality"); |
| 123 |
if(qualityAttrib != CTM_NONE) |
| 124 |
{ |
| 125 |
const CTMfloat *qualities = ctmGetFloatArray(context,colorAttrib); |
| 126 |
for(unsigned int i=0;i<vertCount;++i) |
| 127 |
m.vert[i].Q()=qualities[i*4+0]; |
| 128 |
loadmask |= Mask::IOM_VERTQUALITY; |
| 129 |
} |
| 130 |
|
| 131 |
if(triCount==1) |
| 132 |
{ |
| 133 |
if(indices[0]==0 && indices[1]==0 && indices[2]==0) |
| 134 |
triCount=0; |
| 135 |
} |
| 136 |
Allocator<OpenMeshType>::AddFaces(m, triCount); |
| 137 |
for(unsigned int i=0;i<triCount;++i) |
| 138 |
{ |
| 139 |
m.face[i].V(0)=&m.vert[indices[i*3+0]]; |
| 140 |
m.face[i].V(1)=&m.vert[indices[i*3+1]]; |
| 141 |
m.face[i].V(2)=&m.vert[indices[i*3+2]]; |
| 142 |
} |
| 143 |
|
| 144 |
ctmFreeContext(context); |
| 145 |
} |
| 146 |
|
| 147 |
int result = E_NOERROR; |
| 148 |
return result; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
}; |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
#endif |