initial commit for version 1.5.x patch release
[OpenFOAM-1.5.x.git] / src / OpenFOAM / meshes / meshTools / matchPoints.C
blob943724c4616b456df49811e84e42523d7b998e3f
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2008 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
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
19     for more details.
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 "matchPoints.H"
28 #include "SortableList.H"
29 #include "ListOps.H"
31 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
33 bool Foam::matchPoints
35     const UList<point>& pts0,
36     const UList<point>& pts1,
37     const UList<scalar>& matchDistances,
38     const bool verbose,
39     labelList& from0To1,
40     const point& origin
43     from0To1.setSize(pts0.size());
44     from0To1 = -1;
46     bool fullMatch = true;
48     SortableList<scalar> pts0MagSqr(magSqr(pts0 - origin));
50     SortableList<scalar> pts1MagSqr(magSqr(pts1 - origin));
52     forAll(pts0MagSqr, i)
53     {
54         scalar dist0 = pts0MagSqr[i];
56         label face0I = pts0MagSqr.indices()[i];
58         scalar matchDist = matchDistances[face0I];
60         label startI = findLower(pts1MagSqr, 0.99999*dist0 - matchDist);
62         if (startI == -1)
63         {
64             startI = 0;
65         }
68         // Go through range of equal mag and find nearest vector.
69         scalar minDistSqr = VGREAT;
70         label minFaceI = -1;
71     
72         for
73         (
74             label j = startI;
75             (
76                 (j < pts1MagSqr.size())
77              && (pts1MagSqr[j] < 1.00001*dist0 + matchDist)
78             );
79             j++
80         )
81         {
82             label faceI = pts1MagSqr.indices()[j];
83             // Compare actual vectors
84             scalar distSqr = magSqr(pts0[face0I] - pts1[faceI]);
86             if (distSqr <= sqr(matchDist) && distSqr < minDistSqr)
87             {
88                 minDistSqr = distSqr;
89                 minFaceI = faceI;
90             }
91         }
93         if (minFaceI == -1)
94         {
95             fullMatch = false;
97             if (verbose)
98             {
99                 Pout<< "Cannot find point in pts1 matching point " << face0I
100                     << " coord:" << pts0[face0I]
101                     << " in pts0 when using tolerance " << matchDist << endl;
103                 // Go through range of equal mag and find equal vector.
104                 Pout<< "Searching started from:" << startI << " in pts1"
105                     << endl;
106                 for
107                 (
108                     label j = startI;
109                     (
110                         (j < pts1MagSqr.size())
111                      && (pts1MagSqr[j] < 1.00001*dist0 + matchDist)
112                     );
113                     j++
114                 )
115                 {
116                     label faceI = pts1MagSqr.indices()[j];
118                     Pout<< "Compared coord:" << pts1[faceI]
119                         << " with difference to point "
120                         << mag(pts1[faceI] - pts0[face0I]) << endl;
121                 }
122             }
123         }
125         from0To1[face0I] = minFaceI;
126     }
128     return fullMatch;
131 // ************************************************************************* //