[8bbdfdd] | 1 | #pragma once |
---|
[b545cad] | 2 | #include <math.hfa> |
---|
[9a83ccd] | 3 | #include <iostream.hfa> |
---|
[8bbdfdd] | 4 | |
---|
| 5 | //---------------------- Vector Types ---------------------- |
---|
| 6 | // TODO: make generic, as per glm |
---|
| 7 | |
---|
| 8 | struct vec2 { |
---|
| 9 | float x, y; |
---|
| 10 | }; |
---|
| 11 | |
---|
[9a83ccd] | 12 | void ?{}( vec2 & v, float x, float y) { |
---|
| 13 | v.[x, y] = [x, y]; |
---|
| 14 | } |
---|
| 15 | |
---|
| 16 | forall( dtype ostype | ostream( ostype ) ) { |
---|
| 17 | ostype & ?|?( ostype & os, const vec2& v) with (v) { |
---|
| 18 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); |
---|
| 19 | fmt( os, "<%g,%g>", x, y); |
---|
| 20 | return os; |
---|
| 21 | } |
---|
[b545cad] | 22 | void ?|?( ostype & os, const vec2& v ) { |
---|
[9a83ccd] | 23 | (ostype &)(os | v); ends( os ); |
---|
| 24 | } |
---|
| 25 | } |
---|
| 26 | |
---|
[b545cad] | 27 | void ?{}(vec2& vec, zero_t) with (vec) { |
---|
| 28 | x = y = 0; |
---|
| 29 | } |
---|
| 30 | void ?{}(vec2& vec, vec2& other) with (vec) { |
---|
| 31 | [x,y] = other.[x,y]; |
---|
| 32 | } |
---|
| 33 | |
---|
[9a83ccd] | 34 | vec2 ?-?(const vec2& u, const vec2& v) { |
---|
| 35 | return [u.x - v.x, u.y - v.y]; |
---|
| 36 | } |
---|
[44f41997] | 37 | vec2 ?*?(const vec2& v, float scalar) with (v) { |
---|
| 38 | return [x * scalar, y * scalar]; |
---|
| 39 | } |
---|
[b545cad] | 40 | vec2 ?*?(float scalar, const vec2& v) { |
---|
| 41 | return v * scalar; |
---|
| 42 | } |
---|
[44f41997] | 43 | vec2 ?/?(const vec2& v, float scalar) with (v) { |
---|
| 44 | return [x / scalar, y / scalar]; |
---|
| 45 | } |
---|
[9a83ccd] | 46 | |
---|
| 47 | /* //---------------------- Geometric Functions ---------------------- */ |
---|
| 48 | /* // These functions implement the Geometric Functions section of GLSL */ |
---|
[8bbdfdd] | 49 | |
---|
[9a83ccd] | 50 | static inline float dot(const vec2& u, const vec2& v) { |
---|
| 51 | return u.x * v.x + u.y * v.y; |
---|
[8bbdfdd] | 52 | } |
---|
| 53 | |
---|
| 54 | static inline float length(const vec2& v) { |
---|
| 55 | return sqrt(dot(v, v)); |
---|
| 56 | } |
---|
[44f41997] | 57 | |
---|
| 58 | // Returns the distance betwwen v1 and v2, i.e., length(p0 - p1). |
---|
| 59 | static inline float distance(const vec2& v1, const vec2& v2) { |
---|
| 60 | return length(v1 - v2); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | static inline vec2 normalize(const vec2& v) { |
---|
| 64 | // TODO(dkobets) -- show them inversesqrt |
---|
| 65 | // https://github.com/g-truc/glm/blob/269ae641283426f7f84116f2fe333472b9c914c9/glm/detail/func_exponential.inl |
---|
| 66 | /* return v * inversesqrt(dot(v, v)); */ |
---|
| 67 | return v / sqrt(dot(v, v)); |
---|
| 68 | } |
---|
[b545cad] | 69 | |
---|
| 70 | // project vector u onto vector v |
---|
| 71 | static inline vec2 project(const vec2& u, const vec2& v) { |
---|
| 72 | vec2 v_norm = normalize(v); |
---|
| 73 | return v_norm * dot(u, v_norm); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | /* returns the reflection direction : v - 2.0 * project(v, n) |
---|
| 77 | * for incident vector v and surface normal n |
---|
| 78 | */ |
---|
| 79 | static inline vec2 reflect(const vec2& v, const vec2& n) { |
---|
| 80 | return v - 2 * project(v, n); |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | // incident vector v, surface normal n |
---|
| 84 | // eta = ratio of indices of refraction between starting material and |
---|
| 85 | // entering material (i.e., from air to water, eta = 1/1.33) |
---|
| 86 | static inline vec2 refract(const vec2& v, const vec2& n, float eta) { |
---|
| 87 | float dotValue = dot(n, v); |
---|
| 88 | float k = 1 - eta * eta * (1 - dotValue * dotValue); |
---|
| 89 | if (k < 0) { |
---|
| 90 | return 0; |
---|
| 91 | } |
---|
| 92 | return eta * v - (eta * dotValue + sqrt(k)) * n; |
---|
| 93 | } |
---|