import numpy as np from geometry import Mesh from transform import rotate def rotate_extrude(x, y, theta=np.pi/32, remove_duplicate_vertices=True): x, y, = np.asarray(x), np.asarray(y) if len(x.shape) != 1 or len(y.shape) != 1 or x.size != y.size: raise ValueError('shape mismatch') points = np.zeros((len(x),3), dtype=np.float32) points[:,0] = x points[:,1] = y angles = np.arange(0, 2*np.pi, theta) vertices = np.empty((len(points)*len(angles), 3), dtype=np.float32) triangles = np.zeros((len(angles)*(len(points)-1)*2, 3), dtype=np.int32) step = len(points) - 1 for i, angle in enumerate(angles): this_slice = i*len(points) vertices[this_slice:this_slice+len(points)] = rotate(points, angle, (0,-1,0)) start = 2*i*step next_slice = ((i+1) % len(angles))*len(points) triangles[start:start+step,0] = np.arange(this_slice, this_slice+len(points)-1) triangles[start:start+step,1] = np.arange(next_slice, next_slice+len(points)-1) triangles[start:start+step,2] = np.arange(this_slice+1, this_slice+len(points)) triangles[start+step:start+2*step,0] = np.arange(next_slice, next_slice+len(points)-1) triangles[start+step:start+2*step,2] = np.arange(this_slice+1, this_slice+len(points)) triangles[start+step:start+2*step,1] = np.arange(next_slice+1, next_slice+len(points)) return Mesh(vertices, triangles, remove_duplicate_vertices=remove_duplicate_vertices) def cube(size=1): a = np.sqrt(size**2/2.0) x = [0,a,a,0] y = [-size/2.0,-size/2.0,size/2.0,size/2.0] return rotate_extrude(x, y, np.pi/2) def cylinder(radius1=1, radius2=1, height=2, theta=np.pi/32): x = [0,radius1,radius2,0] y = [-height/2.0, -height/2.0, height/2.0, height/2.0] return rotate_extrude(x, y, theta) def segmented_cylinder(radius, height=2, theta=np.pi/32, n=50): x = np.concatenate((np.linspace(0, radius, n, endpoint=False), [radius] * n, np.linspace(radius, 0, n, endpoint=False), [0.0])) y = np.concatenate(([-height/2.0] * n, np.linspace(-height/2.0, height/2.0, n, endpoint=False), [height/2.0] * (n+1))) #return x,y return rotate_extrude(x, y, theta, remove_duplicate_vertices=False) def sphere(radius=1, theta=np.pi/32): profile_angles = np.arange(-np.pi/2, np.pi/2+theta, theta) return rotate_extrude(radius*np.cos(profile_angles), radius*np.sin(profile_angles), theta) def torus(radius=1, offset=3, phi=np.pi/32, theta=np.pi/32): profile_angles = np.arange(0, 2*np.pi+phi, phi) x, y = np.cos(profile_angles), np.sin(profile_angles) return rotate_extrude(x + offset, y)