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

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