FreeFOAM The Cross-Platform CFD Toolkit
mergePoints.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
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
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "mergePoints.H"
27 #include <OpenFOAM/SortableList.H>
28 #include <OpenFOAM/ListOps.H>
29 
30 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
31 
33 (
34  const UList<point>& points,
35  const scalar mergeTol,
36  const bool verbose,
37  labelList& pointMap,
38  List<point>& newPoints,
39  const point& origin
40 )
41 {
42  point compareOrigin = origin;
43 
44  if (origin == point(VGREAT, VGREAT, VGREAT))
45  {
46  if (points.size())
47  {
48  compareOrigin = sum(points)/points.size();
49  }
50  }
51 
52  // Create a old to new point mapping array
53  pointMap.setSize(points.size());
54  pointMap = -1;
55 
56  // Storage for merged points
57  newPoints.setSize(points.size());
58 
59  if (points.empty())
60  {
61  return false;
62  }
63 
64 
65  const scalar mergeTolSqr = sqr(mergeTol);
66 
67  // Sort points by magSqr
68  SortableList<scalar> sortedMagSqr(magSqr(points - compareOrigin));
69 
70  bool hasMerged = false;
71 
72  label newPointI = 0;
73 
74 
75  // Handle 0th point separately (is always unique)
76  label pointI = sortedMagSqr.indices()[0];
77  pointMap[pointI] = newPointI;
78  newPoints[newPointI++] = points[pointI];
79 
80 
81  for (label sortI = 1; sortI < sortedMagSqr.size(); sortI++)
82  {
83  // Get original point index
84  label pointI = sortedMagSqr.indices()[sortI];
85 
86  // Compare to previous points to find equal one.
87  label equalPointI = -1;
88 
89  for
90  (
91  label prevSortI = sortI - 1;
92  prevSortI >= 0
93  && mag
94  (
95  sortedMagSqr[prevSortI]
96  - sortedMagSqr[sortI]
97  ) <= mergeTolSqr;
98  prevSortI--
99  )
100  {
101  label prevPointI = sortedMagSqr.indices()[prevSortI];
102 
103  if (magSqr(points[pointI] - points[prevPointI]) <= mergeTolSqr)
104  {
105  // Found match.
106  equalPointI = prevPointI;
107 
108  break;
109  }
110  }
111 
112 
113  if (equalPointI != -1)
114  {
115  // Same coordinate as equalPointI. Map to same new point.
116  pointMap[pointI] = pointMap[equalPointI];
117 
118  hasMerged = true;
119 
120  if (verbose)
121  {
122  Pout<< "Foam::mergePoints : Merging points "
123  << pointI << " and " << equalPointI
124  << " with coordinates:" << points[pointI]
125  << " and " << points[equalPointI]
126  << endl;
127  }
128  }
129  else
130  {
131  // Differs. Store new point.
132  pointMap[pointI] = newPointI;
133  newPoints[newPointI++] = points[pointI];
134  }
135  }
136 
137  newPoints.setSize(newPointI);
138 
139  return hasMerged;
140 }
141 
142 
143 // ************************ vim: set sw=4 sts=4 et: ************************ //