def height_field(img): pixels = len(img) origin = vec3(pixels / 2, pixels / 2, 0) radius = pixels // 2 deltaz = radius wtoi = TForm(t=vec3(pixels / 2, pixels / 2, 0) - origin, s=vec3(pixels / (2 * radius), pixels / (2 * radius), 1)) field = scalar_field.topography(img, wtoi, 1.0) field.origin = origin field.radius = radius field.deltaz = deltaz field.z0, field.dz = img.min(), img.max() - img.min() return field
def pixel_polygon(mask): pixels = mask.astype(np.uint8) * 255 pixels = dilate(pixels, (8, 8), 10) ret, thresh = cv2.threshold(pixels, 127, 255, 0) contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) if contours: vs = cv2.approxPolyDP(contours[0], 0.1, True) ps = [vec3(x, y, 0) for x, y in vs[:, 0]] return ps
def segment_regions(img): #clean_border = segmentation.clear_border(img) clean_border = segmentation.morphological_chan_vese(img, 10) labeled = label(clean_border) regions = regionprops(labeled) regions = sorted(regions, key=(lambda r: r.area), reverse=True) masks = [(labeled == r.label) for r in regions] fps = [] for m, r in zip(masks, regions): s = vec3(1.0 / m.shape[0], 1.0 / m.shape[1], 1) fp = [(s * p) for p in pixel_polygon(m)] fps.append(fp) return fps, masks, regions
def eigen(p): dpdx = dx(p) dpdy = dy(p) return vec3(dpdx, dpdy, 0) * weight * max( 1, np.sqrt(dpdx**2 + dpdy**2))
def wtoi(origin, radius, resolution): dp = origin.tov(vec3(resolution / 2, resolution / 2, 0)) ds = vec3(resolution / (2 * radius), resolution / (2 * radius), 1) tf = TForm(dp, None, ds) return tf
from meshmaker.model import Model from meshmaker.mesh import Mesh from meshmaker.tform import TForm from meshmaker.vec3 import vec3 from meshmaker.quat import quat from meshmaker.mgl import show import numpy as np # make a cube and some copies a = Mesh.cube_mesh(r=1) b = a.cp() c = a.cp() d = a.cp() # pick some t, q, s (translation, rotation, scale) t, q, s = vec3.X(2), quat.av(np.pi / 6, vec3.Z()), vec3(0.6, 0.8, 1.2) # transform some cubes with t, q, s t.trnps(q.rot(s.sclps(b.vertices))) t.trnps(t.trnps(q.rot(q.rot(s.sclps(s.sclps(c.vertices)))))) t.trnps( t.trnps(t.trnps(q.rot(q.rot(q.rot(s.sclps(s.sclps(s.sclps( d.vertices))))))))) # put cubes in a model in a scenegraph to assign a texture to each cube meshes = { 'generic_8': [a], 'generic_9': [b], 'generic_10': [c], 'generic_11': [d], }