raymathext.d 9.3 KB

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