schillerCFK commited on
Commit
8ed8d69
·
verified ·
1 Parent(s): 501ec50

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +102 -4
app.py CHANGED
@@ -181,7 +181,7 @@ def calculate_and_plot_divergence(z1, d1, z2, d2, size_type):
181
  full_deg = half_deg * 2
182
  full_mrad = half_mrad * 2
183
 
184
- # 結果文字 (使用雙括號 {{ }} 確保 f-string 能正確顯示 LaTeX 語法,並改用括號避免換行符號問題)
185
  text_result = (
186
  f"### 💡 光束發散計算結果:\n"
187
  f"- 測量距離差 (${{\\Delta L}}$): **{delta_L:.2f} mm**\n"
@@ -220,6 +220,67 @@ def calculate_and_plot_divergence(z1, d1, z2, d2, size_type):
220
  plt.tight_layout()
221
  return text_result, fig
222
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
223
  # --- 更新輸入框標籤函數 ---
224
  def update_offset_label(unit):
225
  if unit == "毫米 (mm)":
@@ -235,6 +296,15 @@ def update_dist_label(unit):
235
  else:
236
  return gr.update(label="測試距離 L (mm)")
237
 
 
 
 
 
 
 
 
 
 
238
  # --- 2. Gradio 介面設計 ---
239
  init_ws = load_rzf_config()
240
 
@@ -299,7 +369,6 @@ with gr.Blocks(title=COMPANY_NAME) as demo:
299
  with gr.Column():
300
  calc_output_plot_2 = gr.Plot(label="Coaxiality Plot")
301
 
302
- # 新增:光束發散分頁
303
  with gr.Tab("🔦 光束發散計算"):
304
  gr.Markdown("## 🔦 測量兩點計算光束發散角 (Beam Divergence)")
305
  with gr.Row():
@@ -328,6 +397,31 @@ with gr.Blocks(title=COMPANY_NAME) as demo:
328
  4. **全角發散** (業界標準):$\\theta_{full} = 2 \\times \\theta_{half}$
329
  """)
330
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
331
  with gr.Tab("📖 規格說明"):
332
  gr.Markdown("### 📘 術語定義\n- **mRad**: 毫弧度,雷射發散角與同軸度常用的專業單位。\n- **發散角**: 光束隨距離擴散的程度,通常以全角 (Full-Angle) 標示。")
333
 
@@ -349,16 +443,20 @@ with gr.Blocks(title=COMPANY_NAME) as demo:
349
  download_btn.click(get_backup_files, outputs=file_output)
350
 
351
  # 同軸度計算
352
- # 綁定單位切換事件,即時更新輸入框標籤
353
  dist_unit_1.change(update_dist_label, inputs=dist_unit_1, outputs=dist_input_1)
354
  dist_unit_2.change(update_dist_label, inputs=dist_unit_2, outputs=dist_input_2)
355
  off_unit.change(update_offset_label, inputs=off_unit, outputs=off_input)
356
-
357
  calc_btn_1.click(calc_angle_from_offset, [dist_input_1, dist_unit_1, off_input, off_type, off_unit], [calc_output_text_1, calc_output_plot_1])
358
  calc_btn_2.click(calc_offset_from_angle, [dist_input_2, dist_unit_2, angle_input, ang_type], [calc_output_text_2, calc_output_plot_2])
359
 
360
  # 光束發散計算
361
  div_calc_btn.click(calculate_and_plot_divergence, [z1_in, d1_in, z2_in, d2_in, size_type_div], [div_out_text, div_out_plot])
362
 
 
 
 
 
 
 
363
  # 部署在 Hugging Face Spaces 時不需加 share=True
364
  demo.launch(theme=gr.themes.Soft())
 
181
  full_deg = half_deg * 2
182
  full_mrad = half_mrad * 2
183
 
184
+ # 結果文字
185
  text_result = (
186
  f"### 💡 光束發散計算結果:\n"
187
  f"- 測量距離差 (${{\\Delta L}}$): **{delta_L:.2f} mm**\n"
 
220
  plt.tight_layout()
221
  return text_result, fig
222
 
223
+
224
+ # --- 線光斑長度與角度 (Line Laser Angle) 邏輯 ---
225
+
226
+ def calculate_and_plot_line_angle(distance, dist_unit, line_length, length_unit):
227
+ if distance <= 0: return "❌ 錯誤:距離必須大於 0", None
228
+ if line_length < 0: return "❌ 錯誤:線長不能為負數", None
229
+
230
+ # 單位轉換至 mm
231
+ dist_mm = distance * 10.0 if dist_unit == "公分 (cm)" else (distance * 1000.0 if dist_unit == "公尺 (m)" else distance)
232
+ len_mm = line_length * 10.0 if length_unit == "公分 (cm)" else (line_length * 1000.0 if length_unit == "公尺 (m)" else line_length)
233
+
234
+ # 半線長
235
+ half_l = len_mm / 2.0
236
+
237
+ # 計算半角
238
+ half_rad = math.atan(half_l / dist_mm)
239
+ half_deg = math.degrees(half_rad)
240
+
241
+ # 計算全角
242
+ full_deg = half_deg * 2
243
+
244
+ text_result = (
245
+ f"### 💡 線斑角度計算結果:\n"
246
+ f"- 測試距離 ($L$): **{dist_mm:.2f} mm**\n"
247
+ f"- 線光斑總長 ($l$): **{len_mm:.2f} mm**\n"
248
+ f"- 半線長 ($l/2$): **{half_l:.2f} mm**\n\n"
249
+ f"#### 📐 發散角度結果\n"
250
+ f"- **半角 (Half-Angle, ${{\\theta/2}}$)**: **{half_deg:.4f}°**\n"
251
+ f"- **發散全角 (Full-Angle, ${{\\theta}}$)**: **{full_deg:.4f}°**"
252
+ )
253
+
254
+ # 繪製圖表
255
+ fig, ax = plt.subplots(figsize=(6, 4), dpi=100)
256
+ ax.set_title(f"{COMPANY_NAME} - Line Laser Angle", fontsize=12)
257
+
258
+ # 繪製等腰三角形 (模擬線光斑展開)
259
+ triangle_coords = [[0, 0], [dist_mm, half_l], [dist_mm, -half_l]]
260
+ poly = plt.Polygon(triangle_coords, closed=True, facecolor='#FFEBEE', edgecolor='#D32F2F', linewidth=2, alpha=0.6)
261
+ ax.add_patch(poly)
262
+
263
+ # 中心線
264
+ ax.plot([0, dist_mm*1.1], [0, 0], color='#9E9E9E', linestyle='-.', linewidth=1)
265
+
266
+ # 標註雷射光線 (紅線)
267
+ ax.plot([dist_mm, dist_mm], [-half_l, half_l], color='#B71C1C', linewidth=3)
268
+
269
+ # 文字標註
270
+ ax.text(dist_mm / 2, -half_l * 0.1, f"L = {dist_mm:.1f} mm", ha='center', va='top', color='#424242')
271
+ ax.text(dist_mm * 1.05, 0, f"l = {len_mm:.1f} mm", va='center', rotation=90, color='#B71C1C')
272
+ ax.text(dist_mm * 0.15, half_l * 0.05, f"Θ ≈ {full_deg:.2f}°", fontsize=11, color='#D32F2F', fontweight='bold')
273
+
274
+ # 動態調整邊界避免圖形過於狹長被裁切
275
+ y_limit = max(half_l, dist_mm * 0.2) * 1.2
276
+ ax.set_xlim(-0.1 * dist_mm, 1.3 * dist_mm)
277
+ ax.set_ylim(-y_limit, y_limit)
278
+ ax.axis('off')
279
+
280
+ plt.tight_layout()
281
+ return text_result, fig
282
+
283
+
284
  # --- 更新輸入框標籤函數 ---
285
  def update_offset_label(unit):
286
  if unit == "毫米 (mm)":
 
296
  else:
297
  return gr.update(label="測試距離 L (mm)")
298
 
299
+ def update_line_length_label(unit):
300
+ if unit == "公分 (cm)":
301
+ return gr.update(label="線光斑長度 l (cm)")
302
+ elif unit == "公尺 (m)":
303
+ return gr.update(label="線光斑長度 l (m)")
304
+ else:
305
+ return gr.update(label="線光斑長度 l (mm)")
306
+
307
+
308
  # --- 2. Gradio 介面設計 ---
309
  init_ws = load_rzf_config()
310
 
 
369
  with gr.Column():
370
  calc_output_plot_2 = gr.Plot(label="Coaxiality Plot")
371
 
 
372
  with gr.Tab("🔦 光束發散計算"):
373
  gr.Markdown("## 🔦 測量兩點計算光束發散角 (Beam Divergence)")
374
  with gr.Row():
 
397
  4. **全角發散** (業界標準):$\\theta_{full} = 2 \\times \\theta_{half}$
398
  """)
