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 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 
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 :
94  points_(points)
95 {}
96 
97 
98 // Construct from dictionary
100 (
101  const polyMesh& mesh,
102  const dictionary& dict
103 )
104 :
106  points_(dict.lookup("points"))
107 {}
108 
109 
110 // Construct from Istream
112 (
113  const polyMesh& mesh,
114  Istream& is
115 )
116 :
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 // ************************************************************************* //
Foam::polyMesh::points
virtual const pointField & points() const
Return raw points.
Definition: polyMesh.C:979
Foam::topoSetSource::ADD
@ ADD
Definition: topoSetSource.H:87
nearestToPoint.H
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::nearestToPoint::combine
void combine(topoSet &set, const bool add) const
Definition: nearestToPoint.C:54
Foam::addToRunTimeSelectionTable
addToRunTimeSelectionTable(ensightPart, ensightPartCells, istream)
Foam::ListListOps::combine
AccessType combine(const List< T > &, AccessOp aop=accessOp< T >())
Combines sublists into one big list.
Definition: ListListOps.C:34
Foam::topoSetSource::addToUsageTable
Class with constructor to add usage string to table.
Definition: topoSetSource.H:100
Foam::dictionary::lookup
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:449
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::topoSetSource::setAction
setAction
Enumeration defining the valid actions.
Definition: topoSetSource.H:82
Foam::nearestToPoint::points_
pointField points_
Points to select nearest to.
Definition: nearestToPoint.H:59
Foam::topoSetSource::NEW
@ NEW
Definition: topoSetSource.H:85
polyMesh.H
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
Foam::nearestToPoint::~nearestToPoint
virtual ~nearestToPoint()
Destructor.
Definition: nearestToPoint.C:124
Foam::label
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
Foam::Field
Pre-declare SubField and related Field type.
Definition: Field.H:57
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:57
Foam::Info
messageStream Info
Foam::nearestToPoint::nearestToPoint
nearestToPoint(const polyMesh &mesh, const pointField &points)
Construct from components.
Definition: nearestToPoint.C:88
Foam::topoSetSource::DELETE
@ DELETE
Definition: topoSetSource.H:88
Foam::topoSet
General set of labels of mesh quantity (points, cells, faces).
Definition: topoSet.H:61
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:137
Foam::add
void add(FieldField< Field1, typename typeOfSum< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
Definition: FieldFieldFunctions.C:870
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:18
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::topoSetSource
Base class of a source for a topoSet.
Definition: topoSetSource.H:63
points
const pointField & points
Definition: gmvOutputHeader.H:1
Foam::nearestToPoint::applyToSet
virtual void applyToSet(const topoSetSource::setAction action, topoSet &) const
Definition: nearestToPoint.C:131
Foam::topoSetSource::mesh_
const polyMesh & mesh_
Definition: topoSetSource.H:126
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
Foam::magSqr
dimensioned< scalar > magSqr(const dimensioned< Type > &)
Foam::topoSetSource::addOrDelete
void addOrDelete(topoSet &set, const label cellI, const bool) const
Add (if bool) cellI to set or delete cellI from set.
Definition: topoSetSource.C:140
Foam::nearestToPoint::usage_
static addToUsageTable usage_
Add usage string.
Definition: nearestToPoint.H:56