-
Notifications
You must be signed in to change notification settings - Fork 0
/
BezierInterpolation3D.py
96 lines (86 loc) · 3.55 KB
/
BezierInterpolation3D.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 plotly.graph_objects as go
from plotly.offline import plot
class BezierInterpolation:
def __init__(self, matrix, N=50, t=5e-3, show_control_points=True):
self.matrix = matrix
self.N = N
self.t = t
self.show_control_points = show_control_points
def combinaison_lineaire(self, A, B, u, v):
return [A[0] * u + B[0] * v, A[1] * u + B[1] * v]
def interpolation_lineaire(self, A, B, t):
return self.combinaison_lineaire(A, B, t, 1 - t)
def reduction(self, points_control, t):
points_sortie = []
N = len(points_control)
for i in range(N - 1):
points_sortie.append(self.interpolation_lineaire(points_control[i], points_control[i + 1], 1 - t))
return points_sortie
def point_bezier_n(self, points_control, t):
n = len(points_control)
while n > 1:
points_control = self.reduction(points_control, t)
n = len(points_control)
return points_control[0]
def courbe_bezier_n(self, points_control, N):
n = len(points_control)
dt = 1.0 / N
t = dt
points_courbe = [points_control[0]]
while t < 1.0:
points_courbe.append(self.point_bezier_n(points_control, t))
t = t + dt
points_courbe.append(points_control[n - 1])
return points_courbe
def plot_points(self, points_courbe):
x = []
y = []
for p in points_courbe:
x.append(p[0])
y.append(p[1])
return x, y
def show_bezier(self):
fig = go.Figure()
row, col = self.matrix.shape
# === vertical
for j in range(row):
Pi = [[i, (self.matrix[j])[i]] for i in range(col)]
points = self.courbe_bezier_n(Pi, 1000)
x_bezier, y_bezier = self.plot_points(points)
fig.add_scatter3d(y=[j] * len(x_bezier),
z=y_bezier,
x=x_bezier,
mode="lines",
name="Bezier curve"
)
if self.show_control_points:
x_control_pts, y_control_pts = self.plot_points(Pi)
fig.add_scatter3d(y=[j] * len(x_control_pts),
z=y_control_pts,
x=x_control_pts,
mode="lines+markers",
name="Control points",
marker=dict(size=3)
)
# === horizontal
for j in range(col):
Pi = [[i, (self.matrix[i])[j]] for i in range(row)]
points = self.courbe_bezier_n(Pi, 1000)
x_bezier, y_bezier = self.plot_points(points)
fig.add_scatter3d(x=[j] * len(x_bezier),
z=y_bezier,
y=x_bezier,
mode="lines",
name="Bezier curve"
)
if self.show_control_points:
x_control_pts, y_control_pts = self.plot_points(Pi)
fig.add_scatter3d(x=[j] * len(x_control_pts),
z=y_control_pts,
y=x_control_pts,
mode="lines+markers",
name="Control points",
marker=dict(size=3)
)
fig.update_layout(showlegend=False)
plot(fig)