fix face direction
[OpenFOAM-1.5.x.git] / src / meshTools / indexedOctree / treeDataEdge.C
blobd918fab9447596f591fefc975eb47e55bac8cb07
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2008 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 Description
27 \*---------------------------------------------------------------------------*/
29 #include "treeDataEdge.H"
30 #include "indexedOctree.H"
31 #include "polyMesh.H"
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
38 Foam::treeBoundBox Foam::treeDataEdge::calcBb(const label edgeI) const
40     const edge& e = edges_[edgeI];
41     const point& p0 = points_[e[0]];
42     const point& p1 = points_[e[1]];
44     return treeBoundBox(min(p0, p1), max(p0, p1));
48 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
50 // Construct from components
51 Foam::treeDataEdge::treeDataEdge
53     const bool cacheBb,
54     const edgeList& edges,
55     const pointField& points,
56     const labelList& edgeLabels
59     edges_(edges),
60     points_(points),
61     edgeLabels_(edgeLabels),
62     cacheBb_(cacheBb)
64     if (cacheBb_)
65     {
66         bbs_.setSize(edgeLabels_.size());
68         forAll(edgeLabels_, i)
69         {
70             bbs_[i] = calcBb(edgeLabels_[i]);
71         }
72     }
76 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
78 Foam::pointField Foam::treeDataEdge::points() const
80     pointField eMids(edgeLabels_.size());
82     forAll(edgeLabels_, i)
83     {
84         const edge& e = edges_[edgeLabels_[i]];
86         eMids[i] = e.centre(points_);
87     }
88     return eMids;
92 //- Get type (inside,outside,mixed,unknown) of point w.r.t. surface.
93 //  Only makes sense for closed surfaces.
94 Foam::label Foam::treeDataEdge::getVolumeType
96     const indexedOctree<treeDataEdge>& oc,
97     const point& sample
98 ) const
100     return indexedOctree<treeDataEdge>::UNKNOWN;
104 // Check if any point on shape is inside cubeBb.
105 bool Foam::treeDataEdge::overlaps
107     const label index,
108     const treeBoundBox& cubeBb
109 ) const
111     if (cacheBb_)
112     {
113         return cubeBb.intersects(bbs_[index]);
114     }
115     else
116     {
117         return cubeBb.intersects(calcBb(edgeLabels_[index]));
118     }
122 // Calculate nearest point to sample. Updates (if any) nearestDistSqr, minIndex,
123 // nearestPoint.
124 void Foam::treeDataEdge::findNearest
126     const labelList& indices,
127     const point& sample,
129     scalar& nearestDistSqr,
130     label& minIndex,
131     point& nearestPoint
132 ) const
134     forAll(indices, i)
135     {
136         label index = indices[i];
138         const edge& e = edges_[index];
140         pointHit nearHit = e.line(points_).nearestDist(sample);
142         scalar distSqr = sqr(nearHit.distance());
144         if (distSqr < nearestDistSqr)
145         {
146             nearestDistSqr = distSqr;
147             minIndex = index;
148             nearestPoint = nearHit.rawPoint();
149         }
150     }
154 //- Calculates nearest (to line) point in shape.
155 //  Returns point and distance (squared)
156 void Foam::treeDataEdge::findNearest
158     const labelList& indices,
159     const linePointRef& ln,
161     treeBoundBox& tightest,
162     label& minIndex,
163     point& linePoint,
164     point& nearestPoint
165 ) const
167     // Best so far
168     scalar nearestDistSqr = magSqr(linePoint - nearestPoint);
170     forAll(indices, i)
171     {
172         label index = indices[i];
174         const edge& e = edges_[index];
176         // Note: could do bb test ? Worthwhile?
178         // Nearest point on line
179         point ePoint, lnPt;
180         scalar dist = e.line(points_).nearestDist(ln, ePoint, lnPt);
181         scalar distSqr = sqr(dist);
183         if (distSqr < nearestDistSqr)
184         {
185             nearestDistSqr = distSqr;
186             minIndex = index;
187             linePoint = lnPt;
188             nearestPoint = ePoint;
190             {
191                 point& minPt = tightest.min();
192                 minPt = min(ln.start(), ln.end());
193                 minPt.x() -= dist;
194                 minPt.y() -= dist;
195                 minPt.z() -= dist;
196             }
197             {
198                 point& maxPt = tightest.max();
199                 maxPt = max(ln.start(), ln.end());
200                 maxPt.x() += dist;
201                 maxPt.y() += dist;
202                 maxPt.z() += dist;
203             }
204         }
205     }
209 // ************************************************************************* //