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

treeDataEdge.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 "treeDataEdge.H"
00028 #include "indexedOctree.H"
00029 
00030 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00031 
00032 defineTypeNameAndDebug(Foam::treeDataEdge, 0);
00033 
00034 
00035 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00036 
00037 Foam::treeBoundBox Foam::treeDataEdge::calcBb(const label edgeI) const
00038 {
00039     const edge& e = edges_[edgeI];
00040     const point& p0 = points_[e[0]];
00041     const point& p1 = points_[e[1]];
00042 
00043     return treeBoundBox(min(p0, p1), max(p0, p1));
00044 }
00045 
00046 
00047 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00048 
00049 // Construct from components
00050 Foam::treeDataEdge::treeDataEdge
00051 (
00052     const bool cacheBb,
00053     const edgeList& edges,
00054     const pointField& points,
00055     const labelList& edgeLabels
00056 )
00057 :
00058     edges_(edges),
00059     points_(points),
00060     edgeLabels_(edgeLabels),
00061     cacheBb_(cacheBb)
00062 {
00063     if (cacheBb_)
00064     {
00065         bbs_.setSize(edgeLabels_.size());
00066 
00067         forAll(edgeLabels_, i)
00068         {
00069             bbs_[i] = calcBb(edgeLabels_[i]);
00070         }
00071     }
00072 }
00073 
00074 
00075 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00076 
00077 Foam::pointField Foam::treeDataEdge::points() const
00078 {
00079     pointField eMids(edgeLabels_.size());
00080 
00081     forAll(edgeLabels_, i)
00082     {
00083         const edge& e = edges_[edgeLabels_[i]];
00084 
00085         eMids[i] = e.centre(points_);
00086     }
00087     return eMids;
00088 }
00089 
00090 
00091 //- Get type (inside,outside,mixed,unknown) of point w.r.t. surface.
00092 //  Only makes sense for closed surfaces.
00093 Foam::label Foam::treeDataEdge::getVolumeType
00094 (
00095     const indexedOctree<treeDataEdge>& oc,
00096     const point& sample
00097 ) const
00098 {
00099     return indexedOctree<treeDataEdge>::UNKNOWN;
00100 }
00101 
00102 
00103 // Check if any point on shape is inside cubeBb.
00104 bool Foam::treeDataEdge::overlaps
00105 (
00106     const label index,
00107     const treeBoundBox& cubeBb
00108 ) const
00109 {
00110     if (cacheBb_)
00111     {
00112         return cubeBb.overlaps(bbs_[index]);
00113     }
00114     else
00115     {
00116         return cubeBb.overlaps(calcBb(edgeLabels_[index]));
00117     }
00118 }
00119 
00120 
00121 // Calculate nearest point to sample. Updates (if any) nearestDistSqr, minIndex,
00122 // nearestPoint.
00123 void Foam::treeDataEdge::findNearest
00124 (
00125     const labelList& indices,
00126     const point& sample,
00127 
00128     scalar& nearestDistSqr,
00129     label& minIndex,
00130     point& nearestPoint
00131 ) const
00132 {
00133     forAll(indices, i)
00134     {
00135         label index = indices[i];
00136 
00137         const edge& e = edges_[edgeLabels_[index]];
00138 
00139         pointHit nearHit = e.line(points_).nearestDist(sample);
00140 
00141         scalar distSqr = sqr(nearHit.distance());
00142 
00143         if (distSqr < nearestDistSqr)
00144         {
00145             nearestDistSqr = distSqr;
00146             minIndex = index;
00147             nearestPoint = nearHit.rawPoint();
00148         }
00149     }
00150 }
00151 
00152 
00153 //- Calculates nearest (to line) point in shape.
00154 //  Returns point and distance (squared)
00155 void Foam::treeDataEdge::findNearest
00156 (
00157     const labelList& indices,
00158     const linePointRef& ln,
00159 
00160     treeBoundBox& tightest,
00161     label& minIndex,
00162     point& linePoint,
00163     point& nearestPoint
00164 ) const
00165 {
00166     // Best so far
00167     scalar nearestDistSqr = magSqr(linePoint - nearestPoint);
00168 
00169     forAll(indices, i)
00170     {
00171         label index = indices[i];
00172 
00173         const edge& e = edges_[edgeLabels_[index]];
00174 
00175         // Note: could do bb test ? Worthwhile?
00176 
00177         // Nearest point on line
00178         point ePoint, lnPt;
00179         scalar dist = e.line(points_).nearestDist(ln, ePoint, lnPt);
00180         scalar distSqr = sqr(dist);
00181 
00182         if (distSqr < nearestDistSqr)
00183         {
00184             nearestDistSqr = distSqr;
00185             minIndex = index;
00186             linePoint = lnPt;
00187             nearestPoint = ePoint;
00188 
00189             {
00190                 point& minPt = tightest.min();
00191                 minPt = min(ln.start(), ln.end());
00192                 minPt.x() -= dist;
00193                 minPt.y() -= dist;
00194                 minPt.z() -= dist;
00195             }
00196             {
00197                 point& maxPt = tightest.max();
00198                 maxPt = max(ln.start(), ln.end());
00199                 maxPt.x() += dist;
00200                 maxPt.y() += dist;
00201                 maxPt.z() += dist;
00202             }
00203         }
00204     }
00205 }
00206 
00207 
00208 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd