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