-
Notifications
You must be signed in to change notification settings - Fork 140
/
dn.py
114 lines (101 loc) · 2.86 KB
/
dn.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import itertools
import numpy as np
_min = np.minimum
_max = np.maximum
def union(a, *bs, k=None):
def f(p):
d1 = a(p)
for b in bs:
d2 = b(p)
K = k or getattr(b, '_k', None)
if K is None:
d1 = _min(d1, d2)
else:
h = np.clip(0.5 + 0.5 * (d2 - d1) / K, 0, 1)
m = d2 + (d1 - d2) * h
d1 = m - K * h * (1 - h)
return d1
return f
def difference(a, *bs, k=None):
def f(p):
d1 = a(p)
for b in bs:
d2 = b(p)
K = k or getattr(b, '_k', None)
if K is None:
d1 = _max(d1, -d2)
else:
h = np.clip(0.5 - 0.5 * (d2 + d1) / K, 0, 1)
m = d1 + (-d2 - d1) * h
d1 = m + K * h * (1 - h)
return d1
return f
def intersection(a, *bs, k=None):
def f(p):
d1 = a(p)
for b in bs:
d2 = b(p)
K = k or getattr(b, '_k', None)
if K is None:
d1 = _max(d1, d2)
else:
h = np.clip(0.5 - 0.5 * (d2 - d1) / K, 0, 1)
m = d2 + (d1 - d2) * h
d1 = m + K * h * (1 - h)
return d1
return f
def blend(a, *bs, k=0.5):
def f(p):
d1 = a(p)
for b in bs:
d2 = b(p)
K = k or getattr(b, '_k', None)
d1 = K * d2 + (1 - K) * d1
return d1
return f
def negate(other):
def f(p):
return -other(p)
return f
def dilate(other, r):
def f(p):
return other(p) - r
return f
def erode(other, r):
def f(p):
return other(p) + r
return f
def shell(other, thickness):
def f(p):
return np.abs(other(p)) - thickness / 2
return f
def repeat(other, spacing, count=None, padding=0):
count = np.array(count) if count is not None else None
spacing = np.array(spacing)
def neighbors(dim, padding, spacing):
try:
padding = [padding[i] for i in range(dim)]
except Exception:
padding = [padding] * dim
try:
spacing = [spacing[i] for i in range(dim)]
except Exception:
spacing = [spacing] * dim
for i, s in enumerate(spacing):
if s == 0:
padding[i] = 0
axes = [list(range(-p, p + 1)) for p in padding]
return list(itertools.product(*axes))
def f(p):
q = np.divide(p, spacing, out=np.zeros_like(p), where=spacing != 0)
if count is None:
index = np.round(q)
else:
index = np.clip(np.round(q), -count, count)
indexes = [index + n for n in neighbors(p.shape[-1], padding, spacing)]
A = [other(p - spacing * i) for i in indexes]
a = A[0]
for b in A[1:]:
a = _min(a, b)
return a
return f