Thomas Buck 10 months ago
parent
commit
251a7a7c4d
2 changed files with 26 additions and 39 deletions
  1. 2
    2
      Makefile
  2. 24
    37
      render.py

+ 2
- 2
Makefile View File

27
 	mkdir output
27
 	mkdir output
28
 
28
 
29
 output/%.wav: %.svg
29
 output/%.wav: %.svg
30
-	./render.py -s 192000 -i 5 -o $@ $<
30
+	./render.py -s 192000 -o $@ $<
31
 
31
 
32
 output/%_rotate.wav: %.svg
32
 output/%_rotate.wav: %.svg
33
 	for number in {000..360..5} ; do \
33
 	for number in {000..360..5} ; do \
34
-	   ./render.py -s 192000 -i 5 -t 0.1 -o output/$(@F)_$$number.wav -r $$number $< ; \
34
+	   ./render.py -s 192000 -t 0.07 -o output/$(@F)_$$number.wav -r $$number $< ; \
35
 	done
35
 	done
36
 	ffmpeg -f concat -safe 0 -i <( for f in output/$(@F)_*.wav; do echo "file '$$(pwd)/$$f'"; done ) $@
36
 	ffmpeg -f concat -safe 0 -i <( for f in output/$(@F)_*.wav; do echo "file '$$(pwd)/$$f'"; done ) $@
37
 	rm output/$(@F)_*.wav
37
 	rm output/$(@F)_*.wav

+ 24
- 37
render.py View File

37
     p = [qx, qy]
37
     p = [qx, qy]
38
     return p
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
 def read_image(filename, path_steps, volume_percent, angle_d):
48
 def read_image(filename, path_steps, volume_percent, angle_d):
41
     paths, attributes = svg2paths(filename)
49
     paths, attributes = svg2paths(filename)
42
     print("paths={}".format(len(paths)))
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
     p0 = [paths[0][0].start.real, paths[0][0].start.imag]
55
     p0 = [paths[0][0].start.real, paths[0][0].start.imag]
45
     p_min = [p0[0], p0[1]]
56
     p_min = [p0[0], p0[1]]
46
     p_max = [p0[0], p0[1]]
57
     p_max = [p0[0], p0[1]]
47
-
48
     for path in paths:
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
         for segment in path:
59
         for segment in path:
56
             p = [segment.end.real, segment.end.imag]
60
             p = [segment.end.real, segment.end.imag]
57
             for i in range(0, 2):
61
             for i in range(0, 2):
59
                     p_min[i] = p[i]
63
                     p_min[i] = p[i]
60
                 if p[i] > p_max[i]:
64
                 if p[i] > p_max[i]:
61
                     p_max[i] = p[i]
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
     p_center = [ p_min[0] + (p_max[0] - p_min[0]) / 2, p_min[1] + (p_max[1] - p_min[1]) / 2 ]
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
     for path in paths:
69
     for path in paths:
73
         # TODO this is not nice.
70
         # TODO this is not nice.
74
         # Doing both range(0, 360, 5) and angle_d is redundant.
71
         # Doing both range(0, 360, 5) and angle_d is redundant.
100
 
97
 
101
     print("min={} max={}".format(p_min, p_max))
98
     print("min={} max={}".format(p_min, p_max))
102
     print("center={} ".format(p_center))
99
     print("center={} ".format(p_center))
103
-    print("dist min={} max={} ".format(dist_min, dist_max))
104
 
100
 
105
     data = bytearray()
101
     data = bytearray()
106
 
102
 
114
             c = int((v * 2 - 1) * (32767 / 100 * volume_percent))
110
             c = int((v * 2 - 1) * (32767 / 100 * volume_percent))
115
             data.extend(c.to_bytes(2, byteorder="little", signed=True))
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
         l = math.sqrt((p2[0] - p1[0]) ** 2 + (p2[1] - p1[1]) ** 2)
114
         l = math.sqrt((p2[0] - p1[0]) ** 2 + (p2[1] - p1[1]) ** 2)
131
         ps = max(1, int(path_steps * l))
115
         ps = max(1, int(path_steps * l))
132
         for step in range(0, ps):
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
     for path in paths:
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
         for segment in path:
123
         for segment in path:
140
             p = [segment.end.real, segment.end.imag]
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
             points.append(p)
126
             points.append(p)
143
 
127
 
144
         # walk path forwards
128
         # walk path forwards
145
         for n in range(0, len(points) - 1):
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
         # walk path backwards
132
         # walk path backwards
133
+        add_point(points[len(points) - 1])
150
         for n in range(len(points) - 2, -1, -1):
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
         add_point(points[0])
139
         add_point(points[0])
153
 
140
 
154
     return data
141
     return data

Loading…
Cancel
Save