python绘制圆柱体
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | import os import random import numpy as np import matplotlib.pyplot as plt def plot_cylinder(center, radius, height, num_points = 100 ): # 生成圆柱体的侧面点坐标 theta = np.linspace( 0 , 2 * np.pi, num_points) intervalZ = np.floor(height / 0.05 ) indx2 = [ix for ix in range ( int (intervalZ))] random_int2 = random.sample(indx2, int (intervalZ - 10 )) xx = [] yy = [] zz = [] print (random_int2) for j in random_int2: indx = [i for i in range (num_points)] random_int = random.sample(indx, 50 ) theta2 = theta[random_int] print (random_int) x = center[ 0 ] + radius * np.cos(theta2) y = center[ 1 ] + radius * np.sin(theta2) z = (center[ 2 ] + 0.05 * j) * np.ones_like(x) xx = np.hstack((xx, x)) yy = np.hstack((yy, y)) zz = np.hstack((zz, z)) print (j) #np.linspace(center[2], center[2]+height, num_points) return xx,yy,zz x,y,z = plot_cylinder(( 0 , 0 , 0 ), 1 , 2 ) print (x.shape,y.shape,z.shape) fig = plt.figure() ax = fig.add_subplot( 111 , projection = '3d' ) ax.scatter(x, y, z) plt.show() |
随机生成一个圆柱体点集合:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | import os import random import numpy as np import matplotlib.pyplot as plt def plot_cylinder(center, radius, height, num_points = 100 ): # 生成圆柱体的侧面点坐标 theta = np.linspace( 0 , 2 * np.pi, num_points) intervalZ = height / num_points indx2 = [ix for ix in range (num_points)] random_int2 = random.sample(indx2, 90 ) xx = [] yy = [] zz = [] print (random_int2) for j in random_int2: indx = [i for i in range (num_points)] random_int = random.sample(indx, 50 ) theta2 = theta[random_int] print (random_int) x = center[ 0 ] + radius * np.cos(theta2) y = center[ 1 ] + radius * np.sin(theta2) z = (center[ 2 ] + intervalZ * j) * np.ones_like(x) xx = np.hstack((xx, x)) yy = np.hstack((yy, y)) zz = np.hstack((zz, z)) print (j) #np.linspace(center[2], center[2]+height, num_points) return xx,yy,zz def rotate_X(x, y, z, alpha): alpha = alpha * (np.pi / 180 ) x_r = x y_r = np.cos(alpha) * y - np.sin(alpha) * z z_r = np.sin(alpha) * y + np.cos(alpha) * z return round (x_r, 4 ), round (y_r, 4 ), round (z_r, 4 ) def rotate_Y(x, y, z, beta): beta = beta * (np.pi / 180 ) x_r = np.cos(beta) * x + np.sin(beta) * z y_r = y z_r = - np.sin(beta) * x + np.cos(beta) * z return round (x_r, 4 ), round (y_r, 4 ), round (z_r, 4 ) def rotate_Z(x, y, z, gamma): gamma = gamma * (np.pi / 180 ) x_r = np.cos(gamma) * x - np.sin(gamma) * y y_r = np.sin(gamma) * x + np.cos(gamma) * y z_r = z return round (x_r, 4 ), round (y_r, 4 ), round (z_r, 4 ) x,y,z = plot_cylinder(( 0 , 0 , 0 ), 0.2 , 1 ) x_new = [] y_new = [] z_new = [] theta = np.linspace( 0 , 360 , 360 ) random_int_angle = random.randint( 1 , 360 ) random_int_angle2 = random.randint( 1 , 360 ) for x_t, y_t, z_t in zip (x, y, z): x1, y1, z1 = rotate_X(x_t, y_t, z_t, theta[random_int_angle]) x2, y2, z2 = rotate_Y(x1, y1, z1, theta[random_int_angle2]) x_new = np.hstack((x_new,x2)) y_new = np.hstack((y_new,y2)) z_new = np.hstack((z_new,z2)) print (x.shape,y.shape,z.shape) fig = plt.figure() ax = fig.add_subplot( 111 , projection = '3d' ) ax.scatter(x_new, y_new, z_new) ax.set_xlim([ - 1.5 * 1 , 1.5 * 1 ]) ax.set_ylim([ - 1.5 * 1 , 1.5 * 1 ]) ax.set_zlim([ - 1 , 2 ]) ax.set_xlabel( 'X' ) ax.set_ylabel( 'Y' ) ax.set_zlabel( 'Z' ) plt.show() |
随机生成一个圆柱体
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | import os import random import numpy as np import matplotlib.pyplot as plt def plot_cylinder(center, radius, height, num_points = 100 ): # 生成圆柱体的侧面点坐标 theta = np.linspace( 0 , 2 * np.pi, num_points) intervalZ = height / num_points indx2 = [ix for ix in range (num_points)] random_int2 = random.sample(indx2, 100 ) xx = [] yy = [] zz = [] nx = [] ny = [] nz = [] print (random_int2) for j in random_int2: indx = [i for i in range (num_points)] random_int = random.sample(indx, 50 ) theta2 = theta[random_int] print (random_int) x = center[ 0 ] + radius * np.cos(theta2) y = center[ 1 ] + radius * np.sin(theta2) z_tmp = intervalZ * j z = (center[ 2 ] + z_tmp) * np.ones_like(x) for iz in range ( 50 ): z[iz] = z[iz] + 0.5 * intervalZ * random.uniform( - 1 , 1 ) nxx = np.cos(theta2) nyy = np.sin(theta2) nzz = 0 * np.ones_like(x) xx = np.hstack((xx, x)) yy = np.hstack((yy, y)) zz = np.hstack((zz, z)) nx = np.hstack((nx, nxx)) ny = np.hstack((ny, nyy)) nz = np.hstack((nz, nzz)) print (j) #np.linspace(center[2], center[2]+height, num_points) return xx,yy,zz,nx,ny,nz def rotate_X(x, y, z, alpha): alpha = alpha * (np.pi / 180 ) x_r = x y_r = np.cos(alpha) * y - np.sin(alpha) * z z_r = np.sin(alpha) * y + np.cos(alpha) * z return round (x_r, 4 ), round (y_r, 4 ), round (z_r, 4 ) def rotate_Y(x, y, z, beta): beta = beta * (np.pi / 180 ) x_r = np.cos(beta) * x + np.sin(beta) * z y_r = y z_r = - np.sin(beta) * x + np.cos(beta) * z return round (x_r, 4 ), round (y_r, 4 ), round (z_r, 4 ) def rotate_Z(x, y, z, gamma): gamma = gamma * (np.pi / 180 ) x_r = np.cos(gamma) * x - np.sin(gamma) * y y_r = np.sin(gamma) * x + np.cos(gamma) * y z_r = z return round (x_r, 4 ), round (y_r, 4 ), round (z_r, 4 ) data_path = '../data/Cylinders' n_samples = 10 if not os.path.exists(data_path): os.makedirs(data_path) theta_ply = np.linspace( 0 , 360 , 360 ) nr = np.linspace( 1 , 8 , 16 ) for isample in range (n_samples): x_new = [] y_new = [] z_new = [] nx_new = [] ny_new = [] nz_new = [] random_int_angle = random.randint( 1 , 360 ) random_int_angle2 = 360 - random.randint( 1 , 360 ) random_int_nr = isample % 16 if (isample % 16 > 0 ) else 1 x,y,z,nx,ny,nz = plot_cylinder(( 0 , 0 , 0 ), 0.025 * random_int_nr, 1 ) for x_t, y_t, z_t,nx_t,ny_t,nz_t in zip (x, y, z, nx, ny, nz): x1, y1, z1 = rotate_X(x_t, y_t, z_t, theta_ply[random_int_angle]) x2, y2, z2 = rotate_Y(x1, y1, z1, theta_ply[random_int_angle2]) nx1, ny1, nz1 = rotate_X(nx_t, ny_t, nz_t, theta_ply[random_int_angle]) nx2, ny2, nz2 = rotate_Y(nx1, ny1, nz1, theta_ply[random_int_angle2]) x_new = np.hstack((x_new,x2)) y_new = np.hstack((y_new,y2)) z_new = np.hstack((z_new,z2)) nx_new = np.hstack((nx_new, nx2)) ny_new = np.hstack((ny_new, ny2)) nz_new = np.hstack((nz_new, nz2)) f_Edges = data_path + '/{}{}.ply' . format ( 'cylinder' , isample) with open (f_Edges, 'w' ) as file : file .writelines( 'ply\nformat ascii 1.0\ncomment VCGLIB generated\nelement vertex 5000\nproperty float x\n' 'property float y\nproperty float z\nproperty float nx\nproperty float ny\n' 'property float nz\nelement face 0\nproperty list uchar int vertex_indices\nend_header\n' ) for a, l, lc,bnx,bny,bnz in zip (x_new, y_new, z_new,nx_new, ny_new, nz_new): #写入一条记录 file .writelines( '{} {} {} {} {} {}\n' . format (a, l, lc,bnx,bny,bnz)) print (x.shape,y.shape,z.shape) fig = plt.figure() ax = fig.add_subplot( 111 , projection = '3d' ) ax.scatter(x_new, y_new, z_new) ax.set_xlim([ - 1.5 * 1 , 1.5 * 1 ]) ax.set_ylim([ - 1.5 * 1 , 1.5 * 1 ]) ax.set_zlim([ - 1 , 2 ]) ax.set_xlabel( 'X' ) ax.set_ylabel( 'Y' ) ax.set_zlabel( 'Z' ) plt.show() |
作者:太一吾鱼水
文章未经说明均属原创,学习笔记可能有大段的引用,一般会注明参考文献。
欢迎大家留言交流,转载请注明出处。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程
2015-08-06 SLAM学习笔记(1)基本概念
2013-08-06 判断点在多边形内