-
Notifications
You must be signed in to change notification settings - Fork 1
/
meshgen.cpp
220 lines (199 loc) · 6.28 KB
/
meshgen.cpp
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
#include "meshgen.h"
#define TETLIBRARY
#include "tetgen.h"
#include "Eigen/Sparse"
#include <vector>
#include <exception>
void MeshGen::GenerateBar(double*& points, int& psize, std::vector<int>& tets, std::vector<int>& faces, std::vector<int>& facetotet) {
tetgenio in, out;
tetgenio::facet *f;
tetgenio::polygon *p;
int i;
in.firstnumber = 1;
in.numberofpoints = 8;
in.pointlist = new REAL[in.numberofpoints * 3];
in.pointlist[0] = 0; // node 1.
in.pointlist[1] = 0;
in.pointlist[2] = 0;
in.pointlist[3] = 2; // node 2.
in.pointlist[4] = 0;
in.pointlist[5] = 0;
in.pointlist[6] = 2; // node 3.
in.pointlist[7] = 2;
in.pointlist[8] = 0;
in.pointlist[9] = 0; // node 4.
in.pointlist[10] = 2;
in.pointlist[11] = 0;
// Set node 5, 6, 7, 8.
for (i = 4; i < 8; i++) {
in.pointlist[i * 3] = in.pointlist[(i - 4) * 3];
in.pointlist[i * 3 + 1] = in.pointlist[(i - 4) * 3 + 1];
in.pointlist[i * 3 + 2] = 10;
}
in.numberoffacets = 6;
in.facetlist = new tetgenio::facet[in.numberoffacets];
in.facetmarkerlist = new int[in.numberoffacets];
// Facet 1. The leftmost facet.
f = &in.facetlist[0];
f->numberofpolygons = 1;
f->polygonlist = new tetgenio::polygon[f->numberofpolygons];
f->numberofholes = 0;
f->holelist = NULL;
p = &f->polygonlist[0];
p->numberofvertices = 4;
p->vertexlist = new int[p->numberofvertices];
p->vertexlist[0] = 1;
p->vertexlist[1] = 2;
p->vertexlist[2] = 3;
p->vertexlist[3] = 4;
// Facet 2. The rightmost facet.
f = &in.facetlist[1];
f->numberofpolygons = 1;
f->polygonlist = new tetgenio::polygon[f->numberofpolygons];
f->numberofholes = 0;
f->holelist = NULL;
p = &f->polygonlist[0];
p->numberofvertices = 4;
p->vertexlist = new int[p->numberofvertices];
p->vertexlist[0] = 5;
p->vertexlist[1] = 6;
p->vertexlist[2] = 7;
p->vertexlist[3] = 8;
// Facet 3. The bottom facet.
f = &in.facetlist[2];
f->numberofpolygons = 1;
f->polygonlist = new tetgenio::polygon[f->numberofpolygons];
f->numberofholes = 0;
f->holelist = NULL;
p = &f->polygonlist[0];
p->numberofvertices = 4;
p->vertexlist = new int[p->numberofvertices];
p->vertexlist[0] = 1;
p->vertexlist[1] = 5;
p->vertexlist[2] = 6;
p->vertexlist[3] = 2;
// Facet 4. The back facet.
f = &in.facetlist[3];
f->numberofpolygons = 1;
f->polygonlist = new tetgenio::polygon[f->numberofpolygons];
f->numberofholes = 0;
f->holelist = NULL;
p = &f->polygonlist[0];
p->numberofvertices = 4;
p->vertexlist = new int[p->numberofvertices];
p->vertexlist[0] = 2;
p->vertexlist[1] = 6;
p->vertexlist[2] = 7;
p->vertexlist[3] = 3;
// Facet 5. The top facet.
f = &in.facetlist[4];
f->numberofpolygons = 1;
f->polygonlist = new tetgenio::polygon[f->numberofpolygons];
f->numberofholes = 0;
f->holelist = NULL;
p = &f->polygonlist[0];
p->numberofvertices = 4;
p->vertexlist = new int[p->numberofvertices];
p->vertexlist[0] = 3;
p->vertexlist[1] = 7;
p->vertexlist[2] = 8;
p->vertexlist[3] = 4;
// Facet 6. The front facet.
f = &in.facetlist[5];
f->numberofpolygons = 1;
f->polygonlist = new tetgenio::polygon[f->numberofpolygons];
f->numberofholes = 0;
f->holelist = NULL;
p = &f->polygonlist[0];
p->numberofvertices = 4;
p->vertexlist = new int[p->numberofvertices];
p->vertexlist[0] = 4;
p->vertexlist[1] = 8;
p->vertexlist[2] = 5;
p->vertexlist[3] = 1;
// Set 'in.facetmarkerlist'
in.facetmarkerlist[0] = -1;
in.facetmarkerlist[1] = -2;
in.facetmarkerlist[2] = 0;
in.facetmarkerlist[3] = 0;
in.facetmarkerlist[4] = 0;
in.facetmarkerlist[5] = 0;
// Tetrahedralize the PLC. Switches are chosen to read a PLC (p),
// do quality mesh generation (q) with a specified quality bound
// (1.414), and apply a maximum volume constraint (a0.1).
tetrahedralize("pnnq1.414a.5", &in, &out);
points = new double[out.numberofpoints*3];
for (int i = 0; i < out.numberofpoints*3;++i) {
if (i%3 == 2) {
points[i] = -1* out.pointlist[i];
} else {
points[i] = out.pointlist[i];
}
}
psize = out.numberofpoints;
for (int i = 0; i <out.numberoftetrahedra*4; ++i) {
tets.push_back(out.tetrahedronlist[i] - 1);
}
for (int i = 0; i < out.numberoftrifaces*3; ++i) {
faces.push_back(out.trifacelist[i] - 1);
}
for (int i = 0; i < out.numberoftrifaces; ++i) {
if (out.adjtetlist[i*2] - 1< 0 || out.adjtetlist[i*2] - 1 >= out.numberoftetrahedra) {
if (out.adjtetlist[i*2+1] - 1< 0 || out.adjtetlist[i*2+1] - 1 >= out.numberoftetrahedra) {
printf("No adj tet for this face %d\n", i);
facetotet.push_back(0);
} else {
facetotet.push_back(out.adjtetlist[i*2+1] - 1);
}
} else {
facetotet.push_back(out.adjtetlist[i*2] - 1);
}
}
}
void MeshGen::GenerateMesh(double*& points, int& psize, std::vector<int>& tets, std::vector<int>& faces, std::vector<int>& facetotet, const char* filename) {
tetgenio out, in;
int i;
// All indices start from 0.
in.firstnumber = 0;
try{
if (!in.load_ply((char*)filename)) {
fprintf(stderr, "Load_ply failed\n");
points = NULL;
return;
}
// Tetrahedralize the PLC. Switches are chosen to read a PLC (p),
// do quality mesh generation (q) with a specified quality bound
// (1.414), and apply a maximum volume constraint (a0.1).
tetrahedralize("pnnqa.5", &in, &out);
} catch (int e) {
fprintf(stderr, "Tetrahedralize aborted with %i\n", e);
points = NULL;
return;
}
points = new double[out.numberofpoints*3];
for (int i = 0; i < out.numberofpoints*3;++i) {
if (i%3 == 2) {
points[i] = -1* out.pointlist[i];
} else {
points[i] = out.pointlist[i];
}
}
psize = out.numberofpoints;
for (int i = 0; i <out.numberoftetrahedra*4; ++i) {
tets.push_back(out.tetrahedronlist[i]);
}
for (int i = 0; i < out.numberoftrifaces*3; ++i) {
faces.push_back(out.trifacelist[i]);
}
for (int i = 0; i < out.numberoftrifaces; ++i) {
if (out.adjtetlist[i*2] < 0 || out.adjtetlist[i*2] >= out.numberoftetrahedra) {
if (out.adjtetlist[i*2+1] < 0 || out.adjtetlist[i*2+1] >= out.numberoftetrahedra) {
facetotet.push_back(0);
} else {
facetotet.push_back(out.adjtetlist[i*2+1]);
}
} else {
facetotet.push_back(out.adjtetlist[i*2]);
}
}
}