| 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 | trait fromint(T) { | 
|---|
| 21 | void ?{}(T&, int); | 
|---|
| 22 | }; | 
|---|
| 23 | trait zeroinit(T) { | 
|---|
| 24 | void ?{}(T&, zero_t); | 
|---|
| 25 | }; | 
|---|
| 26 | trait zero_assign(T) { | 
|---|
| 27 | T ?=?(T&, zero_t); | 
|---|
| 28 | }; | 
|---|
| 29 | trait subtract(T) { | 
|---|
| 30 | T ?-?(T, T); | 
|---|
| 31 | }; | 
|---|
| 32 | trait negate(T) { | 
|---|
| 33 | T -?(T); | 
|---|
| 34 | }; | 
|---|
| 35 | trait add(T) { | 
|---|
| 36 | T ?+?(T, T); | 
|---|
| 37 | }; | 
|---|
| 38 | trait multiply(T) { | 
|---|
| 39 | T ?*?(T, T); | 
|---|
| 40 | }; | 
|---|
| 41 | trait divide(T) { | 
|---|
| 42 | T ?/?(T, T); | 
|---|
| 43 | }; | 
|---|
| 44 | trait lessthan(T) { | 
|---|
| 45 | int ?<?(T, T); | 
|---|
| 46 | }; | 
|---|
| 47 | trait equality(T) { | 
|---|
| 48 | int ?==?(T, T); | 
|---|
| 49 | }; | 
|---|
| 50 | trait sqrt(T) { | 
|---|
| 51 | T sqrt(T); | 
|---|
| 52 | }; | 
|---|
| 53 |  | 
|---|
| 54 | static inline { | 
|---|
| 55 | // int | 
|---|
| 56 | int ?=?(int& n, zero_t) { return n = 0.f; } | 
|---|
| 57 | // unsigned int | 
|---|
| 58 | int ?=?(unsigned int& n, zero_t) { return n = 0.f; } | 
|---|
| 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 |  | 
|---|
| 70 | trait dottable(V, T) { | 
|---|
| 71 | T dot(V, V); | 
|---|
| 72 | }; | 
|---|
| 73 |  | 
|---|
| 74 | static inline { | 
|---|
| 75 |  | 
|---|
| 76 | forall(T | sqrt(T), V | dottable(V, T)) | 
|---|
| 77 | T length(V v) { | 
|---|
| 78 | return sqrt(dot(v, v)); | 
|---|
| 79 | } | 
|---|
| 80 |  | 
|---|
| 81 | forall(T, V | dottable(V, T)) | 
|---|
| 82 | T length_squared(V v) { | 
|---|
| 83 | return dot(v, v); | 
|---|
| 84 | } | 
|---|
| 85 |  | 
|---|
| 86 | forall(T, V | { T length(V); } | subtract(V)) | 
|---|
| 87 | T distance(V v1, V v2) { | 
|---|
| 88 | return length(v1 - v2); | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | forall(T, V | { T length(V); V ?/?(V, T); }) | 
|---|
| 92 | V normalize(V v) { | 
|---|
| 93 | return v / length(v); | 
|---|
| 94 | } | 
|---|
| 95 |  | 
|---|
| 96 | // Project vector u onto vector v | 
|---|
| 97 | forall(T, V | dottable(V, T) | { V normalize(V); V ?*?(V, T); }) | 
|---|
| 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 | 
|---|
| 104 | forall(T | fromint(T), V | { V project(V, V); V ?*?(T, V); V ?-?(V,V); }) | 
|---|
| 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 | 
|---|
| 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); }) | 
|---|
| 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 | 
|---|
| 130 | forall(T | lessthan(T) | zeroinit(T), V | dottable(V, T) | negate(V)) | 
|---|
| 131 | V faceforward(V n, V i, V ng) { | 
|---|
| 132 | return dot(ng, i) < (T){0} ? n : -n; | 
|---|
| 133 | } | 
|---|
| 134 |  | 
|---|
| 135 | } // inline | 
|---|