-
Notifications
You must be signed in to change notification settings - Fork 0
/
splinetrans.py
230 lines (196 loc) · 7.74 KB
/
splinetrans.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
from __future__ import division
from math import sqrt, hypot
from bisect import bisect_left, bisect_right
class Spline(object):
def __init__(self, p0, p1, p2, p3, N = 100):
""" Create a spline given the start point,
two control points and the end point"""
assert not (p0 == p1 == p2 == p3)
self.points = (p0, p1, p2, p3)
self.N = N
self.sampled = False
self._functionals = None
def calculate_functionals(self):
# calculate functional parameters of the spline
p = self.points
A = p[3][0] - 3*p[2][0] + 3*p[1][0] - p[0][0]
B = 3*p[2][0] - 6*p[1][0] + 3*p[0][0]
C = 3*p[1][0] - 3*p[0][0]
D = p[0][0]
E = p[3][1] - 3*p[2][1] + 3*p[1][1] - p[0][1]
F = 3*p[2][1] - 6*p[1][1] + 3*p[0][1]
G = 3*p[1][1] - 3*p[0][1]
H = p[0][1]
self._functionals = A, B, C, D, E, F, G, H
@property
def functionals(self):
if not self._functionals:
self.calculate_functionals()
return self._functionals
def get_point(self, t):
A, B, C, D, E, F, G, H = self.functionals
x = (((A*t) + B)*t + C)*t + D
y = (((E*t) + F)*t + G)*t + H
return x, y
def get_point_perp(self, t):
A, B, C, D, E, F, G, H = self.functionals
x = (((A*t) + B)*t + C)*t + D
y = (((E*t) + F)*t + G)*t + H
dx = 3*A*t**2 + 2*B*t + C
dy = 3*E*t**2 + 2*F*t + G
d = sqrt(dx**2 + dy**2)
if d/self.length < 1e-8:
if t < 0.5:
#print "t = ", t, "increased"
return self.get_point_perp(t + 0.001)
else:
#print "t = ", t, "decreased"
return self.get_point_perp(t - 0.001)
return x, y, -dy/d, dx/d
def get_point_tan_perp(self, t):
A, B, C, D, E, F, G, H = self.functionals
x = (((A*t) + B)*t + C)*t + D
y = (((E*t) + F)*t + G)*t + H
dx = 3*A*t**2 + 2*B*t + C
dy = 3*E*t**2 + 2*F*t + G
d = sqrt(dx**2 + dy**2)
if d/self.length < 1e-8:
if t < 0.5:
#print "t = ", t, "increased"
return self.get_point_tan_perp(t + 0.001)
else:
#print "t = ", t, "decreased"
return self.get_point_tan_perp(t - 0.001)
return x, y, dx/d, dy/d, -dy/d, dx/d
@property
def length(self):
if not self.sampled:
self.sample_path()
return self._length
def sample_path(self, N = None):
if not N:
N = self.N
else:
self.N = N
self.samples = [self.get_point(x / N) for x in range(N + 1)]
self.distances = [hypot(p1[0] - p2[0], p1[1] - p2[1])
for p1, p2 in zip(self.samples[:-1], self.samples[1:])]
self._length = sum(self.distances)
cum = self.cumulative = [0]
for dist in self.distances:
cum.append(cum[-1] + dist)
self.sampled = True
def get_t(self, s):
s = min(1, max(0, s))
s *= self.length
i = bisect_left(self.cumulative, s)
if i == 0:
i = 1
part = (s - self.cumulative[i]) / self.distances[i-1]
return (i*1.0 + part)/self.N
def transform_point(self, ip):
"""transform a point according to the spline trafo
x is length along the spline
y is across and in the same units as x"""
ix, iy = ip
t = self.get_t(ix/self.length)
x, y, px, py = self.get_point_perp(t)
return x + px*iy, y + py*iy
def transform_x_point(self, ix, pt):
"""transform a point pt according to the spline trafo at splinepoint x"""
t = self.get_t(ix/self.length)
x, y, dx, dy, px, py = self.get_point_tan_perp(t)
return x + dx*(pt[0]-ix) + px*pt[1], y + dy*(pt[0]-ix) + py*pt[1]
def transform_spline(self, spline):
s0T = self.transform_point(spline.points[0])
s3T = self.transform_point(spline.points[3])
s1T = self.transform_x_point(spline.points[0][0],spline.points[1])
s2T = self.transform_x_point(spline.points[3][0],spline.points[2])
return Spline(s0T, s1T, s2T, s3T)
def transform_splineline(self, splineline):
new_splines = [self.transform_spline(s) for s in splineline.splines]
return SplineLine(new_splines)
def get_clipped(self, clip_length):
x = self.length - clip_length
p0, p1 = self.points[0], self.points[1]
p2 = list(self.points[2])
p3 = self.transform_point((x, 0))
for i in (0,1):
p2[i] += p3[i] - self.points[3][i]
return Spline(p0, p1, tuple(p2), p3, self.N)
@property
def svg_path_data(self):
return "".join(self.raw_svg_path_data)
@property
def raw_svg_path_data(self):
data = ["M%.2f %.2f" % self.points[0]]
data.append("c")
for i in (1,2,3):
x = self.points[i][0] - self.points[0][0]
y = self.points[i][1] - self.points[0][1]
data.append("%.2f,%.2f " % (x, y))
return data
def __str__(self):
return "spline; start %s; control points %s; %s; end %s" % self.points
class SplineLine(object):
def __init__(self, splines):
self.splines = splines
self.cumulative = None
def cumulate(self):
cum = self.cumulative = [0]
for spline in self.splines:
cum.append(cum[-1] + spline.length)
def find_spline_at(self, s):
if not self.cumulative:
self.cumulate()
i = bisect_left(self.cumulative, s) - 1
i = max(0, min(len(self.splines) - 1, i))
return self.splines[i], s - self.cumulative[i], self.cumulative[i]
def transform_point(self, ip):
spline, x, xleft = self.find_spline_at(ip[0])
return spline.transform_point((x, ip[1]))
def transform_x_point(self, ix, pt):
spline, x, xleft = self.find_spline_at(ix)
return spline.transform_x_point(x, (pt[0] - xleft, pt[1]))
def transform_spline(self, spline):
s0T = self.transform_point(spline.points[0])
s3T = self.transform_point(spline.points[3])
s1T = self.transform_x_point(spline.points[0][0],spline.points[1])
s2T = self.transform_x_point(spline.points[3][0],spline.points[2])
return Spline(s0T, s1T, s2T, s3T)
def transform_splineline(self, splineline):
new_splines = [self.transform_spline(s) for s in splineline.splines]
return SplineLine(new_splines)
@property
def length(self):
return sum(s.length for s in self.splines)
@property
def svg_path_data(self):
#return " ".join(s.svg_path_data for s in self.splines)
data = [self.splines[0].svg_path_data]
for i in range(1, len(self.splines)):
s = self.splines[i]
if s.points[0] == self.splines[i-1].points[3]:
data.append("".join(s.raw_svg_path_data[1:]))
else:
data.append(s.svg_path_data)
return " ".join(data)
def get_clipped(self, clip_length):
splines = self.splines[:-1] + [self.splines[-1].get_clipped(clip_length)]
return SplineLine(splines)
def __str__(self):
start, end = self.splines[0].points[0], self.splines[-1].points[-1]
return "splineline; start %s; end %s" % (start, end)
class Line(Spline):
def __init__(self, p0, p1):
super(Line, self).__init__(p0, p0, p1, p1)
if __name__=="__main__":
s = Spline(5.0, -10, 20.000, -10, 20.0, 10.000, 40.0, 10.000)
lx, ly = 0, 0
for i in range(500):
t = s.get_t(i / 500)
x, y, px, py = s.get_point_perp(t)
if lx == 0 and ly == 0:
lx, ly = x, y
print t, x,y, px, py, hypot(x - lx, y - ly)
lx, ly = x, y