FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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