raymathext.d 9.2 KB

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