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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     | Website:  https://openfoam.org
    \\  /    A nd           | Copyright (C) 2011-2022 OpenFOAM Foundation
     \\/     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 3 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, see <http://www.gnu.org/licenses/>.

\*---------------------------------------------------------------------------*/

#include "triangleFuncs.H"
#include "pointField.H"
#include "treeBoundBox.H"
#include "SortableList.H"
#include "boolList.H"

// * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //

void Foam::triangleFuncs::setIntersection
(
    const point& oppositeSidePt,
    const scalar oppositeSign,

    const point& thisSidePt,
    const scalar thisSign,

    const scalar tol,

    point& pt
)
{
    scalar denom = oppositeSign - thisSign;

    if (mag(denom) < tol)
    {
        // If almost does not cut choose one which certainly cuts.
        pt = oppositeSidePt;
    }
    else
    {
        pt = oppositeSidePt + oppositeSign/denom*(thisSidePt - oppositeSidePt);
    }
}


void Foam::triangleFuncs::selectPt
(
    const bool select0,
    const point& p0,
    const point& p1,
    point& min
)
{
    if (select0)
    {
        min = p0;
    }
    else
    {
        min = p1;
    }
}


// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //

// Intersect triangle with parallel edges aligned with axis i0.
// Returns true (and intersection in pInter) if any of them intersects triangle.
bool Foam::triangleFuncs::intersectAxesBundle
(
    const point& V0,
    const point& V10,
    const point& V20,
    const label i0,
    const pointField& origin,
    const scalar maxLength,
    point& pInter
)
{
    // Based on Graphics Gems - Fast Ray Triangle intersection.
    // Since direction is coordinate axis there is no need to do projection,
    // we can directly check u,v components for inclusion in triangle.

    // Get other components
    const label i1 = (i0 + 1) % 3;
    const label i2 = (i1 + 1) % 3;

    const scalar u1 = V10[i1];
    const scalar v1 = V10[i2];

    const scalar u2 = V20[i1];
    const scalar v2 = V20[i2];

    const scalar localScale = mag(u1) + mag(v1) + mag(u2) + mag(v2);

    const scalar det = v2*u1 - u2*v1;

    // Fix for  V0:(-31.71428 0 -15.10714)
    //          V10:(-1.285715 8.99165e-16 -1.142858)
    //          V20:(0 0 -1.678573)
    //          i0:0
    if (localScale < vSmall || Foam::mag(det)/localScale < small)
    {
        // Triangle parallel to dir
        return false;
    }

    forAll(origin, originI)
    {
        const point& P = origin[originI];

        const scalar u0 = P[i1] - V0[i1];
        const scalar v0 = P[i2] - V0[i2];

        scalar alpha = 0;
        scalar beta = 0;
        bool inter = false;

        if (mag(u1) < rootVSmall)
        {
            beta = u0/u2;
            if ((beta >= 0) && (beta <= 1))
            {
                alpha = (v0 - beta*v2)/v1;
                inter = ((alpha >= 0) && ((alpha + beta) <= 1));
            }
        }
        else
        {
            beta = (v0*u1 - u0*v1)/det;
            if ((beta >= 0) && (beta <= 1))
            {
                alpha = (u0 - beta*u2)/u1;
                inter = ((alpha >= 0) && ((alpha + beta) <= 1));
            }
        }

        if (inter)
        {
            pInter = V0 + alpha*V10 + beta*V20;
            const scalar s = pInter[i0] - P[i0];

            if ((s >= 0) && (s <= maxLength))
            {
                return true;
            }
        }
    }

    return false;
}


