source: libcfa/src/vec/vec3.hfa@ b84ab40

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 b84ab40 was ae09808, checked in by Dmitry Kobets <dkobets@…>, 6 years ago

Add vector division and multiplication to vec3 + tests

  • Property mode set to 100644
File size: 3.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 // 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 // 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) u, vec3(T) v) {
82 return [u.x * v.x, u.y * v.y, u.z * v.z];
83 }
84 vec3(T)& ?*=?(vec3(T)& v, T scalar) {
85 v = v * scalar;
86 return v;
87 }
88 vec3(T)& ?*=?(vec3(T)& u, vec3(T) v) {
89 u = u * v;
90 return u;
91 }
92 }
93
94 // Division
95 forall(| divide(T)) {
96 vec3(T) ?/?(vec3(T) v, T scalar) with (v) {
97 return [x / scalar, y / scalar, z / scalar];
98 }
99 vec3(T) ?/?(vec3(T) u, vec3(T) v) {
100 return [u.x / v.x, u.y / v.y, u.z / v.z];
101 }
102 vec3(T)& ?/=?(vec3(T)& v, T scalar) with (v) {
103 v = v / scalar;
104 return v;
105 }
106 vec3(T)& ?/=?(vec3(T)& u, vec3(T) v) {
107 u = u / v;
108 return u;
109 }
110 }
111
112 // Relational Operators
113 forall(| equality(T)) {
114 bool ?==?(vec3(T) u, vec3(T) v) with (u) {
115 return x == v.x && y == v.y && z == v.z;
116 }
117 bool ?!=?(vec3(T) u, vec3(T) v) {
118 return !(u == v);
119 }
120 }
121
122 // Geometric functions
123 forall(| add(T) | multiply(T))
124 T dot(vec3(T) u, vec3(T) v) {
125 return u.x * v.x + u.y * v.y + u.z * v.z;
126 }
127
128 forall(| subtract(T) | multiply(T))
129 vec3(T) cross(vec3(T) u, vec3(T) v) {
130 return (vec3(T)){ u.y * v.z - v.y * u.z,
131 u.z * v.x - v.z * u.x,
132 u.x * v.y - v.x * u.y };
133 }
134
135 } // static inline
136}
137
138forall(dtype ostype, otype T | writeable(T, ostype)) {
139 ostype & ?|?(ostype & os, vec3(T) v) with (v) {
140 return os | '<' | x | ',' | y | ',' | z | '>';
141 }
142 void ?|?(ostype & os, vec3(T) v ) with (v) {
143 (ostype &)(os | v); ends(os);
144 }
145}
146
Note: See TracBrowser for help on using the repository browser.