1 | #pragma once |
---|
2 | |
---|
3 | #include <math.hfa> |
---|
4 | |
---|
5 | trait fromint(otype T) { |
---|
6 | void ?{}(T&, int); |
---|
7 | }; |
---|
8 | trait zeroinit(otype T) { |
---|
9 | void ?{}(T&, zero_t); |
---|
10 | }; |
---|
11 | trait zero_assign(otype T) { |
---|
12 | T ?=?(T&, zero_t); |
---|
13 | }; |
---|
14 | trait subtract(otype T) { |
---|
15 | T ?-?(T, T); |
---|
16 | }; |
---|
17 | trait negate(otype T) { |
---|
18 | T -?(T); |
---|
19 | }; |
---|
20 | trait add(otype T) { |
---|
21 | T ?+?(T, T); |
---|
22 | }; |
---|
23 | trait multiply(otype T) { |
---|
24 | T ?*?(T, T); |
---|
25 | }; |
---|
26 | trait divide(otype T) { |
---|
27 | T ?/?(T, T); |
---|
28 | }; |
---|
29 | trait lessthan(otype T) { |
---|
30 | int ?<?(T, T); |
---|
31 | }; |
---|
32 | trait equality(otype T) { |
---|
33 | int ?==?(T, T); |
---|
34 | }; |
---|
35 | trait sqrt(otype T) { |
---|
36 | T sqrt(T); |
---|
37 | }; |
---|
38 | |
---|
39 | static inline { |
---|
40 | // int |
---|
41 | int ?=?(int& n, zero_t) { return n = 0.f; } |
---|
42 | // unsigned int |
---|
43 | int ?=?(unsigned int& n, zero_t) { return n = 0.f; } |
---|
44 | /* float */ |
---|
45 | void ?{}(float& a, int b) { a = b; } |
---|
46 | float ?=?(float& n, zero_t) { return n = 0.f; } |
---|
47 | /* double */ |
---|
48 | void ?{}(double& a, int b) { a = b; } |
---|
49 | double ?=?(double& n, zero_t) { return n = 0L; } |
---|
50 | // long double |
---|
51 | void ?{}(long double& a, int b) { a = b; } |
---|
52 | long double ?=?(long double& n, zero_t) { return n = 0L; } |
---|
53 | } |
---|
54 | |
---|
55 | trait dottable(otype V, otype T) { |
---|
56 | T dot(V, V); |
---|
57 | }; |
---|
58 | |
---|
59 | static inline { |
---|
60 | |
---|
61 | forall(otype T | sqrt(T), otype V | dottable(V, T)) |
---|
62 | T length(V v) { |
---|
63 | return sqrt(dot(v, v)); |
---|
64 | } |
---|
65 | |
---|
66 | forall(otype T, otype V | dottable(V, T)) |
---|
67 | T length_squared(V v) { |
---|
68 | return dot(v, v); |
---|
69 | } |
---|
70 | |
---|
71 | forall(otype T, otype V | { T length(V); } | subtract(V)) |
---|
72 | T distance(V v1, V v2) { |
---|
73 | return length(v1 - v2); |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | forall(otype T, otype V | { T length(V); V ?/?(V, T); }) |
---|
78 | V normalize(V v) { |
---|
79 | return v / length(v); |
---|
80 | } |
---|
81 | |
---|
82 | // Project vector u onto vector v |
---|
83 | forall(otype T, otype V | dottable(V, T) | { V normalize(V); V ?*?(V, T); }) |
---|
84 | V project(V u, V v) { |
---|
85 | V v_norm = normalize(v); |
---|
86 | return v_norm * dot(u, v_norm); |
---|
87 | } |
---|
88 | |
---|
89 | // Reflect incident vector v with respect to surface with normal n |
---|
90 | forall(otype T | fromint(T), otype V | { V project(V, V); V ?*?(T, V); V ?-?(V,V); }) |
---|
91 | V reflect(V v, V n) { |
---|
92 | return v - (T){2} * project(v, n); |
---|
93 | } |
---|
94 | |
---|
95 | // Refract incident vector v with respect to surface with normal n |
---|
96 | // eta is the ratio of indices of refraction between starting material and |
---|
97 | // entering material (i.e., from air to water, eta = 1/1.33) |
---|
98 | // v and n must already be normalized |
---|
99 | forall(otype T | fromint(T) | subtract(T) | multiply(T) | add(T) | lessthan(T) | sqrt(T), |
---|
100 | otype V | dottable(V, T) | { V ?*?(T, V); V ?-?(V,V); void ?{}(V&, zero_t); }) |
---|
101 | V refract(V v, V n, T eta) { |
---|
102 | T dotValue = dot(n, v); |
---|
103 | T k = (T){1} - eta * eta * ((T){1} - dotValue * dotValue); |
---|
104 | if (k < (T){0}) { |
---|
105 | return 0; |
---|
106 | } |
---|
107 | return eta * v - (eta * dotValue + sqrt(k)) * n; |
---|
108 | } |
---|
109 | |
---|
110 | // Given a perturbed normal and a geometric normal, |
---|
111 | // flip the perturbed normal if the geometric normal is pointing away |
---|
112 | // from the observer. |
---|
113 | // n is the perturbed vector that we want to align |
---|
114 | // i is the incident vector |
---|
115 | // ng is the geometric normal of the surface |
---|
116 | /* forall(| add(T) | multiply(T) | lessthan(T) | fromint(T) | subtract(T)) */ |
---|
117 | forall(otype T | lessthan(T) | zeroinit(T), otype V | dottable(V, T) | negate(V)) |
---|
118 | V faceforward(V n, V i, V ng) { |
---|
119 | return dot(ng, i) < (T){0} ? n : -n; |
---|
120 | } |
---|
121 | |
---|
122 | } // inline |
---|