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

octreeDataFace.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 "octreeDataFace.H"
00028 #include "labelList.H"
00029 #include "polyMesh.H"
00030 #include "octree.H"
00031 #include "polyPatch.H"
00032 #include "triangleFuncs.H"
00033 #include "linePointRef.H"
00034 
00035 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00036 
00037 defineTypeNameAndDebug(Foam::octreeDataFace, 0);
00038 
00039 Foam::scalar Foam::octreeDataFace::tol(1E-6);
00040 
00041 
00042 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00043 
00044 void Foam::octreeDataFace::calcBb()
00045 {
00046     allBb_.setSize(meshFaces_.size());
00047     allBb_ = treeBoundBox::invertedBox;
00048 
00049     forAll (meshFaces_, i)
00050     {
00051         // Update bb of face
00052         treeBoundBox& myBb = allBb_[i];
00053 
00054         const face& f = mesh_.faces()[meshFaces_[i]];
00055 
00056         forAll(f, faceVertexI)
00057         {
00058             const point& coord = mesh_.points()[f[faceVertexI]];
00059 
00060             myBb.min() = min(myBb.min(), coord);
00061             myBb.max() = max(myBb.max(), coord);
00062         }
00063     }
00064 }
00065 
00066 
00067 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00068 
00069 // Construct from selected mesh faces
00070 Foam::octreeDataFace::octreeDataFace
00071 (
00072     const primitiveMesh& mesh,
00073     const labelList& meshFaces,
00074     const treeBoundBoxList& allBb
00075 )
00076 :
00077     mesh_(mesh),
00078     meshFaces_(meshFaces),
00079     allBb_(allBb)
00080 {}
00081 
00082 
00083 // Construct from selected mesh faces. Bounding box calculated.
00084 Foam::octreeDataFace::octreeDataFace
00085 (
00086     const primitiveMesh& mesh,
00087     const labelList& meshFaces
00088 )
00089 :
00090     mesh_(mesh),
00091     meshFaces_(meshFaces),
00092     allBb_(meshFaces_.size())
00093 {
00094     // Generate tight fitting bounding box
00095     calcBb();
00096 }
00097 
00098 
00099 // Construct from selected mesh faces
00100 Foam::octreeDataFace::octreeDataFace
00101 (
00102     const primitiveMesh& mesh,
00103     const UList<const labelList*>& meshFaceListPtrs,
00104     const UList<const treeBoundBoxList*>& bbListPtrs
00105 )
00106 :
00107     mesh_(mesh),
00108     meshFaces_(0),
00109     allBb_(0)
00110 {
00111     label faceI = 0;
00112 
00113     forAll(meshFaceListPtrs, listI)
00114     {
00115         faceI += meshFaceListPtrs[listI]->size();
00116     }
00117 
00118     meshFaces_.setSize(faceI);
00119     allBb_.setSize(faceI);
00120 
00121     faceI = 0;
00122 
00123     forAll(meshFaceListPtrs, listI)
00124     {
00125         const labelList& meshFaces = *meshFaceListPtrs[listI];
00126         const treeBoundBoxList& allBb = *bbListPtrs[listI];
00127 
00128         forAll(meshFaces, meshFaceI)
00129         {
00130             meshFaces_[faceI] = meshFaces[meshFaceI];
00131             allBb_[faceI] = allBb[meshFaceI];
00132             faceI++;
00133         }
00134     }
00135 }
00136 
00137 
00138 // Construct from selected mesh faces. Bounding box calculated.
00139 Foam::octreeDataFace::octreeDataFace
00140 (
00141     const primitiveMesh& mesh,
00142     const UList<const labelList*>& meshFaceListPtrs
00143 )
00144 :
00145     mesh_(mesh),
00146     meshFaces_(0)
00147 {
00148     label faceI = 0;
00149 
00150     forAll(meshFaceListPtrs, listI)
00151     {
00152         faceI += meshFaceListPtrs[listI]->size();
00153     }
00154 
00155     meshFaces_.setSize(faceI);
00156 
00157     faceI = 0;
00158 
00159     forAll(meshFaceListPtrs, listI)
00160     {
00161         const labelList& meshFaces = *meshFaceListPtrs[listI];
00162 
00163         forAll(meshFaces, meshFaceI)
00164         {
00165             meshFaces_[faceI++] = meshFaces[meshFaceI];
00166         }
00167     }
00168 
00169     // Generate tight fitting bounding box
00170     calcBb();
00171 }
00172 
00173 
00174 // Construct from all faces in polyPatch. Bounding box calculated.
00175 Foam::octreeDataFace::octreeDataFace(const polyPatch& patch)
00176 :
00177     mesh_(patch.boundaryMesh().mesh()),
00178     meshFaces_(patch.size())
00179 {
00180     forAll(patch, patchFaceI)
00181     {
00182         meshFaces_[patchFaceI] = patch.start() + patchFaceI;
00183     }
00184 
00185     // Generate tight fitting bounding box
00186     calcBb();
00187 }
00188 
00189 
00190 // Construct from primitiveMesh. Inserts all boundary faces.
00191 Foam::octreeDataFace::octreeDataFace(const primitiveMesh& mesh)
00192 :
00193     mesh_(mesh),
00194     meshFaces_(0),
00195     allBb_(0)
00196 {
00197     // Size storage
00198     meshFaces_.setSize(mesh_.nFaces() - mesh_.nInternalFaces());
00199 
00200     // Set info for all boundary faces.
00201     label boundaryFaceI = 0;
00202 
00203     for (label faceI = mesh_.nInternalFaces(); faceI < mesh_.nFaces(); faceI++)
00204     {
00205         meshFaces_[boundaryFaceI++] = faceI;
00206     }
00207 
00208     // Generate tight fitting bounding box
00209     calcBb();
00210 }
00211 
00212 
00213 // Construct as copy
00214 Foam::octreeDataFace::octreeDataFace(const octreeDataFace& shapes)
00215 :
00216     mesh_(shapes.mesh()),
00217     meshFaces_(shapes.meshFaces()),
00218     allBb_(shapes.allBb())
00219 {}
00220 
00221 
00222 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00223 
00224 Foam::octreeDataFace::~octreeDataFace()
00225 {}
00226 
00227 
00228 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00229 
00230 Foam::label Foam::octreeDataFace::getSampleType
00231 (
00232     const octree<octreeDataFace>& oc,
00233     const point& sample
00234 ) const
00235 {
00236     // Need to determine whether sample is 'inside' or 'outside'
00237     // Done by finding nearest face. This gives back a face which is
00238     // guaranteed to contain nearest point. This point can be
00239     // - in interior of face: compare to face normal
00240     // - on edge of face: compare to edge normal
00241     // - on point of face: compare to point normal
00242     // Unfortunately the octree does not give us back the intersection point
00243     // or where on the face it has hit so we have to recreate all that
00244     // information.
00245 
00246     treeBoundBox tightest(treeBoundBox::greatBox);
00247     scalar tightestDist(treeBoundBox::great);
00248     // Find nearest face to sample
00249     label index = oc.findNearest(sample, tightest, tightestDist);
00250 
00251     if (index == -1)
00252     {
00253         FatalErrorIn
00254         (
00255             "octreeDataFace::getSampleType"
00256             "(octree<octreeDataFace>&, const point&)"
00257         )   << "Could not find " << sample << " in octree."
00258             << abort(FatalError);
00259     }
00260 
00261 
00262     // Get actual intersection point on face
00263     label faceI = meshFaces_[index];
00264 
00265     if (debug & 2)
00266     {
00267         Pout<< "getSampleType : sample:" << sample
00268             << " nearest face:" << faceI;
00269     }
00270 
00271     const face& f = mesh_.faces()[faceI];
00272 
00273     const pointField& points = mesh_.points();
00274 
00275     pointHit curHit = f.nearestPoint(sample, points);
00276 
00277     //
00278     // 1] Check whether sample is above face
00279     //
00280 
00281     if (curHit.hit())
00282     {
00283         // Simple case. Compare to face normal.
00284 
00285         if (debug & 2)
00286         {
00287             Pout<< " -> face hit:" << curHit.hitPoint()
00288                 << " comparing to face normal " << mesh_.faceAreas()[faceI]
00289                 << endl;
00290         }
00291         return octree<octreeDataFace>::getVolType
00292         (
00293             mesh_.faceAreas()[faceI],
00294             sample - curHit.hitPoint()
00295         );
00296     }
00297 
00298     if (debug & 2)
00299     {
00300         Pout<< " -> face miss:" << curHit.missPoint();
00301     }
00302 
00303     //
00304     // 2] Check whether intersection is on one of the face vertices or
00305     //    face centre
00306     //
00307 
00308     scalar typDim = sqrt(mag(mesh_.faceAreas()[faceI])) + VSMALL;
00309 
00310     forAll(f, fp)
00311     {
00312         if ((mag(points[f[fp]] - curHit.missPoint())/typDim) < tol)
00313         {
00314             // Face intersection point equals face vertex fp
00315 
00316             // Calculate point normal (wrong: uses face normals instead of
00317             // triangle normals)
00318             const labelList& myFaces = mesh_.pointFaces()[f[fp]];
00319 
00320             vector pointNormal(vector::zero);
00321 
00322             forAll(myFaces, myFaceI)
00323             {
00324                 if (myFaces[myFaceI] >= mesh_.nInternalFaces())
00325                 {
00326                     vector n = mesh_.faceAreas()[myFaces[myFaceI]];
00327                     n /= mag(n) + VSMALL;
00328 
00329                     pointNormal += n;
00330                 }
00331             }
00332 
00333             if (debug & 2)
00334             {
00335                     Pout<< " -> face point hit :" << points[f[fp]]
00336                         << " point normal:" << pointNormal
00337                         << " distance:"
00338                         << mag(points[f[fp]] - curHit.missPoint())/typDim
00339                         << endl;
00340             }
00341             return octree<octreeDataFace>::getVolType
00342             (
00343                 pointNormal,
00344                 sample - curHit.missPoint()
00345             );
00346         }
00347     }
00348     if ((mag(mesh_.faceCentres()[faceI] - curHit.missPoint())/typDim) < tol)
00349     {
00350         // Face intersection point equals face centre. Normal at face centre
00351         // is already average of face normals
00352 
00353         if (debug & 2)
00354         {
00355             Pout<< " -> centre hit:" << mesh_.faceCentres()[faceI]
00356                 << " distance:"
00357                 << mag(mesh_.faceCentres()[faceI] - curHit.missPoint())/typDim
00358                 << endl;
00359         }
00360 
00361         return octree<octreeDataFace>::getVolType
00362         (
00363             mesh_.faceAreas()[faceI],
00364             sample - curHit.missPoint()
00365         );
00366     }
00367 
00368 
00369     //
00370     // 3] Get the 'real' edge the face intersection is on
00371     //
00372 
00373     const labelList& myEdges = mesh_.faceEdges()[faceI];
00374 
00375     forAll(myEdges, myEdgeI)
00376     {
00377         const edge& e = mesh_.edges()[myEdges[myEdgeI]];
00378 
00379         pointHit edgeHit = line<point, const point&>
00380         (
00381             points[e.start()],
00382             points[e.end()]
00383         ).nearestDist(sample);
00384 
00385 
00386         if ((mag(edgeHit.rawPoint() - curHit.missPoint())/typDim) < tol)
00387         {
00388             // Face intersection point lies on edge e
00389 
00390             // Calculate edge normal (wrong: uses face normals instead of
00391             // triangle normals)
00392             const labelList& myFaces = mesh_.edgeFaces()[myEdges[myEdgeI]];
00393 
00394             vector edgeNormal(vector::zero);
00395 
00396             forAll(myFaces, myFaceI)
00397             {
00398                 if (myFaces[myFaceI] >= mesh_.nInternalFaces())
00399                 {
00400                     vector n = mesh_.faceAreas()[myFaces[myFaceI]];
00401                     n /= mag(n) + VSMALL;
00402 
00403                     edgeNormal += n;
00404                 }
00405             }
00406 
00407             if (debug & 2)
00408             {
00409                 Pout<< " -> real edge hit point:" << edgeHit.rawPoint()
00410                     << " comparing to edge normal:" << edgeNormal
00411                     << endl;
00412             }
00413 
00414             // Found face intersection point on this edge. Compare to edge
00415             // normal
00416             return octree<octreeDataFace>::getVolType
00417             (
00418                 edgeNormal,
00419                 sample - curHit.missPoint()
00420             );
00421         }
00422     }
00423 
00424 
00425     //
00426     // 4] Get the internal edge the face intersection is on
00427     //
00428 
00429     forAll(f, fp)
00430     {
00431         pointHit edgeHit =
00432             line<point, const point&>
00433             (
00434                 points[f[fp]],
00435                 mesh_.faceCentres()[faceI]
00436             ).nearestDist(sample);
00437 
00438         if ((mag(edgeHit.rawPoint() - curHit.missPoint())/typDim) < tol)
00439         {
00440             // Face intersection point lies on edge between two face triangles
00441 
00442             // Calculate edge normal as average of the two triangle normals
00443             const label fpPrev = f.rcIndex(fp);
00444             const label fpNext = f.fcIndex(fp);
00445 
00446             vector e = points[f[fp]] - mesh_.faceCentres()[faceI];
00447             vector ePrev = points[f[fpPrev]] - mesh_.faceCentres()[faceI];
00448             vector eNext = points[f[fpNext]] - mesh_.faceCentres()[faceI];
00449 
00450             vector nLeft = ePrev ^ e;
00451             nLeft /= mag(nLeft) + VSMALL;
00452 
00453             vector nRight = e ^ eNext;
00454             nRight /= mag(nRight) + VSMALL;
00455 
00456             if (debug & 2)
00457             {
00458                 Pout<< " -> internal edge hit point:" << edgeHit.rawPoint()
00459                     << " comparing to edge normal "
00460                     << 0.5*(nLeft + nRight)
00461                     << endl;
00462             }
00463 
00464             // Found face intersection point on this edge. Compare to edge
00465             // normal
00466             return octree<octreeDataFace>::getVolType
00467             (
00468                 0.5*(nLeft + nRight),
00469                 sample - curHit.missPoint()
00470             );
00471         }
00472     }
00473 
00474     if (debug & 2)
00475     {
00476         Pout<< "Did not find sample " << sample
00477             << " anywhere related to nearest face " << faceI << endl
00478             << "Face:";
00479 
00480         forAll(f, fp)
00481         {
00482             Pout<< "    vertex:" << f[fp] << "  coord:" << points[f[fp]]
00483                 << endl;
00484         }
00485     }
00486 
00487     // Can't determine status of sample with respect to nearest face.
00488     // Either
00489     // - tolerances are wrong. (if e.g. face has zero area)
00490     // - or (more likely) surface is not closed.
00491 
00492     return octree<octreeDataFace>::UNKNOWN;
00493 }
00494 
00495 
00496 bool Foam::octreeDataFace::overlaps
00497 (
00498     const label index,
00499     const treeBoundBox& sampleBb
00500 ) const
00501 {
00502     //return sampleBb.overlaps(allBb_[index]);
00503 
00504     //- Exact test of face intersecting bb
00505 
00506     // 1. Quick rejection: bb does not intersect face bb at all
00507     if (!sampleBb.overlaps(allBb_[index]))
00508     {
00509         return false;
00510     }
00511 
00512     // 2. Check if one or more face points inside
00513     label faceI = meshFaces_[index];
00514 
00515     const face& f = mesh_.faces()[faceI];
00516 
00517     const pointField& points = mesh_.points();
00518 
00519     forAll(f, fp)
00520     {
00521         if (sampleBb.contains(points[f[fp]]))
00522         {
00523             return true;
00524         }
00525     }
00526 
00527     // 3. Difficult case: all points are outside but connecting edges might
00528     // go through cube. Use triangle-bounding box intersection.
00529     const point& fc = mesh_.faceCentres()[faceI];
00530 
00531     forAll(f, fp)
00532     {
00533         const label fp1 = f.fcIndex(fp);
00534 
00535         bool triIntersects = triangleFuncs::intersectBb
00536         (
00537             points[f[fp]],
00538             points[f[fp1]],
00539             fc,
00540             sampleBb
00541         );
00542 
00543         if (triIntersects)
00544         {
00545             return true;
00546         }
00547     }
00548     return false;
00549 }
00550 
00551 
00552 bool Foam::octreeDataFace::contains(const label, const point&) const
00553 {
00554     notImplemented
00555     (
00556         "octreeDataFace::contains(const label, const point&)"
00557     );
00558     return false;
00559 }
00560 
00561 
00562 bool Foam::octreeDataFace::intersects
00563 (
00564     const label index,
00565     const point& start,
00566     const point& end,
00567     point& intersectionPoint
00568 ) const
00569 {
00570     label faceI = meshFaces_[index];
00571 
00572     const face& f = mesh_.faces()[faceI];
00573 
00574     const vector dir(end - start);
00575 
00576     // Disable picking up intersections behind us.
00577     scalar oldTol = intersection::setPlanarTol(0.0);
00578 
00579     pointHit inter = f.ray
00580     (
00581         start,
00582         dir,
00583         mesh_.points(),
00584         intersection::HALF_RAY,
00585         intersection::VECTOR
00586     );
00587 
00588     intersection::setPlanarTol(oldTol);
00589 
00590     if (inter.hit() && inter.distance() <= mag(dir))
00591     {
00592         intersectionPoint = inter.hitPoint();
00593 
00594         return true;
00595     }
00596     else
00597     {
00598         return false;
00599     }
00600 }
00601 
00602 
00603 bool Foam::octreeDataFace::findTightest
00604 (
00605     const label index,
00606     const point& sample,
00607     treeBoundBox& tightest
00608 ) const
00609 {
00610     // Get furthest away vertex
00611     point myNear, myFar;
00612     allBb_[index].calcExtremities(sample, myNear, myFar);
00613 
00614     const point dist = myFar - sample;
00615     scalar myFarDist = mag(dist);
00616 
00617     point tightestNear, tightestFar;
00618     tightest.calcExtremities(sample, tightestNear, tightestFar);
00619 
00620     scalar tightestFarDist = mag(tightestFar - sample);
00621 
00622     if (tightestFarDist < myFarDist)
00623     {
00624         // Keep current tightest.
00625         return false;
00626     }
00627     else
00628     {
00629         // Construct bb around sample and myFar
00630         const point dist2(fabs(dist.x()), fabs(dist.y()), fabs(dist.z()));
00631 
00632         tightest.min() = sample - dist2;
00633         tightest.max() = sample + dist2;
00634 
00635         return true;
00636     }
00637 }
00638 
00639 
00640 // Determine numerical value of sign of sample compared to shape at index
00641 Foam::scalar Foam::octreeDataFace::calcSign
00642 (
00643     const label index,
00644     const point& sample,
00645     point& n
00646 ) const
00647 {
00648     label faceI = meshFaces_[index];
00649 
00650     n = mesh_.faceAreas()[faceI];
00651 
00652     n /= mag(n) + VSMALL;
00653 
00654     vector vec = sample - mesh_.faceCentres()[faceI];
00655 
00656     vec /= mag(vec) + VSMALL;
00657 
00658     return n & vec;
00659 }
00660 
00661 
00662 // Calculate nearest point on/in shapei
00663 Foam::scalar Foam::octreeDataFace::calcNearest
00664 (
00665     const label index,
00666     const point& sample,
00667     point& nearest
00668 ) const
00669 {
00670     label faceI = meshFaces_[index];
00671 
00672     const face& f = mesh_.faces()[faceI];
00673 
00674     pointHit nearHit = f.nearestPoint(sample, mesh_.points());
00675 
00676     nearest = nearHit.rawPoint();
00677 
00678     if (debug & 1)
00679     {
00680         const point& ctr = mesh_.faceCentres()[faceI];
00681 
00682         scalar sign = mesh_.faceAreas()[faceI] & (sample - nearest);
00683 
00684         Pout<< "octreeDataFace::calcNearest : "
00685             << "sample:" << sample
00686             << "  index:" << index
00687             << "  faceI:" << faceI
00688             << "  ctr:" << ctr
00689             << "  sign:" << sign
00690             << "  nearest point:" << nearest
00691             << "  distance to face:" << nearHit.distance()
00692             << endl;
00693     }
00694     return nearHit.distance();
00695 }
00696 
00697 
00698 // Calculate nearest point on/in shapei
00699 Foam::scalar Foam::octreeDataFace::calcNearest
00700 (
00701     const label index,
00702     const linePointRef& ln,
00703     point& linePt,
00704     point& shapePt
00705 ) const
00706 {
00707     notImplemented
00708     (
00709         "octreeDataFace::calcNearest(const label, const linePointRef&"
00710         ", point&, point&)"
00711     );
00712     return GREAT;
00713 }
00714 
00715 
00716 void Foam::octreeDataFace::write(Ostream& os, const label index) const
00717 {
00718     os << meshFaces_[index] << " " << allBb_[index];
00719 }
00720 
00721 
00722 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd