-
Notifications
You must be signed in to change notification settings - Fork 1
/
fastMarchingFoam.C
964 lines (751 loc) · 25 KB
/
fastMarchingFoam.C
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
/*
Notes:
- required to comment line 1164 ("assert(p->start() < p->stop());") out in geodesic/geodesic_algorithm_exact.h
this is also done in the AIMS 4.4 library (http://brainvisa.info/downloadpage.html)
*/
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <memory>
#include "geodesic/geodesic_algorithm_exact.h"
#include "geodesic/geodesic_algorithm_dijkstra.h"
#include "fvCFD.H"
#include "calculatedFvPatchField.H"
#define FM_HUGE 1e20
class GeodesicFoamMesh
{
protected:
// maps global face indices to (geodesic) face center vertex indices
HashTable<label, label> face_to_Cf_vertex_map;
// mesh and algorithm objects
const Foam::fvMesh* pmesh;
geodesic::Mesh* pgeo_mesh;
geodesic::GeodesicAlgorithmBase* pgeo_algorithm;
// vertices and triangles
std::vector<vector> vertices;
std::vector<std::size_t> triangles;
public:
GeodesicFoamMesh(const Foam::fvMesh& mesh)
{
pmesh = &mesh;
// get the boundary mesh
const polyBoundaryMesh& bmesh = mesh.boundaryMesh();
// get mesh points (vertices) and faces
const pointField& points = mesh.points();
const faceList& faces = mesh.faces();
// hash table for testing if a point was already added to vertices vector
HashTable<label, label> point_to_vertex_map;
// maps the global face index to the index of the face center vertex in the vertices vector
HashTable<label, label> face_to_vertex_map;
//std::cout << Pstream::myProcNo() << " points " << points.size() << std::endl;
// iterate over every patch of the boundary mesh
forAll(bmesh, patchI)
{
// get the patch
const polyPatch& patch = bmesh[patchI];
// get the face centers
const vectorField& Cf_patch = mesh.Cf().boundaryField()[patchI];
// get the face normals
const vectorField& Sf_patch = mesh.Sf().boundaryField()[patchI];
//std::cout << Pstream::myProcNo() << " patch " << patch.name() << ": " << patch.size() << std::endl;
// iterate over all faces of the patch
forAll(patch, faceI)
{
// calculate the global face index (on this processor)
label globalFaceI = patch.start() + faceI;
// get the face
const face& f = faces[globalFaceI];
// get face center
vector Cf = Cf_patch[faceI];
vector Sf = Sf_patch[faceI];
vector n = Sf / mag(Sf);
//std::cout << Pstream::myProcNo() << " face " << faceI << " " << globalFaceI << std::endl;
// get the first point
vector p0 = points[f[0]];
// map for angle of face point (relative to center and first point) to global face point index
HashTable<label, scalar> angle_to_point_map;
// iterate over face points
forAll(f, pointI)
{
// get the global point index (on this processor)
label globalPointI = f[pointI];
// get the point
vector p = points[globalPointI];
// compute the angle between (p0 - Cf) and (p - Cf)
vector Cfp0 = p0 - Cf; Cfp0 /= mag(Cfp0);
vector Cfp = p - Cf; Cfp /= mag(Cfp);
scalar cosAngle = Cfp0 & Cfp;
scalar sinAngle = (Cfp0 ^ Cfp) & n;
scalar angle = std::atan2(sinAngle, cosAngle);
if (angle < 0) angle += 2*M_PI;
angle_to_point_map.insert(angle, globalPointI);
// add point to liCfp0st of vertices
if (!point_to_vertex_map.found(globalPointI)) {
// make sure to add this point not again
point_to_vertex_map.insert(globalPointI, vertices.size());
// add point to vertex list
vertices.push_back(points[globalPointI]);
}
}
// triangulate the face
// we sort the face points by angle, since we do not know if they are sorted correctly
List<scalar> angles = angle_to_point_map.sortedToc();
// add face center to vertex list
label Cf_vertex_index = vertices.size();
vertices.push_back(Cf);
face_to_Cf_vertex_map.insert(globalFaceI, Cf_vertex_index);
forAll(f, angleI)
{
// get the triangle vertices
label v0 = Cf_vertex_index;
label v1 = point_to_vertex_map[angle_to_point_map[angles[angleI]]];
label v2 = point_to_vertex_map[angle_to_point_map[angles[(angleI+1) % angles.size()]]];
//std::cout << Pstream::myProcNo() << " triangle " << v0 << " " << v1 << " " << v2 << std::endl;
// add the triangle
triangles.push_back(v0);
triangles.push_back(v1);
triangles.push_back(v2);
}
}
}
// convert vetrices and triangles to geodesic format
std::vector<double> geo_vertices(vertices.size()*3);
std::vector<unsigned> geo_triangles(triangles.size());
for (std::size_t i = 0; i < vertices.size(); i++) {
geo_vertices[3*i + 0] = (double) vertices[i].x();
geo_vertices[3*i + 1] = (double) vertices[i].y();
geo_vertices[3*i + 2] = (double) vertices[i].z();
}
for (std::size_t i = 0; i < triangles.size(); i++) {
geo_triangles[i] = (unsigned) triangles[i];
}
// create internal mesh data structure including edges
pgeo_mesh = new geodesic::Mesh();
pgeo_mesh->initialize_mesh_data(geo_vertices, geo_triangles);
// create exact algorithm for the mesh
pgeo_algorithm = new geodesic::GeodesicAlgorithmExact(pgeo_mesh);
}
~GeodesicFoamMesh()
{
delete pgeo_mesh;
delete pgeo_algorithm;
}
label closestVertex(const vector& v)
{
label imin = 0;
double dmin = Foam::mag(vertices[0] - v);
for (std::size_t i = 1; i < vertices.size(); i++) {
vector dv = vertices[i] - v;
scalar d = Foam::mag(dv);
if (d < dmin) {
dmin = d;
imin = i;
}
}
return imin;
}
void calculateDistance(volScalarField& d, label vertexIndex, double maxDist = FM_HUGE)
{
// get the boundary mesh
const polyBoundaryMesh& bmesh = pmesh->boundaryMesh();
// get mesh faces
const faceList& faces = pmesh->faces();
// create source
// this is the point with zero distance
// in general, there could be multiple sources, but now we have only one
unsigned source_vertex_index = vertexIndex;
geodesic::SurfacePoint source(&pgeo_mesh->vertices()[source_vertex_index]);
std::vector<geodesic::SurfacePoint> all_sources(1, source);
// calculated distances to all vertices (on surface)
pgeo_algorithm->propagate(all_sources, maxDist);
// set default value
d.boundaryField() = maxDist;
// save distance in field d
forAll(bmesh, patchI)
{
const polyPatch& patch = bmesh[patchI];
forAll(patch, faceI)
{
label globalFaceI = patch.start() + faceI;
const face& f = faces[globalFaceI];
// get geodesic vertex index
label vertexI = face_to_Cf_vertex_map[globalFaceI];
// define geodesic surface point
geodesic::SurfacePoint p(&pgeo_mesh->vertices()[vertexI]);
// find closets source and distance to the source
double distance;
unsigned best_source = pgeo_algorithm->best_source(p, distance);
// store distance in field d
d.boundaryField()[patchI][faceI] = (scalar)std::min(distance, maxDist);
}
}
}
#if 0
void closeSurface(const std::vector<geodesic:SurfacePoint>& path)
{
}
void cutPath(const std::vector<geodesic:SurfacePoint>& path)
{
// TODO: get common edge of path[i] and path[i+1] vertex
// determine triangle left of path and right of path
// by checking sign of ((path[i+1] - path[i]) ^ (v - path[i])) & n
// where n is the triangle face normal and v is the vertex opposite to the triangle edge
// replace points of left triangle edge by duplicates
// except first and last vertex, if path is not closed
// except last vertex, if path is closed
}
#endif
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
int main(int argc, char *argv[])
{
Foam::argList args(argc, argv);
// check arguments
if (!args.checkRootCase()) {
Foam::FatalError.exit();
}
// create runtime object
Foam::Time runTime(Foam::Time::controlDictName, args);
// load the mesh
Foam::fvMesh mesh(
Foam::IOobject
(
Foam::fvMesh::defaultRegion,
runTime.timeName(),
runTime,
Foam::IOobject::MUST_READ
)
);
std::cout << "FM_HUGE = " << FM_HUGE << std::endl;
Info<< "Reading fastMarchingDict\n" << endl;
// read dictionary containing the points to be processed
Foam::IOdictionary fastMarchingDict(
Foam::IOobject(
"fastMarchingDict",
runTime.constant(),
mesh,
Foam::IOobject::MUST_READ,
Foam::IOobject::NO_WRITE
)
);
// iterate over the points and process each point
forAll(fastMarchingDict.toc(), key)
{
word name = fastMarchingDict.toc()[key];
dictionary& dict = fastMarchingDict.subDict(name);
Info << "Processing fast marching point: " << name << endl;
// read position
vector center(dict.lookup("sourcePoint"));
// read maximum propagation distance
scalar maxDist(dict.lookupOrDefault<scalar>("maxDist", FM_HUGE));
// calculated distance field (actually only the surface faces are used)
volScalarField d
(
IOobject
(
name,
runTime.timeName(),
mesh,
IOobject::NO_READ,
IOobject::NO_WRITE
),
mesh, 0, "calculated"
);
// calculated distance
GeodesicFoamMesh gfmesh(mesh);
label vertex_index = gfmesh.closestVertex(center);
gfmesh.calculateDistance(d, vertex_index, maxDist);
// write field d
d.write();
}
// program successful
return 0;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// The following is all old stuff:
void getNeighboursRecursive(polyMesh& mesh, label cellI, int nmax, int levels, const labelHashSet& constraint, HashTable<scalar,label>& result);
void getNeighboursRecursive(polyMesh& mesh, label cellI, int nmax, int levels, const labelHashSet& constraint, HashTable<scalar,label>& result, label currentCellI, labelHashSet& visitedCells);
int xmain(int argc, char *argv[])
{
Foam::argList args(argc, argv);
// check arguments
if (!args.checkRootCase()) {
Foam::FatalError.exit();
}
// create runtime object
Foam::Time runTime(Foam::Time::controlDictName, args);
// load the mesh
Foam::fvMesh mesh
(
Foam::IOobject
(
Foam::fvMesh::defaultRegion,
runTime.timeName(),
runTime,
Foam::IOobject::MUST_READ
)
);
volVectorField g
(
IOobject
(
"g",
runTime.timeName(),
mesh,
IOobject::NO_READ,
IOobject::NO_WRITE
),
mesh, vector(0,0,0), "zeroGradient"
);
volScalarField d
(
IOobject
(
"d",
runTime.timeName(),
mesh,
IOobject::NO_READ,
IOobject::NO_WRITE
),
mesh, 0, "calculated"
);
/*
d.boundaryField()[0][10] = 11;
d.write();
std::cout << "patchI " << d.boundaryField()[0].size() << " " << d.boundaryField()[0].type() << std::endl;
return 0;
*/
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
const polyBoundaryMesh& bmesh = mesh.boundaryMesh();
const pointField& points = mesh.points();
const faceList& faces = mesh.faces();
HashTable<label, label> boundary_face_hash;
HashTable<label, label> boundary_point_hash;
labelList boundary_faces;
labelList boundary_points;
forAll(bmesh, patchI)
{
const polyPatch& patch = bmesh[patchI];
// std::cout << "patch " << patch.name() << ": " << patch.size() << std::endl;
forAll(patch, faceI)
{
label globalFaceI = patch.start() + faceI;
// std::cout << "face " << faceI << " " << globalFaceI << std::endl;
const face& f = faces[globalFaceI];
if (!boundary_face_hash.found(globalFaceI)) {
boundary_faces.append(globalFaceI);
boundary_face_hash.insert(globalFaceI, boundary_face_hash.size());
}
forAll(f, pointI)
{
label globalPointI = f[pointI];
//std::cout << "point " << pointI << " " << globalPointI << std::endl;
if (!boundary_point_hash.found(globalPointI)) {
boundary_points.append(globalPointI);
boundary_point_hash.insert(globalPointI, boundary_point_hash.size());
}
}
}
}
std::vector<double> vertices;
std::vector<unsigned> triangles;
// get vertices
for (int i = 0; i < boundary_points.size(); i++)
{
const vector& p = points[boundary_points[i]];
vertices.push_back(p.x());
vertices.push_back(p.y());
vertices.push_back(p.z());
}
// get faces
for (int i = 0; i < boundary_faces.size(); i++)
{
const face& f = faces[boundary_faces[i]];
int n = 0;
forAll(f, pointI)
{
label globalPointI = f[pointI];
if (n >= 3) {
int m = triangles.size();
triangles.push_back(triangles[m-n]);
triangles.push_back(triangles[m-1]);
triangles.push_back(boundary_point_hash[globalPointI]);
n += 3;
}
else {
triangles.push_back(boundary_point_hash[globalPointI]);
n++;
}
}
if (n < 3) {
std::cout << "bad face " << i << std::endl;
return -1;
}
}
std::cout << "ntriangeles " << (triangles.size()/3) << std::endl;
std::cout << "nvertices " << (vertices.size()/3) << std::endl;
#if 0
for (size_t i = 0; i < vertices.size(); i++) {
if ((i % 3) == 0) std::cout << i << ": ";
std::cout << vertices[i] << " ";
if (((i+1) % 3) == 0) std::cout << std::endl;
}
for (size_t i = 0; i < triangles.size(); i++) {
if ((i % 3) == 0) std::cout << i << ": ";
std::cout << triangles[i] << " ";
if (((i+1) % 3) == 0) std::cout << std::endl;
}
#endif
geodesic::Mesh geo_mesh;
geo_mesh.initialize_mesh_data(vertices, triangles); //create internal mesh data structure including edges
geodesic::GeodesicAlgorithmDijkstra algorithm(&geo_mesh); //create exact algorithm for the mesh
unsigned source_vertex_index = 0;
geodesic::SurfacePoint source(&geo_mesh.vertices()[source_vertex_index]); //create source
std::vector<geodesic::SurfacePoint> all_sources(1,source); //in general, there could be multiple sources, but now we have only one
if(false) //target vertex specified, compute single path
{
unsigned target_vertex_index = atol(argv[3]);
geodesic::SurfacePoint target(&geo_mesh.vertices()[target_vertex_index]); //create source
std::vector<geodesic::SurfacePoint> path; //geodesic path is a sequence of SurfacePoints
bool const lazy_people_flag = false; //there are two ways to do exactly the same
if(lazy_people_flag)
{
algorithm.geodesic(source, target, path); //find a single source-target path
}
else //doing the same thing explicitly for educational reasons
{
double const distance_limit = FM_HUGE; // no limit for propagation
std::vector<geodesic::SurfacePoint> stop_points(1, target); //stop propagation when the target is covered
algorithm.propagate(all_sources, distance_limit, &stop_points); //"propagate(all_sources)" is also fine, but take more time because covers the whole mesh
algorithm.trace_back(target, path); //trace back a single path
}
print_info_about_path(path);
for(unsigned i = 0; i<path.size(); ++i)
{
geodesic::SurfacePoint& s = path[i];
std::cout << s.x() << "\t" << s.y() << "\t" << s.z() << std::endl;
}
}
else //target vertex is not specified, print distances to all vertices
{
algorithm.propagate(all_sources); //cover the whole mesh
#if 0
for(unsigned i=0; i<geo_mesh.vertices().size(); ++i)
{
geodesic::SurfacePoint p(&geo_mesh.vertices()[i]);
double distance;
unsigned best_source = algorithm.best_source(p,distance); //for a given surface point, find closets source and distance to this source
std::cout << distance << " "; //print geodesic distance for every vertex
}
std::cout << std::endl;
#endif
forAll(bmesh, patchI)
{
const polyPatch& patch = bmesh[patchI];
const vectorField& cf = mesh.Cf().boundaryField()[patchI];
forAll(patch, faceI)
{
label globalFaceI = patch.start() + faceI;
// std::cout << "face " << faceI << " " << globalFaceI << std::endl;
const face& f = faces[globalFaceI];
d.boundaryField()[patchI][faceI] = 0.0;
// Foam::fvPatchField<scalar>& bf = d.boundaryField()[patchI];
// bf[faceI] = 0.0;
vector c = cf[faceI];
// std::cout << "face " << faceI << " " << c.x() << std::endl;
// geodesic::SurfacePoint p(&geo_mesh.vertices()[i]);
}
}
}
return 0;
// this is the old code (not very accurate, but shows how to work with cells)
#if 1
const vectorField& c = mesh.cellCentres();
labelHashSet frozenCells;
labelHashSet newFront;
frozenCells.insert(23415);
// set inital distance value
for (labelHashSet::iterator i = frozenCells.begin(); i != frozenCells.end(); i++) {
d[i.key()] = 0.0;
g[i.key()] = vector(0.0, 0.0, 0.0);
}
for(;;)
{
// recompute new front
newFront.clear();
for (labelHashSet::iterator i = frozenCells.begin(); i != frozenCells.end(); i++)
{
// get cell neighbours
label cellI = i.key();
const labelList& neighbours = mesh.cellCells()[cellI];
// iterate over cell neighbours
for (int j = 0; j < neighbours.size(); j++)
{
label neighbourCellI = neighbours[j];
// skip already computed cells
if (d[neighbourCellI] >= 0.0) {
continue;
}
newFront.set(neighbourCellI);
}
}
// check if we are done
if (newFront.size() == 0) break;
// compute distance between new and old front
int nmax = 2;
labelHashSet usedCells;
for (labelHashSet::iterator i = newFront.begin(); i != newFront.end(); i++)
{
label cellI = i.key();
// find nmax closest cells to cellI
HashTable<scalar,label> cellDist(nmax);
getNeighboursRecursive(mesh, cellI, nmax, 4, frozenCells, cellDist);
if (cellDist.size() == 1)
{
// only one closest cell
label cellI1 = cellDist.begin().key();
d[cellI] = d[cellI1] + cellDist[cellI1];
g[cellI] = (c[cellI] - c[cellI1])/cellDist[cellI1];
}
else if (cellDist.size() >= 2)
{
// two close cells
// get the cell indices
HashTable<scalar,label>::iterator k = cellDist.begin();
label cellI1 = k.key(); k++;
label cellI2 = k.key();
// get center and gradients
vector p = c[cellI];
vector g1 = g[cellI1];
vector g2 = g[cellI2];
// get the points of equal mean distance
scalar dmean = 0.5*(d[cellI1] + d[cellI2]);
vector p1 = c[cellI1] + (dmean - d[cellI1])*g1;
vector p2 = c[cellI2] + (dmean - d[cellI2])*g2;
vector p12 = p1 - p2;
scalar p12g1 = p12 & g1;
scalar p12g2 = p12 & g2;
scalar g12 = g1 & g2;
// compute interpolation parameter t
// i.e. min distance of p to
// x = p1 + t*(p2-p1)
// i.e. (x - p)*p12 = 0
scalar t = ((p1 - p) & p12)/(p12 & p12);
// discriminant g1 and g2 parallel
scalar D = 1.0 - g12*g12;
if (fabs(D) < SMALL)
{
// this case rarely happens
Info << "parallel" << endl;
if (g12 > 0) {
// lines are parallel
g[cellI] = g1*(1-t) + g2*t;
g[cellI] /= mag(g[cellI]);
vector q = p1*(1-t) + p2*t;
d[cellI] = dmean + mag(p - q);
}
else {
// lines are anti parallel
scalar d12 = mag(p12);
d[cellI] = (d[cellI1] + t*d12)*(1-t) + (d[cellI2] + (1-t)*d12)*t;
g[cellI] = vector(0, 0, 0);
}
}
else
{
// find the intersection/min. distance point of the lines
// line1: x1 = p1 + t1*g1
// line2: x2 = p2 + t2*g2
// i.e. (x1-x2)*g1 = 0 and (x1-x2)*g2 = 0
// i.e. p12g1 + t1 - t2*g12 = 0
// p12g2 + t1*g12 - t2 = 0
// i.e. p12g1 + t1 -p12g2*g12 - t1*g12*g12 = 0
// compute minimum distance points x1 and x2
scalar t1 = (p12g2*g12 - p12g1)/D;
scalar t2 = p12g2 + t1*g12;
vector x1 = p1 + t1*g1;
vector x2 = p2 + t2*g2;
vector d1 = p - x1;
vector d2 = p - x2;
vector n1 = d1/mag(d1);
vector n2 = d2/mag(d2);
// NOTE: maybe better 0.5*(n1 + n2)?
g[cellI] = n1*(1-t) + n2*t;
g[cellI] /= mag(g[cellI]);
if ((g[cellI] & (g1 + g2)) < 0) {
g[cellI] *= -1.0;
}
vector q1 = x1 - n1*t1;
vector q2 = x2 - n2*t2;
// NOTE: maybe better 0.5*(q1 + q2)?
vector q = q1*(1-t) + q2*t;
d[cellI] = dmean + mag(p-q)*(((p - q) & g[cellI]) < 0 ? -1 : 1);
}
}
// for (HashTable<scalar,label>::iterator k = cellDist.begin(); k != cellDist.end(); k++) {
// usedCells.set(k.key());
// }
}
// remove unused cells from frozen cell set
// labelHashSet unUsedCells = frozenCells;
// unUsedCells.erase(usedCells);
// frozenCells.erase(unUsedCells);
// add new front to frozen cell set
for (labelHashSet::iterator i = newFront.begin(); i != newFront.end(); i++) {
frozenCells.set(i.key());
}
Info << frozenCells.size() << endl;
}
#else
const vectorField& c = mesh.cellCentres();
labelHashSet frozenCells;
labelHashSet newFront;
frozenCells.insert(23415);
// set inital distance value
for (labelHashSet::iterator i = frozenCells.begin(); i != frozenCells.end(); i++) {
d[i.key()] = 0.0;
}
for(;;)
{
// clear current front
newFront.clear();
for (labelHashSet::iterator i = frozenCells.begin(); i != frozenCells.end(); i++)
{
// get cell neighbours
label cellI = i.key();
const labelList& neighbours = mesh.cellCells()[cellI];
// iterate over cell neighbours
for (int j = 0; j < neighbours.size(); j++)
{
label neighbourCellI = neighbours[j];
// skip already computed cells
if (d[neighbourCellI] >= 0.0) {
continue;
}
newFront.set(neighbourCellI);
}
}
if (newFront.size() == 0) break;
// compute distance between new and old front
int nmax = 2;
labelHashSet usedCells;
for (labelHashSet::iterator i = newFront.begin(); i != newFront.end(); i++)
{
label cellI = i.key();
// find nmax closest cells to cellI
HashTable<scalar,label> cellDist(nmax);
getNeighboursRecursive(mesh, cellI, nmax, 4, frozenCells, cellDist);
if (cellDist.size() == 1)
{
// only one closest cell
label cellI1 = cellDist.begin().key();
d[cellI] = d[cellI1] + cellDist[cellI1];
}
else if (cellDist.size() >= 2)
{
// two close cells
HashTable<scalar,label>::iterator k = cellDist.begin();
label cellI1 = k.key(); k++;
label cellI2 = k.key();
vector d12 = c[cellI2] - c[cellI1];
scalar alpha = ((c[cellI] - c[cellI1]) & d12)/(d12 & d12);
vector dmin = (c[cellI] - c[cellI1]) - alpha*d12;
// d[cellI] = (d[cellI1]*cellDist[cellI1] + d[cellI2]*cellDist[cellI2])/(cellDist[cellI1] + cellDist[cellI2]) + mag(dmin);
d[cellI] = d[cellI1] + alpha*(d[cellI2] - d[cellI1]) + mag(dmin);
}
else if (cellDist.size() >= 3)
{
/*
HashTable<scalar,label>::iterator k = cellDist.begin();
label cellI1 = k.key(); k++;
label cellI2 = k.key(); k++;
label cellI3 = k.key();
vector d1x = c[cellI] - c[cellI1];
vector d21 = c[cellI2] - c[cellI1];
vector d31 = c[cellI1] - c[cellI1];
vector n = (d21 ^ d31);
*/
}
for (HashTable<scalar,label>::iterator k = cellDist.begin(); k != cellDist.end(); k++) {
usedCells.insert(k.key());
}
}
// remove unused cells from frozen cell set
labelHashSet unUsedCells = frozenCells;
unUsedCells.erase(usedCells);
frozenCells.erase(unUsedCells);
// add new front to frozen cell set
for (labelHashSet::iterator i = newFront.begin(); i != newFront.end(); i++) {
frozenCells.set(i.key());
}
Info << frozenCells.size() << endl;
}
#endif
// write distance function
d.write();
// write gradient function
g.write();
Info<< "End\n" << endl;
return 0;
}
// Get maximum nmax neighbour cells around cellI recursively (levels deep), which are also contained in the constraint set.
// Closer cells are preferred (i.e. nmax closest cells which are in the constraint set are returned).
// The result hash contains the cell ids and the center-center distance to cellI.
void getNeighboursRecursive(polyMesh& mesh, label cellI, int nmax, int levels, const labelHashSet& constraint, HashTable<scalar,label>& result, label currentCellI, labelHashSet& visitedCells)
{
// stop recursion if necessary
if (levels <= 0) return;
// get list of neighbour cells for cellI
const labelList& neighbours = mesh.cellCells()[currentCellI];
// get vector field of cell centers
const vectorField& center = mesh.cellCentres();
// iterate over cell neighbours
for (int j = 0; j < neighbours.size(); j++)
{
// get neighbour cell index
label neighbourCellI = neighbours[j];
// skip already visited cells
if (visitedCells.found(neighbourCellI)) {
continue;
}
// add cells only which are in the constraint set
if (constraint.found(neighbourCellI))
{
// compute cell distance
scalar dist = mag(center[cellI] - center[neighbourCellI]);
if (result.size() < nmax) {
// fill the result array up to nmax elements
result.set(neighbourCellI, dist);
}
else
{
// find the element with maximum distance in results
label kMax = result.begin().key();
for (HashTable<scalar,label>::iterator k = result.begin(); k != result.end(); k++) {
if (result[k.key()] > result[kMax]) {
kMax = k.key();
}
}
// replace the element with maximum distance by neighbourCellI, if closer
if (dist < result[kMax]) {
result.erase(kMax);
result.set(neighbourCellI, dist);
}
}
}
// mark cell as visited
visitedCells.set(neighbourCellI);
// recursive call
if (levels > 1) {
getNeighboursRecursive(mesh, cellI, nmax, levels-1, constraint, result, neighbourCellI, visitedCells);
}
}
}
void getNeighboursRecursive(polyMesh& mesh, label cellI, int nmax, int levels, const labelHashSet& constraint, HashTable<scalar,label>& result)
{
labelHashSet visitedCells(8*levels*levels);
visitedCells.set(cellI);
getNeighboursRecursive(mesh, cellI, nmax, levels, constraint, result, cellI, visitedCells);
}
// ************************************************************************* //