// Intersect triangle with bounding box. Return true if
// any of the faces of bb intersect triangle.
// Note: so returns false if triangle inside bb.
bool Foam::triangleFuncs::intersectBb
(
    const point& p0,
    const point& p1,
    const point& p2,
    const treeBoundBox& cubeBb
)
{
    const vector p10 = p1 - p0;
    const vector p20 = p2 - p0;

    // cubeBb points; counted as if cell with vertex0 at cubeBb.min().
    const point& min = cubeBb.min();
    const point& max = cubeBb.max();

    const point& cube0 = min;
    const point  cube1(min.x(), min.y(), max.z());
    const point  cube2(max.x(), min.y(), max.z());
    const point  cube3(max.x(), min.y(), min.z());

    const point  cube4(min.x(), max.y(), min.z());
    const point  cube5(min.x(), max.y(), max.z());
    const point  cube7(max.x(), max.y(), min.z());

    //
    // Intersect all 12 edges of cube with triangle
    //

    point pInter;
    pointField origin(4);
    // edges in x direction
    origin[0] = cube0;
    origin[1] = cube1;
    origin[2] = cube5;
    origin[3] = cube4;

    scalar maxSx = max.x() - min.x();

    if (intersectAxesBundle(p0, p10, p20, 0, origin, maxSx, pInter))
    {
        return true;
    }

    // edges in y direction
    origin[0] = cube0;
    origin[1] = cube1;
    origin[2] = cube2;
    origin[3] = cube3;

    scalar maxSy = max.y() - min.y();

    if (intersectAxesBundle(p0, p10, p20, 1, origin, maxSy, pInter))
    {
        return true;
    }

    // edges in z direction
    origin[0] = cube0;
    origin[1] = cube3;
    origin[2] = cube7;
    origin[3] = cube4;

    scalar maxSz = max.z() - min.z();

    if (intersectAxesBundle(p0, p10, p20, 2, origin, maxSz, pInter))
    {
        return true;
    }


    // Intersect triangle edges with bounding box
    if (cubeBb.intersects(p0, p1, pInter))
    {
        return true;
    }
    if (cubeBb.intersects(p1, p2, pInter))
    {
        return true;
    }
    if (cubeBb.intersects(p2, p0, pInter))
    {
        return true;
    }

    return false;
}


