00001 /*---------------------------------------------------------------------------*\ 00002 ========= | 00003 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox 00004 \\ / O peration | 00005 \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd. 00006 \\/ M anipulation | 00007 ------------------------------------------------------------------------------- 00008 License 00009 This file is part of OpenFOAM. 00010 00011 OpenFOAM is free software: you can redistribute it and/or modify it 00012 under the terms of the GNU General Public License as published by 00013 the Free Software Foundation, either version 3 of the License, or 00014 (at your option) any later version. 00015 00016 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT 00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00018 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 00019 for more details. 00020 00021 You should have received a copy of the GNU General Public License 00022 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>. 00023 00024 Description 00025 Equality operator for cellShape class 00026 00027 \*---------------------------------------------------------------------------*/ 00028 00029 #include "cellShape.H" 00030 00031 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // 00032 00033 bool Foam::operator==(const cellShape& a, const cellShape& b) 00034 { 00035 // Basic rule: we assume that the sequence of labels in each list 00036 // will be circular in the same order (but not necessarily in the 00037 // same direction). The limitation of this method is that with 3D 00038 // topologies I cannot guarantee that a congruent but not 00039 // identical cellShape (i.e. one sharing the same points but in a 00040 // different order) will necessarily be matched. 00041 00042 const labelList& labsA = a; 00043 const labelList& labsB = b; 00044 00045 // Trivial reject: faces are different size 00046 label sizeA = labsA.size(); 00047 label sizeB = labsB.size(); 00048 if (sizeA != sizeB) 00049 { 00050 return false; 00051 } 00052 00053 00054 // First we look for the occurrence of the first label in A, in B 00055 00056 label Bptr = -1; 00057 label firstA = labsA[0]; 00058 forAll(labsB, i) 00059 { 00060 if (labsB[i] == firstA) 00061 { 00062 Bptr = i; // Denotes 'found match' at element 'i' 00063 break; 00064 } 00065 } 00066 00067 // If no match was found, exit false 00068 if (Bptr < 0) 00069 { 00070 return false; 00071 } 00072 00073 // Now we must look for the direction, if any 00074 label secondA = labsA[1]; 00075 label dir = 0; 00076 00077 // Check whether at top of list 00078 Bptr++; 00079 if (Bptr == labsB.size()) 00080 { 00081 Bptr = 0; 00082 } 00083 00084 // Test whether upward label matches second A label 00085 if (labsB[Bptr] == secondA) 00086 { 00087 // Yes - direction is 'up' 00088 dir = 1; 00089 } 00090 else 00091 { 00092 // No - so look downwards, checking whether at bottom of list 00093 Bptr -= 2; 00094 if (Bptr < 0) 00095 { 00096 // Case (1) Bptr=-1 00097 if (Bptr == -1) 00098 { 00099 Bptr = labsB.size() - 1; 00100 } 00101 00102 // Case (2) Bptr = -2 00103 else 00104 { 00105 Bptr = labsB.size() - 2; 00106 } 00107 } 00108 00109 // Test whether downward label matches second A label 00110 if (labsB[Bptr] == secondA) 00111 { 00112 // Yes - direction is 'down' 00113 dir = -1; 00114 } 00115 } 00116 00117 // Check whether a match was made at all, and exit false if not 00118 if (dir == 0) 00119 { 00120 return false; 00121 } 00122 00123 // Decrement size by 2 to account for first searches 00124 sizeA -= 2; 00125 00126 // We now have both direction of search and next element 00127 // to search, so we can continue search until no more points. 00128 label Aptr = 1; 00129 if (dir > 0) 00130 { 00131 while (sizeA--) 00132 { 00133 Aptr++; 00134 if (Aptr >= labsA.size()) 00135 { 00136 Aptr = 0; 00137 } 00138 00139 Bptr++; 00140 if (Bptr >= labsB.size()) 00141 { 00142 Bptr = 0; 00143 } 00144 if (labsA[Aptr] != labsB[Bptr]) 00145 { 00146 return false; 00147 } 00148 } 00149 } 00150 else 00151 { 00152 while (sizeA--) 00153 { 00154 Aptr++; 00155 if (Aptr >= labsA.size()) 00156 { 00157 Aptr = 0; 00158 } 00159 00160 Bptr--; 00161 if (Bptr < 0) 00162 { 00163 Bptr = labsB.size() - 1; 00164 } 00165 if (labsA[Aptr] != labsB[Bptr]) 00166 { 00167 return false; 00168 } 00169 } 00170 } 00171 00172 // They must be equal 00173 return true; 00174 } 00175 00176 00177 // ************************ vim: set sw=4 sts=4 et: ************************ //