foamVtkToolsTemplates.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2017-2019 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 InNamespace
27  Foam::vtk::Tools
28 
29 \*---------------------------------------------------------------------------*/
30 
31 // OpenFOAM includes
32 #include "error.H"
33 
34 // VTK includes
35 #include "vtkFloatArray.h"
36 #include "vtkCellData.h"
37 #include "vtkPointData.h"
38 #include "vtkSmartPointer.h"
39 
40 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
41 
42 template<class Face>
45 {
46  label nAlloc = faces.size();
47  for (const auto& f : faces)
48  {
49  nAlloc += f.size();
50  }
51 
52  auto vtkcells = vtkSmartPointer<vtkCellArray>::New();
53 
54  UList<vtkIdType> list = asUList(vtkcells, faces.size(), nAlloc);
55 
56  // Cell connectivity for polygons
57  // [size, verts..., size, verts... ]
58  auto iter = list.begin();
59  for (const auto& f : faces)
60  {
61  *(iter++) = f.size();
62 
63  for (const label verti : f)
64  {
65  *(iter++) = verti;
66  }
67  }
68 
69  return vtkcells;
70 }
71 
72 
73 template<class PatchType>
76 {
77  // Local patch points to vtkPoints
78  return Tools::Points(p.localPoints());
79 }
80 
81 
82 template<class PatchType>
85 {
86  return Tools::Faces(p.localFaces());
87 }
88 
89 
90 template<class PatchType>
93 {
94  auto vtkmesh = vtkSmartPointer<vtkPolyData>::New();
95 
96  vtkmesh->SetPoints(points(p));
97  vtkmesh->SetPolys(faces(p));
98 
99  return vtkmesh;
100 }
101 
102 
103 template<class Face>
106 (
107  const UList<point>& pts,
108  const UList<Face>& fcs
109 )
110 {
111  auto vtkmesh = vtkSmartPointer<vtkPolyData>::New();
112 
113  vtkmesh->SetPoints(Tools::Points(pts));
114  vtkmesh->SetPolys(Tools::Faces(fcs));
115 
116  return vtkmesh;
117 }
118 
119 
120 template<class PatchType>
123 {
125 
126  array->SetNumberOfComponents(3);
127  array->SetNumberOfTuples(p.size());
128 
129  // Unit normals for patch faces.
130  // Cached values if available or loop over faces (avoid triggering cache)
131 
132  vtkIdType faceId = 0;
133 
134  if (p.hasFaceNormals())
135  {
136  for (const vector& n : p.faceNormals())
137  {
138  array->SetTuple(faceId++, n.v_);
139  }
140  }
141  else
142  {
143  for (const auto& f : p)
144  {
145  const vector n(f.unitNormal(p.points()));
146  array->SetTuple(faceId++, n.v_);
147  }
148  }
149 
150  return array;
151 }
152 
153 
154 template<class PatchType>
157 {
158  auto vtkpoints = vtkSmartPointer<vtkPoints>::New();
159 
160  vtkpoints->SetNumberOfPoints(p.size());
161 
162  // Use cached values if available or loop over faces
163  // (avoid triggering cache)
164 
165  vtkIdType pointId = 0;
166 
167  if (p.hasFaceCentres())
168  {
169  for (const point& pt : p.faceCentres())
170  {
171  vtkpoints->SetPoint(pointId++, pt.v_);
172  }
173  }
174  else
175  {
176  for (const auto& f : p)
177  {
178  const point pt(f.centre(p.points()));
179  vtkpoints->SetPoint(pointId++, pt.v_);
180  }
181  }
182 
183  return vtkpoints;
184 }
185 
186 
187 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
188 //
189 // Low-Level conversions
190 //
191 
192 template<class Type>
194 (
195  vtkFloatArray* array,
196  const UList<Type>& input,
197  vtkIdType start
198 )
199 {
200  const int nComp(pTraits<Type>::nComponents);
201 
202  if (array->GetNumberOfComponents() != nComp)
203  {
205  << "vtk array '" << array->GetName()
206  << "' has mismatch in number of components for type '"
208  << "' : target array has " << array->GetNumberOfComponents()
209  << " components instead of " << nComp
210  << nl;
211  }
212 
213  const vtkIdType maxSize = array->GetNumberOfTuples();
214  const vtkIdType endPos = start + vtkIdType(input.size());
215 
216  if (!maxSize)
217  {
218  // no-op
219  return 0;
220  }
221  else if (start < 0 || vtkIdType(start) >= maxSize)
222  {
224  << "vtk array '" << array->GetName()
225  << "' copy with out-of-range [0," << long(maxSize) << ")"
226  << " starting at " << long(start)
227  << nl;
228 
229  return 0;
230  }
231  else if (endPos > maxSize)
232  {
234  << "vtk array '" << array->GetName()
235  << "' copy ends out-of-range (" << long(maxSize) << ")"
236  << " using sizing (start,size) = ("
237  << long(start) << "," << input.size() << ")"
238  << nl;
239 
240  return 0;
241  }
242 
243  float scratch[pTraits<Type>::nComponents];
244 
245  for (const Type& val : input)
246  {
247  foamToVtkTuple(scratch, val);
248  array->SetTuple(start++, scratch);
249  }
250 
251  return input.size();
252 }
253 
254 
255 template<class Type>
258 (
259  const word& name,
260  const label size
261 )
262 {
264 
265  data->SetName(name.c_str());
266  data->SetNumberOfComponents(static_cast<int>(pTraits<Type>::nComponents));
267  data->SetNumberOfTuples(size);
268 
269  // Fill() was not available before VTK-8
270  #if (VTK_MAJOR_VERSION < 8)
271  for (int i = 0; i < data->GetNumberOfComponents(); ++i)
272  {
273  data->FillComponent(i, 0);
274  }
275  #else
276  data->Fill(0);
277  #endif
278 
279  return data;
280 }
281 
282 
283 template<class Type>
286 (
287  const word& name,
288  const UList<Type>& fld
289 )
290 {
292 
293  data->SetName(name.c_str());
294  data->SetNumberOfComponents(static_cast<int>(pTraits<Type>::nComponents));
295  data->SetNumberOfTuples(fld.size());
296 
298 
299  return data;
300 }
301 
302 
303 // ************************************************************************* //
Foam::val
label ListType::const_reference val
Definition: ListOps.H:407
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::vtk::Tools::Faces
vtkSmartPointer< vtkCellArray > Faces(const UList< Face > &faces)
Convert a list of faces (or triFaces) to vtk polygon cells.
Definition: foamVtkToolsTemplates.C:44
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::vtk::Tools::Patch::mesh
static vtkSmartPointer< vtkPolyData > mesh(const PatchType &p)
Convert patch points/faces to vtkPolyData.
Definition: foamVtkToolsTemplates.C:92
Foam::vtk::Tools::Patch::faceNormals
static vtkSmartPointer< vtkFloatArray > faceNormals(const PatchType &p)
Convert patch face normals to vtkFloatArray.
Definition: foamVtkToolsTemplates.C:122
Foam::vtk::Tools::Patch::faceCentres
static vtkSmartPointer< vtkPoints > faceCentres(const PatchType &p)
Return patch face centres as vtkPoints.
Definition: foamVtkToolsTemplates.C:156
Foam::vtk::Tools::zeroField
vtkSmartPointer< vtkFloatArray > zeroField(const word &name, const label size)
Create named field initialized to zero.
Definition: foamVtkToolsTemplates.C:258
Foam::vtk::Tools::asUList
UList< uint8_t > asUList(vtkUnsignedCharArray *array, const label size)
Wrap vtkUnsignedCharArray as a UList.
Definition: foamVtkToolsI.H:31
vtkSmartPointer
Definition: runTimePostProcessing.H:148
Foam::VectorSpace::v_
Cmpt v_[Ncmpts]
The components of this vector space.
Definition: VectorSpace.H:83
Foam::UList::begin
iterator begin()
Return an iterator to begin traversing the UList.
Definition: UListI.H:276
Foam::vtk::Tools::transcribeFloatData
label transcribeFloatData(vtkFloatArray *array, const UList< Type > &input, vtkIdType start=0)
Copy list to pre-allocated vtk array.
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::label
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:62
error.H
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
faceId
label faceId(-1)
fld
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< ' ';}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< ' ';}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< ' ';}gmvFile<< nl;for(const word &name :lagrangianScalarNames){ IOField< scalar > fld(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Definition: gmvOutputLagrangian.H:23
Foam::vtk::Tools::convertFieldToVTK
vtkSmartPointer< vtkFloatArray > convertFieldToVTK(const word &name, const UList< Type > &fld)
Convert field data to a vtkFloatArray.
Definition: foamVtkToolsTemplates.C:286
Foam::New
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions)
Global function forwards to reuseTmpDimensionedField::New.
Definition: DimensionedFieldReuseFunctions.H:105
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
Foam::nl
constexpr char nl
Definition: Ostream.H:372
f
labelList f(nPoints)
Foam::Vector< scalar >
Foam::pTraits
Traits class for primitives.
Definition: pTraits.H:52
Foam::start
label ListType::const_reference const label start
Definition: ListOps.H:408
Foam::UList< Face >
points
const pointField & points
Definition: gmvOutputHeader.H:1
Foam::vtk::Tools::Patch::points
static vtkSmartPointer< vtkPoints > points(const PatchType &p)
Return local patch points as vtkPoints.
Definition: foamVtkToolsTemplates.C:75
Foam::vtk::Tools::Points
vtkSmartPointer< vtkPoints > Points(const UList< point > &pts)
Return a list of points as vtkPoints.
Definition: foamVtkToolsI.H:70
Foam::UList::size
void size(const label n) noexcept
Override size to be inconsistent with allocated storage.
Definition: UListI.H:360
Foam::vtk::Tools::foamToVtkTuple
void foamToVtkTuple(float output[], const Type &val)
Copy/transcribe OpenFOAM data types to VTK format.
Definition: foamVtkToolsI.H:173
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:294
Foam::data
Database for solution data, solver performance and other reduced data.
Definition: data.H:54
Foam::vtk::Tools::Patch::faces
static vtkSmartPointer< vtkCellArray > faces(const PatchType &p)
Convert patch faces to vtk polygon cells.
Definition: foamVtkToolsTemplates.C:84