-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimModelGen2d.cc
More file actions
465 lines (426 loc) · 16.4 KB
/
simModelGen2d.cc
File metadata and controls
465 lines (426 loc) · 16.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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#include "simModelGen2d.h"
#include "Quadtree.h"
#include <map>
std::array<double, 3> subtractPts(double a[3], double b[3]) {
return {b[0] - a[0], b[1] - a[1], b[2] - a[2]};
}
std::array<double, 3> getNormal(pGEdge first, pGEdge second) {
// the tail of edge first is the head of edge second
assert(GE_vertex(first, 1) == GE_vertex(second, 0));
pGVertex src = GE_vertex(first, 1);
pGVertex uDest = GE_vertex(first, 0);
pGVertex vDest = GE_vertex(second, 1);
double srcPt[3];
GV_point(src, srcPt);
double uDestPt[3];
GV_point(uDest, uDestPt);
double vDestPt[3];
GV_point(vDest, vDestPt);
auto u = subtractPts(uDestPt, srcPt);
auto v = subtractPts(vDestPt, srcPt);
return {u[1] * v[2] - u[2] * v[1], u[2] * v[0] - u[0] * v[2],
u[0] * v[1] - u[1] * v[0]};
}
double getPt2PtEdgeLength(pGEdge edge) {
pGVertex start = GE_vertex(edge, 1);
pGVertex end = GE_vertex(edge, 0);
double startPt[3];
GV_point(start, startPt);
double endPt[3];
GV_point(end, endPt);
auto lenSq = getLengthSquared(startPt[0], startPt[1], endPt[0], endPt[1]);
return std::sqrt(lenSq);
}
pGEdge fitCurveToContourSimInterp(bool isLinearSpline, pGRegion region, pGVertex first, pGVertex last,
std::vector<double>& pts, bool debug=false) {
assert(pts.size() % 3 == 0); //pts must contain coordinates x1,y1,z1, x2,y2,z2, ...
const int numPts = pts.size()/3;
assert(numPts > 1);
pCurve curve;
if( isLinearSpline || numPts == 2 || numPts == 3) {
curve = SCurve_createPiecewiseLinear(numPts, &pts[0]); //TODO - replace withe bspline?
} else {
const int order = 4;
curve = SCurve_createInterpolatedBSpline(order, numPts, &pts[0], NULL);
}
pGEdge edge = GR_createEdge(region, first, last, curve, 1);
if(numPts>=4 && debug) {
const auto p2pLength = getPt2PtEdgeLength(edge);
const auto eLength = GE_length(edge);
if( eLength > 1.5*p2pLength ) {
std::cerr << "Warning: curve length " << eLength << " is more than 1.5 times longer than the end point to end point length " << p2pLength << "\n";
}
}
return edge;
}
void printModelInfo(pGModel model) {
std::cout << "Number of vertices in model: " << GM_numVertices(model)
<< std::endl;
std::cout << "Number of edges in model: " << GM_numEdges(model)
<< std::endl;
std::cout << "Number of faces in model: " << GM_numFaces(model)
<< std::endl;
std::cout << "Number of regions in model: " << GM_numRegions(model)
<< std::endl;
}
void setClassification(GeomInfo& geom, PointClassification& ptClass, const int firstPt, const int numPts, pGVertex startingMdlVtx, pGVertex endMdlVtx, pGEdge edge, const int splineIdx) {
const auto vtxDim = 0;
ptClass.id.at(firstPt) = GEN_tag(startingMdlVtx);
ptClass.dim.at(firstPt) = vtxDim;
ptClass.splineIdx.at(firstPt) = splineIdx;
auto ptIdx = geom.getNextPtIdx(firstPt); //handle wrap around in indexing
if(numPts > 2) {
const auto edgeTag = GEN_tag(edge);
const auto edgeDim = 1;
auto ptCount = 1;
while(ptCount < numPts-1) {
ptClass.id.at(ptIdx) = edgeTag;
ptClass.dim.at(ptIdx) = edgeDim;
ptClass.splineIdx.at(ptIdx) = splineIdx;
ptIdx = geom.getNextPtIdx(ptIdx);
ptCount++;
}
}
ptClass.id.at(ptIdx) = GEN_tag(endMdlVtx);
ptClass.dim.at(ptIdx) = vtxDim;
ptClass.splineIdx.at(ptIdx) = splineIdx;
}
void createEdges(ModelTopo& mdlTopo, GeomInfo& geom, PointClassification& ptClass, SplineInterp::SplineInfo& splines, std::vector<int>& isPtOnCurve, std::vector<int>& isMdlVtx, const bool debug) {
if(geom.numVtx <= 0) { //no contour
return;
}
enum class State {MdlVtx = 0, OnCurve = 1, NotOnCurve = 2};
enum class Action {Init, Advance, Line, Curve, LinearSpline};
typedef std::pair<State,Action> psa; // next state, action
using func=std::function<psa(int pt)>;
using funcIntBool=std::function<psa(int pt, bool)>;
pGVertex firstMdlVtx;
int firstPtIdx;
int startingCurvePtIdx;
pGVertex startingMdlVtx;
std::vector<int> ptsOnCurve;
funcIntBool createCurve = [&](int pt, bool isLinearSpline=false) {
assert(ptsOnCurve.size() >= 2);
double vtx[3] = {geom.vtx_x[pt], geom.vtx_y[pt], 0};
pGVertex endMdlVtx;
if(pt == firstPtIdx) { //wrap around
endMdlVtx = firstMdlVtx;
} else {
endMdlVtx = GR_createVertex(mdlTopo.region, vtx);
mdlTopo.vertices.push_back(endMdlVtx);
}
std::vector<double> pts(ptsOnCurve.size()*3);
for(int i=0, j = 0; j<ptsOnCurve.size(); j++, i+=3) {
const int ptIdx = ptsOnCurve.at(j);
pts[i] = geom.vtx_x[ptIdx];
pts[i+1] = geom.vtx_y[ptIdx];
pts[i+2] = 0;
}
double first[3];
GV_point(startingMdlVtx, first);
const double tol = 1e-12;
if( ! isPtCoincident(pts[0], pts[1], first[0], first[1], tol)) {
std::cerr << "first model vtx does not match first point on curve!... exiting\n";
exit(EXIT_FAILURE);
}
double last[3];
GV_point(endMdlVtx, last);
const int lpIdx = (ptsOnCurve.size()-1)*3;
if( ! isPtCoincident(pts[lpIdx], pts[lpIdx+1], last[0], last[1], tol) ) {
std::cerr << "last model vtx does not match last point on curve!... exiting\n";
exit(EXIT_FAILURE);
}
auto edge = fitCurveToContourSimInterp(isLinearSpline, mdlTopo.region, startingMdlVtx, endMdlVtx, pts, debug);
setClassification(geom, ptClass, startingCurvePtIdx, ptsOnCurve.size(), startingMdlVtx, endMdlVtx, edge, splines.size());
if(isLinearSpline) {
splines.addSpline(SplineInterp::attach_piecewise_linear_curve(pts));
} else {
splines.addSpline(SplineInterp::fitCubicSplineToPoints(pts));
}
mdlTopo.edges.push_back(edge);
if (debug) {
std::cerr << "edge " << mdlTopo.edges.size()
<< " splineIdx " << splines.size() << " "
<< " isLinearSpline " << isLinearSpline << " "
<< " range " << startingCurvePtIdx << " " << pt
<< " numPts " << ptsOnCurve.size() << "\n";
std::cout << "start " << first[0] << " " << first[1] << "\n";
std::cout << "end " << last[0] << " " << last[1] << "\n";
std::cout << "x,y,z\n";
for(int j=0; j<pts.size(); j+=3) {
std::cerr << pts.at(j) << ", " << pts.at(j+1) << ", " << pts.at(j+2) << "\n";
}
}
startingMdlVtx = endMdlVtx;
startingCurvePtIdx = pt;
ptsOnCurve.clear(); //FIXME - find a cheaper way
ptsOnCurve.push_back(pt);
return psa{State::MdlVtx,Action::Curve};
};
func createLinearSpline = [&](int pt) {
return createCurve(pt,true);
};
func createBSpline = [&](int pt) {
return createCurve(pt,false);
};
func createLine = [&](int pt) {
assert(ptsOnCurve.size() == 1);
ptsOnCurve.push_back(pt);
auto ignored = createCurve(pt, true);
return psa{State::MdlVtx,Action::Line};
};
func advance = [&](int pt) {
ptsOnCurve.push_back(pt);
return psa{State::OnCurve,Action::Advance};
};
func advanceLinearSpline = [&](int pt) {
ptsOnCurve.push_back(pt);
return psa{State::NotOnCurve,Action::Advance};
};
func createCurveFromPriorPt = [&](int pt) {
if(ptsOnCurve.size() == 1 ) {
return createLine(pt);
} else if (ptsOnCurve.size() >= 2 ) {
//we are not adding the current point yet, so there must be
//at least two points in the list to form a curve
auto ignored = createBSpline(ptsOnCurve.back());
ptsOnCurve.push_back(pt);
return psa{State::NotOnCurve,Action::LinearSpline};
} else {
std::cerr << "createCurveFromPriorPt: no points on the curve.... exiting\n";
exit(EXIT_FAILURE);
}
};
func createLinearSplineFromPriorPt = [&](int pt) {
if(ptsOnCurve.size() == 1 ) {
return createLine(pt);
} else if (ptsOnCurve.size() >= 2 ) {
//we are not adding the current point yet, so there must be
//at least two points in the list to form a curve
auto ignored = createLinearSpline(ptsOnCurve.back());
ptsOnCurve.push_back(pt);
return psa{State::OnCurve,Action::LinearSpline};
} else {
std::cerr << "createLinearSplineFromPriorPt: no points on the curve.... exiting\n";
exit(EXIT_FAILURE);
}
};
func createCurveFromCurrentPt = [&](int pt) {
ptsOnCurve.push_back(pt);
auto ret = createBSpline(pt);
return ret;
};
func createLinearSplineFromCurrentPt = [&](int pt) {
ptsOnCurve.push_back(pt);
auto ret = createLinearSpline(pt);
return ret;
};
func fail = [&](int pt) {
std::cerr << "bad state.... exiting\n";
exit(EXIT_FAILURE);
return psa{State::OnCurve,Action::Advance};
};
typedef std::pair<State,State> pss; // current state, next state
std::map<pss,func> machine = {
{{State::MdlVtx,State::MdlVtx}, createLine},
{{State::MdlVtx,State::OnCurve}, advance},
{{State::MdlVtx,State::NotOnCurve}, advanceLinearSpline},
{{State::OnCurve,State::MdlVtx}, createCurveFromCurrentPt},
{{State::OnCurve,State::OnCurve}, advance},
{{State::OnCurve,State::NotOnCurve}, createCurveFromPriorPt},
{{State::NotOnCurve,State::MdlVtx}, createLinearSplineFromCurrentPt},
{{State::NotOnCurve,State::OnCurve}, createLinearSplineFromPriorPt},
{{State::NotOnCurve,State::NotOnCurve}, advanceLinearSpline}
};
const int isVtx=1;
firstPtIdx = startingCurvePtIdx = findFirstPt(isMdlVtx, geom.firstContourPt, isVtx);
if(firstPtIdx == -1) {
std::cerr << "Error: at least one point must be marked as a model vertex... exiting\n";
assert(false);
exit(EXIT_FAILURE);
}
double vtx[3] = {geom.vtx_x[startingCurvePtIdx], geom.vtx_y[startingCurvePtIdx], 0};
firstMdlVtx = startingMdlVtx = GR_createVertex(mdlTopo.region, vtx);
mdlTopo.vertices.push_back(firstMdlVtx);
ptsOnCurve.push_back(startingCurvePtIdx);
State state = State::MdlVtx;
int ptsVisited = 0; //don't count the first vertex until we close the loop
int ptIdx = startingCurvePtIdx+1;
while(ptsVisited < isMdlVtx.size()-geom.firstContourPt) {
State nextState;
if(isMdlVtx[ptIdx] == 1) {
nextState = State::MdlVtx;
} else if (isPtOnCurve[ptIdx] == 1) {
nextState = State::OnCurve;
} else if (isPtOnCurve[ptIdx] == 0) {
nextState = State::NotOnCurve;
} else {
exit(EXIT_FAILURE);
}
psa res = machine[{state,nextState}](ptIdx);
state = res.first;
ptsVisited++;
ptIdx = geom.getNextPtIdx(ptIdx);
}
}
void createFace(ModelTopo& mdlTopo, PlaneBounds& planeBounds, bool debug) {
const double corner[3] = {planeBounds.minX, planeBounds.minY, 0};
const double xPt[3] = {planeBounds.maxX, planeBounds.minY, 0};
const double yPt[3] = {planeBounds.minX, planeBounds.maxY, 0};
const int faceDirectionFwd = 1;
const int sameNormal = 1;
pSurface planarSurface = SSurface_createPlane(corner, xPt, yPt);
const int numEdgesInnerFace = mdlTopo.edges.size();
const int numLoopsInnerFace = 1;
int loopFirstEdgeIdx[1] = {0};
for (int i = 0; i < numEdgesInnerFace; i++) {
mdlTopo.faceDirs.push_back(faceDirectionFwd); // clockwise
mdlTopo.faceEdges.push_back(mdlTopo.edges.at(i));
}
mdlTopo.faces.push_back(GR_createFace(mdlTopo.region, numEdgesInnerFace,
mdlTopo.faceEdges.data(),
mdlTopo.faceDirs.data(),
numLoopsInnerFace, loopFirstEdgeIdx,
planarSurface, sameNormal));
if(debug) {
std::cout << "faces[1] area: " << GF_area(mdlTopo.faces.at(0), 0.2) << "\n";
}
assert(GF_area(mdlTopo.faces.at(0), 0.2) > 0);
}
void createFaces(ModelTopo& mdlTopo, PlaneBounds& planeBounds, bool debug) {
// Now add the faces
double corner[3], xPt[3], yPt[3]; // the points defining the surface of the face
// When defining the loop, will always start with the first edge in the
// faceEdges array
pSurface planarSurface;
// **************
// Create the face between the bounding rectangle and the grounding line
// (water)
// **************
// Define the surface
corner[0] = planeBounds.minX;
corner[1] = planeBounds.minY;
corner[2] = 0;
xPt[0] = planeBounds.maxX;
xPt[1] = planeBounds.minY;
xPt[2] = 0;
yPt[0] = planeBounds.minX;
yPt[1] = planeBounds.maxY;
yPt[2] = 0;
const int faceDirectionFwd = 1;
const int faceDirectionRev = 0;
const int sameNormal = 1;
const int oppositeNormal = 0;
// the first four edges define the outer bounding rectangle
for (int i = 0; i < 4; i++) {
mdlTopo.faceDirs.push_back(faceDirectionFwd); // clockwise
mdlTopo.faceEdges.push_back(mdlTopo.edges.at(i));
}
if (mdlTopo.edges.size() > 4) {
// the remaining edges define the grounding line
// TODO generalize loop creation
int j = mdlTopo.edges.size() - 1;
for (int i = 4; i < mdlTopo.edges.size(); i++) {
mdlTopo.faceDirs.push_back(faceDirectionRev); // counter clockwise
// all edges are input in counter clockwise order,
// reverse the order so the face is on the left (simmetrix requirement)
mdlTopo.faceEdges.push_back(mdlTopo.edges.at(j--));
}
int numLoopsOuterFace = 2;
int loopFirstEdgeIdx[2] = {0, 4};
planarSurface = SSurface_createPlane(corner, xPt, yPt);
mdlTopo.faces.push_back(GR_createFace(mdlTopo.region, mdlTopo.edges.size(),
mdlTopo.faceEdges.data(),
mdlTopo.faceDirs.data(),
numLoopsOuterFace, loopFirstEdgeIdx,
planarSurface, sameNormal));
if(debug) {
std::cout << "faces[0] area: " << GF_area(mdlTopo.faces[0], 0.2) << "\n";
}
assert(GF_area(mdlTopo.faces[0], 0.2) > 0);
} else {
int numLoopsOuterFace = 1;
int loopFirstEdgeIdx[1] = {0};
planarSurface = SSurface_createPlane(corner, xPt, yPt);
mdlTopo.faces.push_back(GR_createFace(mdlTopo.region, mdlTopo.edges.size(),
mdlTopo.faceEdges.data(),
mdlTopo.faceDirs.data(),
numLoopsOuterFace, loopFirstEdgeIdx,
planarSurface, sameNormal));
if(debug) {
std::cout << "faces[0] area: " << GF_area(mdlTopo.faces[0], 0.2) << "\n";
}
assert(GF_area(mdlTopo.faces[0], 0.2) > 0);
}
mdlTopo.faceEdges.clear();
mdlTopo.faceDirs.clear();
if (mdlTopo.edges.size() > 4) {
// **************
// Create the 'ice' face bounded by the grounding line
// **************
planarSurface = SSurface_createPlane(corner, xPt, yPt);
const int numEdgesInnerFace = mdlTopo.edges.size() - 4;
const int numLoopsInnerFace = 1;
int loopFirstEdgeIdx[1] = {0};
int j = 4;
for (int i = 0; i < numEdgesInnerFace; i++) {
mdlTopo.faceDirs.push_back(faceDirectionFwd); // clockwise
mdlTopo.faceEdges.push_back(mdlTopo.edges.at(j++));
}
mdlTopo.faces.push_back(GR_createFace(mdlTopo.region, numEdgesInnerFace,
mdlTopo.faceEdges.data(),
mdlTopo.faceDirs.data(),
numLoopsInnerFace, loopFirstEdgeIdx,
planarSurface, sameNormal));
if(debug) {
std::cout << "faces[1] area: " << GF_area(mdlTopo.faces[1], 0.2) << "\n";
}
assert(GF_area(mdlTopo.faces[1], 0.2) > 0);
}
}
void createMesh(ModelTopo mdlTopo, std::string& meshFileName, pProgress progress, bool debug) {
pMesh mesh = M_new(0, mdlTopo.model);
pACase meshCase = MS_newMeshCase(mdlTopo.model);
pModelItem domain = GM_domain(mdlTopo.model);
// find the smallest size of the geometric model edges
auto minGEdgeLen = std::numeric_limits<double>::max();
for (int i = 0; i < mdlTopo.edges.size(); i++) {
auto len = GE_length(mdlTopo.edges.at(i));
if (len < minGEdgeLen)
minGEdgeLen = len;
}
const auto contourMeshSize = minGEdgeLen * 128;
const auto globMeshSize = contourMeshSize * 128;
if(debug) {
std::cout << "Min geometric model edge length: " << minGEdgeLen << std::endl;
std::cout << "Contour absolute mesh size target: " << contourMeshSize
<< std::endl;
std::cout << "Global absolute mesh size target: " << globMeshSize
<< std::endl;
}
MS_setMeshSize(meshCase, domain, 1, globMeshSize, NULL);
for (int i = 4; i < mdlTopo.edges.size(); i++)
MS_setMeshSize(meshCase, mdlTopo.edges.at(i), 1, contourMeshSize, NULL);
{
GFIter fIter = GM_faceIter(mdlTopo.model);
pGFace modelFace;
while (modelFace = GFIter_next(fIter)) {
const double area = GF_area(modelFace, 0.2);
if(debug) {
std::cout << "face area: " << area << "\n";
}
assert(area > 0);
}
GFIter_delete(fIter);
}
pSurfaceMesher surfMesh = SurfaceMesher_new(meshCase, mesh);
SurfaceMesher_execute(surfMesh, progress);
SurfaceMesher_delete(surfMesh);
std::cout << "Number of mesh faces in surface: " << M_numFaces(mesh)
<< std::endl;
M_write(mesh, meshFileName.c_str(), 0, progress);
std::cout << "Number of mesh regions in volume: " << M_numRegions(mesh)
<< std::endl;
MS_deleteMeshCase(meshCase);
M_release(mesh);
}