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