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