1 | #pragma once |
---|
2 | #include "math.hfa" |
---|
3 | #include <iostream.hfa> |
---|
4 | |
---|
5 | //---------------------- Vector Types ---------------------- |
---|
6 | // TODO: make generic, as per glm |
---|
7 | |
---|
8 | |
---|
9 | struct vec2 { |
---|
10 | float x, y; |
---|
11 | }; |
---|
12 | |
---|
13 | void ?{}( vec2 & v, float x, float y) { |
---|
14 | v.[x, y] = [x, y]; |
---|
15 | } |
---|
16 | |
---|
17 | forall( dtype ostype | ostream( ostype ) ) { |
---|
18 | ostype & ?|?( ostype & os, const vec2& v) with (v) { |
---|
19 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); |
---|
20 | fmt( os, "<%g,%g>", x, y); |
---|
21 | return os; |
---|
22 | } |
---|
23 | void ?|?( ostype & os, const vec2 v ) { |
---|
24 | (ostype &)(os | v); ends( os ); |
---|
25 | } |
---|
26 | } |
---|
27 | |
---|
28 | vec2 ?-?(const vec2& u, const vec2& v) { |
---|
29 | return [u.x - v.x, u.y - v.y]; |
---|
30 | } |
---|
31 | vec2 ?*?(const vec2& v, float scalar) with (v) { |
---|
32 | return [x * scalar, y * scalar]; |
---|
33 | } |
---|
34 | vec2 ?/?(const vec2& v, float scalar) with (v) { |
---|
35 | return [x / scalar, y / scalar]; |
---|
36 | } |
---|
37 | |
---|
38 | /* //---------------------- Geometric Functions ---------------------- */ |
---|
39 | /* // These functions implement the Geometric Functions section of GLSL */ |
---|
40 | |
---|
41 | static inline float dot(const vec2& u, const vec2& v) { |
---|
42 | return u.x * v.x + u.y * v.y; |
---|
43 | } |
---|
44 | |
---|
45 | static inline float length(const vec2& v) { |
---|
46 | return sqrt(dot(v, v)); |
---|
47 | } |
---|
48 | |
---|
49 | // Returns the distance betwwen v1 and v2, i.e., length(p0 - p1). |
---|
50 | static inline float distance(const vec2& v1, const vec2& v2) { |
---|
51 | return length(v1 - v2); |
---|
52 | } |
---|
53 | |
---|
54 | static inline vec2 normalize(const vec2& v) { |
---|
55 | // TODO(dkobets) -- show them inversesqrt |
---|
56 | // https://github.com/g-truc/glm/blob/269ae641283426f7f84116f2fe333472b9c914c9/glm/detail/func_exponential.inl |
---|
57 | /* return v * inversesqrt(dot(v, v)); */ |
---|
58 | return v / sqrt(dot(v, v)); |
---|
59 | } |
---|