nearestToPoint.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation
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 "polyMesh.H"
29 
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 
32 namespace Foam
33 {
34  defineTypeNameAndDebug(nearestToPoint, 0);
35  addToRunTimeSelectionTable(topoSetSource, nearestToPoint, word);
36  addToRunTimeSelectionTable(topoSetSource, nearestToPoint, istream);
37 }
38 
39 
40 Foam::topoSetSource::addToUsageTable Foam::nearestToPoint::usage_
41 (
42  nearestToPoint::typeName,
43  "\n Usage: nearestToPoint (pt0 .. ptn)\n\n"
44  " Select the nearest point for each of the points pt0 ..ptn\n\n"
45 );
46 
47 
48 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
49 
50 void Foam::nearestToPoint::combine(topoSet& set, const bool add) const
51 {
52  // Do linear search since usually just a few points.
53 
54  forAll(points_, pointi)
55  {
56  const pointField& pts = mesh_.points();
57 
58  if (pts.size())
59  {
60  label minPointi = 0;
61  scalar minDistSqr = magSqr(pts[minPointi] - points_[pointi]);
62 
63  for (label i = 1; i < pts.size(); i++)
64  {
65  scalar distSqr = magSqr(pts[i] - points_[pointi]);
66 
67  if (distSqr < minDistSqr)
68  {
69  minDistSqr = distSqr;
70  minPointi = i;
71  }
72  }
73 
74  addOrDelete(set, minPointi, add);
75  }
76  }
77 }
78 
79 
80 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
81 
83 (
84  const polyMesh& mesh,
85  const pointField& points
86 )
87 :
88  topoSetSource(mesh),
89  points_(points)
90 {}
91 
92 
94 (
95  const polyMesh& mesh,
96  const dictionary& dict
97 )
98 :
99  topoSetSource(mesh),
100  points_(dict.lookup("points"))
101 {}
102 
103 
105 (
106  const polyMesh& mesh,
107  Istream& is
108 )
109 :
110  topoSetSource(mesh),
111  points_(checkIs(is))
112 {}
113 
114 
115 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
116 
118 {}
119 
120 
121 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
122 
124 (
125  const topoSetSource::setAction action,
126  topoSet& set
127 ) const
128 {
129  if ((action == topoSetSource::NEW) || (action == topoSetSource::ADD))
130  {
131  Info<< " Adding points nearest to " << points_ << endl;
132 
133  combine(set, true);
134  }
135  else if (action == topoSetSource::DELETE)
136  {
137  Info<< " Removing points nearest to " << points_ << endl;
138 
139  combine(set, false);
140  }
141 }
142 
143 
144 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
virtual void applyToSet(const topoSetSource::setAction action, topoSet &) const
nearestToPoint(const polyMesh &mesh, const pointField &points)
Construct from components.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
AccessType combine(const List< T > &, AccessOp aop=accessOp< T >())
Combines sublists into one big list.
Definition: ListListOps.C:34
Macros for easy insertion into run-time selection tables.
Base class of a source for a topoSet.
Definition: topoSetSource.H:63
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:42
setAction
Enumeration defining the valid actions.
Definition: topoSetSource.H:82
dimensioned< scalar > magSqr(const dimensioned< Type > &)
addToRunTimeSelectionTable(ensightPart, ensightPartCells, istream)
defineTypeNameAndDebug(combustionModel, 0)
General set of labels of mesh quantity (points, cells, faces).
Definition: topoSet.H:61
virtual ~nearestToPoint()
Destructor.
Class with constructor to add usage string to table.
messageStream Info
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
Namespace for OpenFOAM.
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:576