forked from IDEALLab/design_embeddings_jmd_2016
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuniform_bspline.py
354 lines (282 loc) · 10.1 KB
/
uniform_bspline.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
##########################################
# File: uniform_bspline.py #
# Copyright Richard Stebbing 2014. #
# Distributed under the MIT License. #
# (See accompany file LICENSE or copy at #
# http://opensource.org/licenses/MIT) #
##########################################
# Imports
import numpy as np
import sympy as sp
from itertools import groupby
from util import previous_float, raise_if_not_shape
# __all__
__all__ = ['B',
'basis_functions',
'uniform_bspline_basis',
'UniformBSpline']
# B
@sp.cacheit
def B(i, k, x):
"""Symbolically evaluate the B-spline basis with uniform knot spacing
(http://en.wikipedia.org/wiki/B-spline).
Parameters
----------
i : int
Translation (delay) of the basis on the x-axis.
k: int
Order of the B-spline.
x : sp.Symbol
Variable.
Returns
-------
b : list of sp.Expr, len = k + i
`b[i]` is the expression (in `x`) for the basis over the half-open
interval [i, i + 1).
"""
if k < 1:
raise ValueError('k < 1 (= {})'.format(k))
if i < 0:
raise ValueError('i < 0 (= {})'.format(i))
if not isinstance(x, sp.Symbol):
raise ValueError('x is not sympy.Symbol (type(x) = "{}")'.format(
type(x).__name__))
if k == 1:
f = [sp.S.One]
else:
b0, b1 = B(0, k - 1, x), B(1, k - 1, x)
h = sp.Rational(1, k - 1)
p0 = [h* x * e for e in b0]
p0.append(sp.S.Zero)
p1 = [h * (k - x) * e for e in b1]
assert len(p0) == len(p1)
f = map(lambda e0, e1: (e0 + e1).expand(), p0, p1)
return [sp.S.Zero] * i + [e.subs({x : x - i}) for e in f]
# basis_functions
@sp.cacheit
def basis_functions(d, x):
"""Symbolically evaluate the uniform B-spline basis functions.
Parameters
----------
d : int
The degree of the uniform B-spline.
x : sp.Symbol
Interpolation parameter.
Returns
-------
b : list of sp.Expr, len = d + 1
List of B-spline basis functions, where `b[i]` is the interpolating
expression over the unit interval.
"""
if d < 0:
raise ValueError('d < 0 (= {})'.format(d))
return [ib[1].subs({x : x + ib[0]}).expand()
for ib in enumerate(B(0, d + 1, x))][::-1]
# uniform_bspline_basis
UNIFORM_BSPLINE_TEMPLATE = """def {func_name}(t):
t = np.atleast_1d(t)
if np.any((t < 0) | (t > 1)):
raise ValueError('t < 0 or t > 1')
(N,) = t.shape
W = np.empty((N, {num_control_points}), dtype=float)
{W}
return W
"""
@sp.cacheit
def uniform_bspline_basis(d, p=0):
"""Generate a "Numpy friendly" function to facilitate fast evaluation of
uniform B-spline basis functions.
Parameters
----------
d : int
The degree of the uniform B-spline.
p : optional, int
The order of the derivative with respect to the interpolation
parameter.
Returns
-------
uniform_bspline_basis_d : function
"Numpy friendly" function to evaluate the uniform B-spline
interpolation components.
"""
t = sp.Symbol('t')
b = basis_functions(d, t)
for i in range(p):
b = [sp.diff(e, t) for e in b]
func_name = 'uniform_bspline_basis_{}_{}'.format(d, p)
W = [' W[:, {}] = {}'.format(ie[0], ie[1].evalf())
for ie in enumerate(b)]
code = UNIFORM_BSPLINE_TEMPLATE.format(func_name=func_name,
num_control_points=len(W),
W='\n'.join(W))
globals_ = {'np' : np}
exec(code, globals_)
return globals_[func_name]
# UniformBSpline
class UniformBSpline(object):
"""UniformBSpline
Class to facilitate evaluation of points on, and derivatives of, a uniform
B-spline.
Parameters
----------
degree : int
The degree of the uniform B-spline.
num_control_points : int
The number of control points for the B-spline.
dim : int
Number of dimensions of each control point (typically 2 or 3).
is_closed optional, bool
True if the contour is closed, False otherwise.
"""
def __init__(self, degree, num_control_points, dim, is_closed=False):
if degree < 1:
raise ValueError('degree < 1 (= {})'.format(degree))
self._degree = degree
self.num_control_points = num_control_points
self.dim = dim
self.is_closed = is_closed
self.num_segments = (num_control_points if is_closed else
num_control_points - degree)
if self.num_segments <= 0:
raise ValueError('num_segments <= 0 (= {})'.format(
self.num_segments))
self._max_u = previous_float(self.num_segments)
self._W = uniform_bspline_basis(degree, 0)
self._Wt = uniform_bspline_basis(degree, 1)
self._Wtt = uniform_bspline_basis(degree, 2)
def uniform_parameterisation(self, N):
"""Generate a vector of coordinates `u` which parameterise linearly
distributed points on the contour.
Parameters
----------
N : int
The number of points.
Returns
-------
u : float, np.ndarray of shape = (N,)
The vector of contour coordinates.
"""
return np.linspace(0.0, self._max_u, N, endpoint=True)
def clip(self, u):
"""Clip a vector of coordinates `u` to the domain of the contour.
Parameters
----------
u : array_like
The vector of contour coordinates.
Returns
-------
clipped_u : float, np.ndarray
The vector of clipped contour coordinates.
"""
return ((np.asarray(u) % self.num_segments) if self.is_closed else
np.clip(u, 0.0, self._max_u))
def M(self, u, X):
"""Evaluate points on the contour.
Parameters
----------
u : float, array_like of shape = (N,)
The vector of contour coordinates.
X : float, array_like of shape = (num_control_points, dim)
The matrix of control point positions.
Returns
-------
M : float, np.ndarray of shape = (N, dim)
The matrix of evaluated positions.
"""
return self._f(self._W, u, X)
def Mu(self, u, X):
"""Evaluate first derivatives with respect to `u` on the contour.
Parameters
----------
u : float, array_like of shape = (N,)
The vector of contour coordinates.
X : float, array_like of shape = (num_control_points, dim)
The matrix of control point positions.
Returns
-------
Mu : float, np.ndarray of shape = (N, dim)
The matrix of evaluated first derivatives.
"""
return self._f(self._Wt, u, X)
def Muu(self, u, X):
"""Evaluate second derivatives with respect to `u` on the contour.
Parameters
----------
u : float, array_like of shape = (N,)
The vector of contour coordinates.
X : float, array_like of shape = (num_control_points, dim)
The matrix of control point positions.
Returns
-------
Mu : float, np.ndarray of shape = (N, dim)
The matrix of evaluated second derivatives.
"""
return self._f(self._Wtt, u, X)
def MX(self, u):
"""Evaluate first derivatives with respect to `X` on the contour.
Parameters
----------
u : float, array_like of shape = (N,)
The vector of contour coordinates.
Returns
-------
J : float, np.ndarray of shape = (N * dim, num_control_points * dim)
The full Jacobian of first derivatives.
`J[dim * i + k, dim * j + l]` is the derivative of the `k`th
component of the `i`th point with respect to component `l` of the
`j`th control point.
"""
return self._fX(self._W, u)
def MuX(self, u):
"""Evaluate the mixed derivatives with respect to `u` and `X` on the
contour.
Parameters
----------
u : float, array_like of shape = (N,)
The vector of contour coordinates.
Returns
-------
M : float, np.ndarray of shape = (N * dim, num_control_points * dim)
The full matrix of mixed derivatives.
`M[dim * i + k, dim * j + l]` is the derivative of the `k`th
component of the `i`th point with respect to `u[i]` and component
`l` of the `j`th control point.
"""
return self._fX(self._Wt, u)
def _f(self, f, u, X):
u, s, t = self._u_to_s_t(u)
(N,) = u.shape
X = np.atleast_2d(X)
raise_if_not_shape('X', X, (self.num_control_points, self.dim))
R = np.empty((N, self.dim), dtype=float)
for s_, i in groupby(np.argsort(s), key=lambda i: s[i]):
i = list(i)
R[i] = np.dot(f(t[i]), X[self._i(s_)])
return R
def _fX(self, f, u):
u, s, t = self._u_to_s_t(u)
(N,) = u.shape
d = self.dim
R = np.zeros((N * d, self.num_control_points * d), dtype=float)
for s_, i in groupby(np.argsort(s), key=lambda i: s[i]):
i = np.array(list(i))
for j, w in zip(self._i(s_), f(t[i]).T):
for k in range(self.dim):
R[d * i + k, d * j + k] = w
return R
def _u_to_s_t(self, u):
"""Ensure the contour coordinate `u` is valid and translate it to a
segment index `s` and segment coordinate `t`."""
u = np.atleast_1d(u)
(N,) = u.shape
s = np.floor(u).astype(int)
if np.any((s < 0) | (s >= self.num_segments)):
raise ValueError('s < 0 or s >= {}'.format(
self.num_segments))
t = u - s
assert np.all((0.0 <= t) & (t <= 1.0))
return u, s, t
def _i(self, s):
"""Provide the control point indices for segment `s`."""
return [i % self.num_control_points
for i in range(s, s + self._degree + 1)]