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

triangleI.H

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 "IOstreams.H"
00028 #include "pointHit.H"
00029 #include "mathematicalConstants.H"
00030 
00031 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00032 
00033 namespace Foam
00034 {
00035 
00036 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00037 
00038 template<class Point, class PointRef>
00039 pointHit triangle<Point, PointRef>::nearestPoint
00040 (
00041     const Point& baseVertex,
00042     const vector& E0,
00043     const vector& E1,
00044     const point& P
00045 )
00046 {
00047     // Distance vector
00048     const vector D(baseVertex - P);
00049 
00050     // Some geometrical factors
00051     const scalar a = E0 & E0;
00052     const scalar b = E0 & E1;
00053     const scalar c = E1 & E1;
00054 
00055     // Precalculate distance factors
00056     const scalar d = E0 & D;
00057     const scalar e = E1 & D;
00058     const scalar f = D & D;
00059 
00060     // Do classification
00061     const scalar det = a*c - b*b;
00062     scalar s = b*e - c*d;
00063     scalar t = b*d - a*e;
00064 
00065     bool inside = false;
00066 
00067     if (s+t < det)
00068     {
00069         if (s < 0)
00070         {
00071             if (t < 0)
00072             {
00073                 // Region 4
00074                 if (e > 0)
00075                 {
00076                     // min on edge t = 0
00077                     t = 0;
00078                     s = (d >= 0 ? 0 : (-d >= a ? 1 : -d/a));
00079                 }
00080                 else
00081                 {
00082                     // min on edge s=0
00083                     s = 0;
00084                     t = (e >= 0 ? 0 : (-e >= c ? 1 : -e/c));
00085                 }
00086             }
00087             else
00088             {
00089                 // Region 3. Min on edge s = 0
00090                 s = 0;
00091                 t = (e >= 0 ? 0 : (-e >= c ? 1 : -e/c));
00092             }
00093         }
00094         else if (t < 0)
00095         {
00096             // Region 5
00097             t = 0;
00098             s = (d >= 0 ? 0 : (-d >= a ? 1 : -d/a));
00099         }
00100         else
00101         {
00102             // Region 0
00103             const scalar invDet = 1/det;
00104             s *= invDet;
00105             t *= invDet;
00106 
00107             inside = true;
00108         }
00109     }
00110     else
00111     {
00112         if (s < 0)
00113         {
00114             // Region 2
00115             const scalar tmp0 = b + d;
00116             const scalar tmp1 = c + e;
00117             if (tmp1 > tmp0)
00118             {
00119                 // min on edge s+t=1
00120                 const scalar numer = tmp1 - tmp0;
00121                 const scalar denom = a-2*b+c;
00122                 s = (numer >= denom ? 1 : numer/denom);
00123                 t = 1 - s;
00124             }
00125             else
00126             {
00127                 // min on edge s=0
00128                 s = 0;
00129                 t = (tmp1 <= 0 ? 1 : (e >= 0 ? 0 : - e/c));
00130             }
00131         }
00132         else if (t < 0)
00133         {
00134             // Region 6
00135             const scalar tmp0 = b + d;
00136             const scalar tmp1 = c + e;
00137             if (tmp1 > tmp0)
00138             {
00139                 // min on edge s+t=1
00140                 const scalar numer = tmp1 - tmp0;
00141                 const scalar denom = a-2*b+c;
00142                 s = (numer >= denom ? 1 : numer/denom);
00143                 t = 1 - s;
00144             }
00145             else
00146             {
00147                 // min on edge t=0
00148                 t = 0;
00149                 s = (tmp1 <= 0 ? 1 : (d >= 0 ? 0 : - d/a));
00150             }
00151         }
00152         else
00153         {
00154             // Region 1
00155             const scalar numer = c+e-(b+d);
00156             if (numer <= 0)
00157             {
00158                 s = 0;
00159             }
00160             else
00161             {
00162                 const scalar denom = a-2*b+c;
00163                 s = (numer >= denom ? 1 : numer/denom);
00164             }
00165         }
00166 
00167         t = 1 - s;
00168     }
00169 
00170     // Calculate distance.
00171     // Note: Foam::mag used since truncation error causes negative distances
00172     // with points very close to one of the triangle vertices.
00173     // (Up to -2.77556e-14 seen). Could use +SMALL but that not large enough.
00174 
00175     return pointHit
00176     (
00177         inside,
00178         baseVertex + s*E0 + t*E1,
00179         Foam::sqrt
00180         (
00181             Foam::mag(a*s*s + 2*b*s*t + c*t*t + 2*d*s + 2*e*t + f)
00182         ),
00183         !inside
00184     );
00185 }
00186 
00187 
00188 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
00189 
00190 template<class Point, class PointRef>
00191 inline triangle<Point, PointRef>::triangle
00192 (
00193     const Point& a,
00194     const Point& b,
00195     const Point& c
00196 )
00197 :
00198     a_(a),
00199     b_(b),
00200     c_(c)
00201 {}
00202 
00203 
00204 template<class Point, class PointRef>
00205 inline triangle<Point, PointRef>::triangle(Istream& is)
00206 {
00207     // Read beginning of triangle point pair
00208     is.readBegin("triangle");
00209 
00210     is >> a_ >> b_ >> c_;
00211 
00212     // Read end of triangle point pair
00213     is.readEnd("triangle");
00214 
00215     // Check state of Istream
00216     is.check("triangle::triangle(Istream& is)");
00217 }
00218 
00219 
00220 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00221 
00222 template<class Point, class PointRef>
00223 inline const Point& triangle<Point, PointRef>::a() const
00224 {
00225     return a_;
00226 }
00227 
00228 template<class Point, class PointRef>
00229 inline const Point& triangle<Point, PointRef>::b() const
00230 {
00231     return b_;
00232 }
00233 
00234 template<class Point, class PointRef>
00235 inline const Point& triangle<Point, PointRef>::c() const
00236 {
00237     return c_;
00238 }
00239 
00240 
00241 template<class Point, class PointRef>
00242 inline Point triangle<Point, PointRef>::centre() const
00243 {
00244     return (1.0/3.0)*(a_ + b_ + c_);
00245 }
00246 
00247 
00248 template<class Point, class PointRef>
00249 inline scalar triangle<Point, PointRef>::mag() const
00250 {
00251     return ::Foam::mag(normal());
00252 }
00253 
00254 
00255 template<class Point, class PointRef>
00256 inline vector triangle<Point, PointRef>::normal() const
00257 {
00258     return 0.5*((b_ - a_)^(c_ - a_));
00259 }
00260 
00261 
00262 template<class Point, class PointRef>
00263 inline vector triangle<Point, PointRef>::circumCentre() const
00264 {
00265     scalar d1 = (c_ - a_)&(b_ - a_);
00266     scalar d2 = -(c_ - b_)&(b_ - a_);
00267     scalar d3 = (c_ - a_)&(c_ - b_);
00268 
00269     scalar c1 = d2*d3;
00270     scalar c2 = d3*d1;
00271     scalar c3 = d1*d2;
00272 
00273     scalar c = c1 + c2 + c3;
00274 
00275     return
00276     (
00277         ((c2 + c3)*a_ + (c3 + c1)*b_ + (c1 + c2)*c_)/(2*c)
00278     );
00279 }
00280 
00281 
00282 template<class Point, class PointRef>
00283 inline scalar triangle<Point, PointRef>::circumRadius() const
00284 {
00285     scalar d1 = (c_ - a_) & (b_ - a_);
00286     scalar d2 = - (c_ - b_) & (b_ - a_);
00287     scalar d3 = (c_ - a_) & (c_ - b_);
00288 
00289     scalar denom = d2*d3 + d3*d1 + d1*d2;
00290 
00291     if (Foam::mag(denom) < VSMALL)
00292     {
00293         return GREAT;
00294     }
00295     else
00296     {
00297         scalar a = (d1 + d2)*(d2 + d3)*(d3 + d1) / denom;
00298 
00299         return 0.5*Foam::sqrt(min(GREAT, max(0, a)));
00300     }
00301 }
00302 
00303 
00304 template<class Point, class PointRef>
00305 inline scalar triangle<Point, PointRef>::quality() const
00306 {
00307     return
00308         mag()
00309       / (
00310             mathematicalConstant::pi
00311           * Foam::sqr(circumRadius())
00312           * 0.413497
00313           + VSMALL
00314         );
00315 }
00316 
00317 
00318 template<class Point, class PointRef>
00319 inline scalar triangle<Point, PointRef>::sweptVol(const triangle& t) const
00320 {
00321     return (1.0/12.0)*
00322     (
00323         ((t.a_ - a_) & ((b_ - a_)^(c_ - a_)))
00324       + ((t.b_ - b_) & ((c_ - b_)^(t.a_ - b_)))
00325       + ((c_ - t.c_) & ((t.b_ - t.c_)^(t.a_ - t.c_)))
00326 
00327       + ((t.a_ - a_) & ((b_ - a_)^(c_ - a_)))
00328       + ((b_ - t.b_) & ((t.a_ - t.b_)^(t.c_ - t.b_)))
00329       + ((c_ - t.c_) & ((b_ - t.c_)^(t.a_ - t.c_)))
00330     );
00331 }
00332 
00333 
00334 template<class Point, class PointRef>
00335 inline pointHit triangle<Point, PointRef>::ray
00336 (
00337     const point& p,
00338     const vector& q,
00339     const intersection::algorithm alg,
00340     const intersection::direction dir
00341 ) const
00342 {
00343     // Express triangle in terms of baseVertex (point a_) and
00344     // two edge vectors
00345     vector E0 = b_ - a_;
00346     vector E1 = c_ - a_;
00347 
00348     // Initialize intersection to miss.
00349     pointHit inter(p);
00350 
00351     vector n(0.5*(E0 ^ E1));
00352     scalar area = Foam::mag(n);
00353 
00354     if (area < VSMALL)
00355     {
00356         // Ineligible miss.
00357         inter.setMiss(false);
00358 
00359         // The miss point is the nearest point on the triangle. Take any one.
00360         inter.setPoint(a_);
00361 
00362         // The distance to the miss is the distance between the
00363         // original point and plane of intersection. No normal so use
00364         // distance from p to a_
00365         inter.setDistance(Foam::mag(a_ - p));
00366 
00367         return inter;
00368     }
00369 
00370     vector q1 = q/Foam::mag(q);
00371 
00372     if (dir == intersection::CONTACT_SPHERE)
00373     {
00374         n /= area;
00375 
00376         return ray(p, q1 - n, alg, intersection::VECTOR);
00377     }
00378 
00379     point pInter;
00380     bool hit;
00381     {
00382         // Reuse the fast ray intersection routine below in FULL_RAY
00383         // mode since the original intersection routine has rounding problems.
00384         pointHit fastInter = intersection(p, q1, intersection::FULL_RAY);
00385         pInter = fastInter.rawPoint();
00386         hit = fastInter.hit();
00387     }
00388 
00389     scalar dist = q1 & (pInter - p);
00390 
00391     const scalar planarPointTol =
00392         Foam::min
00393         (
00394             Foam::min
00395             (
00396                 Foam::mag(E0),
00397                 Foam::mag(E1)
00398             ),
00399             Foam::mag(c_ - b_)
00400         )*intersection::planarTol();
00401 
00402     bool eligible =
00403         alg == intersection::FULL_RAY
00404      || (alg == intersection::HALF_RAY && dist > -planarPointTol)
00405      || (
00406             alg == intersection::VISIBLE
00407          && ((q1 & normal()) < -VSMALL)
00408         );
00409 
00410     if (hit && eligible)
00411     {
00412         // Hit. Set distance to intersection.
00413         inter.setHit();
00414         inter.setPoint(pInter);
00415         inter.setDistance(dist);
00416     }
00417     else
00418     {
00419         // Miss or ineligible hit.
00420         inter.setMiss(eligible);
00421 
00422         // The miss point is the nearest point on the triangle
00423         inter.setPoint(nearestPoint(a_, E0, E1, p).rawPoint());
00424 
00425         // The distance to the miss is the distance between the
00426         // original point and plane of intersection
00427         inter.setDistance(Foam::mag(pInter - p));
00428     }
00429 
00430 
00431     return inter;
00432 }
00433 
00434 
00435 // From "Fast, Minimum Storage Ray/Triangle Intersection"
00436 // Moeller/Trumbore.
00437 template<class Point, class PointRef>
00438 inline pointHit triangle<Point, PointRef>::intersection
00439 (
00440     const point& orig,
00441     const vector& dir,
00442     const intersection::algorithm alg,
00443     const scalar tol
00444 ) const
00445 {
00446     const vector edge1 = b_ - a_;
00447     const vector edge2 = c_ - a_;
00448 
00449     // begin calculating determinant - also used to calculate U parameter
00450     const vector pVec = dir ^ edge2;
00451 
00452     // if determinant is near zero, ray lies in plane of triangle
00453     const scalar det = edge1 & pVec;
00454 
00455     // Initialise to miss
00456     pointHit intersection(false, vector::zero, GREAT, false);
00457 
00458     if (alg == intersection::VISIBLE)
00459     {
00460         // Culling branch
00461         if (det < ROOTVSMALL)
00462         {
00463             // Ray on wrong side of triangle. Return miss
00464             return intersection;
00465         }
00466     }
00467     else if (alg == intersection::HALF_RAY || alg == intersection::FULL_RAY)
00468     {
00469         // Non-culling branch
00470         if (det > -ROOTVSMALL && det < ROOTVSMALL)
00471         {
00472             // Ray parallel to triangle. Return miss
00473             return intersection;
00474         }
00475     }
00476 
00477     const scalar inv_det = 1.0 / det;
00478 
00479     /* calculate distance from a_ to ray origin */
00480     const vector tVec = orig-a_;
00481 
00482     /* calculate U parameter and test bounds */
00483     const scalar u = (tVec & pVec)*inv_det;
00484 
00485     if (u < -tol || u > 1.0+tol)
00486     {
00487         // return miss
00488         return intersection;
00489     }
00490 
00491     /* prepare to test V parameter */
00492     const vector qVec = tVec ^ edge1;
00493 
00494     /* calculate V parameter and test bounds */
00495     const scalar v = (dir & qVec) * inv_det;
00496 
00497     if (v < -tol || u + v > 1.0+tol)
00498     {
00499         // return miss
00500         return intersection;
00501     }
00502 
00503     /* calculate t, scale parameters, ray intersects triangle */
00504     const scalar t = (edge2 & qVec) * inv_det;
00505 
00506     if (alg == intersection::HALF_RAY && t < -tol)
00507     {
00508         // Wrong side of orig. Return miss
00509         return intersection;
00510     }
00511 
00512     intersection.setHit();
00513     intersection.setPoint(a_ + u*edge1 + v*edge2);
00514     intersection.setDistance(t);
00515 
00516     return intersection;
00517 }
00518 
00519 
00520 template<class Point, class PointRef>
00521 inline pointHit triangle<Point, PointRef>::nearestPoint
00522 (
00523     const point& p
00524 ) const
00525 {
00526     // Express triangle in terms of baseVertex (point a_) and
00527     // two edge vectors
00528     vector E0 = b_ - a_;
00529     vector E1 = c_ - a_;
00530 
00531     return nearestPoint(a_, E0, E1, p);
00532 }
00533 
00534 
00535 template<class Point, class PointRef>
00536 inline bool triangle<Point, PointRef>::classify
00537 (
00538     const point& p,
00539     const scalar tol,
00540     label& nearType,
00541     label& nearLabel
00542 ) const
00543 {
00544     const vector E0 = b_ - a_;
00545     const vector E1 = c_ - a_;
00546     const vector n = 0.5*(E0 ^ E1);
00547 
00548     // Get largest component of normal
00549     scalar magX = Foam::mag(n.x());
00550     scalar magY = Foam::mag(n.y());
00551     scalar magZ = Foam::mag(n.z());
00552 
00553     label i0 = -1;
00554     if ((magX >= magY) && (magX >= magZ))
00555     {
00556         i0 = 0;
00557     }
00558     else if ((magY >= magX) && (magY >= magZ))
00559     {
00560         i0 = 1;
00561     }
00562     else
00563     {
00564         i0 = 2;
00565     }
00566 
00567     // Get other components
00568     label i1 = (i0 + 1) % 3;
00569     label i2 = (i1 + 1) % 3;
00570 
00571 
00572     scalar u1 = E0[i1];
00573     scalar v1 = E0[i2];
00574 
00575     scalar u2 = E1[i1];
00576     scalar v2 = E1[i2];
00577 
00578     scalar det = v2*u1 - u2*v1;
00579 
00580     scalar u0 = p[i1] - a_[i1];
00581     scalar v0 = p[i2] - a_[i2];
00582 
00583     scalar alpha = 0;
00584     scalar beta = 0;
00585 
00586     bool hit = false;
00587 
00588     if (Foam::mag(u1) < ROOTVSMALL)
00589     {
00590         beta = u0/u2;
00591 
00592         alpha = (v0 - beta*v2)/v1;
00593 
00594         hit = ((alpha >= 0) && ((alpha + beta) <= 1));
00595     }
00596     else
00597     {
00598         beta = (v0*u1 - u0*v1)/det;
00599 
00600         alpha = (u0 - beta*u2)/u1;
00601 
00602         hit = ((alpha >= 0) && ((alpha + beta) <= 1));
00603     }
00604 
00605     //
00606     // Now alpha, beta are the coordinates in the triangle local coordinate
00607     // system E0, E1
00608     //
00609 
00610     //Pout<< "alpha:" << alpha << endl;
00611     //Pout<< "beta:" << beta << endl;
00612     //Pout<< "hit:" << hit << endl;
00613     //Pout<< "tol:" << tol << endl;
00614 
00615     if (hit)
00616     {
00617         // alpha,beta might get negative due to precision errors
00618         alpha = max(0.0, min(1.0, alpha));
00619         beta = max(0.0, min(1.0, beta));
00620     }
00621 
00622     nearType = NONE;
00623     nearLabel = -1;
00624 
00625     if (Foam::mag(alpha+beta-1) <= tol)
00626     {
00627         // On edge between vert 1-2 (=edge 1)
00628 
00629         if (Foam::mag(alpha) <= tol)
00630         {
00631             nearType = POINT;
00632             nearLabel = 2;
00633         }
00634         else if (Foam::mag(beta) <= tol)
00635         {
00636             nearType = POINT;
00637             nearLabel = 1;
00638         }
00639         else if ((alpha >= 0) && (alpha <= 1) && (beta >= 0) && (beta <= 1))
00640         {
00641             nearType = EDGE;
00642             nearLabel = 1;
00643         }
00644     }
00645     else if (Foam::mag(alpha) <= tol)
00646     {
00647         // On edge between vert 2-0 (=edge 2)
00648 
00649         if (Foam::mag(beta) <= tol)
00650         {
00651             nearType = POINT;
00652             nearLabel = 0;
00653         }
00654         else if (Foam::mag(beta-1) <= tol)
00655         {
00656             nearType = POINT;
00657             nearLabel = 2;
00658         }
00659         else if ((beta >= 0) && (beta <= 1))
00660         {
00661             nearType = EDGE;
00662             nearLabel = 2;
00663         }
00664     }
00665     else if (Foam::mag(beta) <= tol)
00666     {
00667         // On edge between vert 0-1 (= edge 0)
00668 
00669         if (Foam::mag(alpha) <= tol)
00670         {
00671             nearType = POINT;
00672             nearLabel = 0;
00673         }
00674         else if (Foam::mag(alpha-1) <= tol)
00675         {
00676             nearType = POINT;
00677             nearLabel = 1;
00678         }
00679         else if ((alpha >= 0) && (alpha <= 1))
00680         {
00681             nearType = EDGE;
00682             nearLabel = 0;
00683         }
00684     }
00685 
00686     return hit;
00687 }
00688 
00689 
00690 
00691 
00692 
00693 // * * * * * * * * * * * * * * * Ostream Operator  * * * * * * * * * * * * * //
00694 
00695 template<class point, class pointRef>
00696 inline Istream& operator>>(Istream& is, triangle<point, pointRef>& t)
00697 {
00698     // Read beginning of triangle point pair
00699     is.readBegin("triangle");
00700 
00701     is >> t.a_ >> t.b_ >> t.c_;
00702 
00703     // Read end of triangle point pair
00704     is.readEnd("triangle");
00705 
00706     // Check state of Ostream
00707     is.check("Istream& operator>>(Istream&, triangle&)");
00708 
00709     return is;
00710 }
00711 
00712 
00713 template<class Point, class PointRef>
00714 inline Ostream& operator<<(Ostream& os, const triangle<Point, PointRef>& t)
00715 {
00716     os  << nl
00717         << token::BEGIN_LIST
00718         << t.a_ << token::SPACE << t.b_ << token::SPACE << t.c_
00719         << token::END_LIST;
00720 
00721     return os;
00722 }
00723 
00724 
00725 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00726 
00727 } // End namespace Foam
00728 
00729 // ************************************************************************* //
00730 
Copyright © 2000-2009 OpenCFD Ltd