File: import_ctm.h

package info (click to toggle)
meshlab 2020.09%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 45,132 kB
  • sloc: cpp: 400,238; ansic: 31,952; javascript: 1,578; sh: 387; yacc: 238; lex: 139; python: 86; makefile: 30
file content (157 lines) | stat: -rw-r--r-- 5,534 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/****************************************************************************
* VCGLib                                                            o o     *
* Visual and Computer Graphics Library                            o     o   *
*                                                                _   O  _   *
* Copyright(C) 2011                                                \/)\/    *
* Visual Computing Lab                                            /\/|      *
* ISTI - Italian National Research Council                           |      *
*                                                                    \      *
* All rights reserved.                                                      *
*                                                                           *
* This program is free software; you can redistribute it and/or modify      *   
* it under the terms of the GNU General Public License as published by      *
* the Free Software Foundation; either version 2 of the License, or         *
* (at your option) any later version.                                       *
*                                                                           *
* This program is distributed in the hope that it will be useful,           *
* but WITHOUT ANY WARRANTY; without even the implied warranty of            *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
* for more details.                                                         *
*                                                                           *
****************************************************************************/

#ifndef __VCGLIB_IMPORT_CTM
#define __VCGLIB_IMPORT_CTM
#include <openctm.h>
#include <wrap/callback.h>
#include <wrap/io_trimesh/io_mask.h>

// lib3ds headers



namespace vcg {
namespace tri {
namespace io {

/** 
This class encapsulate a filter for importing 3ds meshes.
It uses the lib3ds library.
*/
template <class OpenMeshType>
class ImporterCTM
{
public:

typedef typename OpenMeshType::CoordType CoordType;
typedef typename OpenMeshType::VertexPointer VertexPointer;
typedef typename OpenMeshType::ScalarType ScalarType;
typedef typename OpenMeshType::VertexType VertexType;
typedef typename OpenMeshType::FaceType FaceType;
typedef typename OpenMeshType::VertexIterator VertexIterator;
typedef typename OpenMeshType::FaceIterator FaceIterator;

enum CTMError {
    // Successfull opening
  E_NOERROR,								// 0
    // Opening Errors
  E_CANTOPEN,								// 1
  E_UNESPECTEDEOF,					// 2
  E_ABORTED,								// 3

  E_NO_VERTEX,							// 4
  E_NO_FACE,								// 5
  E_LESS_THAN_3VERTINFACE,	// 6
  E_BAD_VERT_INDEX,					// 7
  E_BAD_TEX_VERT_INDEX 			// 8
};

static const char* ErrorMsg(int error)
{
  static const char* _3ds_error_msg[] =
  {
    "No errors",												// 0
    "Can't open file",									// 1
    "Premature End of file",						// 2
    "File opening aborted",							// 3
    "No vertex field found",						// 4
    "No face field found",							// 5
    "Face with less than 3 vertices",		// 6
    "Bad vertex index in face",					// 7
    "Bad texture index in face"					// 8
  };

  if(error>8 || error<0) return "Unknown error";
  else return _3ds_error_msg[error];
};


static int Open( OpenMeshType &m, const char * filename, int &loadmask, CallBackPos * /*cb*/=0)
{
    CTMcontext context;

    // Create a new importer context
    context = ctmNewContext(CTM_IMPORT);
    // Load the OpenCTM file
    ctmLoad(context, filename);
    if(ctmGetError(context) == CTM_NONE)
    {
    // Access the mesh data
    CTMuint vertCount = ctmGetInteger(context, CTM_VERTEX_COUNT);
    const CTMfloat *vertices = ctmGetFloatArray(context, CTM_VERTICES);
    CTMuint triCount = ctmGetInteger(context, CTM_TRIANGLE_COUNT);
    const CTMuint *indices = ctmGetIntegerArray(context, CTM_INDICES);
    // Deal with the mesh (e.g. transcode it to our // internal representation) // ...

    // Extract colors
    m.Clear();
    Allocator<OpenMeshType>::AddVertices(m, vertCount);
    for(unsigned int i=0;i<vertCount;++i)
        m.vert[i].P()=CoordType(vertices[i*3+0],vertices[i*3+1],vertices[i*3+2]);

    CTMenum colorAttrib = ctmGetNamedAttribMap(context,"Color");
    if(colorAttrib != CTM_NONE)
    {
      const CTMfloat *colors = ctmGetFloatArray(context,colorAttrib);
      for(unsigned int i=0;i<vertCount;++i)
          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);
      loadmask |= Mask::IOM_VERTCOLOR;
    }

    CTMenum qualityAttrib = ctmGetNamedAttribMap(context,"Quality");
    if(qualityAttrib != CTM_NONE)
    {
      const CTMfloat *qualities = ctmGetFloatArray(context,colorAttrib);
      for(unsigned int i=0;i<vertCount;++i)
          m.vert[i].Q()=qualities[i*4+0];
      loadmask |= Mask::IOM_VERTQUALITY;
    }

    if(triCount==1)
    {
      if(indices[0]==0 && indices[1]==0 && indices[2]==0)
        triCount=0;
    }
    Allocator<OpenMeshType>::AddFaces(m, triCount);
    for(unsigned int i=0;i<triCount;++i)
    {
        m.face[i].V(0)=&m.vert[indices[i*3+0]];
        m.face[i].V(1)=&m.vert[indices[i*3+1]];
        m.face[i].V(2)=&m.vert[indices[i*3+2]];
    }
    // Free the context
    ctmFreeContext(context);
    }

    int result = E_NOERROR;
    return result;
} // end of Open


}; // end class
} // end Namespace tri
} // end Namespace io
} // end Namespace vcg

#endif  // ndef __VCGLIB_IMPORT_3DS