-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbeziercurve.py
96 lines (79 loc) · 3.31 KB
/
beziercurve.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
import matplotlib.pyplot as plt
import numpy as np
from scipy.special import comb
from beziermatrix import bezier_matrix
def bij(t, i, n):
# binomial coefficients
return comb(n, i) * (t ** i) * ((1-t) ** (n-i))
def draw_bezier(ctrlPoints, rWeights = None, nCtrlPoints = 0, nPointsCurve = 100, start_xy = None, annotate = True, return_curve=False,
ctrlPointPlotKwargs = dict(marker='X', color='r', linestyle='--'), curvePlotKwagrs = dict(color='g'),
draw_axis = plt):
'''
Draws a Bezier curve with given control points
ctrlPoints: shape (n+1, 2) matrix containing all control points
nCtrlPoints: No. of control points. If 0, infered from 'ctrlPoints', otherwise consideres first 'nCtrlPoints' points from 'ctrlPoints'
nPointsCurve: granularity of the Bezier curve
return_curve: returns the points on the curve rather than drawing them
ctrlPointPlotKwargs: The **kwargs for control point's plot() function
curvePlotKwagrs: The **kwargs for curve's plot() function
'''
def T(ts: 'time points', d: 'degree'):
# 'ts' is a vector (np.array) of time points
ts = ts[..., np.newaxis]
Q = tuple(ts**n for n in range(d, -1, -1))
return np.concatenate(Q, 1)
if nCtrlPoints == 0:
# Infer the no. of control points
nCtrlPoints, _ = ctrlPoints.shape
else:
# If given, pick first `nCtrlPoints` control points
ctrlPoints = ctrlPoints[0:nCtrlPoints, :]
# curve = np.zeros((nPointsCurve, 2))
# for step, t in enumerate(np.linspace(0.0, 1.0, num = nPointsCurve)):
# s = np.zeros_like(ctrlPoints[0]) # Basically [0., 0.]
# for pointID, point in enumerate(ctrlPoints):
# # 'point' has shape (2,)
# s += bij(t, pointID, nCtrlPoints-1) * point
# curve[step] = s
ts = np.linspace(0., 1., num = nPointsCurve)
if rWeights is None:
curve = np.matmul(
T(ts, nCtrlPoints - 1),
np.matmul(
bezier_matrix(nCtrlPoints-1),
ctrlPoints
)
)
else:
curve = np.matmul(
T(ts, nCtrlPoints - 1),
np.matmul(
bezier_matrix(nCtrlPoints-1),
np.diag(rWeights)
)
)
curve = curve / np.expand_dims(curve.sum(1), 1)
curve = np.matmul(curve, ctrlPoints)
if return_curve: # Return the points of the curve as 'np.array'
return curve
if start_xy is not None:
curve = curve + start_xy
ctrlPoints = ctrlPoints + start_xy
# Plot the curve
draw_axis.plot(ctrlPoints[:,0], ctrlPoints[:,1], **ctrlPointPlotKwargs)
for n, ctrlPoint in enumerate(ctrlPoints):
if annotate:
draw_axis.annotate(str(n), (ctrlPoint[0], ctrlPoint[1]), color=ctrlPointPlotKwargs['color'])
draw_axis.plot(curve[:,0], curve[:,1], **curvePlotKwagrs)
for n, curvePoint in enumerate(curve):
if n % 10 == 0 and annotate:
draw_axis.annotate(str(n), (curvePoint[0], curvePoint[1]), color=curvePlotKwagrs['color'])
if __name__ == '__main__':
## Sample usage of the 'draw_bezier()' function
# few definitions
degree = 4
# random control points over [-30,30] range
ctrlPoints = np.random.randint(-30, 30, (degree + 1, 2)).astype(np.float_)
fig = plt.figure()
draw_bezier(ctrlPoints, draw_axis=plt.gca())
plt.show()