-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTetGenLoader.cpp
More file actions
126 lines (113 loc) · 4.4 KB
/
TetGenLoader.cpp
File metadata and controls
126 lines (113 loc) · 4.4 KB
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
#include "TetGenLoader.h"
#include <Core/Exceptions.h>
#include <Logging/Logger.h>
#include <Resources/File.h>
#include <Utils/Convert.h>
#include <iostream>
TetGenLoader::TetGenLoader(std::string vertexfile, std::string bodyfile,
std::string surfacefile) {
this->vertexfile = vertexfile;
this->bodyfile = bodyfile;
this->surfacefile = surfacefile;
}
TetGenLoader::~TetGenLoader() {
}
void TetGenLoader::Load() {
unsigned int temp;
unsigned int dim;
unsigned int index;
{
// Load body, surface and vertexpool from file
FILE* vFile = fopen(vertexfile.c_str(),"r");
if (!vFile)
throw Core::Exception("vertex file not found: " + vertexfile);
unsigned int numVertices;
int rslt = fscanf(vFile, "%i %i %i %i\n",
&numVertices, &dim, &temp, &temp);
if (rslt != 4) throw Core::Exception("vertex pool header error");
//logger.info << "---- vertex pool ----" << logger.end;
for (unsigned int i=0; i<numVertices && !feof(vFile); i++) {
Math::Vector<3,float> point;
rslt = fscanf(vFile, "%i %f %f %f\n",
&index, &(point[0]), &(point[1]), &(point[2]));
if (rslt != 4) throw Core::Exception("not a vertex");
//logger.info << point << logger.end;
vertexPool.push_back(point);
}
fclose (vFile);
if (numVertices != vertexPool.size())
throw Core::Exception("wrong number of vertex in vertexpool");
}
{
FILE* bFile = fopen(bodyfile.c_str(),"r");
if (!bFile)
throw Core::Exception("body index file not found: " + bodyfile);
unsigned int numTetrahedra;
int rslt = fscanf(bFile, "%i %i %i\n", &numTetrahedra, &dim, &temp);
if (rslt != 3) throw Core::Exception("body file header error");
//logger.info << "---- body indices ----" << logger.end;
for (unsigned int i=0; i<numTetrahedra && !feof(bFile); i++) {
Math::Vector<4,unsigned int> bid;
rslt = fscanf(bFile, "%i %i %i %i %i %i\n", &index,
&(bid[0]), &(bid[1]), &(bid[2]), &(bid[3]), &temp);
if (rslt != 6) throw Core::Exception("malformed tetra");
//logger.info << bid << logger.end;
body.push_back(bid);
}
fclose (bFile);
if (numTetrahedra != body.size())
throw Core::Exception("wrong number of body indices");
}
{
std::ifstream* in = Resources::File::Open(surfacefile);
char buffer[255];
int line = 0;
bool done = false;
while (!done) {
in->getline(buffer, 255);
line++;
if (sscanf(buffer, "%i %i %i %i", &temp, &dim, &temp, &temp) == 4)
done = true;
if (in->eof())
throw Core::Exception("invalid surface file header on line: " +
Utils::Convert::ToString(line));
}
unsigned int numTriangles;
done = false;
while (!done) {
in->getline(buffer, 255);
line++;
if (sscanf (buffer, "%i %i\n", &numTriangles, &temp) == 2)
done = true;
if (in->eof())
throw Core::Exception("invalid surface file header on line: " +
Utils::Convert::ToString(line));
}
//logger.info << "---- surface indices ----" << logger.end;
for (unsigned int i=0; i<numTriangles && !in->eof(); i++) {
Math::Vector<3, unsigned int> sid;
in->getline(buffer, 255);
line++;
if (sscanf (buffer, "%i %i %i %i",
&temp, &(sid[0]), &(sid[1]), &(sid[2])) != 4)
throw Core::Exception("invalid line in surface file: line " +
Utils::Convert::ToString(line));
//logger.info << sid << logger.end;
surface.push_back(sid);
}
in->close();
delete in;
if (numTriangles != surface.size())
throw Core::Exception("wrong number of surface indices");
}
//logger.info << "---- -------------- ----" << logger.end;
}
std::list< Math::Vector<3,float> > TetGenLoader::GetVertexPool() {
return vertexPool;
}
std::list< Math::Vector<3,unsigned int> > TetGenLoader::GetSurface() {
return surface;
}
std::list< Math::Vector<4,unsigned int> > TetGenLoader::GetBody() {
return body;
}