00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "triangleFuncs.H"
00027 #include <OpenFOAM/pointField.H>
00028 #include <meshTools/treeBoundBox.H>
00029 #include <OpenFOAM/SortableList.H>
00030 #include <OpenFOAM/boolList.H>
00031
00032
00033
00034 void Foam::triangleFuncs::setIntersection
00035 (
00036 const point& oppositeSidePt,
00037 const scalar oppositeSign,
00038
00039 const point& thisSidePt,
00040 const scalar thisSign,
00041
00042 const scalar tol,
00043
00044 point& pt
00045 )
00046 {
00047 scalar denom = oppositeSign - thisSign;
00048
00049 if (mag(denom) < tol)
00050 {
00051
00052 pt = oppositeSidePt;
00053 }
00054 else
00055 {
00056 pt = oppositeSidePt + oppositeSign/denom*(thisSidePt - oppositeSidePt);
00057 }
00058 }
00059
00060
00061 void Foam::triangleFuncs::selectPt
00062 (
00063 const bool select0,
00064 const point& p0,
00065 const point& p1,
00066 point& min
00067 )
00068 {
00069 if (select0)
00070 {
00071 min = p0;
00072 }
00073 else
00074 {
00075 min = p1;
00076 }
00077 }
00078
00079
00080
00081
00082
00083
00084 bool Foam::triangleFuncs::intersectAxesBundle
00085 (
00086 const point& V0,
00087 const point& V10,
00088 const point& V20,
00089 const label i0,
00090 const pointField& origin,
00091 const scalar maxLength,
00092 point& pInter
00093 )
00094 {
00095
00096
00097
00098
00099
00100 label i1 = (i0 + 1) % 3;
00101 label i2 = (i1 + 1) % 3;
00102
00103 scalar u1 = V10[i1];
00104 scalar v1 = V10[i2];
00105
00106 scalar u2 = V20[i1];
00107 scalar v2 = V20[i2];
00108
00109 scalar localScale = mag(u1)+mag(v1)+mag(u2)+mag(v2);
00110
00111 scalar det = v2*u1 - u2*v1;
00112
00113
00114
00115
00116
00117 if (localScale < VSMALL || Foam::mag(det)/localScale < SMALL)
00118 {
00119
00120 return false;
00121 }
00122
00123 forAll(origin, originI)
00124 {
00125 const point& P = origin[originI];
00126
00127 scalar u0 = P[i1] - V0[i1];
00128 scalar v0 = P[i2] - V0[i2];
00129
00130 scalar alpha = 0;
00131 scalar beta = 0;
00132 bool inter = false;
00133
00134 if (Foam::mag(u1) < ROOTVSMALL)
00135 {
00136 beta = u0/u2;
00137 if ((beta >= 0) && (beta <= 1))
00138 {
00139 alpha = (v0 - beta*v2)/v1;
00140 inter = ((alpha >= 0) && ((alpha + beta) <= 1));
00141 }
00142 }
00143 else
00144 {
00145 beta = (v0*u1 - u0*v1)/det;
00146 if ((beta >= 0) && (beta <= 1))
00147 {
00148 alpha = (u0 - beta*u2)/u1;
00149 inter = ((alpha >= 0) && ((alpha + beta) <= 1));
00150 }
00151 }
00152
00153 if (inter)
00154 {
00155 pInter = V0 + alpha*V10 + beta*V20;
00156 scalar s = (pInter - origin[originI])[i0];
00157
00158 if ((s >= 0) && (s <= maxLength))
00159 {
00160 return true;
00161 }
00162 }
00163 }
00164 return false;
00165 }
00166
00167
00168
00169
00170
00171 bool Foam::triangleFuncs::intersectBb
00172 (
00173 const point& p0,
00174 const point& p1,
00175 const point& p2,
00176 const treeBoundBox& cubeBb
00177 )
00178 {
00179 const vector p10 = p1 - p0;
00180 const vector p20 = p2 - p0;
00181
00182
00183 const point& min = cubeBb.min();
00184 const point& max = cubeBb.max();
00185
00186 const point& cube0 = min;
00187 const point cube1(min.x(), min.y(), max.z());
00188 const point cube2(max.x(), min.y(), max.z());
00189 const point cube3(max.x(), min.y(), min.z());
00190
00191 const point cube4(min.x(), max.y(), min.z());
00192 const point cube5(min.x(), max.y(), max.z());
00193 const point cube7(max.x(), max.y(), min.z());
00194
00195
00196
00197
00198
00199 point pInter;
00200 pointField origin(4);
00201
00202 origin[0] = cube0;
00203 origin[1] = cube1;
00204 origin[2] = cube5;
00205 origin[3] = cube4;
00206
00207 scalar maxSx = max.x() - min.x();
00208
00209 if (intersectAxesBundle(p0, p10, p20, 0, origin, maxSx, pInter))
00210 {
00211 return true;
00212 }
00213
00214
00215 origin[0] = cube0;
00216 origin[1] = cube1;
00217 origin[2] = cube2;
00218 origin[3] = cube3;
00219
00220 scalar maxSy = max.y() - min.y();
00221
00222 if (intersectAxesBundle(p0, p10, p20, 1, origin, maxSy, pInter))
00223 {
00224 return true;
00225 }
00226
00227
00228 origin[0] = cube0;
00229 origin[1] = cube3;
00230 origin[2] = cube7;
00231 origin[3] = cube4;
00232
00233 scalar maxSz = max.z() - min.z();
00234
00235 if (intersectAxesBundle(p0, p10, p20, 2, origin, maxSz, pInter))
00236 {
00237 return true;
00238 }
00239
00240
00241
00242 if (cubeBb.intersects(p0, p1, pInter))
00243 {
00244 return true;
00245 }
00246 if (cubeBb.intersects(p1, p2, pInter))
00247 {
00248 return true;
00249 }
00250 if (cubeBb.intersects(p2, p0, pInter))
00251 {
00252 return true;
00253 }
00254
00255 return false;
00256 }
00257
00258
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525 bool Foam::triangleFuncs::intersect
00526 (
00527 const point& va0,
00528 const point& va10,
00529 const point& va20,
00530
00531 const point& base,
00532 const point& normal,
00533
00534 point& pInter0,
00535 point& pInter1
00536 )
00537 {
00538
00539 vector na = va10 ^ va20;
00540 scalar magArea = mag(na);
00541 na/magArea;
00542
00543 if (mag(na & normal) > (1 - SMALL))
00544 {
00545
00546 return false;
00547 }
00548
00549 const point va1 = va0 + va10;
00550 const point va2 = va0 + va20;
00551
00552
00553 scalar sign0 = (va0 - base) & normal;
00554 scalar sign1 = (va1 - base) & normal;
00555 scalar sign2 = (va2 - base) & normal;
00556
00557 label oppositeVertex = -1;
00558
00559 if (sign0 < 0)
00560 {
00561 if (sign1 < 0)
00562 {
00563 if (sign2 < 0)
00564 {
00565
00566 return false;
00567 }
00568 else
00569 {
00570
00571 oppositeVertex = 2;
00572 }
00573 }
00574 else
00575 {
00576 if (sign2 < 0)
00577 {
00578
00579 oppositeVertex = 1;
00580 }
00581 else
00582 {
00583
00584 oppositeVertex = 0;
00585 }
00586 }
00587 }
00588 else
00589 {
00590 if (sign1 < 0)
00591 {
00592 if (sign2 < 0)
00593 {
00594
00595 oppositeVertex = 0;
00596 }
00597 else
00598 {
00599
00600 oppositeVertex = 1;
00601 }
00602 }
00603 else
00604 {
00605 if (sign2 < 0)
00606 {
00607
00608 oppositeVertex = 2;
00609 }
00610 else
00611 {
00612
00613 return false;
00614 }
00615 }
00616 }
00617
00618 scalar tol = SMALL*Foam::sqrt(magArea);
00619
00620 if (oppositeVertex == 0)
00621 {
00622
00623 setIntersection(va0, sign0, va1, sign1, tol, pInter0);
00624 setIntersection(va0, sign0, va2, sign2, tol, pInter1);
00625 }
00626 else if (oppositeVertex == 1)
00627 {
00628
00629 setIntersection(va1, sign1, va0, sign0, tol, pInter0);
00630 setIntersection(va1, sign1, va2, sign2, tol, pInter1);
00631 }
00632 else
00633 {
00634
00635 setIntersection(va2, sign2, va0, sign0, tol, pInter0);
00636 setIntersection(va2, sign2, va1, sign1, tol, pInter1);
00637 }
00638
00639 return true;
00640 }
00641
00642
00643 bool Foam::triangleFuncs::intersect
00644 (
00645 const point& va0,
00646 const point& va10,
00647 const point& va20,
00648
00649 const point& vb0,
00650 const point& vb10,
00651 const point& vb20,
00652
00653 point& pInter0,
00654 point& pInter1
00655 )
00656 {
00657
00658 vector na = va10 ^ va20;
00659 na/mag(na);
00660
00661 vector nb = vb10 ^ vb20;
00662 nb/mag(nb);
00663
00664
00665 point planeB0;
00666 point planeB1;
00667 if (!intersect(va0, va10, va20, vb0, nb, planeB0, planeB1))
00668 {
00669 return false;
00670 }
00671
00672
00673 point planeA0;
00674 point planeA1;
00675 if (!intersect(vb0, vb10, vb20, va0, na, planeA0, planeA1))
00676 {
00677 return false;
00678 }
00679
00680
00681
00682
00683 vector intersection(na ^ nb);
00684
00685 scalar coordB0 = planeB0 & intersection;
00686 scalar coordB1 = planeB1 & intersection;
00687
00688 scalar coordA0 = planeA0 & intersection;
00689 scalar coordA1 = planeA1 & intersection;
00690
00691
00692 List<point*> pts(4);
00693 boolList isFromB(4);
00694 SortableList<scalar> sortCoords(4);
00695
00696 pts[0] = &planeB0;
00697 isFromB[0] = true;
00698 sortCoords[0] = coordB0;
00699
00700 pts[1] = &planeB1;
00701 isFromB[1] = true;
00702 sortCoords[1] = coordB1;
00703
00704 pts[2] = &planeA0;
00705 isFromB[2] = false;
00706 sortCoords[2] = coordA0;
00707
00708 pts[3] = &planeA1;
00709 isFromB[3] = false;
00710 sortCoords[3] = coordA1;
00711
00712 sortCoords.sort();
00713
00714 const labelList& indices = sortCoords.indices();
00715
00716 if (isFromB[indices[0]] == isFromB[indices[1]])
00717 {
00718
00719
00720 return false;
00721 }
00722 else
00723 {
00724
00725 pInter0 = *pts[indices[1]];
00726 pInter1 = *pts[indices[2]];
00727
00728 return true;
00729 }
00730 }
00731
00732
00733