source: libcfa/src/vec/vec3.hfa@ 596fc4ad

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 596fc4ad was 1712f542, checked in by Dmitry Kobets <dkobets@…>, 6 years ago

Add extended ops for vec3 and tests

  • Property mode set to 100644
File size: 6.3 KB
Line 
1#pragma once
2
3#include <iostream.hfa>
4#include "vec.hfa"
5
6forall (otype T) {
7 struct vec3 {
8 T x, y, z;
9 };
10}
11
12
13forall (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 // -
45 forall(| subtract(T)) {
46 vec3(T) ?-?(vec3(T) u, vec3(T) v) { // TODO( can't make this const ref )
47 return [u.x - v.x, u.y - v.y, u.z - v.z];
48 }
49 vec3(T)& ?-=?(vec3(T)& u, vec3(T) v) {
50 u = u - v;
51 return u;
52 }
53 }
54
55 forall(| negate(T)) {
56 vec3(T) -?(vec3(T) v) with (v) {
57 return [-x, -y, -z];
58 }
59 }
60
61 forall(| { T --?(T&); }) {
62 vec3(T)& --?(vec3(T)& v) {
63 --v.x;
64 --v.y;
65 --v.z;
66 return v;
67 }
68 vec3(T) ?--(vec3(T)& v) {
69 vec3(T) copy = v;
70 --v;
71 return copy;
72 }
73 }
74
75 // +
76 forall(| add(T)) {
77 vec3(T) ?+?(vec3(T) u, vec3(T) v) { // TODO( can't make this const ref )
78 return [u.x + v.x, u.y + v.y, u.z + v.z];
79 }
80 vec3(T)& ?+=?(vec3(T)& u, vec3(T) v) {
81 u = u + v;
82 return u;
83 }
84 }
85
86
87 forall(| { T ++?(T&); }) {
88 vec3(T)& ++?(vec3(T)& v) {
89 ++v.x;
90 ++v.y;
91 ++v.z;
92 return v;
93 }
94 vec3(T) ?++(vec3(T)& v) {
95 vec3(T) copy = v;
96 ++v;
97 return copy;
98 }
99 }
100
101 // *
102 forall(| multiply(T)) {
103 vec3(T) ?*?(vec3(T) v, T scalar) with (v) { // TODO (can't make this const ref)
104 return [x * scalar, y * scalar, z * scalar];
105 }
106 vec3(T) ?*?(T scalar, vec3(T) v) { // TODO (can't make this const ref)
107 return v * scalar;
108 }
109 vec3(T) ?*?(vec3(T) u, vec3(T) v) {
110 return [u.x * v.x, u.y * v.y, u.z * v.z];
111 }
112 vec3(T)& ?*=?(vec3(T)& v, T scalar) {
113 v = v * scalar;
114 return v;
115 }
116 vec3(T)& ?*=?(vec3(T)& u, vec3(T) v) {
117 u = u * v;
118 return u;
119 }
120 }
121
122 // /
123 forall(| divide(T)) {
124 vec3(T) ?/?(vec3(T) v, T scalar) with (v) {
125 return [x / scalar, y / scalar, z / scalar];
126 }
127 vec3(T) ?/?(vec3(T) u, vec3(T) v) {
128 return [u.x / v.x, u.y / v.y, u.z / v.z];
129 }
130 vec3(T)& ?/=?(vec3(T)& v, T scalar) with (v) {
131 v = v / scalar;
132 return v;
133 }
134 vec3(T)& ?/=?(vec3(T)& u, vec3(T) v) {
135 u = u / v;
136 return u;
137 }
138 }
139
140 // %
141 forall(| { T ?%?(T,T); }) {
142 vec3(T) ?%?(vec3(T) v, T scalar) with (v) {
143 return [x % scalar, y % scalar, z % scalar];
144 }
145 vec3(T)& ?%=?(vec3(T)& u, T scalar) {
146 u = u % scalar;
147 return u;
148 }
149 vec3(T) ?%?(vec3(T) u, vec3(T) v) {
150 return [u.x % v.x, u.y % v.y, u.z % v.z];
151 }
152 vec3(T)& ?%=?(vec3(T)& u, vec3(T) v) {
153 u = u % v;
154 return u;
155 }
156 }
157
158 // &
159 forall(| { T ?&?(T,T); }) {
160 vec3(T) ?&?(vec3(T) v, T scalar) with (v) {
161 return [x & scalar, y & scalar, z & scalar];
162 }
163 vec3(T)& ?&=?(vec3(T)& u, T scalar) {
164 u = u & scalar;
165 return u;
166 }
167 vec3(T) ?&?(vec3(T) u, vec3(T) v) {
168 return [u.x & v.x, u.y & v.y, u.z & v.z];
169 }
170 vec3(T)& ?&=?(vec3(T)& u, vec3(T) v) {
171 u = u & v;
172 return u;
173 }
174 }
175
176 // |
177 forall(| { T ?|?(T,T); }) {
178 vec3(T) ?|?(vec3(T) v, T scalar) with (v) {
179 return [x | scalar, y | scalar, z | scalar];
180 }
181 vec3(T)& ?|=?(vec3(T)& u, T scalar) {
182 u = u | scalar;
183 return u;
184 }
185 vec3(T) ?|?(vec3(T) u, vec3(T) v) {
186 return [u.x | v.x, u.y | v.y, u.z | v.z];
187 }
188 vec3(T)& ?|=?(vec3(T)& u, vec3(T) v) {
189 u = u | v;
190 return u;
191 }
192 }
193
194 // ^
195 forall(| { T ?^?(T,T); }) {
196 vec3(T) ?^?(vec3(T) v, T scalar) with (v) {
197 return [x ^ scalar, y ^ scalar, z ^ scalar];
198 }
199 vec3(T)& ?^=?(vec3(T)& u, T scalar) {
200 u = u ^ scalar;
201 return u;
202 }
203 vec3(T) ?^?(vec3(T) u, vec3(T) v) {
204 return [u.x ^ v.x, u.y ^ v.y, u.z ^ v.z];
205 }
206 vec3(T)& ?^=?(vec3(T)& u, vec3(T) v) {
207 u = u ^ v;
208 return u;
209 }
210 }
211
212 // <<
213 forall(| { T ?<<?(T,T); }) {
214 vec3(T) ?<<?(vec3(T) v, T scalar) with (v) {
215 return [x << scalar, y << scalar, z << scalar];
216 }
217 vec3(T)& ?<<=?(vec3(T)& u, T scalar) {
218 u = u << scalar;
219 return u;
220 }
221 vec3(T) ?<<?(vec3(T) u, vec3(T) v) {
222 return [u.x << v.x, u.y << v.y, u.z << v.z];
223 }
224 vec3(T)& ?<<=?(vec3(T)& u, vec3(T) v) {
225 u = u << v;
226 return u;
227 }
228 }
229
230 // >>
231 forall(| { T ?>>?(T,T); }) {
232 vec3(T) ?>>?(vec3(T) v, T scalar) with (v) {
233 return [x >> scalar, y >> scalar, z >> scalar];
234 }
235 vec3(T)& ?>>=?(vec3(T)& u, T scalar) {
236 u = u >> scalar;
237 return u;
238 }
239 vec3(T) ?>>?(vec3(T) u, vec3(T) v) {
240 return [u.x >> v.x, u.y >> v.y, u.z >> v.z];
241 }
242 vec3(T)& ?>>=?(vec3(T)& u, vec3(T) v) {
243 u = u >> v;
244 return u;
245 }
246 }
247
248 // ~
249 forall(| { T ~?(T); })
250 vec3(T) ~?(vec3(T) v) with (v) {
251 return [~v.x, ~v.y, ~v.z];
252 }
253
254 // relational
255 forall(| equality(T)) {
256 bool ?==?(vec3(T) u, vec3(T) v) with (u) {
257 return x == v.x && y == v.y && z == v.z;
258 }
259 bool ?!=?(vec3(T) u, vec3(T) v) {
260 return !(u == v);
261 }
262 }
263
264 // Geometric functions
265 forall(| add(T) | multiply(T))
266 T dot(vec3(T) u, vec3(T) v) {
267 return u.x * v.x + u.y * v.y + u.z * v.z;
268 }
269
270 forall(| subtract(T) | multiply(T))
271 vec3(T) cross(vec3(T) u, vec3(T) v) {
272 return (vec3(T)){ u.y * v.z - v.y * u.z,
273 u.z * v.x - v.z * u.x,
274 u.x * v.y - v.x * u.y };
275 }
276
277 } // static inline
278}
279
280forall(dtype ostype, otype T | writeable(T, ostype)) {
281 ostype & ?|?(ostype & os, vec3(T) v) with (v) {
282 return os | '<' | x | ',' | y | ',' | z | '>';
283 }
284 void ?|?(ostype & os, vec3(T) v ) with (v) {
285 (ostype &)(os | v); ends(os);
286 }
287}
Note: See TracBrowser for help on using the repository browser.