import matplotlib.pyplot as plt
import numpy as np
def generate_bowl_model(bottom_radius, top_radius, height, num_slices, num_stack, bottom_stack_ratios):
vertices = []
faces = []
# 生成碗的侧面
for i in range(num_stacks + 1):
if i/(num_stacks) < bottom_stack_ratio :#处理碗底
stack_angle = i / (num_stacks*bottom_stack_ratios) * np.pi / 2
stack_radius = np.sin(stack_angle) * bottom_radius
stack_height = 0
else: #处理碗侧边
stack_angle = (i-num_stacks*bottom_stack_ratios) / (num_stacks*(1.0-bottom_stack_ratios)) * np.pi / 2
stack_radius = np.sin(stack_angle) * (top_radius - bottom_radius) + bottom_radius
stack_height = -(np.cos(stack_angle) * height-height)
for j in range(num_slices + 1):
slice_angle = j / num_slices * 2 * np.pi
x = stack_radius * np.cos(slice_angle)
y = stack_radius * np.sin(slice_angle)
z = stack_height
vertices.append([x, y, z])
# 生成碗的三角面片
for i in range(num_stacks):
for j in range(num_slices):
p1 = i * (num_slices + 1) + j
p2 = (i + 1) * (num_slices + 1) + j
p3 = i * (num_slices + 1) + j + 1
p4 = (i + 1) * (num_slices + 1) + j + 1
faces.extend([[p1, p2, p3], [p2, p4, p3]])
return np.array(vertices), np.array(faces)
#3D模型顶点效果
def plot_3d_model(vertices):
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(vertices[:, 0], vertices[:, 1], vertices[:, 2], marker='.')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()
#3D模型面效果
def plot_3d_model_face(vertices, faces):
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_trisurf(vertices[:, 0], vertices[:, 1], vertices[:, 2], triangles=faces, cmap='viridis')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()
if __name__ == "__main__":
bottom_radius = 1.0 # 调整底部半径
top_radius = 1.9 # 调整顶部半径,使其趋近于底部半径
height = 0.2 #碗状模型高度
num_slices = 30 #横向切片数量,即生成模型时在圆周方向上的切片数。它影响了模型侧面的光滑度和细节程度
num_stacks = 30 #纵向切片数量,即模型在高度方向上的分割数。它影响了模型在垂直方向上的光滑度和细节程度
bottom_stack_ratio =0.3 #碗底切片数量占整个纵向切片数量num_stacks的比例,bottom_stack_ratio越大碗底点数越多
car_width =0.4
car_length =0.6
bowl_vertices,faces = generate_bowl_model(bottom_radius, top_radius, height, num_slices, num_stacks,bottom_stack_ratio)
# 输出生成的顶点数据
print("bowl_vertices:num=",bowl_vertices)
for vertex in bowl_vertices:
print(f"Vertex: ({vertex[0]}, {vertex[1]}, {vertex[2]})")
# 显示3D模型顶点效果
# plot_3d_model(bowl_vertices)
# 显示3D模型顶点面效果
plot_3d_model_face(bowl_vertices,faces)
```
效果如下:
[video(video-pYGxkqiy-1705303495486)(type-csdn)(url-https://live.csdn.net/v/embed/359727)(image-https://video-community.csdnimg.cn/vod-84deb4/208fe0aeb37271ee99494531949c0102/snapshots/9b108891774b461dbe31f034acbbeb74-00001.jpg?auth_key=4858901594-0-0-7c818255222b76ba6e334c815bf23417)(title-2024-01-15-14-50-09)]
08-08
3026
02-20
1万+
03-26
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交