matrix3.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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,
  47. const struct matrix3 *m2);
  48. static inline void matrix3_translate(struct matrix3 *dst,
  49. const struct matrix3 *m,
  50. const struct vec3 *v)
  51. {
  52. vec3_sub(&dst->t, &m->t, v);
  53. }
  54. EXPORT void matrix3_rotate(struct matrix3 *dst, const struct matrix3 *m,
  55. const struct quat *q);
  56. EXPORT void matrix3_rotate_aa(struct matrix3 *dst, const struct matrix3 *m,
  57. const struct axisang *aa);
  58. EXPORT void matrix3_scale(struct matrix3 *dst, const struct matrix3 *m,
  59. const struct vec3 *v);
  60. EXPORT void matrix3_transpose(struct matrix3 *dst, const struct matrix3 *m);
  61. EXPORT void matrix3_inv(struct matrix3 *dst, const struct matrix3 *m);
  62. EXPORT void matrix3_mirror(struct matrix3 *dst, const struct matrix3 *m,
  63. const struct plane *p);
  64. EXPORT void matrix3_mirrorv(struct matrix3 *dst, const struct matrix3 *m,
  65. const struct vec3 *v);
  66. static inline void matrix3_translate3f(struct matrix3 *dst,
  67. const struct matrix3 *m, float x,
  68. float y, float z)
  69. {
  70. struct vec3 v;
  71. vec3_set(&v, x, y, z);
  72. matrix3_translate(dst, m, &v);
  73. }
  74. static inline void matrix3_rotate_aa4f(struct matrix3 *dst,
  75. const struct matrix3 *m, float x,
  76. float y, float z, float rot)
  77. {
  78. struct axisang aa;
  79. axisang_set(&aa, x, y, z, rot);
  80. matrix3_rotate_aa(dst, m, &aa);
  81. }
  82. static inline void matrix3_scale3f(struct matrix3 *dst, const struct matrix3 *m,
  83. float x, float y, float z)
  84. {
  85. struct vec3 v;
  86. vec3_set(&v, x, y, z);
  87. matrix3_scale(dst, m, &v);
  88. }
  89. #ifdef __cplusplus
  90. }
  91. #endif