注釈
Go to the end to download the full example code.
表面の法線を計算します#
表面上の法線を計算します.
from __future__ import annotations
import numpy as np
from pyvista import examples
サーフェスの法線を計算するには, pyvista.PolyData
の pyvista.PolyDataFilters.compute_normals()
メソッドを使うと非常に簡単です.
mesh = examples.download_topo_global()
mesh.plot(cmap='gist_earth', show_scalar_bar=False)

これで地球のサーフェスデータセットがロードされました.残念ながら,データセットには地形起伏を隠す均一な半径の地球が表示されます. pyvista.PolyDataFilters.compute_normals()
を使用して,データセット内のすべてのポイントで地球上の法線ベクトルを計算し,データセットで指定された値を使用してサーフェスを法線方向にワープし,誇張された地形レリーフを作成します.
# Compute the normals in-place and use them to warp the globe
mesh.compute_normals(inplace=True) # this activates the normals as well
# Now use those normals to warp the surface
warp = mesh.warp_by_scalar(factor=0.5e-5)
# And let's see it
warp.plot(cmap='gist_earth', show_scalar_bar=False)

面/セル法線を使用して,一般的な方向を向いているメッシュのすべての面を抽出することもできます.次のスニペットでは,メッシュを取得し,そのセル面に沿って法線を計算し,上向きの面を抽出します.
mesh = examples.download_nefertiti()
# Compute normals
mesh.compute_normals(cell_normals=True, point_normals=False, inplace=True)
# Get list of cell IDs that meet condition
ids = np.arange(mesh.n_cells)[mesh['Normals'][:, 2] > 0.0]
# Extract those cells
top = mesh.extract_cells(ids)
cpos = [
(-834.3184529757553, -918.4677714398535, 236.5468795300025),
(11.03829376004883, -13.642289291587957, -35.91218884207208),
(0.19212361465657216, 0.11401076390090074, 0.9747256344254143),
]
top.plot(cpos=cpos, color=True)

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