initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / meshTools / indexedOctree / treeDataEdge.C
blob3f7ae86ce8c27c25bbc2d590ed99b1abecfb1cf8
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software; you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "treeDataEdge.H"
28 #include "indexedOctree.H"
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 defineTypeNameAndDebug(Foam::treeDataEdge, 0);
35 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
37 Foam::treeBoundBox Foam::treeDataEdge::calcBb(const label edgeI) const
39     const edge& e = edges_[edgeI];
40     const point& p0 = points_[e[0]];
41     const point& p1 = points_[e[1]];
43     return treeBoundBox(min(p0, p1), max(p0, p1));
47 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
49 // Construct from components
50 Foam::treeDataEdge::treeDataEdge
52     const bool cacheBb,
53     const edgeList& edges,
54     const pointField& points,
55     const labelList& edgeLabels
58     edges_(edges),
59     points_(points),
60     edgeLabels_(edgeLabels),
61     cacheBb_(cacheBb)
63     if (cacheBb_)
64     {
65         bbs_.setSize(edgeLabels_.size());
67         forAll(edgeLabels_, i)
68         {
69             bbs_[i] = calcBb(edgeLabels_[i]);
70         }
71     }
75 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
77 Foam::pointField Foam::treeDataEdge::points() const
79     pointField eMids(edgeLabels_.size());
81     forAll(edgeLabels_, i)
82     {
83         const edge& e = edges_[edgeLabels_[i]];
85         eMids[i] = e.centre(points_);
86     }
87     return eMids;
91 //- Get type (inside,outside,mixed,unknown) of point w.r.t. surface.
92 //  Only makes sense for closed surfaces.
93 Foam::label Foam::treeDataEdge::getVolumeType
95     const indexedOctree<treeDataEdge>& oc,
96     const point& sample
97 ) const
99     return indexedOctree<treeDataEdge>::UNKNOWN;
103 // Check if any point on shape is inside cubeBb.
104 bool Foam::treeDataEdge::overlaps
106     const label index,
107     const treeBoundBox& cubeBb
108 ) const
110     if (cacheBb_)
111     {
112         return cubeBb.overlaps(bbs_[index]);
113     }
114     else
115     {
116         return cubeBb.overlaps(calcBb(edgeLabels_[index]));
117     }
121 // Calculate nearest point to sample. Updates (if any) nearestDistSqr, minIndex,
122 // nearestPoint.
123 void Foam::treeDataEdge::findNearest
125     const labelList& indices,
126     const point& sample,
128     scalar& nearestDistSqr,
129     label& minIndex,
130     point& nearestPoint
131 ) const
133     forAll(indices, i)
134     {
135         label index = indices[i];
137         const edge& e = edges_[edgeLabels_[index]];
139         pointHit nearHit = e.line(points_).nearestDist(sample);
141         scalar distSqr = sqr(nearHit.distance());
143         if (distSqr < nearestDistSqr)
144         {
145             nearestDistSqr = distSqr;
146             minIndex = index;
147             nearestPoint = nearHit.rawPoint();
148         }
149     }
153 //- Calculates nearest (to line) point in shape.
154 //  Returns point and distance (squared)
155 void Foam::treeDataEdge::findNearest
157     const labelList& indices,
158     const linePointRef& ln,
160     treeBoundBox& tightest,
161     label& minIndex,
162     point& linePoint,
163     point& nearestPoint
164 ) const
166     // Best so far
167     scalar nearestDistSqr = magSqr(linePoint - nearestPoint);
169     forAll(indices, i)
170     {
171         label index = indices[i];
173         const edge& e = edges_[edgeLabels_[index]];
175         // Note: could do bb test ? Worthwhile?
177         // Nearest point on line
178         point ePoint, lnPt;
179         scalar dist = e.line(points_).nearestDist(ln, ePoint, lnPt);
180         scalar distSqr = sqr(dist);
182         if (distSqr < nearestDistSqr)
183         {
184             nearestDistSqr = distSqr;
185             minIndex = index;
186             linePoint = lnPt;
187             nearestPoint = ePoint;
189             {
190                 point& minPt = tightest.min();
191                 minPt = min(ln.start(), ln.end());
192                 minPt.x() -= dist;
193                 minPt.y() -= dist;
194                 minPt.z() -= dist;
195             }
196             {
197                 point& maxPt = tightest.max();
198                 maxPt = max(ln.start(), ln.end());
199                 maxPt.x() += dist;
200                 maxPt.y() += dist;
201                 maxPt.z() += dist;
202             }
203         }
204     }
208 // ************************************************************************* //