Changeset 3376ec9 for libcfa/src/vec
 Timestamp:
 Dec 11, 2019, 4:13:29 PM (3 years ago)
 Branches:
 ADT, armeh, enum, forallpointerdecay, jacob/cs343translation, master, newastuniqueexpr, pthreademulation, qualifiedEnum
 Children:
 aca6a54c
 Parents:
 2444324
 Location:
 libcfa/src/vec
 Files:

 1 added
 2 deleted
 1 edited
Legend:
 Unmodified
 Added
 Removed

libcfa/src/vec/vec2.hfa
r2444324 r3376ec9 1 1 #pragma once 2 #include <math.hfa>3 #include <iostream.hfa>4 2 5 trait zeroassn(otype T) { 6 T ?=?(T&, zero_t); 7 }; 8 trait fromint(otype T) { 9 void ?{}(T&, int); 10 }; 11 trait zero_assign(otype T) { 12 T ?=?(T&, zero_t); 13 }; 14 trait subtract(otype T) { 15 T ??(T, T); 16 T ?(T); 17 }; 18 trait add(otype T) { 19 T ?+?(T, T); 20 }; 21 trait multiply(otype T) { 22 T ?*?(T, T); 23 }; 24 trait divide(otype T) { 25 T ?/?(T, T); 26 }; 27 trait lessthan(otype T) { 28 int ?<?(T, T); 29 }; 30 trait equality(otype T) { 31 int ?==?(T, T); 32 }; 33 trait sqrt(otype T) { 34 T sqrt(T); 35 }; 3 #include "vec.hfa" 36 4 37 static inline { 38 // int 39 int ?=?(int& n, zero_t) { return n = 0.f; } 40 /* float */ 41 void ?{}(float& a, int b) { a = b; } 42 float ?=?(float& n, zero_t) { return n = 0.f; } 43 /* double */ 44 void ?{}(double& a, int b) { a = b; } 45 double ?=?(double& n, zero_t) { return n = 0L; } 46 // long double 47 void ?{}(long double& a, int b) { a = b; } 48 long double ?=?(long double& n, zero_t) { return n = 0L; } 49 } 50 51 forall(otype T) { 5 forall (otype T) { 52 6 struct vec2 { 53 7 T x, y; … … 55 9 } 56 10 57 forall(otype T) { 11 12 forall (otype T) { 58 13 static inline { 59 60 // Constructors61 14 62 15 void ?{}(vec2(T)& v, T x, T y) { … … 98 51 return u; 99 52 } 53 } 54 55 forall( negate(T)) { 100 56 vec2(T) ?(vec2(T)& v) with (v) { 101 57 return [x, y]; … … 149 105 } 150 106 107 // Geometric functions 151 108 forall( add(T)  multiply(T)) 152 109 T dot(vec2(T) u, vec2(T) v) { … … 154 111 } 155 112 156 forall( sqrt(T)  add(T)  multiply(T)) 157 T length(vec2(T) v) { 158 return sqrt(dot(v, v)); 159 } 160 161 forall( add(T)  multiply(T)) 162 T length_squared(vec2(T) v) { 163 return dot(v, v); 164 } 165 166 forall( subtract(T)  sqrt(T)  add(T)  multiply(T)) 167 T distance(vec2(T) v1, vec2(T) v2) { 168 return length(v1  v2); 169 } 170 171 forall( sqrt(T)  divide(T)  add(T)  multiply(T)) 172 vec2(T) normalize(vec2(T) v) { 173 return v / sqrt(dot(v, v)); 174 } 175 176 // Project vector u onto vector v 177 forall( sqrt(T)  divide(T)  add(T)  multiply(T)) 178 vec2(T) project(vec2(T) u, vec2(T) v) { 179 vec2(T) v_norm = normalize(v); 180 return v_norm * dot(u, v_norm); 181 } 182 183 // Reflect incident vector v with respect to surface with normal n 184 forall( sqrt(T)  divide(T)  add(T)  multiply(T)  subtract(T)  fromint(T)) 185 vec2(T) reflect(vec2(T) v, vec2(T) n) { 186 return v  (T){2} * project(v, n); 187 } 188 189 // Refract incident vector v with respect to surface with normal n 190 // eta is the ratio of indices of refraction between starting material and 191 // entering material (i.e., from air to water, eta = 1/1.33) 192 // v and n must already be normalized 193 forall( sqrt(T)  add(T)  multiply(T)  subtract(T)  fromint(T)  lessthan(T)  zeroassn(T)) 194 vec2(T) refract(vec2(T) v, vec2(T) n, T eta) { 195 T dotValue = dot(n, v); 196 T k = (T){1}  eta * eta * ((T){1}  dotValue * dotValue); 197 if (k < (T){0}) { 198 return 0; 199 } 200 return eta * v  (eta * dotValue + sqrt(k)) * n; 201 } 202 203 // Given a perturbed normal and a geometric normal, 204 // flip the perturbed normal if the geometric normal is pointing away 205 // from the observer. 206 // n is the perturbed vector that we want to align 207 // i is the incident vector 208 // ng is the geometric normal of the surface 209 forall( add(T)  multiply(T)  lessthan(T)  fromint(T)  subtract(T)) 210 vec2(T) faceforward(vec2(T) n, vec2(T) i, vec2(T) ng) { 211 return dot(ng, i) < (T){0} ? n : n; 212 } 213 214 } 113 } // static inline 215 114 } 216 217 forall(dtype ostype, otype T  writeable(T, ostype)) {218 ostype & ??( ostype & os, vec2(T) v) with (v) {219 return os  '<'  x  ','  y  '>';220 }221 void ??( ostype & os, vec2(T) v ) with (v) {222 (ostype &)(os  v); ends(os);223 }224 }
Note: See TracChangeset
for help on using the changeset viewer.