ENH: autoLayerDriver: better layering information message
[OpenFOAM-2.0.x.git] / src / meshTools / indexedOctree / treeDataEdge.C
blobd3945822365653f909edc5cd04fba28719507715
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
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
13     the Free Software Foundation, either version 3 of the License, or
14     (at your 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, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "treeDataEdge.H"
27 #include "indexedOctree.H"
29 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 defineTypeNameAndDebug(Foam::treeDataEdge, 0);
34 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
36 Foam::treeBoundBox Foam::treeDataEdge::calcBb(const label edgeI) const
38     const edge& e = edges_[edgeI];
39     const point& p0 = points_[e[0]];
40     const point& p1 = points_[e[1]];
42     return treeBoundBox(min(p0, p1), max(p0, p1));
46 void Foam::treeDataEdge::update()
48     if (cacheBb_)
49     {
50         bbs_.setSize(edgeLabels_.size());
52         forAll(edgeLabels_, i)
53         {
54             bbs_[i] = calcBb(edgeLabels_[i]);
55         }
56     }
60 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
62 Foam::treeDataEdge::treeDataEdge
64     const bool cacheBb,
65     const edgeList& edges,
66     const pointField& points,
67     const labelUList& edgeLabels
70     edges_(edges),
71     points_(points),
72     edgeLabels_(edgeLabels),
73     cacheBb_(cacheBb)
75     update();
79 Foam::treeDataEdge::treeDataEdge
81     const bool cacheBb,
82     const edgeList& edges,
83     const pointField& points,
84     const Xfer<labelList>& edgeLabels
87     edges_(edges),
88     points_(points),
89     edgeLabels_(edgeLabels),
90     cacheBb_(cacheBb)
92     update();
96 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
98 Foam::pointField Foam::treeDataEdge::shapePoints() const
100     pointField eMids(edgeLabels_.size());
102     forAll(edgeLabels_, i)
103     {
104         const edge& e = edges_[edgeLabels_[i]];
106         eMids[i] = e.centre(points_);
107     }
108     return eMids;
112 //- Get type (inside,outside,mixed,unknown) of point w.r.t. surface.
113 //  Only makes sense for closed surfaces.
114 Foam::label Foam::treeDataEdge::getVolumeType
116     const indexedOctree<treeDataEdge>& oc,
117     const point& sample
118 ) const
120     return indexedOctree<treeDataEdge>::UNKNOWN;
124 // Check if any point on shape is inside cubeBb.
125 bool Foam::treeDataEdge::overlaps
127     const label index,
128     const treeBoundBox& cubeBb
129 ) const
131     if (cacheBb_)
132     {
133         return cubeBb.overlaps(bbs_[index]);
134     }
135     else
136     {
137         return cubeBb.overlaps(calcBb(edgeLabels_[index]));
138     }
142 // Calculate nearest point to sample. Updates (if any) nearestDistSqr, minIndex,
143 // nearestPoint.
144 void Foam::treeDataEdge::findNearest
146     const labelUList& indices,
147     const point& sample,
149     scalar& nearestDistSqr,
150     label& minIndex,
151     point& nearestPoint
152 ) const
154     forAll(indices, i)
155     {
156         const label index = indices[i];
158         const edge& e = edges_[edgeLabels_[index]];
160         pointHit nearHit = e.line(points_).nearestDist(sample);
162         scalar distSqr = sqr(nearHit.distance());
164         if (distSqr < nearestDistSqr)
165         {
166             nearestDistSqr = distSqr;
167             minIndex = index;
168             nearestPoint = nearHit.rawPoint();
169         }
170     }
174 //- Calculates nearest (to line) point in shape.
175 //  Returns point and distance (squared)
176 void Foam::treeDataEdge::findNearest
178     const labelUList& indices,
179     const linePointRef& ln,
181     treeBoundBox& tightest,
182     label& minIndex,
183     point& linePoint,
184     point& nearestPoint
185 ) const
187     // Best so far
188     scalar nearestDistSqr = magSqr(linePoint - nearestPoint);
190     forAll(indices, i)
191     {
192         const label index = indices[i];
194         const edge& e = edges_[edgeLabels_[index]];
196         // Note: could do bb test ? Worthwhile?
198         // Nearest point on line
199         point ePoint, lnPt;
200         scalar dist = e.line(points_).nearestDist(ln, ePoint, lnPt);
201         scalar distSqr = sqr(dist);
203         if (distSqr < nearestDistSqr)
204         {
205             nearestDistSqr = distSqr;
206             minIndex = index;
207             linePoint = lnPt;
208             nearestPoint = ePoint;
210             {
211                 point& minPt = tightest.min();
212                 minPt = min(ln.start(), ln.end());
213                 minPt.x() -= dist;
214                 minPt.y() -= dist;
215                 minPt.z() -= dist;
216             }
217             {
218                 point& maxPt = tightest.max();
219                 maxPt = max(ln.start(), ln.end());
220                 maxPt.x() += dist;
221                 maxPt.y() += dist;
222                 maxPt.z() += dist;
223             }
224         }
225     }
229 // ************************************************************************* //