Merge branch 'master' of ssh://git.code.sf.net/p/foam-extend/foam-extend-3.2
[foam-extend-3.2.git] / src / meshTools / triSurface / triSurfaceSearch / triSurfaceSearch.C
blob092bdb7af0efbff6d4e6ec27fb07c372a697848a
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 "triSurfaceSearch.H"
27 #include "indexedOctree.H"
28 #include "boolList.H"
29 #include "treeDataTriSurface.H"
30 #include "triSurface.H"
31 #include "line.H"
32 #include "cpuTime.H"
34 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
36 namespace Foam
39 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
41 const point triSurfaceSearch::greatPoint(GREAT, GREAT, GREAT);
44 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
46 // Construct from surface. Holds reference!
47 triSurfaceSearch::triSurfaceSearch(const triSurface& surface)
49     surface_(surface),
50     treePtr_(NULL)
52     // Random number generator. Bit dodgy since not exactly random ;-)
53     Random rndGen(65431);
55     // Slightly extended bb. Slightly off-centred just so on symmetric
56     // geometry there are less face/edge aligned items.
57     treeBoundBox treeBb
58     (
59         treeBoundBox(surface_.points(), surface_.meshPoints()).extend
60         (
61             rndGen,
62             1E-4
63         )
64     );
65     treeBb.min() -= point(ROOTVSMALL, ROOTVSMALL, ROOTVSMALL);
66     treeBb.max() += point(ROOTVSMALL, ROOTVSMALL, ROOTVSMALL);
68     treePtr_.reset
69     (
70         new indexedOctree<treeDataTriSurface>
71         (
72             treeDataTriSurface(surface_),
73             treeBb,
74             8,      // maxLevel
75             10,     // leafsize
76             3.0     // duplicity
77         )
78     );
82 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
84 // Determine inside/outside for samples
85 boolList triSurfaceSearch::calcInside(const pointField& samples) const
87     boolList inside(samples.size());
89     forAll(samples, sampleI)
90     {
91         const point& sample = samples[sampleI];
93         if (!tree().bb().contains(sample))
94         {
95             inside[sampleI] = false;
96         }
97         else if
98         (
99             tree().getVolumeType(sample)
100          == indexedOctree<treeDataTriSurface>::INSIDE
101         )
102         {
103             inside[sampleI] = true;
104         }
105         else
106         {
107             inside[sampleI] = false;
108         }
109     }
111     return inside;
115 labelList triSurfaceSearch::calcNearestTri
117     const pointField& samples,
118     const vector& span
119 ) const
121     labelList nearest(samples.size());
123     const scalar nearestDistSqr = 0.25*magSqr(span);
125     pointIndexHit hitInfo;
127     forAll(samples, sampleI)
128     {
129         hitInfo = tree().findNearest(samples[sampleI], nearestDistSqr);
131         if (hitInfo.hit())
132         {
133             nearest[sampleI] = hitInfo.index();
134         }
135         else
136         {
137             nearest[sampleI] = -1;
138         }
139     }
141     return nearest;
145 // Nearest point on surface
146 tmp<pointField> triSurfaceSearch::calcNearest
148     const pointField& samples,
149     const vector& span
150 ) const
152     const scalar nearestDistSqr = 0.25*magSqr(span);
154     tmp<pointField> tnearest(new pointField(samples.size()));
155     pointField& nearest = tnearest();
157     pointIndexHit hitInfo;
159     forAll(samples, sampleI)
160     {
161         hitInfo = tree().findNearest(samples[sampleI], nearestDistSqr);
163         if (hitInfo.hit())
164         {
165             nearest[sampleI] = hitInfo.hitPoint();
166         }
167         else
168         {
169             nearest[sampleI] = greatPoint;
170         }
171     }
173     return tnearest;
177 pointIndexHit triSurfaceSearch::nearest
179     const point& pt,
180     const vector& span
181 ) const
183     const scalar nearestDistSqr = 0.25*magSqr(span);
185     return tree().findNearest(pt, nearestDistSqr);
189 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
191 } // End namespace Foam
193 // ************************************************************************* //