initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / meshes / meshShapes / cellShape / cellShapeEqual.C
blob30756f3408586f88813800822e1d00b32d0579b5
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 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 Description
26     Equality operator for cellShape class
28 \*---------------------------------------------------------------------------*/
30 #include "cellShape.H"
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
34 bool Foam::operator==(const cellShape& a, const cellShape& b)
36     // Basic rule: we assume that the sequence of labels in each list
37     // will be circular in the same order (but not necessarily in the
38     // same direction). The limitation of this method is that with 3D
39     // topologies I cannot guarantee that a congruent but not
40     // identical cellShape (i.e. one sharing the same points but in a
41     // different order) will necessarily be matched.
43     const labelList& labsA = a;
44     const labelList& labsB = b;
46     // Trivial reject: faces are different size
47     label sizeA = labsA.size();
48     label sizeB = labsB.size();
49     if (sizeA != sizeB)
50     {
51         return false;
52     }
55     // First we look for the occurrence of the first label in A, in B
57     label Bptr = -1;
58     label firstA = labsA[0];
59     forAll(labsB, i)
60     {
61         if (labsB[i] == firstA)
62         {
63             Bptr = i;                // Denotes 'found match' at element 'i'
64             break;
65         }
66     }
68     // If no match was found, exit false
69     if (Bptr < 0)
70     {
71         return false;
72     }
74     // Now we must look for the direction, if any
75     label secondA = labsA[1];
76     label dir = 0;
78     // Check whether at top of list
79     Bptr++;
80     if (Bptr == labsB.size())
81     {
82         Bptr = 0;
83     }
85     // Test whether upward label matches second A label
86     if (labsB[Bptr] == secondA)
87     {
88         // Yes - direction is 'up'
89         dir = 1;
90     }
91     else
92     {
93         // No - so look downwards, checking whether at bottom of list
94         Bptr -= 2;
95         if (Bptr < 0)
96         {
97             // Case (1) Bptr=-1
98             if (Bptr == -1)
99             {
100                 Bptr = labsB.size() - 1;
101             }
103             // Case (2) Bptr = -2
104             else
105             {
106                 Bptr = labsB.size() - 2;
107             }
108         }
110         // Test whether downward label matches second A label
111         if (labsB[Bptr] == secondA)
112         {
113             // Yes - direction is 'down'
114             dir = -1;
115         }
116     }
118     // Check whether a match was made at all, and exit false if not
119     if (dir == 0)
120     {
121         return false;
122     }
124     // Decrement size by 2 to account for first searches
125     sizeA -= 2;
127     // We now have both direction of search and next element
128     // to search, so we can continue search until no more points.
129     label Aptr = 1;
130     if (dir > 0)
131     {
132         while (sizeA--)
133         {
134             Aptr++;
135             if (Aptr >= labsA.size())
136             {
137                 Aptr = 0;
138             }
140             Bptr++;
141             if (Bptr >= labsB.size())
142             {
143                 Bptr = 0;
144             }
145             if (labsA[Aptr] != labsB[Bptr])
146             {
147                 return false;
148             }
149         }
150     }
151     else
152     {
153         while (sizeA--)
154         {
155             Aptr++;
156             if (Aptr >= labsA.size())
157             {
158                 Aptr = 0;
159             }
161             Bptr--;
162             if (Bptr < 0)
163             {
164                 Bptr = labsB.size() - 1;
165             }
166             if (labsA[Aptr] != labsB[Bptr])
167             {
168                 return false;
169             }
170         }
171     }
173     // They must be equal
174     return true;
178 // ************************************************************************* //