/
splines.py
62 lines (42 loc) · 1.83 KB
/
splines.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
import numpy as np
from scipy import interpolate
from field import Field
from geometry import Geometry
class Spline2D(Geometry, Field):
def __init__(self, degree, uKnots, vKnots, coeffs):
super(Spline2D, self).__init__()
self.coeffs = coeffs
self.coeffElems = len(coeffs)
self.uCoeffsLength = len(uKnots) - degree - 1
self.vCoeffsLength = len(vKnots) - degree - 1
self.tcks = []
for i in range(self.coeffElems):
self.tcks.append([uKnots, vKnots, coeffs[i], degree, degree])
def __coeffsExtremum(self, axis, f):
coeffs = self.coeffs[axis]
extremum = coeffs[0]
for i in range(1, len(coeffs)):
extremum = f(extremum, coeffs[i])
return extremum
def min(self, axis):
return self.__coeffsExtremum(axis, min)
def max(self, axis):
return self.__coeffsExtremum(axis, max)
def evaluate(self, x, y):
result = np.empty(self.coeffElems)
for i in range(self.coeffElems):
result[i] = interpolate.bisplev(x, y, self.tcks[i])
return result
def evaluatePartialDerivativeU(self, x, y):
result = np.empty(self.coeffElems)
for i in range(self.coeffElems):
result[i] = interpolate.bisplev(x, y, self.tcks[i], dx=1)
return result
def evaluatePartialDerivativeV(self, x, y):
result = np.empty(self.coeffElems)
for i in range(self.coeffElems):
result[i] = interpolate.bisplev(x, y, self.tcks[i], dy=1)
return result
def jacob(self, u, v):
return np.matrix([self.evaluatePartialDerivativeU(u, v),
self.evaluatePartialDerivativeV(u, v)]).transpose()