forked from fogleman/sdf
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdn.py
More file actions
143 lines (129 loc) · 3.92 KB
/
Copy pathdn.py
File metadata and controls
143 lines (129 loc) · 3.92 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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
#def _project(a,b,axis=1):
# return b*_dot(a,b,axis=axis)/_dot(b,b,axis=axis)
#
#def round_polygon_vertex(points, corners, radii):
# points = [np.array(pt) for pt in points]
# out = []
# #print("size p:{}".format(p.shape))
# n = len(points)
# for i in range(n):
# j = (i + n - 1) % n
# k = (i + 1) % n
# if not i in corners:
# out.append(points[i])
# else:
# radius = radii[corners.index(i)]
# vj = points[j][0:2]
# vi = points[i][0:2]
# vk = points[k][0:2]
# print("rounding corner {}".format(points[i]))
# # line-line
# if points[i][2] == 0 & points[k][2] == 0:
# print("found line-line")
# bisector = _unit(vj - vi,axis=0) + _unit(vk - vi,axis=0)
# pvk = [vk[1],-vk[0]]
# rk = _unit(_project(bisector,pvk,axis=0),axis=0) * radius
# center = _project(rk,bisector)
# out.append(vi + _project(center,vj))
# out.append(vi + _project(center,vk))