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

octreeDataTriSurface.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 Description
00026 
00027 \*---------------------------------------------------------------------------*/
00028 
00029 #include "octreeDataTriSurface.H"
00030 
00031 #include "labelList.H"
00032 #include "treeBoundBox.H"
00033 #include "faceList.H"
00034 #include "triPointRef.H"
00035 #include "octree.H"
00036 #include "triSurfaceTools.H"
00037 #include "triangleFuncs.H"
00038 
00039 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00040 
00041 defineTypeNameAndDebug(Foam::octreeDataTriSurface, 0);
00042 
00043 Foam::scalar Foam::octreeDataTriSurface::tol(1E-6);
00044 
00045 
00046 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00047 
00048 // Fast distance to triangle calculation. From
00049 // "Distance Between Point and Trangle in 3D"
00050 // David Eberly, Magic Software Inc. Aug. 2003.
00051 // Works on function Q giving distance to point and tries to minimize this.
00052 void Foam::octreeDataTriSurface::nearestCoords
00053 (
00054     const point& base,
00055     const point& E0,
00056     const point& E1,
00057     const scalar a,
00058     const scalar b,
00059     const scalar c,
00060     const point& P,
00061     scalar& s,
00062     scalar& t
00063 )
00064 {
00065     // distance vector
00066     const vector D(base - P);
00067 
00068     // Precalculate distance factors.
00069     const scalar d = E0 & D;
00070     const scalar e = E1 & D;
00071 
00072     // Do classification
00073     const scalar det = a*c - b*b;
00074 
00075     s = b*e - c*d;
00076     t = b*d - a*e;
00077 
00078     if (s+t < det)
00079     {
00080         if (s < 0)
00081         {
00082             if (t < 0)
00083             {
00084                 //region 4
00085                 if (e > 0)
00086                 {
00087                     //min on edge t = 0
00088                     t = 0;
00089                     s = (d >= 0 ? 0 : (-d >= a ? 1 : -d/a));
00090                 }
00091                 else
00092                 {
00093                     //min on edge s=0
00094                     s = 0;
00095                     t = (e >= 0 ? 0 : (-e >= c ? 1 : -e/c));
00096                 }
00097             }
00098             else
00099             {
00100                 //region 3. Min on edge s = 0
00101                 s = 0;
00102                 t = (e >= 0 ? 0 : (-e >= c ? 1 : -e/c));
00103             }
00104         }
00105         else if (t < 0)
00106         {
00107             //region 5
00108             t = 0;
00109             s = (d >= 0 ? 0 : (-d >= a ? 1 : -d/a));
00110         }
00111         else
00112         {
00113             //region 0
00114             const scalar invDet = 1/det;
00115             s *= invDet;
00116             t *= invDet;
00117         }
00118     }
00119     else
00120     {
00121         if (s < 0)
00122         {
00123             //region 2
00124             const scalar tmp0 = b + d;
00125             const scalar tmp1 = c + e;
00126             if (tmp1 > tmp0)
00127             {
00128                 //min on edge s+t=1
00129                 const scalar numer = tmp1 - tmp0;
00130                 const scalar denom = a-2*b+c;
00131                 s = (numer >= denom ? 1 : numer/denom);
00132                 t = 1 - s;
00133             }
00134             else
00135             {
00136                 //min on edge s=0
00137                 s = 0;
00138                 t = (tmp1 <= 0 ? 1 : (e >= 0 ? 0 : - e/c));
00139             }
00140         }
00141         else if (t < 0)
00142         {
00143             //region 6
00144             const scalar tmp0 = b + d;
00145             const scalar tmp1 = c + e;
00146             if (tmp1 > tmp0)
00147             {
00148                 //min on edge s+t=1
00149                 const scalar numer = tmp1 - tmp0;
00150                 const scalar denom = a-2*b+c;
00151                 s = (numer >= denom ? 1 : numer/denom);
00152                 t = 1 - s;
00153             }
00154             else
00155             {
00156                 //min on edge t=0
00157                 t = 0;
00158                 s = (tmp1 <= 0 ? 1 : (d >= 0 ? 0 : - d/a));
00159             }
00160         }
00161         else
00162         {
00163             //region 1
00164             const scalar numer = c+e-(b+d);
00165             if (numer <= 0)
00166             {
00167                 s = 0;
00168             }
00169             else
00170             {
00171                 const scalar denom = a-2*b+c;
00172                 s = (numer >= denom ? 1 : numer/denom);
00173             }
00174         }
00175         t = 1 - s;
00176     }
00177 
00178 
00179     // Calculate distance.
00180     // Note: abs should not be needed but truncation error causes problems
00181     // with points very close to one of the triangle vertices.
00182     // (seen up to -9e-15). Alternatively add some small value.
00183 
00184     // const scalar f = D & D;
00185     // return a*s*s + 2*b*s*t + c*t*t + 2*d*s + 2*e*t + f + SMALL;
00186     // return Foam::mag(a*s*s + 2*b*s*t + c*t*t + 2*d*s + 2*e*t + f);
00187 }
00188 
00189 
00190 Foam::point Foam::octreeDataTriSurface::nearestPoint
00191 (
00192     const label index,
00193     const point& p
00194 ) const
00195 {
00196     scalar s;
00197     scalar t;
00198 
00199     nearestCoords
00200     (
00201         base_[index],
00202         E0_[index],
00203         E1_[index],
00204         a_[index],
00205         b_[index],
00206         c_[index],
00207         p,
00208         s,
00209         t
00210     );
00211 
00212     return base_[index] + s*E0_[index] + t*E1_[index];
00213 }
00214 
00215 
00216 // Helper function to calculate tight fitting bounding boxes.
00217 Foam::treeBoundBoxList Foam::octreeDataTriSurface::calcBb
00218 (
00219     const triSurface& surf
00220 )
00221 {
00222     treeBoundBoxList allBb(surf.size(), treeBoundBox::invertedBox);
00223 
00224     const labelListList& pointFcs = surf.pointFaces();
00225     const pointField& localPts = surf.localPoints();
00226 
00227     forAll(pointFcs, pointI)
00228     {
00229         const labelList& myFaces = pointFcs[pointI];
00230         const point& vertCoord = localPts[pointI];
00231 
00232         forAll(myFaces, myFaceI)
00233         {
00234             // Update bb
00235             label faceI = myFaces[myFaceI];
00236 
00237             treeBoundBox& bb = allBb[faceI];
00238 
00239             bb.min() = min(bb.min(), vertCoord);
00240             bb.max() = max(bb.max(), vertCoord);
00241         }
00242     }
00243 
00244     return allBb;
00245 }
00246 
00247 
00248 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00249 
00250 // Construct from components
00251 Foam::octreeDataTriSurface::octreeDataTriSurface(const triSurface& surface)
00252 :
00253     surface_(surface),
00254     allBb_(calcBb(surface_)),
00255     base_(surface_.size()),
00256     E0_(surface_.size()),
00257     E1_(surface_.size()),
00258     a_(surface_.size()),
00259     b_(surface_.size()),
00260     c_(surface_.size())
00261 {
00262     // Precalculate factors for distance calculation
00263     const pointField& points = surface_.points();
00264 
00265     forAll(surface_, faceI)
00266     {
00267         const labelledTri& f = surface_[faceI];
00268 
00269         // Calculate base and spanning vectors of triangles
00270         base_[faceI] = points[f[1]];
00271         E0_[faceI] = points[f[0]] - points[f[1]];
00272         E1_[faceI] = points[f[2]] - points[f[1]];
00273 
00274         a_[faceI] = E0_[faceI] & E0_[faceI];
00275         b_[faceI] = E0_[faceI] & E1_[faceI];
00276         c_[faceI] = E1_[faceI] & E1_[faceI];
00277     }
00278 }
00279 
00280 
00281 // Construct from components
00282 Foam::octreeDataTriSurface::octreeDataTriSurface
00283 (
00284     const triSurface& surface,
00285     const treeBoundBoxList& allBb
00286 )
00287 :
00288     surface_(surface),
00289     allBb_(allBb),
00290     base_(surface_.size()),
00291     E0_(surface_.size()),
00292     E1_(surface_.size()),
00293     a_(surface_.size()),
00294     b_(surface_.size()),
00295     c_(surface_.size())
00296 {
00297     const pointField& points = surface_.points();
00298 
00299     forAll(surface_, faceI)
00300     {
00301         const labelledTri& f = surface_[faceI];
00302 
00303         // Calculate base and spanning vectors of triangles
00304         base_[faceI] = points[f[1]];
00305         E0_[faceI] = points[f[0]] - points[f[1]];
00306         E1_[faceI] = points[f[2]] - points[f[1]];
00307 
00308         a_[faceI] = E0_[faceI] & E0_[faceI];
00309         b_[faceI] = E0_[faceI] & E1_[faceI];
00310         c_[faceI] = E1_[faceI] & E1_[faceI];
00311     }
00312 }
00313 
00314 
00315 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00316 
00317 Foam::label Foam::octreeDataTriSurface::getSampleType
00318 (
00319     const octree<octreeDataTriSurface>& oc,
00320     const point& sample
00321 ) const
00322 
00323 {
00324     treeBoundBox tightest(treeBoundBox::greatBox);
00325     scalar tightestDist(treeBoundBox::great);
00326 
00327     // Find nearest face to sample
00328     label faceI = oc.findNearest(sample, tightest, tightestDist);
00329 
00330     if (debug & 2)
00331     {
00332         Pout<< "getSampleType : sample:" << sample
00333             << " nearest face:" << faceI;
00334     }
00335 
00336     if (faceI == -1)
00337     {
00338         FatalErrorIn
00339         (
00340             "octreeDataTriSurface::getSampleType"
00341             "(octree<octreeDataTriSurface>&, const point&)"
00342         )   << "Could not find " << sample << " in octree."
00343             << abort(FatalError);
00344     }
00345 
00346     const pointField& pts = surface_.points();
00347     const labelledTri& f = surface_[faceI];
00348 
00349     pointHit curHit = triPointRef
00350     (
00351         pts[f[0]],
00352         pts[f[1]],
00353         pts[f[2]]
00354     ).nearestPoint(sample);
00355 
00356     // Get normal according to position on face. On point -> pointNormal,
00357     // on edge-> edge normal, face normal on interior.
00358     vector n
00359     (
00360         triSurfaceTools::surfaceNormal
00361         (
00362             surface_,
00363             faceI,
00364             curHit.rawPoint()
00365         )
00366     );
00367 
00368     return
00369         octree<octreeDataTriSurface>::getVolType(n, sample - curHit.rawPoint());
00370 }
00371 
00372 
00373 // Check if any point on triangle is inside cubeBb.
00374 bool Foam::octreeDataTriSurface::overlaps
00375 (
00376     const label index,
00377     const treeBoundBox& cubeBb
00378 ) const
00379 {
00380     //return cubeBb.overlaps(allBb_[index]);
00381 
00382     //- Exact test of triangle intersecting bb
00383 
00384     // Quick rejection.
00385     if (!cubeBb.overlaps(allBb_[index]))
00386     {
00387         return false;
00388     }
00389 
00390     // Triangle points
00391     const pointField& points = surface_.points();
00392     const labelledTri& f = surface_[index];
00393     const point& p0 = points[f[0]];
00394     const point& p1 = points[f[1]];
00395     const point& p2 = points[f[2]];
00396 
00397     // Check if one or more triangle point inside
00398     if (cubeBb.contains(p0) || cubeBb.contains(p1) || cubeBb.contains(p2))
00399     {
00400         // One or more points inside
00401         return true;
00402     }
00403 
00404     // Now we have the difficult case: all points are outside but connecting
00405     // edges might go through cube. Use fast intersection of bounding box.
00406 
00407     return triangleFuncs::intersectBb(p0, p1, p2, cubeBb);
00408 }
00409 
00410 
00411 bool Foam::octreeDataTriSurface::contains
00412 (
00413     const label,
00414     const point&
00415 ) const
00416 {
00417     notImplemented
00418     (
00419         "octreeDataTriSurface::contains(const label, const point&)"
00420     );
00421 
00422     return false;
00423 }
00424 
00425 
00426 bool Foam::octreeDataTriSurface::intersects
00427 (
00428     const label index,
00429     const point& start,
00430     const point& end,
00431     point& intersectionPoint
00432 ) const
00433 {
00434     if (mag(surface_.faceNormals()[index]) < VSMALL)
00435     {
00436         return false;
00437     }
00438 
00439     const pointField& points = surface_.points();
00440 
00441     const labelledTri& f = surface_[index];
00442 
00443     triPointRef tri(points[f[0]], points[f[1]], points[f[2]]);
00444 
00445     const vector dir(end - start);
00446 
00447     // Disable picking up intersections behind us.
00448     scalar oldTol = intersection::setPlanarTol(0.0);
00449 
00450     pointHit inter = tri.ray(start, dir, intersection::HALF_RAY);
00451 
00452     intersection::setPlanarTol(oldTol);
00453 
00454     if (inter.hit() && inter.distance() <= mag(dir))
00455     {
00456         // Note: no extra test on whether intersection is in front of us
00457         // since using half_ray AND zero tolerance. (note that tolerance
00458         // is used to look behind us)
00459         intersectionPoint = inter.hitPoint();
00460 
00461         return true;
00462     }
00463     else
00464     {
00465         return false;
00466     }
00467 }
00468 
00469 
00470 bool Foam::octreeDataTriSurface::findTightest
00471 (
00472     const label index,
00473     const point& sample,
00474     treeBoundBox& tightest
00475 ) const
00476 {
00477 
00478     // get nearest and furthest away vertex
00479     point myNear, myFar;
00480     allBb_[index].calcExtremities(sample, myNear, myFar);
00481 
00482     const point dist = myFar - sample;
00483     scalar myFarDist = mag(dist);
00484 
00485     point tightestNear, tightestFar;
00486     tightest.calcExtremities(sample, tightestNear, tightestFar);
00487 
00488     scalar tightestFarDist = mag(tightestFar - sample);
00489 
00490     if (tightestFarDist < myFarDist)
00491     {
00492         // Keep current tightest.
00493         return false;
00494     }
00495     else
00496     {
00497         // Construct bb around sample and myFar
00498         const point dist2(fabs(dist.x()), fabs(dist.y()), fabs(dist.z()));
00499 
00500         tightest.min() = sample - dist2;
00501         tightest.max() = sample + dist2;
00502 
00503         return true;
00504     }
00505 }
00506 
00507 
00508 // Determine numerical value of sign of sample compared to shape at index
00509 Foam::scalar Foam::octreeDataTriSurface::calcSign
00510 (
00511     const label index,
00512     const point& sample,
00513     vector& n
00514 ) const
00515 {
00516     n = surface_.faceNormals()[index];
00517 
00518     const labelledTri& tri = surface_[index];
00519 
00520     // take vector from sample to any point on triangle (we use vertex 0)
00521     vector vec = sample - surface_.points()[tri[0]];
00522 
00523     vec /= mag(vec) + VSMALL;
00524 
00525     return n & vec;
00526 }
00527 
00528 
00529 // Calculate nearest point to sample on/in shapei. !Does not set nearest
00530 Foam::scalar Foam::octreeDataTriSurface::calcNearest
00531 (
00532     const label index,
00533     const point& sample,
00534     point&
00535 ) const
00536 {
00537     return mag(nearestPoint(index, sample) - sample);
00538 }
00539 
00540 
00541 // Calculate nearest point on/in shapei
00542 Foam::scalar Foam::octreeDataTriSurface::calcNearest
00543 (
00544     const label index,
00545     const linePointRef& ln,
00546     point& linePt,
00547     point& shapePt
00548 ) const
00549 {
00550     notImplemented
00551     (
00552         "octreeDataTriSurface::calcNearest"
00553         "(const label, const linePointRef&, point& linePt, point&)"
00554     );
00555     return GREAT;
00556 }
00557 
00558 
00559 void Foam::octreeDataTriSurface::write
00560 (
00561     Ostream& os,
00562     const label index
00563 ) const
00564 {
00565     os << surface_[index] << token::SPACE << allBb_[index];
00566 }
00567 
00568 
00569 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd