vtkPVblockMeshConvert.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2019 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
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.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "vtkPVblockMesh.H"
27 #include "vtkPVblockMeshReader.h"
28 
29 // OpenFOAM includes
30 #include "blockMesh.H"
31 #include "Time.H"
32 
33 #include "vtkOpenFOAMPoints.H"
34 
35 // VTK includes
36 #include "vtkCellArray.h"
37 #include "vtkDataArraySelection.h"
38 #include "vtkMultiBlockDataSet.h"
39 #include "vtkPoints.h"
40 #include "vtkPolyData.h"
41 #include "vtkUnstructuredGrid.h"
42 
43 
44 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
45 
46 void Foam::vtkPVblockMesh::convertMeshBlocks
47 (
48  vtkMultiBlockDataSet* output,
49  int& blockNo
50 )
51 {
52  vtkDataArraySelection* selection = reader_->GetBlockSelection();
53  arrayRange& range = arrayRangeBlocks_;
54  range.block(blockNo); // Set output block
55  label datasetNo = 0; // Restart at dataset 0
56 
57  const blockMesh& blkMesh = *meshPtr_;
58  const Foam::pointField& blockPoints = blkMesh.vertices();
59 
60  if (debug)
61  {
63  }
64 
65  int blockI = 0;
66  const scalar scaleFactor = blkMesh.scaleFactor();
67 
68  for
69  (
70  int partId = range.start();
71  partId < range.end();
72  ++partId, ++blockI
73  )
74  {
75  if (!blockStatus_[partId])
76  {
77  continue;
78  }
79 
80  const blockDescriptor& blockDef = blkMesh[blockI];
81 
82  vtkUnstructuredGrid* vtkmesh = vtkUnstructuredGrid::New();
83 
84  // Convert OpenFOAM mesh vertices to VTK
85  vtkPoints *vtkpoints = vtkPoints::New();
86  vtkpoints->Allocate( blockDef.nPoints() );
87  const labelList& blockLabels = blockDef.blockShape();
88 
89  vtkmesh->Allocate(1);
90  vtkIdType nodeIds[8];
91 
92  forAll(blockLabels, ptI)
93  {
95  (
96  vtkpoints,
97  blockPoints[blockLabels[ptI]],
98  scaleFactor
99  );
100 
101  nodeIds[ptI] = ptI;
102  }
103 
104  vtkmesh->InsertNextCell
105  (
106  VTK_HEXAHEDRON,
107  8,
108  nodeIds
109  );
110 
111  vtkmesh->SetPoints(vtkpoints);
112  vtkpoints->Delete();
113 
114  AddToBlock
115  (
116  output, vtkmesh, range, datasetNo,
117  selection->GetArrayName(partId)
118  );
119 
120  vtkmesh->Delete();
121  datasetNo++;
122  }
123 
124 
125  // Anything added?
126  if (datasetNo)
127  {
128  ++blockNo;
129  }
130 }
131 
132 
133 void Foam::vtkPVblockMesh::convertMeshEdges
134 (
135  vtkMultiBlockDataSet* output,
136  int& blockNo
137 )
138 {
139  vtkDataArraySelection* selection = reader_->GetCurvedEdgesSelection();
140  arrayRange& range = arrayRangeEdges_;
141 
142  range.block(blockNo); // Set output block
143  label datasetNo = 0; // Restart at dataset 0
144 
145  const blockMesh& blkMesh = *meshPtr_;
146  const blockEdgeList& edges = blkMesh.edges();
147 
148  int edgeI = 0;
149  const scalar scaleFactor = blkMesh.scaleFactor();
150 
151  for
152  (
153  int partId = range.start();
154  partId < range.end();
155  ++partId, ++edgeI
156  )
157  {
158  if (!edgeStatus_[partId])
159  {
160  continue;
161  }
162 
163  // Search each block
164  forAll(blkMesh, blockI)
165  {
166  const blockDescriptor& blockDef = blkMesh[blockI];
167 
168  edgeList blkEdges = blockDef.blockShape().edges();
169 
170  // List of edge point and weighting factors
171  pointField edgesPoints[12];
172  scalarList edgesWeights[12];
173  blockDef.edgesPointsWeights(edgesPoints, edgesWeights);
174 
175  // Find the corresponding edge within the block
176  label foundEdgeI = -1;
177  forAll(blkEdges, blkEdgeI)
178  {
179  if (edges[edgeI].compare(blkEdges[blkEdgeI]))
180  {
181  foundEdgeI = blkEdgeI;
182  break;
183  }
184  }
185 
186  if (foundEdgeI != -1)
187  {
188  const List<point>& edgePoints = edgesPoints[foundEdgeI];
189 
190  vtkPolyData* vtkmesh = vtkPolyData::New();
191  vtkPoints* vtkpoints = vtkPoints::New();
192 
193  vtkpoints->Allocate( edgePoints.size() );
194  vtkmesh->Allocate(1);
195 
196  vtkIdType pointIds[edgePoints.size()];
197  forAll(edgePoints, ptI)
198  {
200  (
201  vtkpoints,
202  edgePoints[ptI],
203  scaleFactor
204  );
205  pointIds[ptI] = ptI;
206  }
207 
208  vtkmesh->InsertNextCell
209  (
210  VTK_POLY_LINE,
211  edgePoints.size(),
212  pointIds
213  );
214 
215  vtkmesh->SetPoints(vtkpoints);
216  vtkpoints->Delete();
217 
218  AddToBlock
219  (
220  output, vtkmesh, range, datasetNo,
221  selection->GetArrayName(partId)
222  );
223 
224  vtkmesh->Delete();
225  datasetNo++;
226 
227  break;
228  }
229  }
230  }
231 
232 
233  // Anything added?
234  if (datasetNo)
235  {
236  ++blockNo;
237  }
238 }
239 
240 
241 void Foam::vtkPVblockMesh::convertMeshCorners
242 (
243  vtkMultiBlockDataSet* output,
244  int& blockNo
245 )
246 {
247  arrayRange& range = arrayRangeCorners_;
248  range.block(blockNo); // Set output block
249  label datasetNo = 0; // Restart at dataset 0
250 
251  const pointField& blockPoints = meshPtr_->vertices();
252  const scalar& scaleFactor = meshPtr_->scaleFactor();
253 
254  if (debug)
255  {
256  InfoInFunction << endl;
257  }
258 
259  if (true) // Or some flag or other condition
260  {
261  vtkPolyData* vtkmesh = vtkPolyData::New();
262  vtkPoints* vtkpoints = vtkPoints::New();
263  vtkCellArray* vtkcells = vtkCellArray::New();
264 
265  vtkpoints->Allocate( blockPoints.size() );
266  vtkcells->Allocate( blockPoints.size() );
267 
268  vtkIdType pointId = 0;
269  forAll(blockPoints, ptI)
270  {
272  (
273  vtkpoints,
274  blockPoints[ptI],
275  scaleFactor
276  );
277 
278  vtkcells->InsertNextCell(1, &pointId);
279  pointId++;
280  }
281 
282  vtkmesh->SetPoints(vtkpoints);
283  vtkpoints->Delete();
284 
285  vtkmesh->SetVerts(vtkcells);
286  vtkcells->Delete();
287 
288  AddToBlock
289  (
290  output, vtkmesh, range, datasetNo,
291  arrayRangeCorners_.name()
292  );
293  vtkmesh->Delete();
294 
295  datasetNo++;
296  }
297 
298  // Anything added?
299  if (datasetNo)
300  {
301  ++blockNo;
302  }
303 }
304 
305 
306 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
void vtkInsertNextOpenFOAMPoint(vtkPoints *points, const Foam::point &p)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
scalar scaleFactor() const
The scaling factor used to convert to meters.
const pointField & vertices() const
Reference to point field defining the blockMesh.
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:42
List< edge > edgeList
Definition: edgeList.H:38
Pre-declare SubField and related Field type.
Definition: Field.H:56
autoPtr< BasicCompressibleMomentumTransportModel > New(const volScalarField &rho, const volVectorField &U, const surfaceScalarField &phi, const typename BasicCompressibleMomentumTransportModel::transportModel &transport)
List< scalar > scalarList
A List of scalars.
Definition: scalarList.H:50
List< label > labelList
A List of labels.
Definition: labelList.H:56
PtrList< blockEdge > blockEdgeList
A PtrList of blockEdges.
Definition: blockEdgeList.H:45
#define InfoInFunction
Report an information message using Foam::Info.