def get_iso_mesh(distobj, bounds): # for RPC from skimage.measure import marching_cubes_lewiner import numpy as np from compas_vol.primitives import VolBox from compas.geometry import Box bx = bounds[0] by = bounds[1] bz = bounds[2] x, y, z = np.ogrid[bx[0]:bx[1]:bx[2] * 1j, by[0]:by[1]:by[2] * 1j, bz[0]:bz[1]:bz[2] * 1j] #x, y, z = np.ogrid[-30:30:60j, -30:30:60j, -30:30:60j] # spacing: (max-min)/num (or num-1?) sx = (bx[1] - bx[0]) / (bx[2] - 1) sy = (by[1] - by[0]) / (by[2] - 1) sz = (bz[1] - bz[0]) / (bz[2] - 1) b = Box.from_data(distobj['box']) vb = VolBox(b, distobj['radius']) vb.data = distobj dm = vb.get_distance_numpy(x, y, z) verts, faces, norms, vals = marching_cubes_lewiner(dm, 0.0, spacing=(sx, sy, sz)) mesh = get_compas_mesh(verts, faces) #return str(mesh.number_of_vertices()) return str(mesh.to_data())
return da + self.f * db def get_distance_numpy(self, x, y, z): """ vectorized distance function """ da = self.a.get_distance_numpy(x, y, z) db = self.b.get_distance_numpy(x, y, z) return da + self.f * db if __name__ == "__main__": from compas_vol.primitives import VolSphere, VolBox from compas.geometry import Box, Frame, Point, Sphere s = Sphere(Point(2, 3, 0), 7) b = Box(Frame.worldXY(), 20, 15, 10) vs = VolSphere(s) vb = VolBox(b, 2.5) f = Overlay(vs, vb, 0.2) print(f) for y in range(-15, 15): s = '' for x in range(-30, 30): d = f.get_distance((x * 0.5, y, 0)) if d < 0: s += 'x' else: s += '.' print(s)
import rhinoscriptsyntax as rs import Rhino.Geometry as rg from compas_vol.primitives import VolSphere, VolBox from compas_vol.combinations import Union,Subtraction from compas.geometry import Sphere, Box, Frame from compas.rpc import Proxy s = VolSphere(Sphere((0, 0, 0), 6)) b = VolBox(Box(Frame.worldXY(), 10, 10, 10), 1.5) u = Union(s, b) t = Subtraction(b, s) p = Proxy('compas_vol.utilities') #p.stop_server() #p = Proxy('compas_vol.utilities') bounds = ((-25,25,100), (-25,25,100), (-25,25,100)) vs,fs = p.get_vfs_from_tree(str(t), bounds, 1.0) mesh = rg.Mesh() for v1, v2, v3 in vs: mesh.Vertices.Add(v1, v2, v3) for f in fs: if len(set(f))>2: mesh.Faces.AddFace(f[0], f[1], f[2]) mesh.Compact() mesh.Normals.ComputeNormals() a = mesh b = [rs.CreatePoint(v1,v2,v3) for v1, v2, v3 in vs]
if __name__ == "__main__": from compas_vol.primitives import VolBox from compas_vol.combinations import Union from compas.geometry import Box, Frame import numpy as np import matplotlib.pyplot as plt import random boxes = [] for i in range(10): pt = [random.random() * 8 - 4 for _ in range(3)] xa = [random.random() for _ in range(3)] ya = [random.random() for _ in range(3)] b = Box(Frame(pt, xa, ya), 4, 3, 2) vb = VolBox(b) boxes.append(vb) u = Union(boxes) x, y, z = np.ogrid[-5:5:70j, -5:5:70j, -5:5:50j] m = u.get_distance_numpy(x, y, z) plt.subplot(1, 2, 1) plt.imshow(m[:, :, 25], cmap='RdBu') b = Blur(m) o = b.get_blurred() plt.subplot(1, 2, 2) plt.imshow(o[:, :, 25], cmap='RdBu') plt.show()
# plane = Plane(self.frame.point, self.frame.normal) # vp = VolPlane(plane) # dm = vp.get_distance_numpy(x, y, z) m = matrix_from_axis_and_angle(self.frame.normal, 10, self.frame.point) mi = matrix_inverse(m) p = np.array([x, y, z, 1]) xt, yt, zt, _ = np.dot(mi, p) return self.obj.get_distance_numpy(xt, yt, zt) if __name__ == "__main__": from compas_vol.primitives import VolBox from compas.geometry import Box bx = Box(Frame.worldXY(), 20, 15, 10) vb = VolBox(bx, 1.5) t = Twist(vb, Frame((0, 0, 0), (1, 0, 0), (0, 0, 1)), 15) for y in range(-15, 15): s = '' for x in range(-30, 30): d = t.get_distance((x * 0.5, y, 0)) if d < 0: s += 'x' else: s += '.' print(s)
# x, y = np.ogrid[-10:10:resolution+0j, -10:10:resolution+0j] y, x = np.ogrid[-10:10:resolution * 1j, -10:10:resolution * 1j] d = distfield.get_distance_numpy(x, y, level) # linear gradient # d = d - d.min() # m = d / d.max() # binary image # m = (d < 0)*1.0 # absolute # d = np.abs(d) # m = d / d.max() # with aliasing m = 1 - (np.tanh(d) + 1) / 2 io.imsave(filename, m) return d if __name__ == "__main__": from compas_vol.primitives import VolBox from compas.geometry import Box, Frame cb = Box(Frame((0, 0, 0), (1, 0.2, 0.1), (-0.1, 1, 0.2)), 16, 12, 8) vb = VolBox(cb, 1.5) o = export_layer(vb, 100, 0) print(type(o))