def construct_sphere(radius=1, cx=0, cy=0, cz=0, u_res=10, v_res=10): """ Constructs a uv sphere mesh. Optional Arguments: ---------- radius : float<br> The radius of the sphere.<br> cx,cy,cz : float<br> The coordinates of the center point.<br> u_res : float<br> The u resolution of the sphere.<br> y_res : float<br> The v resolution of the sphere. """ mesh = Mesh() for v in range(v_res + 1): theta = math.pi * (float(v) / v_res) for u in range(u_res): phi = 2.0 * math.pi * (float(u) / u_res) cartesian = _polar_to_cartesian(radius, theta, phi) mesh.add_vertex(cartesian[0] + cx, cartesian[1] + cy, cartesian[2] + cz) #work around weld_vertices problem v_top = mesh.vertices[0] v_bottom = mesh.vertices[v_res * u_res + u_res - 1] for v in range(v_res): for u in range(u_res): v0 = mesh.vertices[v * u_res + u] v1 = mesh.vertices[(v + 1) * u_res + u] v2 = mesh.vertices[(v + 1) * u_res + (u + 1) % u_res] v3 = mesh.vertices[v * u_res + (u + 1) % u_res] if (v == 0): mesh.add_face([v_top, v1, v2]) elif (v == v_res - 1): mesh.add_face([v0, v_bottom, v3]) else: mesh.add_face([v0, v1, v2, v3]) mesh.update_topology() return mesh
def numpy_to_voxel_mesh(voxel_bools, voxel_colors): """Returns the Mesh of a voxel geometry that is described by Numpy Arrays. Arguments ---------- voxel_bools : numpy.ndarray Numpy Array of shape (nX,nY,nZ) and dtype=bool where True corresponds to a Solid voxel and False corresponds to a Void voxel. voxel_colors : numpy.ndarray Numpy Array of shape (nX,nY,nZ,3) containing r,g,b values for each voxel. """ #add one dimension if input is 2d if voxel_bools.ndim == 2 and voxel_colors.ndim == 3: voxel_bools = voxel_bools[:,:,np.newaxis] voxel_colors = np.expand_dims(voxel_colors, axis=2) shape = voxel_bools.shape nx, ny, nz = shape[0], shape[1], shape[2] mesh = Mesh() for index in np.ndindex(shape): x = index[0] y = index[1] z = index[2] if voxel_bools[x, y, z]: # (x,y) (x1,y) (x1,y1) (x,y1) r = voxel_colors[x, y, z, 0] g = voxel_colors[x, y, z, 1] b = voxel_colors[x, y, z, 2] # rgb=voxel_colors[x,y,z] rgb = (r, g, b, 1) if x == nx - 1 or not voxel_bools[x+1, y, z]: v1 = mesh.add_vertex(x + 1, y, z) v2 = mesh.add_vertex(x + 1, y + 1, z) v3 = mesh.add_vertex(x + 1, y + 1, z + 1) v4 = mesh.add_vertex(x + 1, y, z + 1) new_face = mesh.add_face([v1, v2, v3, v4]) new_face.color = rgb if x == 0 or not voxel_bools[x-1, y, z]: v1 = mesh.add_vertex(x, y + 1, z) v2 = mesh.add_vertex(x, y, z) v3 = mesh.add_vertex(x, y, z + 1) v4 = mesh.add_vertex(x, y + 1, z + 1) new_face = mesh.add_face([v1, v2, v3, v4]) new_face.color = rgb if y == ny - 1 or not voxel_bools[x, y+1, z]: v1 = mesh.add_vertex(x + 1, y + 1, z) v2 = mesh.add_vertex(x, y + 1, z) v3 = mesh.add_vertex(x, y + 1, z + 1) v4 = mesh.add_vertex(x + 1, y + 1, z + 1) new_face = mesh.add_face([v1, v2, v3, v4]) new_face.color = rgb if y == 0 or not voxel_bools[x, y-1, z]: v1 = mesh.add_vertex(x, y, z) v2 = mesh.add_vertex(x + 1, y, z) v3 = mesh.add_vertex(x + 1, y, z + 1) v4 = mesh.add_vertex(x, y, z + 1) new_face = mesh.add_face([v1, v2, v3, v4]) new_face.color = rgb if z == nz-1 or not voxel_bools[x, y, z+1]: v1 = mesh.add_vertex(x, y, z + 1) v2 = mesh.add_vertex(x + 1, y, z + 1) v3 = mesh.add_vertex(x + 1, y + 1, z + 1) v4 = mesh.add_vertex(x, y + 1, z + 1) new_face = mesh.add_face([v1, v2, v3, v4]) new_face.color = rgb if z == 0 or not voxel_bools[x, y, z-1]: v1 = mesh.add_vertex(x, y + 1, z) v2 = mesh.add_vertex(x + 1, y + 1, z) v3 = mesh.add_vertex(x + 1, y, z) v4 = mesh.add_vertex(x, y, z) new_face = mesh.add_face([v1, v2, v3, v4]) new_face.color = rgb mesh.update_topology() return mesh