碰撞#

執行兩個網格之間的碰撞檢測。

此範例使用 collision 過濾器來檢測一個球體與另一個球體碰撞的面。

注意

由於 vtk.vtkCollisionDetectionFilter 的特性,重複使用此方法會比直接使用 vtk.vtkCollisionDetectionFilter 慢。過濾器的第一次更新會建立兩個 vtkOBBTree 的實例,隨後可以透過修改輸入網格的轉換或矩陣來更新這些實例。

此方法假設沒有轉換,並且更容易用於單次碰撞測試,但建議結合使用 pyvistavtk 來快速計算重複碰撞。請參閱碰撞檢測範例

import numpy as np
import pyvista as pv

pv.set_plot_theme("document")

建立主網格和次要「移動」網格。

碰撞面將會繪製在這個球體上,為此我們初始化一個初始的 "collisions" 遮罩。

sphere0 = pv.Sphere()
sphere0["collisions"] = np.zeros(sphere0.n_cells, dtype=bool)

# This mesh will be the moving mesh
sphere1 = pv.Sphere(radius=0.6, center=(-1, 0, 0))

設定繪圖器、開啟影片,並在移動球體後寫入一個影格。

pl = pv.Plotter()
pl.enable_hidden_line_removal()
pl.add_mesh(sphere0, show_scalar_bar=False, cmap="bwr")
pl.camera_position = "xz"
pl.add_mesh(sphere1, style="wireframe", color="green", line_width=5)

# for this example
pl.open_gif("collision_movie.gif")

# alternatively, to disable movie generation:
# pl.show(auto_close=False, interactive=False)

delta_x = 0.05
for i in range(int(2 / delta_x)):
    sphere1.translate([delta_x, 0, 0])
    col, n_contacts = sphere0.collision(sphere1)

    collision_mask = np.zeros(sphere0.n_cells, dtype=bool)
    if n_contacts:
        collision_mask[col["ContactCells"]] = True
    sphere0["collisions"] = collision_mask
    pl.write_frame()

    # alternatively, disable movie plotting and simply render the image
    # pl.render()

pl.close()
plot collisions

腳本總執行時間: (0 分鐘 8.973 秒)

預計記憶體使用量: 393 MB

由 Sphinx-Gallery 產生的範例集