cellShapeEqual.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation
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 Description
25  Equality operator for cellShape class
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "cellShape.H"
30 
31 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
32 
33 bool Foam::operator==(const cellShape& a, const cellShape& b)
34 {
35  // Basic rule: we assume that the sequence of labels in each list
36  // will be circular in the same order (but not necessarily in the
37  // same direction). The limitation of this method is that with 3D
38  // topologies I cannot guarantee that a congruent but not
39  // identical cellShape (i.e. one sharing the same points but in a
40  // different order) will necessarily be matched.
41 
42  const labelList& labsA = a;
43  const labelList& labsB = b;
44 
45  // Trivial reject: faces are different size
46  label sizeA = labsA.size();
47  label sizeB = labsB.size();
48  if (sizeA != sizeB)
49  {
50  return false;
51  }
52 
53 
54  // First we look for the occurrence of the first label in A, in B
55 
56  label Bptr = -1;
57  label firstA = labsA[0];
58  forAll(labsB, i)
59  {
60  if (labsB[i] == firstA)
61  {
62  Bptr = i; // Denotes 'found match' at element 'i'
63  break;
64  }
65  }
66 
67  // If no match was found, exit false
68  if (Bptr < 0)
69  {
70  return false;
71  }
72 
73  // Now we must look for the direction, if any
74  label secondA = labsA[1];
75  label dir = 0;
76 
77  // Check whether at top of list
78  Bptr++;
79  if (Bptr == labsB.size())
80  {
81  Bptr = 0;
82  }
83 
84  // Test whether upward label matches second A label
85  if (labsB[Bptr] == secondA)
86  {
87  // Yes - direction is 'up'
88  dir = 1;
89  }
90  else
91  {
92  // No - so look downwards, checking whether at bottom of list
93  Bptr -= 2;
94  if (Bptr < 0)
95  {
96  // Case (1) Bptr=-1
97  if (Bptr == -1)
98  {
99  Bptr = labsB.size() - 1;
100  }
101 
102  // Case (2) Bptr = -2
103  else
104  {
105  Bptr = labsB.size() - 2;
106  }
107  }
108 
109  // Test whether downward label matches second A label
110  if (labsB[Bptr] == secondA)
111  {
112  // Yes - direction is 'down'
113  dir = -1;
114  }
115  }
116 
117  // Check whether a match was made at all, and exit false if not
118  if (dir == 0)
119  {
120  return false;
121  }
122 
123  // Decrement size by 2 to account for first searches
124  sizeA -= 2;
125 
126  // We now have both direction of search and next element
127  // to search, so we can continue search until no more points.
128  label Aptr = 1;
129  if (dir > 0)
130  {
131  while (sizeA--)
132  {
133  Aptr++;
134  if (Aptr >= labsA.size())
135  {
136  Aptr = 0;
137  }
138 
139  Bptr++;
140  if (Bptr >= labsB.size())
141  {
142  Bptr = 0;
143  }
144  if (labsA[Aptr] != labsB[Bptr])
145  {
146  return false;
147  }
148  }
149  }
150  else
151  {
152  while (sizeA--)
153  {
154  Aptr++;
155  if (Aptr >= labsA.size())
156  {
157  Aptr = 0;
158  }
159 
160  Bptr--;
161  if (Bptr < 0)
162  {
163  Bptr = labsB.size() - 1;
164  }
165  if (labsA[Aptr] != labsB[Bptr])
166  {
167  return false;
168  }
169  }
170  }
171 
172  // They must be equal
173  return true;
174 }
175 
176 
177 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
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:59
An analytical geometric cellShape.
Definition: cellShape.H:69
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:163
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
tmp< fvMatrix< Type > > operator==(const fvMatrix< Type > &, const fvMatrix< Type > &)