Changeset 1dd929e for libcfa/src/vec/vec2.hfa
 Timestamp:
 Nov 29, 2019, 10:43:18 AM (3 years ago)
 Branches:
 armeh, enum, forallpointerdecay, jacob/cs343translation, master, newastuniqueexpr, pthreademulation, qualifiedEnum
 Children:
 0dedf756
 Parents:
 9ec35db
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

libcfa/src/vec/vec2.hfa
r9ec35db r1dd929e 3 3 #include <iostream.hfa> 4 4 5 trait vec2_t(otype T) { 5 trait zeroassn(otype T) { 6 T ?=?(T&, zero_t); 7 }; 8 trait fromint(otype T) { 6 9 void ?{}(T&, int); 10 }; 11 trait zero_assign(otype T) { 7 12 T ?=?(T&, zero_t); 13 }; 14 trait subtract(otype T) { 8 15 T ??(T, T); 9 16 T ?(T); 17 }; 18 trait add(otype T) { 10 19 T ?+?(T, T); 20 }; 21 trait multiply(otype T) { 11 22 T ?*?(T, T); 23 }; 24 trait divide(otype T) { 12 25 T ?/?(T, T); 26 }; 27 trait lessthan(otype T) { 28 int ?<?(T, T); 29 }; 30 trait equality(otype T) { 13 31 int ?==?(T, T); 14 int ?<?(T, T); 32 }; 33 trait sqrt(otype T) { 15 34 T sqrt(T); 16 35 }; … … 31 50 } 32 51 33 forall(otype T  vec2_t(T)) {52 forall(otype T) { 34 53 struct vec2 { 35 54 T x, y; … … 37 56 } 38 57 39 /* static inline { */ 40 forall(otype T  vec2_t(T)) { 58 forall(otype T) { 41 59 static inline { 42 60 … … 46 64 v.[x, y] = [x, y]; 47 65 } 66 67 forall( zero_assign(T)) 48 68 void ?{}(vec2(T)& vec, zero_t) with (vec) { 49 69 x = y = 0; 50 70 } 71 51 72 void ?{}(vec2(T)& vec, T val) with (vec) { 52 73 x = y = val; 53 74 } 75 54 76 void ?{}(vec2(T)& vec, vec2(T) other) with (vec) { 55 77 [x,y] = other.[x,y]; … … 60 82 [x,y] = other.[x,y]; 61 83 } 84 forall( zero_assign(T)) 62 85 void ?=?(vec2(T)& vec, zero_t) with (vec) { 63 86 x = y = 0; … … 67 90 68 91 // Subtraction 92 93 forall( subtract(T)) { 69 94 vec2(T) ??(vec2(T) u, vec2(T) v) { // TODO( can't make this const ref ) 70 95 return [u.x  v.x, u.y  v.y]; … … 77 102 return [x, y]; 78 103 } 104 } 79 105 80 106 // Addition 107 forall( add(T)) { 81 108 vec2(T) ?+?(vec2(T) u, vec2(T) v) { // TODO( can't make this const ref ) 82 109 return [u.x + v.x, u.y + v.y]; … … 86 113 return u; 87 114 } 115 } 88 116 89 117 // Scalar Multiplication 118 forall( multiply(T)) { 90 119 vec2(T) ?*?(vec2(T) v, T scalar) with (v) { // TODO (can't make this const ref) 91 120 return [x * scalar, y * scalar]; … … 98 127 return v; 99 128 } 100 129 } 101 130 102 131 // Scalar Division 132 forall( divide(T)) { 103 133 vec2(T) ?/?(vec2(T) v, T scalar) with (v) { 104 134 return [x / scalar, y / scalar]; … … 108 138 return v; 109 139 } 140 } 141 110 142 // Relational Operators 143 forall( equality(T)) { 111 144 bool ?==?(vec2(T) u, vec2(T) v) with (u) { 112 145 return x == v.x && y == v.y; … … 115 148 return !(u == v); 116 149 } 117 150 } 151 152 forall( add(T)  multiply(T)) 118 153 T dot(vec2(T) u, vec2(T) v) { 119 154 return u.x * v.x + u.y * v.y; 120 155 } 121 156 157 forall( sqrt(T)  add(T)  multiply(T)) 122 158 T length(vec2(T) v) { 123 159 return sqrt(dot(v, v)); 124 160 } 125 161 162 forall( add(T)  multiply(T)) 126 163 T length_squared(vec2(T) v) { 127 164 return dot(v, v); 128 165 } 129 166 167 forall( subtract(T)  sqrt(T)  add(T)  multiply(T)) 130 168 T distance(vec2(T) v1, vec2(T) v2) { 131 169 return length(v1  v2); 132 170 } 133 171 172 forall( sqrt(T)  divide(T)  add(T)  multiply(T)) 134 173 vec2(T) normalize(vec2(T) v) { 135 174 return v / sqrt(dot(v, v)); … … 137 176 138 177 // Project vector u onto vector v 178 forall( sqrt(T)  divide(T)  add(T)  multiply(T)) 139 179 vec2(T) project(vec2(T) u, vec2(T) v) { 140 180 vec2(T) v_norm = normalize(v); … … 143 183 144 184 // Reflect incident vector v with respect to surface with normal n 185 forall( sqrt(T)  divide(T)  add(T)  multiply(T)  subtract(T)  fromint(T)) 145 186 vec2(T) reflect(vec2(T) v, vec2(T) n) { 146 187 return v  (T){2} * project(v, n); … … 151 192 // entering material (i.e., from air to water, eta = 1/1.33) 152 193 // v and n must already be normalized 194 forall( sqrt(T)  add(T)  multiply(T)  subtract(T)  fromint(T)  lessthan(T)  zeroassn(T)) 153 195 vec2(T) refract(vec2(T) v, vec2(T) n, T eta) { 154 196 T dotValue = dot(n, v); … … 166 208 // i is the incident vector 167 209 // ng is the geometric normal of the surface 210 forall( add(T)  multiply(T)  lessthan(T)  fromint(T)  subtract(T)) 168 211 vec2(T) faceforward(vec2(T) n, vec2(T) i, vec2(T) ng) { 169 212 return dot(ng, i) < (T){0} ? n : n; 170 213 } 214 171 215 } 172 216 }
Note: See TracChangeset
for help on using the changeset viewer.