| 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 |
#ifndef __VCGLIB_IMPORT_GTS |
| 27 |
#define __VCGLIB_IMPORT_GTS |
| 28 |
|
| 29 |
#include <fstream> |
| 30 |
#include <string> |
| 31 |
#include <vector> |
| 32 |
#include <assert.h> |
| 33 |
#include <vcg/space/color4.h> |
| 34 |
#include <wrap/callback.h> |
| 35 |
#include <wrap/io_trimesh/io_mask.h> |
| 36 |
|
| 37 |
namespace vcg |
| 38 |
{ |
| 39 |
namespace tri |
| 40 |
{ |
| 41 |
namespace io |
| 42 |
{ |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
template<class MESH_TYPE> |
| 50 |
class ImporterGTS |
| 51 |
{ |
| 52 |
public: |
| 53 |
|
| 54 |
typedef typename MESH_TYPE::VertexType VertexType; |
| 55 |
typedef typename MESH_TYPE::VertexIterator VertexIterator; |
| 56 |
typedef typename MESH_TYPE::VertexPointer VertexPointer; |
| 57 |
typedef typename MESH_TYPE::FaceType FaceType; |
| 58 |
typedef typename MESH_TYPE::FaceIterator FaceIterator; |
| 59 |
typedef typename MESH_TYPE::FacePointer FacePointer; |
| 60 |
typedef typename MESH_TYPE::CoordType CoordType; |
| 61 |
typedef typename MESH_TYPE::ScalarType ScalarType; |
| 62 |
|
| 63 |
enum GTSCodes {NoError=0, CantOpen, InvalidFile, |
| 64 |
UnsupportedFormat, ErrorNotTriangularFace}; |
| 65 |
|
| 66 |
struct Options |
| 67 |
{ |
| 68 |
Options() |
| 69 |
: onlyMaskFlag(false), onlyPoints(false), flipFaces(false) |
| 70 |
{} |
| 71 |
bool onlyMaskFlag; |
| 72 |
bool onlyPoints; |
| 73 |
bool flipFaces; |
| 74 |
}; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
static const char* ErrorMsg(int message_code) |
| 82 |
{ |
| 83 |
static const char* error_msg[] = |
| 84 |
{ |
| 85 |
"No errors", "Can't open file", "Invalid file", |
| 86 |
"Unsupported format", "Face with more than 3 vertices" |
| 87 |
}; |
| 88 |
|
| 89 |
if(message_code>4 || message_code<0) |
| 90 |
return "Unknown error"; |
| 91 |
else |
| 92 |
return error_msg[message_code]; |
| 93 |
}; |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
static bool LoadMask(const char *filename, int &loadmask) |
| 103 |
{ |
| 104 |
|
| 105 |
|
| 106 |
loadmask=0; |
| 107 |
MESH_TYPE dummyMesh; |
| 108 |
return (Open(dummyMesh, filename, loadmask,0,true)==NoError); |
| 109 |
} |
| 110 |
|
| 111 |
static int Open(MESH_TYPE &mesh, const char *filename, CallBackPos *cb=0) |
| 112 |
{ |
| 113 |
int loadmask; |
| 114 |
return Open(mesh,filename,loadmask,cb); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
static int Open(MESH_TYPE &mesh, const char *filename, int &loadmask, |
| 125 |
CallBackPos *cb=0, bool onlyMaskFlag=false) |
| 126 |
{ |
| 127 |
Options opt; |
| 128 |
opt.onlyMaskFlag = onlyMaskFlag; |
| 129 |
return Open(mesh, filename, loadmask, opt, cb); |
| 130 |
} |
| 131 |
|
| 132 |
static int Open(MESH_TYPE &mesh, const char *filename, int &loadmask, |
| 133 |
const Options& options, CallBackPos *cb=0) |
| 134 |
{ |
| 135 |
QFile device(filename); |
| 136 |
if ( (!device.open(QFile::ReadOnly)) ) |
| 137 |
return CantOpen; |
| 138 |
|
| 139 |
QTextStream stream(&device); |
| 140 |
|
| 141 |
loadmask = Mask::IOM_VERTCOORD | Mask::IOM_FACEINDEX; |
| 142 |
|
| 143 |
int nofVertices, nofFaces, nofEdges; |
| 144 |
int id0, id1, id2; |
| 145 |
int eid0, eid1, eid2; |
| 146 |
typename MESH_TYPE::CoordType v; |
| 147 |
|
| 148 |
QString line; |
| 149 |
QStringList sa; |
| 150 |
bool done = false; |
| 151 |
do |
| 152 |
{ |
| 153 |
line = stream.readLine(); |
| 154 |
line = line.trimmed(); |
| 155 |
sa = line.split(' '); |
| 156 |
if ((!line.startsWith("#")) && (sa.size()>=3)) |
| 157 |
{ |
| 158 |
nofVertices = sa[0].toInt(); |
| 159 |
nofEdges = sa[1].toInt(); |
| 160 |
nofFaces = sa[2].toInt(); |
| 161 |
done = true; |
| 162 |
} |
| 163 |
} while (!done); |
| 164 |
|
| 165 |
if(options.onlyMaskFlag) return NoError; |
| 166 |
|
| 167 |
int total = nofVertices + nofEdges + nofFaces; |
| 168 |
|
| 169 |
std::cerr << "GtsMeshReader::nofVertices = " << nofVertices << std::endl; |
| 170 |
std::cerr << "GtsMeshReader::nofEdges = " << nofEdges << std::endl; |
| 171 |
std::cerr << "GtsMeshReader::nofFaces = " << nofFaces << std::endl; |
| 172 |
|
| 173 |
mesh.Clear(); |
| 174 |
VertexIterator v_iter = Allocator<MESH_TYPE>::AddVertices(mesh, nofVertices); |
| 175 |
for(int i=0 ; i<nofVertices ;) |
| 176 |
{ |
| 177 |
if (cb && (i%1000)==0) |
| 178 |
cb(i/total, "Vertex Loading"); |
| 179 |
|
| 180 |
line = stream.readLine().trimmed(); |
| 181 |
|
| 182 |
if (line.startsWith("#")) |
| 183 |
{ |
| 184 |
continue; |
| 185 |
} |
| 186 |
|
| 187 |
sa = line.split(' '); |
| 188 |
if (!(sa.size()>=3)) |
| 189 |
{ |
| 190 |
std::cerr << "Error parsing vertex " << line.toLocal8Bit().data() << "\n"; |
| 191 |
return InvalidFile; |
| 192 |
} |
| 193 |
v.X() = sa[0].toDouble(); |
| 194 |
v.Y() = sa[1].toDouble(); |
| 195 |
v.Z() = sa[2].toDouble(); |
| 196 |
|
| 197 |
(*v_iter).P() = v; |
| 198 |
++v_iter; |
| 199 |
i++; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
std::vector< std::pair<int,int> > edges; |
| 204 |
for(int i=0 ; i<nofEdges ; ++i) |
| 205 |
{ |
| 206 |
if (cb && (i%1000)==0) |
| 207 |
cb((i+nofVertices)/total, "Edge Loading"); |
| 208 |
|
| 209 |
line = stream.readLine(); |
| 210 |
if (line.startsWith("#")) |
| 211 |
{ |
| 212 |
continue; |
| 213 |
} |
| 214 |
|
| 215 |
sa = line.split(' '); |
| 216 |
if (!(sa.size()>=2)) |
| 217 |
{ |
| 218 |
std::cerr << "Error parsing edge " << line.toLocal8Bit().data() << "\n"; |
| 219 |
return InvalidFile; |
| 220 |
} |
| 221 |
id0 = sa[0].toInt(); |
| 222 |
id1 = sa[1].toInt(); |
| 223 |
|
| 224 |
edges.push_back(std::pair<int,int>(id0-1,id1-1)); |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
Allocator<MESH_TYPE>::AddFaces(mesh, nofFaces); |
| 229 |
for(int i=0 ; i<nofFaces ; ) |
| 230 |
{ |
| 231 |
if (cb && (i%1000)==0) |
| 232 |
cb((nofVertices+nofEdges+i)/total, "Face Loading"); |
| 233 |
|
| 234 |
line = stream.readLine(); |
| 235 |
if (line.startsWith("#")) |
| 236 |
{ |
| 237 |
continue; |
| 238 |
} |
| 239 |
|
| 240 |
sa = line.split(' '); |
| 241 |
if (!(sa.size()>=3)) |
| 242 |
{ |
| 243 |
std::cerr << "Error parsing face " << line.toLocal8Bit().data() << "\n"; |
| 244 |
return InvalidFile; |
| 245 |
} |
| 246 |
eid0 = sa[0].toInt(); |
| 247 |
eid1 = sa[1].toInt(); |
| 248 |
eid2 = sa[2].toInt(); |
| 249 |
|
| 250 |
int e11 = edges[eid0-1].first; |
| 251 |
int e12 = edges[eid0-1].second; |
| 252 |
int e21 = edges[eid1-1].first; |
| 253 |
int e22 = edges[eid1-1].second; |
| 254 |
if (e12 == e21) { |
| 255 |
id0 = e11; |
| 256 |
id1 = e12; |
| 257 |
id2 = e22; |
| 258 |
} |
| 259 |
else if (e12 == e22) { |
| 260 |
id0 = e11; |
| 261 |
id1 = e12; |
| 262 |
id2 = e21; |
| 263 |
} |
| 264 |
else if (e11 == e21) { |
| 265 |
id0 = e12; |
| 266 |
id1 = e11; |
| 267 |
id2 = e22; |
| 268 |
} |
| 269 |
else { |
| 270 |
id0 = e12; |
| 271 |
id1 = e11; |
| 272 |
id2 = e21; |
| 273 |
} |
| 274 |
|
| 275 |
if (options.flipFaces) |
| 276 |
{ |
| 277 |
mesh.face[i].V(0) = &(mesh.vert[id0]); |
| 278 |
mesh.face[i].V(1) = &(mesh.vert[id2]); |
| 279 |
mesh.face[i].V(2) = &(mesh.vert[id1]); |
| 280 |
} |
| 281 |
else |
| 282 |
{ |
| 283 |
mesh.face[i].V(0) = &(mesh.vert[id0]); |
| 284 |
mesh.face[i].V(1) = &(mesh.vert[id1]); |
| 285 |
mesh.face[i].V(2) = &(mesh.vert[id2]); |
| 286 |
} |
| 287 |
|
| 288 |
++i; |
| 289 |
} |
| 290 |
return NoError; |
| 291 |
} |
| 292 |
|
| 293 |
protected: |
| 294 |
|
| 295 |
}; |
| 296 |
|
| 297 |
} |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
#endif |