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

treeDataFace.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 "treeDataFace.H"
00028 #include "polyMesh.H"
00029 #include "triangleFuncs.H"
00030 
00031 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00032 
00033 defineTypeNameAndDebug(Foam::treeDataFace, 0);
00034 
00035 Foam::scalar Foam::treeDataFace::tolSqr = sqr(1E-6);
00036 
00037 
00038 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00039 
00040 Foam::treeBoundBox Foam::treeDataFace::calcBb(const label faceI) const
00041 {
00042     const pointField& points = mesh_.points();
00043 
00044     const face& f = mesh_.faces()[faceI];
00045 
00046     treeBoundBox bb(points[f[0]], points[f[0]]);
00047 
00048     for (label fp = 1; fp < f.size(); fp++)
00049     {
00050         const point& p = points[f[fp]];
00051 
00052         bb.min() = min(bb.min(), p);
00053         bb.max() = max(bb.max(), p);
00054     }
00055     return bb;
00056 }
00057 
00058 
00059 void Foam::treeDataFace::update()
00060 {
00061     forAll(faceLabels_, i)
00062     {
00063         isTreeFace_.set(faceLabels_[i], 1);
00064     }
00065 
00066     if (cacheBb_)
00067     {
00068         bbs_.setSize(faceLabels_.size());
00069 
00070         forAll(faceLabels_, i)
00071         {
00072             bbs_[i] = calcBb(faceLabels_[i]);
00073         }
00074     }
00075 }
00076 
00077 
00078 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00079 
00080 // Construct from components
00081 Foam::treeDataFace::treeDataFace
00082 (
00083     const bool cacheBb,
00084     const primitiveMesh& mesh,
00085     const labelList& faceLabels
00086 )
00087 :
00088     mesh_(mesh),
00089     faceLabels_(faceLabels),
00090     isTreeFace_(mesh.nFaces(), 0),
00091     cacheBb_(cacheBb)
00092 {
00093     update();
00094 }
00095 
00096 
00097 Foam::treeDataFace::treeDataFace
00098 (
00099     const bool cacheBb,
00100     const primitiveMesh& mesh
00101 )
00102 :
00103     mesh_(mesh),
00104     faceLabels_(identity(mesh_.nFaces())),
00105     isTreeFace_(mesh.nFaces(), 0),
00106     cacheBb_(cacheBb)
00107 {
00108     update();
00109 }
00110 
00111 
00112 Foam::treeDataFace::treeDataFace
00113 (
00114     const bool cacheBb,
00115     const polyPatch& patch
00116 )
00117 :
00118     mesh_(patch.boundaryMesh().mesh()),
00119     faceLabels_
00120     (
00121         identity(patch.size())
00122       + patch.start()
00123     ),
00124     isTreeFace_(mesh_.nFaces(), 0),
00125     cacheBb_(cacheBb)
00126 {
00127     update();
00128 }
00129 
00130 
00131 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00132 
00133 Foam::pointField Foam::treeDataFace::points() const
00134 {
00135     pointField cc(faceLabels_.size());
00136 
00137     forAll(faceLabels_, i)
00138     {
00139         cc[i] = mesh_.faceCentres()[faceLabels_[i]];
00140     }
00141 
00142     return cc;
00143 }
00144 
00145 
00146 //- Get type (inside,outside,mixed,unknown) of point w.r.t. surface.
00147 //  Only makes sense for closed surfaces.
00148 Foam::label Foam::treeDataFace::getVolumeType
00149 (
00150     const indexedOctree<treeDataFace>& oc,
00151     const point& sample
00152 ) const
00153 {
00154     // Need to determine whether sample is 'inside' or 'outside'
00155     // Done by finding nearest face. This gives back a face which is
00156     // guaranteed to contain nearest point. This point can be
00157     // - in interior of face: compare to face normal
00158     // - on edge of face: compare to edge normal
00159     // - on point of face: compare to point normal
00160     // Unfortunately the octree does not give us back the intersection point
00161     // or where on the face it has hit so we have to recreate all that
00162     // information.
00163 
00164 
00165     // Find nearest face to sample
00166     pointIndexHit info = oc.findNearest(sample, sqr(GREAT));
00167 
00168     if (info.index() == -1)
00169     {
00170         FatalErrorIn
00171         (
00172             "treeDataFace::getSampleType"
00173             "(indexedOctree<treeDataFace>&, const point&)"
00174         )   << "Could not find " << sample << " in octree."
00175             << abort(FatalError);
00176     }
00177 
00178 
00179     // Get actual intersection point on face
00180     label faceI = faceLabels_[info.index()];
00181 
00182     if (debug & 2)
00183     {
00184         Pout<< "getSampleType : sample:" << sample
00185             << " nearest face:" << faceI;
00186     }
00187 
00188     const pointField& points = mesh_.points();
00189 
00190     // Retest to classify where on face info is. Note: could be improved. We
00191     // already have point.
00192 
00193     const face& f = mesh_.faces()[faceI];
00194     const vector& area = mesh_.faceAreas()[faceI];
00195     const point& fc = mesh_.faceCentres()[faceI];
00196 
00197     pointHit curHit = f.nearestPoint(sample, points);
00198     const point& curPt = curHit.rawPoint();
00199 
00200     //
00201     // 1] Check whether sample is above face
00202     //
00203 
00204     if (curHit.hit())
00205     {
00206         // Nearest point inside face. Compare to face normal.
00207 
00208         if (debug & 2)
00209         {
00210             Pout<< " -> face hit:" << curPt
00211                 << " comparing to face normal " << area << endl;
00212         }
00213         return indexedOctree<treeDataFace>::getSide(area, sample - curPt);
00214     }
00215 
00216     if (debug & 2)
00217     {
00218         Pout<< " -> face miss:" << curPt;
00219     }
00220 
00221     //
00222     // 2] Check whether intersection is on one of the face vertices or
00223     //    face centre
00224     //
00225 
00226     const scalar typDimSqr = mag(area) + VSMALL;
00227 
00228     forAll(f, fp)
00229     {
00230         if ((magSqr(points[f[fp]] - curPt)/typDimSqr) < tolSqr)
00231         {
00232             // Face intersection point equals face vertex fp
00233 
00234             // Calculate point normal (wrong: uses face normals instead of
00235             // triangle normals)
00236             const labelList& pFaces = mesh_.pointFaces()[f[fp]];
00237 
00238             vector pointNormal(vector::zero);
00239 
00240             forAll(pFaces, i)
00241             {
00242                 if (isTreeFace_.get(pFaces[i]) == 1)
00243                 {
00244                     vector n = mesh_.faceAreas()[pFaces[i]];
00245                     n /= mag(n) + VSMALL;
00246 
00247                     pointNormal += n;
00248                 }
00249             }
00250 
00251             if (debug & 2)
00252             {
00253                     Pout<< " -> face point hit :" << points[f[fp]]
00254                         << " point normal:" << pointNormal
00255                         << " distance:"
00256                         << magSqr(points[f[fp]] - curPt)/typDimSqr << endl;
00257             }
00258             return indexedOctree<treeDataFace>::getSide
00259             (
00260                 pointNormal,
00261                 sample - curPt
00262             );
00263         }
00264     }
00265     if ((magSqr(fc - curPt)/typDimSqr) < tolSqr)
00266     {
00267         // Face intersection point equals face centre. Normal at face centre
00268         // is already average of face normals
00269 
00270         if (debug & 2)
00271         {
00272             Pout<< " -> centre hit:" << fc
00273                 << " distance:" << magSqr(fc - curPt)/typDimSqr << endl;
00274         }
00275 
00276         return indexedOctree<treeDataFace>::getSide(area,  sample - curPt);
00277     }
00278 
00279 
00280 
00281     //
00282     // 3] Get the 'real' edge the face intersection is on
00283     //
00284 
00285     const labelList& myEdges = mesh_.faceEdges()[faceI];
00286 
00287     forAll(myEdges, myEdgeI)
00288     {
00289         const edge& e = mesh_.edges()[myEdges[myEdgeI]];
00290 
00291         pointHit edgeHit =
00292             line<point, const point&>
00293             (
00294                 points[e.start()],
00295                 points[e.end()]
00296             ).nearestDist(sample);
00297 
00298 
00299         if ((magSqr(edgeHit.rawPoint() - curPt)/typDimSqr) < tolSqr)
00300         {
00301             // Face intersection point lies on edge e
00302 
00303             // Calculate edge normal (wrong: uses face normals instead of
00304             // triangle normals)
00305             const labelList& eFaces = mesh_.edgeFaces()[myEdges[myEdgeI]];
00306 
00307             vector edgeNormal(vector::zero);
00308 
00309             forAll(eFaces, i)
00310             {
00311                 if (isTreeFace_.get(eFaces[i]) == 1)
00312                 {
00313                     vector n = mesh_.faceAreas()[eFaces[i]];
00314                     n /= mag(n) + VSMALL;
00315 
00316                     edgeNormal += n;
00317                 }
00318             }
00319 
00320             if (debug & 2)
00321             {
00322                 Pout<< " -> real edge hit point:" << edgeHit.rawPoint()
00323                     << " comparing to edge normal:" << edgeNormal
00324                     << endl;
00325             }
00326 
00327             // Found face intersection point on this edge. Compare to edge
00328             // normal
00329             return indexedOctree<treeDataFace>::getSide
00330             (
00331                 edgeNormal,
00332                 sample - curPt
00333             );
00334         }
00335     }
00336 
00337 
00338     //
00339     // 4] Get the internal edge the face intersection is on
00340     //
00341 
00342     forAll(f, fp)
00343     {
00344         pointHit edgeHit = line<point, const point&>
00345         (
00346             points[f[fp]],
00347             fc
00348         ).nearestDist(sample);
00349 
00350         if ((magSqr(edgeHit.rawPoint() - curPt)/typDimSqr) < tolSqr)
00351         {
00352             // Face intersection point lies on edge between two face triangles
00353 
00354             // Calculate edge normal as average of the two triangle normals
00355             vector e = points[f[fp]] - fc;
00356             vector ePrev = points[f[f.rcIndex(fp)]] - fc;
00357             vector eNext = points[f[f.fcIndex(fp)]] - fc;
00358 
00359             vector nLeft = ePrev ^ e;
00360             nLeft /= mag(nLeft) + VSMALL;
00361 
00362             vector nRight = e ^ eNext;
00363             nRight /= mag(nRight) + VSMALL;
00364 
00365             if (debug & 2)
00366             {
00367                 Pout<< " -> internal edge hit point:" << edgeHit.rawPoint()
00368                     << " comparing to edge normal "
00369                     << 0.5*(nLeft + nRight)
00370                     << endl;
00371             }
00372 
00373             // Found face intersection point on this edge. Compare to edge
00374             // normal
00375             return indexedOctree<treeDataFace>::getSide
00376             (
00377                 0.5*(nLeft + nRight),
00378                 sample - curPt
00379             );
00380         }
00381     }
00382 
00383     if (debug & 2)
00384     {
00385         Pout<< "Did not find sample " << sample
00386             << " anywhere related to nearest face " << faceI << endl
00387             << "Face:";
00388 
00389         forAll(f, fp)
00390         {
00391             Pout<< "    vertex:" << f[fp] << "  coord:" << points[f[fp]]
00392                 << endl;
00393         }
00394     }
00395 
00396     // Can't determine status of sample with respect to nearest face.
00397     // Either
00398     // - tolerances are wrong. (if e.g. face has zero area)
00399     // - or (more likely) surface is not closed.
00400 
00401     return indexedOctree<treeDataFace>::UNKNOWN;
00402 }
00403 
00404 
00405 // Check if any point on shape is inside cubeBb.
00406 bool Foam::treeDataFace::overlaps
00407 (
00408     const label index,
00409     const treeBoundBox& cubeBb
00410 ) const
00411 {
00412     // 1. Quick rejection: bb does not intersect face bb at all
00413     if (cacheBb_)
00414     {
00415         if (!cubeBb.overlaps(bbs_[index]))
00416         {
00417             return false;
00418         }
00419     }
00420     else
00421     {
00422         if (!cubeBb.overlaps(calcBb(faceLabels_[index])))
00423         {
00424             return false;
00425         }
00426     }
00427 
00428     const pointField& points = mesh_.points();
00429 
00430 
00431     // 2. Check if one or more face points inside
00432     label faceI = faceLabels_[index];
00433 
00434     const face& f = mesh_.faces()[faceI];
00435 
00436     forAll(f, fp)
00437     {
00438         if (cubeBb.contains(points[f[fp]]))
00439         {
00440             return true;
00441         }
00442     }
00443 
00444     // 3. Difficult case: all points are outside but connecting edges might
00445     // go through cube. Use triangle-bounding box intersection.
00446     const point& fc = mesh_.faceCentres()[faceI];
00447 
00448     forAll(f, fp)
00449     {
00450         bool triIntersects = triangleFuncs::intersectBb
00451         (
00452             points[f[fp]],
00453             points[f[f.fcIndex(fp)]],
00454             fc,
00455             cubeBb
00456         );
00457 
00458         if (triIntersects)
00459         {
00460             return true;
00461         }
00462     }
00463     return false;
00464 }
00465 
00466 
00467 // Calculate nearest point to sample. Updates (if any) nearestDistSqr, minIndex,
00468 // nearestPoint.
00469 void Foam::treeDataFace::findNearest
00470 (
00471     const labelList& indices,
00472     const point& sample,
00473 
00474     scalar& nearestDistSqr,
00475     label& minIndex,
00476     point& nearestPoint
00477 ) const
00478 {
00479     forAll(indices, i)
00480     {
00481         label index = indices[i];
00482 
00483         const face& f = mesh_.faces()[faceLabels_[index]];
00484 
00485         pointHit nearHit = f.nearestPoint(sample, mesh_.points());
00486         scalar distSqr = sqr(nearHit.distance());
00487 
00488         if (distSqr < nearestDistSqr)
00489         {
00490             nearestDistSqr = distSqr;
00491             minIndex = index;
00492             nearestPoint = nearHit.rawPoint();
00493         }
00494     }
00495 }
00496 
00497 
00498 bool Foam::treeDataFace::intersects
00499 (
00500     const label index,
00501     const point& start,
00502     const point& end,
00503     point& intersectionPoint
00504 ) const
00505 {
00506     // Do quick rejection test
00507     if (cacheBb_)
00508     {
00509         const treeBoundBox& faceBb = bbs_[index];
00510 
00511         if ((faceBb.posBits(start) & faceBb.posBits(end)) != 0)
00512         {
00513             // start and end in same block outside of faceBb.
00514             return false;
00515         }
00516     }
00517 
00518     label faceI = faceLabels_[index];
00519 
00520     const vector dir(end - start);
00521 
00522     pointHit inter = mesh_.faces()[faceI].intersection
00523     (
00524         start,
00525         dir,
00526         mesh_.faceCentres()[faceI],
00527         mesh_.points(),
00528         intersection::HALF_RAY
00529     );
00530 
00531     if (inter.hit() && inter.distance() <= 1)
00532     {
00533         // Note: no extra test on whether intersection is in front of us
00534         // since using half_ray
00535         intersectionPoint = inter.hitPoint();
00536         return true;
00537     }
00538     else
00539     {
00540         return false;
00541     }
00542 }
00543 
00544 
00545 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd