matrix3.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh Bailey <[email protected]>
  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 3 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. dst->x.x = 1.0f - (yy + zz);
  33. dst->x.y = xy + wz;
  34. dst->x.z = xz - wy;
  35. dst->x.w = 0.0f;
  36. dst->y.x = xy - wz;
  37. dst->y.y = 1.0f - (xx + zz);
  38. dst->y.z = yz + wx;
  39. dst->y.w = 0.0f;
  40. dst->z.x = xz + wy;
  41. dst->z.y = yz - wx;
  42. dst->z.z = 1.0f - (xx + yy);
  43. dst->z.w = 0.0f;
  44. vec3_zero(&dst->t);
  45. }
  46. void matrix3_from_axisang(struct matrix3 *dst, const struct axisang *aa)
  47. {
  48. struct quat q;
  49. quat_from_axisang(&q, aa);
  50. matrix3_from_quat(dst, &q);
  51. }
  52. void matrix3_from_matrix4(struct matrix3 *dst, const struct matrix4 *m)
  53. {
  54. dst->x.m = m->x.m;
  55. dst->y.m = m->y.m;
  56. dst->z.m = m->z.m;
  57. dst->t.m = m->t.m;
  58. dst->x.w = 0.0f;
  59. dst->y.w = 0.0f;
  60. dst->z.w = 0.0f;
  61. dst->t.w = 0.0f;
  62. }
  63. void matrix3_mul(struct matrix3 *dst, const struct matrix3 *m1,
  64. const struct matrix3 *m2)
  65. {
  66. vec3_rotate(&dst->x, &m1->x, m2);
  67. vec3_rotate(&dst->y, &m1->y, m2);
  68. vec3_rotate(&dst->z, &m1->z, m2);
  69. vec3_transform(&dst->t, &m1->t, m2);
  70. }
  71. void matrix3_rotate(struct matrix3 *dst, const struct matrix3 *m,
  72. const struct quat *q)
  73. {
  74. struct matrix3 temp;
  75. matrix3_from_quat(&temp, q);
  76. matrix3_mul(dst, m, &temp);
  77. }
  78. void matrix3_rotate_aa(struct matrix3 *dst, const struct matrix3 *m,
  79. const struct axisang *aa)
  80. {
  81. struct matrix3 temp;
  82. matrix3_from_axisang(&temp, aa);
  83. matrix3_mul(dst, m, &temp);
  84. }
  85. void matrix3_scale(struct matrix3 *dst, const struct matrix3 *m,
  86. const struct vec3 *v)
  87. {
  88. vec3_mul(&dst->x, &m->x, v);
  89. vec3_mul(&dst->y, &m->y, v);
  90. vec3_mul(&dst->z, &m->z, v);
  91. vec3_mul(&dst->t, &m->t, v);
  92. }
  93. void matrix3_transpose(struct matrix3 *dst, const struct matrix3 *m)
  94. {
  95. __m128 tmp1, tmp2;
  96. vec3_transform(&dst->t, &m->t, m);
  97. vec3_neg(&dst->t, &dst->t);
  98. tmp1 = _mm_movelh_ps(m->x.m, m->y.m);
  99. tmp2 = _mm_movehl_ps(m->y.m, m->x.m);
  100. dst->x.m = _mm_shuffle_ps(tmp1, m->z.m, _MM_SHUFFLE(3, 0, 2, 0));
  101. dst->y.m = _mm_shuffle_ps(tmp1, m->z.m, _MM_SHUFFLE(3, 1, 3, 1));
  102. dst->z.m = _mm_shuffle_ps(tmp2, m->z.m, _MM_SHUFFLE(3, 2, 2, 0));
  103. }
  104. void matrix3_inv(struct matrix3 *dst, const struct matrix3 *m)
  105. {
  106. matrix4_inv((struct matrix4*)dst, (struct matrix4*)m);
  107. }
  108. void matrix3_mirror(struct matrix3 *dst, const struct matrix3 *m,
  109. const struct plane *p)
  110. {
  111. vec3_mirrorv(&dst->x, &m->x, &p->dir);
  112. vec3_mirrorv(&dst->y, &m->y, &p->dir);
  113. vec3_mirrorv(&dst->z, &m->z, &p->dir);
  114. vec3_mirror(&dst->t, &m->t, p);
  115. }
  116. void matrix3_mirrorv(struct matrix3 *dst, const struct matrix3 *m,
  117. const struct vec3 *v)
  118. {
  119. vec3_mirrorv(&dst->x, &m->x, v);
  120. vec3_mirrorv(&dst->y, &m->y, v);
  121. vec3_mirrorv(&dst->z, &m->z, v);
  122. vec3_mirrorv(&dst->t, &m->t, v);
  123. }