Debian Codegraph @

src/vcglib/wrap/io_trimesh/import_ctm.h

raw symbols SCIP
1 /****************************************************************************
2 * VCGLib o o *
3 * Visual and Computer Graphics Library o o *
4 * _ O _ *
5 * Copyright(C) 2011 \/)\/ *
6 * Visual Computing Lab /\/| *
7 * ISTI - Italian National Research Council | *
8 * \ *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
20 * for more details. *
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 // lib3ds headers
31
32
33
34 namespace vcg {
35 namespace tri {
36 namespace io {
37
38 /**
39 This class encapsulate a filter for importing 3ds meshes.
40 It uses the lib3ds library.
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 // Successfull opening
57 E_NOERROR, // 0
58 // Opening Errors
59 E_CANTOPEN, // 1
60 E_UNESPECTEDEOF, // 2
61 E_ABORTED, // 3
62
63 E_NO_VERTEX, // 4
64 E_NO_FACE, // 5
65 E_LESS_THAN_3VERTINFACE, // 6
66 E_BAD_VERT_INDEX, // 7
67 E_BAD_TEX_VERT_INDEX // 8
68 };
69
70 static const char* ErrorMsg(int error)
71 {
72 static const char* _3ds_error_msg[] =
73 {
74 "No errors", // 0
75 "Can't open file", // 1
76 "Premature End of file", // 2
77 "File opening aborted", // 3
78 "No vertex field found", // 4
79 "No face field found", // 5
80 "Face with less than 3 vertices", // 6
81 "Bad vertex index in face", // 7
82 "Bad texture index in face" // 8
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 * /*cb*/=0)
91 {
92 CTMcontext context;
93
94 // Create a new importer context
95 context = ctmNewContext(CTM_IMPORT);
96 // Load the OpenCTM file
97 ctmLoad(context, filename);
98 if(ctmGetError(context) == CTM_NONE)
99 {
100 // Access the mesh data
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 // Deal with the mesh (e.g. transcode it to our // internal representation) // ...
106
107 // Extract colors
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 // Free the context
144 ctmFreeContext(context);
145 }
146
147 int result = E_NOERROR;
148 return result;
149 } // end of Open
150
151
152 }; // end class
153 } // end Namespace tri
154 } // end Namespace io
155 } // end Namespace vcg
156
157 #endif // ndef __VCGLIB_IMPORT_3DS