-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbezier.py
110 lines (93 loc) · 3.69 KB
/
bezier.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
"""Bezier, a module for creating Bezier curves.
Version 1.1, from < BezierCurveFunction-v1.ipynb > on 2019-05-02
Source: https://github.com/torresjrjr/Bezier.py/blob/master/Bezier.py
"""
import numpy as np
import random
__all__ = ["Bezier"]
class Bezier():
def TwoPoints(t, P1, P2):
"""
Returns a point between P1 and P2, parametised by t.
INPUTS:
t float/int; a parameterisation.
P1 numpy array; a point.
P2 numpy array; a point.
OUTPUTS:
Q1 numpy array; a point.
"""
if not isinstance(P1, np.ndarray) or not isinstance(P2, np.ndarray):
raise TypeError('Points must be an instance of the numpy.ndarray!')
if not isinstance(t, (int, float)):
raise TypeError('Parameter t must be an int or float!')
Q1 = (1 - t) * P1 + t * P2
return Q1
def Points(t, points):
"""
Returns a list of points interpolated by the Bezier process
INPUTS:
t float/int; a parameterisation.
points list of numpy arrays; points.
OUTPUTS:
newpoints list of numpy arrays; points.
"""
newpoints = []
#print("points =", points, "\n")
for i1 in range(0, len(points) - 1):
#print("i1 =", i1)
#print("points[i1] =", points[i1])
newpoints += [Bezier.TwoPoints(t, points[i1], points[i1 + 1])]
#print("newpoints =", newpoints, "\n")
return newpoints
def Point(t, points):
"""
Returns a point interpolated by the Bezier process
INPUTS:
t float/int; a parameterisation.
points list of numpy arrays; points.
OUTPUTS:
newpoint numpy array; a point.
"""
newpoints = points
#print("newpoints = ", newpoints)
while len(newpoints) > 1:
newpoints = Bezier.Points(t, newpoints)
#print("newpoints in loop = ", newpoints)
#print("newpoints = ", newpoints)
#print("newpoints[0] = ", newpoints[0])
return newpoints[0]
def Curve(t_values, points):
"""
Returns a point interpolated by the Bezier process
INPUTS:
t_values list of floats/ints; a parameterisation.
points list of numpy arrays; points.
OUTPUTS:
curve list of numpy arrays; points.
"""
if not hasattr(t_values, '__iter__'):
raise TypeError("`t_values` Must be an iterable of integers or floats, of length greater than 0 .")
if len(t_values) < 1:
raise TypeError("`t_values` Must be an iterable of integers or floats, of length greater than 0 .")
if not isinstance(t_values[0], (int, float)):
raise TypeError("`t_values` Must be an iterable of integers or floats, of length greater than 0 .")
curve = np.array([[0.0] * len(points[0])])
for t in t_values:
#print("curve \n", curve)
#print("Bezier.Point(t, points) \n", Bezier.Point(t, points))
curve = np.append(curve, [Bezier.Point(t, points)], axis=0)
#print("curve after \n", curve, "\n--- --- --- --- --- --- ")
curve = np.delete(curve, 0, 0)
#print("curve final \n", curve, "\n--- --- --- --- --- --- ")
return curve
def generate_random_ndt_motion(length=5, height=10, width=10):
points = []
for i in range(length):
points.append(
[
random.randrange(0, width),
random.randrange(0, height)
]
)
t_points = np.arange(0, 1, 0.01)
return Bezier.Curve(t_points, np.array(points))