//// Intersect triangle with bounding box. Return true if
//// any of the faces of bb intersect triangle.
//// Note: so returns false if triangle inside bb.
//bool Foam::triangleFuncs::intersectBbExact
//(
//    const point& p0,
//    const point& p1,
//    const point& p2,
//    const treeBoundBox& cubeBb
//)
//{
//    const point& min = cubeBb.min();
//    const point& max = cubeBb.max();
//
//    const point& cube0 = min;
//    const point  cube1(min.x(), min.y(), max.z());
//    const point  cube2(max.x(), min.y(), max.z());
//    const point  cube3(max.x(), min.y(), min.z());
//
//    const point  cube4(min.x(), max.y(), min.z());
//    const point  cube5(min.x(), max.y(), max.z());
//    const point& cube6 = max;
//    const point  cube7(max.x(), max.y(), min.z());
//
//    // Test intersection of triangle with twelve edges of box.
//    {
//        triPointRef tri(p0, p1, p2);
//        if (tri.intersectionExact(cube0, cube1).hit())
//        {
//            return true;
//        }
//        if (tri.intersectionExact(cube1, cube2).hit())
//        {
//            return true;
//        }
//        if (tri.intersectionExact(cube2, cube3).hit())
//        {
//            return true;
//        }
//        if (tri.intersectionExact(cube3, cube0).hit())
//        {
//            return true;
//        }
//
//        if (tri.intersectionExact(cube4, cube5).hit())
//        {
//            return true;
//        }
//        if (tri.intersectionExact(cube5, cube6).hit())
//        {
//            return true;
//        }
//        if (tri.intersectionExact(cube6, cube7).hit())
//        {
//            return true;
//        }
//        if (tri.intersectionExact(cube7, cube4).hit())
//        {
//            return true;
//        }
//
//        if (tri.intersectionExact(cube0, cube4).hit())
//        {
//            return true;
//        }
//        if (tri.intersectionExact(cube1, cube5).hit())
//        {
//            return true;
//        }
//        if (tri.intersectionExact(cube2, cube6).hit())
//        {
//            return true;
//        }
//        if (tri.intersectionExact(cube3, cube7).hit())
//        {
//            return true;
//        }
//    }
//    // Test intersection of triangle edges with bounding box
//    {
//        triPointRef tri(cube0, cube1, cube2);
//        if (tri.intersectionExact(p0, p1).hit())
//        {
//            return true;
//        }
//        if (tri.intersectionExact(p1, p2).hit())
//        {
//            return true;
//        }
//        if (tri.intersectionExact(p2, p0).hit())
//        {
//            return true;
//        }
//    }
//    {
//        triPointRef tri(cube2, cube3, cube0);
//        if (tri.intersectionExact(p0, p1).hit())
//        {
//            return true;
//        }
//        if (tri.intersectionExact(p1, p2).hit())
//        {
//            return true;
//        }
//        if (tri.intersectionExact(p2, p0).hit())
//        {
//            return true;
//        }
//    }
//    {
//        triPointRef tri(cube4, cube5, cube6);
//        if (tri.intersectionExact(p0, p1).hit())
//        {
//            return true;
//        }
//        if (tri.intersectionExact(p1, p2).hit())
//        {
//            return true;
//        }
//        if (tri.intersectionExact(p2, p0).hit())
//        {
//            return true;
//        }
//    }
//    {
//        triPointRef tri(cube6, cube7, cube4);
//        if (tri.intersectionExact(p0, p1).hit())
//        {
//            return true;
//        }
//        if (tri.intersectionExact(p1, p2).hit())
//        {
//            return true;
//        }
//        if (tri.intersectionExact(p2, p0).hit())
//        {
//            return true;
//        }
//    }
//
//
//    {
//        triPointRef tri(cube4, cube5, cube1);
//        if (tri.intersectionExact(p0, p1).hit())
//        {
//            return true;
//        }
//        if (tri.intersectionExact(p1, p2).hit())
//        {
//            return true;
//        }
//        if (tri.intersectionExact(p2, p0).hit())
//        {
//            return true;
//        }
//    }
//    {
//        triPointRef tri(cube1, cube0, cube4);
//        if (tri.intersectionExact(p0, p1).hit())
//        {
//            return true;
//        }
//        if (tri.intersectionExact(p1, p2).hit())
//        {
//            return true;
//        }
//        if (tri.intersectionExact(p2, p0).hit())
//        {
//            return true;
//        }
//    }
//    {
//        triPointRef tri(cube7, cube6, cube2);
//        if (tri.intersectionExact(p0, p1).hit())
//        {
//            return true;
//        }
//        if (tri.intersectionExact(p1, p2).hit())
//        {
//            return true;
//        }
//        if (tri.intersectionExact(p2, p0).hit())
//        {
//            return true;
//        }
//    }
//    {
//        triPointRef tri(cube2, cube3, cube7);
//        if (tri.intersectionExact(p0, p1).hit())
//        {
//            return true;
//        }
//        if (tri.intersectionExact(p1, p2).hit())
//        {
//            return true;
//        }
//        if (tri.intersectionExact(p2, p0).hit())
//        {
//            return true;
//        }
//    }
//
//    {
//        triPointRef tri(cube0, cube4, cube7);
//        if (tri.intersectionExact(p0, p1).hit())
//        {
//            return true;
//        }
//        if (tri.intersectionExact(p1, p2).hit())
//        {
//            return true;
//        }
//        if (tri.intersectionExact(p2, p0).hit())
//        {
//            return true;
//        }
//    }
//    {
//        triPointRef tri(cube7, cube3, cube0);
//        if (tri.intersectionExact(p0, p1).hit())
//        {
//            return true;
//        }
//        if (tri.intersectionExact(p1, p2).hit())
//        {
//            return true;
//        }
//        if (tri.intersectionExact(p2, p0).hit())
//        {
//            return true;
//        }
//    }
//    {
//        triPointRef tri(cube1, cube5, cube6);
//        if (tri.intersectionExact(p0, p1).hit())
//        {
//            return true;
//        }
//        if (tri.intersectionExact(p1, p2).hit())
//        {
//            return true;
//        }
//        if (tri.intersectionExact(p2, p0).hit())
//        {
//            return true;
//        }
//    }
//    {
//        triPointRef tri(cube6, cube2, cube1);
//        if (tri.intersectionExact(p0, p1).hit())
//        {
//            return true;
//        }
//        if (tri.intersectionExact(p1, p2).hit())
//        {
//            return true;
//        }
//        if (tri.intersectionExact(p2, p0).hit())
//        {
//            return true;
//        }
//    }
//    return false;
//}


