-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.cpp
More file actions
346 lines (333 loc) · 9.29 KB
/
util.cpp
File metadata and controls
346 lines (333 loc) · 9.29 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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include "util.h"
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <string>
double distance(std::vector<double> p0, std::vector<double> p1) {
double res = 0.;
for (size_t i = 0; i < p0.size() && i < p1.size(); ++i) {
res += (p0[i] - p1[i]) * (p0[i] - p1[i]);
}
return sqrt(res);
}
double distance(std::vector<std::vector<double>> p) {
double res = 0.;
for (size_t i = 1; i < p.size(); ++i) {
res += distance(p[i - 1], p[i]);
}
return res;
}
bool NanString(const char *buffer, size_t N) {
for (size_t i = 0; i < N && buffer[i]; ++i) {
if (buffer[i] != ' ') {
return (buffer[i] < '0' || buffer[i] > '9');
}
}
return true;
}
void OutGeo(std::string filename, std::vector<std::vector<double>> outerbox,
std::vector<std::vector<std::vector<double>>> innerbnd) {
std::ofstream outgmsh(filename.c_str());
int index = 1, j;
// inner boundary points
std::vector<std::vector<int>> line;
for (int i = 0; i < innerbnd.size(); ++i) {
int indexs = index;
for (j = 0; j < innerbnd[i].size(); ++j) {
outgmsh << std::scientific << std::setprecision(17);
std::vector<double> datum = innerbnd[i][j];
datum.resize(4, 0.);
outgmsh << "Point(" << index << ") = {" << std::setw(26) << datum[0]
<< ", " << datum[1] << ", " << datum[2] << ", " << datum[3]
<< "};\n";
std::vector<int> l;
l.push_back(index);
if (j < innerbnd[i].size() - 1) {
l.push_back(index + 1);
} else {
l.push_back(indexs);
}
line.push_back(l);
++index;
}
}
// outer boundary points
if (outerbox.size() >= 3) {
int indexs = index;
for (j = 0; j < outerbox.size(); ++j) {
std::vector<double> datum = outerbox[j];
datum.resize(4, 0.);
outgmsh << std::scientific << std::setprecision(17);
outgmsh << "Point(" << index << ") = {" << std::setw(26) << datum[0]
<< ", " << datum[1] << ", " << datum[2] << ", " << datum[3]
<< "};\n";
std::vector<int> l;
l.push_back(index);
if (j < outerbox.size() - 1) {
l.push_back(index + 1);
} else {
l.push_back(indexs);
}
line.push_back(l);
++index;
}
}
// lines
int linestart = index;
for (j = 0; j < line.size(); ++j) {
outgmsh << "Line(" << index << ") = {" << line[j][0] << ", " << line[j][1]
<< "};\n";
++index;
}
// line loops
int count = 0;
std::vector<std::vector<int>> lineloop;
for (int i = 0; i < innerbnd.size(); ++i) {
std::vector<int> c;
lineloop.push_back(c);
for (j = 0; j < innerbnd[i].size(); ++j) {
lineloop[i].push_back(count + linestart);
++count;
}
}
std::vector<int> c;
for (j = 0; j < outerbox.size(); ++j) {
c.push_back(count + linestart);
++count;
}
if (c.size() > 0) {
lineloop.push_back(c);
}
// lineloop
std::vector<int> surf;
for (int i = 0; i < lineloop.size(); ++i) {
outgmsh << "Line Loop(" << index << ") = {";
for (int j = 0; j < lineloop[i].size() - 1; ++j) {
outgmsh << lineloop[i][j] << ", ";
}
outgmsh << lineloop[i][lineloop[i].size() - 1] << "};\n";
surf.push_back(index);
++index;
}
// surf
outgmsh << "Plane Surface(" << index << ") = {";
for (int i = 0; i < surf.size() - 1; ++i) {
outgmsh << surf[i] << ", ";
}
outgmsh << surf[surf.size() - 1] << "};\n";
outgmsh << "Recombine Surface {" << index << "};\n";
std::cout << "Output file " << filename << std::endl;
}
std::vector<double> intersect(std::vector<double> p0, std::vector<double> n0,
std::vector<double> p1, std::vector<double> n1) {
std::vector<double> res(2, 1E38);
double jac = -n0[0] * n1[1] + n1[0] * n0[1];
if (fabs(jac) < 1E-38) {
return res;
}
double b0 = p1[0] - p0[0], b1 = p1[1] - p0[1];
jac = 1. / jac;
res[0] = jac * (-n1[1] * b0 + n1[0] * b1);
res[1] = jac * (-n0[1] * b0 + n0[0] * b1);
return res;
}
std::vector<double> AddVect(const double a1, const std::vector<double> &a,
const double b1, const std::vector<double> &b) {
size_t n = std::min(a.size(), b.size());
std::vector<double> res(n);
for (size_t i = 0; i < n; ++i) {
res[i] = a1 * a[i] + b1 * b[i];
}
return res;
}
int findNlayers(double h, double q, double R, double m) {
int n = 0;
double len = 0;
double delta = h;
for (n = 1; n <= 1000000; ++n) {
if (delta >= m)
delta = m;
len += delta;
if (len >= R)
return n;
delta *= q;
}
return n;
}
// relation :A in C in D; B in C in D
// [A; empty]
// [B; empty]
// [C; A, B];
// [D, C];
void BuildTopoTree(std::vector<std::vector<std::vector<double>>> &pts,
std::map<int, std::set<int>> &trees, std::set<int> &roots) {
trees.clear();
roots.clear();
std::vector<std::vector<double>> boxes;
for (size_t i = 0; i < pts.size(); ++i) {
boxes.push_back(GetBoundingBox(pts[i]));
}
// buid the relation tree
for (size_t p = 0; p < pts.size(); ++p) {
trees[p] = std::set<int>();
}
std::set<int> sons;
for (size_t p0 = 0; p0 + 1 < pts.size(); ++p0) {
for (size_t p1 = p0 + 1; p1 < pts.size(); ++p1) {
int r = FindRelationByBoundBox(boxes[p0], boxes[p1]);
if (r == 1) {
trees[p0].insert(p1);
sons.insert(p1);
} else if (r == -1) {
trees[p1].insert(p0);
sons.insert(p0);
}
}
}
// trim the tree
std::map<int, std::set<int>> toremove;
for (size_t p = 0; p < trees.size(); ++p) {
std::vector<int> list(trees[p].begin(), trees[p].end());
std::set<int> rm;
for (size_t i1 = 0; i1 + 1 < list.size(); ++i1) {
for (size_t i2 = i1 + 1; i2 < list.size(); ++i2) {
if (trees[list[i2]].find(list[i1]) != trees[list[i2]].end()) {
rm.insert(list[i1]);
} else if (trees[list[i1]].find(list[i2]) != trees[list[i1]].end()) {
rm.insert(list[i2]);
}
}
}
toremove[p] = rm;
}
for (size_t p = 0; p < trees.size(); ++p) {
if (sons.find(p) == sons.end()) {
roots.insert(p);
}
for (auto p1 : toremove[p]) {
trees[p].erase(p1);
}
}
}
// 1, 0 contains 1; -1, 1 contains 0; 0 overlap; 2, no contact.
int FindRelationByBoundBox(std::vector<double> &box0,
std::vector<double> &box1) {
int dim = (int)box0.size() / 2;
int sum = 0;
for (int i = 0; i < dim; ++i) {
if (box0[2 * i] < box1[2 * i] && box1[2 * i + 1] < box0[2 * i + 1]) {
sum += 1;
} else if (box1[2 * i] < box0[2 * i] && box0[2 * i + 1] < box1[2 * i + 1]) {
sum -= 1;
} else if (box0[2 * i + 1] < box1[2 * i] || box1[2 * i + 1] < box0[2 * i]) {
return 2;
}
}
if (sum == dim) {
return 1;
} else if (sum == -dim) {
return -1;
} else {
return 0; // to do improve this using Argz check
}
}
std::vector<double> GetBoundingBox(std::vector<std::vector<double>> &pts) {
std::vector<double> res;
if (pts.size() == 0) {
return res;
}
size_t dim = pts[0].size();
res.resize(2 * dim);
for (size_t i = 0; i < dim; ++i) {
res[2 * i] = std::numeric_limits<double>::max();
res[2 * i + 1] = std::numeric_limits<double>::lowest();
}
for (auto &p : pts) {
for (size_t i = 0; i < dim; ++i) {
if (res[2 * i] > p[i]) {
res[2 * i] = p[i];
}
if (res[2 * i + 1] < p[i]) {
res[2 * i + 1] = p[i];
}
}
}
return res;
}
void FindTreesDepths(int root, int lroot, std::map<int, std::set<int>> &trees,
std::vector<int> &levels) {
levels[root] = lroot;
for (auto p : trees[root]) {
FindTreesDepths(p, 1 + lroot, trees, levels);
}
}
void parserDouble(const char *cstr, std::vector<double> &value) {
value.clear();
std::vector<int> digs;
std::vector<int> dige;
int i = 0;
int flag = 0; // digit chunk
while (1) {
if ((cstr[i] >= '0' && cstr[i] <= '9') || cstr[i] == '.' ||
cstr[i] == 'e' || cstr[i] == 'E' || cstr[i] == '+' || cstr[i] == '-') {
if (flag == 0) {
digs.push_back(i);
}
flag = 1;
} else {
if (flag == 1) {
dige.push_back(i);
}
flag = 0;
}
if (cstr[i] == 0)
break;
++i;
}
double k;
for (int i = 0; i < digs.size(); ++i) {
std::string cuts(cstr + digs[i], dige[i] - digs[i]);
if (sscanf(cuts.c_str(), "%lf", &k) < 1) {
std::cout << "error: parser double " << cuts << std::endl;
}
value.push_back(k);
}
}
void parserUInt(const char *cstr, std::vector<int> &value) {
value.clear();
std::vector<int> digs;
std::vector<int> dige;
int i = 0;
int flag = 0; // digit chunk
while (1) {
if (cstr[i] >= '0' && cstr[i] <= '9') {
if (flag == 0) {
digs.push_back(i);
}
flag = 1;
} else {
if (flag == 1) {
dige.push_back(i);
}
flag = 0;
}
if (cstr[i] == 0)
break;
++i;
}
int k;
for (int i = 0; i < digs.size(); ++i) {
std::string cuts(cstr + digs[i], dige[i] - digs[i]); // data in [s e-1]
if (sscanf(cuts.c_str(), "%d", &k) < 1) {
std::cout << "error: parser int " << cuts << std::endl;
}
if (i > 0 && (digs[i] - dige[i - 1]) == 1 && cstr[digs[i] - 1] == '-') {
for (int j = value[value.size() - 1] + 1; j < k; ++j) {
value.push_back(j);
}
}
value.push_back(k);
}
}