Newer
Older
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
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
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM; if not, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Description
\*---------------------------------------------------------------------------*/
#include "surfaceIntersection.H"
#include "triSurfaceSearch.H"
#include "labelPairLookup.H"
#include "OFstream.H"
#include "HashSet.H"
#include "triSurface.H"
#include "pointIndexHit.H"
#include "octreeDataTriSurface.H"
#include "octree.H"
#include "mergePoints.H"
#include "plane.H"
#include "edgeIntersections.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
defineTypeNameAndDebug(Foam::surfaceIntersection, 0);
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
// Checks if there exists a special topological situation that causes
// edge and the face it hit not to be recognized.
//
// For now if the face shares a point with the edge
bool Foam::surfaceIntersection::excludeEdgeHit
(
const triSurface& surf,
const label edgeI,
const label faceI,
const scalar
)
{
const labelledTri& f = surf.localFaces()[faceI];
const edge& e = surf.edges()[edgeI];
if
(
(f[0] == e.start())
|| (f[0] == e.end())
|| (f[1] == e.start())
|| (f[1] == e.end())
|| (f[2] == e.start())
|| (f[2] == e.end())
)
{
return true;
// // Get edge vector
// vector eVec = e.vec(surf.localPoints());
// eVec /= mag(eVec) + VSMALL;
//
// const labelList& eLabels = surf.faceEdges()[faceI];
//
// // Get edge vector of 0th edge of face
// vector e0Vec = surf.edges()[eLabels[0]].vec(surf.localPoints());
// e0Vec /= mag(e0Vec) + VSMALL;
//
// vector n = e0Vec ^ eVec;
//
// if (mag(n) < SMALL)
// {
// // e0 is aligned with e. Choose next edge of face.
// vector e1Vec = surf.edges()[eLabels[1]].vec(surf.localPoints());
// e1Vec /= mag(e1Vec) + VSMALL;
//
// n = e1Vec ^ eVec;
//
// if (mag(n) < SMALL)
// {
// // Problematic triangle. Two edges aligned with edgeI. Give
// // up.
// return true;
// }
// }
//
// // Check if same as faceNormal
// if (mag(n & surf.faceNormals()[faceI]) > 1-tol)
// {
//
// Pout<< "edge:" << e << " face:" << faceI
// << " e0Vec:" << e0Vec << " n:" << n
// << " normalComponent:" << (n & surf.faceNormals()[faceI])
// << " tol:" << tol << endl;
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
// return true;
// }
// else
// {
// return false;
// }
}
else
{
return false;
}
}
//// Find intersection of plane with edges of hitFaceI. Returns
//// - edgeI
//// - intersection point
//Foam::pointIndexHit Foam::surfaceIntersection::faceEdgeIntersection
//(
// const triSurface& surf,
// const label hitFaceI,
//
// const vector& n,
// const point& eStart,
// const point& eEnd
//)
//{
// pointIndexHit pInter;
//
// const pointField& points = surf.points();
//
// const labelledTri& f = surf.localFaces()[hitFaceI];
//
// // Plane for intersect test.
// plane pl(eStart, n);
//
// forAll(f, fp)
// {
// label fp1 = (fp + 1) % 3;
//
// const point& start = points[f[fp]];
// const point& end = points[f[fp1]];
//
// vector eVec(end - start);
//
// scalar s = pl.normalIntersect(start, eVec);
//
// if (s < 0 || s > 1)
// {
// pInter.setPoint(start + s*eVec);
//
// // Check if is correct one: orientation walking
// // eStart - eEnd - hitPoint should be opposite n
// vector n2(triPointRef(start, end, pInter.hitPoint()).normal());
//
// Pout<< "plane normal:" << n
// << " start:" << start << " end:" << end
// << " hit at:" << pInter.hitPoint()
// << " resulting normal:" << n2 << endl;
//
// if ((n2 & n) < 0)
// {
// pInter.setHit();
//
// // Find corresponding edge between f[fp] f[fp1]
// label edgeI =
// meshTools::findEdge
// (
// surf.edges(),
// surf.faceEdges()[hitFaceI],
// f[fp],
// f[fp1]
// );
//
// pInter.setIndex(edgeI);
//
// return pInter;
// }
// }
// }
//
// FatalErrorIn("surfaceIntersection::borderEdgeIntersection")
// << "Did not find intersection of plane " << pl
// << " with edges of face " << hitFaceI << " verts:" << f
// << abort(FatalError);
//
// return pInter;
//}
void Foam::surfaceIntersection::storeIntersection
(
const bool isFirstSurf,
const labelList& facesA,
const label faceB,
DynamicList<edge>& allCutEdges,
DynamicList<point>& allCutPoints
)
{
forAll(facesA, facesAI)
{
label faceA = facesA[facesAI];
// Combine two faces. Always make sure the face from the first surface
// is element 0.
FixedList<label, 2> twoFaces;
if (isFirstSurf)
{
twoFaces[0] = faceA;
twoFaces[1] = faceB;
}
else
{
twoFaces[0] = faceB;
twoFaces[1] = faceA;
}
labelPairLookup::const_iterator iter = facePairToVertex_.find(twoFaces);
if (iter == facePairToVertex_.end())
{
// New intersection. Store face-face intersection.
facePairToVertex_.insert(twoFaces, allCutPoints.size()-1);
}
else
{
// Second occurrence of surf1-surf2 intersection.
// Or rather the face on surf1 intersects a face on
// surface2 twice -> we found edge.
// Check whether perhaps degenerate
const point& prevHit = allCutPoints[*iter];
const point& thisHit = allCutPoints[allCutPoints.size()-1];
if (mag(prevHit - thisHit) < SMALL)
{
WarningIn
(
"Foam::surfaceIntersection::storeIntersection"
"(const bool isFirstSurf, const labelList& facesA,"
"const label faceB, DynamicList<edge>& allCutEdges,"
"DynamicList<point>& allCutPoints)"
) << "Encountered degenerate edge between face "
<< twoFaces[0] << " on first surface"
<< " and face " << twoFaces[1] << " on second surface"
<< endl
<< "Point on first surface:" << prevHit << endl
<< "Point on second surface:" << thisHit << endl
<< endl;
}
else
{
allCutEdges.append(edge(*iter, allCutPoints.size()-1));
// Remember face on surf
facePairToEdge_.insert(twoFaces, allCutEdges.size()-1);
}
}
}
}
// Classify cut of edge of surface1 with surface2:
// 1- point of edge hits point on surface2
// 2- edge pierces point on surface2
// 3- point of edge hits edge on surface2
// 4- edge pierces edge on surface2
// 5- point of edge hits face on surface2
// 6- edge pierces face on surface2
//
// Note that handling of 2 and 4 should be the same but with surface1 and
// surface2 reversed.
void Foam::surfaceIntersection::classifyHit
(
const triSurface& surf1,
const scalarField& surf1PointTol,
const triSurface& surf2,
const bool isFirstSurf,
const label edgeI,
const scalar tolDim,
const pointIndexHit& pHit,
DynamicList<edge>& allCutEdges,
DynamicList<point>& allCutPoints,
List<DynamicList<label> >& surfEdgeCuts
)
{
const edge& e = surf1.edges()[edgeI];
const labelList& facesA = surf1.edgeFaces()[edgeI];
// Label of face on surface2 edgeI intersected
label surf2FaceI = pHit.index();
// Classify point on surface2
const labelledTri& f2 = surf2.localFaces()[surf2FaceI];
const pointField& surf2Pts = surf2.localPoints();
label nearType;
label nearLabel;
(void)triPointRef
(
surf2Pts[f2[0]],
surf2Pts[f2[1]],
surf2Pts[f2[2]]
).classify(pHit.hitPoint(), tolDim, nearType, nearLabel);
// Classify points on edge of surface1
label edgeEnd =
classify
(
surf1PointTol[e.start()],
surf1PointTol[e.end()],
pHit.hitPoint(),
e,
surf1.localPoints()
);
if (nearType == triPointRef::POINT)
{
if (edgeEnd >= 0)
{
// 1. Point hits point. Do nothing.
if (debug&2)
{
Pout<< pHit.hitPoint() << " is surf1:"
<< " end point of edge " << e
<< " surf2: vertex " << f2[nearLabel]
<< " coord:" << surf2Pts[f2[nearLabel]] << endl;
}
}
else
{
// 2. Edge hits point. Cut edge with new point.
if (debug&2)
{
Pout<< pHit.hitPoint() << " is surf1:"
<< " somewhere on edge " << e
<< " surf2: vertex " << f2[nearLabel]
<< " coord:" << surf2Pts[f2[nearLabel]] << endl;
}
allCutPoints.append(pHit.hitPoint());
surfEdgeCuts[edgeI].append(allCutPoints.size()-1);
const labelList& facesB = surf2.pointFaces()[f2[nearLabel]];
forAll(facesB, faceBI)
{
storeIntersection
(
isFirstSurf,
facesA,
facesB[faceBI],
allCutEdges,
allCutPoints
);
}
}
}
else if (nearType == triPointRef::EDGE)
{
if (edgeEnd >= 0)
{
// 3. Point hits edge. Do nothing on this side. Reverse
// is handled by 2 (edge hits point)
label edge2I = getEdge(surf2, surf2FaceI, nearLabel);
const edge& e2 = surf2.edges()[edge2I];
if (debug&2)
{
Pout<< pHit.hitPoint() << " is surf1:"
<< " end point of edge " << e
<< " surf2: edge " << e2
<< " coords:" << surf2Pts[e2.start()]
<< surf2Pts[e2.end()] << endl;
}
}
else
{
// 4. Edge hits edge.
// Cut edge with new point (creates duplicates when
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
// doing the surf2 with surf1 intersection but these
// are merged later on)
label edge2I = getEdge(surf2, surf2FaceI, nearLabel);
const edge& e2 = surf2.edges()[edge2I];
if (debug&2)
{
Pout<< pHit.hitPoint() << " is surf1:"
<< " somewhere on edge " << e
<< " surf2: edge " << e2
<< " coords:" << surf2Pts[e2.start()]
<< surf2Pts[e2.end()] << endl;
}
allCutPoints.append(pHit.hitPoint());
surfEdgeCuts[edgeI].append(allCutPoints.size()-1);
// edge hits all faces on surf2 connected to the edge
if (isFirstSurf)
{
// edge-edge intersection is symmetric, store only
// once.
// edge hits all faces on surf2 connected to the
// edge
const labelList& facesB = surf2.edgeFaces()[edge2I];
forAll(facesB, faceBI)
{
storeIntersection
(
isFirstSurf,
facesA,
facesB[faceBI],
allCutEdges,
allCutPoints
);
}
}
}
}
else
{
if (edgeEnd >= 0)
{
// 5. Point hits face. Do what? Introduce
// point & triangulation in face?
if (debug&2)
{
Pout<< pHit.hitPoint() << " is surf1:"
<< " end point of edge " << e
<< " surf2: face " << surf2FaceI
<< endl;
}
//
// Look exactly at what side (of surf2) edge is. Leave out ones on
// inside of surf2 (i.e. on opposite side of normal)
//
// Vertex on/near surf2
label nearVert = -1;
if (edgeEnd == 0)
{
nearVert = e.start();
}
else
{
nearVert = e.end();
}
const point& nearPt = surf1.localPoints()[nearVert];
// Vertex away from surf2
label otherVert = e.otherVertex(nearVert);
const point& otherPt = surf1.localPoints()[otherVert];
if (debug)
{
Pout
<< pHit.hitPoint() << " is surf1:"
<< " end point of edge " << e << " coord:"
<< surf1.localPoints()[nearVert]
<< " surf2: face " << surf2FaceI << endl;
}
vector eVec = otherPt - nearPt;
if ((surf2.faceNormals()[surf2FaceI] & eVec) > 0)
{
// otherVert on outside of surf2
// Shift hitPoint a bit along edge.
//point hitPt = nearPt + 0.1*eVec;
point hitPt = nearPt;
if (debug&2)
{
Pout<< "Shifted " << pHit.hitPoint()
<< " to " << hitPt
<< " along edge:" << e
<< " coords:" << surf1.localPoints()[e.start()]
<< surf1.localPoints()[e.end()] << endl;
}
// Reclassify as normal edge-face pierce (see below)
allCutPoints.append(hitPt);
surfEdgeCuts[edgeI].append(allCutPoints.size()-1);
// edge hits single face only
storeIntersection
(
isFirstSurf,
facesA,
surf2FaceI,
allCutEdges,
allCutPoints
);
}
else
{
if (debug&2)
{
Pout<< "Discarding " << pHit.hitPoint()
<< " since edge " << e << " on inside of surf2."
<< " surf2 normal:" << surf2.faceNormals()[surf2FaceI]
<< endl;
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
}
}
else
{
// 6. Edge pierces face. 'Normal' situation.
if (debug&2)
{
Pout<< pHit.hitPoint() << " is surf1:"
<< " somewhere on edge " << e
<< " surf2: face " << surf2FaceI
<< endl;
}
// edgeI intersects surf2. Store point.
allCutPoints.append(pHit.hitPoint());
surfEdgeCuts[edgeI].append(allCutPoints.size()-1);
// edge hits single face only
storeIntersection
(
isFirstSurf,
facesA,
surf2FaceI,
allCutEdges,
allCutPoints
);
}
}
if (debug&2)
{
Pout<< endl;
}
}
// Cut all edges of surf1 with surf2. Sets
// - cutPoints : coordinates of cutPoints
// - cutEdges : newly created edges between cutPoints
// - facePairToVertex : hash from face1I and face2I to cutPoint
// - facePairToEdge : hash from face1I and face2I to cutEdge
// - surfEdgeCuts : gives for each edge the cutPoints
// (in order from start to end)
//
void Foam::surfaceIntersection::doCutEdges
(
const triSurface& surf1,
const triSurfaceSearch& querySurf2,
const bool isFirstSurf,
const bool isSelfIntersection,
DynamicList<edge>& allCutEdges,
DynamicList<point>& allCutPoints,
List<DynamicList<label> >& surfEdgeCuts
)
{
scalar oldTol = intersection::setPlanarTol(1E-3);
const pointField& surf1Pts = surf1.localPoints();
// Calculate local (to point) tolerance based on min edge length.
scalarField surf1PointTol(surf1Pts.size());
forAll(surf1PointTol, pointI)
{
surf1PointTol[pointI] =
intersection::planarTol()
* minEdgeLen(surf1, pointI);
}
const triSurface& surf2 = querySurf2.surface();
forAll(surf1.edges(), edgeI)
{
const edge& e = surf1.edges()[edgeI];
point pStart = surf1Pts[e.start()];
const point& pEnd = surf1Pts[e.end()];
const point tolVec = intersection::planarTol()*(pEnd-pStart);
const scalar tolDim = mag(tolVec);
bool doTrack = false;
do
{
pointIndexHit pHit = querySurf2.tree().findLine(pStart, pEnd);
if (pHit.hit())
{
if (isSelfIntersection)
{
// Skip all intersections which are hit at endpoints of
// edge.
// Problem is that if faces are almost coincident the
// intersection point will be calculated quite incorrectly
// The error might easily be larger than 1% of the edge
// length.
// So what we do here is to exclude hit faces if our edge
// is in their plane and they share a point with the edge.
// Label of face on surface2 edgeI intersected
label hitFaceI = pHit.index();
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
(
!excludeEdgeHit
(
surf1,
edgeI,
hitFaceI,
0.1 // 1-cos of angle between normals
)
)
{
// Classify point on surface1
label edgeEnd = classify
(
surf1PointTol[e.start()],
surf1PointTol[e.end()],
pHit.hitPoint(),
e,
surf1Pts
);
if (edgeEnd < 0)
{
if (debug)
{
Pout<< "edge:" << edgeI << " vertices:" << e
<< " start:" << surf1Pts[e.start()]
<< " end:" << surf1Pts[e.end()]
<< " hit:" << pHit.hitPoint()
<< " tolDim:" << tolDim
<< " planarTol:"
<< intersection::planarTol()
<< endl;
}
allCutPoints.append(pHit.hitPoint());
surfEdgeCuts[edgeI].append(allCutPoints.size()-1);
}
}
}
else
{
classifyHit
(
surf1,
surf1PointTol,
surf2,
isFirstSurf,
edgeI,
tolDim,
pHit,
allCutEdges,
allCutPoints,
surfEdgeCuts
);
}
if (mag(pHit.hitPoint() - pEnd) < tolDim)
{
doTrack = false;
}
else
{
pStart = pHit.hitPoint() + tolVec;
doTrack = true;
}
}
else
{
doTrack = false;
}
}
while(doTrack);
}
intersection::setPlanarTol(oldTol);
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
// Null constructor
Foam::surfaceIntersection::surfaceIntersection()
:
cutPoints_(0),
cutEdges_(0),
facePairToVertex_(0),
facePairToEdge_(0),
surf1EdgeCuts_(0),
surf2EdgeCuts_(0)
{}
// Construct from two surfaces
Foam::surfaceIntersection::surfaceIntersection
(
const triSurfaceSearch& query1,
const triSurfaceSearch& query2
)
:
cutPoints_(0),
cutEdges_(0),
facePairToVertex_(2*max(query1.surface().size(), query2.surface().size())),
facePairToEdge_(2*max(query1.surface().size(), query2.surface().size())),
surf1EdgeCuts_(0),
surf2EdgeCuts_(0)
{
const triSurface& surf1 = query1.surface();
const triSurface& surf2 = query2.surface();
//
// Cut all edges of surf1 with surf2.
//
if (debug)
{
Pout<< "Cutting surf1 edges" << endl;
}
DynamicList<edge> allCutEdges(surf1.nEdges()/20);
DynamicList<point> allCutPoints(surf1.nPoints()/20);
// From edge to cut index on surface1
List<DynamicList<label> > edgeCuts1(query1.surface().nEdges());
doCutEdges
(
surf1,
query2,
true, // is first surface; construct labelPair in correct
// order
false, // not self intersection
allCutEdges,
allCutPoints,
edgeCuts1
);
// Transfer to straight labelListList
transfer(edgeCuts1, surf1EdgeCuts_);
//
// Cut all edges of surf2 with surf1.
//
if (debug)
{
Pout<< "Cutting surf2 edges" << endl;
}
// From edge to cut index
List<DynamicList<label> > edgeCuts2(query2.surface().nEdges());
doCutEdges
(
surf2,
query1,
false, // is second surface
false, // not self intersection
allCutEdges,
allCutPoints,
edgeCuts2
);
// Transfer to straight label(List)List
transfer(edgeCuts2, surf2EdgeCuts_);
cutEdges_.transfer(allCutEdges);
cutPoints_.transfer(allCutPoints);
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
if (debug)
{
Pout<< "surfaceIntersection : Intersection generated:"
<< endl
<< " points:" << cutPoints_.size() << endl
<< " edges :" << cutEdges_.size() << endl;
Pout<< "surfaceIntersection : Writing intersection to intEdges.obj"
<< endl;
OFstream intStream("intEdges.obj");
writeOBJ(cutPoints_, cutEdges_, intStream);
// Dump all cut edges to files
Pout<< "Dumping cut edges of surface1 to surf1EdgeCuts.obj" << endl;
OFstream edge1Stream("surf1EdgeCuts.obj");
writeIntersectedEdges(surf1, surf1EdgeCuts_, edge1Stream);
Pout<< "Dumping cut edges of surface2 to surf2EdgeCuts.obj" << endl;
OFstream edge2Stream("surf2EdgeCuts.obj");
writeIntersectedEdges(surf2, surf2EdgeCuts_, edge2Stream);
}
}
// Construct from full intersection Poutrmation
Foam::surfaceIntersection::surfaceIntersection
(
const triSurface& surf1,
const edgeIntersections& intersections1,
const triSurface& surf2,
const edgeIntersections& intersections2
)
:
cutPoints_(0),
cutEdges_(0),
facePairToVertex_(2*max(surf1.size(), surf2.size())),
facePairToEdge_(2*max(surf1.size(), surf2.size())),
surf1EdgeCuts_(0),
surf2EdgeCuts_(0)
{
// All intersection Pout (so for both surfaces)
DynamicList<edge> allCutEdges((surf1.nEdges() + surf2.nEdges())/20);
DynamicList<point> allCutPoints((surf1.nPoints() + surf2.nPoints())/20);
// Cut all edges of surf1 with surf2
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if (debug)
{
Pout<< "Storing surf1 intersections" << endl;
}
{
// From edge to cut index on surface1
List<DynamicList<label> > edgeCuts1(surf1.nEdges());
forAll(intersections1, edgeI)
{
const List<pointIndexHit>& intersections = intersections1[edgeI];
forAll(intersections, i)
{
const pointIndexHit& pHit = intersections[i];
// edgeI intersects surf2. Store point.
allCutPoints.append(pHit.hitPoint());
edgeCuts1[edgeI].append(allCutPoints.size()-1);
storeIntersection
(
true, // is first surface
surf1.edgeFaces()[edgeI],
pHit.index(), // surf2FaceI
allCutEdges,
allCutPoints
);
}
}
// Transfer to straight labelListList
transfer(edgeCuts1, surf1EdgeCuts_);
}
// Cut all edges of surf2 with surf1
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if (debug)
{
Pout<< "Storing surf2 intersections" << endl;
}
{
// From edge to cut index on surface2
List<DynamicList<label> > edgeCuts2(surf2.nEdges());
forAll(intersections2, edgeI)
{
const List<pointIndexHit>& intersections = intersections2[edgeI];
forAll(intersections, i)
{
const pointIndexHit& pHit = intersections[i];
// edgeI intersects surf1. Store point.
allCutPoints.append(pHit.hitPoint());
edgeCuts2[edgeI].append(allCutPoints.size()-1);
storeIntersection
(
false, // is second surface
surf2.edgeFaces()[edgeI],
pHit.index(), // surf2FaceI
allCutEdges,
allCutPoints
);
}
}
// Transfer to surf2EdgeCuts_ (straight labelListList)
transfer(edgeCuts2, surf2EdgeCuts_);
}
// Transfer to straight label(List)List
cutEdges_.transfer(allCutEdges);
cutPoints_.transfer(allCutPoints);
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
if (debug)
{
Pout<< "surfaceIntersection : Intersection generated:"
<< endl
<< " points:" << cutPoints_.size() << endl
<< " edges :" << cutEdges_.size() << endl;
Pout<< "surfaceIntersection : Writing intersection to intEdges.obj"
<< endl;
OFstream intStream("intEdges.obj");
writeOBJ(cutPoints_, cutEdges_, intStream);
// Dump all cut edges to files
Pout<< "Dumping cut edges of surface1 to surf1EdgeCuts.obj" << endl;
OFstream edge1Stream("surf1EdgeCuts.obj");
writeIntersectedEdges(surf1, surf1EdgeCuts_, edge1Stream);
Pout<< "Dumping cut edges of surface2 to surf2EdgeCuts.obj" << endl;
OFstream edge2Stream("surf2EdgeCuts.obj");
writeIntersectedEdges(surf2, surf2EdgeCuts_, edge2Stream);
}
// Debugging stuff
{
// Check all facePairToVertex is used.
labelHashSet usedPoints;
forAllConstIter(labelPairLookup, facePairToEdge_, iter)
{
label edgeI = iter();
const edge& e = cutEdges_[edgeI];
usedPoints.insert(e[0]);
usedPoints.insert(e[1]);
}
forAllConstIter(labelPairLookup, facePairToVertex_, iter)
{
label pointI = iter();
if (!usedPoints.found(pointI))
{
FatalErrorIn("surfaceIntersection::surfaceIntersection")
<< "Problem: cut point:" << pointI
<< " coord:" << cutPoints_[pointI]
<< " not used by any edge" << abort(FatalError);
}
}
}
}
// Construct from single surface. Used to test for self-intersection.
Foam::surfaceIntersection::surfaceIntersection
(