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-2025 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 #include "vtkOpenFOAMPoints.H"
29 
30 // OpenFOAM includes
31 #include "blockMesh.H"
32 #include "Time.H"
33 
34 // VTK includes
35 #include "vtkCellArray.h"
36 #include "vtkDataArraySelection.h"
37 #include "vtkMultiBlockDataSet.h"
38 #include "vtkPoints.h"
39 #include "vtkPolyData.h"
40 #include "vtkUnstructuredGrid.h"
41 
42 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
43 
44 void Foam::vtkPVblockMesh::convertMeshBlocks
45 (
46  vtkMultiBlockDataSet* output,
47  int& blockNo
48 )
49 {
51 
52  if (!meshPtr_.valid()) return;
53 
54  vtkDataArraySelection* selection = reader_->GetBlockSelection();
55  arrayRange& range = arrayRangeBlocks_;
56  range.block(blockNo); // Set output block
57  label datasetNo = 0; // Restart at dataset 0
58 
59  const blockMesh& blkMesh = *meshPtr_;
60  const Foam::pointField& blockPoints = blkMesh.vertices();
61 
62  int blockI = 0;
63  const scalar scaleFactor = blkMesh.scaleFactor();
64 
65  for (int partId = range.start(); partId < range.end(); ++partId, ++blockI)
66  {
67  if (!blockStatus_[partId]) continue;
68 
69  const blockDescriptor& blockDef = blkMesh[blockI];
70 
71  vtkUnstructuredGrid* vtkmesh = vtkUnstructuredGrid::New();
72 
73  // Convert OpenFOAM mesh vertices to VTK
74  vtkPoints *vtkpoints = vtkPoints::New();
75  vtkpoints->Allocate(blockDef.nPoints());
76  const labelList& blockLabels = blockDef.blockShape();
77 
78  vtkmesh->Allocate(1);
79  vtkIdType nodeIds[8];
80 
81  forAll(blockLabels, ptI)
82  {
84  (
85  vtkpoints,
86  blockPoints[blockLabels[ptI]],
87  scaleFactor
88  );
89 
90  nodeIds[ptI] = ptI;
91  }
92 
93  vtkmesh->InsertNextCell
94  (
95  VTK_HEXAHEDRON,
96  8,
97  nodeIds
98  );
99 
100  vtkmesh->SetPoints(vtkpoints);
101  vtkpoints->Delete();
102 
103  AddToBlock
104  (
105  output, vtkmesh, range, datasetNo,
106  selection->GetArrayName(partId)
107  );
108 
109  vtkmesh->Delete();
110  datasetNo++;
111  }
112 
113  // Anything added?
114  if (datasetNo) ++blockNo;
115 }
116 
117 
118 void Foam::vtkPVblockMesh::convertMeshEdges
119 (
120  vtkMultiBlockDataSet* output,
121  int& blockNo
122 )
123 {
125 
126  if (!meshPtr_.valid()) return;
127 
128  vtkDataArraySelection* selection = reader_->GetCurvedEdgesSelection();
129  arrayRange& range = arrayRangeEdges_;
130 
131  range.block(blockNo); // Set output block
132  label datasetNo = 0; // Restart at dataset 0
133 
134  const blockMesh& blkMesh = *meshPtr_;
135  const blockEdgeList& edges = blkMesh.edges();
136 
137  int edgeI = 0;
138  const scalar scaleFactor = blkMesh.scaleFactor();
139 
140  for (int partId = range.start(); partId < range.end(); ++partId, ++edgeI)
141  {
142  if (!edgeStatus_[partId]) continue;
143 
144  // Search each block
145  forAll(blkMesh, blockI)
146  {
147  const blockDescriptor& blockDef = blkMesh[blockI];
148 
149  edgeList blkEdges = blockDef.blockShape().edges();
150 
151  // List of edge point and weighting factors
152  pointField edgesPoints[12];
153  scalarList edgesWeights[12];
154  blockDef.edgesPointsWeights(edgesPoints, edgesWeights);
155 
156  // Find the corresponding edge within the block
157  label foundEdgeI = -1;
158  forAll(blkEdges, blkEdgeI)
159  {
160  if (edges[edgeI].compare(blkEdges[blkEdgeI]))
161  {
162  foundEdgeI = blkEdgeI;
163  break;
164  }
165  }
166 
167  if (foundEdgeI != -1)
168  {
169  const List<point>& edgePoints = edgesPoints[foundEdgeI];
170 
171  vtkPolyData* vtkmesh = vtkPolyData::New();
172  vtkPoints* vtkpoints = vtkPoints::New();
173 
174  vtkpoints->Allocate(edgePoints.size());
175  vtkmesh->Allocate(1);
176 
177  List<vtkIdType> pointIds(edgePoints.size());
178  forAll(edgePoints, ptI)
179  {
181  (
182  vtkpoints,
183  edgePoints[ptI],
184  scaleFactor
185  );
186  pointIds[ptI] = ptI;
187  }
188 
189  vtkmesh->InsertNextCell
190  (
191  VTK_POLY_LINE,
192  edgePoints.size(),
193  pointIds.data()
194  );
195 
196  vtkmesh->SetPoints(vtkpoints);
197  vtkpoints->Delete();
198 
199  AddToBlock
200  (
201  output, vtkmesh, range, datasetNo,
202  selection->GetArrayName(partId)
203  );
204 
205  vtkmesh->Delete();
206  datasetNo++;
207 
208  break;
209  }
210  }
211  }
212 
213  // Anything added?
214  if (datasetNo) ++blockNo;
215 }
216 
217 
218 void Foam::vtkPVblockMesh::convertMeshCorners
219 (
220  vtkMultiBlockDataSet* output,
221  int& blockNo
222 )
223 {
225 
226  if (!meshPtr_.valid()) return;
227 
228  arrayRange& range = arrayRangeCorners_;
229  range.block(blockNo); // Set output block
230  label datasetNo = 0; // Restart at dataset 0
231 
232  const pointField& blockPoints = meshPtr_->vertices();
233  const scalar& scaleFactor = meshPtr_->scaleFactor();
234 
235  vtkPolyData* vtkmesh = vtkPolyData::New();
236  vtkPoints* vtkpoints = vtkPoints::New();
237  vtkCellArray* vtkcells = vtkCellArray::New();
238 
239  vtkpoints->Allocate(blockPoints.size());
240  vtkcells->Allocate(blockPoints.size());
241 
242  vtkIdType pointId = 0;
243  forAll(blockPoints, ptI)
244  {
246  (
247  vtkpoints,
248  blockPoints[ptI],
249  scaleFactor
250  );
251 
252  vtkcells->InsertNextCell(1, &pointId);
253  pointId++;
254  }
255 
256  vtkmesh->SetPoints(vtkpoints);
257  vtkpoints->Delete();
258 
259  vtkmesh->SetVerts(vtkcells);
260  vtkcells->Delete();
261 
262  AddToBlock
263  (
264  output, vtkmesh, range, datasetNo,
265  arrayRangeCorners_.name()
266  );
267  vtkmesh->Delete();
268 
269  datasetNo++;
270 
271  // Anything added?
272  if (datasetNo) ++blockNo;
273 }
274 
275 
276 // ************************************************************************* //
scalar range
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:449
Motion of the mesh specified as a list of pointMeshMovers.
#define DebugInFunction
Report an information message using Foam::Info.
List< label > labelList
A List of labels.
Definition: labelList.H:56
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
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:42
List< scalar > scalarList
A List of scalars.
Definition: scalarList.H:50
PtrList< blockEdge > blockEdgeList
A PtrList of blockEdges.
Definition: blockEdgeList.H:45
List< edge > edgeList
Definition: edgeList.H:38
tmp< DimensionedField< TypeR, GeoMesh, Field > > New(const tmp< DimensionedField< TypeR, GeoMesh, Field >> &tdf1, const word &name, const dimensionSet &dimensions)
void vtkInsertNextOpenFOAMPoint(vtkPoints *points, const Foam::point &p)