-
Notifications
You must be signed in to change notification settings - Fork 0
/
spline.py
146 lines (124 loc) · 5.92 KB
/
spline.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
# -*- coding: utf-8 -*-
from __future__ import division
import scipy as sp
import numpy as np
import matplotlib.pyplot as plt
from Nurbs import Crv, Util
from Nurbs.Util import NURBSError
import util as util
from math import radians, pi
class SplineError(NURBSError):
pass
class Spline(Crv.Crv):
def basisarray(self):
degree = self.degree
knots = self.uknots
def knotfraction(u0, u1, u2, u3):
if u2 == u3: return 0
return (u0 - u1)/(u2 - u3)
def basisfunc(x, i, deg):
if deg == 0:
if knots[i + 1] == knots[-1]:
return float(knots[i] <= x <= knots[i + 1])
if knots[i] == knots[i + 1]: return 0.
return float(knots[i] <= x < knots[i + 1])
return knotfraction(x,knots[i], knots[i + deg], knots[i])*basisfunc(
x, i, deg - 1) + knotfraction(knots[i + deg + 1], x, knots[i + deg + 1], knots[i + 1]
)*basisfunc(x, i + 1, deg - 1)
self.basislist = [[(lambda x, i = j, deg = degr: basisfunc(x, i, deg)) for j in range(len(knots) - 1 - degr)] for degr in range(degree + 1)]
return self.basislist
def basisderivative(self, deriv):
basislist = self.basislist
knots = self.uknots
degree = len(basislist) - 1
if type(deriv) != int:
raise SplineError, "Deriv has to be an integer"
if deriv < 1:
raise SplineError, " Deriv has to be 1 or larger"
basisfuncs = basislist[-1 - deriv]
def knotfraction(deg, u1, u2):
if u1 == u2: return 0
return 1/float(u1 - u2)
def derivative(x, i, deg, der):
if der == 1:
return knotfraction(deg, knots[i + deg], knots[i])*basisfuncs[i](x
) - knotfraction(deg, knots[i + deg + 1], knots[i + 1])*basisfuncs[i + 1](x)
return knotfraction(deg, knots[deg + i], knots[i])*derivative(x, i, deg - 1, der - 1
) - knotfraction(deg, knots[deg + i + 1], knots[i + 1])*derivative(
x, i + 1, deg - 1, der - 1)
derivativelist = [(lambda x, i = j, deg = degree, der = deriv: derivative(x, i, deg, deriv)) for j in range(len(basislist[-1]))]
return derivativelist
def plot2D(self, n = 25, points = True, fig = None, text = True, color = 'k'):
pnts = self.pnt3D(np.arange(n + 1, dtype = np.float64)/n)
knot = self.pnt3D(self.uknots)
ctrl = self.cntrl[:3]/self.cntrl[3]
"""
plt.plot(pnts[0], pnts[1], label='parametric bspline')
if points:
plt.plot(ctrl[0], ctrl[1], 'ro-', label='control pts', linewidth=.5)
plt.plot(knot[0], knot[1], 'y+', markersize = 10, markeredgewidth=1.8, label="knots")
"""
if fig == None:
fig = plt.figure()
ax = fig.add_subplot(111)
if points:
if text:
ax.set_title( "B-spline curve, degree={0}".format(self.degree) )
ax.plot(pnts[0], pnts[1], color, label='parametric bspline')
ax.plot(ctrl[0], ctrl[1], 'ro-', label='control pts', linewidth=.5)
ax.plot(knot[0], knot[1], 'y+', markersize = 10, markeredgewidth=1.8, label="knots")
else:
ax.plot(pnts[0], pnts[1], color)
ax.plot(ctrl[0], ctrl[1], 'ro-')
ax.plot(knot[0], knot[1], 'y+')
else:
if text:
ax.set_title( "B-spline curve, degree={0}".format(self.degree) )
ax.plot(pnts[0], pnts[1], color, label='parametric bspline')
else:
ax.plot(pnts[0], pnts[1], color)
ax.legend(fontsize='x-small',bbox_to_anchor=(0.95, .9), loc=2, borderaxespad=-1.)
def _derivcntrl(self, update = None):
"""
A support function that calculates all the control points for the derivatives
"""
derivmatrix = [self.cntrl]
for i in range(1, self.degree):
cntrlmatrix = np.ones((4, len(derivmatrix[i - 1][0]) - 1))
for j in range(len(cntrlmatrix[0])):
for k in range(3):
cntrlmatrix[k, j] = (self.degree + 1 - i)*(derivmatrix[i - 1][k, j + 1]-derivmatrix[i - 1][k, j])/(self.uknots[self.degree + i]- self.uknots[i])
derivmatrix.append(cntrlmatrix.copy())
return derivmatrix
def nderiv(self, n):
"""
Computes the n'th derivative of the b-spline and returns it as a b-spline
as described by De Boor, C. 'On Calculating With B-splines' 1972.
It is assumed that the curve is clamped to the interval [0,1] with a multiplicity
one greater than the degree.
"""
if n >= self.degree:
raise Util.NURBSError, 'Unable to differentiate up to or more than the degree.'
# cntrl = self.cntrl
# knots = self.uknots
self._dcntrl = self._derivcntrl()
return Spline(self._dcntrl[n],self.uknots[n:-n])
class Arc(Crv.Arc):
def plot2D(self, n = 25, points = True, color = 'k'):
pnts = self.pnt3D(np.arange(n + 1, dtype = np.float64)/n)
knot = self.pnt3D(self.uknots)
ctrl = self.cntrl[:3]/self.cntrl[3]
plt.plot(pnts[0], pnts[1], color, label='parametric bspline')
if points:
plt.plot(ctrl[0], ctrl[1], 'ro-', label='control pts', linewidth=.5)
plt.plot(knot[0], knot[1], 'y+', markersize = 10, markeredgewidth=1.8, label="knots")
"""
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_title( "b-spline Curve, degree={0}".format(self.degree) )
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.plot(pnts[0], pnts[1], label='parametric bspline')
ax.plot(ctrl[0], ctrl[1], 'ro-', label='control pts', linewidth=.5)
ax.plot(knot[0], knot[1], 'y+', markersize = 10, markeredgewidth=1.8, label="knots")
"""