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
00027 #include "triangleFuncs.H"
00028 #include "pointField.H"
00029 #include "treeBoundBox.H"
00030 #include "SortableList.H"
00031 #include "boolList.H"
00032
00033
00034
00035 void Foam::triangleFuncs::setIntersection
00036 (
00037 const point& oppositeSidePt,
00038 const scalar oppositeSign,
00039
00040 const point& thisSidePt,
00041 const scalar thisSign,
00042
00043 const scalar tol,
00044
00045 point& pt
00046 )
00047 {
00048 scalar denom = oppositeSign - thisSign;
00049
00050 if (mag(denom) < tol)
00051 {
00052
00053 pt = oppositeSidePt;
00054 }
00055 else
00056 {
00057 pt = oppositeSidePt + oppositeSign/denom*(thisSidePt - oppositeSidePt);
00058 }
00059 }
00060
00061
00062 void Foam::triangleFuncs::selectPt
00063 (
00064 const bool select0,
00065 const point& p0,
00066 const point& p1,
00067 point& min
00068 )
00069 {
00070 if (select0)
00071 {
00072 min = p0;
00073 }
00074 else
00075 {
00076 min = p1;
00077 }
00078 }
00079
00080
00081
00082
00083
00084
00085 bool Foam::triangleFuncs::intersectAxesBundle
00086 (
00087 const point& V0,
00088 const point& V10,
00089 const point& V20,
00090 const label i0,
00091 const pointField& origin,
00092 const scalar maxLength,
00093 point& pInter
00094 )
00095 {
00096
00097
00098
00099
00100
00101 label i1 = (i0 + 1) % 3;
00102 label i2 = (i1 + 1) % 3;
00103
00104 scalar u1 = V10[i1];
00105 scalar v1 = V10[i2];
00106
00107 scalar u2 = V20[i1];
00108 scalar v2 = V20[i2];
00109
00110 scalar localScale = mag(u1)+mag(v1)+mag(u2)+mag(v2);
00111
00112 scalar det = v2*u1 - u2*v1;
00113
00114
00115
00116
00117
00118 if (Foam::mag(det)/localScale < SMALL)
00119 {
00120
00121 return false;
00122 }
00123
00124 forAll(origin, originI)
00125 {
00126 const point& P = origin[originI];
00127
00128 scalar u0 = P[i1] - V0[i1];
00129 scalar v0 = P[i2] - V0[i2];
00130
00131 scalar alpha = 0;
00132 scalar beta = 0;
00133 bool inter = false;
00134
00135 if (Foam::mag(u1) < ROOTVSMALL)
00136 {
00137 beta = u0/u2;
00138 if ((beta >= 0) && (beta <= 1))
00139 {
00140 alpha = (v0 - beta*v2)/v1;
00141 inter = ((alpha >= 0) && ((alpha + beta) <= 1));
00142 }
00143 }
00144 else
00145 {
00146 beta = (v0*u1 - u0*v1)/det;
00147 if ((beta >= 0) && (beta <= 1))
00148 {
00149 alpha = (u0 - beta*u2)/u1;
00150 inter = ((alpha >= 0) && ((alpha + beta) <= 1));
00151 }
00152 }
00153
00154 if (inter)
00155 {
00156 pInter = V0 + alpha*V10 + beta*V20;
00157 scalar s = (pInter - origin[originI])[i0];
00158
00159 if ((s >= 0) && (s <= maxLength))
00160 {
00161 return true;
00162 }
00163 }
00164 }
00165 return false;
00166 }
00167
00168
00169
00170
00171
00172 bool Foam::triangleFuncs::intersectBb
00173 (
00174 const point& p0,
00175 const point& p1,
00176 const point& p2,
00177 const treeBoundBox& cubeBb
00178 )
00179 {
00180 const vector p10 = p1 - p0;
00181 const vector p20 = p2 - p0;
00182
00183
00184 const point& min = cubeBb.min();
00185 const point& max = cubeBb.max();
00186
00187 const point& cube0 = min;
00188 const point cube1(min.x(), min.y(), max.z());
00189 const point cube2(max.x(), min.y(), max.z());
00190 const point cube3(max.x(), min.y(), min.z());
00191
00192 const point cube4(min.x(), max.y(), min.z());
00193 const point cube5(min.x(), max.y(), max.z());
00194 const point cube7(max.x(), max.y(), min.z());
00195
00196
00197
00198
00199
00200 point pInter;
00201 pointField origin(4);
00202
00203 origin[0] = cube0;
00204 origin[1] = cube1;
00205 origin[2] = cube5;
00206 origin[3] = cube4;
00207
00208 scalar maxSx = max.x() - min.x();
00209
00210 if (intersectAxesBundle(p0, p10, p20, 0, origin, maxSx, pInter))
00211 {
00212 return true;
00213 }
00214
00215
00216 origin[0] = cube0;
00217 origin[1] = cube1;
00218 origin[2] = cube2;
00219 origin[3] = cube3;
00220
00221 scalar maxSy = max.y() - min.y();
00222
00223 if (intersectAxesBundle(p0, p10, p20, 1, origin, maxSy, pInter))
00224 {
00225 return true;
00226 }
00227
00228
00229 origin[0] = cube0;
00230 origin[1] = cube3;
00231 origin[2] = cube7;
00232 origin[3] = cube4;
00233
00234 scalar maxSz = max.z() - min.z();
00235
00236 if (intersectAxesBundle(p0, p10, p20, 2, origin, maxSz, pInter))
00237 {
00238 return true;
00239 }
00240
00241
00242
00243 if (cubeBb.intersects(p0, p1, pInter))
00244 {
00245 return true;
00246 }
00247 if (cubeBb.intersects(p1, p2, pInter))
00248 {
00249 return true;
00250 }
00251 if (cubeBb.intersects(p2, p0, pInter))
00252 {
00253 return true;
00254 }
00255
00256 return false;
00257 }
00258
00259
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
00526 bool Foam::triangleFuncs::intersect
00527 (
00528 const point& va0,
00529 const point& va10,
00530 const point& va20,
00531
00532 const point& base,
00533 const point& normal,
00534
00535 point& pInter0,
00536 point& pInter1
00537 )
00538 {
00539
00540 vector na = va10 ^ va20;
00541 scalar magArea = mag(na);
00542 na/magArea;
00543
00544 if (mag(na & normal) > (1 - SMALL))
00545 {
00546
00547 return false;
00548 }
00549
00550 const point va1 = va0 + va10;
00551 const point va2 = va0 + va20;
00552
00553
00554 scalar sign0 = (va0 - base) & normal;
00555 scalar sign1 = (va1 - base) & normal;
00556 scalar sign2 = (va2 - base) & normal;
00557
00558 label oppositeVertex = -1;
00559
00560 if (sign0 < 0)
00561 {
00562 if (sign1 < 0)
00563 {
00564 if (sign2 < 0)
00565 {
00566
00567 return false;
00568 }
00569 else
00570 {
00571
00572 oppositeVertex = 2;
00573 }
00574 }
00575 else
00576 {
00577 if (sign2 < 0)
00578 {
00579
00580 oppositeVertex = 1;
00581 }
00582 else
00583 {
00584
00585 oppositeVertex = 0;
00586 }
00587 }
00588 }
00589 else
00590 {
00591 if (sign1 < 0)
00592 {
00593 if (sign2 < 0)
00594 {
00595
00596 oppositeVertex = 0;
00597 }
00598 else
00599 {
00600
00601 oppositeVertex = 1;
00602 }
00603 }
00604 else
00605 {
00606 if (sign2 < 0)
00607 {
00608
00609 oppositeVertex = 2;
00610 }
00611 else
00612 {
00613
00614 return false;
00615 }
00616 }
00617 }
00618
00619 scalar tol = SMALL*Foam::sqrt(magArea);
00620
00621 if (oppositeVertex == 0)
00622 {
00623
00624 setIntersection(va0, sign0, va1, sign1, tol, pInter0);
00625 setIntersection(va0, sign0, va2, sign2, tol, pInter1);
00626 }
00627 else if (oppositeVertex == 1)
00628 {
00629
00630 setIntersection(va1, sign1, va0, sign0, tol, pInter0);
00631 setIntersection(va1, sign1, va2, sign2, tol, pInter1);
00632 }
00633 else
00634 {
00635
00636 setIntersection(va2, sign2, va0, sign0, tol, pInter0);
00637 setIntersection(va2, sign2, va1, sign1, tol, pInter1);
00638 }
00639
00640 return true;
00641 }
00642
00643
00644 bool Foam::triangleFuncs::intersect
00645 (
00646 const point& va0,
00647 const point& va10,
00648 const point& va20,
00649
00650 const point& vb0,
00651 const point& vb10,
00652 const point& vb20,
00653
00654 point& pInter0,
00655 point& pInter1
00656 )
00657 {
00658
00659 vector na = va10 ^ va20;
00660 na/mag(na);
00661
00662 vector nb = vb10 ^ vb20;
00663 nb/mag(nb);
00664
00665
00666 point planeB0;
00667 point planeB1;
00668 if (!intersect(va0, va10, va20, vb0, nb, planeB0, planeB1))
00669 {
00670 return false;
00671 }
00672
00673
00674 point planeA0;
00675 point planeA1;
00676 if (!intersect(vb0, vb10, vb20, va0, na, planeA0, planeA1))
00677 {
00678 return false;
00679 }
00680
00681
00682
00683
00684 vector intersection(na ^ nb);
00685
00686 scalar coordB0 = planeB0 & intersection;
00687 scalar coordB1 = planeB1 & intersection;
00688
00689 scalar coordA0 = planeA0 & intersection;
00690 scalar coordA1 = planeA1 & intersection;
00691
00692
00693 List<point*> pts(4);
00694 boolList isFromB(4);
00695 SortableList<scalar> sortCoords(4);
00696
00697 pts[0] = &planeB0;
00698 isFromB[0] = true;
00699 sortCoords[0] = coordB0;
00700
00701 pts[1] = &planeB1;
00702 isFromB[1] = true;
00703 sortCoords[1] = coordB1;
00704
00705 pts[2] = &planeA0;
00706 isFromB[2] = false;
00707 sortCoords[2] = coordA0;
00708
00709 pts[3] = &planeA1;
00710 isFromB[3] = false;
00711 sortCoords[3] = coordA1;
00712
00713 sortCoords.sort();
00714
00715 const labelList& indices = sortCoords.indices();
00716
00717 if (isFromB[indices[0]] == isFromB[indices[1]])
00718 {
00719
00720
00721 return false;
00722 }
00723 else
00724 {
00725
00726 pInter0 = *pts[indices[1]];
00727 pInter1 = *pts[indices[2]];
00728
00729 return true;
00730 }
00731 }
00732
00733
00734