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

vtkFoamAddInternalMesh.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 "vtkFoam.H"
00030 #include "fvMesh.H"
00031 #include "cellModeller.H"
00032 
00033 #include "vtkUnstructuredGrid.h"
00034 #include "vtkCellArray.h"
00035 
00036 #include "vtkFoamInsertNextPoint.H"
00037 
00038 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00039 
00040 void Foam::vtkFoam::addInternalMesh
00041 (
00042     const fvMesh& mesh,
00043     vtkUnstructuredGrid* vtkMesh
00044 )
00045 {
00046     SetName(vtkMesh, "Internal Mesh");
00047 
00048     // Number of additional points needed by the decomposition of polyhedra
00049     label nAddPoints = 0;
00050 
00051     // Number of additional cells generated by the decomposition of polyhedra
00052     label nAddCells = 0;
00053 
00054     const cellModel& tet = *(cellModeller::lookup("tet"));
00055     const cellModel& pyr = *(cellModeller::lookup("pyr"));
00056     const cellModel& prism = *(cellModeller::lookup("prism"));
00057     const cellModel& wedge = *(cellModeller::lookup("wedge"));
00058     const cellModel& tetWedge = *(cellModeller::lookup("tetWedge"));
00059     const cellModel& hex = *(cellModeller::lookup("hex"));
00060 
00061     // Scan for cells which need to be decomposed and count additional points
00062     // and cells
00063     if (debug)
00064     {
00065         Info<< "building cell-shapes" << endl;
00066     }
00067     const cellShapeList& cellShapes = mesh.cellShapes();
00068 
00069     if (debug)
00070     {
00071         Info<< "scanning" << endl;
00072     }
00073 
00074     forAll(cellShapes, cellI)
00075     {
00076         const cellModel& model = cellShapes[cellI].model();
00077 
00078         if 
00079         (
00080             model != hex
00081          && model != wedge
00082          && model != prism
00083          && model != pyr
00084          && model != tet
00085          && model != tetWedge
00086         )
00087         {
00088             const cell& cFaces = mesh.cells()[cellI];
00089 
00090             forAll(cFaces, cFaceI)
00091             {
00092                 const face& f = mesh.faces()[cFaces[cFaceI]];
00093 
00094                 label nFacePoints = f.size();
00095 
00096                 label nQuads = (nFacePoints - 2)/2;
00097                 label nTris = (nFacePoints - 2)%2;
00098                 nAddCells += nQuads + nTris;
00099             }
00100 
00101             nAddCells--;
00102             nAddPoints++;
00103         }
00104     }
00105 
00106     // Set size of additional point addressing array
00107     // (from added point to original cell)
00108     addPointCellLabels_.setSize(nAddPoints);
00109 
00110     // Set size of additional cells mapping array
00111     // (from added cell to original cell)
00112     superCells_.setSize(mesh.nCells() + nAddCells);
00113 
00114     if (debug)
00115     {
00116         Info<< "converting points" << endl;
00117     }
00118 
00119     // Convert Foam mesh vertices to VTK
00120     vtkPoints *vtkpoints = vtkPoints::New();
00121     vtkpoints->Allocate(mesh.nPoints() + nAddPoints);
00122 
00123     const Foam::pointField& points = mesh.points();
00124 
00125     forAll(points, i)
00126     {
00127         vtkFoamInsertNextPoint(vtkpoints, points[i]);
00128     }
00129 
00130     if (debug)
00131     {
00132         Info<< "converting cells" << endl;
00133     }
00134 
00135     vtkMesh->Allocate(mesh.nCells() + nAddCells);
00136 
00137     // Set counters for additional points and additional cells
00138     label api = 0, aci = 0;
00139 
00140     forAll(cellShapes, celli)
00141     {
00142         const cellShape& cellShape = cellShapes[celli];
00143         const cellModel& cellModel = cellShape.model();
00144 
00145         superCells_[aci++] = celli;
00146 
00147         if (cellModel == tet)
00148         {
00149             vtkMesh->InsertNextCell
00150             (
00151                 VTK_TETRA,
00152                 4,
00153                 const_cast<int*>(cellShape.begin())
00154             );
00155         }
00156         else if (cellModel == pyr)
00157         {
00158             vtkMesh->InsertNextCell
00159             (
00160                 VTK_PYRAMID,
00161                 5,
00162                 const_cast<int*>(cellShape.begin())
00163             );
00164         }
00165         else if (cellModel == prism)
00166         {
00167             vtkMesh->InsertNextCell
00168             (
00169                 VTK_WEDGE,
00170                 6,
00171                 const_cast<int*>(cellShape.begin())
00172             );
00173         }
00174         else if (cellModel == tetWedge)
00175         {
00176             // Treat as squeezed prism
00177 
00178             int vtkVerts[6];
00179             vtkVerts[0] = cellShape[0];
00180             vtkVerts[1] = cellShape[2];
00181             vtkVerts[2] = cellShape[1];
00182             vtkVerts[3] = cellShape[3];
00183             vtkVerts[4] = cellShape[4];
00184             vtkVerts[5] = cellShape[4];
00185 
00186             vtkMesh->InsertNextCell(VTK_WEDGE, 6, vtkVerts);
00187         }
00188         else if (cellModel == wedge)
00189         {
00190             // Treat as squeezed hex
00191 
00192             int vtkVerts[8];
00193             vtkVerts[0] = cellShape[0];
00194             vtkVerts[1] = cellShape[1];
00195             vtkVerts[2] = cellShape[2];
00196             vtkVerts[3] = cellShape[2];
00197             vtkVerts[4] = cellShape[3];
00198             vtkVerts[5] = cellShape[4];
00199             vtkVerts[6] = cellShape[5];
00200             vtkVerts[7] = cellShape[6];
00201 
00202             vtkMesh->InsertNextCell(VTK_HEXAHEDRON, 8, vtkVerts);
00203         }
00204         else if (cellModel == hex)
00205         {
00206             vtkMesh->InsertNextCell
00207             (
00208                 VTK_HEXAHEDRON,
00209                 8,
00210                 const_cast<int*>(cellShape.begin())
00211             );
00212         }
00213         else
00214         {
00215             // Polyhedral cell. Decompose into tets + prisms.
00216 
00217             // Mapping from additional point to cell
00218             addPointCellLabels_[api] = celli;
00219 
00220             // Insert the new vertex from the cell-centre
00221             label newVertexLabel = mesh.nPoints() + api;
00222             vtkFoamInsertNextPoint(vtkpoints, mesh.C()[celli]);
00223 
00224             // Whether to insert cell in place of original or not.
00225             bool substituteCell = true;
00226 
00227             const labelList& cFaces = mesh.cells()[celli];
00228 
00229             forAll(cFaces, cFaceI)
00230             {
00231                 const face& f = mesh.faces()[cFaces[cFaceI]];
00232 
00233                 label nFacePoints = f.size();
00234 
00235                 label nQuads = (nFacePoints - 2)/2;
00236                 label nTris = (nFacePoints - 2)%2;
00237 
00238                 label qpi = 0;
00239 
00240                 for (label quadi=0; quadi<nQuads; quadi++)
00241                 {
00242                     label thisCellI = -1;
00243 
00244                     if (substituteCell)
00245                     {
00246                         thisCellI = celli;
00247                         substituteCell = false;
00248                     }
00249                     else
00250                     {
00251                         thisCellI = mesh.nCells() + aci;
00252                         superCells_[aci++] = celli;
00253                     }
00254 
00255                     int addVtkVerts[5];
00256                     addVtkVerts[0] = f[0];
00257                     addVtkVerts[1] = f[qpi + 1];
00258                     addVtkVerts[2] = f[qpi + 2];
00259                     addVtkVerts[3] = f[qpi + 3];
00260                     addVtkVerts[4] = newVertexLabel;
00261                     vtkMesh->InsertNextCell(VTK_PYRAMID, 5, addVtkVerts);
00262 
00263                     qpi += 2;
00264                 }
00265 
00266                 if (nTris)
00267                 {
00268                     label thisCellI = -1;
00269 
00270                     if (substituteCell)
00271                     {
00272                         thisCellI = celli;
00273                         substituteCell = false;
00274                     }
00275                     else
00276                     {
00277                         thisCellI = mesh.nCells() + aci;
00278                         superCells_[aci++] = celli;
00279                     }
00280 
00281                     int addVtkVerts[4];
00282                     addVtkVerts[0] = f[0];
00283                     addVtkVerts[1] = f[qpi + 1];
00284                     addVtkVerts[2] = f[qpi + 2];
00285                     addVtkVerts[3] = newVertexLabel;
00286                     vtkMesh->InsertNextCell(VTK_TETRA, 4, addVtkVerts);
00287                 }
00288             }
00289 
00290             api++;
00291         }
00292     }
00293 
00294     vtkMesh->SetPoints(vtkpoints);
00295     vtkpoints->Delete();
00296 }
00297 
00298 
00299 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd