OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

triangleFuncs.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
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         // If almost does not cut choose one which certainly cuts.
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 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00082 
00083 // Intersect triangle with parallel edges aligned with axis i0.
00084 // Returns true (and intersection in pInter) if any of them intersects triangle.
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     // Based on Graphics Gems - Fast Ray Triangle intersection.
00097     // Since direction is coordinate axis there is no need to do projection,
00098     // we can directly check u,v components for inclusion in triangle.
00099 
00100     // Get other components
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     // Fix for  V0:(-31.71428 0 -15.10714)
00115     //          V10:(-1.285715 8.99165e-16 -1.142858)
00116     //          V20:(0 0 -1.678573)
00117     //          i0:0
00118     if (Foam::mag(det)/localScale < SMALL)
00119     {
00120         // Triangle parallel to dir
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 // Intersect triangle with bounding box. Return true if
00170 // any of the faces of bb intersect triangle.
00171 // Note: so returns false if triangle inside bb.
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     // cubeBb points; counted as if cell with vertex0 at cubeBb.min().
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     // Intersect all 12 edges of cube with triangle
00198     //
00199 
00200     point pInter;
00201     pointField origin(4);
00202     // edges in x direction
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     // edges in y direction
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     // edges in z direction
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     // Intersect triangle edges with bounding box
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 //bool Foam::triangleFuncs::intersectBbExact
00264 //(
00265 //    const point& p0,
00266 //    const point& p1,
00267 //    const point& p2,
00268 //    const treeBoundBox& cubeBb
00269 //)
00270 //{
00271 //    const point& min = cubeBb.min();
00272 //    const point& max = cubeBb.max();
00273 //
00274 //    const point& cube0 = min;
00275 //    const point  cube1(min.x(), min.y(), max.z());
00276 //    const point  cube2(max.x(), min.y(), max.z());
00277 //    const point  cube3(max.x(), min.y(), min.z());
00278 //
00279 //    const point  cube4(min.x(), max.y(), min.z());
00280 //    const point  cube5(min.x(), max.y(), max.z());
00281 //    const point& cube6 = max;
00282 //    const point  cube7(max.x(), max.y(), min.z());
00283 //
00284 //    // Test intersection of triangle with twelve edges of box.
00285 //    {
00286 //        triPointRef tri(p0, p1, p2);
00287 //        if (tri.intersectionExact(cube0, cube1).hit())
00288 //        {
00289 //            return true;
00290 //        }
00291 //        if (tri.intersectionExact(cube1, cube2).hit())
00292 //        {
00293 //            return true;
00294 //        }
00295 //        if (tri.intersectionExact(cube2, cube3).hit())
00296 //        {
00297 //            return true;
00298 //        }
00299 //        if (tri.intersectionExact(cube3, cube0).hit())
00300 //        {   
00301 //            return true;
00302 //        }
00303 //
00304 //        if (tri.intersectionExact(cube4, cube5).hit())
00305 //        {
00306 //            return true;
00307 //        }
00308 //        if (tri.intersectionExact(cube5, cube6).hit())
00309 //        {
00310 //            return true;
00311 //        }
00312 //        if (tri.intersectionExact(cube6, cube7).hit())
00313 //        {
00314 //            return true;
00315 //        }
00316 //        if (tri.intersectionExact(cube7, cube4).hit())
00317 //        {
00318 //            return true;
00319 //        }
00320 //
00321 //        if (tri.intersectionExact(cube0, cube4).hit())
00322 //        {
00323 //            return true;
00324 //        }
00325 //        if (tri.intersectionExact(cube1, cube5).hit())
00326 //        {
00327 //            return true;
00328 //        }
00329 //        if (tri.intersectionExact(cube2, cube6).hit())
00330 //        {
00331 //            return true;
00332 //        }
00333 //        if (tri.intersectionExact(cube3, cube7).hit())
00334 //        {
00335 //            return true;
00336 //        }
00337 //    }
00338 //    // Test intersection of triangle edges with bounding box
00339 //    {
00340 //        triPointRef tri(cube0, cube1, cube2);
00341 //        if (tri.intersectionExact(p0, p1).hit())
00342 //        {
00343 //            return true;
00344 //        }
00345 //        if (tri.intersectionExact(p1, p2).hit())
00346 //        {
00347 //            return true;
00348 //        }
00349 //        if (tri.intersectionExact(p2, p0).hit())
00350 //        {
00351 //            return true;
00352 //        }
00353 //    }
00354 //    {
00355 //        triPointRef tri(cube2, cube3, cube0);
00356 //        if (tri.intersectionExact(p0, p1).hit())
00357 //        {
00358 //            return true;
00359 //        }
00360 //        if (tri.intersectionExact(p1, p2).hit())
00361 //        {
00362 //            return true;
00363 //        }
00364 //        if (tri.intersectionExact(p2, p0).hit())
00365 //        {
00366 //            return true;
00367 //        }
00368 //    }
00369 //    {
00370 //        triPointRef tri(cube4, cube5, cube6);
00371 //        if (tri.intersectionExact(p0, p1).hit())
00372 //        {
00373 //            return true;
00374 //        }
00375 //        if (tri.intersectionExact(p1, p2).hit())
00376 //        {
00377 //            return true;
00378 //        }
00379 //        if (tri.intersectionExact(p2, p0).hit())
00380 //        {
00381 //            return true;
00382 //        }
00383 //    }
00384 //    {
00385 //        triPointRef tri(cube6, cube7, cube4);
00386 //        if (tri.intersectionExact(p0, p1).hit())
00387 //        {
00388 //            return true;
00389 //        }
00390 //        if (tri.intersectionExact(p1, p2).hit())
00391 //        {
00392 //            return true;
00393 //        }
00394 //        if (tri.intersectionExact(p2, p0).hit())
00395 //        {
00396 //            return true;
00397 //        }
00398 //    }
00399 //
00400 //
00401 //    {
00402 //        triPointRef tri(cube4, cube5, cube1);
00403 //        if (tri.intersectionExact(p0, p1).hit())
00404 //        {
00405 //            return true;
00406 //        }
00407 //        if (tri.intersectionExact(p1, p2).hit())
00408 //        {
00409 //            return true;
00410 //        }
00411 //        if (tri.intersectionExact(p2, p0).hit())
00412 //        {
00413 //            return true;
00414 //        }
00415 //    }
00416 //    {
00417 //        triPointRef tri(cube1, cube0, cube4);
00418 //        if (tri.intersectionExact(p0, p1).hit())
00419 //        {
00420 //            return true;
00421 //        }
00422 //        if (tri.intersectionExact(p1, p2).hit())
00423 //        {
00424 //            return true;
00425 //        }
00426 //        if (tri.intersectionExact(p2, p0).hit())
00427 //        {
00428 //            return true;
00429 //        }
00430 //    }
00431 //    {
00432 //        triPointRef tri(cube7, cube6, cube2);
00433 //        if (tri.intersectionExact(p0, p1).hit())
00434 //        {
00435 //            return true;
00436 //        }
00437 //        if (tri.intersectionExact(p1, p2).hit())
00438 //        {
00439 //            return true;
00440 //        }
00441 //        if (tri.intersectionExact(p2, p0).hit())
00442 //        {
00443 //            return true;
00444 //        }
00445 //    }
00446 //    {
00447 //        triPointRef tri(cube2, cube3, cube7);
00448 //        if (tri.intersectionExact(p0, p1).hit())
00449 //        {
00450 //            return true;
00451 //        }
00452 //        if (tri.intersectionExact(p1, p2).hit())
00453 //        {
00454 //            return true;
00455 //        }
00456 //        if (tri.intersectionExact(p2, p0).hit())
00457 //        {
00458 //            return true;
00459 //        }
00460 //    }
00461 //
00462 //    {
00463 //        triPointRef tri(cube0, cube4, cube7);
00464 //        if (tri.intersectionExact(p0, p1).hit())
00465 //        {
00466 //            return true;
00467 //        }
00468 //        if (tri.intersectionExact(p1, p2).hit())
00469 //        {
00470 //            return true;
00471 //        }
00472 //        if (tri.intersectionExact(p2, p0).hit())
00473 //        {
00474 //            return true;
00475 //        }
00476 //    }
00477 //    {
00478 //        triPointRef tri(cube7, cube3, cube0);
00479 //        if (tri.intersectionExact(p0, p1).hit())
00480 //        {
00481 //            return true;
00482 //        }
00483 //        if (tri.intersectionExact(p1, p2).hit())
00484 //        {
00485 //            return true;
00486 //        }
00487 //        if (tri.intersectionExact(p2, p0).hit())
00488 //        {
00489 //            return true;
00490 //        }
00491 //    }
00492 //    {
00493 //        triPointRef tri(cube1, cube5, cube6);
00494 //        if (tri.intersectionExact(p0, p1).hit())
00495 //        {
00496 //            return true;
00497 //        }
00498 //        if (tri.intersectionExact(p1, p2).hit())
00499 //        {
00500 //            return true;
00501 //        }
00502 //        if (tri.intersectionExact(p2, p0).hit())
00503 //        {
00504 //            return true;
00505 //        }
00506 //    }
00507 //    {
00508 //        triPointRef tri(cube6, cube2, cube1);
00509 //        if (tri.intersectionExact(p0, p1).hit())
00510 //        {
00511 //            return true;
00512 //        }
00513 //        if (tri.intersectionExact(p1, p2).hit())
00514 //        {
00515 //            return true;
00516 //        }
00517 //        if (tri.intersectionExact(p2, p0).hit())
00518 //        {
00519 //            return true;
00520 //        }
00521 //    }
00522 //    return false;
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     // Get triangle normal
00540     vector na = va10 ^ va20;
00541     scalar magArea = mag(na);
00542     na/magArea;
00543 
00544     if (mag(na & normal) > (1 - SMALL))
00545     {
00546         // Parallel
00547         return false;
00548     }
00549 
00550     const point va1 = va0 + va10;
00551     const point va2 = va0 + va20;
00552 
00553     // Find the triangle point on the other side.
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                 // All on same side of plane
00567                 return false;
00568             }
00569             else    // sign2 >= 0
00570             {
00571                 // 2 on opposite side.
00572                 oppositeVertex = 2;
00573             }
00574         }
00575         else    // sign1 >= 0
00576         {
00577             if (sign2 < 0)
00578             {
00579                 // 1 on opposite side.
00580                 oppositeVertex = 1;
00581             }
00582             else
00583             {
00584                 // 0 on opposite side.
00585                 oppositeVertex = 0;
00586             }
00587         }
00588     }
00589     else    // sign0 >= 0
00590     {
00591         if (sign1 < 0)
00592         {
00593             if (sign2 < 0)
00594             {
00595                 // 0 on opposite side.
00596                 oppositeVertex = 0;
00597             }
00598             else    // sign2 >= 0
00599             {
00600                 // 1 on opposite side.
00601                 oppositeVertex = 1;
00602             }
00603         }
00604         else    // sign1 >= 0
00605         {
00606             if (sign2 < 0)
00607             {
00608                 // 2 on opposite side.
00609                 oppositeVertex = 2;
00610             }
00611             else    // sign2 >= 0
00612             {
00613                 // All on same side of plane
00614                 return false;
00615             }
00616         }
00617     }
00618 
00619     scalar tol = SMALL*Foam::sqrt(magArea);
00620 
00621     if (oppositeVertex == 0)
00622     {
00623         // 0 on opposite side. Cut edges 01 and 02
00624         setIntersection(va0, sign0, va1, sign1, tol, pInter0);
00625         setIntersection(va0, sign0, va2, sign2, tol, pInter1);
00626     }
00627     else if (oppositeVertex == 1)
00628     {
00629         // 1 on opposite side. Cut edges 10 and 12
00630         setIntersection(va1, sign1, va0, sign0, tol, pInter0);
00631         setIntersection(va1, sign1, va2, sign2, tol, pInter1);
00632     }
00633     else // oppositeVertex == 2
00634     {
00635         // 2 on opposite side. Cut edges 20 and 21
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     // Get triangle normals
00659     vector na = va10 ^ va20;
00660     na/mag(na);
00661 
00662     vector nb = vb10 ^ vb20;
00663     nb/mag(nb);
00664 
00665     // Calculate intersection of triangle a with plane of b
00666     point planeB0;
00667     point planeB1;
00668     if (!intersect(va0, va10, va20, vb0, nb, planeB0, planeB1))
00669     {
00670         return false;
00671     }
00672 
00673     //       ,,  triangle b with plane of a
00674     point planeA0;
00675     point planeA1;
00676     if (!intersect(vb0, vb10, vb20, va0, na, planeA0, planeA1))
00677     {
00678         return false;
00679     }
00680 
00681     // Now check if intersections overlap (w.r.t. intersection of the two
00682     // planes)
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     // Put information in indexable form for sorting.
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         // Entry 0 and 1 are of same region (both a or both b). Hence that
00720         // region does not overlap.
00721         return false;
00722     }
00723     else
00724     {
00725         // Different type. Start of overlap at indices[1], end at indices[2]
00726         pInter0 = *pts[indices[1]];
00727         pInter1 = *pts[indices[2]];
00728 
00729         return true;
00730     }
00731 }
00732 
00733 
00734 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd