-
Notifications
You must be signed in to change notification settings - Fork 0
/
curve.py
76 lines (64 loc) · 2.97 KB
/
curve.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
'''
Description: Cubic bezier curve interpolation
Author: movic
Date:2020-06-19
'''
import argparse
import bezier
import numpy as np
import matplotlib.pyplot as plt
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--seed", type=int, default=0, help="Random seed.")
parser.add_argument("--max", type=float, default=512, help="Maximum value.")
parser.add_argument("--num", type=int, default=512, help="Number of points.")
args = parser.parse_args()
print(args)
np.random.seed(args.seed)
ctrl_pt1 = np.array([[32, 80, 8, 48], [32, 64, 384, 448]])
curve1 = bezier.Curve(ctrl_pt1, degree=3)
curve1_x, curve1_y = curve1.evaluate_multi(np.linspace(0.0, 1.0, args.num))
plt.subplot(1, 2, 1).plot(curve1_x, curve1_y)
plt.subplot(1, 2, 2).plot(curve1_x, curve1_y)
ctrl_pt2 = np.array([[48, 256, 320, 448], [448, 480, 352, 480]])
curve2 = bezier.Curve(ctrl_pt2, degree=3)
curve2_x, curve2_y = curve2.evaluate_multi(np.linspace(0.0, 1.0, args.num))
plt.subplot(1, 2, 1).plot(curve2_x, curve2_y)
plt.subplot(1, 2, 2).plot(curve2_x, curve2_y)
ctrl_pt3 = np.array([[32, 256, 320, 464], [32, 64, 224, 48]])
curve3 = bezier.Curve(ctrl_pt3, degree=3)
curve3_x, curve3_y = curve3.evaluate_multi(np.linspace(0.0, 1.0, args.num))
plt.subplot(1, 2, 1).plot(curve3_x, curve3_y)
plt.subplot(1, 2, 2).plot(curve3_x, curve3_y)
ctrl_pt4 = np.array([[464, 464, 320, 448], [48, 64, 224, 480]])
curve4 = bezier.Curve(ctrl_pt4, degree=3)
curve4_x, curve4_y = curve4.evaluate_multi(np.linspace(0.0, 1.0, args.num))
plt.subplot(1, 2, 1).plot(curve4_x, curve4_y)
plt.subplot(1, 2, 2).plot(curve4_x, curve4_y)
canvas = np.zeros((args.max, args.max), dtype=np.float32)
ts = np.linspace(0.0, 1.0, 2048)
for t in ts:
pt1 = curve2.evaluate(t).squeeze()
pt2 = curve3.evaluate(t).squeeze()
ctrl_1 = (1 - t) * ctrl_pt1[:, 1] + t * ctrl_pt4[:, 1]
ctrl_2 = (1 - t) * ctrl_pt1[:, 2] + t * ctrl_pt4[:, 2]
ctrl = list(zip(pt1, ctrl_2, ctrl_1, pt2))
curve = bezier.Curve(ctrl, degree=3)
coordinates = curve.evaluate_multi(np.linspace(0.0, 1.0, args.num)).astype(np.int32)
canvas[coordinates[1], coordinates[0]] = t
plt.subplot(1, 2, 1).imshow(canvas, cmap="jet", vmin=0, vmax=1)
canvas = np.zeros((args.max, args.max), dtype=np.float32)
ts = np.linspace(0.0, 1.0, 2048)
for t in ts:
pt1 = curve4.evaluate(t).squeeze()
pt2 = curve1.evaluate(t).squeeze()
ctrl_1 = (1 - t) * ctrl_pt3[:, 1] + t * ctrl_pt2[:, 1]
ctrl_2 = (1 - t) * ctrl_pt3[:, 2] + t * ctrl_pt2[:, 2]
ctrl = list(zip(pt1, ctrl_2, ctrl_1, pt2))
curve = bezier.Curve(ctrl, degree=3)
coordinates = curve.evaluate_multi(np.linspace(0.0, 1.0, args.num)).astype(np.int32)
canvas[coordinates[1], coordinates[0]] = t
plt.subplot(1, 2, 2).imshow(canvas, cmap="jet", vmin=0, vmax=1)
plt.show()
if __name__ == '__main__':
main()