Changeset e1a0945
- Timestamp:
- Nov 11, 2019, 2:41:41 PM (5 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- af0bf71
- Parents:
- a39fd1d
- git-author:
- Dmitry Kobets <dkobets@…> (10/11/19 15:13:21)
- git-committer:
- Dmitry Kobets <dkobets@…> (11/11/19 14:41:41)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/vector.hfa
ra39fd1d re1a0945 2 2 #include <math.hfa> 3 3 #include <iostream.hfa> 4 5 //---------------------- Vector Types ----------------------6 // TODO: make generic, as per glm7 4 8 5 struct vec2 { … … 10 7 }; 11 8 9 // Constructors 10 12 11 void ?{}( vec2 & v, float x, float y) { 13 12 v.[x, y] = [x, y]; 14 13 } 14 void ?{}(vec2& vec, zero_t) with (vec) { 15 x = y = 0; 16 } 17 void ?{}(vec2& vec, float val) with (vec) { 18 x = y = val; 19 } 20 void ?{}(vec2& vec, vec2& other) with (vec) { 21 [x,y] = other.[x,y]; 22 } 15 23 24 // Assignment 25 void ?=?(vec2& vec, const vec2& other) with (vec) { 26 [x,y] = other.[x,y]; 27 } 28 29 // Primitive mathematical operations 30 31 // Subtraction 32 vec2 ?-?(const vec2& u, const vec2& v) { 33 return [u.x - v.x, u.y - v.y]; 34 } 35 vec2& ?-=?(vec2& u, const vec2& v) { 36 u = u - v; 37 return u; 38 } 39 vec2 -?(const vec2& v) with (v) { 40 return [-x, -y]; 41 } 42 43 // Addition 44 vec2 ?+?(const vec2& u, const vec2& v) { 45 return [u.x + v.x, u.y + v.y]; 46 } 47 vec2& ?+=?(vec2& u, const vec2& v) { 48 u = u + v; 49 return u; 50 } 51 52 // Scalar Multiplication 53 vec2 ?*?(const vec2& v, float scalar) with (v) { 54 return [x * scalar, y * scalar]; 55 } 56 vec2& ?*=?(vec2& v, float scalar) with (v) { 57 v = v * scalar; 58 return v; 59 } 60 vec2 ?*?(float scalar, const vec2& v) { 61 return v * scalar; 62 } 63 64 65 // Scalar Division 66 vec2 ?/?(const vec2& v, float scalar) with (v) { 67 return [x / scalar, y / scalar]; 68 } 69 vec2& ?/=?(vec2& v, float scalar) with (v) { 70 v = v / scalar; 71 return v; 72 } 73 74 // Relational Operators 75 bool ?==?(const vec2& u, const vec2& v) with (u) { 76 return x == v.x && y == v.y; 77 } 78 bool ?!=?(const vec2& u, const vec2& v) { 79 return !(u == v); 80 } 81 82 // Printing the vector (ostream) 16 83 forall( dtype ostype | ostream( ostype ) ) { 17 84 ostype & ?|?( ostype & os, const vec2& v) with (v) { … … 25 92 } 26 93 27 void ?{}(vec2& vec, zero_t) with (vec) {28 x = y = 0;29 }30 void ?{}(vec2& vec, vec2& other) with (vec) {31 [x,y] = other.[x,y];32 }33 34 vec2 ?-?(const vec2& u, const vec2& v) {35 return [u.x - v.x, u.y - v.y];36 }37 vec2 ?*?(const vec2& v, float scalar) with (v) {38 return [x * scalar, y * scalar];39 }40 vec2 ?*?(float scalar, const vec2& v) {41 return v * scalar;42 }43 vec2 ?/?(const vec2& v, float scalar) with (v) {44 return [x / scalar, y / scalar];45 }46 vec2 -?(const vec2& v) with (v) {47 return [-x, -y];48 }49 50 94 /* //---------------------- Geometric Functions ---------------------- */ 51 /* // These functions implement the Geometric Functions section of GLSL */95 /* // These functions implement the Geometric Functions section of GLSL for 2D vectors*/ 52 96 53 97 static inline float dot(const vec2& u, const vec2& v) { … … 59 103 } 60 104 61 // Returns the distance betwwen v1 and v2, i.e., length(p0 - p1).62 105 static inline float distance(const vec2& v1, const vec2& v2) { 63 106 return length(v1 - v2); … … 65 108 66 109 static inline vec2 normalize(const vec2& v) { 67 // TODO(dkobets) -- show them inversesqrt68 // https://github.com/g-truc/glm/blob/269ae641283426f7f84116f2fe333472b9c914c9/glm/detail/func_exponential.inl69 /* return v * inversesqrt(dot(v, v)); */70 110 return v / sqrt(dot(v, v)); 71 111 } 72 112 73 // project vector u onto vector v113 // Project vector u onto vector v 74 114 static inline vec2 project(const vec2& u, const vec2& v) { 75 115 vec2 v_norm = normalize(v); … … 77 117 } 78 118 79 /* returns the reflection direction : v - 2.0 * project(v, n) 80 * for incident vector v and surface normal n 81 */ 119 // Reflect incident vector v with respect to surface with normal n 82 120 static inline vec2 reflect(const vec2& v, const vec2& n) { 83 121 return v - 2 * project(v, n); 84 122 } 85 123 86 // incident vector v, surfacenormal n87 // eta =ratio of indices of refraction between starting material and124 // Refract incident vector v with respect to surface with normal n 125 // eta is the ratio of indices of refraction between starting material and 88 126 // entering material (i.e., from air to water, eta = 1/1.33) 89 127 static inline vec2 refract(const vec2& v, const vec2& n, float eta) { … … 96 134 } 97 135 98 // TODO: I don't quite understand the use-case for faceforward 136 // Used to render perturbed surfaces by ensuring that a perturbed normal 137 // is pointing in the same direction as the geometric normal of the 138 // surface. 139 // n is the perturbed vector that we want to align 140 // i is the incident vector 141 // ng is the geometric normal of the surface 142 static inline vec2 faceforward(const vec2& n, const vec2& i, float ng) { 143 return dot(n, i) < 0 ? ng : -ng; 144 }
Note: See TracChangeset
for help on using the changeset viewer.