FreeFOAM The Cross-Platform CFD Toolkit
matchPoints.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-2011 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 "matchPoints.H"
27 #include <OpenFOAM/SortableList.H>
28 #include <OpenFOAM/ListOps.H>
29 
30 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
31 
33 (
34  const UList<point>& pts0,
35  const UList<point>& pts1,
36  const UList<scalar>& matchDistances,
37  const bool verbose,
38  labelList& from0To1,
39  const point& origin
40 )
41 {
42  from0To1.setSize(pts0.size());
43  from0To1 = -1;
44 
45  bool fullMatch = true;
46 
47  point compareOrigin = origin;
48 
49  if (origin == point(VGREAT, VGREAT, VGREAT))
50  {
51  if (pts1.size())
52  {
53  compareOrigin = sum(pts1)/pts1.size();
54  }
55  }
56 
57  SortableList<scalar> pts0MagSqr(magSqr(pts0 - compareOrigin));
58 
59  SortableList<scalar> pts1MagSqr(magSqr(pts1 - compareOrigin));
60 
61  forAll(pts0MagSqr, i)
62  {
63  scalar dist0 = pts0MagSqr[i];
64 
65  label face0I = pts0MagSqr.indices()[i];
66 
67  scalar matchDist = matchDistances[face0I];
68 
69  label startI = findLower(pts1MagSqr, 0.99999*dist0 - 2*matchDist);
70 
71  if (startI == -1)
72  {
73  startI = 0;
74  }
75 
76 
77  // Go through range of equal mag and find nearest vector.
78  scalar minDistSqr = VGREAT;
79  label minFaceI = -1;
80 
81  for
82  (
83  label j = startI;
84  (
85  (j < pts1MagSqr.size())
86  && (pts1MagSqr[j] < 1.00001*dist0 + 2*matchDist)
87  );
88  j++
89  )
90  {
91  label faceI = pts1MagSqr.indices()[j];
92  // Compare actual vectors
93  scalar distSqr = magSqr(pts0[face0I] - pts1[faceI]);
94 
95  if (distSqr <= sqr(matchDist) && distSqr < minDistSqr)
96  {
97  minDistSqr = distSqr;
98  minFaceI = faceI;
99  }
100  }
101 
102  if (minFaceI == -1)
103  {
104  fullMatch = false;
105 
106  if (verbose)
107  {
108  Pout<< "Cannot find point in pts1 matching point " << face0I
109  << " coord:" << pts0[face0I]
110  << " in pts0 when using tolerance " << matchDist << endl;
111 
112  // Go through range of equal mag and find equal vector.
113  Pout<< "Searching started from:" << startI << " in pts1"
114  << endl;
115  for
116  (
117  label j = startI;
118  (
119  (j < pts1MagSqr.size())
120  && (pts1MagSqr[j] < 1.00001*dist0 + 2*matchDist)
121  );
122  j++
123  )
124  {
125  label faceI = pts1MagSqr.indices()[j];
126 
127  Pout<< " Compared coord:" << pts1[faceI]
128  << " with difference to point "
129  << mag(pts1[faceI] - pts0[face0I]) << endl;
130  }
131  }
132  }
133 
134  from0To1[face0I] = minFaceI;
135  }
136 
137  return fullMatch;
138 }
139 
140 // ************************ vim: set sw=4 sts=4 et: ************************ //