1 /*
2 * Copyright (c) 2019 Kevin Townsend (KTOWN)
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <math.h>
8 #include <errno.h>
9 #include <zsl/zsl.h>
10 #include <zsl/physics/optics.h>
11
12 int
zsl_phy_opt_refrac_index(zsl_real_t v,zsl_real_t * n)13 zsl_phy_opt_refrac_index(zsl_real_t v, zsl_real_t *n)
14 {
15 if (v < 0 || v > ZSL_LIGHT_SPEED) {
16 *n = NAN;
17 return -EINVAL;
18 }
19
20 *n = v / ZSL_LIGHT_SPEED;
21
22 return 0;
23 }
24
25 int
zsl_phy_opt_snell(zsl_real_t n1,zsl_real_t n2,zsl_real_t o1,zsl_real_t * o2)26 zsl_phy_opt_snell(zsl_real_t n1, zsl_real_t n2, zsl_real_t o1, zsl_real_t *o2)
27 {
28 zsl_real_t o = (n1 * ZSL_SIN(o1)) / n2;
29
30 if (n1 < 0 || n2 <= 0 || ZSL_ABS(o) > 1) {
31 *o2 = NAN;
32 return -EINVAL;
33 }
34
35 *o2 = ZSL_ASIN((n1 * ZSL_SIN(o1)) / n2);
36
37 return 0;
38 }
39
40 int
zsl_phy_opt_focus(zsl_real_t sr,zsl_real_t si,zsl_real_t * f)41 zsl_phy_opt_focus(zsl_real_t sr, zsl_real_t si, zsl_real_t *f)
42 {
43 if (sr + si == 0) {
44 *f = NAN;
45 return -EINVAL;
46 }
47
48 *f = (sr * si) / (sr + si);
49
50 return 0;
51 }
52
53 int
zsl_phy_opt_critic_angle(zsl_real_t n1,zsl_real_t n2,zsl_real_t * oc)54 zsl_phy_opt_critic_angle(zsl_real_t n1, zsl_real_t n2, zsl_real_t *oc)
55 {
56 if (n1 <= 0 || n2 < 0 || n2 > n1) {
57 *oc = NAN;
58 return -EINVAL;
59 }
60
61 *oc = ZSL_ASIN(n2 / n1);
62
63 return 0;
64 }
65
66 int
zsl_phy_opt_power(zsl_real_t f,zsl_real_t * p)67 zsl_phy_opt_power(zsl_real_t f, zsl_real_t *p)
68 {
69 if (f == 0) {
70 *p = NAN;
71 return -EINVAL;
72 }
73
74 *p = 1 / f;
75
76 return 0;
77 }
78
79 int
zsl_phy_opt_magn(zsl_real_t y1,zsl_real_t y2,zsl_real_t * m)80 zsl_phy_opt_magn(zsl_real_t y1, zsl_real_t y2, zsl_real_t *m)
81 {
82 if (y1 <= 0 || y2 < 0) {
83 *m = NAN;
84 return -EINVAL;
85 }
86
87 *m = y2 / y1;
88
89 return 0;
90 }
91
92 int
zsl_phy_opt_dif(uint8_t n,zsl_real_t lambda,zsl_real_t o,zsl_real_t * d)93 zsl_phy_opt_dif(uint8_t n, zsl_real_t lambda, zsl_real_t o, zsl_real_t *d)
94 {
95 if (lambda < 0 || ZSL_SIN(o) <= 0) {
96 *d = NAN;
97 return -EINVAL;
98 }
99
100 *d = ((zsl_real_t) n * lambda) / ZSL_SIN(o);
101
102 return 0;
103 }
104