[eef8dfb] | 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 | |
---|
[3376ec9] | 16 | #pragma once |
---|
| 17 | |
---|
| 18 | #include <math.hfa> |
---|
| 19 | |
---|
[dd3576b] | 20 | forall(T) |
---|
| 21 | trait fromint { |
---|
[3376ec9] | 22 | void ?{}(T&, int); |
---|
| 23 | }; |
---|
[dd3576b] | 24 | forall(T) |
---|
| 25 | trait zeroinit { |
---|
[3376ec9] | 26 | void ?{}(T&, zero_t); |
---|
| 27 | }; |
---|
[dd3576b] | 28 | forall(T) |
---|
| 29 | trait zero_assign { |
---|
[3376ec9] | 30 | T ?=?(T&, zero_t); |
---|
| 31 | }; |
---|
[dd3576b] | 32 | forall(T) |
---|
| 33 | trait subtract { |
---|
[3376ec9] | 34 | T ?-?(T, T); |
---|
| 35 | }; |
---|
[dd3576b] | 36 | forall(T) |
---|
| 37 | trait negate { |
---|
[3376ec9] | 38 | T -?(T); |
---|
| 39 | }; |
---|
[dd3576b] | 40 | forall(T) |
---|
| 41 | trait add { |
---|
[3376ec9] | 42 | T ?+?(T, T); |
---|
| 43 | }; |
---|
[dd3576b] | 44 | forall(T) |
---|
| 45 | trait multiply { |
---|
[3376ec9] | 46 | T ?*?(T, T); |
---|
| 47 | }; |
---|
[dd3576b] | 48 | forall(T) |
---|
| 49 | trait divide { |
---|
[3376ec9] | 50 | T ?/?(T, T); |
---|
| 51 | }; |
---|
[dd3576b] | 52 | forall(T) |
---|
| 53 | trait lessthan { |
---|
[3376ec9] | 54 | int ?<?(T, T); |
---|
| 55 | }; |
---|
[dd3576b] | 56 | forall(T) |
---|
| 57 | trait equality { |
---|
[3376ec9] | 58 | int ?==?(T, T); |
---|
| 59 | }; |
---|
[dd3576b] | 60 | forall(T) |
---|
| 61 | trait sqrt { |
---|
[3376ec9] | 62 | T sqrt(T); |
---|
| 63 | }; |
---|
| 64 | |
---|
| 65 | static inline { |
---|
| 66 | // int |
---|
| 67 | int ?=?(int& n, zero_t) { return n = 0.f; } |
---|
[ae3db00] | 68 | // unsigned int |
---|
| 69 | int ?=?(unsigned int& n, zero_t) { return n = 0.f; } |
---|
[3376ec9] | 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 | |
---|
[fd54fef] | 81 | trait dottable(V, T) { |
---|
[3376ec9] | 82 | T dot(V, V); |
---|
| 83 | }; |
---|
| 84 | |
---|
| 85 | static inline { |
---|
| 86 | |
---|
[fd54fef] | 87 | forall(T | sqrt(T), V | dottable(V, T)) |
---|
[3376ec9] | 88 | T length(V v) { |
---|
| 89 | return sqrt(dot(v, v)); |
---|
| 90 | } |
---|
| 91 | |
---|
[fd54fef] | 92 | forall(T, V | dottable(V, T)) |
---|
[3376ec9] | 93 | T length_squared(V v) { |
---|
| 94 | return dot(v, v); |
---|
| 95 | } |
---|
| 96 | |
---|
[fd54fef] | 97 | forall(T, V | { T length(V); } | subtract(V)) |
---|
[3376ec9] | 98 | T distance(V v1, V v2) { |
---|
| 99 | return length(v1 - v2); |
---|
| 100 | } |
---|
| 101 | |
---|
[fd54fef] | 102 | forall(T, V | { T length(V); V ?/?(V, T); }) |
---|
[3376ec9] | 103 | V normalize(V v) { |
---|
| 104 | return v / length(v); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | // Project vector u onto vector v |
---|
[fd54fef] | 108 | forall(T, V | dottable(V, T) | { V normalize(V); V ?*?(V, T); }) |
---|
[3376ec9] | 109 | V project(V u, V v) { |
---|
| 110 | V v_norm = normalize(v); |
---|
| 111 | return v_norm * dot(u, v_norm); |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | // Reflect incident vector v with respect to surface with normal n |
---|
[fd54fef] | 115 | forall(T | fromint(T), V | { V project(V, V); V ?*?(T, V); V ?-?(V,V); }) |
---|
[3376ec9] | 116 | V reflect(V v, V n) { |
---|
| 117 | return v - (T){2} * project(v, n); |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | // Refract incident vector v with respect to surface with normal n |
---|
| 121 | // eta is the ratio of indices of refraction between starting material and |
---|
| 122 | // entering material (i.e., from air to water, eta = 1/1.33) |
---|
| 123 | // v and n must already be normalized |
---|
[fd54fef] | 124 | forall(T | fromint(T) | subtract(T) | multiply(T) | add(T) | lessthan(T) | sqrt(T), |
---|
| 125 | V | dottable(V, T) | { V ?*?(T, V); V ?-?(V,V); void ?{}(V&, zero_t); }) |
---|
[3376ec9] | 126 | V refract(V v, V n, T eta) { |
---|
| 127 | T dotValue = dot(n, v); |
---|
| 128 | T k = (T){1} - eta * eta * ((T){1} - dotValue * dotValue); |
---|
| 129 | if (k < (T){0}) { |
---|
| 130 | return 0; |
---|
| 131 | } |
---|
| 132 | return eta * v - (eta * dotValue + sqrt(k)) * n; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | // Given a perturbed normal and a geometric normal, |
---|
| 136 | // flip the perturbed normal if the geometric normal is pointing away |
---|
| 137 | // from the observer. |
---|
| 138 | // n is the perturbed vector that we want to align |
---|
| 139 | // i is the incident vector |
---|
| 140 | // ng is the geometric normal of the surface |
---|
[fd54fef] | 141 | forall(T | lessthan(T) | zeroinit(T), V | dottable(V, T) | negate(V)) |
---|
[3376ec9] | 142 | V faceforward(V n, V i, V ng) { |
---|
| 143 | return dot(ng, i) < (T){0} ? n : -n; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | } // inline |
---|