399
 
400
+ # 新增:線斑角度計算分頁
401
+ with gr.Tab("📏 線斑角度計算"):
402
+ gr.Markdown("## 📏 測量線光斑長度計算發散角度 (Line Laser Angle)")
403
+ with gr.Row():
404
+ with gr.Column():
405
+ with gr.Row():
406
+ dist_input_line = gr.Number(label="測試距離 L (mm)", value=100)
407
+ dist_unit_line = gr.Radio(["毫米 (mm)", "公分 (cm)", "公尺 (m)"], label="距離單位", value="毫米 (mm)")
408
+
409
+ with gr.Row():
410
+ length_input_line = gr.Number(label="線光斑長度 l (mm)", value=100)
411
+ length_unit_line = gr.Radio(["毫米 (mm)", "公分 (cm)", "公尺 (m)"], label="長度單位", value="毫米 (mm)")
412
+
413
+ calc_btn_line = gr.Button("開始計算角度", variant="primary")
414
+ calc_output_text_line = gr.Markdown("等待輸入...")
415
+
416
+ with gr.Column():
417
+ calc_output_plot_line = gr.Plot(label="Line Laser Angle Plot")
418
+ gr.Markdown("""
419
+ **計算原理說明**:
420
+ 1. 將打出的線光斑切半,與光源形成直角三角形:對邊為半線長 $l / 2$,鄰邊為測試距離 $L$。
421
+ 2. **半角**:$\\theta_{half} = \\arctan(\\frac{l / 2}{L})$
422
+ 3. **全角 (發散角)**:$\\theta_{full} = 2 \\times \\theta_{half}$
423
+ """)
424
+
425
  with gr.Tab("📖 規格說明"):
