foamVtkToolsI.H
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-2020 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 \*---------------------------------------------------------------------------*/
27 
28 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
29 
31 (
32  vtkUnsignedCharArray* array,
33  const label size
34 )
35 {
36  array->SetNumberOfComponents(1);
37  array->SetNumberOfTuples(size);
38 
39  return UList<uint8_t>(array->WritePointer(0, size), size);
40 }
41 
42 
44 (
45  vtkIdTypeArray* array,
46  const label size
47 )
48 {
49  array->SetNumberOfComponents(1);
50  array->SetNumberOfTuples(size);
51 
52  return UList<vtkIdType>(array->WritePointer(0, size), size);
53 }
54 
55 
56 inline vtkSmartPointer<vtkPoints>
58 {
59  auto vtkpoints = vtkSmartPointer<vtkPoints>::New();
60 
61  vtkpoints->SetNumberOfPoints(pts.size());
62 
63  vtkIdType pointId = 0;
64  for (const point& pt : pts)
65  {
66  vtkpoints->SetPoint(pointId++, pt.v_);
67  }
68 
69  return vtkpoints;
70 }
71 
72 
73 inline vtkSmartPointer<vtkPoints>
75 {
76  auto vtkpoints = vtkSmartPointer<vtkPoints>::New();
77 
78  vtkpoints->SetNumberOfPoints(addr.size());
79 
80  vtkIdType pointId = 0;
81  for (const label pointi : addr)
82  {
83  vtkpoints->SetPoint(pointId++, pts[pointi].v_);
84  }
85 
86  return vtkpoints;
87 }
88 
89 
90 inline vtkSmartPointer<vtkPolyData>
92 {
93  auto vtkmesh = vtkSmartPointer<vtkPolyData>::New();
94 
95  vtkmesh->SetPoints(Tools::Points(pts));
96  vtkmesh->SetVerts(Tools::identityVertices(pts.size()));
97 
98  return vtkmesh;
99 }
100 
101 
102 inline vtkSmartPointer<vtkPolyData>
104 {
105  auto vtkmesh = vtkSmartPointer<vtkPolyData>::New();
106 
107  vtkmesh->SetPoints(Tools::Points(pts, addr));
108  vtkmesh->SetVerts(Tools::identityVertices(addr.size()));
109 
110  return vtkmesh;
111 }
112 
113 
114 inline Foam::scalarMinMax Foam::vtk::Tools::rangeOf(vtkDataArray* data)
115 {
116  double range[2]{GREAT, -GREAT};
117 
118  if (data)
119  {
120  if (data->GetNumberOfComponents() == 1)
121  {
122  data->GetRange(range);
123  }
124  else
125  {
126  // Mag
127  data->GetRange(range, -1);
128  }
129  }
130 
131  return scalarMinMax(range[0], range[1]);
132 }
133 
134 
135 inline vtkSmartPointer<vtkCellArray> Foam::vtk::Tools::identityVertices
136 (
137  const label size
138 )
139 {
141 
142  #ifdef VTK_CELL_ARRAY_V2
143 
144  // Offsets
145  // [0, n1, n1+n2, n1+n2+n3... ]
146 
147  auto offsets = vtkSmartPointer<vtkIdTypeArray>::New();
148 
149  {
150  const vtkIdType nOffsets(size+1);
151 
152  offsets->SetNumberOfTuples(nOffsets);
153 
154  vtkIdType* iter = offsets->WritePointer(0, nOffsets);
155 
156  std::iota(iter, (iter + nOffsets), vtkIdType(0));
157  }
158 
159 
160  auto connect = vtkSmartPointer<vtkIdTypeArray>::New();
161 
162  // Connectivity
163  {
164  const vtkIdType nConnect(size);
165 
166  connect->SetNumberOfTuples(nConnect);
167 
168  vtkIdType* iter = connect->WritePointer(0, nConnect);
169 
170  std::iota(iter, (iter + nConnect), vtkIdType(0));
171  }
172 
173  // Move into a vtkCellArray
174 
175  cells->SetData(offsets, connect);
176 
177  #else
178 
179  // In VTK-8.2.0 and older,
180  // sizes are interwoven (prefixed) in the connectivity
181 
182  // Connectivity size, with prefixed size information
183 
184  // Cell connectivity for vertex
185  // [size, ids.., size, ids...] -> therefore [1, id, 1, id, ...]
186 
187  const vtkIdType nElem(size);
188  const vtkIdType nConnect(2*size);
189 
190  {
191  cells->GetData()->SetNumberOfTuples(nConnect);
192 
193  vtkIdType* iter = cells->WritePointer(nElem, nConnect);
194 
195  // Fill in the connectivity array, with prefixed size information
196 
197  for (vtkIdType id = 0; id < nElem; ++id)
198  {
199  *(iter++) = 1;
200  *(iter++) = id;
201  }
202  }
203 
204  #endif
206  return cells;
207 };
208 
209 
210 template<class Type>
212 (
213  float output[],
214  const Type& val
215 )
216 {
217  for (direction cmpt=0; cmpt < pTraits<Type>::nComponents; ++cmpt)
218  {
219  output[cmpt] = component(val, cmpt);
220  }
221  remapTuple<Type>(output);
222 }
223 
224 
225 template<class Type>
227 (
228  double output[],
229  const Type& val
230 )
231 {
232  for (direction cmpt=0; cmpt < pTraits<Type>::nComponents; ++cmpt)
233  {
234  output[cmpt] = component(val, cmpt);
235  }
236  remapTuple<Type>(output);
237 }
238 
239 
240 // ************************************************************************* //
vtkSmartPointer< vtkCellArray > identityVertices(const label size)
An identity list of VTK_VERTEX.
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
uint8_t direction
Definition: direction.H:46
MinMax< scalar > scalarMinMax
A scalar min/max range.
Definition: MinMax.H:97
vtkSmartPointer< vtkPoints > Points(const UList< point > &pts)
Return a list of points as vtkPoints.
Definition: foamVtkToolsI.H:50
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tf1, const word &name, const dimensionSet &dimensions, const bool initCopy=false)
Global function forwards to reuseTmpDimensionedField::New.
UList< uint8_t > asUList(vtkUnsignedCharArray *array, const label size)
Wrap vtkUnsignedCharArray as a UList.
Definition: foamVtkToolsI.H:24
::Foam::direction nComponents(const expressions::valueTypeCode) noexcept
The number of components associated with given valueTypeCode.
Definition: exprTraits.C:40
scalar range
vtkSmartPointer< vtkPolyData > Vertices(const UList< point > &pts)
Return vtkPolyData of vertices for each point.
Definition: foamVtkToolsI.H:84
const cellShapeList & cells
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:105
A Vector of values with scalar precision, where scalar is float/double depending on the compilation f...
scalarMinMax rangeOf(vtkDataArray *data)
Min/Max of scalar, or mag() of non-scalars. Includes nullptr check.
void foamToVtkTuple(float output[], const Type &val)
Copy/transcribe OpenFOAM data types to VTK format.
static Ostream & output(Ostream &os, const IntRange< T > &range)
Definition: IntRanges.C:44
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
const pointField & pts