bool Foam::triangleFuncs::intersect
(
    const point& va0,
    const point& va10,
    const point& va20,

    const point& base,
    const point& normal,

    point& pInter0,
    point& pInter1
)
{
    // Get triangle normal
    vector na = va10 ^ va20;
    scalar magArea = mag(na);
    na/magArea;

    if (mag(na & normal) > (1 - small))
    {
        // Parallel
        return false;
    }

    const point va1 = va0 + va10;
    const point va2 = va0 + va20;

    // Find the triangle point on the other side.
    scalar sign0 = (va0 - base) & normal;
    scalar sign1 = (va1 - base) & normal;
    scalar sign2 = (va2 - base) & normal;

    label oppositeVertex = -1;

    if (sign0 < 0)
    {
        if (sign1 < 0)
        {
            if (sign2 < 0)
            {
                // All on same side of plane
                return false;
            }
            else    // sign2 >= 0
            {
                // 2 on opposite side.
                oppositeVertex = 2;
            }
        }
        else    // sign1 >= 0
        {
            if (sign2 < 0)
            {
                // 1 on opposite side.
                oppositeVertex = 1;
            }
            else
            {
                // 0 on opposite side.
                oppositeVertex = 0;
            }
        }
    }
    else    // sign0 >= 0
    {
        if (sign1 < 0)
        {
            if (sign2 < 0)
            {
                // 0 on opposite side.
                oppositeVertex = 0;
            }
            else    // sign2 >= 0
            {
                // 1 on opposite side.
                oppositeVertex = 1;
            }
        }
        else    // sign1 >= 0
        {
            if (sign2 < 0)
            {
                // 2 on opposite side.
                oppositeVertex = 2;
            }
            else    // sign2 >= 0
            {
                // All on same side of plane
                return false;
            }
        }
    }

    scalar tol = small*Foam::sqrt(magArea);

    if (oppositeVertex == 0)
    {
        // 0 on opposite side. Cut edges 01 and 02
        setIntersection(va0, sign0, va1, sign1, tol, pInter0);
        setIntersection(va0, sign0, va2, sign2, tol, pInter1);
    }
    else if (oppositeVertex == 1)
    {
        // 1 on opposite side. Cut edges 10 and 12
        setIntersection(va1, sign1, va0, sign0, tol, pInter0);
        setIntersection(va1, sign1, va2, sign2, tol, pInter1);
    }
    else // oppositeVertex == 2
    {
        // 2 on opposite side. Cut edges 20 and 21
        setIntersection(va2, sign2, va0, sign0, tol, pInter0);
        setIntersection(va2, sign2, va1, sign1, tol, pInter1);
    }

    return true;
}


bool Foam::triangleFuncs::intersect
(
    const point& va0,
    const point& va10,
    const point& va20,

    const point& vb0,
    const point& vb10,
    const point& vb20,

    point& pInter0,
    point& pInter1
)
{
    // Get triangle normals
    vector na = va10 ^ va20;
    na/mag(na);

    vector nb = vb10 ^ vb20;
    nb/mag(nb);

    // Calculate intersection of triangle a with plane of b
    point planeB0;
    point planeB1;
    if (!intersect(va0, va10, va20, vb0, nb, planeB0, planeB1))
    {
        return false;
    }

    //       ,,  triangle b with plane of a
    point planeA0;
    point planeA1;
    if (!intersect(vb0, vb10, vb20, va0, na, planeA0, planeA1))
    {
        return false;
    }

    // Now check if intersections overlap (w.r.t. intersection of the two
    // planes)

    vector intersection(na ^ nb);

    scalar coordB0 = planeB0 & intersection;
    scalar coordB1 = planeB1 & intersection;

    scalar coordA0 = planeA0 & intersection;
    scalar coordA1 = planeA1 & intersection;

    // Put information in indexable form for sorting.
    List<point*> pts(4);
    boolList isFromB(4);
    SortableList<scalar> sortCoords(4);

    pts[0] = &planeB0;
    isFromB[0] = true;
    sortCoords[0] = coordB0;

    pts[1] = &planeB1;
    isFromB[1] = true;
    sortCoords[1] = coordB1;

    pts[2] = &planeA0;
    isFromB[2] = false;
    sortCoords[2] = coordA0;

    pts[3] = &planeA1;
    isFromB[3] = false;
    sortCoords[3] = coordA1;

    sortCoords.sort();

    const labelList& indices = sortCoords.indices();

    if (isFromB[indices[0]] == isFromB[indices[1]])
    {
        // Entry 0 and 1 are of same region (both a or both b). Hence that
        // region does not overlap.
        return false;
    }
    else
    {
        // Different type. Start of overlap at indices[1], end at indices[2]
        pInter0 = *pts[indices[1]];
        pInter1 = *pts[indices[2]];

        return true;
    }
}


// ************************************************************************* //