​他のオブジェクトのラップ

​他のオブジェクトのラップ#

You can wrap() several other object types using pyvista including:

​これにより,モジュール性のためにPythonに特別な "​両方の世界の最良の部分" なプログラミングが可能になります.​pyvistaに何らかの制限がある場合(またはtrimesh),複数のモジュールの最良の機能を使用するようにスクリプトを調整することができます.

from __future__ import annotations

import numpy as np
import pyvista as pv
import trimesh
import vtk

Wrap Point Arrays#

Wrap a point cloud composed of random points from numpy.

rng = np.random.default_rng(seed=0)
points = rng.random((30, 3))
cloud = pv.wrap(points)
pv.plot(
    cloud,
    scalars=points[:, 2],
    render_points_as_spheres=True,
    point_size=50,
    opacity=points[:, 0],
    cpos='xz',
)
wrap trimesh

Wrap Trimesh Objects#

Create a simple trimesh.Trimesh and wrap it.

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)
print(mesh)
PolyData (0x71b69184df60)
  N Cells:    1
  N Points:   3
  N Strips:   0
  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

We can also convert to a trimesh.Trimesh mesh with to_trimesh(). First create PolyData with data.

mesh = pv.Sphere()
mesh.point_data['point_ids'] = np.arange(mesh.n_points)
mesh.cell_data['cell_ids'] = np.arange(mesh.n_cells)
mesh.field_data['misc'] = np.array([1, 2, 3])
mesh.user_dict['name'] = 'sphere'
mesh.user_dict['number'] = 42

Now convert it.

<trimesh.Trimesh(vertices.shape=(842, 3), faces.shape=(1680, 3))>

Point data is accessible via vertex_attributes.

print(tmesh.vertex_attributes.keys())
dict_keys(['point_ids'])

Cell data is accessible via face_attributes.

print(tmesh.face_attributes.keys())
dict_keys(['cell_ids'])

Field data is accessible via metadata. Both field data keys and user_dict keys are stored.

{'misc': pyvista_ndarray([1, 2, 3]), 'name': 'sphere', 'number': 42}

Use from_trimesh() to convert it back to a PolyData mesh. This is the same as using wrap().

The data contained in the trimesh object is recovered as point data,

['point_ids']

cell data,

['cell_ids']

and field data, including field data stored in the user_dict:

['misc', '_PYVISTA_USER_DICT']
{"name": "sphere", "number": 42}

Wrap VTK Meshes#

vtkPolyData のインスタンスをラップする

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)
print(mesh)
PolyData (0x71b69184dcc0)
  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

Tags: load

Total running time of the script: (0 minutes 2.279 seconds)

Sphinx-Galleryによるギャラリー