plane.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. #pragma once
  15. #include "math-defs.h"
  16. #include "vec3.h"
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. struct matrix3;
  21. struct matrix4;
  22. struct plane {
  23. struct vec3 dir;
  24. float dist;
  25. };
  26. static inline void plane_copy(struct plane *dst, const struct plane *p)
  27. {
  28. vec3_copy(&dst->dir, &p->dir);
  29. dst->dist = p->dist;
  30. }
  31. static inline void plane_set(struct plane *dst, const struct vec3 *dir, float dist)
  32. {
  33. vec3_copy(&dst->dir, dir);
  34. dst->dist = dist;
  35. }
  36. static inline void plane_setf(struct plane *dst, float a, float b, float c, float d)
  37. {
  38. vec3_set(&dst->dir, a, b, c);
  39. dst->dist = d;
  40. }
  41. EXPORT void plane_from_tri(struct plane *dst, const struct vec3 *v1, const struct vec3 *v2, const struct vec3 *v3);
  42. EXPORT void plane_transform(struct plane *dst, const struct plane *p, const struct matrix4 *m);
  43. EXPORT void plane_transform3x4(struct plane *dst, const struct plane *p, const struct matrix3 *m);
  44. EXPORT bool plane_intersection_ray(const struct plane *p, const struct vec3 *orig, const struct vec3 *dir, float *t);
  45. EXPORT bool plane_intersection_line(const struct plane *p, const struct vec3 *v1, const struct vec3 *v2, float *t);
  46. EXPORT bool plane_tri_inside(const struct plane *p, const struct vec3 *v1, const struct vec3 *v2, const struct vec3 *v3,
  47. float precision);
  48. EXPORT bool plane_line_inside(const struct plane *p, const struct vec3 *v1, const struct vec3 *v2, float precision);
  49. static inline bool plane_close(const struct plane *p1, const struct plane *p2, float precision)
  50. {
  51. return vec3_close(&p1->dir, &p2->dir, precision) && close_float(p1->dist, p2->dist, precision);
  52. }
  53. static inline bool plane_coplanar(const struct plane *p1, const struct plane *p2, float precision)
  54. {
  55. float cos_angle = vec3_dot(&p1->dir, &p2->dir);
  56. if (close_float(cos_angle, 1.0f, precision))
  57. return close_float(p1->dist, p2->dist, precision);
  58. else if (close_float(cos_angle, -1.0f, precision))
  59. return close_float(-p1->dist, p2->dist, precision);
  60. return false;
  61. }
  62. #ifdef __cplusplus
  63. }
  64. #endif