1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
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 the
13 Free Software Foundation; either version 2 of the License, or (at your
14 option) any later version.
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
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM; if not, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "mergePoints.H"
28 #include "SortableList.H"
31 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
33 bool Foam::mergePoints
35 const UList<point>& points,
36 const scalar mergeTol,
39 List<point>& newPoints,
43 point compareOrigin = origin;
45 if (origin == point(VGREAT, VGREAT, VGREAT))
49 compareOrigin = sum(points)/points.size();
53 // Create a old to new point mapping array
54 pointMap.setSize(points.size());
57 // Storage for merged points
58 newPoints.setSize(points.size());
66 const scalar mergeTolSqr = sqr(mergeTol);
68 // Sort points by magSqr
69 SortableList<scalar> sortedMagSqr(magSqr(points - compareOrigin));
71 bool hasMerged = false;
76 // Handle 0th point separately (is always unique)
77 label pointI = sortedMagSqr.indices()[0];
78 pointMap[pointI] = newPointI;
79 newPoints[newPointI++] = points[pointI];
82 for (label sortI = 1; sortI < sortedMagSqr.size(); sortI++)
84 // Get original point index
85 label pointI = sortedMagSqr.indices()[sortI];
87 // Compare to previous points to find equal one.
88 label equalPointI = -1;
92 label prevSortI = sortI - 1;
96 sortedMagSqr[prevSortI]
102 label prevPointI = sortedMagSqr.indices()[prevSortI];
104 if (magSqr(points[pointI] - points[prevPointI]) <= mergeTolSqr)
107 equalPointI = prevPointI;
114 if (equalPointI != -1)
116 // Same coordinate as equalPointI. Map to same new point.
117 pointMap[pointI] = pointMap[equalPointI];
123 Pout<< "Foam::mergePoints : Merging points "
124 << pointI << " and " << equalPointI
125 << " with coordinates:" << points[pointI]
126 << " and " << points[equalPointI]
132 // Differs. Store new point.
133 pointMap[pointI] = newPointI;
134 newPoints[newPointI++] = points[pointI];
138 newPoints.setSize(newPointI);
144 // ************************************************************************* //