File: import.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 (216 lines) | stat: -rw-r--r-- 6,579 bytes parent folder | download | duplicates (2)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/****************************************************************************
* VCGLib                                                            o o     *
* Visual and Computer Graphics Library                            o     o   *
*                                                                _   O  _   *
* Copyright(C) 2004-2016                                           \/)\/    *
* 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.                                                         *
*                                                                           *
****************************************************************************/

/****************************************************************************
  History

$Log: not supported by cvs2svn $
Revision 1.11  2006/03/29 09:27:07  cignoni
Added managemnt of non critical errors

Revision 1.10  2006/03/29 08:16:31  corsini
Minor change in LoadMask

Revision 1.9  2006/03/27 07:17:49  cignoni
Added generic LoadMask

Revision 1.8  2006/03/07 13:19:29  cignoni
First Release with OBJ import support

Revision 1.7  2006/02/28 14:50:00  corsini
Fix comments

Revision 1.6  2006/02/10 16:14:53  corsini
Fix typo

Revision 1.5  2006/02/10 08:14:32  cignoni
Refactored import. No more duplicated code

Revision 1.4  2006/02/09 16:04:45  corsini
Expose load mask

Revision 1.3  2006/01/11 10:37:45  cignoni
Added use of Callback

Revision 1.2  2005/01/26 22:43:19  cignoni
Add std:: to stl containers

Revision 1.1  2004/11/29 08:12:10  cignoni
Initial Update


****************************************************************************/

#ifndef __VCGLIB_IMPORT
#define __VCGLIB_IMPORT

#include <wrap/io_trimesh/import_obj.h>
#include <wrap/io_trimesh/import_ply.h>
#include <wrap/io_trimesh/import_stl.h>
#include <wrap/io_trimesh/import_off.h>
#include <wrap/io_trimesh/import_vmi.h>

#include <locale>

namespace vcg {
namespace tri {
namespace io {

/**
This class encapsulate a filter for automatically importing meshes by guessing
the right filter according to the extension
*/

template <class OpenMeshType>
class Importer
{
private:
  enum KnownTypes { KT_UNKNOWN, KT_PLY, KT_STL, KT_OFF, KT_OBJ, KT_VMI };
static int &LastType()
{
  static int lastType= KT_UNKNOWN;
return lastType;
}

public:
enum ImporterError {
  E_NOERROR =0 // No error =0 is the standard for ALL the imported files.
};
// simple aux function that returns true if a given file has a given extesnion
static bool FileExtension(std::string filename, std::string extension)
{
  std::transform(filename.begin(), filename.end(), filename.begin(), ::tolower);
  std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
  std::string end=filename.substr(filename.length()-extension.length(),extension.length());
  return end==extension;
}
// Open Mesh, returns 0 on success.
static int Open(OpenMeshType &m, const char *filename, CallBackPos *cb=0)
{
  int dummymask = 0;
  return Open(m,filename,dummymask,cb);
}

/// Open Mesh and fills the load mask (the load mask must be initialized first); returns 0 on success.
static int Open(OpenMeshType &m, const char *filename, int &loadmask, CallBackPos *cb=0)
{
	int err;
	if(FileExtension(filename,"ply"))
	{
		err = ImporterPLY<OpenMeshType>::Open(m, filename, loadmask, cb);
		LastType()=KT_PLY;
	}
	else if(FileExtension(filename,"stl"))
	{
		err = ImporterSTL<OpenMeshType>::Open(m, filename, loadmask, cb);
		LastType()=KT_STL;
	}
	else if(FileExtension(filename,"off"))
	{
		err = ImporterOFF<OpenMeshType>::Open(m, filename, loadmask, cb);
		LastType()=KT_OFF;
	}
	else if(FileExtension(filename,"obj"))
	{
		err = ImporterOBJ<OpenMeshType>::Open(m, filename, loadmask, cb);
		LastType()=KT_OBJ;
	}
    else if(FileExtension(filename,"vmi"))
    {
        err = ImporterVMI<OpenMeshType>::Open(m, filename, loadmask, cb);
        LastType()=KT_VMI;
    }
  else {
		err=1;
		LastType()=KT_UNKNOWN;
	}

	return err;
}

static bool ErrorCritical(int error)
{
  switch(LastType())
  {
    case KT_PLY : return (error>0); break;
    case KT_STL : return (error>0); break;
    case KT_OFF : return (error>0); break;
    case KT_OBJ : return ImporterOBJ<OpenMeshType>::ErrorCritical(error); break;
  }

  return true;
}

static const char *ErrorMsg(int error)
{
  switch(LastType())
  {
    case KT_PLY : return ImporterPLY<OpenMeshType>::ErrorMsg(error); break;
    case KT_STL : return ImporterSTL<OpenMeshType>::ErrorMsg(error); break;
    case KT_OFF : return ImporterOFF<OpenMeshType>::ErrorMsg(error); break;
    case KT_OBJ : return ImporterOBJ<OpenMeshType>::ErrorMsg(error); break;
    case KT_VMI : return ImporterVMI<OpenMeshType>::ErrorMsg(error); break;
  }
  return "Unknown type";
}

static bool LoadMask(const char * filename, int &mask)
{
	bool err;

	if(FileExtension(filename,"ply"))
	{
		err = ImporterPLY<OpenMeshType>::LoadMask(filename, mask);
		LastType()=KT_PLY;
	}
	else if(FileExtension(filename,"stl"))
	{
		mask = Mask::IOM_VERTCOORD | Mask::IOM_FACEINDEX;
		err = true;
		LastType()=KT_STL;
	}
	else if(FileExtension(filename,"off"))
	{
		mask = Mask::IOM_VERTCOORD | Mask::IOM_FACEINDEX;
		err = ImporterOFF<OpenMeshType>::LoadMask(filename, mask);
		LastType()=KT_OFF;
	}
	else if(FileExtension(filename,"obj"))
	{
		err = ImporterOBJ<OpenMeshType>::LoadMask(filename, mask);
		LastType()=KT_OBJ;
	}
	else
	{
		err = false;
		LastType()=KT_UNKNOWN;
	}

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

#endif