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

mergePoints.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 "mergePoints.H"
00028 #include "SortableList.H"
00029 #include "ListOps.H"
00030 
00031 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00032 
00033 bool Foam::mergePoints
00034 (
00035     const UList<point>& points,
00036     const scalar mergeTol,
00037     const bool verbose,
00038     labelList& pointMap,
00039     List<point>& newPoints,
00040     const point& origin
00041 )
00042 {
00043     point compareOrigin = origin;
00044 
00045     if (origin == point(VGREAT, VGREAT, VGREAT))
00046     {
00047         if (points.size())
00048         {
00049             compareOrigin = sum(points)/points.size();
00050         }
00051     }
00052 
00053     // Create a old to new point mapping array
00054     pointMap.setSize(points.size());
00055     pointMap = -1;
00056 
00057     // Storage for merged points
00058     newPoints.setSize(points.size());
00059 
00060     if (points.empty())
00061     {
00062         return false;
00063     }
00064 
00065 
00066     const scalar mergeTolSqr = sqr(mergeTol);
00067 
00068     // Sort points by magSqr
00069     SortableList<scalar> sortedMagSqr(magSqr(points - compareOrigin));
00070 
00071     bool hasMerged = false;
00072 
00073     label newPointI = 0;
00074 
00075 
00076     // Handle 0th point separately (is always unique)
00077     label pointI = sortedMagSqr.indices()[0];
00078     pointMap[pointI] = newPointI;
00079     newPoints[newPointI++] = points[pointI];
00080 
00081 
00082     for (label sortI = 1; sortI < sortedMagSqr.size(); sortI++)
00083     {
00084         // Get original point index
00085         label pointI = sortedMagSqr.indices()[sortI];
00086 
00087         // Compare to previous points to find equal one.
00088         label equalPointI = -1;
00089 
00090         for
00091         (
00092             label prevSortI = sortI - 1;
00093             prevSortI >= 0
00094          && mag
00095             (
00096                 sortedMagSqr[prevSortI]
00097              -  sortedMagSqr[sortI]
00098             ) <= mergeTolSqr;
00099             prevSortI--
00100         )
00101         {
00102             label prevPointI = sortedMagSqr.indices()[prevSortI];
00103 
00104             if (magSqr(points[pointI] - points[prevPointI]) <= mergeTolSqr)
00105             {
00106                 // Found match.
00107                 equalPointI = prevPointI;
00108 
00109                 break;
00110             }
00111         }
00112 
00113 
00114         if (equalPointI != -1)
00115         {
00116             // Same coordinate as equalPointI. Map to same new point.
00117             pointMap[pointI] = pointMap[equalPointI];
00118 
00119             hasMerged = true;
00120 
00121             if (verbose)
00122             {
00123                 Pout<< "Foam::mergePoints : Merging points "
00124                     << pointI << " and " << equalPointI
00125                     << " with coordinates:" << points[pointI]
00126                     << " and " << points[equalPointI]
00127                     << endl;
00128             }
00129         }
00130         else
00131         {
00132             // Differs. Store new point.
00133             pointMap[pointI] = newPointI;
00134             newPoints[newPointI++] = points[pointI];
00135         }
00136     }
00137 
00138     newPoints.setSize(newPointI);
00139 
00140     return hasMerged;
00141 }
00142 
00143 
00144 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd