-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTetMesh.cpp
More file actions
145 lines (118 loc) · 3.04 KB
/
TetMesh.cpp
File metadata and controls
145 lines (118 loc) · 3.04 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "TetVertex.h"
#include "TetHalfFace.h"
#include "TetFace.h"
#include "TetEdge.h"
#include "TetMesh.h"
#include "Tetra.h"
#include "Point.h"
#include "Triple.h"
#include "TetMeshUtility.h"
#include <iostream>
#include <fstream>
#include <map>
TetMesh::TetMesh()
: m_next_vid(0), m_next_tid(0)
{
}
TetMesh::~TetMesh() {
}
bool TetMesh::read(const char *filename) {
std::ifstream input(filename);
while(input.good()) {
std::string line;
getline(input, line);
if (line.empty()) {
continue;
}
std::stringstream ss(line);
std::string title, remain;
ss >> title;
if (title == "Vertex") {
TetVertex *tv = createTetVertex(ss);
id2Ver[tv->id()] = tv;
} else if (title == "Tetrahedron") {
Tetra *tet = createTetra(ss);
id2Tetra[tet->id()] = tet;
}
}
input.close();
id2Ver.rehash(id2Ver.size());
id2Tetra.rehash(id2Tetra.size());
m_next_vid = id2Ver.size();
m_next_tid = id2Tetra.size();
//link vertex to tetra
for (stdext::hash_map<int, Tetra*>::iterator b = id2Tetra.begin(), e = id2Tetra.end(); b != e; ++b) {
Tetra *tet = (*b).second;
for (int i = 0; i < 4; ++i) {
TetVertex *tv = tet->vertex(i);
tv->add(tet);
}
}
std::vector<TetHalfFace*> halffaces;
for (stdext::hash_map<int, Tetra*>::iterator b = id2Tetra.begin(), e = id2Tetra.end(); b != e; ++b) {
Tetra *tet = (*b).second;
for (int i = 0; i < 4; ++i) {
TetHalfFace *hf = tet->halfface(i);
halffaces.push_back(hf);
}
}
std::vector<TetFace*> faces;
TetMeshUtility::buildFaces(halffaces, faces);
std::vector<TetEdge*> edges;
TetMeshUtility::buildEdges(faces, edges);
return true;
}
void TetMesh::save(const char *filename) {
std::ofstream output(filename);
for (int vid = 0; vid < m_next_vid; ++vid) {
TetVertex *v = id2Ver[vid];
if (!v) {
continue;
}
output << "Vertex " << vid << " "
<< v->point()[0] << " "
<< v->point()[1] << " "
<< v->point()[2] << " " << v->property() << "\n";
}
for (int tid = 0; tid < m_next_tid; ++tid) {
Tetra *t = id2Tetra[tid];
if (!t || !t->isValid()) {
continue;
}
output << "Tetrahedron " << tid << " "
<< t->vertex(0)->id() << " "
<< t->vertex(1)->id() << " "
<< t->vertex(2)->id() << " "
<< t->vertex(3)->id() << " " << t->property()<< "\n";
}
output.close();
}
TetVertex * TetMesh::createTetVertex(std::stringstream &ss) {
int id;
Point pos;
std::string property;
ss >> id >> pos.v[0] >> pos.v[1] >> pos.v[2];
getline(ss, property);
TetVertex *v = new TetVertex(id, pos, property);
v->index() = id2Ver.size();
return v;
}
TetHalfFace * TetMesh::createTetHalfFace(TetVertex *half[3]) {
return new TetHalfFace(half);
}
TetFace * TetMesh::createTetFace(TetHalfFace *halfface) {
return new TetFace(halfface);
}
Tetra * TetMesh::createTetra(std::stringstream &ss) {
int id;
int vids[4];
std::string property;
ss >> id >> vids[0] >> vids[1] >> vids[2] >> vids[3];
TetVertex *vers[4];
for (int i = 0; i < 4; ++i) {
vers[i] = id2Ver[vids[i]];
}
getline(ss, property);
Tetra *tet = new Tetra(id, id2Tetra.size(), vers, property);
return tet;
}