-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPeriodicBSplineMatrix.py
More file actions
79 lines (57 loc) · 2.4 KB
/
Copy pathPeriodicBSplineMatrix.py
File metadata and controls
79 lines (57 loc) · 2.4 KB
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
import numpy as np
import matplotlib.pyplot as plt
from Bezier import Combination
from KnotVectors import PeriodicKnotVector
from BSpline import BSpline
def TMatrix(T, order):
countT = np.size(T)
TMat = np.ones([countT, order])
for i in range(order - 1):
TMat[:, i] = np.power(T, order - i - 1)
return TMat
def BasisSum(i, j, order):
value = 0.
for l in range(j, order):
value += np.power((order - (l + 1)), i) * np.power(-1., l - j) * Combination(order, l - j)
return value
def BasisMatrix(order):
Mat = np.zeros([order, order])
for i in range(order):
for j in range(order):
ki = (1.0 / np.math.factorial(order - 1)) * Combination(order - 1, i)
Mat[i, j] = ki * BasisSum(i, j , order)
return Mat
# 0.0 <= T < 1.0, evaluated for each segment
def PeriodicBSplineMatrix(Points, order, T, closed=False):
TMat = TMatrix(T, order)
Basis = BasisMatrix(order)
countPoints = np.size(Points, 0)
dimension = np.size(Points, 1)
orderSubTwo = order - 2 #k - 1 repeated/looped, subtract 2 since first vertex already there
totalPoints = countPoints + orderSubTwo * 2
PointsMatrix = np.zeros([totalPoints, dimension])
PointsMatrix[orderSubTwo:orderSubTwo + countPoints, :] = Points
if closed:
PointsMatrix[0:orderSubTwo] = Points[-orderSubTwo:, :]
PointsMatrix[orderSubTwo + countPoints:, :] = Points[:orderSubTwo, :]
else:
PointsMatrix[0:orderSubTwo] = Points[0, :]
PointsMatrix[orderSubTwo + countPoints:, :] = Points[-1, :]
SplineFinal = np.empty([0, dimension])
#have to iterate over each segment of the curve
for i in range(0, totalPoints - order + 1):
SplineSegment = np.dot(np.dot(TMat, Basis), PointsMatrix[i:i + order, :])
SplineFinal = np.append(SplineFinal, SplineSegment, axis=0)
return SplineFinal
if __name__ == "__main__":
order = 3
Points = np.array([[0., 0.], [3., 10.], [6., 3.], [10., 5.]])
countPoints = np.size(Points, 0)
T = np.arange(0.0, 1.0, 0.01)
Spline = PeriodicBSplineMatrix(Points, order, T, False)
plt.plot(Points[:, 0], Points[:, 1])
plt.plot(Spline[:, 0], Spline[:, 1])
PointsBox = np.array([[2, 0], [4, 0], [4, 2], [4, 4], [2, 4], [0, 4], [0, 2], [0, 0]])
BoxSpline = PeriodicBSplineMatrix(PointsBox, order, T, True)
plt.plot(BoxSpline[:, 0], BoxSpline[:, 1])
plt.show()