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
|
"""Core helper utilities."""
from __future__ import annotations
from collections import deque
from typing import TYPE_CHECKING
from typing import Sequence
from typing import cast
if TYPE_CHECKING: # pragma: no cover
from meshio import Mesh
from trimesh import Trimesh
from pyvista.core._typing_core import NumpyArray
import numpy as np
import pyvista
from pyvista.core import _vtk_core as _vtk
from . import transformations
from .fileio import from_meshio
from .fileio import is_meshio_mesh
def wrap(
dataset: NumpyArray[float] | _vtk.vtkDataSet | Trimesh | Mesh | None,
) -> pyvista.DataSet | pyvista.pyvista_ndarray | None:
"""Wrap any given VTK data object to its appropriate PyVista data object.
Other formats that are supported include:
* 2D :class:`numpy.ndarray` of XYZ vertices
* 3D :class:`numpy.ndarray` representing a volume. Values will be scalars.
* 3D :class:`trimesh.Trimesh` mesh.
* 3D :class:`meshio.Mesh` mesh.
.. versionchanged:: 0.38.0
If the passed object is already a wrapped PyVista object, then
this is no-op and will return that object directly. In previous
versions of PyVista, this would perform a shallow copy.
Parameters
----------
dataset : :class:`numpy.ndarray` | :class:`trimesh.Trimesh` | vtk.DataSet
Dataset to wrap.
Returns
-------
pyvista.DataSet
The PyVista wrapped dataset.
Examples
--------
Wrap a numpy array representing a random point cloud.
>>> import numpy as np
>>> import pyvista as pv
>>> points = np.random.default_rng().random((10, 3))
>>> cloud = pv.wrap(points)
>>> cloud
PolyData (...)
N Cells: 10
N Points: 10
N Strips: 0
X Bounds: ...
Y Bounds: ...
Z Bounds: ...
N Arrays: 0
Wrap a VTK object.
>>> import pyvista as pv
>>> import vtk
>>> points = vtk.vtkPoints()
>>> p = [1.0, 2.0, 3.0]
>>> vertices = vtk.vtkCellArray()
>>> pid = points.InsertNextPoint(p)
>>> _ = vertices.InsertNextCell(1)
>>> _ = vertices.InsertCellPoint(pid)
>>> point = vtk.vtkPolyData()
>>> _ = point.SetPoints(points)
>>> _ = point.SetVerts(vertices)
>>> mesh = pv.wrap(point)
>>> mesh
PolyData (...)
N Cells: 1
N Points: 1
N Strips: 0
X Bounds: 1.000e+00, 1.000e+00
Y Bounds: 2.000e+00, 2.000e+00
Z Bounds: 3.000e+00, 3.000e+00
N Arrays: 0
Wrap a Trimesh object.
>>> import trimesh
>>> import pyvista as pv
>>> points = [[0, 0, 0], [0, 0, 1], [0, 1, 0]]
>>> faces = [[0, 1, 2]]
>>> tmesh = trimesh.Trimesh(points, faces=faces, process=False)
>>> mesh = pv.wrap(tmesh)
>>> mesh # doctest:+SKIP
PolyData (0x7fc55ff27ad0)
N Cells: 1
N Points: 3
X Bounds: 0.000e+00, 0.000e+00
Y Bounds: 0.000e+00, 1.000e+00
Z Bounds: 0.000e+00, 1.000e+00
N Arrays: 0
"""
# Return if None
if dataset is None:
return None
if isinstance(dataset, tuple(pyvista._wrappers.values())):
# Return object if it is already wrapped
return dataset # type: ignore[return-value]
# Check if dataset is a numpy array. We do this first since
# pyvista_ndarray contains a VTK type that we don't want to
# directly wrap.
if isinstance(dataset, (np.ndarray, pyvista.pyvista_ndarray)):
if dataset.ndim == 1 and dataset.shape[0] == 3:
return pyvista.PolyData(dataset)
if dataset.ndim > 1 and dataset.ndim < 3 and dataset.shape[1] == 3:
return pyvista.PolyData(dataset)
elif dataset.ndim == 3:
mesh = pyvista.ImageData(dimensions=dataset.shape)
if isinstance(dataset, pyvista.pyvista_ndarray):
# this gets rid of pesky VTK reference since we're raveling this
dataset = np.asarray(dataset)
mesh['values'] = dataset.ravel(order='F')
mesh.active_scalars_name = 'values'
return mesh
else:
raise NotImplementedError('NumPy array could not be wrapped pyvista.')
# wrap VTK arrays as pyvista_ndarray
if isinstance(dataset, _vtk.vtkDataArray):
return pyvista.pyvista_ndarray(dataset)
# Check if a dataset is a VTK type
if hasattr(dataset, 'GetClassName'):
key = dataset.GetClassName()
try:
return pyvista._wrappers[key](dataset)
except KeyError:
raise TypeError(f'VTK data type ({key}) is not currently supported by pyvista.')
return None # pragma: no cover
# wrap meshio
if is_meshio_mesh(dataset):
return from_meshio(dataset)
# wrap trimesh
if dataset.__class__.__name__ == 'Trimesh':
# trimesh doesn't pad faces
dataset = cast('Trimesh', dataset)
polydata = pyvista.PolyData.from_regular_faces(
np.asarray(dataset.vertices),
faces=dataset.faces,
)
# If the Trimesh object has uv, pass them to the PolyData
if hasattr(dataset.visual, 'uv'):
polydata.active_texture_coordinates = np.asarray(dataset.visual.uv)
return polydata
# otherwise, flag tell the user we can't wrap this object
raise NotImplementedError(f'Unable to wrap ({type(dataset)}) into a pyvista type.')
def is_pyvista_dataset(obj):
"""Return ``True`` if the object is a PyVista wrapped dataset.
Parameters
----------
obj : Any
Any object to test.
Returns
-------
bool
``True`` when the object is a :class:`pyvista.DataSet`.
"""
return isinstance(obj, (pyvista.DataSet, pyvista.MultiBlock))
def generate_plane(normal, origin):
"""Return a _vtk.vtkPlane.
Parameters
----------
normal : sequence[float]
Three item sequence representing the normal of the plane.
origin : sequence[float]
Three item sequence representing the origin of the plane.
Returns
-------
vtk.vtkPlane
VTK plane.
"""
plane = _vtk.vtkPlane()
# NORMAL MUST HAVE MAGNITUDE OF 1
normal = normal / np.linalg.norm(normal)
plane.SetNormal(normal)
plane.SetOrigin(origin)
return plane
def axis_rotation(points, angle, inplace=False, deg=True, axis='z'):
"""Rotate points by angle about an axis.
Parameters
----------
points : numpy.ndarray
Array of points with shape ``(N, 3)``.
angle : float
Rotation angle.
inplace : bool, default: False
Updates points in-place while returning nothing.
deg : bool, default: True
If ``True``, the angle is interpreted as degrees instead of
radians.
axis : str, default: "z"
Name of axis to rotate about. Valid options are ``'x'``, ``'y'``,
and ``'z'``.
Returns
-------
numpy.ndarray
Rotated points.
Examples
--------
Rotate a set of points by 90 degrees about the x-axis in-place.
>>> import numpy as np
>>> import pyvista as pv
>>> from pyvista import examples
>>> points = examples.load_airplane().points
>>> points_orig = points.copy()
>>> pv.axis_rotation(points, 90, axis='x', deg=True, inplace=True)
>>> assert np.all(np.isclose(points[:, 0], points_orig[:, 0]))
>>> assert np.all(np.isclose(points[:, 1], -points_orig[:, 2]))
>>> assert np.all(np.isclose(points[:, 2], points_orig[:, 1]))
"""
axis = axis.lower()
axis_to_vec = {'x': (1, 0, 0), 'y': (0, 1, 0), 'z': (0, 0, 1)}
if axis not in axis_to_vec:
raise ValueError('Invalid axis. Must be either "x", "y", or "z"')
rot_mat = transformations.axis_angle_rotation(axis_to_vec[axis], angle, deg=deg)
return transformations.apply_transformation_to_points(rot_mat, points, inplace=inplace)
def is_inside_bounds(point, bounds):
"""Check if a point is inside a set of bounds.
This is implemented through recursion so that this is N-dimensional.
Parameters
----------
point : sequence[float]
Three item cartesian point (i.e. ``[x, y, z]``).
bounds : sequence[float]
Six item bounds in the form of ``(xMin, xMax, yMin, yMax, zMin, zMax)``.
Returns
-------
bool
``True`` when ``point`` is inside ``bounds``.
"""
if isinstance(point, (int, float)):
point = [point]
if isinstance(point, (np.ndarray, Sequence)) and not isinstance(
point,
deque,
):
if len(bounds) < 2 * len(point) or len(bounds) % 2 != 0:
raise ValueError('Bounds mismatch point dimensionality')
point = deque(point)
bounds = deque(bounds)
return is_inside_bounds(point, bounds)
if not isinstance(point, deque):
raise TypeError(f'Unknown input data type ({type(point)}).')
if len(point) < 1:
return True
p = point.popleft()
lower, upper = bounds.popleft(), bounds.popleft()
if lower <= p <= upper:
return is_inside_bounds(point, bounds)
return False
|