问题描述
所以我有 2 个表面(PyVista 中的 polyData)一个在另一个之上:
它们在 Z 访问上的形状略有不同,但只要顶部在 X、Y 平面上具有 Z 值,我们就可以确定底部具有相同的值。那么如何将两个对齐的 X、Y 曲面合并为一个实体网格?
我的尝试:
import numpy as np
import pyvista as pv
import vtk
def extruder(mesh,val_z):
extrude = vtk.vtkLinearExtrusionFilter()
extrude.SetInputData(mesh)
extrude.SetVector(0,val_z)
extrude.Update()
extruded_mesh = pv.wrap(extrude.Getoutput())
return extruded_mesh
# generate two sheets of input data
noise = pv.perlin_noise(2,(0.2,0.2,0.2),(0,0))
bounds_2d = (-10,10,-10,10)
dim = (40,50,1)
bottom,top = [
pv.sample_function(noise,dim=dim,bounds=bounds_2d + (z,z)).warp_by_scalar()
for z in [-5,5]
]
bottom = bottom.extract_surface(nonlinear_subdivision=5)
top = top.extract_surface(nonlinear_subdivision=5)
top = extruder(top,-50).triangulate()
bottom = extruder(bottom,50).triangulate()
intersection = bottom.boolean_cut(top)
#top = top.clip_surface(bottom,invert=False,compute_distance=True)
#top = top.extrude([0,-50]).triangulate()
#bottom = bottom.extrude([0,50]).triangulate()
#intersection = bottom.boolean_cut(top).triangulate()
p = pv.Plotter()
p.add_mesh(top,cmap="hot",opacity=0.15)
p.add_mesh(bottom,cmap="RdYlBu",opacity=0.15)
p.add_mesh(intersection,cmap="Dark2",opacity=1)
p.show()
解决方法
所以必须这样做:
import numpy as np
import pyvista as pv
# generate two sheets of input data
noise = pv.perlin_noise(2,(0.2,0.2,0.2),(0,0))
bounds_2d = (-10,10,-10,10)
dim = (40,50,1)
bottom,top = [
pv.sample_function(noise,dim=dim,bounds=bounds_2d + (z,z)).warp_by_scalar()
for z in [-5,5]
]
bottom = bottom.extract_surface()
top = top.extract_surface()
topm = top.extrude([0,-50]).triangulate().clean()
bottomm = bottom.extrude([0,50]).triangulate().clean()
topm = topm.clip_surface(bottom,invert=False)
bottomm = bottomm.clip_surface(top,invert=True)
intersection = topm.boolean_add(bottomm).triangulate().clean().subdivide(2).clean()
p = pv.Plotter()
#p.add_mesh(topm,cmap="hot",opacity=0.15)
#p.add_mesh(bottomm,cmap="gnuplot2",opacity=0.15)
p.add_mesh(intersection,cmap="Dark2",opacity=1)
p.show()