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) 2011-2016 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 
35 defineTypeNameAndDebug(nearestToPoint, 0);
36 
37 addToRunTimeSelectionTable(topoSetSource, nearestToPoint, word);
38 
39 addToRunTimeSelectionTable(topoSetSource, nearestToPoint, istream);
40 
41 }
42 
43 
44 Foam::topoSetSource::addToUsageTable Foam::nearestToPoint::usage_
45 (
46  nearestToPoint::typeName,
47  "\n Usage: nearestToPoint (pt0 .. ptn)\n\n"
48  " Select the nearest point for each of the points pt0 ..ptn\n\n"
49 );
50 
51 
52 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
53 
54 void Foam::nearestToPoint::combine(topoSet& set, const bool add) const
55 {
56  // Do linear search since usually just a few points.
57 
58  forAll(points_, pointi)
59  {
60  const pointField& pts = mesh_.points();
61 
62  if (pts.size())
63  {
64  label minPointi = 0;
65  scalar minDistSqr = magSqr(pts[minPointi] - points_[pointi]);
66 
67  for (label i = 1; i < pts.size(); i++)
68  {
69  scalar distSqr = magSqr(pts[i] - points_[pointi]);
70 
71  if (distSqr < minDistSqr)
72  {
73  minDistSqr = distSqr;
74  minPointi = i;
75  }
76  }
77 
78  addOrDelete(set, minPointi, add);
79  }
80  }
81 }
82 
83 
84 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
85 
86 // Construct from components
88 (
89  const polyMesh& mesh,
90  const pointField& points
91 )
92 :
93  topoSetSource(mesh),
94  points_(points)
95 {}
96 
97 
98 // Construct from dictionary
100 (
101  const polyMesh& mesh,
102  const dictionary& dict
103 )
104 :
105  topoSetSource(mesh),
106  points_(dict.lookup("points"))
107 {}
108 
109 
110 // Construct from Istream
112 (
113  const polyMesh& mesh,
114  Istream& is
115 )
116 :
117  topoSetSource(mesh),
118  points_(checkIs(is))
119 {}
120 
121 
122 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
123 
125 {}
126 
127 
128 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
129 
131 (
132  const topoSetSource::setAction action,
133  topoSet& set
134 ) const
135 {
136  if ((action == topoSetSource::NEW) || (action == topoSetSource::ADD))
137  {
138  Info<< " Adding points nearest to " << points_ << endl;
139 
140  combine(set, true);
141  }
142  else if (action == topoSetSource::DELETE)
143  {
144  Info<< " Removing points nearest to " << points_ << endl;
145 
146  combine(set, false);
147  }
148 }
149 
150 
151 // ************************************************************************* //
#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
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:253
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)
virtual void applyToSet(const topoSetSource::setAction action, topoSet &) const
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:451