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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
/****************************************************************************
* 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. *
* *
****************************************************************************/
/* This code has been adapted from the OFF importer and Gael's GTS loader from Expe */
#ifndef __VCGLIB_IMPORT_GTS
#define __VCGLIB_IMPORT_GTS
#include <fstream>
#include <string>
#include <vector>
#include <assert.h>
#include <vcg/space/color4.h>
#include <wrap/callback.h>
#include <wrap/io_trimesh/io_mask.h>
namespace vcg
{
namespace tri
{
namespace io
{
// /** \addtogroup */
// /* @{ */
/**
This class encapsulate a filter for importing GTS meshes.
The GTS file format of the GNU Triangulated Surface library http://gts.sourceforge.net/
*/
template<class MESH_TYPE>
class ImporterGTS
{
public:
typedef typename MESH_TYPE::VertexType VertexType;
typedef typename MESH_TYPE::VertexIterator VertexIterator;
typedef typename MESH_TYPE::VertexPointer VertexPointer;
typedef typename MESH_TYPE::FaceType FaceType;
typedef typename MESH_TYPE::FaceIterator FaceIterator;
typedef typename MESH_TYPE::FacePointer FacePointer;
typedef typename MESH_TYPE::CoordType CoordType;
typedef typename MESH_TYPE::ScalarType ScalarType;
enum GTSCodes {NoError=0, CantOpen, InvalidFile,
UnsupportedFormat, ErrorNotTriangularFace};
struct Options
{
Options()
: onlyMaskFlag(false), onlyPoints(false), flipFaces(false)
{}
bool onlyMaskFlag;
bool onlyPoints;
bool flipFaces;
};
/*!
* Standard call for knowing the meaning of an error code
* \param message_code The code returned by <CODE>Open</CODE>
* \return The string describing the error code
*/
static const char* ErrorMsg(int message_code)
{
static const char* error_msg[] =
{
"No errors", "Can't open file", "Invalid file",
"Unsupported format", "Face with more than 3 vertices"
};
if(message_code>4 || message_code<0)
return "Unknown error";
else
return error_msg[message_code];
};
/**
* Load only the properties of the 3D objects.
*
* \param filename the name of the file to read from
* \param loadmask the mask which encodes the properties
* \return the operation result
*/
static bool LoadMask(const char *filename, int &loadmask)
{
// To obtain the loading mask all the file must be parsed
// to distinguish between per-vertex and per-face color attribute.
loadmask=0;
MESH_TYPE dummyMesh;
return (Open(dummyMesh, filename, loadmask,0,true)==NoError);
}
static int Open(MESH_TYPE &mesh, const char *filename, CallBackPos *cb=0)
{
int loadmask;
return Open(mesh,filename,loadmask,cb);
}
/*!
* Standard call for reading a mesh.
*
* \param mesh the destination mesh
* \param filename the name of the file to read from
* \return the operation result
*/
static int Open(MESH_TYPE &mesh, const char *filename, int &loadmask,
CallBackPos *cb=0, bool onlyMaskFlag=false)
{
Options opt;
opt.onlyMaskFlag = onlyMaskFlag;
return Open(mesh, filename, loadmask, opt, cb);
}
static int Open(MESH_TYPE &mesh, const char *filename, int &loadmask,
const Options& options, CallBackPos *cb=0)
{
QFile device(filename);
if ( (!device.open(QFile::ReadOnly)) )
return CantOpen;
QTextStream stream(&device);
loadmask = Mask::IOM_VERTCOORD | Mask::IOM_FACEINDEX;
int nofVertices, nofFaces, nofEdges;
int id0, id1, id2;
int eid0, eid1, eid2;
typename MESH_TYPE::CoordType v;
QString line;
QStringList sa;
bool done = false;
do
{
line = stream.readLine();
line = line.trimmed();
sa = line.split(' ');
if ((!line.startsWith("#")) && (sa.size()>=3))
{
nofVertices = sa[0].toInt();
nofEdges = sa[1].toInt();
nofFaces = sa[2].toInt();
done = true;
}
} while (!done);
if(options.onlyMaskFlag) return NoError;
int total = nofVertices + nofEdges + nofFaces;
std::cerr << "GtsMeshReader::nofVertices = " << nofVertices << std::endl;
std::cerr << "GtsMeshReader::nofEdges = " << nofEdges << std::endl;
std::cerr << "GtsMeshReader::nofFaces = " << nofFaces << std::endl;
mesh.Clear();
VertexIterator v_iter = Allocator<MESH_TYPE>::AddVertices(mesh, nofVertices);
for(int i=0 ; i<nofVertices ;)
{
if (cb && (i%1000)==0)
cb(i/total, "Vertex Loading");
line = stream.readLine().trimmed();
if (line.startsWith("#"))
{
continue;
}
sa = line.split(' ');
if (!(sa.size()>=3))
{
std::cerr << "Error parsing vertex " << line.toLocal8Bit().data() << "\n";
return InvalidFile;
}
v.X() = sa[0].toDouble();
v.Y() = sa[1].toDouble();
v.Z() = sa[2].toDouble();
(*v_iter).P() = v;
++v_iter;
i++;
}
// load edges
std::vector< std::pair<int,int> > edges;
for(int i=0 ; i<nofEdges ; ++i)
{
if (cb && (i%1000)==0)
cb((i+nofVertices)/total, "Edge Loading");
line = stream.readLine();
if (line.startsWith("#"))
{
continue;
}
sa = line.split(' ');
if (!(sa.size()>=2))
{
std::cerr << "Error parsing edge " << line.toLocal8Bit().data() << "\n";
return InvalidFile;
}
id0 = sa[0].toInt();
id1 = sa[1].toInt();
edges.push_back(std::pair<int,int>(id0-1,id1-1));
}
// load faces
Allocator<MESH_TYPE>::AddFaces(mesh, nofFaces);
for(int i=0 ; i<nofFaces ; )
{
if (cb && (i%1000)==0)
cb((nofVertices+nofEdges+i)/total, "Face Loading");
line = stream.readLine();
if (line.startsWith("#"))
{
continue;
}
sa = line.split(' ');
if (!(sa.size()>=3))
{
std::cerr << "Error parsing face " << line.toLocal8Bit().data() << "\n";
return InvalidFile;
}
eid0 = sa[0].toInt();
eid1 = sa[1].toInt();
eid2 = sa[2].toInt();
int e11 = edges[eid0-1].first;
int e12 = edges[eid0-1].second;
int e21 = edges[eid1-1].first;
int e22 = edges[eid1-1].second;
if (e12 == e21) {
id0 = e11;
id1 = e12;
id2 = e22;
}
else if (e12 == e22) {
id0 = e11;
id1 = e12;
id2 = e21;
}
else if (e11 == e21) {
id0 = e12;
id1 = e11;
id2 = e22;
}
else {
id0 = e12;
id1 = e11;
id2 = e21;
}
if (options.flipFaces)
{
mesh.face[i].V(0) = &(mesh.vert[id0]);
mesh.face[i].V(1) = &(mesh.vert[id2]);
mesh.face[i].V(2) = &(mesh.vert[id1]);
}
else
{
mesh.face[i].V(0) = &(mesh.vert[id0]);
mesh.face[i].V(1) = &(mesh.vert[id1]);
mesh.face[i].V(2) = &(mesh.vert[id2]);
}
++i;
}
return NoError;
} // end Open
protected:
};
// /*! @} */
} //namespace io
}//namespace tri
} // namespace vcg
#endif //__VCGLIB_IMPORT_GTS
|