2020-11-18 20:48:57 +05:30
|
|
|
import os
|
2021-06-15 23:08:01 +05:30
|
|
|
import warnings
|
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-01-22 12:35:49 +05:30
|
|
|
from typing import Union, Literal, List
|
2020-03-11 22:37:31 +05:30
|
|
|
|
2020-03-11 04:22:02 +05:30
|
|
|
import numpy as np
|
|
|
|
import vtk
|
2020-03-19 15:29:53 +05:30
|
|
|
from vtk.util.numpy_support import numpy_to_vtk as np_to_vtk
|
|
|
|
from vtk.util.numpy_support import numpy_to_vtkIdTypeArray as np_to_vtkIdTypeArray
|
2020-08-24 10:10:36 +05:30
|
|
|
from vtk.util.numpy_support import vtk_to_numpy as vtk_to_np
|
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
|
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,
|
2022-01-26 20:55:27 +05:30
|
|
|
vtk_data: vtk.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
|
|
|
|
----------
|
2020-08-25 13:26:24 +05:30
|
|
|
vtk_data : subclass of vtk.vtkDataSet
|
|
|
|
Description of geometry and topology, optionally with attached data.
|
2022-01-13 03:43:38 +05:30
|
|
|
Valid types are vtk.vtkImageData, vtk.vtkUnstructuredGrid,
|
|
|
|
vtk.vtkPolyData, and vtk.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
|
|
|
|
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
|
|
|
"""
|
|
|
|
Create VTK of type vtk.vtkImageData.
|
|
|
|
|
|
|
|
This is the common type for grid solver results.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
cells : iterable of int, len (3)
|
|
|
|
Number of cells along each dimension.
|
|
|
|
size : iterable of float, len (3)
|
|
|
|
Physical length along each dimension.
|
|
|
|
origin : iterable of float, len (3), optional
|
|
|
|
Coordinates of grid origin.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
new : damask.VTK
|
|
|
|
VTK-based geometry without nodal or cell data.
|
|
|
|
|
|
|
|
"""
|
|
|
|
vtk_data = vtk.vtkImageData()
|
|
|
|
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_rectilinear_grid(grid: np.ndarray,
|
|
|
|
size: FloatSequence,
|
|
|
|
origin: FloatSequence = np.zeros(3)) -> 'VTK':
|
2020-03-12 11:21:52 +05:30
|
|
|
"""
|
|
|
|
Create VTK of type vtk.vtkRectilinearGrid.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2020-09-10 04:29:40 +05:30
|
|
|
grid : iterable of int, len (3)
|
|
|
|
Number of cells along each dimension.
|
|
|
|
size : iterable of float, len (3)
|
2021-04-24 22:42:44 +05:30
|
|
|
Physical length along each dimension.
|
2020-09-10 04:29:40 +05:30
|
|
|
origin : iterable of float, len (3), optional
|
2021-04-24 22:42:44 +05:30
|
|
|
Coordinates of grid origin.
|
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
|
|
|
"""
|
2021-06-15 23:08:01 +05:30
|
|
|
warnings.warn('Support for vtr files will be removed in DAMASK 3.1.0', DeprecationWarning,2)
|
2020-08-25 13:26:24 +05:30
|
|
|
vtk_data = vtk.vtkRectilinearGrid()
|
2020-09-10 04:29:40 +05:30
|
|
|
vtk_data.SetDimensions(*(np.array(grid)+1))
|
2020-06-28 03:07:09 +05:30
|
|
|
coord = [np_to_vtk(np.linspace(origin[i],origin[i]+size[i],grid[i]+1),deep=True) for i in [0,1,2]]
|
|
|
|
[coord[i].SetName(n) for i,n in enumerate(['x','y','z'])]
|
2020-08-25 13:26:24 +05:30
|
|
|
vtk_data.SetXCoordinates(coord[0])
|
|
|
|
vtk_data.SetYCoordinates(coord[1])
|
|
|
|
vtk_data.SetZCoordinates(coord[2])
|
2020-03-11 12:02:03 +05:30
|
|
|
|
2020-08-25 13:26:24 +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
|
|
|
"""
|
2020-03-12 11:21:52 +05:30
|
|
|
Create VTK of type vtk.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
|
|
|
|
----------
|
|
|
|
nodes : numpy.ndarray of shape (:,3)
|
2020-03-19 12:34:15 +05:30
|
|
|
Spatial position of the nodes.
|
2020-03-12 11:21:52 +05:30
|
|
|
connectivity : numpy.ndarray of np.dtype = int
|
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
|
2020-03-31 14:34:06 +05:30
|
|
|
Name of the vtk.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
|
|
|
"""
|
|
|
|
vtk_nodes = vtk.vtkPoints()
|
2021-09-02 11:34:09 +05:30
|
|
|
vtk_nodes.SetData(np_to_vtk(np.ascontiguousarray(nodes)))
|
2020-03-12 01:26:58 +05:30
|
|
|
cells = vtk.vtkCellArray()
|
|
|
|
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()
|
2020-03-19 15:29:53 +05:30
|
|
|
cells.SetCells(connectivity.shape[0],np_to_vtkIdTypeArray(T,deep=True))
|
2020-03-12 01:26:58 +05:30
|
|
|
|
2020-08-25 13:26:24 +05:30
|
|
|
vtk_data = vtk.vtkUnstructuredGrid()
|
|
|
|
vtk_data.SetPoints(vtk_nodes)
|
2020-12-02 19:07:44 +05:30
|
|
|
cell_types = {'TRIANGLE':vtk.VTK_TRIANGLE, 'QUAD':vtk.VTK_QUAD,
|
|
|
|
'TETRA' :vtk.VTK_TETRA, 'HEXAHEDRON':vtk.VTK_HEXAHEDRON}
|
|
|
|
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
|
|
|
"""
|
|
|
|
Create VTK of type vtk.polyData.
|
|
|
|
|
|
|
|
This is the common type for point-wise data.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
points : numpy.ndarray of 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]
|
2020-03-22 21:33:28 +05:30
|
|
|
vtk_points = vtk.vtkPoints()
|
2021-09-02 11:34:09 +05:30
|
|
|
vtk_points.SetData(np_to_vtk(np.ascontiguousarray(points)))
|
2020-03-12 03:05:58 +05:30
|
|
|
|
2020-10-27 17:49:53 +05:30
|
|
|
vtk_cells = vtk.vtkCellArray()
|
|
|
|
vtk_cells.SetNumberOfCells(N)
|
|
|
|
vtk_cells.SetCells(N,np_to_vtkIdTypeArray(np.stack((np.ones (N,dtype=np.int64),
|
|
|
|
np.arange(N,dtype=np.int64)),axis=1).ravel(),deep=True))
|
|
|
|
|
2020-08-25 13:26:24 +05:30
|
|
|
vtk_data = vtk.vtkPolyData()
|
|
|
|
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
|
|
|
|
2020-03-12 04:24:36 +05:30
|
|
|
@staticmethod
|
2022-01-17 19:30:25 +05:30
|
|
|
def load(fname: Union[str, Path],
|
2022-01-22 12:35:49 +05:30
|
|
|
dataset_type: Literal['ImageData', 'UnstructuredGrid', 'PolyData'] = 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
|
2022-01-22 12:35:49 +05:30
|
|
|
Filename for reading.
|
|
|
|
Valid extensions are .vti, .vtr, .vtu, .vtp, and .vtk.
|
|
|
|
dataset_type : {'ImageData', 'UnstructuredGrid', 'PolyData'}, optional
|
2021-04-24 10:43:36 +05:30
|
|
|
Name of the vtk.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-01-22 12:35:49 +05:30
|
|
|
if not os.path.isfile(fname): # vtk has a strange error handling
|
2021-04-24 22:42:44 +05:30
|
|
|
raise FileNotFoundError(f'No such file: {fname}')
|
2022-01-29 22:46:19 +05:30
|
|
|
if (ext := Path(fname).suffix) == '.vtk' or dataset_type is not None:
|
2020-03-12 04:24:36 +05:30
|
|
|
reader = vtk.vtkGenericDataObjectReader()
|
2020-06-28 10:47:51 +05:30
|
|
|
reader.SetFileName(str(fname))
|
2020-03-21 15:37:21 +05:30
|
|
|
if dataset_type is None:
|
2022-02-22 20:16: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(('rectilineargrid','rectilinear_grid')):
|
2020-08-25 11:19:56 +05:30
|
|
|
reader.Update()
|
2020-08-25 13:26:24 +05:30
|
|
|
vtk_data = reader.GetRectilinearGridOutput()
|
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()
|
2020-03-12 04:24:36 +05:30
|
|
|
else:
|
2022-02-22 20:16: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':
|
|
|
|
reader = vtk.vtkXMLImageDataReader()
|
|
|
|
elif ext == '.vtr':
|
2020-03-12 04:24:36 +05:30
|
|
|
reader = vtk.vtkXMLRectilinearGridReader()
|
|
|
|
elif ext == '.vtu':
|
|
|
|
reader = vtk.vtkXMLUnstructuredGridReader()
|
|
|
|
elif ext == '.vtp':
|
|
|
|
reader = vtk.vtkXMLPolyDataReader()
|
|
|
|
else:
|
2022-02-22 20:16:12 +05:30
|
|
|
raise TypeError(f'Unknown file extension "{ext}"')
|
2020-03-12 04:24:36 +05:30
|
|
|
|
2020-06-28 10:47:51 +05:30
|
|
|
reader.SetFileName(str(fname))
|
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
|
|
|
|
|
|
|
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
|
2020-03-19 12:34:15 +05:30
|
|
|
Filename for writing.
|
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
|
|
|
|
|
|
|
"""
|
2021-06-15 20:32:02 +05:30
|
|
|
if isinstance(self.vtk_data,vtk.vtkImageData):
|
|
|
|
writer = vtk.vtkXMLImageDataWriter()
|
|
|
|
elif isinstance(self.vtk_data,vtk.vtkRectilinearGrid):
|
2020-03-11 12:02:03 +05:30
|
|
|
writer = vtk.vtkXMLRectilinearGridWriter()
|
2020-08-25 13:26:24 +05:30
|
|
|
elif isinstance(self.vtk_data,vtk.vtkUnstructuredGrid):
|
2020-03-12 01:26:58 +05:30
|
|
|
writer = vtk.vtkXMLUnstructuredGridWriter()
|
2020-08-25 13:26:24 +05:30
|
|
|
elif isinstance(self.vtk_data,vtk.vtkPolyData):
|
2020-03-11 12:02:03 +05:30
|
|
|
writer = vtk.vtkXMLPolyDataWriter()
|
|
|
|
|
2020-11-04 22:38:04 +05:30
|
|
|
default_ext = '.'+writer.GetDefaultFileExtension()
|
2020-06-03 14:13:07 +05:30
|
|
|
ext = Path(fname).suffix
|
2020-11-06 02:08:00 +05:30
|
|
|
writer.SetFileName(str(fname)+(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
|
2021-04-01 20:59:12 +05:30
|
|
|
# Needs support for damask.Table
|
2022-01-26 20:55:27 +05:30
|
|
|
def add(self,
|
|
|
|
data: Union[np.ndarray, np.ma.MaskedArray],
|
|
|
|
label: str = None):
|
2020-08-24 10:10:36 +05:30
|
|
|
"""
|
|
|
|
Add data to either cells or points.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2021-04-02 11:37:22 +05:30
|
|
|
data : numpy.ndarray or numpy.ma.MaskedArray
|
2020-09-10 04:29:40 +05:30
|
|
|
Data to add. First dimension needs to match either
|
|
|
|
number of cells or number of points.
|
2020-08-24 10:10:36 +05:30
|
|
|
label : str
|
|
|
|
Data label.
|
|
|
|
|
|
|
|
"""
|
2020-08-25 13:26:24 +05:30
|
|
|
N_points = self.vtk_data.GetNumberOfPoints()
|
|
|
|
N_cells = self.vtk_data.GetNumberOfCells()
|
2020-03-12 04:24:36 +05:30
|
|
|
|
2020-09-10 04:29:40 +05:30
|
|
|
if isinstance(data,np.ndarray):
|
2020-03-21 15:37:21 +05:30
|
|
|
if label is None:
|
2020-03-19 23:13:49 +05:30
|
|
|
raise ValueError('No label defined for numpy.ndarray')
|
2020-06-26 15:15:54 +05:30
|
|
|
|
2020-09-10 04:40:03 +05:30
|
|
|
N_data = data.shape[0]
|
2021-04-25 00:54:26 +05:30
|
|
|
data_ = (data if not isinstance(data,np.ma.MaskedArray) else
|
|
|
|
np.where(data.mask,data.fill_value,data)).reshape(N_data,-1)
|
|
|
|
|
|
|
|
if data_.dtype in [np.double,np.longdouble]:
|
|
|
|
d = np_to_vtk(data_.astype(np.single),deep=True) # avoid large files
|
|
|
|
elif data_.dtype.type is np.str_:
|
|
|
|
d = vtk.vtkStringArray()
|
|
|
|
for s in np.squeeze(data_):
|
|
|
|
d.InsertNextValue(s)
|
|
|
|
else:
|
|
|
|
d = np_to_vtk(data_,deep=True)
|
|
|
|
|
2020-03-12 04:24:36 +05:30
|
|
|
d.SetName(label)
|
2020-06-26 15:15:54 +05:30
|
|
|
|
2020-10-27 17:49:53 +05:30
|
|
|
if N_data == N_points:
|
2020-08-25 13:26:24 +05:30
|
|
|
self.vtk_data.GetPointData().AddArray(d)
|
2020-10-27 17:49:53 +05:30
|
|
|
elif N_data == N_cells:
|
|
|
|
self.vtk_data.GetCellData().AddArray(d)
|
2020-06-26 15:15:54 +05:30
|
|
|
else:
|
2022-02-22 20:16:12 +05:30
|
|
|
raise ValueError(f'Cell / point count ({N_cells} / {N_points}) differs from data ({N_data})')
|
2020-03-13 00:22:33 +05:30
|
|
|
elif isinstance(data,Table):
|
2020-03-19 23:13:49 +05:30
|
|
|
raise NotImplementedError('damask.Table')
|
2020-03-19 13:33:38 +05:30
|
|
|
else:
|
|
|
|
raise TypeError
|
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()
|
2020-08-25 11:19:56 +05:30
|
|
|
for a in range(cell_data.GetNumberOfArrays()):
|
|
|
|
if cell_data.GetArrayName(a) == label:
|
2021-04-25 00:54:26 +05:30
|
|
|
try:
|
|
|
|
return vtk_to_np(cell_data.GetArray(a))
|
|
|
|
except AttributeError:
|
|
|
|
vtk_array = cell_data.GetAbstractArray(a) # string array
|
2020-08-24 10:10:36 +05:30
|
|
|
|
2020-08-25 13:26:24 +05:30
|
|
|
point_data = self.vtk_data.GetPointData()
|
2020-08-25 11:19:56 +05:30
|
|
|
for a in range(point_data.GetNumberOfArrays()):
|
|
|
|
if point_data.GetArrayName(a) == label:
|
2021-04-25 00:54:26 +05:30
|
|
|
try:
|
|
|
|
return vtk_to_np(point_data.GetArray(a))
|
|
|
|
except AttributeError:
|
|
|
|
vtk_array = point_data.GetAbstractArray(a) # string array
|
|
|
|
|
|
|
|
try:
|
|
|
|
# string array
|
|
|
|
return np.array([vtk_array.GetValue(i) for i in range(vtk_array.GetNumberOfValues())]).astype(str)
|
|
|
|
except UnboundLocalError:
|
2022-02-22 20:16:12 +05:30
|
|
|
raise ValueError(f'Array "{label}" not found')
|
2020-08-24 10:10:36 +05:30
|
|
|
|
|
|
|
|
2022-01-17 19:30:25 +05:30
|
|
|
def get_comments(self) -> List[str]:
|
2020-08-24 02:52:53 +05:30
|
|
|
"""Return the comments."""
|
2020-08-25 13:26:24 +05:30
|
|
|
fielddata = self.vtk_data.GetFieldData()
|
2020-08-24 02:52:53 +05:30
|
|
|
for a in range(fielddata.GetNumberOfArrays()):
|
|
|
|
if fielddata.GetArrayName(a) == 'comments':
|
|
|
|
comments = fielddata.GetAbstractArray(a)
|
|
|
|
return [comments.GetValue(i) for i in range(comments.GetNumberOfValues())]
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
2022-01-26 20:55:27 +05:30
|
|
|
def set_comments(self,
|
|
|
|
comments: Union[str, List[str]]):
|
2020-08-24 02:52:53 +05:30
|
|
|
"""
|
2020-08-25 04:10:14 +05:30
|
|
|
Set comments.
|
2020-08-24 02:52:53 +05:30
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
comments : str or list of str
|
2020-08-24 10:10:36 +05:30
|
|
|
Comments.
|
2020-08-24 02:52:53 +05:30
|
|
|
|
|
|
|
"""
|
|
|
|
s = vtk.vtkStringArray()
|
|
|
|
s.SetName('comments')
|
|
|
|
for c in [comments] if isinstance(comments,str) else comments:
|
|
|
|
s.InsertNextValue(c)
|
2020-08-25 13:26:24 +05:30
|
|
|
self.vtk_data.GetFieldData().AddArray(s)
|
2020-08-24 02:52:53 +05:30
|
|
|
|
|
|
|
|
2022-01-26 20:55:27 +05:30
|
|
|
def add_comments(self,
|
|
|
|
comments: Union[str, List[str]]):
|
2020-08-24 10:10:36 +05:30
|
|
|
"""
|
2020-08-25 04:10:14 +05:30
|
|
|
Add comments.
|
2020-08-24 10:10:36 +05:30
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
comments : str or list of str
|
|
|
|
Comments to add.
|
|
|
|
|
|
|
|
"""
|
2020-08-25 04:10:14 +05:30
|
|
|
self.set_comments(self.get_comments() + ([comments] if isinstance(comments,str) else comments))
|
2020-08-24 10:10:36 +05:30
|
|
|
|
|
|
|
|
2022-01-17 19:30:25 +05:30
|
|
|
def __repr__(self) -> str:
|
2020-03-11 12:02:03 +05:30
|
|
|
"""ASCII representation of the VTK data."""
|
|
|
|
writer = vtk.vtkDataSetWriter()
|
2020-08-25 00:20:40 +05:30
|
|
|
writer.SetHeader(f'# {util.execution_stamp("VTK")}')
|
2020-03-11 12:02:03 +05:30
|
|
|
writer.WriteToOutputStringOn()
|
2020-08-25 13:26:24 +05:30
|
|
|
writer.SetInputData(self.vtk_data)
|
2020-03-11 12:02:03 +05:30
|
|
|
writer.Write()
|
|
|
|
return writer.GetOutputString()
|
2020-08-24 04:04:07 +05:30
|
|
|
|
2020-03-12 18:15:47 +05:30
|
|
|
|
2022-01-22 12:35:49 +05:30
|
|
|
def show(self):
|
2020-03-12 18:15:47 +05:30
|
|
|
"""
|
|
|
|
Render.
|
|
|
|
|
|
|
|
See http://compilatrix.com/article/vtk-1 for further ideas.
|
|
|
|
"""
|
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
|
|
|
|
|
2020-03-12 18:15:47 +05:30
|
|
|
mapper = vtk.vtkDataSetMapper()
|
2020-08-25 13:26:24 +05:30
|
|
|
mapper.SetInputData(self.vtk_data)
|
2022-01-16 12:48:06 +05:30
|
|
|
|
2020-03-12 18:15:47 +05:30
|
|
|
actor = vtk.vtkActor()
|
|
|
|
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
|
|
|
|
|
|
|
ren = vtk.vtkRenderer()
|
2022-01-16 12:48:06 +05:30
|
|
|
ren.AddActor(actor)
|
|
|
|
ren.SetBackground(67/255,128/255,208/255)
|
2020-03-12 18:15:47 +05:30
|
|
|
|
2020-03-22 21:33:28 +05:30
|
|
|
window = vtk.vtkRenderWindow()
|
|
|
|
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
|
|
|
|
|
|
|
iren = vtk.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()
|