1 | #pragma once
|
---|
2 |
|
---|
3 | #include <iostream.hfa>
|
---|
4 | #include "vec.hfa"
|
---|
5 |
|
---|
6 | forall (otype T) {
|
---|
7 | struct vec4 {
|
---|
8 | T x, y, z, w;
|
---|
9 | };
|
---|
10 | }
|
---|
11 |
|
---|
12 |
|
---|
13 | forall (otype T) {
|
---|
14 | static inline {
|
---|
15 |
|
---|
16 | void ?{}(vec4(T)& v, T x, T y, T z, T w) {
|
---|
17 | v.[x, y, z, w] = [x, y, z, w];
|
---|
18 | }
|
---|
19 |
|
---|
20 | forall(| zero_assign(T))
|
---|
21 | void ?{}(vec4(T)& vec, zero_t) with (vec) {
|
---|
22 | x = y = z = w = 0;
|
---|
23 | }
|
---|
24 |
|
---|
25 | void ?{}(vec4(T)& vec, T val) with (vec) {
|
---|
26 | x = y = z = w = val;
|
---|
27 | }
|
---|
28 |
|
---|
29 | void ?{}(vec4(T)& vec, vec4(T) other) with (vec) {
|
---|
30 | [x,y,z,w] = other.[x,y,z,w];
|
---|
31 | }
|
---|
32 |
|
---|
33 | // Assignment
|
---|
34 | void ?=?(vec4(T)& vec, vec4(T) other) with (vec) {
|
---|
35 | [x,y,z,w] = other.[x,y,z,w];
|
---|
36 | }
|
---|
37 | forall(| zero_assign(T))
|
---|
38 | void ?=?(vec4(T)& vec, zero_t) with (vec) {
|
---|
39 | x = y = z = w = 0;
|
---|
40 | }
|
---|
41 |
|
---|
42 | // Primitive mathematical operations
|
---|
43 |
|
---|
44 | // Subtraction
|
---|
45 |
|
---|
46 | forall(| subtract(T)) {
|
---|
47 | vec4(T) ?-?(vec4(T) u, vec4(T) v) { // TODO( can't make this const ref )
|
---|
48 | return [u.x - v.x, u.y - v.y, u.z - v.z, u.w - v.w];
|
---|
49 | }
|
---|
50 | vec4(T)& ?-=?(vec4(T)& u, vec4(T) v) {
|
---|
51 | u = u - v;
|
---|
52 | return u;
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | forall(| negate(T)) {
|
---|
57 | vec4(T) -?(vec4(T) v) with (v) {
|
---|
58 | return [-x, -y, -z, -w];
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | // Addition
|
---|
63 | forall(| add(T)) {
|
---|
64 | vec4(T) ?+?(vec4(T) u, vec4(T) v) { // TODO( can't make this const ref )
|
---|
65 | return [u.x + v.x, u.y + v.y, u.z + v.z, u.w + v.w];
|
---|
66 | }
|
---|
67 | vec4(T)& ?+=?(vec4(T)& u, vec4(T) v) {
|
---|
68 | u = u + v;
|
---|
69 | return u;
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | // Scalar Multiplication
|
---|
74 | forall(| multiply(T)) {
|
---|
75 | vec4(T) ?*?(vec4(T) v, T scalar) with (v) { // TODO (can't make this const ref)
|
---|
76 | return [x * scalar, y * scalar, z * scalar, w * scalar];
|
---|
77 | }
|
---|
78 | vec4(T) ?*?(T scalar, vec4(T) v) { // TODO (can't make this const ref)
|
---|
79 | return v * scalar;
|
---|
80 | }
|
---|
81 | vec4(T)& ?*=?(vec4(T)& v, T scalar) {
|
---|
82 | v = v * scalar;
|
---|
83 | return v;
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | // Scalar Division
|
---|
88 | forall(| divide(T)) {
|
---|
89 | vec4(T) ?/?(vec4(T) v, T scalar) with (v) {
|
---|
90 | return [x / scalar, y / scalar, z / scalar, w / scalar];
|
---|
91 | }
|
---|
92 | vec4(T)& ?/=?(vec4(T)& v, T scalar) with (v) {
|
---|
93 | v = v / scalar;
|
---|
94 | return v;
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | // Relational Operators
|
---|
99 | forall(| equality(T)) {
|
---|
100 | bool ?==?(vec4(T) u, vec4(T) v) with (u) {
|
---|
101 | return x == v.x && y == v.y && z == v.z && w == v.w;
|
---|
102 | }
|
---|
103 | bool ?!=?(vec4(T) u, vec4(T) v) {
|
---|
104 | return !(u == v);
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | // Geometric functions
|
---|
109 | forall(| add(T) | multiply(T))
|
---|
110 | T dot(vec4(T) u, vec4(T) v) {
|
---|
111 | return u.x * v.x + u.y * v.y + u.z * v.z + u.w * v.w;
|
---|
112 | }
|
---|
113 |
|
---|
114 | } // static inline
|
---|
115 | }
|
---|
116 |
|
---|
117 | forall(dtype ostype, otype T | writeable(T, ostype)) {
|
---|
118 | ostype & ?|?(ostype & os, vec4(T) v) with (v) {
|
---|
119 | return os | '<' | x | ',' | y | ',' | z | ',' | w | '>';
|
---|
120 | }
|
---|
121 | void ?|?(ostype & os, vec4(T) v ) with (v) {
|
---|
122 | (ostype &)(os | v); ends(os);
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|