注釈
Go to the end to download the full example code.
点群を作成#
頂点のポイントクラウドとそれらのポイントのスカラー配列から pyvista.PolyData
オブジェクトを作成します.
from __future__ import annotations
import numpy as np
import pyvista as pv
from pyvista import examples
点群は一般に pyvista.PolyData
クラスで構成され,点群に関連付けられたスカラー/ベクトルデータ配列を簡単に持つことができます.この例では, examples
モジュールから使用可能な点群を使用して,少し逆方向に作業します.ただし,これは独自の頂点位置のNumPy配列を使用してPyVistaメッシュを作成するのと同じです.
# Seed the random number generator for generating data
rng = np.random.default_rng(seed=0)
# Define some helpers - ignore these and use your own data.
def generate_points(subset=0.02):
"""Make a 3D NumPy array of points (n_points by 3)."""
dataset = examples.download_lidar()
ids = rng.integers(
low=0,
high=dataset.n_points - 1,
size=int(dataset.n_points * subset),
)
return dataset.points[ids]
points = generate_points()
# Print first 5 rows to prove its a numpy array (n_points by 3)
# Columns are (X Y Z)
points[0:5, :]
pyvista_ndarray([[4.81111875e+05, 4.40022180e+06, 1.76938000e+03],
[4.80971375e+05, 4.40018510e+06, 1.75958997e+03],
[4.81060975e+05, 4.40016340e+06, 1.76020996e+03],
[4.80989475e+05, 4.40012190e+06, 1.76153003e+03],
[4.81082475e+05, 4.40012840e+06, 1.76506006e+03]])
サンプルデータまたは独自のプロジェクトからポイント/頂点のNumPy配列を取得したので,これらのポイントのPyVistaメッシュを作成するのは簡単です.
point_cloud = pv.PolyData(points)
point_cloud
健全性のチェックもできます
np.allclose(points, point_cloud.points)
True
これでPyVistaメッシュができたので,プロットすることができます.ここでは,eye domeライティングを使用するオプションを追加します.これは,ポイントクラウド( Eye Dome Lighting についてもっと学ぶ)を使用して深さの認識を向上させるシェーディング技法です.
point_cloud.plot(eye_dome_lighting=True)

ここで,メッシュのすべてのノードに関連付けるデータアトリビュート(スカラー/ベクトル配列)があるとします.最初の軸に沿ったメッシュ内の点の数と同じ長さのNumPyデータ配列を簡単に追加できます.例えば,この新しい point_cloud
メッシュにいくつかの配列を追加しましょう.
points配列と同じ長さのスカラー値の配列を作成します.この配列の各要素は,同じインデックスのポイントに対応します.
# Make data array using z-component of points array
data = points[:, -1]
# Add that data to the mesh with the name "uniform dist"
point_cloud['elevation'] = data
これで,ランダムデータを使用して点群をプロットできます.PyVistaは,デフォルトで追加したスカラー配列をプロットするのに十分な性能を持っています.ここでは,すべてのポイントを独自の球としてレンダリングするように指定します.
point_cloud.plot(render_points_as_spheres=True)

そのデータは退屈ですよね?複数のスカラー値を持つデータ配列,例えば3つの要素を持つベクトルを追加することもできます.ポイントクラウド内のすべてのノードのベクトルを計算し,それらのベクトルをメッシュに追加する簡単な関数を作成します.
そのデータは退屈ですよね?複数のスカラー値を持つデータ配列,例えば3つの要素を持つベクトルを追加することもできます.ポイントクラウド内のすべてのノードのベクトルを計算し,それらのベクトルをメッシュに追加する簡単な関数を作成します.
# Create random XYZ points
points = rng.random((100, 3))
# Make PolyData
point_cloud = pv.PolyData(points)
def compute_vectors(mesh):
origin = mesh.center
vectors = mesh.points - origin
return vectors / np.linalg.norm(vectors, axis=1)[:, None]
vectors = compute_vectors(point_cloud)
vectors[0:5, :]
pyvista_ndarray([[ 0.13068497, 0.92471027, -0.3575365 ],
[ 0.61781309, 0.78456133, -0.05263558],
[ 0.002668 , 0.56683936, 0.82382402],
[-0.62674273, 0.76148038, 0.16535169],
[-0.25670454, -0.63806243, 0.72593328]])
point_cloud['vectors'] = vectors
今度は,グリフフィルター(詳細は 字形のプロット (ベクトルかPolyData) を参照)を使って,これらのベクトルを使って矢印を作ることができます.
arrows = point_cloud.glyph(
orient='vectors',
scale=False,
factor=0.15,
)
# Display the arrows
plotter = pv.Plotter()
plotter.add_mesh(point_cloud, color='maroon', point_size=10.0, render_points_as_spheres=True)
plotter.add_mesh(arrows, color='lightblue')
# plotter.add_point_labels([point_cloud.center,], ['Center',],
# point_color='yellow', point_size=20)
plotter.show_grid()
plotter.show()

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