matrix4x4.vala 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (c) 2012-2026 Daniele Bartolini et al.
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. namespace Crown
  6. {
  7. [Compact]
  8. public struct Matrix4x4
  9. {
  10. public Vector4 x;
  11. public Vector4 y;
  12. public Vector4 z;
  13. public Vector4 t;
  14. public Matrix4x4(Vector4 x, Vector4 y, Vector4 z, Vector4 t)
  15. {
  16. this.x = x;
  17. this.y = y;
  18. this.z = z;
  19. this.t = t;
  20. }
  21. public Matrix4x4.from_array(Gee.ArrayList<Value?> arr)
  22. {
  23. this.x.x = (double)arr[ 0];
  24. this.x.y = (double)arr[ 1];
  25. this.x.z = (double)arr[ 2];
  26. this.x.w = (double)arr[ 3];
  27. this.y.x = (double)arr[ 4];
  28. this.y.y = (double)arr[ 5];
  29. this.y.z = (double)arr[ 6];
  30. this.y.w = (double)arr[ 7];
  31. this.z.x = (double)arr[ 8];
  32. this.z.y = (double)arr[ 9];
  33. this.z.z = (double)arr[10];
  34. this.z.w = (double)arr[11];
  35. this.t.x = (double)arr[12];
  36. this.t.y = (double)arr[13];
  37. this.t.z = (double)arr[14];
  38. this.t.w = (double)arr[15];
  39. }
  40. public Vector3 scale()
  41. {
  42. double sx = this.x.to_vector3().length();
  43. double sy = this.y.to_vector3().length();
  44. double sz = this.z.to_vector3().length();
  45. return Vector3(sx, sy, sz);
  46. }
  47. public Quaternion rotation()
  48. {
  49. double lx = this.x.length();
  50. double ly = this.y.length();
  51. double lz = this.z.length();
  52. if (MathUtils.equal(lx, 0.0f) || MathUtils.equal(ly, 0.0f) || MathUtils.equal(lz, 0.0f))
  53. return QUATERNION_IDENTITY;
  54. Matrix4x4 rot = this;
  55. rot.x.normalize();
  56. rot.y.normalize();
  57. rot.z.normalize();
  58. Quaternion q = Quaternion.from_matrix(rot);
  59. q.normalize();
  60. return q;
  61. }
  62. }
  63. } /* namespace Crown */