Merge branch 'master' of ssh://git.code.sf.net/p/foam-extend/foam-extend-3.2
[foam-extend-3.2.git] / src / meshTools / sets / pointSources / nearestToPoint / nearestToPoint.C
blob8ea8b48c3d247858f8711ed65e4ee0551352d228
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 "nearestToPoint.H"
27 #include "polyMesh.H"
28 #include "meshSearch.H"
30 #include "addToRunTimeSelectionTable.H"
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 namespace Foam
37 defineTypeNameAndDebug(nearestToPoint, 0);
39 addToRunTimeSelectionTable(topoSetSource, nearestToPoint, word);
41 addToRunTimeSelectionTable(topoSetSource, nearestToPoint, istream);
46 Foam::topoSetSource::addToUsageTable Foam::nearestToPoint::usage_
48     nearestToPoint::typeName,
49     "\n    Usage: nearestToPoint (pt0 .. ptn)\n\n"
50     "    Select the nearest point for each of the points pt0 ..ptn\n\n"
54 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
56 void Foam::nearestToPoint::combine(topoSet& set, const bool add) const
58     // Do linear search since usually just a few points.
60     forAll(points_, pointI)
61     {
62         const pointField& pts = mesh_.points();
64         if (pts.size())
65         {
66             label minPointI = 0;
67             scalar minDistSqr = magSqr(pts[minPointI] - points_[pointI]);
69             for (label i = 1; i < pts.size(); i++)
70             {
71                 scalar distSqr = magSqr(pts[i] - points_[pointI]);
73                 if (distSqr < minDistSqr)
74                 {
75                     minDistSqr = distSqr;
76                     minPointI = i;
77                 }
78             }
80             addOrDelete(set, minPointI, add);
81         }
82     }
86 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
88 // Construct from components
89 Foam::nearestToPoint::nearestToPoint
91     const polyMesh& mesh,
92     const pointField& points
95     topoSetSource(mesh),
96     points_(points)
100 // Construct from dictionary
101 Foam::nearestToPoint::nearestToPoint
103     const polyMesh& mesh,
104     const dictionary& dict
107     topoSetSource(mesh),
108     points_(dict.lookup("points"))
112 // Construct from Istream
113 Foam::nearestToPoint::nearestToPoint
115     const polyMesh& mesh,
116     Istream& is
119     topoSetSource(mesh),
120     points_(checkIs(is))
124 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
126 Foam::nearestToPoint::~nearestToPoint()
130 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
132 void Foam::nearestToPoint::applyToSet
134     const topoSetSource::setAction action,
135     topoSet& set
136 ) const
138     if ((action == topoSetSource::NEW) || (action == topoSetSource::ADD))
139     {
140         Info<< "    Adding points nearest to " << points_ << endl;
142         combine(set, true);
143     }
144     else if (action == topoSetSource::DELETE)
145     {
146         Info<< "    Removing points nearest to " << points_ << endl;
148         combine(set, false);
149     }
153 // ************************************************************************* //