matrix3.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain 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 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. #pragma once
  15. #include "vec3.h"
  16. #include "axisang.h"
  17. /* 3x4 Matrix */
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. struct matrix4;
  22. struct matrix3 {
  23. struct vec3 x;
  24. struct vec3 y;
  25. struct vec3 z;
  26. struct vec3 t;
  27. };
  28. static inline void matrix3_copy(struct matrix3 *dst, const struct matrix3 *m)
  29. {
  30. vec3_copy(&dst->x, &m->x);
  31. vec3_copy(&dst->y, &m->y);
  32. vec3_copy(&dst->z, &m->z);
  33. vec3_copy(&dst->t, &m->t);
  34. }
  35. static inline void matrix3_identity(struct matrix3 *dst)
  36. {
  37. vec3_zero(&dst->x);
  38. vec3_zero(&dst->y);
  39. vec3_zero(&dst->z);
  40. vec3_zero(&dst->t);
  41. dst->x.x = dst->y.y = dst->z.z = 1.0f;
  42. }
  43. EXPORT void matrix3_from_quat(struct matrix3 *dst, const struct quat *q);
  44. EXPORT void matrix3_from_axisang(struct matrix3 *dst, const struct axisang *aa);
  45. EXPORT void matrix3_from_matrix4(struct matrix3 *dst, const struct matrix4 *m);
  46. EXPORT void matrix3_mul(struct matrix3 *dst, const struct matrix3 *m1, const struct matrix3 *m2);
  47. static inline void matrix3_translate(struct matrix3 *dst, const struct matrix3 *m, const struct vec3 *v)
  48. {
  49. vec3_sub(&dst->t, &m->t, v);
  50. }
  51. EXPORT void matrix3_rotate(struct matrix3 *dst, const struct matrix3 *m, const struct quat *q);
  52. EXPORT void matrix3_rotate_aa(struct matrix3 *dst, const struct matrix3 *m, const struct axisang *aa);
  53. EXPORT void matrix3_scale(struct matrix3 *dst, const struct matrix3 *m, const struct vec3 *v);
  54. EXPORT void matrix3_transpose(struct matrix3 *dst, const struct matrix3 *m);
  55. EXPORT void matrix3_inv(struct matrix3 *dst, const struct matrix3 *m);
  56. EXPORT void matrix3_mirror(struct matrix3 *dst, const struct matrix3 *m, const struct plane *p);
  57. EXPORT void matrix3_mirrorv(struct matrix3 *dst, const struct matrix3 *m, const struct vec3 *v);
  58. static inline void matrix3_translate3f(struct matrix3 *dst, const struct matrix3 *m, float x, float y, float z)
  59. {
  60. struct vec3 v;
  61. vec3_set(&v, x, y, z);
  62. matrix3_translate(dst, m, &v);
  63. }
  64. static inline void matrix3_rotate_aa4f(struct matrix3 *dst, const struct matrix3 *m, float x, float y, float z,
  65. float rot)
  66. {
  67. struct axisang aa;
  68. axisang_set(&aa, x, y, z, rot);
  69. matrix3_rotate_aa(dst, m, &aa);
  70. }
  71. static inline void matrix3_scale3f(struct matrix3 *dst, const struct matrix3 *m, float x, float y, float z)
  72. {
  73. struct vec3 v;
  74. vec3_set(&v, x, y, z);
  75. matrix3_scale(dst, m, &v);
  76. }
  77. #ifdef __cplusplus
  78. }
  79. #endif