-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdf.py
72 lines (57 loc) · 1.85 KB
/
sdf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import numpy as np
import matplotlib.pyplot as plt
from numpy.linalg import norm
from functools import reduce
def dot(xy1, xy2):
return np.sum(np.multiply(xy1, xy2), -1)
def length2(xy):
return dot(xy, xy)
def length(xy):
return norm(xy, axis=-1)
def grid(x0=-10, x1=10, xn=1001, y0=-10, y1=10, yn=1001):
x, y = np.meshgrid(np.linspace(x0, x1, xn), np.linspace(y0, y1, yn))
return np.dstack((x, y))
def splitxy(xy):
return xy[..., 0], xy[..., 1]
def get_extent(xy):
x, y = xy[..., 0], xy[..., 1]
x0, x1 = x.min(), x.max()
y0, y1 = y.min(), y.max()
dx = (x1 - x0) / (x.shape[1] - 1)
dy = (y1 - y0) / (y.shape[0] - 1)
return [x0-dx/2, x1+dx/2, y0-dy/2, y1+dy/2]
def draw_udf(xy, z, cmap='gray', contour=False, contourc='blue'):
x, y = xy[..., 0], xy[..., 1]
plt.clf()
plt.imshow(z, cmap=cmap, vmin=0, origin='lower', extent=get_extent(xy))
plt.colorbar()
if contour:
plt.contour(x, y, z, colors=contourc)
def draw_sdf(xy, z, cmap='bwr_r', contour=False, contourc='black'):
x, y = xy[..., 0], xy[..., 1]
plt.clf()
vmax = abs(z).max()
plt.imshow(z, cmap=cmap, vmin=-vmax, vmax=vmax, origin='lower',
extent=get_extent(xy))
plt.colorbar()
if contour:
plt.contour(x, y, z, colors=contourc)
def draw_grad(xy, z, spacing=100):
x, y = xy[..., 0], xy[..., 1]
gy, gx = np.gradient(z)
s = spacing
plt.quiver(x[::s,::s], y[::s,::s], gx[::s,::s], gy[::s,::s], angles='xy')
def circle(xy, r):
return length(xy) - r
def box(xy, wh):
d = abs(xy) - wh
return length(np.maximum(d, 0)) + np.minimum(np.amax(d, axis=-1), 0)
def ubox(xy, wh):
d = abs(xy) - wh
return length(np.maximum(d, 0))
def union(*objs):
return reduce(np.minimum, objs)
def intersect(*objs):
return reduce(np.maximum, objs)
def sub(a, b):
return np.maximum(a, -b)