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

vtkPV3FoamMeshVolume.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 
00027 \*---------------------------------------------------------------------------*/
00028 
00029 #include "vtkPV3Foam.H"
00030 
00031 // Foam includes
00032 #include "fvMesh.H"
00033 #include "cellModeller.H"
00034 #include "vtkPV3FoamPoints.H"
00035 
00036 // VTK includes
00037 #include "vtkCellArray.h"
00038 #include "vtkUnstructuredGrid.h"
00039 
00040 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00041 
00042 vtkUnstructuredGrid* Foam::vtkPV3Foam::volumeVTKMesh
00043 (
00044     const fvMesh& mesh,
00045     polyDecomp& decompInfo
00046 )
00047 {
00048     vtkUnstructuredGrid* vtkmesh = vtkUnstructuredGrid::New();
00049 
00050     if (debug)
00051     {
00052         Info<< "<beg> Foam::vtkPV3Foam::volumeVTKMesh" << endl;
00053         printMemory();
00054     }
00055 
00056     // Number of additional points needed by the decomposition of polyhedra
00057     label nAddPoints = 0;
00058 
00059     // Number of additional cells generated by the decomposition of polyhedra
00060     label nAddCells = 0;
00061 
00062     labelList& superCells = decompInfo.superCells();
00063     labelList& addPointCellLabels = decompInfo.addPointCellLabels();
00064 
00065     const cellModel& tet = *(cellModeller::lookup("tet"));
00066     const cellModel& pyr = *(cellModeller::lookup("pyr"));
00067     const cellModel& prism = *(cellModeller::lookup("prism"));
00068     const cellModel& wedge = *(cellModeller::lookup("wedge"));
00069     const cellModel& tetWedge = *(cellModeller::lookup("tetWedge"));
00070     const cellModel& hex = *(cellModeller::lookup("hex"));
00071 
00072     // Scan for cells which need to be decomposed and count additional points
00073     // and cells
00074     if (debug)
00075     {
00076         Info<< "... building cell-shapes" << endl;
00077     }
00078     const cellShapeList& cellShapes = mesh.cellShapes();
00079 
00080     if (debug)
00081     {
00082         Info<< "... scanning" << endl;
00083     }
00084     forAll(cellShapes, cellI)
00085     {
00086         const cellModel& model = cellShapes[cellI].model();
00087 
00088         if
00089         (
00090             model != hex
00091          && model != wedge
00092          && model != prism
00093          && model != pyr
00094          && model != tet
00095          && model != tetWedge
00096         )
00097         {
00098             const cell& cFaces = mesh.cells()[cellI];
00099 
00100             forAll(cFaces, cFaceI)
00101             {
00102                 const face& f = mesh.faces()[cFaces[cFaceI]];
00103 
00104                 label nFacePoints = f.size();
00105 
00106                 label nQuads = (nFacePoints - 2)/2;
00107                 label nTris = (nFacePoints - 2)%2;
00108                 nAddCells += nQuads + nTris;
00109             }
00110 
00111             nAddCells--;
00112             nAddPoints++;
00113         }
00114     }
00115 
00116     // Set size of additional point addressing array
00117     // (from added point to original cell)
00118     addPointCellLabels.setSize(nAddPoints);
00119 
00120     // Set size of additional cells mapping array
00121     // (from added cell to original cell)
00122 
00123     if (debug)
00124     {
00125         Info<<" mesh nCells     = " << mesh.nCells() << nl
00126             <<"      nPoints    = " << mesh.nPoints() << nl
00127             <<"      nAddCells  = " << nAddCells << nl
00128             <<"      nAddPoints = " << nAddPoints << endl;
00129     }
00130 
00131     superCells.setSize(mesh.nCells() + nAddCells);
00132 
00133     if (debug)
00134     {
00135         Info<< "... converting points" << endl;
00136     }
00137 
00138     // Convert Foam mesh vertices to VTK
00139     vtkPoints *vtkpoints = vtkPoints::New();
00140     vtkpoints->Allocate( mesh.nPoints() + nAddPoints );
00141 
00142     const Foam::pointField& points = mesh.points();
00143 
00144     forAll(points, i)
00145     {
00146         vtkPV3FoamInsertNextPoint(vtkpoints, points[i]);
00147     }
00148 
00149 
00150     if (debug)
00151     {
00152         Info<< "... converting cells" << endl;
00153     }
00154 
00155     vtkmesh->Allocate( mesh.nCells() + nAddCells );
00156 
00157     // Set counters for additional points and additional cells
00158     label addPointI = 0, addCellI = 0;
00159 
00160     // Create storage for points - needed for mapping from Foam to VTK
00161     // data types - max 'order' = hex = 8 points
00162     vtkIdType nodeIds[8];
00163 
00164     forAll(cellShapes, cellI)
00165     {
00166         const cellShape& cellShape = cellShapes[cellI];
00167         const cellModel& cellModel = cellShape.model();
00168 
00169         superCells[addCellI++] = cellI;
00170 
00171         if (cellModel == tet)
00172         {
00173             for (int j = 0; j < 4; j++)
00174             {
00175                 nodeIds[j] = cellShape[j];
00176             }
00177             vtkmesh->InsertNextCell
00178             (
00179                 VTK_TETRA,
00180                 4,
00181                 nodeIds
00182             );
00183         }
00184         else if (cellModel == pyr)
00185         {
00186             for (int j = 0; j < 5; j++)
00187             {
00188                 nodeIds[j] = cellShape[j];
00189             }
00190             vtkmesh->InsertNextCell
00191             (
00192                 VTK_PYRAMID,
00193                 5,
00194                 nodeIds
00195             );
00196         }
00197         else if (cellModel == prism)
00198         {
00199             for (int j = 0; j < 6; j++)
00200             {
00201                 nodeIds[j] = cellShape[j];
00202             }
00203             vtkmesh->InsertNextCell
00204             (
00205                 VTK_WEDGE,
00206                 6,
00207                 nodeIds
00208             );
00209         }
00210         else if (cellModel == tetWedge)
00211         {
00212             // Treat as squeezed prism
00213 
00214             nodeIds[0] = cellShape[0];
00215             nodeIds[1] = cellShape[2];
00216             nodeIds[2] = cellShape[1];
00217             nodeIds[3] = cellShape[3];
00218             nodeIds[4] = cellShape[4];
00219             nodeIds[5] = cellShape[4];
00220 
00221             vtkmesh->InsertNextCell
00222             (
00223                 VTK_WEDGE,
00224                 6,
00225                 nodeIds
00226             );
00227         }
00228         else if (cellModel == wedge)
00229         {
00230             // Treat as squeezed hex
00231 
00232             nodeIds[0] = cellShape[0];
00233             nodeIds[1] = cellShape[1];
00234             nodeIds[2] = cellShape[2];
00235             nodeIds[3] = cellShape[2];
00236             nodeIds[4] = cellShape[3];
00237             nodeIds[5] = cellShape[4];
00238             nodeIds[6] = cellShape[5];
00239             nodeIds[7] = cellShape[6];
00240 
00241             vtkmesh->InsertNextCell
00242             (
00243                 VTK_HEXAHEDRON,
00244                 8,
00245                 nodeIds
00246             );
00247         }
00248         else if (cellModel == hex)
00249         {
00250             for (int j = 0; j < 8; j++)
00251             {
00252                 nodeIds[j] = cellShape[j];
00253             }
00254             vtkmesh->InsertNextCell
00255             (
00256                 VTK_HEXAHEDRON,
00257                 8,
00258                 nodeIds
00259             );
00260         }
00261         else
00262         {
00263             // Polyhedral cell. Decompose into tets + prisms.
00264 
00265             // Mapping from additional point to cell
00266             addPointCellLabels[addPointI] = cellI;
00267 
00268             // Insert the new vertex from the cell-centre
00269             label newVertexLabel = mesh.nPoints() + addPointI;
00270             vtkPV3FoamInsertNextPoint(vtkpoints, mesh.C()[cellI]);
00271 
00272             // Whether to insert cell in place of original or not.
00273             bool substituteCell = true;
00274 
00275             const labelList& cFaces = mesh.cells()[cellI];
00276 
00277             forAll(cFaces, cFaceI)
00278             {
00279                 const face& f = mesh.faces()[cFaces[cFaceI]];
00280 
00281                 label nFacePoints = f.size();
00282 
00283                 label nQuads = (nFacePoints - 2)/2;
00284                 label nTris = (nFacePoints - 2)%2;
00285 
00286                 label qpi = 0;
00287 
00288                 for (label quadi=0; quadi<nQuads; quadi++)
00289                 {
00290                     label thisCellI = -1;
00291 
00292                     if (substituteCell)
00293                     {
00294                         thisCellI = cellI;
00295                         substituteCell = false;
00296                     }
00297                     else
00298                     {
00299                         thisCellI = mesh.nCells() + addCellI;
00300                         superCells[addCellI++] = cellI;
00301                     }
00302 
00303                     nodeIds[0] = f[0];
00304                     nodeIds[1] = f[qpi + 1];
00305                     nodeIds[2] = f[qpi + 2];
00306                     nodeIds[3] = f[qpi + 3];
00307                     nodeIds[4] = newVertexLabel;
00308                     vtkmesh->InsertNextCell
00309                     (
00310                         VTK_PYRAMID,
00311                         5,
00312                         nodeIds
00313                     );
00314 
00315                     qpi += 2;
00316                 }
00317 
00318                 if (nTris)
00319                 {
00320                     label thisCellI = -1;
00321 
00322                     if (substituteCell)
00323                     {
00324                         thisCellI = cellI;
00325                         substituteCell = false;
00326                     }
00327                     else
00328                     {
00329                         thisCellI = mesh.nCells() + addCellI;
00330                         superCells[addCellI++] = cellI;
00331                     }
00332 
00333                     nodeIds[0] = f[0];
00334                     nodeIds[1] = f[qpi + 1];
00335                     nodeIds[2] = f[qpi + 2];
00336                     nodeIds[3] = newVertexLabel;
00337                     vtkmesh->InsertNextCell
00338                     (
00339                         VTK_TETRA,
00340                         4,
00341                         nodeIds
00342                     );
00343                 }
00344             }
00345 
00346             addPointI++;
00347         }
00348     }
00349 
00350     vtkmesh->SetPoints(vtkpoints);
00351     vtkpoints->Delete();
00352 
00353     if (debug)
00354     {
00355         Info<< "<end> Foam::vtkPV3Foam::volumeVTKMesh" << endl;
00356         printMemory();
00357     }
00358 
00359     return vtkmesh;
00360 }
00361 
00362 
00363 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd