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