OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

nearestToPoint.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "nearestToPoint.H"
00028 #include "polyMesh.H"
00029 #include "meshSearch.H"
00030 
00031 #include "addToRunTimeSelectionTable.H"
00032 
00033 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00034 
00035 namespace Foam
00036 {
00037 
00038 defineTypeNameAndDebug(nearestToPoint, 0);
00039 
00040 addToRunTimeSelectionTable(topoSetSource, nearestToPoint, word);
00041 
00042 addToRunTimeSelectionTable(topoSetSource, nearestToPoint, istream);
00043 
00044 }
00045 
00046 
00047 Foam::topoSetSource::addToUsageTable Foam::nearestToPoint::usage_
00048 (
00049     nearestToPoint::typeName,
00050     "\n    Usage: nearestToPoint (pt0 .. ptn)\n\n"
00051     "    Select the nearest point for each of the points pt0 ..ptn\n\n"
00052 );
00053 
00054 
00055 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00056 
00057 void Foam::nearestToPoint::combine(topoSet& set, const bool add) const
00058 {
00059     // Do linear search since usually just a few points.
00060 
00061     forAll(points_, pointI)
00062     {
00063         const pointField& pts = mesh_.points();
00064 
00065         if (pts.size())
00066         {
00067             label minPointI = 0;
00068             scalar minDistSqr = magSqr(pts[minPointI] - points_[pointI]);
00069 
00070             for (label i = 1; i < pts.size(); i++)
00071             {
00072                 scalar distSqr = magSqr(pts[i] - points_[pointI]);
00073 
00074                 if (distSqr < minDistSqr)
00075                 {
00076                     minDistSqr = distSqr;
00077                     minPointI = i;
00078                 }
00079             }
00080 
00081             addOrDelete(set, minPointI, add);
00082         }
00083     }
00084 }
00085 
00086 
00087 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00088 
00089 // Construct from components
00090 Foam::nearestToPoint::nearestToPoint
00091 (
00092     const polyMesh& mesh,
00093     const pointField& points
00094 )
00095 :
00096     topoSetSource(mesh),
00097     points_(points)
00098 {}
00099 
00100 
00101 // Construct from dictionary
00102 Foam::nearestToPoint::nearestToPoint
00103 (
00104     const polyMesh& mesh,
00105     const dictionary& dict
00106 )
00107 :
00108     topoSetSource(mesh),
00109     points_(dict.lookup("points"))
00110 {}
00111 
00112 
00113 // Construct from Istream
00114 Foam::nearestToPoint::nearestToPoint
00115 (
00116     const polyMesh& mesh,
00117     Istream& is
00118 )
00119 :
00120     topoSetSource(mesh),
00121     points_(checkIs(is))
00122 {}
00123 
00124 
00125 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00126 
00127 Foam::nearestToPoint::~nearestToPoint()
00128 {}
00129 
00130 
00131 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00132 
00133 void Foam::nearestToPoint::applyToSet
00134 (
00135     const topoSetSource::setAction action,
00136     topoSet& set
00137 ) const
00138 {
00139     if ((action == topoSetSource::NEW) || (action == topoSetSource::ADD))
00140     {
00141         Info<< "    Adding points nearest to " << points_ << endl;
00142 
00143         combine(set, true);
00144     }
00145     else if (action == topoSetSource::DELETE)
00146     {
00147         Info<< "    Removing points nearest to " << points_ << endl;
00148 
00149         combine(set, false);
00150     }
00151 }
00152 
00153 
00154 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd