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

treeDataTriSurface.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 "treeDataTriSurface.H"
00028 #include "triSurfaceTools.H"
00029 #include "triangleFuncs.H"
00030 #include "indexedOctree.H"
00031 
00032 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00033 
00034 defineTypeNameAndDebug(Foam::treeDataTriSurface, 0);
00035 
00036 
00037 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00038 
00039 // Fast distance to triangle calculation. From
00040 // "Distance Between Point and Trangle in 3D"
00041 // David Eberly, Magic Software Inc. Aug. 2003.
00042 // Works on function Q giving distance to point and tries to minimize this.
00043 Foam::scalar Foam::treeDataTriSurface::nearestCoords
00044 (
00045     const point& base,
00046     const point& E0,
00047     const point& E1,
00048     const scalar a,
00049     const scalar b,
00050     const scalar c,
00051     const point& P,
00052     scalar& s,
00053     scalar& t
00054 )
00055 {
00056     // distance vector
00057     const vector D(base - P);
00058 
00059     // Precalculate distance factors.
00060     const scalar d = E0 & D;
00061     const scalar e = E1 & D;
00062 
00063     // Do classification
00064     const scalar det = a*c - b*b;
00065 
00066     s = b*e - c*d;
00067     t = b*d - a*e;
00068 
00069     if (s+t < det)
00070     {
00071         if (s < 0)
00072         {
00073             if (t < 0)
00074             {
00075                 //region 4
00076                 if (e > 0)
00077                 {
00078                     //min on edge t = 0
00079                     t = 0;
00080                     s = (d >= 0 ? 0 : (-d >= a ? 1 : -d/a));
00081                 }
00082                 else
00083                 {
00084                     //min on edge s=0
00085                     s = 0;
00086                     t = (e >= 0 ? 0 : (-e >= c ? 1 : -e/c));
00087                 }
00088             }
00089             else
00090             {
00091                 //region 3. Min on edge s = 0
00092                 s = 0;
00093                 t = (e >= 0 ? 0 : (-e >= c ? 1 : -e/c));
00094             }
00095         }
00096         else if (t < 0)
00097         {
00098             //region 5
00099             t = 0;
00100             s = (d >= 0 ? 0 : (-d >= a ? 1 : -d/a));
00101         }
00102         else
00103         {
00104             //region 0
00105             const scalar invDet = 1/det;
00106             s *= invDet;
00107             t *= invDet;
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         t = 1 - s;
00167     }
00168 
00169 
00170     // Calculate distance.
00171     // Note: abs should not be needed but truncation error causes problems
00172     // with points very close to one of the triangle vertices.
00173     // (seen up to -9e-15). Alternatively add some small value.
00174 
00175     const scalar f = D & D;
00176     return Foam::mag(a*s*s + 2*b*s*t + c*t*t + 2*d*s + 2*e*t + f);
00177 }
00178 
00179 
00180 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00181 
00182 // Construct from components
00183 Foam::treeDataTriSurface::treeDataTriSurface(const triSurface& surface)
00184 :
00185     surface_(surface)
00186 {}
00187 
00188 
00189 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00190 
00191 Foam::pointField Foam::treeDataTriSurface::points() const
00192 {
00193     const pointField& points = surface_.points();
00194 
00195     pointField centres(surface_.size());
00196 
00197     forAll(surface_, triI)
00198     {
00199         centres[triI] = surface_[triI].centre(points);
00200     }
00201     return centres;
00202 }
00203 
00204 
00205 //- Get type of sample (inside/outside/mixed) w.r.t. surface.
00206 Foam::label Foam::treeDataTriSurface::getVolumeType
00207 (
00208     const indexedOctree<treeDataTriSurface>& tree,
00209     const point& sample
00210 ) const
00211 {
00212     // Find nearest point
00213     const treeBoundBox& treeBb = tree.bb();
00214 
00215     pointIndexHit pHit = tree.findNearest
00216     (
00217         sample,
00218         max
00219         (
00220             Foam::sqr(GREAT),
00221             Foam::magSqr(treeBb.span())
00222         )
00223     );
00224 
00225     if (!pHit.hit())
00226     {
00227         FatalErrorIn("treeDataTriSurface::getVolumeType(..)")
00228             << "treeBb:" << treeBb
00229             << " sample:" << sample
00230             << " pHit:" << pHit
00231             << abort(FatalError);
00232     }
00233 
00234     triSurfaceTools::sideType t = triSurfaceTools::surfaceSide
00235     (
00236         surface_,
00237         sample,
00238         pHit.index(),
00239         pHit.hitPoint(),
00240         indexedOctree<treeDataTriSurface>::perturbTol()
00241     );
00242 
00243     if (t == triSurfaceTools::UNKNOWN)
00244     {
00245         return indexedOctree<treeDataTriSurface>::UNKNOWN;
00246     }
00247     else if (t == triSurfaceTools::INSIDE)
00248     {
00249         return indexedOctree<treeDataTriSurface>::INSIDE;
00250     }
00251     else if (t == triSurfaceTools::OUTSIDE)
00252     {
00253         return indexedOctree<treeDataTriSurface>::OUTSIDE;
00254     }
00255     else
00256     {
00257         FatalErrorIn("treeDataTriSurface::getVolumeType(..)")
00258             << "problem" << abort(FatalError);
00259         return indexedOctree<treeDataTriSurface>::UNKNOWN;
00260     }
00261 }
00262 
00263 
00264 // Check if any point on triangle is inside cubeBb.
00265 bool Foam::treeDataTriSurface::overlaps
00266 (
00267     const label index,
00268     const treeBoundBox& cubeBb
00269 ) const
00270 {
00271     const pointField& points = surface_.points();
00272     const labelledTri& f = surface_[index];
00273 
00274     // Triangle points
00275     const point& p0 = points[f[0]];
00276     const point& p1 = points[f[1]];
00277     const point& p2 = points[f[2]];
00278 
00279     boundBox triBb(p0, p0);
00280     triBb.min() = min(triBb.min(), p1);
00281     triBb.min() = min(triBb.min(), p2);
00282 
00283     triBb.max() = max(triBb.max(), p1);
00284     triBb.max() = max(triBb.max(), p2);
00285 
00286     //- For testing: robust one
00287     //return cubeBb.overlaps(triBb);
00288 
00289     //- Exact test of triangle intersecting bb
00290 
00291     // Quick rejection. If whole bounding box of tri is outside cubeBb then
00292     // there will be no intersection.
00293     if (!cubeBb.overlaps(triBb))
00294     {
00295         return false;
00296     }
00297 
00298     // Check if one or more triangle point inside
00299     if (cubeBb.contains(p0) || cubeBb.contains(p1) || cubeBb.contains(p2))
00300     {
00301         // One or more points inside
00302         return true;
00303     }
00304 
00305     // Now we have the difficult case: all points are outside but connecting
00306     // edges might go through cube. Use fast intersection of bounding box.
00307 
00308     //return triangleFuncs::intersectBbExact(p0, p1, p2, cubeBb);
00309     return triangleFuncs::intersectBb(p0, p1, p2, cubeBb);
00310 }
00311 
00312 
00313 // Calculate nearest point to sample. Updates (if any) nearestDistSqr, minIndex,
00314 // nearestPoint.
00315 void Foam::treeDataTriSurface::findNearest
00316 (
00317     const labelList& indices,
00318     const point& sample,
00319 
00320     scalar& nearestDistSqr,
00321     label& minIndex,
00322     point& nearestPoint
00323 ) const
00324 {
00325     const pointField& points = surface_.points();
00326 
00327     forAll(indices, i)
00328     {
00329         label index = indices[i];
00330         const labelledTri& f = surface_[index];
00331 
00332         // Triangle points
00333         const point& p0 = points[f[0]];
00334         const point& p1 = points[f[1]];
00335         const point& p2 = points[f[2]];
00336 
00337 
00341         //
00343         //point triBbMin = min(p0, min(p1, p2));
00344         //point triBbMax = max(p0, max(p1, p2));
00345         //
00346         //if
00347         //(
00348         //    indexedOctree<treeDataTriSurface>::intersects
00349         //    (
00350         //        triBbMin,
00351         //        triBbMax,
00352         //        nearestDistSqr,
00353         //        sample
00354         //    )
00355         //)
00356         {
00357             // Get spanning vectors of triangle
00358             vector base(p1);
00359             vector E0(p0 - p1);
00360             vector E1(p2 - p1);
00361 
00362             scalar a(E0& E0);
00363             scalar b(E0& E1);
00364             scalar c(E1& E1);
00365 
00366             // Get nearest point in s,t coordinates (s is along E0, t is along
00367             // E1)
00368             scalar s;
00369             scalar t;
00370 
00371             scalar distSqr = nearestCoords
00372             (
00373                 base,
00374                 E0,
00375                 E1,
00376                 a,
00377                 b,
00378                 c,
00379                 sample,
00380 
00381                 s,
00382                 t
00383             );
00384 
00385             if (distSqr < nearestDistSqr)
00386             {
00387                 nearestDistSqr = distSqr;
00388                 minIndex = index;
00389                 nearestPoint = base + s*E0 + t*E1;
00390             }
00391         }
00392     }
00393 }
00394 
00395 
00396 // Calculate nearest point to line. Updates (if any) nearestDistSqr, minIndex,
00397 // nearestPoint.
00398 void Foam::treeDataTriSurface::findNearest
00399 (
00400     const labelList& indices,
00401     const linePointRef& ln,
00402 
00403     treeBoundBox& tightest,
00404     label& minIndex,
00405     point& linePoint,
00406     point& nearestPoint
00407 ) const
00408 {
00409     notImplemented
00410     (
00411         "treeDataTriSurface::findNearest(const labelList&"
00412         ", const linePointRef&, treeBoundBox&, label&, point&, point&) const"
00413     );
00414 }
00415 
00416 
00417 bool Foam::treeDataTriSurface::intersects
00418 (
00419     const label index,
00420     const point& start,
00421     const point& end,
00422     point& intersectionPoint
00423 ) const
00424 {
00425     const pointField& points = surface_.points();
00426 
00427     const labelledTri& f = surface_[index];
00428 
00429     // Do quick rejection test
00430     treeBoundBox triBb(points[f[0]], points[f[0]]);
00431     triBb.min() = min(triBb.min(), points[f[1]]);
00432     triBb.max() = max(triBb.max(), points[f[1]]);
00433     triBb.min() = min(triBb.min(), points[f[2]]);
00434     triBb.max() = max(triBb.max(), points[f[2]]);
00435 
00436     const direction startBits(triBb.posBits(start));
00437     const direction endBits(triBb.posBits(end));
00438 
00439     if ((startBits & endBits) != 0)
00440     {
00441         // start and end in same block outside of triBb.
00442         return false;
00443     }
00444 
00445     const triPointRef tri(points[f[0]], points[f[1]], points[f[2]]);
00446 
00447     const vector dir(end - start);
00448 
00449     // Use relative tolerance (from octree) to determine intersection.
00450 
00451     pointHit inter = tri.intersection
00452     (
00453         start,
00454         dir,
00455         intersection::HALF_RAY,
00456         indexedOctree<treeDataTriSurface>::perturbTol()
00457     );
00458 
00459     if (inter.hit() && inter.distance() <= 1)
00460     {
00461         // Note: no extra test on whether intersection is in front of us
00462         // since using half_ray.
00463         intersectionPoint = inter.hitPoint();
00464 
00465         return true;
00466     }
00467     else
00468     {
00469         return false;
00470     }
00471 
00472 
00473     //- Using exact intersections
00474     //pointHit info = f.tri(points).intersectionExact(start, end);
00475     //
00476     //if (info.hit())
00477     //{
00478     //    intersectionPoint = info.hitPoint();
00479     //}
00480     //return info.hit();
00481 }
00482 
00483 
00484 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd