Formatting
[foam-extend-3.2.git] / src / foam / algorithms / octree / indexedOctree / treeDataEdge.C
blob2faee93511f8cdadac45e9fda4b90b0396ee9ead
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     | Version:     3.2
5     \\  /    A nd           | Web:         http://www.foam-extend.org
6      \\/     M anipulation  | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9     This file is part of foam-extend.
11     foam-extend 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 3 of the License, or (at your
14     option) any later version.
16     foam-extend is distributed in the hope that it will be useful, but
17     WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
21     You should have received a copy of the GNU General Public License
22     along with foam-extend.  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 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
48 // Construct from components
49 Foam::treeDataEdge::treeDataEdge
51     const bool cacheBb,
52     const edgeList& edges,
53     const pointField& points,
54     const labelList& edgeLabels
57     edges_(edges),
58     points_(points),
59     edgeLabels_(edgeLabels),
60     cacheBb_(cacheBb)
62     if (cacheBb_)
63     {
64         bbs_.setSize(edgeLabels_.size());
66         forAll(edgeLabels_, i)
67         {
68             bbs_[i] = calcBb(edgeLabels_[i]);
69         }
70     }
74 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
76 Foam::pointField Foam::treeDataEdge::points() const
78     pointField eMids(edgeLabels_.size());
80     forAll(edgeLabels_, i)
81     {
82         const edge& e = edges_[edgeLabels_[i]];
84         eMids[i] = e.centre(points_);
85     }
86     return eMids;
90 //- Get type (inside,outside,mixed,unknown) of point w.r.t. surface.
91 //  Only makes sense for closed surfaces.
92 Foam::label Foam::treeDataEdge::getVolumeType
94     const indexedOctree<treeDataEdge>& oc,
95     const point& sample
96 ) const
98     return indexedOctree<treeDataEdge>::UNKNOWN;
102 // Check if any point on shape is inside cubeBb.
103 bool Foam::treeDataEdge::overlaps
105     const label index,
106     const treeBoundBox& cubeBb
107 ) const
109     if (cacheBb_)
110     {
111         return cubeBb.overlaps(bbs_[index]);
112     }
113     else
114     {
115         return cubeBb.overlaps(calcBb(edgeLabels_[index]));
116     }
120 // Calculate nearest point to sample. Updates (if any) nearestDistSqr, minIndex,
121 // nearestPoint.
122 void Foam::treeDataEdge::findNearest
124     const labelList& indices,
125     const point& sample,
127     scalar& nearestDistSqr,
128     label& minIndex,
129     point& nearestPoint
130 ) const
132     forAll(indices, i)
133     {
134         label index = indices[i];
136         const edge& e = edges_[index];
138         pointHit nearHit = e.line(points_).nearestDist(sample);
140         scalar distSqr = sqr(nearHit.distance());
142         if (distSqr < nearestDistSqr)
143         {
144             nearestDistSqr = distSqr;
145             minIndex = index;
146             nearestPoint = nearHit.rawPoint();
147         }
148     }
152 //- Calculates nearest (to line) point in shape.
153 //  Returns point and distance (squared)
154 void Foam::treeDataEdge::findNearest
156     const labelList& indices,
157     const linePointRef& ln,
159     treeBoundBox& tightest,
160     label& minIndex,
161     point& linePoint,
162     point& nearestPoint
163 ) const
165     // Best so far
166     scalar nearestDistSqr = magSqr(linePoint - nearestPoint);
168     forAll(indices, i)
169     {
170         label index = indices[i];
172         const edge& e = edges_[index];
174         // Note: could do bb test ? Worthwhile?
176         // Nearest point on line
177         point ePoint, lnPt;
178         scalar dist = e.line(points_).nearestDist(ln, ePoint, lnPt);
179         scalar distSqr = sqr(dist);
181         if (distSqr < nearestDistSqr)
182         {
183             nearestDistSqr = distSqr;
184             minIndex = index;
185             linePoint = lnPt;
186             nearestPoint = ePoint;
188             {
189                 point& minPt = tightest.min();
190                 minPt = min(ln.start(), ln.end());
191                 minPt.x() -= dist;
192                 minPt.y() -= dist;
193                 minPt.z() -= dist;
194             }
195             {
196                 point& maxPt = tightest.max();
197                 maxPt = max(ln.start(), ln.end());
198                 maxPt.x() += dist;
199                 maxPt.y() += dist;
200                 maxPt.z() += dist;
201             }
202         }
203     }
207 // ************************************************************************* //