Files
spectro/main.py
T

105 lines
3.0 KiB
Python

import numpy as np
import matplotlib.pyplot as plt
from spectro import LightWaveVec, LightDir, diffracted_light
from math import pi, acos, cos, sqrt
from color import wavelength_to_rgb
wavelen_min_nm = 380
wavelen_max_nm = 750
wavelen_step_nm = 1
wavelens = [404.66e-9, 435.83e-9, 546.07e-9, 576.96e-9, 579.00e-9]
"""
wavelens = []
for i in range(wavelen_min_nm, wavelen_max_nm, wavelen_step_nm):
wavelens.append(i * 10**-9)
"""
light_len = 36
grating_c = 1600 * 10**-9
diffraction_orders = [-2, -1, 0]
# Order that rended earlier might be covered by other orders
# So lower order should be placed at the tail
colorlist = []
arrow_light = []
"""
dirs = []
for i in range(0, 3, 1):
for j in range(0, 3, 1):
for k in range(0, 3, 1):
if (i**2+j**2+k**2 <= 5**2):
dirs.append((j, i, k, pi/2, pi/2))
"""
dirs = [(0, 0, 0, pi / 2, 10 / 180 * pi)]
for dir in dirs:
beta_i = dir[3]
gamma_i = dir[4]
cos_alpha_i = sqrt(1 - cos(beta_i) ** 2 - cos(gamma_i) ** 2)
alpha_i = acos(cos_alpha_i)
for m in diffraction_orders:
dir_range = []
for wavelen in wavelens:
light_i = LightWaveVec(pi * 2 / wavelen, LightDir(alpha_i, beta_i, gamma_i))
colorlist.append((1, 1, 1))
arrow_light.append(
[
-cos_alpha_i * light_len,
-cos(beta_i) * light_len,
-cos(gamma_i) * light_len,
cos_alpha_i * light_len,
cos(beta_i) * light_len,
cos(gamma_i) * light_len,
]
)
light_r_list = diffracted_light(light_i, m, grating_c)
if light_r_list:
if len(dir_range) > 1:
dir_range.pop()
dir_range.append(
[
(
light.direction.alpha * 180 / pi,
light.direction.beta * 180 / pi,
light.direction.gamma * 180 / pi,
)
for light in light_r_list
]
)
for light_r in light_r_list:
colorlist.append(wavelength_to_rgb(wavelen))
arrow_light.append(
[
dir[0],
dir[1],
dir[2],
cos(light_r.direction.alpha) * light_len,
cos(light_r.direction.beta) * light_len,
cos(light_r.direction.gamma) * light_len,
]
)
print(dir_range)
X, Y, Z, U, V, W = zip(*arrow_light)
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
ax.quiver(X, Y, Z, U, V, W, colors=colorlist, arrow_length_ratio=0)
ax.set_xlim([-light_len, light_len])
ax.set_ylim([-light_len, light_len])
ax.set_zlim([-light_len, light_len])
ax.set_xlabel("X Label")
ax.set_ylabel("Y Label")
ax.set_zlabel("Z Label")
plt.show()