initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / applications / utilities / postProcessing / dataConversion / foamToVTK / vtkTopo.C
blobc21b1095736cf231df24ddbe3cdfe2cfb9c96778
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software; you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Description
27     Note: bug in vtk displaying wedges? Seems to display ok if we decompose
28     them. Should be thoroughly tested!
29     (they appear rarely in polyhedral meshes, do appear in some cut meshes)
31 \*---------------------------------------------------------------------------*/
33 #include "vtkTopo.H"
34 #include "polyMesh.H"
35 #include "cellShape.H"
36 #include "cellModeller.H"
38 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
40 // Construct from components
41 Foam::vtkTopo::vtkTopo(const polyMesh& mesh)
43     mesh_(mesh),
44     vertLabels_(),
45     cellTypes_(),
46     addPointCellLabels_(),
47     superCells_()
49     const cellModel& tet = *(cellModeller::lookup("tet"));
50     const cellModel& pyr = *(cellModeller::lookup("pyr"));
51     const cellModel& prism = *(cellModeller::lookup("prism"));
52     const cellModel& tetWedge = *(cellModeller::lookup("tetWedge"));
53     const cellModel& hex = *(cellModeller::lookup("hex"));
56     const cellShapeList& cellShapes = mesh_.cellShapes();
59     // Number of additional points needed by the decomposition of polyhedra
60     label nAddPoints = 0;
62     // Number of additional cells generated by the decomposition of polyhedra
63     label nAddCells = 0;
65     // Scan for cells which need to be decomposed and count additional points
66     // and cells
68     forAll(cellShapes, cellI)
69     {
70         const cellModel& model = cellShapes[cellI].model();
72         if 
73         (
74             model != hex
75 //         && model != wedge            // See above.
76          && model != prism
77          && model != pyr
78          && model != tet
79          && model != tetWedge
80         )
81         {
82             const cell& cFaces = mesh_.cells()[cellI];
84             forAll(cFaces, cFaceI)
85             {
86                 const face& f = mesh_.faces()[cFaces[cFaceI]];
88                 label nQuads = 0;
89                 label nTris = 0;
90                 f.nTrianglesQuads(mesh_.points(), nTris, nQuads);
92                 nAddCells += nQuads + nTris;
93             }
95             nAddCells--;
96             nAddPoints++;
97         }
98     }
100     // Set size of additional point addressing array
101     // (from added point to original cell)
102     addPointCellLabels_.setSize(nAddPoints);
104     // Set size of additional cells mapping array
105     // (from added cell to original cell)
106     superCells_.setSize(nAddCells);
108     // List of vertex labels in VTK ordering
109     vertLabels_.setSize(cellShapes.size() + nAddCells);
111     // Label of vtk type
112     cellTypes_.setSize(cellShapes.size() + nAddCells);
114     // Set counters for additional points and additional cells
115     label api = 0, aci = 0;
117     forAll(cellShapes, cellI)
118     {
119         const cellShape& cellShape = cellShapes[cellI];
120         const cellModel& cellModel = cellShape.model();
122         labelList& vtkVerts = vertLabels_[cellI];
124         if (cellModel == tet)
125         {
126             vtkVerts = cellShape;
128             cellTypes_[cellI] = VTK_TETRA;
129         }
130         else if (cellModel == pyr)
131         {
132             vtkVerts = cellShape;
134             cellTypes_[cellI] = VTK_PYRAMID;
135         }
136         else if (cellModel == prism)
137         {
138             vtkVerts.setSize(6);
139             vtkVerts[0] = cellShape[0];
140             vtkVerts[1] = cellShape[2];
141             vtkVerts[2] = cellShape[1];
142             vtkVerts[3] = cellShape[3];
143             vtkVerts[4] = cellShape[5];
144             vtkVerts[5] = cellShape[4];
146             // VTK calls this a wedge.
147             cellTypes_[cellI] = VTK_WEDGE;
148         }
149         else if (cellModel == tetWedge)
150         {
151             // Treat as squeezed prism
152             vtkVerts.setSize(6);
153             vtkVerts[0] = cellShape[0];
154             vtkVerts[1] = cellShape[2];
155             vtkVerts[2] = cellShape[1];
156             vtkVerts[3] = cellShape[3];
157             vtkVerts[4] = cellShape[4];
158             vtkVerts[5] = cellShape[4];
160             cellTypes_[cellI] = VTK_WEDGE;
161         }
162 //        else if (cellModel == wedge)
163 //        {
164 //            // Treat as squeezed hex
165 //            vtkVerts.setSize(8);
166 //            vtkVerts[0] = cellShape[0];
167 //            vtkVerts[1] = cellShape[1];
168 //            vtkVerts[2] = cellShape[2];
169 //            vtkVerts[3] = cellShape[0];
170 //            vtkVerts[4] = cellShape[3];
171 //            vtkVerts[5] = cellShape[4];
172 //            vtkVerts[6] = cellShape[5];
173 //            vtkVerts[7] = cellShape[6];
175 //            cellTypes_[cellI] = VTK_HEXAHEDRON;
176 //        }
177         else if (cellModel == hex)
178         {
179             vtkVerts.setSize(8);
180             vtkVerts[0] = cellShape[0];
181             vtkVerts[1] = cellShape[1];
182             vtkVerts[2] = cellShape[2];
183             vtkVerts[3] = cellShape[3];
184             vtkVerts[4] = cellShape[4];
185             vtkVerts[5] = cellShape[5];
186             vtkVerts[6] = cellShape[6];
187             vtkVerts[7] = cellShape[7];
189             cellTypes_[cellI] = VTK_HEXAHEDRON;
190         }
191         else
192         {
193             // Polyhedral cell. Decompose into tets + prisms.
194             // (see dxFoamExec/createDxConnections.C)
196             // Mapping from additional point to cell
197             addPointCellLabels_[api] = cellI;
199             // Whether to insert cell in place of original or not.
200             bool substituteCell = true;
202             const labelList& cFaces = mesh_.cells()[cellI];
204             forAll(cFaces, cFaceI)
205             {
206                 const face& f = mesh_.faces()[cFaces[cFaceI]];
208                 // Number of triangles and quads in decomposition
209                 label nTris = 0;
210                 label nQuads = 0;
211                 f.nTrianglesQuads(mesh_.points(), nTris, nQuads);
213                 // Do actual decomposition into triFcs and quadFcs.
214                 faceList triFcs(nTris);
215                 faceList quadFcs(nQuads);
216                 label trii = 0;
217                 label quadi = 0;
218                 f.trianglesQuads(mesh_.points(), trii, quadi, triFcs, quadFcs);
220                 forAll(quadFcs, quadi)
221                 {
222                     label thisCellI = -1;
224                     if (substituteCell)
225                     {
226                         thisCellI = cellI;
228                         substituteCell = false;
229                     }
230                     else
231                     {
232                         thisCellI = mesh_.nCells() + aci;
234                         superCells_[aci] = cellI;
236                         aci++;
237                     }
239                     labelList& addVtkVerts = vertLabels_[thisCellI];
241                     addVtkVerts.setSize(5);
243                     const face& quad = quadFcs[quadi];
245                     addVtkVerts[0] = quad[0];
246                     addVtkVerts[1] = quad[1];
247                     addVtkVerts[2] = quad[2];
248                     addVtkVerts[3] = quad[3];
249                     addVtkVerts[4] = mesh_.nPoints() + api;
251                     cellTypes_[thisCellI] = VTK_PYRAMID;
252                 }
254                 forAll(triFcs, trii)
255                 {
256                     label thisCellI = -1;
258                     if (substituteCell)
259                     {
260                         thisCellI = cellI;
262                         substituteCell = false;
263                     }
264                     else
265                     {
266                         thisCellI = mesh_.nCells() + aci;
268                         superCells_[aci] = cellI;
270                         aci++;
271                     }
274                     labelList& addVtkVerts = vertLabels_[thisCellI];
276                     const face& tri = triFcs[trii];
278                     addVtkVerts.setSize(4);
279                     addVtkVerts[0] = tri[0];
280                     addVtkVerts[1] = tri[1];
281                     addVtkVerts[2] = tri[2];
282                     addVtkVerts[3] = mesh_.nPoints() + api;
284                     cellTypes_[thisCellI] = VTK_TETRA;
285                 }
286             }
288             api++;
289         }
290     }
292     Pout<< "    Original cells:" << mesh_.nCells()
293         << " points:" << mesh_.nPoints()
294         << "   Additional cells:" << superCells_.size()
295         << "  additional points:" << addPointCellLabels_.size()
296         << nl << endl;
299 // ************************************************************************* //