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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "matchPoints.H"
29 #include "SortableList.H"
30 #include "ListOps.H"
31 
32 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
33 
35 (
36  const UList<point>& pts0,
37  const UList<point>& pts1,
38  const UList<scalar>& matchDistances,
39  const bool verbose,
40  labelList& from0To1,
41  const point& origin
42 )
43 {
44  from0To1.setSize(pts0.size());
45  from0To1 = -1;
46 
47  bool fullMatch = true;
48 
49  point compareOrigin = origin;
50 
51  if (origin == point(VGREAT, VGREAT, VGREAT))
52  {
53  if (pts1.size())
54  {
55  compareOrigin = sum(pts1)/pts1.size();
56  }
57  }
58 
59  SortableList<scalar> pts0MagSqr(magSqr(pts0 - compareOrigin));
60 
61  SortableList<scalar> pts1MagSqr(magSqr(pts1 - compareOrigin));
62 
63  forAll(pts0MagSqr, i)
64  {
65  scalar dist0 = pts0MagSqr[i];
66 
67  label face0I = pts0MagSqr.indices()[i];
68 
69  scalar matchDist = matchDistances[face0I];
70 
71  label startI = findLower(pts1MagSqr, 0.99999*dist0 - 2*matchDist);
72 
73  if (startI == -1)
74  {
75  startI = 0;
76  }
77 
78 
79  // Go through range of equal mag and find nearest vector.
80  scalar minDistSqr = VGREAT;
81  label minFacei = -1;
82 
83  for
84  (
85  label j = startI;
86  (
87  (j < pts1MagSqr.size())
88  && (pts1MagSqr[j] < 1.00001*dist0 + 2*matchDist)
89  );
90  j++
91  )
92  {
93  label facei = pts1MagSqr.indices()[j];
94  // Compare actual vectors
95  scalar distSqr = magSqr(pts0[face0I] - pts1[facei]);
96 
97  if (distSqr <= sqr(matchDist) && distSqr < minDistSqr)
98  {
99  minDistSqr = distSqr;
100  minFacei = facei;
101  }
102  }
103 
104  if (minFacei == -1)
105  {
106  fullMatch = false;
107 
108  if (verbose)
109  {
110  Pout<< "Cannot find point in pts1 matching point " << face0I
111  << " coord:" << pts0[face0I]
112  << " in pts0 when using tolerance " << matchDist << endl;
113 
114  // Go through range of equal mag and find equal vector.
115  Pout<< "Searching started from:" << startI << " in pts1"
116  << endl;
117  for
118  (
119  label j = startI;
120  (
121  (j < pts1MagSqr.size())
122  && (pts1MagSqr[j] < 1.00001*dist0 + 2*matchDist)
123  );
124  j++
125  )
126  {
127  label facei = pts1MagSqr.indices()[j];
128 
129  Pout<< " Compared coord: " << pts1[facei]
130  << " at index " << j
131  << " with difference to point "
132  << mag(pts1[facei] - pts0[face0I]) << endl;
133  }
134  }
135  }
136 
137  from0To1[face0I] = minFacei;
138  }
139 
140  return fullMatch;
141 }
142 
143 
145 (
146  const UList<point>& pts0,
147  const UList<point>& pts1,
148  const UList<point>& pts0Dir,
149  const UList<point>& pts1Dir,
150  const UList<scalar>& matchDistances,
151  const bool verbose,
152  labelList& from0To1,
153  const point& origin
154 )
155 {
156  from0To1.setSize(pts0.size());
157  from0To1 = -1;
158 
159  bool fullMatch = true;
160 
161  point compareOrigin = origin;
162 
163  if (origin == point(VGREAT, VGREAT, VGREAT))
164  {
165  if (pts1.size())
166  {
167  compareOrigin = sum(pts1)/pts1.size();
168  }
169  }
170 
171  SortableList<scalar> pts0MagSqr(magSqr(pts0 - compareOrigin));
172 
173  SortableList<scalar> pts1MagSqr(magSqr(pts1 - compareOrigin));
174 
175  forAll(pts0MagSqr, i)
176  {
177  scalar dist0 = pts0MagSqr[i];
178 
179  label face0I = pts0MagSqr.indices()[i];
180 
181  scalar matchDist = matchDistances[face0I];
182 
183  label startI = findLower(pts1MagSqr, 0.99999*dist0 - 2*matchDist);
184 
185  if (startI == -1)
186  {
187  startI = 0;
188  }
189 
190  // Go through range of equal mag and find nearest vector.
191  scalar minDistSqr = VGREAT;
192  label minFacei = -1;
193 
194  // Valid candidate points should have opposite normal
195  const scalar minDistNorm = 0;
196 
197  for
198  (
199  label j = startI;
200  (
201  (j < pts1MagSqr.size())
202  && (pts1MagSqr[j] < 1.00001*dist0 + 2*matchDist)
203  );
204  j++
205  )
206  {
207  label facei = pts1MagSqr.indices()[j];
208  // Compare actual vectors
209  scalar distSqr = magSqr(pts0[face0I] - pts1[facei]);
210 
211  scalar distNorm = (pts0Dir[face0I] & pts1Dir[facei]);
212 
213  if
214  (
215  magSqr(pts0Dir[face0I]) < sqr(SMALL)
216  && magSqr(pts1Dir[facei]) < sqr(SMALL)
217  )
218  {
219  distNorm = -1;
220  }
221 
222  if (distSqr <= sqr(matchDist) && distSqr < minDistSqr)
223  {
224  // Check that the normals point in equal and opposite directions
225  if (distNorm < minDistNorm)
226  {
227  minDistSqr = distSqr;
228  minFacei = facei;
229  }
230  }
231  }
232 
233  if (minFacei == -1)
234  {
235  fullMatch = false;
236 
237  if (verbose)
238  {
239  Pout<< "Cannot find point in pts1 matching point " << face0I
240  << " coord:" << pts0[face0I]
241  << " in pts0 when using tolerance " << matchDist << endl;
242 
243  // Go through range of equal mag and find equal vector.
244  Pout<< "Searching started from:" << startI << " in pts1"
245  << endl;
246  for
247  (
248  label j = startI;
249  (
250  (j < pts1MagSqr.size())
251  && (pts1MagSqr[j] < 1.00001*dist0 + 2*matchDist)
252  );
253  j++
254  )
255  {
256  label facei = pts1MagSqr.indices()[j];
257 
258  Pout<< " Compared coord: " << pts1[facei]
259  << " at index " << j
260  << " with difference to point "
261  << mag(pts1[facei] - pts0[face0I]) << endl;
262  }
263  }
264  }
265 
266  from0To1[face0I] = minFacei;
267  }
268 
269  return fullMatch;
270 }
271 
272 
273 // ************************************************************************* //
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
Foam::Pout
prefixOSstream Pout
An Ostream wrapper for parallel output to std::cout.
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:290
Foam::magSqr
dimensioned< typename typeOfMag< Type >::type > magSqr(const dimensioned< Type > &dt)
SortableList.H
matchPoints.H
Determine correspondence between points. See below.
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
Foam::findLower
label findLower(const ListType &input, const T &val, const label start, const ComparePredicate &comp)
Foam::SortableList
A list that is sorted upon construction or when explicitly requested with the sort() method.
Definition: List.H:66
Foam::sqr
dimensionedSymmTensor sqr(const dimensionedVector &dv)
Definition: dimensionedSymmTensor.C:51
Foam::Vector< scalar >
Foam::List< label >
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
Foam::UList
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:103
Foam::sum
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh > &df)
Definition: DimensionedFieldFunctions.C:327
Foam::SortableList::indices
const labelList & indices() const
Return the list of sorted indices. Updated every sort.
Definition: SortableList.H:113
Foam::UList::size
void size(const label n) noexcept
Override size to be inconsistent with allocated storage.
Definition: UListI.H:360
ListOps.H
Various functions to operate on Lists.
Foam::point
vector point
Point is a vector.
Definition: point.H:43
Foam::List::setSize
void setSize(const label newSize)
Alias for resize(const label)
Definition: ListI.H:146
Foam::matchPoints
bool matchPoints(const UList< point > &pts0, const UList< point > &pts1, const UList< scalar > &matchDistance, const bool verbose, labelList &from0To1, const point &origin=point::zero)
Determine correspondence between pointFields. Gets passed.
Definition: matchPoints.C:35