2020-11-18 20:48:57 +05:30
|
|
|
import os
|
2020-06-26 15:15:54 +05:30
|
|
|
import multiprocessing as mp
|
2020-06-03 14:13:07 +05:30
|
|
|
from pathlib import Path
|
2022-11-23 02:56:15 +05:30
|
|
|
from typing import Optional, Union, Literal, List, Sequence
|
2020-03-11 22:37:31 +05:30
|
|
|
|
2020-03-11 04:22:02 +05:30
|
|
|
import numpy as np
|
2023-03-08 23:33:22 +05:30
|
|
|
from vtkmodules.vtkCommonCore import (
|
|
|
|
vtkPoints,
|
|
|
|
vtkStringArray,
|
|
|
|
vtkLookupTable,
|
|
|
|
)
|
|
|
|
from vtkmodules.vtkCommonDataModel import (
|
|
|
|
vtkDataSet,
|
|
|
|
vtkCellArray,
|
|
|
|
vtkImageData,
|
|
|
|
vtkRectilinearGrid,
|
|
|
|
vtkUnstructuredGrid,
|
|
|
|
vtkPolyData,
|
|
|
|
)
|
|
|
|
from vtkmodules.vtkIOLegacy import (
|
|
|
|
vtkGenericDataObjectReader,
|
|
|
|
vtkDataSetWriter,
|
|
|
|
)
|
|
|
|
from vtkmodules.vtkIOXML import (
|
|
|
|
vtkXMLImageDataReader,
|
|
|
|
vtkXMLImageDataWriter,
|
|
|
|
vtkXMLRectilinearGridReader,
|
|
|
|
vtkXMLRectilinearGridWriter,
|
|
|
|
vtkXMLUnstructuredGridReader,
|
|
|
|
vtkXMLUnstructuredGridWriter,
|
|
|
|
vtkXMLPolyDataReader,
|
|
|
|
vtkXMLPolyDataWriter,
|
|
|
|
)
|
|
|
|
from vtkmodules.vtkRenderingCore import (
|
|
|
|
vtkDataSetMapper,
|
|
|
|
vtkActor,
|
|
|
|
vtkRenderer,
|
|
|
|
vtkRenderWindow,
|
|
|
|
vtkRenderWindowInteractor,
|
|
|
|
)
|
|
|
|
from vtkmodules.vtkRenderingAnnotation import (
|
|
|
|
vtkScalarBarActor,
|
|
|
|
)
|
|
|
|
from vtkmodules.util.vtkConstants import (
|
|
|
|
VTK_TRIANGLE,
|
|
|
|
VTK_QUAD,
|
|
|
|
VTK_TETRA,
|
|
|
|
VTK_HEXAHEDRON,
|
|
|
|
)
|
|
|
|
from vtkmodules.util.numpy_support import (
|
|
|
|
numpy_to_vtk,
|
|
|
|
numpy_to_vtkIdTypeArray,
|
|
|
|
vtk_to_numpy,
|
|
|
|
)
|
2020-03-11 22:37:31 +05:30
|
|
|
|
2022-01-22 12:35:49 +05:30
|
|
|
from ._typehints import FloatSequence, IntSequence
|
2020-08-24 13:25:41 +05:30
|
|
|
from . import util
|
2020-03-13 00:22:33 +05:30
|
|
|
from . import Table
|
2022-02-21 11:57:35 +05:30
|
|
|
from . import Colormap
|
2020-03-11 04:22:02 +05:30
|
|
|
|
2020-03-13 00:22:33 +05:30
|
|
|
|
2020-03-12 11:21:52 +05:30
|
|
|
class VTK:
|
2020-03-11 04:22:02 +05:30
|
|
|
"""
|
2020-03-12 11:21:52 +05:30
|
|
|
Spatial visualization (and potentially manipulation).
|
2020-03-11 04:22:02 +05:30
|
|
|
|
2020-03-12 11:21:52 +05:30
|
|
|
High-level interface to VTK.
|
2020-03-11 04:22:02 +05:30
|
|
|
"""
|
|
|
|
|
2022-01-29 22:46:19 +05:30
|
|
|
def __init__(self,
|
2023-03-08 23:33:22 +05:30
|
|
|
vtk_data: vtkDataSet):
|
2020-03-12 11:21:52 +05:30
|
|
|
"""
|
2021-03-27 14:40:35 +05:30
|
|
|
New spatial visualization.
|
2020-03-12 11:21:52 +05:30
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2023-03-08 23:33:22 +05:30
|
|
|
vtk_data : subclass of vtkDataSet
|
2020-08-25 13:26:24 +05:30
|
|
|
Description of geometry and topology, optionally with attached data.
|
2023-03-08 23:33:22 +05:30
|
|
|
Valid types are vtkImageData, vtkUnstructuredGrid,
|
|
|
|
vtkPolyData, and vtkRectilinearGrid.
|
2020-03-12 11:21:52 +05:30
|
|
|
|
|
|
|
"""
|
2020-08-25 13:26:24 +05:30
|
|
|
self.vtk_data = vtk_data
|
2020-03-11 04:22:02 +05:30
|
|
|
|
2020-03-19 12:34:15 +05:30
|
|
|
|
2022-02-23 19:07:23 +05:30
|
|
|
def __repr__(self) -> str:
|
2022-07-08 21:36:41 +05:30
|
|
|
"""
|
|
|
|
Return repr(self).
|
|
|
|
|
2023-02-21 20:57:06 +05:30
|
|
|
Give short, human-readable summary.
|
2022-07-08 21:36:41 +05:30
|
|
|
|
|
|
|
"""
|
2022-02-23 19:07:23 +05:30
|
|
|
info = [self.vtk_data.__vtkname__]
|
|
|
|
|
|
|
|
for data in ['Cell Data', 'Point Data']:
|
|
|
|
if data == 'Cell Data': info.append(f'\n# cells: {self.N_cells}')
|
|
|
|
if data == 'Point Data': info.append(f'\n# points: {self.N_points}')
|
|
|
|
if data in self.labels:
|
|
|
|
info += [f' - {l}' for l in self.labels[data]]
|
|
|
|
|
|
|
|
return util.srepr(info)
|
|
|
|
|
|
|
|
|
|
|
|
def __eq__(self,
|
|
|
|
other: object) -> bool:
|
|
|
|
"""
|
2022-07-08 21:36:41 +05:30
|
|
|
Return self==other.
|
|
|
|
|
2022-07-08 21:37:07 +05:30
|
|
|
Test equality of other.
|
2022-02-23 19:07:23 +05:30
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
other : damask.VTK
|
2022-02-23 19:42:55 +05:30
|
|
|
VTK to check for equality.
|
2022-02-23 19:07:23 +05:30
|
|
|
|
|
|
|
"""
|
2022-02-23 19:42:55 +05:30
|
|
|
if not isinstance(other, VTK):
|
|
|
|
return NotImplemented
|
|
|
|
return self.as_ASCII() == other.as_ASCII()
|
|
|
|
|
2022-02-23 19:07:23 +05:30
|
|
|
|
|
|
|
|
2022-02-21 16:42:17 +05:30
|
|
|
def copy(self):
|
2023-03-08 23:33:22 +05:30
|
|
|
if isinstance(self.vtk_data,vtkImageData):
|
|
|
|
dup = vtkImageData()
|
|
|
|
elif isinstance(self.vtk_data,vtkUnstructuredGrid):
|
|
|
|
dup = vtkUnstructuredGrid()
|
|
|
|
elif isinstance(self.vtk_data,vtkPolyData):
|
|
|
|
dup = vtkPolyData()
|
|
|
|
elif isinstance(self.vtk_data,vtkRectilinearGrid):
|
|
|
|
dup = vtkRectilinearGrid()
|
2022-02-21 16:42:17 +05:30
|
|
|
else:
|
|
|
|
raise TypeError
|
|
|
|
|
|
|
|
dup.DeepCopy(self.vtk_data)
|
|
|
|
|
|
|
|
return VTK(dup)
|
|
|
|
|
|
|
|
|
2022-02-23 19:07:23 +05:30
|
|
|
@property
|
|
|
|
def comments(self) -> List[str]:
|
|
|
|
"""Return the comments."""
|
2022-03-07 15:08:56 +05:30
|
|
|
field_data = self.vtk_data.GetFieldData()
|
|
|
|
for a in range(field_data.GetNumberOfArrays()):
|
|
|
|
if field_data.GetArrayName(a) == 'comments':
|
|
|
|
comments = field_data.GetAbstractArray(a)
|
2022-02-23 19:07:23 +05:30
|
|
|
return [comments.GetValue(i) for i in range(comments.GetNumberOfValues())]
|
|
|
|
return []
|
|
|
|
|
|
|
|
@comments.setter
|
|
|
|
def comments(self,
|
2022-12-14 00:02:19 +05:30
|
|
|
comments: Sequence[str]):
|
2022-02-23 19:07:23 +05:30
|
|
|
"""
|
|
|
|
Set comments.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2022-12-14 00:02:19 +05:30
|
|
|
comments : sequence of str
|
2022-02-23 19:07:23 +05:30
|
|
|
Comments.
|
|
|
|
|
|
|
|
"""
|
2023-03-08 23:33:22 +05:30
|
|
|
s = vtkStringArray()
|
2022-02-23 19:07:23 +05:30
|
|
|
s.SetName('comments')
|
2022-12-14 00:02:19 +05:30
|
|
|
for c in comments:
|
2022-02-23 19:07:23 +05:30
|
|
|
s.InsertNextValue(c)
|
|
|
|
self.vtk_data.GetFieldData().AddArray(s)
|
|
|
|
|
|
|
|
|
2022-02-16 03:08:02 +05:30
|
|
|
@property
|
|
|
|
def N_points(self) -> int:
|
|
|
|
"""Number of points in vtkdata."""
|
|
|
|
return self.vtk_data.GetNumberOfPoints()
|
|
|
|
|
2022-02-23 19:07:23 +05:30
|
|
|
|
2022-02-16 03:08:02 +05:30
|
|
|
@property
|
|
|
|
def N_cells(self) -> int:
|
|
|
|
"""Number of cells in vtkdata."""
|
|
|
|
return self.vtk_data.GetNumberOfCells()
|
|
|
|
|
2022-02-23 19:07:23 +05:30
|
|
|
|
2022-02-22 12:38:41 +05:30
|
|
|
@property
|
|
|
|
def labels(self):
|
|
|
|
"""Labels of datasets."""
|
|
|
|
labels = {}
|
|
|
|
|
|
|
|
cell_data = self.vtk_data.GetCellData()
|
|
|
|
if c := [cell_data.GetArrayName(a) for a in range(cell_data.GetNumberOfArrays())]:
|
|
|
|
labels['Cell Data'] = c
|
|
|
|
|
|
|
|
point_data = self.vtk_data.GetPointData()
|
|
|
|
if p := [point_data.GetArrayName(a) for a in range(point_data.GetNumberOfArrays())]:
|
|
|
|
labels['Point Data'] = p
|
|
|
|
|
|
|
|
return labels
|
2022-02-16 03:08:02 +05:30
|
|
|
|
2022-02-23 19:07:23 +05:30
|
|
|
|
2021-06-15 20:32:02 +05:30
|
|
|
@staticmethod
|
2022-01-26 20:55:27 +05:30
|
|
|
def from_image_data(cells: IntSequence,
|
|
|
|
size: FloatSequence,
|
|
|
|
origin: FloatSequence = np.zeros(3)) -> 'VTK':
|
2021-06-15 20:32:02 +05:30
|
|
|
"""
|
2023-03-08 23:33:22 +05:30
|
|
|
Create VTK of type vtkImageData.
|
2021-06-15 20:32:02 +05:30
|
|
|
|
|
|
|
This is the common type for grid solver results.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2022-05-18 18:51:32 +05:30
|
|
|
cells : sequence of int, len (3)
|
2021-06-15 20:32:02 +05:30
|
|
|
Number of cells along each dimension.
|
2022-05-18 18:51:32 +05:30
|
|
|
size : sequence of float, len (3)
|
2023-02-21 20:57:06 +05:30
|
|
|
Edge length along each dimension.
|
2022-05-18 18:51:32 +05:30
|
|
|
origin : sequence of float, len (3), optional
|
2021-06-15 20:32:02 +05:30
|
|
|
Coordinates of grid origin.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
new : damask.VTK
|
|
|
|
VTK-based geometry without nodal or cell data.
|
|
|
|
|
|
|
|
"""
|
2023-03-08 23:33:22 +05:30
|
|
|
vtk_data = vtkImageData()
|
2021-06-15 20:32:02 +05:30
|
|
|
vtk_data.SetDimensions(*(np.array(cells)+1))
|
|
|
|
vtk_data.SetOrigin(*(np.array(origin)))
|
2022-01-22 12:35:49 +05:30
|
|
|
vtk_data.SetSpacing(*(np.array(size)/np.array(cells)))
|
2021-06-15 20:32:02 +05:30
|
|
|
|
|
|
|
return VTK(vtk_data)
|
|
|
|
|
|
|
|
|
2020-03-11 04:22:02 +05:30
|
|
|
@staticmethod
|
2022-01-26 20:55:27 +05:30
|
|
|
def from_unstructured_grid(nodes: np.ndarray,
|
|
|
|
connectivity: np.ndarray,
|
|
|
|
cell_type: str) -> 'VTK':
|
2020-03-12 01:26:58 +05:30
|
|
|
"""
|
2023-03-08 23:33:22 +05:30
|
|
|
Create VTK of type vtkUnstructuredGrid.
|
2020-03-12 01:26:58 +05:30
|
|
|
|
2021-04-24 18:17:52 +05:30
|
|
|
This is the common type for mesh solver results.
|
2020-03-12 11:21:52 +05:30
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2022-02-18 05:19:45 +05:30
|
|
|
nodes : numpy.ndarray, shape (:,3)
|
2020-03-19 12:34:15 +05:30
|
|
|
Spatial position of the nodes.
|
2022-06-02 23:10:18 +05:30
|
|
|
connectivity : numpy.ndarray of np.dtype = np.int64
|
2020-12-02 19:07:44 +05:30
|
|
|
Cell connectivity (0-based), first dimension determines #Cells,
|
|
|
|
second dimension determines #Nodes/Cell.
|
2020-03-12 11:21:52 +05:30
|
|
|
cell_type : str
|
2023-03-08 23:33:22 +05:30
|
|
|
Name of the vtkCell subclass. Tested for TRIANGLE, QUAD, TETRA, and HEXAHEDRON.
|
2020-03-12 01:26:58 +05:30
|
|
|
|
2021-04-24 18:17:52 +05:30
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
new : damask.VTK
|
|
|
|
VTK-based geometry without nodal or cell data.
|
|
|
|
|
2020-03-12 01:26:58 +05:30
|
|
|
"""
|
2023-03-08 23:33:22 +05:30
|
|
|
vtk_nodes = vtkPoints()
|
|
|
|
vtk_nodes.SetData(numpy_to_vtk(np.ascontiguousarray(nodes)))
|
|
|
|
cells = vtkCellArray()
|
2020-03-12 01:26:58 +05:30
|
|
|
cells.SetNumberOfCells(connectivity.shape[0])
|
|
|
|
T = np.concatenate((np.ones((connectivity.shape[0],1),dtype=np.int64)*connectivity.shape[1],
|
2020-03-12 12:45:12 +05:30
|
|
|
connectivity),axis=1).ravel()
|
2023-03-08 23:33:22 +05:30
|
|
|
cells.SetCells(connectivity.shape[0],numpy_to_vtkIdTypeArray(T,deep=True))
|
2020-03-12 01:26:58 +05:30
|
|
|
|
2023-03-08 23:33:22 +05:30
|
|
|
vtk_data = vtkUnstructuredGrid()
|
2020-08-25 13:26:24 +05:30
|
|
|
vtk_data.SetPoints(vtk_nodes)
|
2023-03-08 23:33:22 +05:30
|
|
|
cell_types = {'TRIANGLE':VTK_TRIANGLE, 'QUAD':VTK_QUAD,
|
|
|
|
'TETRA' :VTK_TETRA, 'HEXAHEDRON':VTK_HEXAHEDRON}
|
2020-12-02 19:07:44 +05:30
|
|
|
vtk_data.SetCells(cell_types[cell_type.split("_",1)[-1].upper()],cells)
|
2020-03-11 04:22:02 +05:30
|
|
|
|
2020-08-25 13:26:24 +05:30
|
|
|
return VTK(vtk_data)
|
2020-03-11 04:22:02 +05:30
|
|
|
|
2020-03-11 12:02:03 +05:30
|
|
|
|
2020-03-12 01:26:58 +05:30
|
|
|
@staticmethod
|
2022-01-22 12:35:49 +05:30
|
|
|
def from_poly_data(points: np.ndarray) -> 'VTK':
|
2020-03-12 11:21:52 +05:30
|
|
|
"""
|
2023-03-08 23:33:22 +05:30
|
|
|
Create VTK of type polyData.
|
2020-03-12 11:21:52 +05:30
|
|
|
|
|
|
|
This is the common type for point-wise data.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2022-02-18 05:19:45 +05:30
|
|
|
points : numpy.ndarray, shape (:,3)
|
2020-03-19 12:34:15 +05:30
|
|
|
Spatial position of the points.
|
2020-03-12 11:21:52 +05:30
|
|
|
|
2021-04-24 18:17:52 +05:30
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
new : damask.VTK
|
|
|
|
VTK-based geometry without nodal or cell data.
|
|
|
|
|
2020-03-12 11:21:52 +05:30
|
|
|
"""
|
2020-10-27 17:49:53 +05:30
|
|
|
N = points.shape[0]
|
2023-03-08 23:33:22 +05:30
|
|
|
vtk_points = vtkPoints()
|
|
|
|
vtk_points.SetData(numpy_to_vtk(np.ascontiguousarray(points)))
|
2020-03-12 03:05:58 +05:30
|
|
|
|
2023-03-08 23:33:22 +05:30
|
|
|
vtk_cells = vtkCellArray()
|
2020-10-27 17:49:53 +05:30
|
|
|
vtk_cells.SetNumberOfCells(N)
|
2023-03-08 23:33:22 +05:30
|
|
|
vtk_cells.SetCells(N,numpy_to_vtkIdTypeArray(np.stack((np.ones (N,dtype=np.int64),
|
2020-10-27 17:49:53 +05:30
|
|
|
np.arange(N,dtype=np.int64)),axis=1).ravel(),deep=True))
|
|
|
|
|
2023-03-08 23:33:22 +05:30
|
|
|
vtk_data = vtkPolyData()
|
2020-08-25 13:26:24 +05:30
|
|
|
vtk_data.SetPoints(vtk_points)
|
2020-10-27 17:49:53 +05:30
|
|
|
vtk_data.SetVerts(vtk_cells)
|
2020-03-12 03:05:58 +05:30
|
|
|
|
2020-08-25 13:26:24 +05:30
|
|
|
return VTK(vtk_data)
|
2020-03-12 01:26:58 +05:30
|
|
|
|
2020-03-12 12:45:12 +05:30
|
|
|
|
2022-02-18 22:15:21 +05:30
|
|
|
@staticmethod
|
|
|
|
def from_rectilinear_grid(grid: FloatSequence) -> 'VTK':
|
|
|
|
"""
|
2023-03-08 23:33:22 +05:30
|
|
|
Create VTK of type vtkRectilinearGrid.
|
2022-02-18 22:15:21 +05:30
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2022-05-18 19:01:19 +05:30
|
|
|
grid : sequence of sequences of floats, len (3)
|
2022-02-18 22:15:21 +05:30
|
|
|
Grid coordinates along x, y, and z directions.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
new : damask.VTK
|
|
|
|
VTK-based geometry without nodal or cell data.
|
|
|
|
|
|
|
|
"""
|
2023-03-08 23:33:22 +05:30
|
|
|
vtk_data = vtkRectilinearGrid()
|
2022-02-18 22:15:21 +05:30
|
|
|
vtk_data.SetDimensions(*map(len,grid))
|
2023-03-08 23:33:22 +05:30
|
|
|
coord = [numpy_to_vtk(np.array(grid[i]),deep=True) for i in [0,1,2]]
|
2022-02-18 22:15:21 +05:30
|
|
|
[coord[i].SetName(n) for i,n in enumerate(['x','y','z'])]
|
|
|
|
vtk_data.SetXCoordinates(coord[0])
|
|
|
|
vtk_data.SetYCoordinates(coord[1])
|
|
|
|
vtk_data.SetZCoordinates(coord[2])
|
|
|
|
|
|
|
|
return VTK(vtk_data)
|
|
|
|
|
|
|
|
|
2020-03-12 04:24:36 +05:30
|
|
|
@staticmethod
|
2022-01-17 19:30:25 +05:30
|
|
|
def load(fname: Union[str, Path],
|
2022-11-23 02:56:15 +05:30
|
|
|
dataset_type: Literal[None, 'ImageData', 'UnstructuredGrid', 'PolyData', 'RectilinearGrid'] = None) -> 'VTK':
|
2020-03-12 11:21:52 +05:30
|
|
|
"""
|
2020-12-04 02:28:24 +05:30
|
|
|
Load from VTK file.
|
2020-03-12 11:21:52 +05:30
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2020-06-28 10:47:51 +05:30
|
|
|
fname : str or pathlib.Path
|
2023-02-21 20:57:06 +05:30
|
|
|
Filename to read.
|
2022-02-18 22:15:21 +05:30
|
|
|
Valid extensions are .vti, .vtu, .vtp, .vtr, and .vtk.
|
|
|
|
dataset_type : {'ImageData', 'UnstructuredGrid', 'PolyData', 'RectilinearGrid'}, optional
|
2023-03-08 23:33:22 +05:30
|
|
|
Name of the vtkDataSet subclass when opening a .vtk file.
|
2020-03-12 11:21:52 +05:30
|
|
|
|
2021-04-24 18:17:52 +05:30
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
loaded : damask.VTK
|
|
|
|
VTK-based geometry from file.
|
|
|
|
|
2020-03-12 11:21:52 +05:30
|
|
|
"""
|
2022-02-21 16:47:00 +05:30
|
|
|
if not Path(fname).expanduser().is_file(): # vtk has a strange error handling
|
2022-02-22 21:26:12 +05:30
|
|
|
raise FileNotFoundError(f'file "{fname}" not found')
|
2022-01-29 22:46:19 +05:30
|
|
|
if (ext := Path(fname).suffix) == '.vtk' or dataset_type is not None:
|
2023-03-08 23:33:22 +05:30
|
|
|
reader = vtkGenericDataObjectReader()
|
2022-02-21 11:43:19 +05:30
|
|
|
reader.SetFileName(str(Path(fname).expanduser()))
|
2020-03-21 15:37:21 +05:30
|
|
|
if dataset_type is None:
|
2022-02-22 21:26:12 +05:30
|
|
|
raise TypeError('dataset type for *.vtk file not given')
|
2021-06-15 20:32:02 +05:30
|
|
|
elif dataset_type.lower().endswith(('imagedata','image_data')):
|
|
|
|
reader.Update()
|
2021-06-16 00:53:10 +05:30
|
|
|
vtk_data = reader.GetStructuredPointsOutput()
|
2021-04-24 10:43:36 +05:30
|
|
|
elif dataset_type.lower().endswith(('unstructuredgrid','unstructured_grid')):
|
2020-08-25 11:19:56 +05:30
|
|
|
reader.Update()
|
2020-08-25 13:26:24 +05:30
|
|
|
vtk_data = reader.GetUnstructuredGridOutput()
|
2021-04-24 10:43:36 +05:30
|
|
|
elif dataset_type.lower().endswith(('polydata','poly_data')):
|
2020-08-25 11:19:56 +05:30
|
|
|
reader.Update()
|
2020-08-25 13:26:24 +05:30
|
|
|
vtk_data = reader.GetPolyDataOutput()
|
2022-02-18 22:15:21 +05:30
|
|
|
elif dataset_type.lower().endswith(('rectilineargrid','rectilinear_grid')):
|
|
|
|
reader.Update()
|
|
|
|
vtk_data = reader.GetRectilinearGridOutput()
|
2020-03-12 04:24:36 +05:30
|
|
|
else:
|
2022-02-22 21:26:12 +05:30
|
|
|
raise TypeError(f'unknown dataset type "{dataset_type}" for vtk file')
|
2020-03-12 04:24:36 +05:30
|
|
|
else:
|
2021-06-15 20:32:02 +05:30
|
|
|
if ext == '.vti':
|
2023-03-08 23:33:22 +05:30
|
|
|
reader = vtkXMLImageDataReader()
|
2020-03-12 04:24:36 +05:30
|
|
|
elif ext == '.vtu':
|
2023-03-08 23:33:22 +05:30
|
|
|
reader = vtkXMLUnstructuredGridReader()
|
2020-03-12 04:24:36 +05:30
|
|
|
elif ext == '.vtp':
|
2023-03-08 23:33:22 +05:30
|
|
|
reader = vtkXMLPolyDataReader()
|
2022-02-18 22:15:21 +05:30
|
|
|
elif ext == '.vtr':
|
2023-03-08 23:33:22 +05:30
|
|
|
reader = vtkXMLRectilinearGridReader()
|
2020-03-12 04:24:36 +05:30
|
|
|
else:
|
2022-02-22 21:26:12 +05:30
|
|
|
raise TypeError(f'unknown file extension "{ext}"')
|
2020-03-12 04:24:36 +05:30
|
|
|
|
2022-02-21 11:43:19 +05:30
|
|
|
reader.SetFileName(str(Path(fname).expanduser()))
|
2020-03-12 04:24:36 +05:30
|
|
|
reader.Update()
|
2020-08-25 13:26:24 +05:30
|
|
|
vtk_data = reader.GetOutput()
|
2020-03-12 04:24:36 +05:30
|
|
|
|
2020-08-25 13:26:24 +05:30
|
|
|
return VTK(vtk_data)
|
2020-03-12 04:24:36 +05:30
|
|
|
|
2020-10-27 17:49:53 +05:30
|
|
|
|
2020-06-26 15:15:54 +05:30
|
|
|
@staticmethod
|
|
|
|
def _write(writer):
|
|
|
|
"""Wrapper for parallel writing."""
|
|
|
|
writer.Write()
|
2022-01-26 20:55:27 +05:30
|
|
|
|
2022-02-23 19:07:23 +05:30
|
|
|
|
|
|
|
def as_ASCII(self) -> str:
|
|
|
|
"""ASCII representation of the VTK data."""
|
2023-03-08 23:33:22 +05:30
|
|
|
writer = vtkDataSetWriter()
|
2022-02-23 19:07:23 +05:30
|
|
|
writer.SetHeader(f'# {util.execution_stamp("VTK")}')
|
|
|
|
writer.WriteToOutputStringOn()
|
|
|
|
writer.SetInputData(self.vtk_data)
|
|
|
|
writer.Write()
|
|
|
|
return writer.GetOutputString()
|
|
|
|
|
|
|
|
|
2022-01-26 20:55:27 +05:30
|
|
|
def save(self,
|
|
|
|
fname: Union[str, Path],
|
|
|
|
parallel: bool = True,
|
|
|
|
compress: bool = True):
|
2020-03-12 11:21:52 +05:30
|
|
|
"""
|
2020-12-04 02:28:24 +05:30
|
|
|
Save as VTK file.
|
2020-03-12 11:21:52 +05:30
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2020-06-28 10:47:51 +05:30
|
|
|
fname : str or pathlib.Path
|
2023-02-21 20:57:06 +05:30
|
|
|
Filename to write.
|
2022-01-13 03:43:38 +05:30
|
|
|
parallel : bool, optional
|
2020-06-26 15:15:54 +05:30
|
|
|
Write data in parallel background process. Defaults to True.
|
2020-09-18 20:02:08 +05:30
|
|
|
compress : bool, optional
|
|
|
|
Compress with zlib algorithm. Defaults to True.
|
2020-03-12 11:21:52 +05:30
|
|
|
|
|
|
|
"""
|
2023-03-08 23:33:22 +05:30
|
|
|
if isinstance(self.vtk_data,vtkImageData):
|
|
|
|
writer = vtkXMLImageDataWriter()
|
|
|
|
elif isinstance(self.vtk_data,vtkUnstructuredGrid):
|
|
|
|
writer = vtkXMLUnstructuredGridWriter()
|
|
|
|
elif isinstance(self.vtk_data,vtkPolyData):
|
|
|
|
writer = vtkXMLPolyDataWriter()
|
|
|
|
elif isinstance(self.vtk_data,vtkRectilinearGrid):
|
|
|
|
writer = vtkXMLRectilinearGridWriter()
|
2020-03-11 12:02:03 +05:30
|
|
|
|
2020-11-04 22:38:04 +05:30
|
|
|
default_ext = '.'+writer.GetDefaultFileExtension()
|
2020-06-03 14:13:07 +05:30
|
|
|
ext = Path(fname).suffix
|
2022-02-21 11:43:19 +05:30
|
|
|
writer.SetFileName(str(Path(fname).expanduser())+(default_ext if default_ext != ext else ''))
|
2020-11-04 22:38:04 +05:30
|
|
|
|
2020-09-12 17:16:55 +05:30
|
|
|
if compress:
|
|
|
|
writer.SetCompressorTypeToZLib()
|
|
|
|
else:
|
|
|
|
writer.SetCompressorTypeToNone()
|
2020-03-11 12:02:03 +05:30
|
|
|
writer.SetDataModeToBinary()
|
2020-08-25 13:26:24 +05:30
|
|
|
writer.SetInputData(self.vtk_data)
|
2020-08-03 21:49:38 +05:30
|
|
|
|
2020-06-26 15:15:54 +05:30
|
|
|
if parallel:
|
2020-08-03 21:49:38 +05:30
|
|
|
try:
|
|
|
|
mp_writer = mp.Process(target=self._write,args=(writer,))
|
|
|
|
mp_writer.start()
|
|
|
|
except TypeError:
|
|
|
|
writer.Write()
|
2020-06-26 15:15:54 +05:30
|
|
|
else:
|
|
|
|
writer.Write()
|
2020-03-11 12:02:03 +05:30
|
|
|
|
2020-03-11 22:37:31 +05:30
|
|
|
|
2020-03-12 11:21:52 +05:30
|
|
|
# Check https://blog.kitware.com/ghost-and-blanking-visibility-changes/ for missing data
|
2022-05-12 03:49:10 +05:30
|
|
|
def set(self,
|
2022-11-23 02:56:15 +05:30
|
|
|
label: Optional[str] = None,
|
|
|
|
data: Union[None, np.ndarray, np.ma.MaskedArray] = None,
|
|
|
|
info: Optional[str] = None,
|
2022-03-12 06:37:18 +05:30
|
|
|
*,
|
2023-11-28 16:20:12 +05:30
|
|
|
table: Optional['Table'] = None) -> 'VTK':
|
2020-08-24 10:10:36 +05:30
|
|
|
"""
|
2022-05-13 09:32:22 +05:30
|
|
|
Add new or replace existing point or cell data.
|
2020-08-24 10:10:36 +05:30
|
|
|
|
2022-03-12 06:37:18 +05:30
|
|
|
Data can either be a numpy.array, which requires a corresponding label,
|
|
|
|
or a damask.Table.
|
|
|
|
|
2020-08-24 10:10:36 +05:30
|
|
|
Parameters
|
|
|
|
----------
|
2022-03-12 06:37:18 +05:30
|
|
|
label : str, optional
|
|
|
|
Label of data array.
|
|
|
|
data : numpy.ndarray or numpy.ma.MaskedArray, optional
|
2022-05-12 03:49:10 +05:30
|
|
|
Data to add or replace. First array dimension needs to match either
|
2020-09-10 04:29:40 +05:30
|
|
|
number of cells or number of points.
|
2022-05-13 09:32:22 +05:30
|
|
|
info : str, optional
|
|
|
|
Human-readable information about the data.
|
2022-03-12 06:37:18 +05:30
|
|
|
table: damask.Table, optional
|
2022-05-12 03:49:10 +05:30
|
|
|
Data to add or replace. Each table label is individually considered.
|
|
|
|
Number of rows needs to match either number of cells or number of points.
|
|
|
|
|
2023-02-21 20:57:06 +05:30
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
updated : damask.VTK
|
|
|
|
Updated VTK-based geometry.
|
|
|
|
|
2022-05-12 03:49:10 +05:30
|
|
|
Notes
|
|
|
|
-----
|
|
|
|
If the number of cells equals the number of points, the data is added to both.
|
2020-08-24 10:10:36 +05:30
|
|
|
|
|
|
|
"""
|
2020-03-12 04:24:36 +05:30
|
|
|
|
2022-02-21 16:47:00 +05:30
|
|
|
def _add_array(vtk_data,
|
2022-03-12 06:37:18 +05:30
|
|
|
label: str,
|
|
|
|
data: np.ndarray):
|
2022-02-16 03:08:02 +05:30
|
|
|
|
2022-05-12 03:49:10 +05:30
|
|
|
N_p,N_c = vtk_data.GetNumberOfPoints(),vtk_data.GetNumberOfCells()
|
|
|
|
if (N_data := data.shape[0]) not in [N_p,N_c]:
|
|
|
|
raise ValueError(f'data count mismatch ({N_data} ≠ {N_p} & {N_c})')
|
|
|
|
|
2022-02-23 19:07:23 +05:30
|
|
|
data_ = data.reshape(N_data,-1) \
|
2022-02-16 03:08:02 +05:30
|
|
|
.astype(np.single if data.dtype in [np.double,np.longdouble] else data.dtype)
|
2021-04-25 00:54:26 +05:30
|
|
|
|
2022-02-16 03:08:02 +05:30
|
|
|
if data.dtype.type is np.str_:
|
2023-03-08 23:33:22 +05:30
|
|
|
d = vtkStringArray()
|
2021-04-25 00:54:26 +05:30
|
|
|
for s in np.squeeze(data_):
|
|
|
|
d.InsertNextValue(s)
|
|
|
|
else:
|
2023-03-08 23:33:22 +05:30
|
|
|
d = numpy_to_vtk(data_,deep=True)
|
2021-04-25 00:54:26 +05:30
|
|
|
|
2020-03-12 04:24:36 +05:30
|
|
|
d.SetName(label)
|
2020-06-26 15:15:54 +05:30
|
|
|
|
2022-05-12 03:49:10 +05:30
|
|
|
if N_data == N_p:
|
2022-02-21 16:47:00 +05:30
|
|
|
vtk_data.GetPointData().AddArray(d)
|
2022-05-12 03:49:10 +05:30
|
|
|
if N_data == N_c:
|
2022-02-21 16:47:00 +05:30
|
|
|
vtk_data.GetCellData().AddArray(d)
|
2022-02-16 03:08:02 +05:30
|
|
|
|
2022-03-12 06:37:18 +05:30
|
|
|
if data is None and table is None:
|
|
|
|
raise KeyError('no data given')
|
|
|
|
if data is not None and table is not None:
|
|
|
|
raise KeyError('cannot use both, data and table')
|
|
|
|
|
2022-02-21 16:47:00 +05:30
|
|
|
dup = self.copy()
|
2022-02-16 03:08:02 +05:30
|
|
|
if isinstance(data,np.ndarray):
|
|
|
|
if label is not None:
|
2022-02-21 16:47:00 +05:30
|
|
|
_add_array(dup.vtk_data,
|
2022-03-12 06:37:18 +05:30
|
|
|
label,
|
|
|
|
np.where(data.mask,data.fill_value,data) if isinstance(data,np.ma.MaskedArray) else data)
|
2022-12-14 00:02:19 +05:30
|
|
|
if info is not None: dup.comments += [f'{label}: {info}']
|
2022-02-16 03:08:02 +05:30
|
|
|
else:
|
2022-03-12 06:37:18 +05:30
|
|
|
raise ValueError('no label defined for data')
|
|
|
|
elif isinstance(table,Table):
|
|
|
|
for l in table.labels:
|
|
|
|
_add_array(dup.vtk_data,l,table.get(l))
|
2022-12-14 00:02:19 +05:30
|
|
|
if info is not None: dup.comments += [f'{l}: {info}']
|
2020-03-19 13:33:38 +05:30
|
|
|
else:
|
|
|
|
raise TypeError
|
2020-03-11 22:37:31 +05:30
|
|
|
|
2022-02-21 16:47:00 +05:30
|
|
|
return dup
|
|
|
|
|
2020-03-11 22:37:31 +05:30
|
|
|
|
2022-01-26 20:55:27 +05:30
|
|
|
def get(self,
|
|
|
|
label: str) -> np.ndarray:
|
2020-08-24 10:10:36 +05:30
|
|
|
"""
|
|
|
|
Get either cell or point data.
|
|
|
|
|
|
|
|
Cell data takes precedence over point data, i.e. this
|
|
|
|
function assumes that labels are unique among cell and
|
|
|
|
point data.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
label : str
|
|
|
|
Data label.
|
|
|
|
|
2021-04-24 18:17:52 +05:30
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
data : numpy.ndarray
|
|
|
|
Data stored under the given label.
|
|
|
|
|
2020-08-24 10:10:36 +05:30
|
|
|
"""
|
2020-08-25 13:26:24 +05:30
|
|
|
cell_data = self.vtk_data.GetCellData()
|
2023-11-29 01:22:49 +05:30
|
|
|
if label in [cell_data.GetArrayName(a) for a in range(cell_data.GetNumberOfArrays())]:
|
|
|
|
try:
|
|
|
|
return vtk_to_numpy(cell_data.GetArray(label))
|
|
|
|
except AttributeError:
|
|
|
|
vtk_array = cell_data.GetAbstractArray(label) # string array
|
2020-08-24 10:10:36 +05:30
|
|
|
|
2020-08-25 13:26:24 +05:30
|
|
|
point_data = self.vtk_data.GetPointData()
|
2023-11-29 01:22:49 +05:30
|
|
|
if label in [point_data.GetArrayName(a) for a in range(point_data.GetNumberOfArrays())]:
|
|
|
|
try:
|
|
|
|
return vtk_to_numpy(point_data.GetArray(label))
|
|
|
|
except AttributeError:
|
|
|
|
vtk_array = point_data.GetAbstractArray(label) # string array
|
2021-04-25 00:54:26 +05:30
|
|
|
|
|
|
|
try:
|
|
|
|
# string array
|
|
|
|
return np.array([vtk_array.GetValue(i) for i in range(vtk_array.GetNumberOfValues())]).astype(str)
|
|
|
|
except UnboundLocalError:
|
2022-03-07 15:58:17 +05:30
|
|
|
raise KeyError(f'array "{label}" not found')
|
2020-08-24 10:10:36 +05:30
|
|
|
|
|
|
|
|
2023-11-28 16:20:12 +05:30
|
|
|
def delete(self,
|
|
|
|
label: str) -> 'VTK':
|
|
|
|
"""
|
|
|
|
Delete either cell or point data.
|
|
|
|
|
|
|
|
Cell data takes precedence over point data, i.e. this
|
|
|
|
function assumes that labels are unique among cell and
|
|
|
|
point data.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
label : str
|
|
|
|
Data label.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
updated : damask.VTK
|
|
|
|
Updated VTK-based geometry.
|
|
|
|
|
|
|
|
"""
|
|
|
|
dup = self.copy()
|
2023-11-28 23:01:47 +05:30
|
|
|
|
2023-11-28 16:20:12 +05:30
|
|
|
cell_data = dup.vtk_data.GetCellData()
|
2023-11-28 23:01:47 +05:30
|
|
|
if label in [cell_data.GetArrayName(a) for a in range(cell_data.GetNumberOfArrays())]:
|
|
|
|
dup.vtk_data.GetCellData().RemoveArray(label)
|
|
|
|
return dup
|
2023-11-28 16:20:12 +05:30
|
|
|
|
|
|
|
point_data = self.vtk_data.GetPointData()
|
2023-11-28 23:01:47 +05:30
|
|
|
if label in [point_data.GetArrayName(a) for a in range(point_data.GetNumberOfArrays())]:
|
|
|
|
dup.vtk_data.GetPointData().RemoveArray(label)
|
|
|
|
return dup
|
2023-11-28 16:20:12 +05:30
|
|
|
|
|
|
|
raise KeyError(f'array "{label}" not found')
|
|
|
|
|
|
|
|
|
2022-02-21 15:49:53 +05:30
|
|
|
def show(self,
|
2022-11-23 02:56:15 +05:30
|
|
|
label: Optional[str] = None,
|
2022-03-09 03:13:54 +05:30
|
|
|
colormap: Union[Colormap, str] = 'cividis'):
|
2020-03-12 18:15:47 +05:30
|
|
|
"""
|
|
|
|
Render.
|
|
|
|
|
2022-03-06 03:20:33 +05:30
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
label : str, optional
|
|
|
|
Label of the dataset to show.
|
2022-03-08 20:01:08 +05:30
|
|
|
colormap : damask.Colormap or str, optional
|
|
|
|
Colormap for visualization of dataset. Defaults to 'cividis'.
|
2022-03-06 03:20:33 +05:30
|
|
|
|
|
|
|
Notes
|
|
|
|
-----
|
2022-11-29 01:35:39 +05:30
|
|
|
The first component is shown when visualizing vector datasets
|
2023-02-21 20:57:06 +05:30
|
|
|
(this includes tensor datasets as they are flattened).
|
2022-03-06 03:20:33 +05:30
|
|
|
|
2020-03-12 18:15:47 +05:30
|
|
|
"""
|
2022-11-29 01:35:39 +05:30
|
|
|
# See http://compilatrix.com/article/vtk-1 for possible improvements.
|
2021-02-12 02:26:53 +05:30
|
|
|
try:
|
|
|
|
import wx
|
|
|
|
_ = wx.App(False) # noqa
|
|
|
|
width, height = wx.GetDisplaySize()
|
|
|
|
except ImportError:
|
2021-01-15 01:42:29 +05:30
|
|
|
try:
|
2021-02-12 02:26:53 +05:30
|
|
|
import tkinter
|
|
|
|
tk = tkinter.Tk()
|
|
|
|
width = tk.winfo_screenwidth()
|
|
|
|
height = tk.winfo_screenheight()
|
|
|
|
tk.destroy()
|
2022-01-29 23:40:12 +05:30
|
|
|
except Exception:
|
2021-02-12 02:26:53 +05:30
|
|
|
width = 1024
|
|
|
|
height = 768
|
|
|
|
|
2023-03-08 23:33:22 +05:30
|
|
|
lut = vtkLookupTable()
|
2022-03-09 03:13:54 +05:30
|
|
|
colormap_ = Colormap.from_predefined(colormap) if isinstance(colormap,str) else \
|
|
|
|
colormap
|
2022-03-06 03:20:33 +05:30
|
|
|
lut.SetNumberOfTableValues(len(colormap_.colors))
|
|
|
|
for i,c in enumerate(colormap_.colors):
|
2022-02-21 11:57:35 +05:30
|
|
|
lut.SetTableValue(i,c if len(c)==4 else np.append(c,1.0))
|
|
|
|
lut.Build()
|
|
|
|
|
|
|
|
self.vtk_data.GetCellData().SetActiveScalars(label)
|
2023-03-08 23:33:22 +05:30
|
|
|
mapper = vtkDataSetMapper()
|
2020-08-25 13:26:24 +05:30
|
|
|
mapper.SetInputData(self.vtk_data)
|
2022-02-21 11:57:35 +05:30
|
|
|
mapper.SetLookupTable(lut)
|
|
|
|
mapper.SetScalarRange(self.vtk_data.GetScalarRange())
|
2022-01-16 12:48:06 +05:30
|
|
|
|
2023-03-08 23:33:22 +05:30
|
|
|
actor = vtkActor()
|
2020-03-12 18:15:47 +05:30
|
|
|
actor.SetMapper(mapper)
|
2022-01-16 12:48:06 +05:30
|
|
|
actor.GetProperty().SetColor(230/255,150/255,68/255)
|
2020-03-12 18:15:47 +05:30
|
|
|
|
2023-03-08 23:33:22 +05:30
|
|
|
ren = vtkRenderer()
|
2022-01-16 12:48:06 +05:30
|
|
|
ren.AddActor(actor)
|
2022-02-21 15:49:53 +05:30
|
|
|
if label is None:
|
|
|
|
ren.SetBackground(67/255,128/255,208/255)
|
|
|
|
else:
|
2023-03-08 23:33:22 +05:30
|
|
|
colormap_vtk = vtkScalarBarActor()
|
2022-03-06 03:20:33 +05:30
|
|
|
colormap_vtk.SetLookupTable(lut)
|
|
|
|
colormap_vtk.SetTitle(label)
|
|
|
|
colormap_vtk.SetMaximumWidthInPixels(width//100)
|
|
|
|
ren.AddActor2D(colormap_vtk)
|
2022-02-21 15:49:53 +05:30
|
|
|
ren.SetBackground(0.3,0.3,0.3)
|
2020-03-12 18:15:47 +05:30
|
|
|
|
2023-03-08 23:33:22 +05:30
|
|
|
window = vtkRenderWindow()
|
2020-03-22 21:33:28 +05:30
|
|
|
window.AddRenderer(ren)
|
2021-02-12 02:26:53 +05:30
|
|
|
window.SetSize(width,height)
|
2022-01-16 12:48:06 +05:30
|
|
|
window.SetWindowName(util.execution_stamp('VTK','show'))
|
2020-03-12 18:15:47 +05:30
|
|
|
|
2023-03-08 23:33:22 +05:30
|
|
|
iren = vtkRenderWindowInteractor()
|
2020-03-22 21:33:28 +05:30
|
|
|
iren.SetRenderWindow(window)
|
2022-01-16 12:48:06 +05:30
|
|
|
if os.name == 'posix' and 'DISPLAY' not in os.environ:
|
|
|
|
print('Found no rendering device')
|
|
|
|
else:
|
|
|
|
window.Render()
|
|
|
|
iren.Start()
|