-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsurface_profile.py
More file actions
354 lines (301 loc) · 9.24 KB
/
surface_profile.py
File metadata and controls
354 lines (301 loc) · 9.24 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import marimo
__generated_with = "0.23.3"
app = marimo.App(width="full")
@app.cell
def _():
import marimo as mo
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
from scipy.interpolate import RegularGridInterpolator
from zernike import RZern
return RegularGridInterpolator, go, mo, np
@app.cell
def _(RegularGridInterpolator, np):
def slice_3d_surface(x, y, z, x0, y0, angle_deg, num_points=100, t_range=10.0):
"""
Take a slice through a 3D surface defined by mesh grids (x, y, z) at an arbitrary point (x0, y0)
with a given angle (in degrees).
Parameters:
- x, y, z: 2D numpy arrays defining the mesh grids.
- x0, y0: The point through which the slice passes.
- angle_deg: The angle of the slice in degrees (0-360).
- num_points: Number of points along the slice.
- t_range: The range of the parameter t (distance from (x0, y0)) to consider.
Returns:
- t: Array of distances along the slice from (x0, y0).
- z_slice: Interpolated z values along the slice.
"""
# Convert angle to radians
angle_rad = np.deg2rad(angle_deg)
# Create direction vector
dx = np.cos(angle_rad)
dy = np.sin(angle_rad)
# Create parameter t (distance along the line)
t = np.linspace(-t_range, t_range, num_points)
# Compute points along the line
x_line = x0 + t * dx
y_line = y0 + t * dy
# Create interpolator for z
interpolator = RegularGridInterpolator(
(y[:, 0], x[0, :]), z, bounds_error=False, fill_value=None
)
# Interpolate z values
points = np.column_stack((x_line.ravel(), y_line.ravel()))
z_slice = interpolator(points).reshape(x_line.shape)
return t, z_slice
return (slice_3d_surface,)
@app.cell
def _(go):
def add_custom_dashed_line_3d(fig, x, y, z_min, z_max,
pattern=(0.6, 0.1, 0.1, 0.1),
color='black', width=3):
"""
Adds a vertical dashed line in 3D with a custom dash pattern.
pattern: tuple of (draw, gap, draw, gap, ...) in absolute units
e.g. (0.6, 0.1, 0.1, 0.1) means:
- draw 0.6, gap 0.1, draw 0.1, gap 0.1 (repeating)
"""
z_segments = []
x_segments = []
y_segments = []
z_current = z_min
idx = 0 # index in pattern
draw = True # start with drawing
while z_current < z_max:
segment_length = pattern[idx % len(pattern)]
z_next = min(z_current + segment_length, z_max)
if draw:
z_segments += [z_current, z_next, None]
x_segments += [x, x, None]
y_segments += [y, y, None]
z_current = z_next
idx += 1
draw = not draw # alternate draw/gap
fig.add_trace(go.Scatter3d(
x=x_segments,
y=y_segments,
z=z_segments,
mode='lines',
line=dict(color=color, width=width),
showlegend=False,
name='Custom Dashed Axis'
))
return fig
return (add_custom_dashed_line_3d,)
@app.cell
def _(np):
def eval_asphere_raw(r, R, k, asph_coeffs):
return r**2 / R / (1+np.sqrt(1-(1+k)*r**2/R**2)) + np.sum([asph_coeffs[i] * r**(2*(i+2)) for i in range(len(asph_coeffs))], axis=0)
def eval_asphere(x, y, R, k, asph_coeffs, diam_opt, diam_ext):
r = np.sqrt(x**2+y**2)
asphere_raw = eval_asphere_raw(r, R, k, asph_coeffs)
asphere_raw[(r > diam_opt/2) & (r <= diam_ext/2)] = eval_asphere_raw(diam_opt/2, R, k, asph_coeffs)
asphere_raw[r > diam_ext/2] = np.nan
return asphere_raw
return eval_asphere, eval_asphere_raw
@app.cell
def _(np):
R = 20
k = 0.0
asph_coeffs = []
diam_opt = 16
diam_ext = 20
_L, _K = 200, 250
_ddx = np.linspace(-10.0, 10.0, _K)
_ddy = np.linspace(-10.0, 10.0, _L)
xv, yv = np.meshgrid(_ddx, _ddy)
return R, asph_coeffs, diam_ext, diam_opt, k, xv, yv
@app.cell
def _(
R,
add_custom_dashed_line_3d,
asph_coeffs,
diam_ext,
diam_opt,
eval_asphere,
go,
k,
mo,
np,
slice_3d_surface,
xv,
yv,
):
zv = eval_asphere(xv, yv, R, k, asph_coeffs, diam_opt, diam_ext)
_p = go.Figure()
_p.add_trace(
go.Surface(
x=xv,
y=yv,
z=zv,
contours_z=dict(
show=True,
# usecolormap=True,
highlightcolor="limegreen",
# project_z=True,
start=-1.5,
end=1.5,
size=0.4,
color="white"
),
showscale=False,
# cmin=-1.5,
# cmax=1.5,
)
)
_p.update_layout(
width=800, # Set width (in pixels)
height=800, # Set height (in pixels)
margin=dict(l=0, r=0, b=0, t=0), # Remove margins
scene=dict(
domain=dict(x=[0, 1], y=[0, 1]), # Make the 3D scene fill the entire figure
xaxis=dict(
nticks=4,
range=[-10.0, 10.0],
),
yaxis=dict(
nticks=4,
range=[-10.0, 10.0],
),
zaxis=dict(
nticks=4,
range=[-3, 3.5],
),
),
)
_p.update_scenes(
camera=dict(
eye=dict(x=1., y=1.5, z=0.8), # Camera position (higher = farther away)
up=dict(x=0, y=0, z=1), # "Up" direction (default: z-axis)
center=dict(x=0, y=0, z=0), # Point the camera is looking at
),
aspectratio={"x": 1, "y": 1, "z": 0.5},
camera_projection_type="orthographic",
)
slice_x, slice_z = slice_3d_surface(xv, yv, zv, 0, 0, 0, num_points=100, t_range=10.0)
slice_y = np.zeros_like(slice_x)
_p.add_trace(
go.Scatter3d(
x=slice_x,
y=slice_y,
z=slice_z,
mode="lines",
line=dict(
color='red',
width=10
),showlegend=False
)
)
_p = add_custom_dashed_line_3d(_p, x=0, y=0, z_min=-3, z_max=3.5, pattern=(0.6, 0.15, 0.1, 0.15), width=5, color="white")
# Create a grid for the plane (adjust resolution as needed)
_x = np.linspace(-10, 10, 5)
_z = np.linspace(-3, 3.5, 5)
_x_grid, _z_grid = np.meshgrid(_x, _z)
# Create x-grid (constant for vertical plane)
_y_grid = np.full_like(_x_grid, 0.0)
# Add the plane to the figure
_p.add_trace(
go.Surface(
x=_x_grid,
y=_y_grid,
z=_z_grid,
opacity=0.3, # Semi-transparency (0=invisible, 1=solid)
colorscale=[[0, 'blue'], [1, 'blue']],
showscale=False, # Hide color scale
name="Vertical Plane",
)
)
_plot = mo.ui.plotly(_p)
mo.vstack([_plot])
return
@app.cell
def _(R, eval_asphere_raw, go, k, np):
_asph_coeffs = [0, -0.000001]
_r = np.linspace(-10, 10, 101)
_asph_nominal = eval_asphere_raw(_r, R, k, _asph_coeffs)
_asph_min = eval_asphere_raw(_r, R+5, k, _asph_coeffs)
_asph_max = eval_asphere_raw(_r, R-5, k, _asph_coeffs)
_asph_measured = eval_asphere_raw(_r, R+3, k, _asph_coeffs)
_asph_measured_w_noise = _asph_measured + np.random.normal(0,0.05,101)
_p = go.Figure()
_p.add_trace(
go.Line(
x=_r,
y=_asph_nominal,
line=dict(
color="royalblue",
width=2,
dash="dash",
),
name="Nominal profile"
)
)
_p.add_trace(
go.Line(
x=_r,
y=_asph_min,
line=dict(
color="royalblue",
width=0.5
),
showlegend=False
)
)
_p.add_trace(
go.Line(
x=_r,
y=_asph_max,
line=dict(
color="royalblue",
width=0.5
),
fill="tonexty",
fillcolor="rgba(166, 184, 255, 0.2)",
showlegend=False
)
)
_p.add_trace(
go.Line(
x=_r,
y=_asph_measured_w_noise,
line=dict(
color="darkgreen",
width=1,
),
name="Measured profile"
)
)
_p.add_trace(
go.Line(
x=_r,
y=_asph_measured,
line=dict(
color="darkgreen",
width=2,
# dash="dash"
),
name="Nominal profile w/ optimized radius"
)
)
_p.update_layout(
width=800, # Set width (in pixels)
height=400, # Set height (in pixels)
)
config = {
'toImageButtonOptions': {
'format': 'png', # one of png, svg, jpeg, webp
'filename': 'optimized_radius',
'height': 400,
'width': 800,
'scale': 6
}
}
# _p.update_layout(template="simple_white")
_p.show(config=config)
return
@app.cell
def _():
return
if __name__ == "__main__":
app.run()