-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlissajous.py
238 lines (220 loc) · 8.25 KB
/
lissajous.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import numpy as np
import plotly.graph_objects as go
def lissajous_knot(a, b, c, phi1, phi2, n_points, offset=1.5):
"""
Calculate X, Y and Z cartesian coordinates for Lissajous knots according.
Reference: LISSAJOUS KNOTS - M. G. V. BOGLE, J. E. HEARST, V. F. R. JONES and L. STOILOV
Parameters
----------
a : int
Parameter for the X coordinates.
b : int
Parameter for the Y coordinates.
c : int
Parameter for the Z coordinates.
phi1 : float
Phase value for the X coordinates.
phi2 : float
Phase value for the Y coordinates.
n_points : int
Number of curve sampling points.
offset : float, optional
Percentual distance between 3D object and 2D projections.
Returns
-------
Arrays for XYZ coordinates of 3D curve and XYZ coordinates of 2D projections.
"""
t = np.linspace(-np.pi, np.pi, n_points)
x = np.cos(a * t + phi1)
y = np.cos(b * t + phi2)
z = np.cos(c * t)
x_offset = np.min(x) * np.ones(x.shape) * offset
y_offset = np.min(y) * np.ones(y.shape) * offset
z_offset = np.min(z) * np.ones(z.shape) * offset
return x, y, z, x_offset, y_offset, z_offset
def plot_curves(x, y, z, x_offset, y_offset, z_offset, figsize=(850, 850)):
"""
Plot 3D Lissajous Knot and its 2D projections.
Parameters
----------
x : array
X axis cartesian coordinates of the 3D curve.
y : array
Y axis cartesian coordinates of the 3D curve.
z : array
Z axis cartesian coordinates of the 3D curve.
x_offset : array
X axis cartesian coordinates of the 2D projection.
y_offset : array
Y axis cartesian coordinates of the 2D projection.
z_offset : array
Z axis cartesian coordinates of the 2D projection.
figsize : tuple, optional
Plotly Figure width and height in pixels.
Returns
-------
Plotly Figure object.
"""
fig = go.Figure()
n_points = len(x)
for n in range(n_points):
showlegend = True if n == 0 else False
name = "3D"
fig.add_trace(go.Scatter3d(
x=[x[n], x[n + 1]] if n < n_points - 1 else [x[n], x[0]],
y=[y[n], y[n + 1]] if n < n_points - 1 else [y[n], y[0]],
z=[z[n], z[n + 1]] if n < n_points - 1 else [z[n], z[0]],
mode="lines",
line=dict(
color='darkgoldenrod',
width=6,
),
name=name,
legendgroup=name,
showlegend=showlegend,
))
name = "X Offset"
fig.add_trace(go.Scatter3d(
x=[x_offset[n], x_offset[n + 1]] if n < n_points - 1 else [x_offset[n], x_offset[0]],
y=[y[n], y[n + 1]] if n < n_points - 1 else [y[n], y[0]],
z=[z[n], z[n + 1]] if n < n_points - 1 else [z[n], z[0]],
mode="lines",
line=dict(
color='mediumspringgreen',
width=3,
),
name=name,
legendgroup=name,
showlegend=showlegend,
))
name = "Y Offset"
fig.add_trace(go.Scatter3d(
x=[x[n], x[n + 1]] if n < n_points - 1 else [x[n], x[0]],
y=[y_offset[n], y_offset[n + 1]] if n < n_points - 1 else [y_offset[n], y_offset[0]],
z=[z[n], z[n + 1]] if n < n_points - 1 else [z[n], z[0]],
mode="lines",
line=dict(
color='mediumspringgreen',
width=3,
),
name=name,
legendgroup=name,
showlegend=showlegend,
))
name = "Z Offset"
fig.add_trace(go.Scatter3d(
x=[x[n], x[n + 1]] if n < n_points - 1 else [x[n], x[0]],
y=[y[n], y[n + 1]] if n < n_points - 1 else [y[n], y[0]],
z=[z_offset[n], z_offset[n + 1]] if n < n_points - 1 else [z_offset[n], z_offset[0]],
mode="lines",
line=dict(
color='mediumspringgreen',
width=3,
),
name=name,
legendgroup=name,
showlegend=showlegend,
))
fig.update_layout(
title=dict(
text="<b>" + f"Lissajous Knot" + "</b>",
xanchor="left",
xref="paper",
x=0,
yanchor="bottom",
yref="paper",
y=1,
pad=dict(b=3, l=0, r=0, t=0)
),
template="plotly_dark",
width=figsize[0], height=figsize[1],
scene_camera=dict(
eye=dict(x=1.50, y=1.50, z=1.50),
up=dict(x=0, y=0, z=1),
center=dict(x=0, y=0, z=0),
projection_type="orthographic"),
scene={'xaxis_title': "x",
'yaxis_title': "y",
'zaxis_title': "z",
'xaxis': dict(showticklabels=False,
showgrid=False,
showline=True,
zeroline=False,
showbackground=False),
'yaxis': dict(showticklabels=False,
showgrid=False,
showline=True,
zeroline=False,
showbackground=False),
'zaxis': dict(showticklabels=False,
showgrid=False,
showline=True,
zeroline=False,
showbackground=False),
},
)
fig.update_layout({
"plot_bgcolor": "rgba(0, 0, 0, 0)",
"paper_bgcolor": "rgba(0, 0, 0, 0)",
})
fig.update_layout(
updatemenus=[
dict(
buttons=list([
dict(
args=[{"scene.camera.eye": {"x": 1.5, "y": 1.5, "z": 1.5},
"scene.camera.up": {"x": 0, "y": 0, "z": 1},
"scene.camera.center": {"x": 0, "y": 0, "z": 0}}],
label="Default",
method="relayout",
),
dict(
args=[{"scene.camera.eye": {"x": 0, "y": 0, "z": 2.5},
"scene.camera.up": {"x": 0, "y": 1, "z": 0},
"scene.camera.center": {"x": 0, "y": 0, "z": 0}}],
label="Top",
method="relayout",
),
dict(
args=[{"scene.camera.eye": {"x": 2.5, "y": 0, "z": 0},
"scene.camera.up": {"x": 0, "y": 0, "z": 1},
"scene.camera.center": {"x": 0, "y": 0, "z": 0}}],
label="Lateral Right",
method="relayout",
),
dict(
args=[{"scene.camera.eye": {"x": -2.5, "y": 0, "z": 0},
"scene.camera.up": {"x": 0, "y": 0, "z": 1},
"scene.camera.center": {"x": 0, "y": 0, "z": 0}}],
label="Lateral Left",
method="relayout",
),
dict(
args=[{"scene.camera.eye": {"x": 0, "y": 2.5, "z": 0},
"scene.camera.up": {"x": 0, "y": 1, "z": 1},
"scene.camera.center": {"x": 0, "y": 0, "z": 0}}],
label="Front",
method="relayout",
),
dict(
args=[{"scene.camera.eye": {"x": 0, "y": -2.5, "z": 0},
"scene.camera.up": {"x": 0, "y": 1, "z": 1},
"scene.camera.center": {"x": 0, "y": 0, "z": 0}}],
label="Rear",
method="relayout",
),
]),
type="buttons",
direction="right",
pad={"r": 10, "t": 10},
showactive=True,
x=0,
y=0,
xanchor="left",
yanchor="top",
bgcolor="grey",
font_color="black"
),
]
)
return fig