FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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-2010 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
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your 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, see <http://www.gnu.org/licenses/>.
00023 
00024 \*---------------------------------------------------------------------------*/
00025 
00026 #include "nearestToPoint.H"
00027 #include <OpenFOAM/polyMesh.H>
00028 #include <meshTools/meshSearch.H>
00029 
00030 #include <OpenFOAM/addToRunTimeSelectionTable.H>
00031 
00032 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00033 
00034 namespace Foam
00035 {
00036 
00037 defineTypeNameAndDebug(nearestToPoint, 0);
00038 
00039 addToRunTimeSelectionTable(topoSetSource, nearestToPoint, word);
00040 
00041 addToRunTimeSelectionTable(topoSetSource, nearestToPoint, istream);
00042 
00043 }
00044 
00045 
00046 Foam::topoSetSource::addToUsageTable Foam::nearestToPoint::usage_
00047 (
00048     nearestToPoint::typeName,
00049     "\n    Usage: nearestToPoint (pt0 .. ptn)\n\n"
00050     "    Select the nearest point for each of the points pt0 ..ptn\n\n"
00051 );
00052 
00053 
00054 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00055 
00056 void Foam::nearestToPoint::combine(topoSet& set, const bool add) const
00057 {
00058     // Do linear search since usually just a few points.
00059 
00060     forAll(points_, pointI)
00061     {
00062         const pointField& pts = mesh_.points();
00063 
00064         if (pts.size())
00065         {
00066             label minPointI = 0;
00067             scalar minDistSqr = magSqr(pts[minPointI] - points_[pointI]);
00068 
00069             for (label i = 1; i < pts.size(); i++)
00070             {
00071                 scalar distSqr = magSqr(pts[i] - points_[pointI]);
00072 
00073                 if (distSqr < minDistSqr)
00074                 {
00075                     minDistSqr = distSqr;
00076                     minPointI = i;
00077                 }
00078             }
00079 
00080             addOrDelete(set, minPointI, add);
00081         }
00082     }
00083 }
00084 
00085 
00086 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00087 
00088 // Construct from components
00089 Foam::nearestToPoint::nearestToPoint
00090 (
00091     const polyMesh& mesh,
00092     const pointField& points
00093 )
00094 :
00095     topoSetSource(mesh),
00096     points_(points)
00097 {}
00098 
00099 
00100 // Construct from dictionary
00101 Foam::nearestToPoint::nearestToPoint
00102 (
00103     const polyMesh& mesh,
00104     const dictionary& dict
00105 )
00106 :
00107     topoSetSource(mesh),
00108     points_(dict.lookup("points"))
00109 {}
00110 
00111 
00112 // Construct from Istream
00113 Foam::nearestToPoint::nearestToPoint
00114 (
00115     const polyMesh& mesh,
00116     Istream& is
00117 )
00118 :
00119     topoSetSource(mesh),
00120     points_(checkIs(is))
00121 {}
00122 
00123 
00124 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00125 
00126 Foam::nearestToPoint::~nearestToPoint()
00127 {}
00128 
00129 
00130 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00131 
00132 void Foam::nearestToPoint::applyToSet
00133 (
00134     const topoSetSource::setAction action,
00135     topoSet& set
00136 ) const
00137 {
00138     if ((action == topoSetSource::NEW) || (action == topoSetSource::ADD))
00139     {
00140         Info<< "    Adding points nearest to " << points_ << endl;
00141 
00142         combine(set, true);
00143     }
00144     else if (action == topoSetSource::DELETE)
00145     {
00146         Info<< "    Removing points nearest to " << points_ << endl;
00147 
00148         combine(set, false);
00149     }
00150 }
00151 
00152 
00153 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines