FreeFOAM The Cross-Platform CFD Toolkit
nearestToPoint.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
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.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "nearestToPoint.H"
27 #include <OpenFOAM/polyMesh.H>
28 #include <meshTools/meshSearch.H>
29 
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 
37 defineTypeNameAndDebug(nearestToPoint, 0);
38 
39 addToRunTimeSelectionTable(topoSetSource, nearestToPoint, word);
40 
41 addToRunTimeSelectionTable(topoSetSource, nearestToPoint, istream);
42 
43 }
44 
45 
46 Foam::topoSetSource::addToUsageTable Foam::nearestToPoint::usage_
47 (
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"
51 );
52 
53 
54 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
55 
56 void Foam::nearestToPoint::combine(topoSet& set, const bool add) const
57 {
58  // Do linear search since usually just a few points.
59 
60  forAll(points_, pointI)
61  {
62  const pointField& pts = mesh_.points();
63 
64  if (pts.size())
65  {
66  label minPointI = 0;
67  scalar minDistSqr = magSqr(pts[minPointI] - points_[pointI]);
68 
69  for (label i = 1; i < pts.size(); i++)
70  {
71  scalar distSqr = magSqr(pts[i] - points_[pointI]);
72 
73  if (distSqr < minDistSqr)
74  {
75  minDistSqr = distSqr;
76  minPointI = i;
77  }
78  }
79 
80  addOrDelete(set, minPointI, add);
81  }
82  }
83 }
84 
85 
86 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
87 
88 // Construct from components
90 (
91  const polyMesh& mesh,
92  const pointField& points
93 )
94 :
95  topoSetSource(mesh),
96  points_(points)
97 {}
98 
99 
100 // Construct from dictionary
102 (
103  const polyMesh& mesh,
104  const dictionary& dict
105 )
106 :
107  topoSetSource(mesh),
108  points_(dict.lookup("points"))
109 {}
110 
111 
112 // Construct from Istream
114 (
115  const polyMesh& mesh,
116  Istream& is
117 )
118 :
119  topoSetSource(mesh),
120  points_(checkIs(is))
121 {}
122 
123 
124 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
125 
127 {}
128 
129 
130 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
131 
133 (
134  const topoSetSource::setAction action,
135  topoSet& set
136 ) const
137 {
138  if ((action == topoSetSource::NEW) || (action == topoSetSource::ADD))
139  {
140  Info<< " Adding points nearest to " << points_ << endl;
141 
142  combine(set, true);
143  }
144  else if (action == topoSetSource::DELETE)
145  {
146  Info<< " Removing points nearest to " << points_ << endl;
147 
148  combine(set, false);
149  }
150 }
151 
152 
153 // ************************ vim: set sw=4 sts=4 et: ************************ //