|
@@ -37,21 +37,25 @@ def rot_p(p_center, p, angle_d):
|
37
|
37
|
p = [qx, qy]
|
38
|
38
|
return p
|
39
|
39
|
|
|
40
|
+def interpolate(p1, p2, step):
|
|
41
|
+ p = []
|
|
42
|
+ for i in range(0, 2):
|
|
43
|
+ diff = p2[i] - p1[i]
|
|
44
|
+ v = p1[i] + diff * step
|
|
45
|
+ p.append(v)
|
|
46
|
+ return p
|
|
47
|
+
|
40
|
48
|
def read_image(filename, path_steps, volume_percent, angle_d):
|
41
|
49
|
paths, attributes = svg2paths(filename)
|
42
|
50
|
print("paths={}".format(len(paths)))
|
|
51
|
+ for i, path in enumerate(paths):
|
|
52
|
+ print("path={} segments={}".format(i, len(path)))
|
43
|
53
|
|
|
54
|
+ # find center point to rotate around
|
44
|
55
|
p0 = [paths[0][0].start.real, paths[0][0].start.imag]
|
45
|
56
|
p_min = [p0[0], p0[1]]
|
46
|
57
|
p_max = [p0[0], p0[1]]
|
47
|
|
-
|
48
|
58
|
for path in paths:
|
49
|
|
- print("segments={}".format(len(path)))
|
50
|
|
-
|
51
|
|
- # find center
|
52
|
|
- dist_min = float('inf')
|
53
|
|
- dist_max = 0
|
54
|
|
- p_prev = p_min
|
55
|
59
|
for segment in path:
|
56
|
60
|
p = [segment.end.real, segment.end.imag]
|
57
|
61
|
for i in range(0, 2):
|
|
@@ -59,16 +63,9 @@ def read_image(filename, path_steps, volume_percent, angle_d):
|
59
|
63
|
p_min[i] = p[i]
|
60
|
64
|
if p[i] > p_max[i]:
|
61
|
65
|
p_max[i] = p[i]
|
62
|
|
-
|
63
|
|
- dist_curr = math.sqrt((p[0] - p_prev[0]) ** 2 + (p[1] - p_prev[1]) ** 2)
|
64
|
|
- p_prev = p
|
65
|
|
- if dist_curr > dist_max:
|
66
|
|
- dist_max = dist_curr
|
67
|
|
- if dist_curr < dist_min:
|
68
|
|
- dist_min = dist_curr
|
69
|
|
-
|
70
|
66
|
p_center = [ p_min[0] + (p_max[0] - p_min[0]) / 2, p_min[1] + (p_max[1] - p_min[1]) / 2 ]
|
71
|
67
|
|
|
68
|
+ # find bounding box for rotated object
|
72
|
69
|
for path in paths:
|
73
|
70
|
# TODO this is not nice.
|
74
|
71
|
# Doing both range(0, 360, 5) and angle_d is redundant.
|
|
@@ -100,7 +97,6 @@ def read_image(filename, path_steps, volume_percent, angle_d):
|
100
|
97
|
|
101
|
98
|
print("min={} max={}".format(p_min, p_max))
|
102
|
99
|
print("center={} ".format(p_center))
|
103
|
|
- print("dist min={} max={} ".format(dist_min, dist_max))
|
104
|
100
|
|
105
|
101
|
data = bytearray()
|
106
|
102
|
|
|
@@ -114,41 +110,32 @@ def read_image(filename, path_steps, volume_percent, angle_d):
|
114
|
110
|
c = int((v * 2 - 1) * (32767 / 100 * volume_percent))
|
115
|
111
|
data.extend(c.to_bytes(2, byteorder="little", signed=True))
|
116
|
112
|
|
117
|
|
- def interpolate(p1, p2, step):
|
118
|
|
- p = []
|
119
|
|
- for i in range(0, 2):
|
120
|
|
- diff = p2[i] - p1[i]
|
121
|
|
- v = p1[i] + diff * step
|
122
|
|
- p.append(v)
|
123
|
|
- return p
|
124
|
|
-
|
125
|
|
- def add_segment(p1, p2, f):
|
126
|
|
- p = interpolate(p1, p2, f)
|
127
|
|
- add_point(p)
|
128
|
|
-
|
129
|
|
- def add_path(p1, p2):
|
|
113
|
+ def add_segment(p1, p2):
|
130
|
114
|
l = math.sqrt((p2[0] - p1[0]) ** 2 + (p2[1] - p1[1]) ** 2)
|
131
|
115
|
ps = max(1, int(path_steps * l))
|
132
|
116
|
for step in range(0, ps):
|
133
|
|
- add_segment(p1, p2, step / ps)
|
|
117
|
+ p = interpolate(p1, p2, step / ps)
|
|
118
|
+ add_point(p)
|
134
|
119
|
|
135
|
120
|
for path in paths:
|
136
|
|
- p = [path[0].start.real, path[0].start.imag]
|
137
|
|
- p = rot_p(p_center, p , angle_d)
|
138
|
|
- points = [p]
|
|
121
|
+ p0 = [path[0].start.real, path[0].start.imag]
|
|
122
|
+ points = [rot_p(p_center, p0, angle_d)]
|
139
|
123
|
for segment in path:
|
140
|
124
|
p = [segment.end.real, segment.end.imag]
|
141
|
|
- p = rot_p(p_center, p , angle_d)
|
|
125
|
+ p = rot_p(p_center, p, angle_d)
|
142
|
126
|
points.append(p)
|
143
|
127
|
|
144
|
128
|
# walk path forwards
|
145
|
129
|
for n in range(0, len(points) - 1):
|
146
|
|
- add_path(points[n], points[n + 1])
|
147
|
|
- add_point(points[len(points) - 1])
|
|
130
|
+ add_segment(points[n], points[n + 1])
|
148
|
131
|
|
149
|
132
|
# walk path backwards
|
|
133
|
+ add_point(points[len(points) - 1])
|
150
|
134
|
for n in range(len(points) - 2, -1, -1):
|
151
|
|
- add_path(points[n + 1], points[n])
|
|
135
|
+ add_segment(points[n + 1], points[n])
|
|
136
|
+ add_point(points[0])
|
|
137
|
+
|
|
138
|
+ # improve start/end of path. TODO only in virtual scope?
|
152
|
139
|
add_point(points[0])
|
153
|
140
|
|
154
|
141
|
return data
|