1 module raymathext; 2 3 import raylib; 4 import std.math; 5 6 pragma(inline, true): 7 8 // Bivector2 type 9 struct Bivector2 10 { 11 float xy = 0.0f; 12 alias xy this; 13 mixin Linear; 14 } 15 16 // Bivector3 type 17 /// Beware of the field order 18 /// xy is the first field 19 struct Bivector3 20 { 21 float xy = 0.0f; 22 float yz = 0.0f; 23 float zx = 0.0f; 24 mixin Linear; 25 } 26 27 // Rotor type 28 struct Rotor3 29 { 30 float a = 1.0f; 31 float xy = 0.0f; 32 float yz = 0.0f; 33 float zx = 0.0f; 34 mixin Linear; 35 36 alias i = yz; 37 alias j = zx; 38 alias k = xy; 39 40 @property Bivector3 b() 41 { 42 return Bivector3(xy, yz, zx); 43 } 44 45 @property Bivector3 b(Bivector3 _b) 46 { 47 xy = _b.xy; 48 yz = _b.yz; 49 zx = _b.zx; 50 return _b; 51 } 52 53 this(float _a, Bivector3 _b) 54 { 55 a = _a; 56 b = _b; 57 } 58 59 this(float _a, float _xy, float _yz, float _zx) 60 { 61 a = _a; 62 xy = _xy; 63 yz = _yz; 64 zx = _zx; 65 } 66 } 67 68 alias Matrix4 = Matrix; 69 70 version (unittest) 71 { 72 import fluent.asserts; 73 } 74 75 mixin template Linear() 76 { 77 import std.algorithm : canFind, map; 78 import std.range : join; 79 import std.traits : FieldNameTuple; 80 81 private static alias T = typeof(this); 82 83 static T zero() 84 { 85 enum fragment = [FieldNameTuple!T].map!(field => "0.").join(","); 86 return mixin("T(" ~ fragment ~ ")"); 87 } 88 89 static T one() 90 { 91 enum fragment = [FieldNameTuple!T].map!(field => "1.").join(","); 92 return mixin("T(" ~ fragment ~ ")"); 93 } 94 95 inout T opUnary(string op)() if (["+", "-"].canFind(op)) 96 { 97 enum fragment = [FieldNameTuple!T].map!(field => op ~ field).join(","); 98 return mixin("T(" ~ fragment ~ ")"); 99 } 100 101 static if (is(T == Rotor3)) 102 { 103 /// Returns a rotor equivalent to first apply p, then apply q 104 inout Rotor3 opBinary(string op)(inout Rotor3 q) if (op == "*") 105 { 106 alias p = this; 107 Rotor3 r; 108 r.a = p.a * q.a - p.i * q.i - p.j * q.j - p.k * q.k; 109 r.i = p.i * q.a + p.a * q.i + p.j * q.k - p.k * q.j; 110 r.j = p.j * q.a + p.a * q.j + p.k * q.i - p.i * q.k; 111 r.k = p.k * q.a + p.a * q.k + p.i * q.j - p.j * q.i; 112 return r; 113 } 114 115 inout Vector3 opBinary(string op)(inout Vector3 v) if (op == "*") 116 { 117 Vector3 rv; 118 rv.x = a * v.x + xy * v.y - zx * v.z; 119 rv.y = a * v.y + yz * v.z - xy * v.x; 120 rv.z = a * v.z + zx * v.x - yz * v.y; 121 return rv; 122 } 123 124 inout Vector3 opBinaryRight(string op)(inout Vector3 v) if (op == "*") 125 { 126 Vector3 vr; 127 vr.x = v.x * a - v.y * xy + v.z * zx; 128 vr.y = v.y * a - v.z * yz + v.x * xy; 129 vr.z = v.z * a - v.x * zx + v.y * yz; 130 return vr; 131 } 132 } 133 else 134 { 135 inout T opBinary(string op)(inout T rhs) if (["+", "-"].canFind(op)) 136 { 137 enum fragment = [FieldNameTuple!T].map!(field => field ~ op ~ "rhs." ~ field).join(","); 138 return mixin("T(" ~ fragment ~ ")"); 139 } 140 } 141 142 inout T opBinary(string op)(inout float rhs) if (["+", "-", "*", "/"].canFind(op)) 143 { 144 enum fragment = [FieldNameTuple!T].map!(field => field ~ op ~ "rhs").join(","); 145 return mixin("T(" ~ fragment ~ ")"); 146 } 147 148 inout T opBinaryRight(string op)(inout float lhs) if (["+", "-", "*", "/"].canFind(op)) 149 { 150 enum fragment = [FieldNameTuple!T].map!(field => "lhs" ~ op ~ field).join(","); 151 return mixin("T(" ~ fragment ~ ")"); 152 } 153 } 154 155 unittest 156 { 157 Assert.equal(Vector2.init, Vector2.zero); 158 Assert.equal(Vector2(), Vector2.zero); 159 Assert.equal(-Vector2(1, 2), Vector2(-1, -2)); 160 auto a = Vector3(1, 2, 9); 161 immutable b = Vector3(3, 4, 9); 162 Vector3 c = a + b; 163 Assert.equal(c, Vector3(4, 6, 18)); 164 Assert.equal(4.0f - Vector2.zero, Vector2(4, 4)); 165 Assert.equal(Vector2.one - 3.0f, Vector2(-2, -2)); 166 } 167 168 import std.traits : FieldNameTuple; 169 import std.algorithm : map; 170 import std.range : join; 171 172 float length(T)(T v) 173 { 174 enum fragment = [FieldNameTuple!T].map!(field => "v." ~ field ~ "*" ~ "v." ~ field).join("+"); 175 return mixin("sqrt(" ~ fragment ~ ")"); 176 } 177 178 T normal(T)(T v) 179 { 180 return v / v.length; 181 } 182 183 float distance(T)(T lhs, T rhs) 184 { 185 return (lhs - rhs).length; 186 } 187 188 float dot(T)(T lhs, T rhs) 189 { 190 enum fragment = [FieldNameTuple!T].map!(field => "lhs." ~ field ~ "*" ~ "rhs." ~ field).join( 191 "+"); 192 return mixin(fragment); 193 } 194 195 unittest 196 { 197 Assert.equal(Vector2(3, 4).length, 5); 198 const a = Vector2(-3, 4); 199 Assert.equal(a.normal, Vector2(-3. / 5., 4. / 5.)); 200 immutable b = Vector2(9, 8); 201 Assert.equal(b.distance(Vector2(-3, 3)), 13); 202 Assert.equal(Vector3(2, 3, 4).dot(Vector3(4, 5, 6)), 47); 203 Assert.equal(Vector2.one.length, sqrt(2.0f)); 204 } 205 206 unittest 207 { 208 Assert.equal(Rotor3(1, 2, 3, 4), Rotor3(1, Bivector3(2, 3, 4))); 209 } 210 211 /// Mix `amount` of `lhs` with `1-amount` of `rhs` 212 /// `amount` should be between 0 and 1, but can be anything 213 /// lerp(lhs, rhs, 0) == lhs 214 /// lerp(lhs, rhs, 1) == rhs 215 T lerp(T)(T lhs, T rhs, float amount) 216 { 217 return lhs + amount * (rhs - lhs); 218 } 219 220 /// angle betwenn vector and x-axis (+y +x -> positive) 221 float angle(Vector2 v) 222 { 223 return atan2(v.y, v.x); 224 } 225 226 Vector2 rotate(Vector2 v, float angle) 227 { 228 return Vector2(v.x * cos(angle) - v.y * sin(angle), v.x * sin(angle) + v.y * cos(angle)); 229 } 230 231 Vector2 slide(Vector2 v, Vector2 along) 232 { 233 return along.normal * dot(v, along); 234 } 235 236 Bivector2 wedge(Vector2 lhs, Vector2 rhs) 237 { 238 Bivector2 result = {xy: lhs.x * rhs.y - lhs.y * rhs.x}; 239 return result; 240 } 241 242 // dfmt off 243 Bivector3 wedge(Vector3 lhs, Vector3 rhs) 244 { 245 Bivector3 result = { 246 xy: lhs.x * rhs.y - lhs.y * rhs.x, 247 yz: lhs.y * rhs.z - lhs.z * rhs.y, 248 zx: lhs.z * rhs.x - lhs.x * rhs.z, 249 }; 250 return result; 251 } 252 253 Vector3 transform(Vector3 v, Matrix4 mat) 254 { 255 with (v) with (mat) 256 return Vector3( 257 m0 * x + m4 * y + m8 * z + m12, 258 m1 * x + m5 * y + m9 * z + m13, 259 m2 * x + m6 * y + m10 * z + m14 260 ); 261 } 262 // dfmt on 263 264 Vector3 cross(Vector3 lhs, Vector3 rhs) 265 { 266 auto v = wedge(lhs, rhs); 267 return Vector3(v.yz, v.zx, v.xy); 268 } 269 270 unittest { 271 // TODO 272 } 273 274 /// Returns a unit rotor that rotates `from` to `to` 275 Rotor3 rotation(Vector3 from, Vector3 to) 276 { 277 return Rotor3(1 + dot(to, from), wedge(to, from)).normal; 278 } 279 280 Rotor3 rotation(float angle, Bivector3 plane) 281 { 282 return Rotor3(cos(angle / 2.0f), -sin(angle / 2.0f) * plane); 283 } 284 285 /// Rotate q by p 286 Rotor3 rotate(Rotor3 p, Rotor3 q) 287 { 288 return p * q * p.reverse; 289 } 290 291 /// Rotate v by r 292 Vector3 rotate(Rotor3 r, Vector3 v) 293 { 294 return r * v * r.reverse; 295 } 296 297 Rotor3 reverse(Rotor3 r) 298 { 299 return Rotor3(r.a, -r.b); 300 } 301 302 unittest 303 { 304 // TODO 305 }