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

create3DCellShape.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     Construct a cell shape from face information
00027 
00028 \*---------------------------------------------------------------------------*/
00029 
00030 #include "cellShapeRecognition.H"
00031 #include "labelList.H"
00032 
00033 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00034 
00035 namespace Foam
00036 {
00037 
00038 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00039 
00040 cellShape create3DCellShape
00041 (
00042     const label cellIndex,
00043     const labelList& faceLabels,
00044     const faceList& faces,
00045     const labelList& owner,
00046     const labelList& neighbour,
00047     const label fluentCellModelID
00048 )
00049 {
00050     // List of pointers to shape models for 3-D shape recognition
00051     static List<const cellModel*> fluentCellModelLookup
00052     (
00053         7,
00054         reinterpret_cast<const cellModel*>(0)
00055     );
00056 
00057     fluentCellModelLookup[2] = cellModeller::lookup("tet");
00058     fluentCellModelLookup[4] = cellModeller::lookup("hex");
00059     fluentCellModelLookup[5] = cellModeller::lookup("pyr");
00060     fluentCellModelLookup[6] = cellModeller::lookup("prism");
00061 
00062     static label faceMatchingOrder[7][6] =
00063     {
00064         {-1, -1, -1, -1, -1, -1},
00065         {-1, -1, -1, -1, -1, -1},
00066         { 0,  1,  2,  3, -1, -1},    // tet
00067         {-1, -1, -1, -1, -1, -1},
00068         { 0,  2,  4,  3,  5,  1},    // hex
00069         { 0,  1,  2,  3,  4, -1},    // pyr
00070         { 0,  2,  3,  4,  1, -1},    // prism
00071     };
00072 
00073     const cellModel& curModel = *fluentCellModelLookup[fluentCellModelID];
00074 
00075     // Checking
00076     if (faceLabels.size() != curModel.nFaces())
00077     {
00078         FatalErrorIn
00079         (
00080             "create3DCellShape(const label cellIndex, "
00081             "const labelList& faceLabels, const labelListList& faces, "
00082             "const labelList& owner, const labelList& neighbour, "
00083             "const label fluentCellModelID)"
00084         )   << "Number of face labels not equal to"
00085             << "number of face in the model. "
00086             << "Number of face labels: " << faceLabels.size()
00087             << " number of faces in model: " << curModel.nFaces()
00088             << abort(FatalError);
00089     }
00090 
00091     // make a list of outward-pointing faces
00092     labelListList localFaces(faceLabels.size());
00093 
00094     forAll  (faceLabels, faceI)
00095     {
00096         const label curFaceLabel = faceLabels[faceI];
00097 
00098         const labelList& curFace = faces[curFaceLabel];
00099 
00100         if (owner[curFaceLabel] == cellIndex)
00101         {
00102             localFaces[faceI] = curFace;
00103         }
00104         else if (neighbour[curFaceLabel] == cellIndex)
00105         {
00106             // Reverse the face
00107             localFaces[faceI].setSize(curFace.size());
00108 
00109             forAllReverse(curFace, i)
00110             {
00111                 localFaces[faceI][curFace.size() - i - 1] =
00112                     curFace[i];
00113             }
00114         }
00115         else
00116         {
00117             FatalErrorIn
00118             (
00119                 "create3DCellShape(const label cellIndex, "
00120                 "const labelList& faceLabels, const labelListList& faces, "
00121                 "const labelList& owner, const labelList& neighbour, "
00122                 "const label fluentCellModelID)"
00123             )   << "face " << curFaceLabel
00124                 << " does not belong to cell " << cellIndex
00125                 << ". Face owner: " << owner[curFaceLabel] << " neighbour: "
00126                 << neighbour[curFaceLabel]
00127                 << abort(FatalError);
00128         }
00129     }
00130 
00131     // Algorithm:
00132     // Make an empty list of pointLabels and initialise it with -1. Pick the
00133     // first face from modelFaces and look through the faces to find one with
00134     // the same number of labels. Insert face by copying its labels into
00135     // pointLabels. Mark the face as used. Loop through all model faces.
00136     // For each model face loop through faces. If the face is unused and the
00137     // numbers of labels fit, try to match the face onto the point labels. If
00138     // at least one edge is matched, insert the face into pointLabels. If at
00139     // any stage the matching algorithm reaches the end of faces, the matching
00140     // algorithm has failed. Once all the faces are matched, the list of
00141     // pointLabels defines the model.
00142 
00143     // Make a list of empty pointLabels
00144     labelList pointLabels(curModel.nPoints(), -1);
00145 
00146     // Follow the used mesh faces
00147     List<bool> meshFaceUsed(localFaces.size(), false);
00148 
00149     // Get the raw model faces
00150     const faceList& modelFaces = curModel.modelFaces();
00151 
00152     // Insert the first face into the list
00153     const labelList& firstModelFace =
00154         modelFaces[faceMatchingOrder[fluentCellModelID][0]];
00155 
00156     bool found = false;
00157 
00158     forAll (localFaces, meshFaceI)
00159     {
00160         if (localFaces[meshFaceI].size() == firstModelFace.size())
00161         {
00162             // Match. Insert points into the pointLabels
00163             found = true;
00164 
00165             const labelList& curMeshFace = localFaces[meshFaceI];
00166 
00167             meshFaceUsed[meshFaceI] = true;
00168 
00169             forAll (curMeshFace, pointI)
00170             {
00171                 pointLabels[firstModelFace[pointI]] = curMeshFace[pointI];
00172             }
00173 
00174             break;
00175         }
00176     }
00177 
00178     if (!found)
00179     {
00180         FatalErrorIn
00181         (
00182             "create3DCellShape(const label cellIndex, "
00183             "const labelList& faceLabels, const labelListList& faces, "
00184             "const labelList& owner, const labelList& neighbour, "
00185             "const label fluentCellModelID)"
00186         )   << "Cannot find match for first face. "
00187             << "cell model: " << curModel.name() << " first model face: "
00188             << firstModelFace << " Mesh faces: " << localFaces
00189             << abort(FatalError);
00190     }
00191 
00192     for (label modelFaceI = 1; modelFaceI < modelFaces.size(); modelFaceI++)
00193     {
00194         // get the next model face
00195         const labelList& curModelFace =
00196             modelFaces
00197             [faceMatchingOrder[fluentCellModelID][modelFaceI]];
00198 
00199         found = false;
00200 
00201         // Loop through mesh faces until a match is found
00202         forAll (localFaces, meshFaceI)
00203         {
00204             if
00205             (
00206                 !meshFaceUsed[meshFaceI]
00207              && localFaces[meshFaceI].size() == curModelFace.size()
00208             )
00209             {
00210                 // A possible match. A mesh face will be rotated, so make a copy
00211                 labelList meshFaceLabels = localFaces[meshFaceI];
00212 
00213                 for
00214                 (
00215                     label rotation = 0;
00216                     rotation < meshFaceLabels.size();
00217                     rotation++
00218                 )
00219                 {
00220                     // try matching the face
00221                     label nMatchedLabels = 0;
00222 
00223                     forAll (meshFaceLabels, pointI)
00224                     {
00225                         if
00226                         (
00227                             pointLabels[curModelFace[pointI]]
00228                          == meshFaceLabels[pointI]
00229                         )
00230                         {
00231                             nMatchedLabels++;
00232                         }
00233                     }
00234 
00235                     if (nMatchedLabels >= 2)
00236                     {
00237                         // match!
00238                         found = true;
00239                     }
00240 
00241                     if (found)
00242                     {
00243                         // match found. Insert mesh face
00244                         forAll (meshFaceLabels, pointI)
00245                         {
00246                             pointLabels[curModelFace[pointI]] =
00247                                 meshFaceLabels[pointI];
00248                         }
00249 
00250                         meshFaceUsed[meshFaceI] = true;
00251 
00252                         break;
00253                     }
00254                     else
00255                     {
00256                         // No match found. Rotate face
00257                         label firstLabel = meshFaceLabels[0];
00258 
00259                         for (label i = 1; i < meshFaceLabels.size(); i++)
00260                         {
00261                             meshFaceLabels[i - 1] = meshFaceLabels[i];
00262                         }
00263 
00264                         meshFaceLabels[meshFaceLabels.size() - 1] = firstLabel;
00265                     }
00266                 }
00267 
00268                 if (found) break;
00269             }
00270         }
00271 
00272         if (!found)
00273         {
00274             // A model face is not matched. Shape detection failed
00275             FatalErrorIn
00276             (
00277                 "create3DCellShape(const label cellIndex, "
00278                 "const labelList& faceLabels, const labelListList& faces, "
00279                 "const labelList& owner, const labelList& neighbour, "
00280                 "const label fluentCellModelID)"
00281             )   << "Cannot find match for face "
00282                 << modelFaceI
00283                 << ".\nModel: " << curModel.name() << " model face: "
00284                 << curModelFace << " Mesh faces: " << localFaces
00285                 << "Matched points: " << pointLabels
00286                 << abort(FatalError);
00287         }
00288     }
00289 
00290     return cellShape(curModel, pointLabels);
00291 }
00292 
00293 
00294 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00295 
00296 } // End namespace Foam
00297 
00298 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd