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

octreeDataFaceList.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 "octreeDataFaceList.H"
00030 #include "octree.H"
00031 
00032 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00033 
00034 defineTypeNameAndDebug(Foam::octreeDataFaceList, 0);
00035 
00036 Foam::scalar Foam::octreeDataFaceList::tol = 1E-6;
00037 
00038 
00039 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00040 
00041 void Foam::octreeDataFaceList::calcBb()
00042 {
00043     allBb_.setSize(faceLabels_.size());
00044     allBb_ = treeBoundBox
00045     (
00046         vector(GREAT, GREAT, GREAT),
00047         vector(-GREAT, -GREAT, -GREAT)
00048     );
00049 
00050     forAll (faceLabels_, faceLabelI)
00051     {
00052         label faceI = faceLabels_[faceLabelI];
00053 
00054         // Update bb of face
00055         treeBoundBox& myBb = allBb_[faceLabelI];
00056 
00057         const face& f = mesh_.localFaces()[faceI];
00058 
00059         forAll(f, fp)
00060         {
00061             const point& coord = mesh_.localPoints()[f[fp]];
00062 
00063             myBb.min() = min(myBb.min(), coord);
00064             myBb.max() = max(myBb.max(), coord);
00065         }
00066     }
00067 }
00068 
00069 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00070 
00071 // Construct from all faces in bMesh
00072 Foam::octreeDataFaceList::octreeDataFaceList(const bMesh& mesh)
00073 :
00074     mesh_(mesh),
00075     faceLabels_(mesh_.size()),
00076     allBb_(mesh_.size())
00077 {
00078     forAll(faceLabels_, faceI)
00079     {
00080         faceLabels_[faceI] = faceI;
00081     }
00082 
00083     // Generate tight fitting bounding box
00084     calcBb();
00085 }
00086 
00087 
00088 // Construct from selected faces in bMesh
00089 Foam::octreeDataFaceList::octreeDataFaceList
00090 (
00091     const bMesh& mesh,
00092     const labelList& faceLabels
00093 )
00094 :
00095     mesh_(mesh),
00096     faceLabels_(faceLabels),
00097     allBb_(faceLabels.size())
00098 {
00099     // Generate tight fitting bounding box
00100     calcBb();
00101 }
00102 
00103 
00104 
00105 // Construct as copy
00106 Foam::octreeDataFaceList::octreeDataFaceList(const octreeDataFaceList& shapes)
00107 :
00108     mesh_(shapes.mesh()),
00109     faceLabels_(shapes.faceLabels()),
00110     allBb_(shapes.allBb())
00111 {}
00112 
00113 
00114 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00115 
00116 Foam::octreeDataFaceList::~octreeDataFaceList()
00117 {}
00118 
00119 
00120 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00121 
00122 
00123 Foam::label Foam::octreeDataFaceList::getSampleType
00124 (
00125     const octree<octreeDataFaceList>& oc,
00126     const point& sample
00127 ) const
00128 {
00129     // Need to determine whether sample is 'inside' or 'outside'
00130     // Done by finding nearest face. This gives back a face which is
00131     // guaranteed to contain nearest point. This point can be
00132     // - in interior of face: compare to face normal
00133     // - on edge of face: compare to edge normal
00134     // - on point of face: compare to point normal
00135     // Unfortunately the octree does not give us back the intersection point
00136     // or where on the face it has hit so we have to recreate all that
00137     // information.
00138 
00139 
00140     // Find nearest face to sample
00141     treeBoundBox tightest(treeBoundBox::greatBox);
00142 
00143     scalar tightestDist = GREAT;
00144 
00145     label index = oc.findNearest(sample, tightest, tightestDist);
00146 
00147     if (index == -1)
00148     {
00149         FatalErrorIn
00150         (
00151             "octreeDataFaceList::getSampleType"
00152             "(octree<octreeDataFaceList>&, const point&)"
00153         )   << "Could not find " << sample << " in octree."
00154             << abort(FatalError);
00155     }
00156 
00157     label faceI = faceLabels_[index];
00158 
00159     // Get actual intersection point on face
00160 
00161     if (debug & 2)
00162     {
00163         Pout<< "getSampleType : sample:" << sample
00164             << " nearest face:" << faceI;
00165     }
00166 
00167     const face& f = mesh_.localFaces()[faceI];
00168 
00169     const pointField& points = mesh_.localPoints();
00170 
00171     pointHit curHit = f.nearestPoint(sample, points);
00172 
00173     //
00174     // 1] Check whether sample is above face
00175     //
00176 
00177     if (curHit.hit())
00178     {
00179         // Simple case. Compare to face normal.
00180 
00181         if (debug & 2)
00182         {
00183             Pout<< " -> face hit:" << curHit.hitPoint()
00184                 << " comparing to face normal " << mesh_.faceNormals()[faceI]
00185                 << endl;
00186         }
00187         return octree<octreeDataFaceList>::getVolType
00188         (
00189             mesh_.faceNormals()[faceI],
00190             sample - curHit.hitPoint()
00191         );
00192     }
00193 
00194     if (debug & 2)
00195     {
00196         Pout<< " -> face miss:" << curHit.missPoint();
00197     }
00198 
00199     //
00200     // 2] Check whether intersection is on one of the face vertices or
00201     //    face centre
00202     //
00203 
00204     // typical dimension as sqrt of face area.
00205     scalar typDim = sqrt(mag(f.normal(points))) + VSMALL;
00206 
00207     forAll(f, fp)
00208     {
00209         if ((mag(points[f[fp]] - curHit.missPoint())/typDim) < tol)
00210         {
00211             // Face intersection point equals face vertex fp
00212 
00213             if (debug & 2)
00214             {
00215                     Pout<< " -> face point hit :" << points[f[fp]]
00216                         << " point normal:" << mesh_.pointNormals()[f[fp]]
00217                         << " distance:"
00218                         << mag(points[f[fp]] - curHit.missPoint())/typDim
00219                         << endl;
00220             }
00221             return octree<octreeDataFaceList>::getVolType
00222             (
00223                 mesh_.pointNormals()[f[fp]],
00224                 sample - curHit.missPoint()
00225             );
00226         }
00227     }
00228 
00229     // Get face centre
00230     point ctr(f.centre(points));
00231 
00232     if ((mag(ctr - curHit.missPoint())/typDim) < tol)
00233     {
00234         // Face intersection point equals face centre. Normal at face centre
00235         // is already average of face normals
00236 
00237         if (debug & 2)
00238         {
00239             Pout<< " -> centre hit:" << ctr
00240                 << " distance:"
00241                 << mag(ctr - curHit.missPoint())/typDim
00242                 << endl;
00243         }
00244 
00245         return octree<octreeDataFaceList>::getVolType
00246         (
00247             mesh_.faceNormals()[faceI],
00248             sample - curHit.missPoint()
00249         );
00250     }
00251 
00252 
00253     //
00254     // 3] Get the 'real' edge the face intersection is on
00255     //
00256 
00257     const labelList& myEdges = mesh_.faceEdges()[faceI];
00258 
00259     forAll(myEdges, myEdgeI)
00260     {
00261         const edge& e = mesh_.edges()[myEdges[myEdgeI]];
00262 
00263         pointHit edgeHit =
00264             line<point, const point&>
00265             (
00266                 points[e.start()],
00267                 points[e.end()]
00268             ).nearestDist(sample);
00269 
00270         point edgePoint;
00271         if (edgeHit.hit())
00272         {
00273             edgePoint = edgeHit.hitPoint();
00274         }
00275         else
00276         {
00277             edgePoint = edgeHit.missPoint();
00278         }
00279 
00280 
00281         if ((mag(edgePoint - curHit.missPoint())/typDim) < tol)
00282         {
00283             // Face intersection point lies on edge e
00284 
00285             // Calculate edge normal (wrong: uses face normals instead of
00286             // triangle normals)
00287             const labelList& myFaces = mesh_.edgeFaces()[myEdges[myEdgeI]];
00288 
00289             vector edgeNormal(vector::zero);
00290 
00291             forAll(myFaces, myFaceI)
00292             {
00293                 edgeNormal += mesh_.faceNormals()[myFaces[myFaceI]];
00294             }
00295 
00296             if (debug & 2)
00297             {
00298                 Pout<< " -> real edge hit point:" << edgePoint
00299                     << " comparing to edge normal:" << edgeNormal
00300                     << endl;
00301             }
00302 
00303             // Found face intersection point on this edge. Compare to edge
00304             // normal
00305             return octree<octreeDataFaceList>::getVolType
00306             (
00307                 edgeNormal,
00308                 sample - curHit.missPoint()
00309             );
00310         }
00311     }
00312 
00313 
00314     //
00315     // 4] Get the internal edge (vertex - faceCentre) the face intersection
00316     //    is on
00317     //
00318 
00319     forAll(f, fp)
00320     {
00321         pointHit edgeHit =
00322             line<point, const point&>
00323             (
00324                 points[f[fp]],
00325                 ctr
00326             ).nearestDist(sample);
00327 
00328         point edgePoint;
00329         if (edgeHit.hit())
00330         {
00331             edgePoint = edgeHit.hitPoint();
00332         }
00333         else
00334         {
00335             edgePoint = edgeHit.missPoint();
00336         }
00337 
00338         if ((mag(edgePoint - curHit.missPoint())/typDim) < tol)
00339         {
00340             // Face intersection point lies on edge between two face triangles
00341 
00342             // Calculate edge normal as average of the two triangle normals
00343             label fpPrev = f.rcIndex(fp);
00344             label fpNext = f.fcIndex(fp);
00345 
00346             vector e = points[f[fp]] - ctr;
00347             vector ePrev = points[f[fpPrev]] - ctr;
00348             vector eNext = points[f[fpNext]] - ctr;
00349 
00350             vector nLeft = ePrev ^ e;
00351             nLeft /= mag(nLeft) + VSMALL;
00352 
00353             vector nRight = e ^ eNext;
00354             nRight /= mag(nRight) + VSMALL;
00355 
00356             if (debug & 2)
00357             {
00358                 Pout<< " -> internal edge hit point:" << edgePoint
00359                     << " comparing to edge normal "
00360                     << 0.5*(nLeft + nRight)
00361                     << endl;
00362             }
00363 
00364             // Found face intersection point on this edge. Compare to edge
00365             // normal
00366             return octree<octreeDataFaceList>::getVolType
00367             (
00368                 0.5*(nLeft + nRight),
00369                 sample - curHit.missPoint()
00370             );
00371         }
00372     }
00373 
00374     if (debug & 2)
00375     {
00376         Pout<< "Did not find sample " << sample
00377             << " anywhere related to nearest face " << faceI << endl
00378             << "Face:";
00379 
00380         forAll(f, fp)
00381         {
00382             Pout<< "    vertex:" << f[fp] << "  coord:" << points[f[fp]]
00383                 << endl;
00384         }
00385     }
00386 
00387     // Can't determine status of sample with respect to nearest face.
00388     // Either
00389     // - tolerances are wrong. (if e.g. face has zero area)
00390     // - or (more likely) surface is not closed.
00391 
00392     return octree<octreeDataFaceList>::UNKNOWN;
00393 }
00394 
00395 
00396 bool Foam::octreeDataFaceList::overlaps
00397 (
00398     const label index,
00399     const treeBoundBox& sampleBb
00400 ) const
00401 {
00402     return sampleBb.overlaps(allBb_[index]);
00403 }
00404 
00405 
00406 bool Foam::octreeDataFaceList::contains
00407 (
00408     const label,
00409     const point&
00410 ) const
00411 {
00412     notImplemented
00413     (
00414         "octreeDataFaceList::contains(const label, const point&)"
00415     );
00416     return false;
00417 }
00418 
00419 
00420 bool Foam::octreeDataFaceList::intersects
00421 (
00422     const label index,
00423     const point& start,
00424     const point& end,
00425     point& intersectionPoint
00426 ) const
00427 {
00428     label faceI = faceLabels_[index];
00429 
00430     const face& f = mesh_.localFaces()[faceI];
00431 
00432     const vector dir(end - start);
00433 
00434     // Disable picking up intersections behind us.
00435     scalar oldTol = intersection::setPlanarTol(0.0);
00436 
00437     pointHit inter =
00438         f.ray
00439         (
00440             start,
00441             dir,
00442             mesh_.localPoints(),
00443             intersection::HALF_RAY,
00444             intersection::VECTOR
00445         );
00446 
00447     intersection::setPlanarTol(oldTol);
00448 
00449     if (inter.hit() && inter.distance() <= mag(dir))
00450     {
00451         intersectionPoint = inter.hitPoint();
00452 
00453         return true;
00454     }
00455     else
00456     {
00457         return false;
00458     }
00459 }
00460 
00461 
00462 bool Foam::octreeDataFaceList::findTightest
00463 (
00464     const label index,
00465     const point& sample,
00466     treeBoundBox& tightest
00467 ) const
00468 {
00469     // Get nearest and furthest away vertex
00470     point myNear, myFar;
00471     allBb_[index].calcExtremities(sample, myNear, myFar);
00472 
00473     const point dist = myFar - sample;
00474     scalar myFarDist = mag(dist);
00475 
00476     point tightestNear, tightestFar;
00477     tightest.calcExtremities(sample, tightestNear, tightestFar);
00478 
00479     scalar tightestFarDist = mag(tightestFar - sample);
00480 
00481     if (tightestFarDist < myFarDist)
00482     {
00483         // Keep current tightest.
00484         return false;
00485     }
00486     else
00487     {
00488         // Construct bb around sample and myFar
00489         const point dist2(fabs(dist.x()), fabs(dist.y()), fabs(dist.z()));
00490 
00491         tightest.min() = sample - dist2;
00492         tightest.max() = sample + dist2;
00493 
00494         return true;
00495     }
00496 }
00497 
00498 
00499 // Determine numerical value of sign of sample compared to shape at index
00500 Foam::scalar Foam::octreeDataFaceList::calcSign
00501 (
00502     const label index,
00503     const point& sample,
00504     vector&
00505 ) const
00506 {
00507     label faceI = faceLabels_[index];
00508 
00509     const face& f = mesh_.localFaces()[faceI];
00510 
00511     point ctr = f.centre(mesh_.localPoints());
00512 
00513     vector vec = sample - ctr;
00514 
00515     vec /= mag(vec) + VSMALL;
00516 
00517     return mesh_.faceNormals()[faceI] & vec;
00518 }
00519 
00520 
00521 // Calculate nearest point on/in shapei
00522 Foam::scalar Foam::octreeDataFaceList::calcNearest
00523 (
00524     const label index,
00525     const point& sample,
00526     point& nearest
00527 ) const
00528 {
00529     label faceI = faceLabels_[index];
00530 
00531     const face& f = mesh_.localFaces()[faceI];
00532 
00533     pointHit nearHit = f.nearestPoint(sample, mesh_.localPoints());
00534 
00535     if (nearHit.hit())
00536     {
00537         nearest = nearHit.hitPoint();
00538     }
00539     else
00540     {
00541         nearest = nearHit.missPoint();
00542     }
00543 
00544     if (debug & 1)
00545     {
00546         point ctr = f.centre(mesh_.localPoints());
00547 
00548         scalar sign = mesh_.faceNormals()[faceI] & (sample - nearest);
00549 
00550         Pout<< "octreeDataFaceList::calcNearest : "
00551             << "sample:" << sample
00552             << "  faceI:" << faceI
00553             << "  ctr:" << ctr
00554             << "  sign:" << sign
00555             << "  nearest point:" << nearest
00556             << "  distance to face:" << nearHit.distance()
00557             << endl;
00558     }
00559     return nearHit.distance();
00560 }
00561 
00562 
00563 void Foam::octreeDataFaceList::write
00564 (
00565     Ostream& os,
00566     const label index
00567 ) const
00568 {
00569     os << faceLabels_[index] << " " << allBb_[index];
00570 }
00571 
00572 
00573 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd