2020-03-11 12:02:03 +05:30
|
|
|
import os
|
2020-03-11 22:37:31 +05:30
|
|
|
|
|
|
|
import pandas as pd
|
2020-03-11 04:22:02 +05:30
|
|
|
import numpy as np
|
|
|
|
import vtk
|
2020-03-12 01:26:58 +05:30
|
|
|
from vtk.util import numpy_support as nps
|
2020-03-11 22:37:31 +05:30
|
|
|
|
|
|
|
from . import table
|
|
|
|
from . import version
|
2020-03-11 04:22:02 +05:30
|
|
|
|
|
|
|
class VTK: # capitals needed/preferred?
|
|
|
|
"""
|
|
|
|
Manage vtk files.
|
|
|
|
|
|
|
|
tbd
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self,geom):
|
|
|
|
"""tbd."""
|
|
|
|
self.geom = geom
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def from_rectilinearGrid(grid,size,origin=np.zeros(3)):
|
2020-03-11 12:02:03 +05:30
|
|
|
"""Check https://blog.kitware.com/ghost-and-blanking-visibility-changes/ for missing data."""
|
2020-03-11 04:22:02 +05:30
|
|
|
coordArray = [vtk.vtkDoubleArray(),vtk.vtkDoubleArray(),vtk.vtkDoubleArray()]
|
|
|
|
for dim in [0,1,2]:
|
2020-03-12 01:26:58 +05:30
|
|
|
for c in np.linspace(origin[dim],origin[dim]+size[dim],grid[dim]):
|
2020-03-11 04:22:02 +05:30
|
|
|
coordArray[dim].InsertNextValue(c)
|
|
|
|
|
|
|
|
geom = vtk.vtkRectilinearGrid()
|
2020-03-12 01:26:58 +05:30
|
|
|
geom.SetDimensions(*grid)
|
2020-03-11 04:22:02 +05:30
|
|
|
geom.SetXCoordinates(coordArray[0])
|
|
|
|
geom.SetYCoordinates(coordArray[1])
|
|
|
|
geom.SetZCoordinates(coordArray[2])
|
2020-03-11 12:02:03 +05:30
|
|
|
|
2020-03-11 04:22:02 +05:30
|
|
|
return VTK(geom)
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
2020-03-12 01:26:58 +05:30
|
|
|
def from_unstructuredGrid(nodes,connectivity,cell_type):
|
|
|
|
"""
|
|
|
|
Create an unstructured grid (mesh).
|
|
|
|
|
2020-03-12 03:05:58 +05:30
|
|
|
connectivity: 0 based at the moment, shape Ncell x N nodes
|
2020-03-12 01:26:58 +05:30
|
|
|
cell_type: TRIANGLE, 'QUAD', 'TETRA','HEXAHEDRON'
|
|
|
|
|
|
|
|
"""
|
|
|
|
vtk_nodes = vtk.vtkPoints()
|
|
|
|
vtk_nodes.SetData(nps.numpy_to_vtk(nodes))
|
|
|
|
cells = vtk.vtkCellArray()
|
|
|
|
cells.SetNumberOfCells(connectivity.shape[0])
|
|
|
|
T = np.concatenate((np.ones((connectivity.shape[0],1),dtype=np.int64)*connectivity.shape[1],
|
|
|
|
connectivity),axis=1).ravel()
|
|
|
|
cells.SetCells(connectivity.shape[0],nps.numpy_to_vtk(T, deep=True, array_type=vtk.VTK_ID_TYPE))
|
|
|
|
|
2020-03-11 04:22:02 +05:30
|
|
|
geom = vtk.vtkUnstructuredGrid()
|
2020-03-12 01:26:58 +05:30
|
|
|
geom.SetPoints(vtk_nodes)
|
|
|
|
geom.SetCells(eval('vtk.VTK_{}'.format(cell_type.upper())),cells)
|
2020-03-11 04:22:02 +05:30
|
|
|
|
|
|
|
return VTK(geom)
|
|
|
|
|
2020-03-11 12:02:03 +05:30
|
|
|
|
2020-03-12 01:26:58 +05:30
|
|
|
@staticmethod
|
2020-03-12 03:05:58 +05:30
|
|
|
def from_polyData(points):
|
|
|
|
vtk_points= vtk.vtkPoints()
|
|
|
|
vtk_points.SetData(nps.numpy_to_vtk(points))
|
|
|
|
|
|
|
|
vertices = vtk.vtkCellArray()
|
|
|
|
vertices.SetNumberOfCells(points.shape[0])
|
|
|
|
T = np.concatenate((np.ones((points.shape[0],1),dtype=np.int64),
|
|
|
|
np.arange(points.shape[0],dtype=np.int64).reshape(-1,1)),axis=1).ravel()
|
|
|
|
vertices.SetCells(points.shape[0],nps.numpy_to_vtk(T, deep=True, array_type=vtk.VTK_ID_TYPE))
|
|
|
|
|
|
|
|
geom = vtk.vtkPolyData()
|
|
|
|
geom.SetPoints(vtk_points)
|
|
|
|
geom.SetVerts(vertices)
|
|
|
|
|
|
|
|
return VTK(geom)
|
2020-03-12 01:26:58 +05:30
|
|
|
|
|
|
|
|
2020-03-11 12:02:03 +05:30
|
|
|
def write(self,fname): #ToDo: Discuss how to handle consistently filename extensions
|
2020-03-11 12:20:11 +05:30
|
|
|
if (isinstance(self.geom,vtk.vtkRectilinearGrid)):
|
2020-03-11 12:02:03 +05:30
|
|
|
writer = vtk.vtkXMLRectilinearGridWriter()
|
2020-03-11 12:20:11 +05:30
|
|
|
elif(isinstance(self.geom,vtk.vtkUnstructuredGrid)):
|
2020-03-12 01:26:58 +05:30
|
|
|
writer = vtk.vtkXMLUnstructuredGridWriter()
|
2020-03-11 12:20:11 +05:30
|
|
|
elif(isinstance(self.geom,vtk.vtkPolyData)):
|
2020-03-11 12:02:03 +05:30
|
|
|
writer = vtk.vtkXMLPolyDataWriter()
|
|
|
|
|
|
|
|
writer.SetFileName('{}.{}'.format(os.path.splitext(fname)[0],
|
|
|
|
writer.GetDefaultFileExtension()))
|
|
|
|
writer.SetCompressorTypeToZLib()
|
|
|
|
writer.SetDataModeToBinary()
|
|
|
|
writer.SetInputData(self.geom)
|
|
|
|
|
|
|
|
writer.Write()
|
|
|
|
|
2020-03-11 22:37:31 +05:30
|
|
|
|
|
|
|
def add(data,label=None):
|
|
|
|
if isinstance(data,np.ndarray):
|
|
|
|
pass
|
|
|
|
elif isinstance(data,pd.DataFrame):
|
|
|
|
pass
|
|
|
|
elif isinstance(data,table):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2020-03-11 12:02:03 +05:30
|
|
|
def __repr__(self):
|
|
|
|
"""ASCII representation of the VTK data."""
|
|
|
|
writer = vtk.vtkDataSetWriter()
|
2020-03-12 03:05:58 +05:30
|
|
|
writer.SetHeader('# DAMASK.VTK v{}'.format(version))
|
2020-03-11 12:02:03 +05:30
|
|
|
writer.WriteToOutputStringOn()
|
|
|
|
writer.SetInputData(self.geom)
|
|
|
|
writer.Write()
|
|
|
|
return writer.GetOutputString()
|