initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / meshTools / octree / octreeDataPointTreeLeaf.C
blob2e5cafc9b7a5411c703da40dceeada1065481a10
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 Description
27 \*---------------------------------------------------------------------------*/
29 //#include "octreeDataPointTreeLeaf.H"
30 #include "octreeDataPoint.H"
31 #include "treeLeaf.H"
33 // * * * * * * * * * * * * * Template Specialisations  * * * * * * * * * * * //
35 template<>
36 Foam::label Foam::treeLeaf<Foam::octreeDataPoint>::find
38     const octreeDataPoint& shapes,
39     const point& sample
40 ) const
42     notImplemented
43     (
44         "Foam::treeLeaf<Foam::octreeDataPoint>::find("
45         "const octreeDataPoint& shapes,"
46         "const point& sample"
47     );
49     return false;
53 template<>
54 bool Foam::treeLeaf<Foam::octreeDataPoint>::findNearest
56     const octreeDataPoint& shapes,
57     const point& sample,
58     treeBoundBox& tightest,
59     label& tightestI,
60     scalar& tightestDist
61 ) const
63     // Some aliases
64     const pointField& points = shapes.points();
65     point& tMin = tightest.min();
66     point& tMax = tightest.max();
68     scalar minDist2 = sqr(tightestDist);
70     label minIndex = -1;
71     forAll(indices_, i)
72     {
73         label pointi = indices_[i];
74         scalar dist = magSqr(points[pointi] - sample);
76         if (dist < minDist2)
77         {
78             minDist2 = dist;
79             minIndex = pointi;
80         }
81     }
83     if (minIndex != -1)
84     {
85         tightestDist = sqrt(minDist2);
86     
87         // New nearer. Update 'tightest' bounding box
88         tMin.x() = sample.x() - tightestDist;
89         tMin.y() = sample.y() - tightestDist;
90         tMin.z() = sample.z() - tightestDist;
92         tMax.x() = sample.x() + tightestDist;
93         tMax.y() = sample.y() + tightestDist;
94         tMax.z() = sample.z() + tightestDist;
96         tightestI = minIndex;
98         return true;
99     }
100     else
101     {
102         // New no nearer so nothing changed
103         return false;
104     }
108 // ************************************************************************* //