1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
|
"""This module provides classes that allow numpy style access
to VTK datasets. See examples at bottom.
"""
from contextlib import suppress
from vtkmodules.vtkCommonCore import vtkPoints, vtkAbstractArray, vtkDataArray
from vtkmodules.vtkCommonDataModel import (
vtkCellArray,
vtkDataObject,
vtkFieldData,
vtkDataSetAttributes,
vtkPointData,
vtkCellData,
vtkDataObject,
vtkImageData,
vtkMultiBlockDataSet,
vtkPolyData,
vtkStructuredGrid,
vtkRectilinearGrid,
vtkUnstructuredGrid,
vtkOverlappingAMR,
vtkPartitionedDataSet,
vtkPartitionedDataSetCollection,
)
import weakref
NUMPY_AVAILABLE = False
with suppress(ImportError):
import numpy
from vtkmodules.numpy_interface import dataset_adapter as dsa
NUMPY_AVAILABLE = True
class FieldDataBase(object):
def __init__(self):
self.association = None
self.dataset = None
def __getitem__(self, idx):
"""Implements the [] operator. Accepts an array name or index."""
return self.get_array(idx)
def __setitem__(self, name, value):
"""Implements the [] operator. Accepts an array name or index."""
return self.set_array(name, value)
def get_array(self, idx):
"Given an index or name, returns a VTKArray."
if isinstance(idx, int) and idx >= self.GetNumberOfArrays():
raise IndexError("array index out of range")
vtkarray = super().GetArray(idx)
if not NUMPY_AVAILABLE:
return vtkarray if vtkarray else self.GetAbstractArray(idx)
if not vtkarray:
vtkarray = self.GetAbstractArray(idx)
if vtkarray:
return vtkarray
return dsa.NoneArray
array = dsa.vtkDataArrayToVTKArray(vtkarray, self.dataset)
array.Association = self.association
return array
def __contains__(self, aname):
"""Returns true if the container contains arrays
with the given name, false otherwise"""
return self.HasArray(aname)
def keys(self):
"""Returns the names of the arrays as a list."""
kys = []
narrays = self.GetNumberOfArrays()
for i in range(narrays):
name = self.GetAbstractArray(i).GetName()
if name:
kys.append(name)
return tuple(kys)
def values(self):
"""Returns the arrays as a tuple."""
vals = []
narrays = self.GetNumberOfArrays()
for i in range(narrays):
a = self.get_array(i)
if a.GetName():
vals.append(a)
return tuple(vals)
def items(self):
"""Returns a tuple of pairs (name, array)"""
pairs = []
narrays = self.GetNumberOfArrays()
for i in range(narrays):
arr = self.get_array(i)
name = arr.GetName()
if name:
pairs.append((name, arr))
return tuple(pairs)
def set_array(self, name, narray):
"""Appends a new array to the dataset attributes."""
if not NUMPY_AVAILABLE:
if isinstance(narray, vtkAbstractArray):
narray.SetName(name)
self.AddArray(narray)
return
if narray is dsa.NoneArray:
# if NoneArray, nothing to do.
return
if self.association == vtkDataObject.POINT:
arrLength = self.dataset.GetNumberOfPoints()
elif self.association == vtkDataObject.CELL:
arrLength = self.dataset.GetNumberOfCells()
elif (
self.association == vtkDataObject.ROW
and self.dataset.GetNumberOfColumns() > 0
):
arrLength = self.dataset.GetNumberOfRows()
else:
if not isinstance(narray, numpy.ndarray):
arrLength = 1
else:
arrLength = narray.shape[0]
# Fixup input array length:
if (
not isinstance(narray, numpy.ndarray) or numpy.ndim(narray) == 0
): # Scalar input
dtype = narray.dtype if isinstance(narray, numpy.ndarray) else type(narray)
tmparray = numpy.empty(arrLength, dtype=dtype)
tmparray.fill(narray)
narray = tmparray
elif narray.shape[0] != arrLength: # Vector input
components = 1
for l in narray.shape:
components *= l
tmparray = numpy.empty((arrLength, components), dtype=narray.dtype)
tmparray[:] = narray.flatten()
narray = tmparray
shape = narray.shape
if len(shape) == 3:
# Array of matrices. We need to make sure the order in memory is right.
# If column order (c order), transpose. VTK wants row order (fortran
# order). The deep copy later will make sure that the array is contiguous.
# If row order but not contiguous, transpose so that the deep copy below
# does not happen.
size = narray.dtype.itemsize
if (narray.strides[1] / size == 3 and narray.strides[2] / size == 1) or (
narray.strides[1] / size == 1
and narray.strides[2] / size == 3
and not narray.flags.contiguous
):
narray = narray.transpose(0, 2, 1)
# If array is not contiguous, make a deep copy that is contiguous
if not narray.flags.contiguous:
narray = numpy.ascontiguousarray(narray)
# Flatten array of matrices to array of vectors
if len(shape) == 3:
narray = narray.reshape(shape[0], shape[1] * shape[2])
# this handle the case when an input array is directly appended on the
# output. We want to make sure that the array added to the output is not
# referring to the input dataset.
copy = dsa.VTKArray(narray)
try:
copy.VTKObject = narray.VTKObject
except AttributeError:
pass
arr = dsa.numpyTovtkDataArray(copy, name)
self.AddArray(arr)
def __eq__(self, other: object) -> bool:
"""Test dict-like equivalency."""
# here we check if other is the same class or a subclass of self.
if not isinstance(other, type(self)):
return False
if self is other:
return True
"""
If numpy is not available, only check for identity without comparing contents of the data arrays
"""
if not NUMPY_AVAILABLE:
return False
if set(self.keys()) != set(other.keys()):
return False
# verify the value of the arrays
for key, value in other.items():
if not numpy.array_equal(value, self[key]):
return False
return True
def __iter__(self):
return iter(self.keys())
def __len__(self):
return self.GetNumberOfArrays()
@vtkFieldData.override
class FieldData(FieldDataBase, vtkFieldData):
pass
class DataSetAttributesBase(FieldDataBase):
pass
@vtkDataSetAttributes.override
class DataSetAttributes(DataSetAttributesBase, vtkDataSetAttributes):
def __eq__(self, other: object) -> bool:
"""Test dict-like equivalency."""
if not super().__eq__(other):
return False
for attr in [
"GetScalars",
"GetVectors",
"GetNormals",
"GetTangents",
"GetTCoords",
"GetTensors",
"GetGlobalIds",
"GetPedigreeIds",
"GetRationalWeights",
"GetHigherOrderDegrees",
"GetProcessIds",
]:
self_attr = getattr(self, attr)()
other_attr = getattr(other, attr)()
if self_attr and other_attr:
if self_attr.GetName() != other_attr.GetName():
return False
elif self_attr != other_attr:
return False
return True
@vtkPointData.override
class PointData(DataSetAttributesBase, vtkPointData):
pass
@vtkCellData.override
class CellData(DataSetAttributesBase, vtkCellData):
pass
class CompositeDataSetAttributesIterator(object):
def __init__(self, cdsa):
self._cdsa = cdsa
if cdsa:
self._itr = iter(cdsa.keys())
else:
self._itr = None
def __iter__(self):
return self
def __next__(self):
if not self._cdsa:
raise StopIteration
name = next(self._itr)
return self._cdsa[name]
def next(self):
return self.__next__()
class CompositeDataSetAttributes(object):
"""This is a python friendly wrapper for vtkDataSetAttributes for composite
datasets. Since composite datasets themselves don't have attribute data, but
the attribute data is associated with the leaf nodes in the composite
dataset, this class simulates a DataSetAttributes interface by taking a
union of DataSetAttributes associated with all leaf nodes."""
def __init__(self, dataset, association):
self.DataSet = dataset
self.Association = association
self.ArrayNames = []
self.Arrays = {}
# build the set of arrays available in the composite dataset. Since
# composite datasets can have partial arrays, we need to iterate over
# all non-null blocks in the dataset.
self.__determine_arraynames()
def __determine_arraynames(self):
array_set = set()
array_list = []
for dataset in self.DataSet:
dsa = dataset.GetAttributesAsFieldData(self.Association)
for array_name in dsa.keys():
if array_name not in array_set:
array_set.add(array_name)
array_list.append(array_name)
self.ArrayNames = array_list
def modified(self):
"""Rescans the contained dataset to update the
internal list of arrays."""
self.__determine_arraynames()
def __contains__(self, aname):
"""Returns true if the container contains arrays
with the given name, false otherwise"""
return aname in self.ArrayNames
def keys(self):
"""Returns the names of the arrays as a tuple."""
return tuple(self.ArrayNames)
def values(self):
"""Returns all the arrays as a tuple."""
arrays = []
for array in self:
arrays.append(array)
return tuple(arrays)
def items(self):
"""Returns (name, array) pairs as a tuple."""
items = []
for name in self.keys():
items.append((name, self[name]))
return tuple(items)
def __getitem__(self, idx):
"""Implements the [] operator. Accepts an array name."""
return self.get_array(idx)
def __setitem__(self, name, narray):
"""Implements the [] operator. Accepts an array name."""
return self.set_array(name, narray)
def set_array(self, name, narray):
"""Appends a new array to the composite dataset attributes."""
if not NUMPY_AVAILABLE:
# don't know how to handle composite dataset attribute when numpy not around
raise NotImplementedError("Only available with numpy")
if narray is dsa.NoneArray:
# if NoneArray, nothing to do.
return
added = False
if not isinstance(narray, dsa.VTKCompositeDataArray): # Scalar input
for ds in self.DataSet:
ds.GetAttributesAsFieldData(self.Association).set_array(name, narray)
added = True
if added:
self.ArrayNames.append(name)
# don't add the narray since it's a scalar. GetArray() will create a
# VTKCompositeArray on-demand.
else:
for ds, array in zip(self.DataSet, narray.Arrays):
if array is not None:
ds.GetAttributesAsFieldData(self.Association).set_array(name, array)
added = True
if added:
self.ArrayNames.append(name)
self.Arrays[name] = weakref.ref(narray)
def get_array(self, idx):
"""Given a name, returns a VTKCompositeArray."""
arrayname = idx
if not NUMPY_AVAILABLE:
# don't know how to handle composite dataset attribute when numpy not around
raise NotImplementedError("Only available with numpy")
if arrayname not in self.ArrayNames:
return dsa.NoneArray
if arrayname not in self.Arrays or self.Arrays[arrayname]() is None:
array = dsa.VTKCompositeDataArray(
dataset=self.DataSet, name=arrayname, association=self.Association
)
self.Arrays[arrayname] = weakref.ref(array)
else:
array = self.Arrays[arrayname]()
return array
def __iter__(self):
"""Iterators on keys"""
return iter(self.ArrayNames)
def __len__(self):
return len(self.ArrayNames)
# class DataSet(DataObjectBase):
class DataSet(object):
def __init__(self, **kwargs) -> None:
self._numpy_attrs = []
@property
def point_data(self):
pd = super().GetPointData()
pd.dataset = self
pd.association = self.POINT
return pd
@property
def cell_data(self):
cd = super().GetCellData()
cd.dataset = self
cd.association = self.CELL
return cd
@property
def field_data(self):
fd = super().GetFieldData()
if fd:
fd.dataset = self
fd.association = self.FIELD
return fd
def __eq__(self, other: object) -> bool:
"""Test equivalency between data objects."""
if not isinstance(self, type(other)):
return False
if self is other:
return True
"""
If numpy is not available, only check for identity without comparing contents of the data arrays
"""
if not NUMPY_AVAILABLE:
return False
for attr in self._numpy_attrs:
if hasattr(self, attr):
if not numpy.array_equal(getattr(self, attr), getattr(other, attr)):
return False
for attr in ["field_data", "point_data", "cell_data"]:
if getattr(self, attr) != getattr(other, attr):
return False
return True
def convert_to_unstructured_grid(self):
from vtkmodules.vtkFiltersCore import vtkExtractCells
ecells = vtkExtractCells()
ecells.SetInputData(self)
ecells.ExtractAllCellsOn()
ecells.Update()
return ecells.GetOutput()
class PointSet(DataSet):
def __init__(self, **kwargs) -> None:
DataSet.__init__(self, **kwargs)
self._numpy_attrs.append("points")
@property
def points(self):
pts = self.GetPoints()
if not NUMPY_AVAILABLE:
return pts
if not pts or not pts.GetData():
return None
return dsa.vtkDataArrayToVTKArray(pts.GetData())
@points.setter
def points(self, points):
if isinstance(points, vtkPoints):
self.SetPoints(points)
return
if not NUMPY_AVAILABLE:
raise ValueError("Expect vtkPoints")
pts = dsa.numpyTovtkDataArray(points, "points")
vtkpts = vtkPoints()
vtkpts.SetData(pts)
self.SetPoints(vtkpts)
@vtkUnstructuredGrid.override
class UnstructuredGrid(PointSet, vtkUnstructuredGrid):
def __init__(self, **kwargs):
PointSet.__init__(self, **kwargs)
vtkUnstructuredGrid.__init__(self, **kwargs)
@property
def cells(self):
ca = self.GetCells()
conn_vtk = ca.GetConnectivityArray()
offsets_vtk = ca.GetOffsetsArray()
ct_vtk = self.GetCellTypesArray()
if not NUMPY_AVAILABLE:
return {
"connectivity": conn_vtk,
"offsets": offsets_vtk,
"cell_types": ct_vtk,
}
conn = dsa.vtkDataArrayToVTKArray(conn_vtk)
offsets = dsa.vtkDataArrayToVTKArray(offsets_vtk)
ct = dsa.vtkDataArrayToVTKArray(ct_vtk)
return {"connectivity": conn, "offsets": offsets, "cell_types": ct}
@cells.setter
def cells(self, cells):
ca = vtkCellArray()
if not NUMPY_AVAILABLE:
ca.SetData(cells["offsets"], cells["connectivity"])
self.SetCells(cells["cell_types"], ca)
return
conn_vtk = dsa.numpyTovtkDataArray(cells["connectivity"])
offsets_vtk = dsa.numpyTovtkDataArray(cells["offsets"])
cell_types_vtk = dsa.numpyTovtkDataArray(cells["cell_types"])
ca.SetData(offsets_vtk, conn_vtk)
self.SetCells(cell_types_vtk, ca)
@vtkImageData.override
class ImageData(DataSet, vtkImageData):
def __init__(self, **kwargs):
DataSet.__init__(self, **kwargs)
vtkImageData.__init__(self, **kwargs)
@vtkPolyData.override
class PolyData(PointSet, vtkPolyData):
def __init__(self, **kwargs) -> None:
PointSet.__init__(self, **kwargs)
vtkPolyData.__init__(self, **kwargs)
self._numpy_attrs.extend(["verts", "lines", "strips", "polys"])
@property
def verts_arrays(self):
ca = self.GetVerts()
conn_vtk = ca.GetConnectivityArray()
offsets_vtk = ca.GetOffsetsArray()
if not NUMPY_AVAILABLE:
return {
"connectivity": conn_vtk,
"offsets": offsets_vtk,
}
conn = dsa.vtkDataArrayToVTKArray(conn_vtk)
offsets = dsa.vtkDataArrayToVTKArray(offsets_vtk)
return {"connectivity": conn, "offsets": offsets}
@property
def lines_arrays(self):
ca = self.GetLines()
conn_vtk = ca.GetConnectivityArray()
offsets_vtk = ca.GetOffsetsArray()
if not NUMPY_AVAILABLE:
return {
"connectivity": conn_vtk,
"offsets": offsets_vtk,
}
conn = dsa.vtkDataArrayToVTKArray(conn_vtk)
offsets = dsa.vtkDataArrayToVTKArray(offsets_vtk)
return {"connectivity": conn, "offsets": offsets}
@property
def strips_arrays(self):
ca = self.GetStrips()
conn_vtk = ca.GetConnectivityArray()
offsets_vtk = ca.GetOffsetsArray()
if not NUMPY_AVAILABLE:
return {
"connectivity": conn_vtk,
"offsets": offsets_vtk,
}
conn = dsa.vtkDataArrayToVTKArray(conn_vtk)
offsets = dsa.vtkDataArrayToVTKArray(offsets_vtk)
return {"connectivity": conn, "offsets": offsets}
@property
def polys_arrays(self):
ca = self.GetPolys()
conn_vtk = ca.GetConnectivityArray()
offsets_vtk = ca.GetOffsetsArray()
if not NUMPY_AVAILABLE:
return {
"connectivity": conn_vtk,
"offsets": offsets_vtk,
}
conn = dsa.vtkDataArrayToVTKArray(conn_vtk)
offsets = dsa.vtkDataArrayToVTKArray(offsets_vtk)
return {"connectivity": conn, "offsets": offsets}
@vtkRectilinearGrid.override
class RectilinearGrid(DataSet, vtkRectilinearGrid):
def __init__(self, **kwargs) -> None:
DataSet.__init__(self, **kwargs)
vtkRectilinearGrid.__init__(self, **kwargs)
self._numpy_attrs.extend(["x_coordinates", "y_coordinates", "z_coordinates"])
@property
def x_coordinates(self):
pts = self.GetXCoordinates()
if not NUMPY_AVAILABLE:
return pts
if not pts:
return None
return dsa.vtkDataArrayToVTKArray(pts)
@x_coordinates.setter
def x_coordinates(self, points):
if isinstance(points, vtkDataArray):
self.SetXCoordinates(points)
return
if not NUMPY_AVAILABLE:
raise ValueError("Expect vtkDataArray")
pts = dsa.numpyTovtkDataArray(points, "x_coords")
self.SetXCoordinates(pts)
@property
def y_coordinates(self):
pts = self.GetYCoordinates()
if not NUMPY_AVAILABLE:
return pts
if not pts:
return None
return dsa.vtkDataArrayToVTKArray(pts)
@y_coordinates.setter
def y_coordinates(self, points):
if isinstance(points, vtkDataArray):
self.SetYCoordinates(points)
return
if not NUMPY_AVAILABLE:
raise ValueError("Expect vtkDataArray")
pts = dsa.numpyTovtkDataArray(points, "y_coords")
self.SetYCoordinates(pts)
@property
def z_coordinates(self):
pts = self.GetZCoordinates()
if not NUMPY_AVAILABLE:
return pts
if not pts:
return None
return dsa.vtkDataArrayToVTKArray(pts)
@z_coordinates.setter
def z_coordinates(self, points):
if isinstance(points, vtkDataArray):
self.SetZCoordinates(points)
return
if not NUMPY_AVAILABLE:
raise ValueError("Expect vtkDataArray")
pts = dsa.numpyTovtkDataArray(points, "z_coords")
self.SetZCoordinates(pts)
class CompositeDataIterator(object):
"""Wrapper for a vtkCompositeDataIterator class to satisfy
the python iterator protocol. This iterator iterates
over non-empty leaf nodes. To iterate over empty or
non-leaf nodes, use the vtkCompositeDataIterator directly.
"""
def __init__(self, cds):
self.Iterator = cds.NewIterator()
if self.Iterator:
self.Iterator.UnRegister(None)
self.Iterator.GoToFirstItem()
def __iter__(self):
return self
def __next__(self):
if not self.Iterator:
raise StopIteration
if self.Iterator.IsDoneWithTraversal():
raise StopIteration
retVal = self.Iterator.GetCurrentDataObject()
self.Iterator.GoToNextItem()
return retVal
def next(self):
return self.__next__()
def __getattr__(self, name):
"""Returns attributes from the vtkCompositeDataIterator."""
return getattr(self.Iterator, name)
class CompositeDataSetBase(object):
"""A wrapper for vtkCompositeData and subclasses that makes it easier
to access Point/Cell/Field data as VTKCompositeDataArrays. It also
provides a Python type iterator."""
def __init__(self, **kwargs):
self._PointData = None
self._CellData = None
self._FieldData = None
self._Points = None
def __iter__(self):
"Creates an iterator for the contained datasets."
return CompositeDataIterator(self)
def get_attributes(self, type):
"""Returns the attributes specified by the type as a
CompositeDataSetAttributes instance."""
return CompositeDataSetAttributes(self, type)
@property
def point_data(self):
"Returns the point data as a DataSetAttributes instance."
if self._PointData is None or self._PointData() is None:
pdata = self.get_attributes(vtkDataObject.POINT)
self._PointData = weakref.ref(pdata)
return self._PointData()
@property
def cell_data(self):
"Returns the cell data as a DataSetAttributes instance."
if self._CellData is None or self._CellData() is None:
cdata = self.get_attributes(vtkDataObject.CELL)
self._CellData = weakref.ref(cdata)
return self._CellData()
@property
def field_data(self):
"Returns the field data as a DataSetAttributes instance."
if self._FieldData is None or self._FieldData() is None:
fdata = self.get_attributes(vtkDataObject.FIELD)
self._FieldData = weakref.ref(fdata)
return self._FieldData()
@property
def points(self):
"Returns the points as a VTKCompositeDataArray instance."
if not NUMPY_AVAILABLE:
# don't know how to handle composite dataset when numpy not around
raise NotImplementedError("Only available with numpy")
if self._Points is None or self._Points() is None:
pts = []
for ds in self:
try:
_pts = ds.Points
except AttributeError:
_pts = None
if _pts is None:
pts.append(dsa.NoneArray)
else:
pts.append(_pts)
if len(pts) == 0 or all([a is dsa.NoneArray for a in pts]):
cpts = dsa.NoneArray
else:
cpts = dsa.VTKCompositeDataArray(pts, dataset=self)
self._Points = weakref.ref(cpts)
return self._Points()
@vtkPartitionedDataSet.override
class PartitionedDataSet(CompositeDataSetBase, vtkPartitionedDataSet):
def append(self, dataset):
self.SetPartition(self.GetNumberOfPartitions(), dataset)
@vtkPartitionedDataSetCollection.override
class PartitionedDataSetCollection(CompositeDataSetBase, vtkPartitionedDataSetCollection):
def append(self, dataset):
self.SetPartitionedDataSet(self.GetNumberOfPartitionedDataSets(), dataset)
@vtkOverlappingAMR.override
class OverlappingAMR(CompositeDataSetBase, vtkOverlappingAMR):
pass
@vtkMultiBlockDataSet.override
class MultiBlockDataSet(CompositeDataSetBase, vtkMultiBlockDataSet):
pass
@vtkStructuredGrid.override
class StructuredGrid(PointSet, vtkStructuredGrid):
def __init__(self, **kwargs):
PointSet.__init__(self, **kwargs)
vtkStructuredGrid.__init__(self, **kwargs)
@property
def x_coordinates(self):
if not NUMPY_AVAILABLE:
raise NotImplementedError("Only available with numpy")
dims = [0,0,0]
self.GetDimensions(dims)
return self.points[:, 0].reshape(dims, order="F")
@property
def y_coordinates(self):
if not NUMPY_AVAILABLE:
raise NotImplementedError("Only available with numpy")
dims = [0,0,0]
self.GetDimensions(dims)
return self.points[:, 1].reshape(dims, order="F")
@property
def z_coordinates(self):
if not NUMPY_AVAILABLE:
raise NotImplementedError("Only available with numpy")
dims = [0,0,0]
self.GetDimensions(dims)
return self.points[:, 2].reshape(dims, order="F")
# -----------------------------------------------------------------------------
# Handle pickle registration
# -----------------------------------------------------------------------------
with suppress(ImportError):
import copyreg
from vtkmodules.util.pickle_support import serialize_VTK_data_object
copyreg.pickle(PolyData, serialize_VTK_data_object)
copyreg.pickle(UnstructuredGrid, serialize_VTK_data_object)
copyreg.pickle(ImageData, serialize_VTK_data_object)
copyreg.pickle(PartitionedDataSet, serialize_VTK_data_object)
copyreg.pickle(StructuredGrid, serialize_VTK_data_object)
|