OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

cellShapeEqual.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 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 the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     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, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Description
00026     Equality operator for cellShape class
00027 
00028 \*---------------------------------------------------------------------------*/
00029 
00030 #include "cellShape.H"
00031 
00032 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00033 
00034 bool Foam::operator==(const cellShape& a, const cellShape& b)
00035 {
00036     // Basic rule: we assume that the sequence of labels in each list
00037     // will be circular in the same order (but not necessarily in the
00038     // same direction). The limitation of this method is that with 3D
00039     // topologies I cannot guarantee that a congruent but not
00040     // identical cellShape (i.e. one sharing the same points but in a
00041     // different order) will necessarily be matched.
00042 
00043     const labelList& labsA = a;
00044     const labelList& labsB = b;
00045 
00046     // Trivial reject: faces are different size
00047     label sizeA = labsA.size();
00048     label sizeB = labsB.size();
00049     if (sizeA != sizeB)
00050     {
00051         return false;
00052     }
00053 
00054 
00055     // First we look for the occurrence of the first label in A, in B
00056 
00057     label Bptr = -1;
00058     label firstA = labsA[0];
00059     forAll(labsB, i)
00060     {
00061         if (labsB[i] == firstA)
00062         {
00063             Bptr = i;                // Denotes 'found match' at element 'i'
00064             break;
00065         }
00066     }
00067 
00068     // If no match was found, exit false
00069     if (Bptr < 0)
00070     {
00071         return false;
00072     }
00073 
00074     // Now we must look for the direction, if any
00075     label secondA = labsA[1];
00076     label dir = 0;
00077 
00078     // Check whether at top of list
00079     Bptr++;
00080     if (Bptr == labsB.size())
00081     {
00082         Bptr = 0;
00083     }
00084 
00085     // Test whether upward label matches second A label
00086     if (labsB[Bptr] == secondA)
00087     {
00088         // Yes - direction is 'up'
00089         dir = 1;
00090     }
00091     else
00092     {
00093         // No - so look downwards, checking whether at bottom of list
00094         Bptr -= 2;
00095         if (Bptr < 0)
00096         {
00097             // Case (1) Bptr=-1
00098             if (Bptr == -1)
00099             {
00100                 Bptr = labsB.size() - 1;
00101             }
00102 
00103             // Case (2) Bptr = -2
00104             else
00105             {
00106                 Bptr = labsB.size() - 2;
00107             }
00108         }
00109 
00110         // Test whether downward label matches second A label
00111         if (labsB[Bptr] == secondA)
00112         {
00113             // Yes - direction is 'down'
00114             dir = -1;
00115         }
00116     }
00117 
00118     // Check whether a match was made at all, and exit false if not
00119     if (dir == 0)
00120     {
00121         return false;
00122     }
00123 
00124     // Decrement size by 2 to account for first searches
00125     sizeA -= 2;
00126 
00127     // We now have both direction of search and next element
00128     // to search, so we can continue search until no more points.
00129     label Aptr = 1;
00130     if (dir > 0)
00131     {
00132         while (sizeA--)
00133         {
00134             Aptr++;
00135             if (Aptr >= labsA.size())
00136             {
00137                 Aptr = 0;
00138             }
00139 
00140             Bptr++;
00141             if (Bptr >= labsB.size())
00142             {
00143                 Bptr = 0;
00144             }
00145             if (labsA[Aptr] != labsB[Bptr])
00146             {
00147                 return false;
00148             }
00149         }
00150     }
00151     else
00152     {
00153         while (sizeA--)
00154         {
00155             Aptr++;
00156             if (Aptr >= labsA.size())
00157             {
00158                 Aptr = 0;
00159             }
00160 
00161             Bptr--;
00162             if (Bptr < 0)
00163             {
00164                 Bptr = labsB.size() - 1;
00165             }
00166             if (labsA[Aptr] != labsB[Bptr])
00167             {
00168                 return false;
00169             }
00170         }
00171     }
00172 
00173     // They must be equal
00174     return true;
00175 }
00176 
00177 
00178 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd