def test_udf(): c0 = create_circle(200, 200, 0.3) c1 = create_circle(200, 200, 0.08, 0.8, 0.8) c0 = np.clip(c0 + c1, 0, 1) circles = snowy.add_border(c0, value=1) mask = circles != 0.0 udf = snowy.unitize(snowy.generate_udf(mask)) nx, ny = snowy.gradient(udf) grad = snowy.unitize(nx + ny) snowy.show(snowy.hstack([circles, udf, grad]))
def create_wrap_figures(): ground = snowy.load(qualify('ground.jpg')) hground = np.hstack([ground, ground]) ground2x2 = np.vstack([hground, hground]) snowy.export(ground2x2, qualify('ground2x2.jpg')) ground = snowy.blur(ground, radius=14, filter=snowy.LANCZOS) snowy.export(ground, qualify('blurry_ground_bad.jpg')) hground = np.hstack([ground, ground]) ground2x2 = np.vstack([hground, hground]) snowy.export(ground2x2, qualify('blurry_ground2x2_bad.jpg')) ground = snowy.load(qualify('ground.jpg')) ground = snowy.blur(ground, radius=14, wrapx=True, wrapy=True, filter=snowy.LANCZOS) snowy.export(ground, qualify('blurry_ground_good.jpg')) hground = np.hstack([ground, ground]) ground2x2 = np.vstack([hground, hground]) snowy.export(ground2x2, qualify('blurry_ground2x2_good.jpg')) n = snowy.generate_noise(256, 512, frequency=4, seed=42, wrapx=False) n = 0.5 + 0.5 * np.sign(n) - n n = np.hstack([n, n]) n = snowy.add_border(n, width=4) snowy.export(n, qualify('tiled_noise_bad.png')) n = snowy.generate_noise(256, 512, frequency=4, seed=42, wrapx=True) n = 0.5 + 0.5 * np.sign(n) - n n = np.hstack([n, n]) n = snowy.add_border(n, width=4) snowy.export(n, qualify('tiled_noise_good.png')) c0 = create_circle(400, 200, 0.3) c1 = create_circle(400, 200, 0.08, 0.8, 0.8) circles = np.clip(c0 + c1, 0, 1) mask = circles != 0.0 sdf = snowy.unitize(snowy.generate_sdf(mask, wrapx=True, wrapy=True)) sdf = np.hstack([sdf, sdf, sdf, sdf]) sdf = snowy.resize(np.vstack([sdf, sdf]), width=512) sdf = snowy.add_border(sdf) snowy.export(sdf, qualify('tiled_sdf_good.png')) sdf = snowy.unitize(snowy.generate_sdf(mask, wrapx=False, wrapy=False)) sdf = np.hstack([sdf, sdf, sdf, sdf]) sdf = snowy.resize(np.vstack([sdf, sdf]), width=512) sdf = snowy.add_border(sdf) snowy.export(sdf, qualify('tiled_sdf_bad.png'))
def test_gdf(): "This is a (failed) effort to create a smoother distance field." c0 = create_circle(200, 200, 0.3) c1 = create_circle(200, 200, 0.08, 0.8, 0.8) c0 = np.clip(c0 + c1, 0, 1) circles = snowy.add_border(c0, value=1) circles = np.clip(snowy.blur(circles, radius=2), 0, 1) circles = np.clip(snowy.blur(circles, radius=2), 0, 1) source = (1.0 - circles) * 2000.0 gdf = np.sqrt(snowy.generate_gdf(source)) gdf = snowy.unitize(gdf) nx, ny = snowy.gradient(gdf) grad = snowy.unitize(nx + ny) snowy.show(snowy.hstack([circles, gdf, grad]))
def create_falloff(w, h, radius=0.4, cx=0.5, cy=0.5): hw, hh = 0.5 / w, 0.5 / h x = np.linspace(hw, 1 - hw, w) y = np.linspace(hh, 1 - hh, h) u, v = np.meshgrid(x, y, sparse=True) d2 = (u-cx)**2 + (v-cy)**2 return 1-snowy.unitize(snowy.reshape(d2))
def test_draw_quad(): w, h = 100, 100 def show(im): snowy.show(snowy.resize(im, height=100, filter=None)) yellow = np.full((w, h, 4), (1, 1, 0, 1)) red = np.full((w, h, 4), (1, 0, 0, 1)) trans_border = np.full((w, h, 4), (0, 0, 1, 0.2)) t = 5 trans_border[t:h - t, t:w - t] *= 0 c0 = create_circle(w, h, 0.3) * yellow * 100000 c1 = create_circle(w, h, 0.07, 0.8, 0.8) * red * 10000 circles = np.clip(c0 + c1 + trans_border, 0, 1) r, g, b, a = circles.swapaxes(0, 2) luma = snowy.reshape(r + g + b) mask = luma != 0.0 sdf = snowy.unitize(np.abs(snowy.generate_sdf(mask))) cpcf = snowy.generate_cpcf(mask) voronoi = snowy.dereference_coords(circles, cpcf) show(voronoi) target = np.full((2000, 4000, 4), (0, 0, 0, 1), dtype=np.float32) seconds = timeit.timeit(lambda: snowy.draw_polygon( target, voronoi, np.array([(-1., -1, 1., 0., 1.), (-.5, +1, 1., 0., 0.), (+.5, +1, 1., 1., 0.), (+1., -1, 1., 1., 1.)])), number=1) show(target) print(seconds)
def test_cpcf(): w, h = 500, 500 def show(im): snowy.show(snowy.resize(im, height=100, filter=None)) yellow = np.full((w, h, 3), (1, 1, 0)) red = np.full((w, h, 3), (1, 0, 0)) blue_border = np.full((w, h, 3), (0, 0, 1)) t = 5 blue_border[t:h - t, t:w - t] *= 0 c0 = create_circle(w, h, 0.3) * yellow * 100000 c1 = create_circle(w, h, 0.07, 0.8, 0.8) * red * 10000 circles = np.clip(c0 + c1 + blue_border, 0, 1) r, g, b = circles.swapaxes(0, 2) luma = snowy.reshape(r + g + b) mask = luma != 0.0 sdf = snowy.unitize(np.abs(snowy.generate_sdf(mask))) cpcf = snowy.generate_cpcf(mask) voronoi = np.empty(circles.shape) np.copyto(voronoi, snowy.dereference_coords(circles, cpcf)) luma = np.dstack([luma, luma, luma]) sdf = np.dstack([sdf, sdf, sdf]) final = np.hstack([circles, luma, sdf, voronoi]) final = snowy.resize(final, height=400) show(final)
def test_tileable_distance(): c0 = create_circle(400, 200, 0.3) c1 = create_circle(400, 200, 0.08, 0.8, 0.8) circles = np.clip(c0 + c1, 0, 1) mask = circles != 0.0 sdf = snowy.unitize(snowy.generate_sdf(mask, wrapx=True, wrapy=True)) nx, ny = snowy.gradient(sdf) grad = snowy.unitize(nx + ny) stack2 = np.hstack([sdf, sdf, grad, grad]) snowy.show(snowy.resize(np.vstack([stack2, stack2]), 600, 200)) get_mask = lambda L, U: np.logical_and(sdf > L, sdf < U) get_contour = lambda L, U: np.where(get_mask(L, U), sdf, 0) sdf -= get_contour(.20, .25) sdf -= get_contour(.60, .65) sdf -= get_contour(.90, .95) snowy.show(snowy.resize(np.hstack([sdf, sdf, sdf, sdf]), height=300))
def create_island(seed, gradient, freq=3.5): w, h = 750, 512 falloff = create_falloff(w, h) n1 = 1.000 * snowy.generate_noise(w, h, freq*1, seed+0) n2 = 0.500 * snowy.generate_noise(w, h, freq*2, seed+1) n3 = 0.250 * snowy.generate_noise(w, h, freq*4, seed+2) n4 = 0.125 * snowy.generate_noise(w, h, freq*8, seed+3) elevation = falloff * (falloff / 2 + n1 + n2 + n3 + n4) mask = elevation < 0.4 elevation = snowy.unitize(snowy.generate_sdf(mask)) if GRAY_ISLAND: return (1 - mask) * np.power(elevation, 3.0) elevation = snowy.generate_sdf(mask) - 100 * n4 mask = np.where(elevation < 0, 1, 0) el = 128 + 127 * elevation / np.amax(elevation) return applyColorGradient(el, gradient)
def Distance_extraction(self,prediction, target): #print(target.shape) #target = target[:,:,:,0].float().cuda() prediction_stack = [] prediction = F.softmax(prediction, dim=1) #print(prediction.shape) for idx,image in enumerate(prediction): tensor=image.argmax(0).cpu().numpy() tensor = np.uint8(tensor) edges = cv2.Canny(tensor, 0.5, 1) edges = edges != 0 edges = np.expand_dims(edges, axis=-1) sdf = snowy.unitize(snowy.generate_sdf(edges))[:,:,0] prediction_stack.append(torch.tensor(sdf,requires_grad=True).float().cuda()) prediction_stack =torch.stack(prediction_stack, dim=0) #print(target.shape) #print(prediction_stack.shape) loss = self.distance_loss(prediction_stack, target) #print(loss) return loss
apply_lut = interpolate.interp1d(xvals, yvals, axis=0) return apply_lut(snowy.unshape(np.clip(elevation_image, 0, 255))) def create_falloff(w, h, radius=0.4, cx=0.5, cy=0.5): hw, hh = 0.5 / w, 0.5 / h x = np.linspace(hw, 1 - hw, w) y = np.linspace(hh, 1 - hh, h) u, v = np.meshgrid(x, y, sparse=True) d2 = (u-cx)**2 + (v-cy)**2 return 1-snowy.unitize(snowy.reshape(d2)) c0 = create_circle(200, 200, 0.3) c1 = create_circle(200, 200, 0.08, 0.8, 0.8) c0 = np.clip(c0 + c1, 0, 1) circles = snowy.add_border(c0, value=1) sdf = snowy.unitize(snowy.generate_sdf(circles != 0.0)) stack = snowy.hstack([circles, sdf]) snowy.export(stack, qualify('sdf.png')) snowy.show(stack) # Islands def create_island(seed, gradient, freq=3.5): w, h = 750, 512 falloff = create_falloff(w, h) n1 = 1.000 * snowy.generate_noise(w, h, freq*1, seed+0) n2 = 0.500 * snowy.generate_noise(w, h, freq*2, seed+1) n3 = 0.250 * snowy.generate_noise(w, h, freq*4, seed+2) n4 = 0.125 * snowy.generate_noise(w, h, freq*8, seed+3) elevation = falloff * (falloff / 2 + n1 + n2 + n3 + n4) mask = elevation < 0.4 elevation = snowy.unitize(snowy.generate_sdf(mask))
def get_dfm_image(sketch): dfm_image = snowy.unitize( snowy.generate_sdf(np.expand_dims(1 - sketch, 2) != 0)).squeeze() return dfm_image
def test_noise_smoothness(): noise = 0.5 + 0.5 * snowy.generate_noise(300, 150, 4, seed=42) grad = snowy.gradient(noise) grad = grad[0] + grad[1] grad = snowy.unitize(grad) snowy.show(grad)