matrix3.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <lain@obsproject.com>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include <string.h>
  15. #include "matrix3.h"
  16. #include "matrix4.h"
  17. #include "plane.h"
  18. #include "quat.h"
  19. void matrix3_from_quat(struct matrix3 *dst, const struct quat *q)
  20. {
  21. float norm = quat_dot(q, q);
  22. float s = (norm > 0.0f) ? (2.0f / norm) : 0.0f;
  23. float xx = q->x * q->x * s;
  24. float yy = q->y * q->y * s;
  25. float zz = q->z * q->z * s;
  26. float xy = q->x * q->y * s;
  27. float xz = q->x * q->z * s;
  28. float yz = q->y * q->z * s;
  29. float wx = q->w * q->x * s;
  30. float wy = q->w * q->y * s;
  31. float wz = q->w * q->z * s;
  32. vec3_set(&dst->x, 1.0f - (yy + zz), xy + wz, xz - wy);
  33. vec3_set(&dst->y, xy - wz, 1.0f - (xx + zz), yz + wx);
  34. vec3_set(&dst->z, xz + wy, yz - wx, 1.0f - (xx + yy));
  35. vec3_zero(&dst->t);
  36. }
  37. void matrix3_from_axisang(struct matrix3 *dst, const struct axisang *aa)
  38. {
  39. struct quat q;
  40. quat_from_axisang(&q, aa);
  41. matrix3_from_quat(dst, &q);
  42. }
  43. void matrix3_from_matrix4(struct matrix3 *dst, const struct matrix4 *m)
  44. {
  45. dst->x.m = m->x.m;
  46. dst->y.m = m->y.m;
  47. dst->z.m = m->z.m;
  48. dst->t.m = m->t.m;
  49. dst->x.w = 0.0f;
  50. dst->y.w = 0.0f;
  51. dst->z.w = 0.0f;
  52. dst->t.w = 0.0f;
  53. }
  54. void matrix3_mul(struct matrix3 *dst, const struct matrix3 *m1, const struct matrix3 *m2)
  55. {
  56. if (dst == m2) {
  57. struct matrix3 temp;
  58. vec3_rotate(&temp.x, &m1->x, m2);
  59. vec3_rotate(&temp.y, &m1->y, m2);
  60. vec3_rotate(&temp.z, &m1->z, m2);
  61. vec3_transform3x4(&temp.t, &m1->t, m2);
  62. matrix3_copy(dst, &temp);
  63. } else {
  64. vec3_rotate(&dst->x, &m1->x, m2);
  65. vec3_rotate(&dst->y, &m1->y, m2);
  66. vec3_rotate(&dst->z, &m1->z, m2);
  67. vec3_transform3x4(&dst->t, &m1->t, m2);
  68. }
  69. }
  70. void matrix3_rotate(struct matrix3 *dst, const struct matrix3 *m, const struct quat *q)
  71. {
  72. struct matrix3 temp;
  73. matrix3_from_quat(&temp, q);
  74. matrix3_mul(dst, m, &temp);
  75. }
  76. void matrix3_rotate_aa(struct matrix3 *dst, const struct matrix3 *m, const struct axisang *aa)
  77. {
  78. struct matrix3 temp;
  79. matrix3_from_axisang(&temp, aa);
  80. matrix3_mul(dst, m, &temp);
  81. }
  82. void matrix3_scale(struct matrix3 *dst, const struct matrix3 *m, const struct vec3 *v)
  83. {
  84. vec3_mul(&dst->x, &m->x, v);
  85. vec3_mul(&dst->y, &m->y, v);
  86. vec3_mul(&dst->z, &m->z, v);
  87. vec3_mul(&dst->t, &m->t, v);
  88. }
  89. void matrix3_transpose(struct matrix3 *dst, const struct matrix3 *m)
  90. {
  91. __m128 tmp1, tmp2;
  92. vec3_rotate(&dst->t, &m->t, m);
  93. vec3_neg(&dst->t, &dst->t);
  94. tmp1 = _mm_movelh_ps(m->x.m, m->y.m);
  95. tmp2 = _mm_movehl_ps(m->y.m, m->x.m);
  96. dst->x.m = _mm_shuffle_ps(tmp1, m->z.m, _MM_SHUFFLE(3, 0, 2, 0));
  97. dst->y.m = _mm_shuffle_ps(tmp1, m->z.m, _MM_SHUFFLE(3, 1, 3, 1));
  98. dst->z.m = _mm_shuffle_ps(tmp2, m->z.m, _MM_SHUFFLE(3, 2, 2, 0));
  99. }
  100. void matrix3_inv(struct matrix3 *dst, const struct matrix3 *m)
  101. {
  102. struct matrix4 m4;
  103. matrix4_from_matrix3(&m4, m);
  104. matrix4_inv((struct matrix4 *)dst, &m4);
  105. dst->t.w = 0.0f;
  106. }
  107. void matrix3_mirror(struct matrix3 *dst, const struct matrix3 *m, const struct plane *p)
  108. {
  109. vec3_mirrorv(&dst->x, &m->x, &p->dir);
  110. vec3_mirrorv(&dst->y, &m->y, &p->dir);
  111. vec3_mirrorv(&dst->z, &m->z, &p->dir);
  112. vec3_mirror(&dst->t, &m->t, p);
  113. }
  114. void matrix3_mirrorv(struct matrix3 *dst, const struct matrix3 *m, const struct vec3 *v)
  115. {
  116. vec3_mirrorv(&dst->x, &m->x, v);
  117. vec3_mirrorv(&dst->y, &m->y, v);
  118. vec3_mirrorv(&dst->z, &m->z, v);
  119. vec3_mirrorv(&dst->t, &m->t, v);
  120. }