raymathext.d 8.0 KB

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