VTKio 0.1.0.dev2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- vtkio/__init__.py +27 -0
- vtkio/_git.py +45 -0
- vtkio/helpers.py +110 -0
- vtkio/reader/__init__.py +15 -0
- vtkio/reader/hdf5.py +379 -0
- vtkio/reader/xml.py +712 -0
- vtkio/simplified.py +621 -0
- vtkio/utilities.py +222 -0
- vtkio/version.py +78 -0
- vtkio/vtk_cell_types.py +98 -0
- vtkio/vtk_structures.py +306 -0
- vtkio/writer/__init__.py +16 -0
- vtkio/writer/pvd_writer.py +132 -0
- vtkio/writer/vtkhdf.py +1184 -0
- vtkio/writer/writers.py +393 -0
- vtkio/writer/xml_writer.py +1597 -0
- vtkio-0.1.0.dev2.dist-info/METADATA +86 -0
- vtkio-0.1.0.dev2.dist-info/RECORD +20 -0
- vtkio-0.1.0.dev2.dist-info/WHEEL +4 -0
- vtkio-0.1.0.dev2.dist-info/licenses/LICENSE +28 -0
vtkio/simplified.py
ADDED
|
@@ -0,0 +1,621 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
"""
|
|
3
|
+
VTK XML Writer Class for creating VTK's XML based format.
|
|
4
|
+
|
|
5
|
+
Supports ASCII, Base64 and Appended Raw encoding of data.
|
|
6
|
+
|
|
7
|
+
Created at 13:01, 24 Feb, 2022
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
__author__ = 'J.P. Morrissey'
|
|
11
|
+
__copyright__ = 'Copyright 2022-2025'
|
|
12
|
+
__maintainer__ = 'J.P. Morrissey'
|
|
13
|
+
__email__ = 'morrissey.jp@gmail.com'
|
|
14
|
+
__status__ = 'Development'
|
|
15
|
+
|
|
16
|
+
__all__ = ['uniform_grid', 'rectilinear_grid', 'regular_grid_from_extents', 'regular_grid_from_extents',
|
|
17
|
+
'regular_grid_from_coordinates', 'structured_grid', 'unstructured_points', 'unstructured_point_cells',
|
|
18
|
+
'unstructured_grid_to_vtk', 'lines_to_poly', 'points_to_poly', 'polylines_to_poly']
|
|
19
|
+
|
|
20
|
+
# Standard Library
|
|
21
|
+
import numpy as np
|
|
22
|
+
|
|
23
|
+
# Local imports
|
|
24
|
+
from .vtk_cell_types import VTK_Line, VTK_PolyLine, VTK_Vertex
|
|
25
|
+
from .writer.writers import write_vti, write_vtp, write_vtr, write_vts, write_vtu
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def uniform_grid(filepath, num_cells, origin=(0, 0, 0), spacing=(1, 1, 1), cell_data=None, point_data=None,
|
|
29
|
+
field_data=None, encoding='ascii'):
|
|
30
|
+
"""
|
|
31
|
+
Create a uniform grid from a fixed number of cells and spacing.
|
|
32
|
+
|
|
33
|
+
Results are written in to a VTK file in an `xml` format.
|
|
34
|
+
|
|
35
|
+
Parameters
|
|
36
|
+
----------
|
|
37
|
+
filepath : str
|
|
38
|
+
Full path (or path relative to working directory) of the file to be written. Do not include the file extension.
|
|
39
|
+
num_cells : array-like
|
|
40
|
+
List, Tuple or Numpy array of the number of cells (nx, ny, nz) in each of the three grid directions.
|
|
41
|
+
origin : array-like (default = (0,0,0))
|
|
42
|
+
List, Tuple or Numpy array of the origin of the grid.
|
|
43
|
+
spacing : array-like (default = (1,1,1))
|
|
44
|
+
List, Tuple or Numpy array of the spacing (cell dimension) of the grid in each of the three grid directions.
|
|
45
|
+
cell_data : dictionary
|
|
46
|
+
Cell data to be written to VTK file. The dictionary should have the following key-value pairs: variable name: data.
|
|
47
|
+
Data arrays should all be the same length and should have the same number of values as the number of cells in the grid.
|
|
48
|
+
Data can contain `Scalars`, `Vectors`, `Tensors`, `Normals` and `Texture Coordinates` as defined in the VTK specification.
|
|
49
|
+
The number of points in the grid is the product of (nx, ny, nz).
|
|
50
|
+
point_data : dictionary
|
|
51
|
+
Point data to be written to VTK file. The dictionary should have the following key-value pairs: variable name: data.
|
|
52
|
+
Data arrays should all be the same length and should have the same number of values as points in the grid.
|
|
53
|
+
Data can contain `Scalars`, `Vectors`, `Tensors`, `Normals` and `Texture Coordinates` as defined in the VTK specification.
|
|
54
|
+
The number of points in the grid is the product of (nx+1, ny+1, nz+1).
|
|
55
|
+
field_data : dictionary
|
|
56
|
+
|
|
57
|
+
encoding : str (default = 'ascii')
|
|
58
|
+
Encoding methods supported by the VTK XML writer. This can be any of 'ascii', 'binary' or 'appended'.
|
|
59
|
+
In order to adhere to the xml specification both the binary and appended data is written as base64 encoded.
|
|
60
|
+
|
|
61
|
+
Returns
|
|
62
|
+
-------
|
|
63
|
+
None
|
|
64
|
+
|
|
65
|
+
"""
|
|
66
|
+
num_cells = np.asarray(num_cells).astype(int)
|
|
67
|
+
num_points = np.asarray(num_cells) + 1
|
|
68
|
+
max_extent = np.asarray(num_cells)
|
|
69
|
+
whole_extents = np.array([[0, 0, 0], max_extent]).T.flatten()
|
|
70
|
+
|
|
71
|
+
# assumes single piece, so piece extents is equal to whole extents
|
|
72
|
+
write_vti(filepath, whole_extents, whole_extents, spacing=spacing, origin=origin, point_data=point_data,
|
|
73
|
+
cell_data=cell_data, field_data=field_data, encoding=encoding)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def unstructured_points(filepath, positions, pointData=None, fieldData=None, encoding='ascii'):
|
|
77
|
+
"""
|
|
78
|
+
Writes unstructured points data to a VTU file. This function facilitates saving points with
|
|
79
|
+
metadata in the Visualization Toolkit unstructured grid file format.
|
|
80
|
+
|
|
81
|
+
Parameters
|
|
82
|
+
----------
|
|
83
|
+
filepath : str
|
|
84
|
+
The output file path where the VTU file will be written.
|
|
85
|
+
positions : numpy.ndarray
|
|
86
|
+
Array of shape (n, 3) representing the 3D coordinates of the points.
|
|
87
|
+
pointData : dict, optional
|
|
88
|
+
Dictionary of point-wise data attributes, where each key represents a data attribute
|
|
89
|
+
name and the value is an array containing its values. Default is None.
|
|
90
|
+
fieldData : dict, optional
|
|
91
|
+
Dictionary of field data attributes, where each key represents global metadata and the
|
|
92
|
+
value is its corresponding value. Default is None.
|
|
93
|
+
encoding : {'ascii', 'binary'}, optional
|
|
94
|
+
Format in which the VTU file is written. 'ascii' for human-readable, 'binary' for
|
|
95
|
+
compact data. Default is 'ascii'.
|
|
96
|
+
"""
|
|
97
|
+
num_points = positions.shape[0]
|
|
98
|
+
|
|
99
|
+
write_vtu(filepath, nodes=positions, cell_type=None, connectivity=None, offsets=None,
|
|
100
|
+
point_data=pointData, cell_data=None, field_data=fieldData, encoding=encoding)
|
|
101
|
+
|
|
102
|
+
def unstructured_point_cells(filepath, positions, pointData=None, fieldData=None, encoding='ascii'):
|
|
103
|
+
"""
|
|
104
|
+
Creates and writes unstructured point cell data to a .vtu file.
|
|
105
|
+
|
|
106
|
+
This function generates unstructured point cells for given point positions and writes
|
|
107
|
+
them to a .vtu file. In this process, each point is treated as an individual cell
|
|
108
|
+
connected only to itself, creating a simple topology for unstructured grid data. The
|
|
109
|
+
data is then saved in the specified file employing the appropriate encoding scheme.
|
|
110
|
+
|
|
111
|
+
Parameters
|
|
112
|
+
----------
|
|
113
|
+
filepath : str
|
|
114
|
+
Path to the output .vtu file where the unstructured grid data should be written.
|
|
115
|
+
positions : numpy.ndarray
|
|
116
|
+
Array of shape (n, 3) representing the positions of the points in 3D space.
|
|
117
|
+
pointData : dict of str, numpy.ndarray, optional
|
|
118
|
+
Dictionary where keys are the names of point data arrays, and values
|
|
119
|
+
are numpy arrays containing the corresponding data. Defaults to None.
|
|
120
|
+
fieldData : dict of str, numpy.ndarray, optional
|
|
121
|
+
Dictionary where keys are the names of field data arrays, and values
|
|
122
|
+
are numpy arrays containing the corresponding data. Field data associates
|
|
123
|
+
metadata or attributes with the entire dataset. Defaults to None.
|
|
124
|
+
encoding : str, optional
|
|
125
|
+
Encoding format for the .vtu file. Common options are 'ascii' and 'binary'.
|
|
126
|
+
Defaults to 'ascii'.
|
|
127
|
+
"""
|
|
128
|
+
num_points = positions.shape[0]
|
|
129
|
+
|
|
130
|
+
# create some arrays to set grid topology for unstructured point data
|
|
131
|
+
# each point is only connected to itself (starts at zero)
|
|
132
|
+
connectivity = np.arange(num_points, dtype="int32")
|
|
133
|
+
# index of last node in each cell
|
|
134
|
+
offsets = np.arange(start=1, stop=num_points + 1, dtype="int32")
|
|
135
|
+
|
|
136
|
+
cell_types = np.empty(num_points, dtype="uint8")
|
|
137
|
+
cell_types[:] = VTK_Vertex
|
|
138
|
+
|
|
139
|
+
write_vtu(filepath, nodes=positions, cell_type=cell_types, connectivity=connectivity, offsets=offsets,
|
|
140
|
+
point_data=pointData, cell_data=None, field_data=fieldData, encoding=encoding)
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def rectilinear_grid(filepath, x, y, z, cell_data=None, point_data=None, whole_extents=None, piece_extents=None,
|
|
144
|
+
field_data=None, encoding='appended'):
|
|
145
|
+
"""
|
|
146
|
+
A rectilinear grid consisting of cells of the same type.
|
|
147
|
+
|
|
148
|
+
This helper functions allows the creation of a rectilinear grid from the definition of the minimum and maximum
|
|
149
|
+
coordinates in each of the three principle directions combined with the number of cells in each direction.
|
|
150
|
+
|
|
151
|
+
Parameters
|
|
152
|
+
----------
|
|
153
|
+
filepath : str
|
|
154
|
+
Full path (or path relative to working directory) of the file to be written. Do not include the file extension.
|
|
155
|
+
x : array-like
|
|
156
|
+
List, Tuple or Numpy array of the grid coordinates along the x-axis. This should be a set increasing values that
|
|
157
|
+
can be unequally spaced.
|
|
158
|
+
y : array-like
|
|
159
|
+
List, Tuple or Numpy array of the grid coordinates along the y-axis. This should be a set increasing values that
|
|
160
|
+
can be unequally spaced.
|
|
161
|
+
z : array-like
|
|
162
|
+
List, Tuple or Numpy array of the grid coordinates along the z-axis. This should be a set increasing values that
|
|
163
|
+
can be unequally spaced.
|
|
164
|
+
cell_data : dictionary
|
|
165
|
+
Cell data to be written to VTK file. The dictionary should have the following key-value pairs: variable name: data.
|
|
166
|
+
Data arrays should all be the same length and should have the same number of values as the number of cells in the grid.
|
|
167
|
+
Data can contain `Scalars`, `Vectors`, `Tensors`, `Normals` and `Texture Coordinates` as defined in the VTK specification.
|
|
168
|
+
The number of points in the grid is the product of (nx, ny, nz).
|
|
169
|
+
point_data : dictionary
|
|
170
|
+
Point data to be written to VTK file. The dictionary should have the following key-value pairs: variable name: data.
|
|
171
|
+
Data arrays should all be the same length and should have the same number of values as points in the grid.
|
|
172
|
+
Data can contain `Scalars`, `Vectors`, `Tensors`, `Normals` and `Texture Coordinates` as defined in the VTK specification.
|
|
173
|
+
The number of points in the grid is the product of (nx+1, ny+1, nz+1).
|
|
174
|
+
field_data : dictionary
|
|
175
|
+
Additional data to be written to the VTK files.
|
|
176
|
+
encoding : str (default = 'ascii')
|
|
177
|
+
Encoding methods supported by the VTK XML writer. This can be any of 'ascii', 'binary' or 'appended'.
|
|
178
|
+
In order to adhere to the xml specification both the binary and appended data is written as base64 encoded.
|
|
179
|
+
|
|
180
|
+
Returns
|
|
181
|
+
-------
|
|
182
|
+
None
|
|
183
|
+
|
|
184
|
+
"""
|
|
185
|
+
if whole_extents is None:
|
|
186
|
+
whole_extents = np.array([np.zeros(3),
|
|
187
|
+
np.array([len(x), len(y), len(z)])-1
|
|
188
|
+
]).T.flatten()
|
|
189
|
+
|
|
190
|
+
if piece_extents is None:
|
|
191
|
+
piece_extents = whole_extents
|
|
192
|
+
|
|
193
|
+
write_vtr(filepath, x, y, z, whole_extent=whole_extents, piece_extent=piece_extents,
|
|
194
|
+
point_data=point_data, cell_data=cell_data, field_data=field_data, encoding=encoding)
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
def regular_grid_from_extents(filepath, min_extents, max_extents, num_cells, cell_data=None, point_data=None,
|
|
198
|
+
piece_extents=None, field_data=None, encoding='appended'):
|
|
199
|
+
"""
|
|
200
|
+
A uniform rectilinear grid consisting of cells of the same type.
|
|
201
|
+
|
|
202
|
+
This helper functions allows the creation of a regular grid from the definition of the minimum and maximum
|
|
203
|
+
coordinates in each of the three principle directions combined with the number of cells in each direction.
|
|
204
|
+
|
|
205
|
+
Parameters
|
|
206
|
+
----------
|
|
207
|
+
filepath : str
|
|
208
|
+
Full path (or path relative to working directory) of the file to be written. Do not include the file extension.
|
|
209
|
+
num_cells : array-like
|
|
210
|
+
List, Tuple or Numpy array of the number of cells (nx, ny, nz) in each of the three grid directions.
|
|
211
|
+
min_extents : array-like (default = (0,0,0))
|
|
212
|
+
List, Tuple or Numpy array of the origin of the grid.
|
|
213
|
+
max_extents : array-like (default = (1,1,1))
|
|
214
|
+
List, Tuple or Numpy array of maximum extents of the grid in each of the three grid directions.
|
|
215
|
+
cell_data : dictionary
|
|
216
|
+
Cell data to be written to VTK file. The dictionary should have the following key-value pairs: variable name: data.
|
|
217
|
+
Data arrays should all be the same length and should have the same number of values as the number of cells in the grid.
|
|
218
|
+
Data can contain `Scalars`, `Vectors`, `Tensors`, `Normals` and `Texture Coordinates` as defined in the VTK specification.
|
|
219
|
+
The number of points in the grid is the product of (nx, ny, nz).
|
|
220
|
+
point_data : dictionary
|
|
221
|
+
Point data to be written to VTK file. The dictionary should have the following key-value pairs: variable name: data.
|
|
222
|
+
Data arrays should all be the same length and should have the same number of values as points in the grid.
|
|
223
|
+
Data can contain `Scalars`, `Vectors`, `Tensors`, `Normals` and `Texture Coordinates` as defined in the VTK specification.
|
|
224
|
+
The number of points in the grid is the product of (nx+1, ny+1, nz+1).
|
|
225
|
+
field_data : dictionary
|
|
226
|
+
Additional data to be written to the VTK files.
|
|
227
|
+
encoding : str (default = 'ascii')
|
|
228
|
+
Encoding methods supported by the VTK XML writer. This can be any of 'ascii', 'binary' or 'appended'.
|
|
229
|
+
In order to adhere to the xml specification both the binary and appended data is written as base64 encoded.
|
|
230
|
+
|
|
231
|
+
Returns
|
|
232
|
+
-------
|
|
233
|
+
None
|
|
234
|
+
|
|
235
|
+
"""
|
|
236
|
+
num_cells = np.asarray(num_cells).astype(int)
|
|
237
|
+
|
|
238
|
+
whole_extents = np.array([min_extents, max_extents]).T.flatten()
|
|
239
|
+
|
|
240
|
+
if piece_extents is None:
|
|
241
|
+
piece_extents = whole_extents
|
|
242
|
+
|
|
243
|
+
# Dimensions
|
|
244
|
+
ncells = np.prod(num_cells)
|
|
245
|
+
npoints = np.prod(num_cells + 1)
|
|
246
|
+
|
|
247
|
+
# do some error checking on array data being written - does it match the number of cells and points and are they
|
|
248
|
+
# all equal in length
|
|
249
|
+
|
|
250
|
+
# Coordinates
|
|
251
|
+
x = np.linspace(min_extents[0], max_extents[0], num_cells[0] + 1, dtype='float64')
|
|
252
|
+
y = np.linspace(min_extents[1], max_extents[1], num_cells[1] + 1, dtype='float64')
|
|
253
|
+
z = np.linspace(min_extents[2], max_extents[2], num_cells[2] + 1, dtype='float64')
|
|
254
|
+
|
|
255
|
+
write_vtr(filepath, x, y, z, whole_extent=whole_extents, piece_extent=piece_extents,
|
|
256
|
+
point_data=point_data, cell_data=cell_data, field_data=field_data, encoding=encoding)
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
def regular_grid_from_coordinates(filepath, coordinates, cell_data=None, point_data=None,
|
|
260
|
+
piece_extents=None, field_data=None, encoding='appended'):
|
|
261
|
+
"""
|
|
262
|
+
Generate a regular grid from given coordinates and write it to a VTR (VTK Rectilinear grid) file.
|
|
263
|
+
|
|
264
|
+
This function creates a rectilinear grid from a set of 3D coordinates and writes the resulting
|
|
265
|
+
grid along with associated data (if provided) to a VTR file. The function ensures the coordinates
|
|
266
|
+
are properly sorted to maintain grid consistency, and it organizes the dataset according to the
|
|
267
|
+
desired extents and encoding format.
|
|
268
|
+
|
|
269
|
+
Parameters
|
|
270
|
+
----------
|
|
271
|
+
filepath : str
|
|
272
|
+
Path to the output VTR file.
|
|
273
|
+
coordinates : numpy.ndarray
|
|
274
|
+
A 2D array of shape (npoints, 3), where `npoints` is the number of points. Each row represents
|
|
275
|
+
the (x, y, z) coordinate of a point.
|
|
276
|
+
cell_data : dict, optional
|
|
277
|
+
A dictionary containing the data associated with cells. Keys are data categories, and values
|
|
278
|
+
are dictionaries where the keys are names of cell data fields and values are NumPy arrays of
|
|
279
|
+
cell-related data.
|
|
280
|
+
point_data : dict, optional
|
|
281
|
+
A dictionary containing the data associated with points. Keys are data categories, and values
|
|
282
|
+
are dictionaries where the keys are names of point data fields and values are NumPy arrays of
|
|
283
|
+
point-related data. Each array within a category must have the same length as `coordinates`.
|
|
284
|
+
piece_extents : numpy.ndarray, optional
|
|
285
|
+
An array defining the extents of the grid piece to be written. It should be in the order
|
|
286
|
+
[xmin, xmax, ymin, ymax, zmin, zmax]. If not provided, it defaults to the extents of the
|
|
287
|
+
entire dataset.
|
|
288
|
+
field_data : dict, optional
|
|
289
|
+
A dictionary containing global field data for the VTR file. Keys are names of fields, and values
|
|
290
|
+
are NumPy arrays representing the values of these fields.
|
|
291
|
+
encoding : str, optional
|
|
292
|
+
A string specifying the type of encoding for the VTR file. Supported values are 'ascii',
|
|
293
|
+
'binary', or 'appended'. It defaults to 'appended'.
|
|
294
|
+
|
|
295
|
+
Raises
|
|
296
|
+
------
|
|
297
|
+
ValueError
|
|
298
|
+
If any point data arrays have a size that is inconsistent with the number of points.
|
|
299
|
+
|
|
300
|
+
Notes
|
|
301
|
+
-----
|
|
302
|
+
The function uses `np.unique` and `np.lexsort` to process and organize coordinate data before
|
|
303
|
+
generating the regular grid. All associated data (point data) is also reorganized to match the
|
|
304
|
+
sorted order of the coordinates. The `writeVTR` function is responsible for writing the processed
|
|
305
|
+
grid and associated data to the specified VTR file.
|
|
306
|
+
|
|
307
|
+
"""
|
|
308
|
+
npoints = len(coordinates)
|
|
309
|
+
|
|
310
|
+
x = np.unique(coordinates[:, 0])
|
|
311
|
+
y = np.unique(coordinates[:, 1])
|
|
312
|
+
z = np.unique(coordinates[:, 2])
|
|
313
|
+
|
|
314
|
+
nx = len(x)
|
|
315
|
+
ny = len(y)
|
|
316
|
+
nz = len(z)
|
|
317
|
+
|
|
318
|
+
sort_index = np.lexsort((coordinates[:, 0], coordinates[:, 1], coordinates[:, 2]))
|
|
319
|
+
sort_index_F = np.lexsort((coordinates[:, 2], coordinates[:, 1], coordinates[:, 0]))
|
|
320
|
+
sorted_coords = coordinates[sort_index]
|
|
321
|
+
sorted_coords_2 = coordinates[sort_index_F]
|
|
322
|
+
|
|
323
|
+
# need to sort any point data in the same order
|
|
324
|
+
if point_data:
|
|
325
|
+
for key, arrays in point_data.items():
|
|
326
|
+
for name, data in arrays.items():
|
|
327
|
+
if data.shape[0] != npoints:
|
|
328
|
+
raise ValueError('PointData is expected to have the same size as number of points.')
|
|
329
|
+
else:
|
|
330
|
+
data = data[sort_index]
|
|
331
|
+
|
|
332
|
+
whole_extents = np.array([np.min(coordinates, axis=0), np.max(coordinates, axis=0)]).T.flatten()
|
|
333
|
+
|
|
334
|
+
if piece_extents is None:
|
|
335
|
+
piece_extents = whole_extents
|
|
336
|
+
|
|
337
|
+
# Dimensions
|
|
338
|
+
ncells = np.prod([nx - 1, ny - 1, nz - 1])
|
|
339
|
+
|
|
340
|
+
write_vtr(filepath, x, y, z, whole_extent=whole_extents, piece_extent=piece_extents,
|
|
341
|
+
point_data=point_data, cell_data=cell_data, field_data=field_data, encoding=encoding)
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
def vtk_structured_grid_flat_index(i, j, k, nx, ny):
|
|
345
|
+
"""
|
|
346
|
+
VTK structured grid flat index calculator.
|
|
347
|
+
|
|
348
|
+
Parameters
|
|
349
|
+
----------
|
|
350
|
+
i : int
|
|
351
|
+
Grid x-coordinate
|
|
352
|
+
j : int
|
|
353
|
+
Grid y-coordinate
|
|
354
|
+
k : int
|
|
355
|
+
Grid z-coordinate
|
|
356
|
+
nx : int
|
|
357
|
+
Number of point in the x-direction of the grid.
|
|
358
|
+
ny : int
|
|
359
|
+
Number of point in the y-direction of the grid.
|
|
360
|
+
|
|
361
|
+
Returns
|
|
362
|
+
-------
|
|
363
|
+
index : int
|
|
364
|
+
VTK flattened index
|
|
365
|
+
|
|
366
|
+
"""
|
|
367
|
+
return (k * (nx * ny)) + (j * nx) + i
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
def structured_grid(filepath, points, cell_data=None, point_data=None,
|
|
371
|
+
num_cells=None, field_data=None, encoding='appended'):
|
|
372
|
+
"""
|
|
373
|
+
A uniform rectilinear grid consisting of cells of the same type.
|
|
374
|
+
|
|
375
|
+
This helper functions allows the creation of a regular grid from the definition of the minimum and maximum
|
|
376
|
+
coordinates in each of the three principle directions combined with the number of cells in each direction.
|
|
377
|
+
|
|
378
|
+
Parameters
|
|
379
|
+
----------
|
|
380
|
+
filepath : str
|
|
381
|
+
Full path (or path relative to working directory) of the file to be written. Do not include the file extension.
|
|
382
|
+
num_cells : array-like
|
|
383
|
+
List, Tuple or Numpy array of the number of cells (nx, ny, nz) in each of the three grid directions.
|
|
384
|
+
cell_data : dictionary
|
|
385
|
+
Cell data to be written to VTK file. The dictionary should have the following key-value pairs: variable name: data.
|
|
386
|
+
Data arrays should all be the same length and should have the same number of values as the number of cells in the grid.
|
|
387
|
+
Data can contain `Scalars`, `Vectors`, `Tensors`, `Normals` and `Texture Coordinates` as defined in the VTK specification.
|
|
388
|
+
The number of points in the grid is the product of (nx, ny, nz).
|
|
389
|
+
point_data : dictionary
|
|
390
|
+
Point data to be written to VTK file. The dictionary should have the following key-value pairs: variable name: data.
|
|
391
|
+
Data arrays should all be the same length and should have the same number of values as points in the grid.
|
|
392
|
+
Data can contain `Scalars`, `Vectors`, `Tensors`, `Normals` and `Texture Coordinates` as defined in the VTK specification.
|
|
393
|
+
The number of points in the grid is the product of (nx+1, ny+1, nz+1).
|
|
394
|
+
field_data : dictionary
|
|
395
|
+
Additional data to be written to the VTK files.
|
|
396
|
+
encoding : str (default = 'ascii')
|
|
397
|
+
Encoding methods supported by the VTK XML writer. This can be any of 'ascii', 'binary' or 'appended'.
|
|
398
|
+
In order to adhere to the xml specification both the binary and appended data is written as base64 encoded.
|
|
399
|
+
|
|
400
|
+
Returns
|
|
401
|
+
-------
|
|
402
|
+
None
|
|
403
|
+
|
|
404
|
+
"""
|
|
405
|
+
num_cells = num_cells.astype(int)
|
|
406
|
+
|
|
407
|
+
whole_extents = np.array([[0, 0, 0], num_cells]).T.flatten()
|
|
408
|
+
piece_extents = whole_extents
|
|
409
|
+
|
|
410
|
+
# Dimensions
|
|
411
|
+
ncells = np.prod(num_cells)
|
|
412
|
+
npoints = len(points)
|
|
413
|
+
|
|
414
|
+
# do some error checking on array data being written - does it match the number of cells and points and are they
|
|
415
|
+
# all equal in length
|
|
416
|
+
|
|
417
|
+
write_vts(filepath, points=points, whole_extent=whole_extents, piece_extent=piece_extents,
|
|
418
|
+
point_data=point_data, cell_data=cell_data, field_data=field_data, encoding=encoding)
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
def unstructured_grid_to_vtk(filepath, positions, connectivity, offsets, cell_types, cellData=None, pointData=None,
|
|
422
|
+
fieldData=None, encoding='appended'):
|
|
423
|
+
"""
|
|
424
|
+
Converts data describing an unstructured grid into a VTK file format.
|
|
425
|
+
|
|
426
|
+
The method enables exporting geometric and attribute data for unstructured grids to VTK
|
|
427
|
+
files, which can then be visualized with software supporting the VTK format.
|
|
428
|
+
This function supports cell, point, and field data, with flexibility in encoding
|
|
429
|
+
type for exported files.
|
|
430
|
+
|
|
431
|
+
Parameters
|
|
432
|
+
----------
|
|
433
|
+
filepath : str
|
|
434
|
+
The path where the resulting VTK file will be saved.
|
|
435
|
+
positions : numpy.ndarray
|
|
436
|
+
An array of shape (n, 3) containing the coordinates of the points in
|
|
437
|
+
3D space.
|
|
438
|
+
connectivity : numpy.ndarray
|
|
439
|
+
A 1D array describing the connectivity of the points to form cells
|
|
440
|
+
for the unstructured grid.
|
|
441
|
+
offsets : numpy.ndarray
|
|
442
|
+
A 1D array specifying the starting position of each cell in the
|
|
443
|
+
connectivity array.
|
|
444
|
+
cell_types : numpy.ndarray
|
|
445
|
+
A 1D array containing the integer identifiers defining the type of
|
|
446
|
+
each cell (e.g., VTK tetrahedron, VTK hexahedron, etc.).
|
|
447
|
+
cellData : dict, optional
|
|
448
|
+
Dictionary containing cell-specific attribute data. Keys represent
|
|
449
|
+
attribute names, and values are numpy arrays for the corresponding
|
|
450
|
+
attribute data.
|
|
451
|
+
pointData : dict, optional
|
|
452
|
+
Dictionary containing point-specific attribute data. Keys represent
|
|
453
|
+
attribute names, and values are numpy arrays for the corresponding
|
|
454
|
+
attribute data.
|
|
455
|
+
fieldData : dict, optional
|
|
456
|
+
Dictionary containing global field data. Keys represent field names,
|
|
457
|
+
and values are numpy arrays for the corresponding data.
|
|
458
|
+
encoding : str, optional
|
|
459
|
+
Specifies the encoding type for the file. Options include 'appended',
|
|
460
|
+
'ascii', or 'binary'. Default is 'appended'.
|
|
461
|
+
"""
|
|
462
|
+
num_points = positions.shape[0]
|
|
463
|
+
num_cells = cell_types.size
|
|
464
|
+
assert offsets.size == num_cells
|
|
465
|
+
|
|
466
|
+
write_vtu(filepath, nodes=None, cell_type=None, connectivity=None, offsets=None, pointData=pointData,
|
|
467
|
+
cellData=cellData, fieldData=fieldData, encoding=encoding)
|
|
468
|
+
|
|
469
|
+
|
|
470
|
+
def lines_to_poly(filepath, positions, connectivity=None, point_data=None, cell_data=None, fieldData=None,
|
|
471
|
+
encoding='appended', comments=None):
|
|
472
|
+
"""
|
|
473
|
+
Converts a series of line segments or points into a VTK PolyData file format and writes it to disk.
|
|
474
|
+
|
|
475
|
+
This function takes in point coordinates, connectivity information, and optional data arrays associated with the
|
|
476
|
+
points and cells to generate a VTK PolyData file. It supports a variety of input formats and encodings for
|
|
477
|
+
flexibility and compatibility with visualization tools. The generated file can be used to represent both
|
|
478
|
+
line-based topology and other data fields.
|
|
479
|
+
|
|
480
|
+
Parameters
|
|
481
|
+
----------
|
|
482
|
+
filepath : str
|
|
483
|
+
Path to the output .vtp file.
|
|
484
|
+
positions : numpy.ndarray
|
|
485
|
+
Array of point coordinates with shape (n, 3), where n is the number of points.
|
|
486
|
+
connectivity : numpy.ndarray or None, optional
|
|
487
|
+
Array of connectivity representing line segments. If None, each point will be treated as disconnected.
|
|
488
|
+
point_data : dict or None, optional
|
|
489
|
+
Dictionary containing data arrays associated with points. Keys represent names of the arrays, and values
|
|
490
|
+
are numpy arrays of the same length as the number of points.
|
|
491
|
+
cell_data : dict or None, optional
|
|
492
|
+
Dictionary containing data arrays associated with cells. Keys represent names of the arrays, and values
|
|
493
|
+
are numpy arrays of the same length as the number of cells.
|
|
494
|
+
fieldData : dict or None, optional
|
|
495
|
+
Dictionary containing general dataset attributes. Keys represent names of attributes, and values
|
|
496
|
+
are numpy arrays or other relevant data types.
|
|
497
|
+
encoding : str, optional
|
|
498
|
+
Type of encoding to be used for the .vtp file. Supported values are 'ascii', 'binary', and 'appended'.
|
|
499
|
+
Defaults to 'appended'.
|
|
500
|
+
comments : str or None, optional
|
|
501
|
+
Optional comments to include in the .vtp file.
|
|
502
|
+
|
|
503
|
+
Notes
|
|
504
|
+
-----
|
|
505
|
+
The function determines the number of lines and segments from the input `connectivity` array. If no
|
|
506
|
+
connectivity is provided, each point is treated as an isolated entity. Different cell types
|
|
507
|
+
(e.g., `VTK_PolyLine`, `VTK_Line`) are used for the topological representation of lines.
|
|
508
|
+
|
|
509
|
+
This function uses an external helper `writeVTP` to perform the actual writing of the .vtp file.
|
|
510
|
+
|
|
511
|
+
"""
|
|
512
|
+
num_points = positions.shape[0]
|
|
513
|
+
|
|
514
|
+
# create some temporary arrays to write grid topology
|
|
515
|
+
if connectivity is None:
|
|
516
|
+
connectivity = np.arange(num_points, dtype="int32") # each point is only connected to itself
|
|
517
|
+
|
|
518
|
+
if connectivity.ndim == 2:
|
|
519
|
+
connectivity = connectivity.flatten()
|
|
520
|
+
|
|
521
|
+
num_lines = connectivity.size // 2
|
|
522
|
+
offsets = (np.arange(num_lines) + 1) * 2
|
|
523
|
+
|
|
524
|
+
cell_types = np.empty(num_points, dtype="uint8")
|
|
525
|
+
cell_types[:] = VTK_PolyLine.type_id
|
|
526
|
+
cell_types[:] = VTK_Line.type_id
|
|
527
|
+
|
|
528
|
+
write_vtp(filepath, points=positions, lines=(connectivity, offsets),
|
|
529
|
+
point_data=point_data, cell_data=cell_data, field_data=fieldData, encoding=encoding)
|
|
530
|
+
|
|
531
|
+
|
|
532
|
+
def polylines_to_poly(filepath, positions, points_per_line, point_data=None, cell_data=None, fieldData=None,
|
|
533
|
+
encoding='appended', comments=None):
|
|
534
|
+
"""
|
|
535
|
+
Convert polylines to a VTK PolyData file and write it to the specified path.
|
|
536
|
+
|
|
537
|
+
This function generates VTK PolyData from given input positions and generates line connectivity information based
|
|
538
|
+
on the provided `points_per_line`. It then writes the resulting PolyData to the specified file.
|
|
539
|
+
|
|
540
|
+
Parameters
|
|
541
|
+
----------
|
|
542
|
+
filepath : str
|
|
543
|
+
Path to the output file where the resulting VTK PolyData will be written.
|
|
544
|
+
|
|
545
|
+
positions : numpy.ndarray
|
|
546
|
+
Array of shape (n, 3) containing the coordinates of the points.
|
|
547
|
+
|
|
548
|
+
points_per_line : numpy.ndarray
|
|
549
|
+
Array specifying the number of points per line in the PolyData.
|
|
550
|
+
|
|
551
|
+
point_data : dict, optional
|
|
552
|
+
Dictionary mapping keys to arrays with per-point data (default is None).
|
|
553
|
+
|
|
554
|
+
cell_data : dict, optional
|
|
555
|
+
Dictionary mapping keys to arrays with per-line data (default is None).
|
|
556
|
+
|
|
557
|
+
fieldData : dict, optional
|
|
558
|
+
Dictionary containing field data (data not associated with points or
|
|
559
|
+
lines, default is None).
|
|
560
|
+
|
|
561
|
+
encoding : str, optional
|
|
562
|
+
Encoding to use for writing the file. Possible values are 'ascii',
|
|
563
|
+
'binary', or 'appended'. Defaults to 'appended'.
|
|
564
|
+
|
|
565
|
+
comments : str, optional
|
|
566
|
+
Any comments to include in the header of the VTK file (default is None).
|
|
567
|
+
"""
|
|
568
|
+
num_points = positions.shape[0]
|
|
569
|
+
|
|
570
|
+
# create some temporary arrays to write grid topology
|
|
571
|
+
# for points, this is simply a list of the points ids and offset by 1
|
|
572
|
+
# this is passed as vertices info
|
|
573
|
+
|
|
574
|
+
connectivity = np.arange(num_points, dtype="int32") # each point is only connected to itself
|
|
575
|
+
# offsets = np.arange(start=1, stop=num_points + 1, dtype="int32") # index of last node in each cell
|
|
576
|
+
|
|
577
|
+
cell_types = np.empty(num_points, dtype="uint8")
|
|
578
|
+
cell_types[:] = VTK_PolyLine.type_id
|
|
579
|
+
cell_types[:] = VTK_Line.type_id
|
|
580
|
+
|
|
581
|
+
write_vtp(filepath, points=positions, lines=(connectivity, np.cumsum(points_per_line)),
|
|
582
|
+
point_data=point_data, cell_data=cell_data, field_data=fieldData, encoding=encoding)
|
|
583
|
+
|
|
584
|
+
|
|
585
|
+
def points_to_poly(filepath, positions, data=None, fieldData=None, encoding='appended'):
|
|
586
|
+
"""
|
|
587
|
+
Generates a VTP file representing a polydata structure where each input point is treated as a vertex cell.
|
|
588
|
+
|
|
589
|
+
This function primarily converts the input data into the required VTK file
|
|
590
|
+
structure for points represented as individual vertices in the dataset.
|
|
591
|
+
|
|
592
|
+
Parameters
|
|
593
|
+
----------
|
|
594
|
+
filepath : str
|
|
595
|
+
The path to save the generated VTP file.
|
|
596
|
+
positions : np.ndarray
|
|
597
|
+
An array of shape (N, 3) where N is the number of points, and each point is represented
|
|
598
|
+
by its x, y, and z coordinates in 3D space.
|
|
599
|
+
data : dict, optional
|
|
600
|
+
Dictionary containing point-associated data. Keys represent the data names, and values
|
|
601
|
+
are arrays of data corresponding to each point. This is used for storing per-point custom
|
|
602
|
+
attributes or properties.
|
|
603
|
+
fieldData : dict, optional
|
|
604
|
+
Dictionary containing metadata or additional information that is not directly associated
|
|
605
|
+
with the points or cells. Keys represent the names, and values are arrays of the data.
|
|
606
|
+
encoding : str, optional
|
|
607
|
+
Specifies how the output file should be encoded. Defaults to 'appended'.
|
|
608
|
+
"""
|
|
609
|
+
num_points = positions.shape[0]
|
|
610
|
+
|
|
611
|
+
cell_types = np.empty(num_points, dtype="uint8")
|
|
612
|
+
cell_types[:] = VTK_Vertex.type_id
|
|
613
|
+
|
|
614
|
+
# create some temporary arrays to write grid topology
|
|
615
|
+
# for points, this is simply a list of the points ids and offset by 1
|
|
616
|
+
# this is passed as vertices info
|
|
617
|
+
connectivity = np.arange(num_points, dtype="int32") # each point is only connected to itself
|
|
618
|
+
offsets = np.arange(start=1, stop=num_points + 1, dtype="int32") # index of last node in each cell
|
|
619
|
+
|
|
620
|
+
write_vtp(filepath, points=positions, verts=(connectivity, offsets),
|
|
621
|
+
point_data=data, cell_data=None, field_data=fieldData, encoding=encoding)
|