426
  gr.Markdown("### 📘 術語定義\n- **mRad**: 毫弧度,雷射發散角與同軸度常用的專業單位。\n- **發散角**: 光束隨距離擴散的程度,通常以全角 (Full-Angle) 標示。")
427
 
 
443
  download_btn.click(get_backup_files, outputs=file_output)
444
 
445
  # 同軸度計算
 
446
  dist_unit_1.change(update_dist_label, inputs=dist_unit_1, outputs=dist_input_1)
447
  dist_unit_2.change(update_dist_label, inputs=dist_unit_2, outputs=dist_input_2)
448
  off_unit.change(update_offset_label, inputs=off_unit, outputs=off_input)
 
449
  calc_btn_1.click(calc_angle_from_offset, [dist_input_1, dist_unit_1, off_input, off_type, off_unit], [calc_output_text_1, calc_output_plot_1])
450
  calc_btn_2.click(calc_offset_from_angle, [dist_input_2, dist_unit_2, angle_input, ang_type], [calc_output_text_2, calc_output_plot_2])
451
 
452
  # 光束發散計算
453
  div_calc_btn.click(calculate_and_plot_divergence, [z1_in, d1_in, z2_in, d2_in, size_type_div], [div_out_text, div_out_plot])
454
 
455
+ # 線斑角度計算
456
+ dist_unit_line.change(update_dist_label, inputs=dist_unit_line, outputs=dist_input_line)
457
+ length_unit_line.change(update_line_length_label, inputs=length_unit_line, outputs=length_input_line)
458
+ calc_btn_line.click(calculate_and_plot_line_angle, [dist_input_line, dist_unit_line, length_input_line, length_unit_line], [calc_output_text_line, calc_output_plot_line])
459
+
460
+
461
  # 部署在 Hugging Face Spaces 時不需加 share=True
462
  demo.launch(theme=gr.themes.Soft())