using central functionality

This commit is contained in:
Martin Diehl 2020-03-11 07:32:03 +01:00
parent b3e8a4405e
commit 32734e7dce
2 changed files with 48 additions and 40 deletions

View File

@ -6,6 +6,7 @@ from scipy import ndimage
import vtk import vtk
from vtk.util import numpy_support from vtk.util import numpy_support
from . import VTK
from . import util from . import util
from . import version from . import version
@ -382,27 +383,8 @@ class Geom():
vtk file to write. If no file is given, a string is returned. vtk file to write. If no file is given, a string is returned.
""" """
grid = self.get_grid() + np.ones(3,dtype=int) v = VTK.from_rectilinearGrid(self.grid,self.size,self.origin)
size = self.get_size() rGrid = v.geom
origin = self.get_origin()
coords = [
np.linspace(0,size[0],grid[0]) + origin[0],
np.linspace(0,size[1],grid[1]) + origin[1],
np.linspace(0,size[2],grid[2]) + origin[2]
]
rGrid = vtk.vtkRectilinearGrid()
coordArray = [vtk.vtkDoubleArray(),vtk.vtkDoubleArray(),vtk.vtkDoubleArray()]
rGrid.SetDimensions(*grid)
for d,coord in enumerate(coords):
for c in coord:
coordArray[d].InsertNextValue(c)
rGrid.SetXCoordinates(coordArray[0])
rGrid.SetYCoordinates(coordArray[1])
rGrid.SetZCoordinates(coordArray[2])
ms = numpy_support.numpy_to_vtk(num_array=self.microstructure.flatten(order='F'), ms = numpy_support.numpy_to_vtk(num_array=self.microstructure.flatten(order='F'),
array_type=vtk.VTK_INT if self.microstructure.dtype == int else vtk.VTK_FLOAT) array_type=vtk.VTK_INT if self.microstructure.dtype == int else vtk.VTK_FLOAT)
@ -431,7 +413,7 @@ class Geom():
writer.SetInputData(rGrid) writer.SetInputData(rGrid)
writer.Write() writer.Write()
if fname is None: return writer.GetOutputString() if not fname: return writer.GetOutputString()
def show(self): def show(self):

View File

@ -1,5 +1,7 @@
import os
import numpy as np import numpy as np
import vtk import vtk
import vtkmodules
#from vtk.util import numpy_support #from vtk.util import numpy_support
class VTK: # capitals needed/preferred? class VTK: # capitals needed/preferred?
@ -15,6 +17,7 @@ class VTK: # capitals needed/preferred?
@staticmethod @staticmethod
def from_rectilinearGrid(grid,size,origin=np.zeros(3)): def from_rectilinearGrid(grid,size,origin=np.zeros(3)):
"""Check https://blog.kitware.com/ghost-and-blanking-visibility-changes/ for missing data."""
coordArray = [vtk.vtkDoubleArray(),vtk.vtkDoubleArray(),vtk.vtkDoubleArray()] coordArray = [vtk.vtkDoubleArray(),vtk.vtkDoubleArray(),vtk.vtkDoubleArray()]
for dim in [0,1,2]: for dim in [0,1,2]:
for c in np.linspace(0,size[dim],1+grid[dim]): for c in np.linspace(0,size[dim],1+grid[dim]):
@ -53,5 +56,28 @@ class VTK: # capitals needed/preferred?
return VTK(geom) return VTK(geom)
def write(self,fname):
print('tbd',fname) def write(self,fname): #ToDo: Discuss how to handle consistently filename extensions
if (isinstance(self.geom,vtkmodules.vtkCommonDataModel.vtkRectilinearGrid)):
writer = vtk.vtkXMLRectilinearGridWriter()
elif(isinstance(self.geom,vtkmodules.vtkCommonDataModel.vtkUnstructuredGrid)):
writer = vtk.vtkUnstructuredGrid()
elif(isinstance(self.geom,vtkmodules.vtkCommonDataModel.vtkPolyData)):
writer = vtk.vtkXMLPolyDataWriter()
writer.SetFileName('{}.{}'.format(os.path.splitext(fname)[0],
writer.GetDefaultFileExtension()))
writer.SetCompressorTypeToZLib()
writer.SetDataModeToBinary()
writer.SetInputData(self.geom)
writer.Write()
def __repr__(self):
"""ASCII representation of the VTK data."""
writer = vtk.vtkDataSetWriter()
#writer.SetHeader('damask.Geom '+version)
writer.WriteToOutputStringOn()
writer.SetInputData(self.geom)
writer.Write()
return writer.GetOutputString()