plane.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 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,
  32. float dist)
  33. {
  34. vec3_copy(&dst->dir, dir);
  35. dst->dist = dist;
  36. }
  37. static inline void plane_setf(struct plane *dst, float a, float b, float c,
  38. float d)
  39. {
  40. vec3_set(&dst->dir, a, b, c);
  41. dst->dist = d;
  42. }
  43. EXPORT void plane_from_tri(struct plane *dst, const struct vec3 *v1,
  44. const struct vec3 *v2, const struct vec3 *v3);
  45. EXPORT void plane_transform(struct plane *dst, const struct plane *p,
  46. const struct matrix4 *m);
  47. EXPORT void plane_transform3x4(struct plane *dst, const struct plane *p,
  48. const struct matrix3 *m);
  49. EXPORT bool plane_intersection_ray(const struct plane *p,
  50. const struct vec3 *orig,
  51. const struct vec3 *dir, float *t);
  52. EXPORT bool plane_intersection_line(const struct plane *p,
  53. const struct vec3 *v1,
  54. const struct vec3 *v2, float *t);
  55. EXPORT bool plane_tri_inside(const struct plane *p, const struct vec3 *v1,
  56. const struct vec3 *v2, const struct vec3 *v3,
  57. float precision);
  58. EXPORT bool plane_line_inside(const struct plane *p, const struct vec3 *v1,
  59. const struct vec3 *v2, float precision);
  60. static inline bool plane_close(const struct plane *p1, const struct plane *p2,
  61. float precision)
  62. {
  63. return vec3_close(&p1->dir, &p2->dir, precision) &&
  64. close_float(p1->dist, p2->dist, precision);
  65. }
  66. static inline bool plane_coplanar(const struct plane *p1,
  67. const struct plane *p2, float precision)
  68. {
  69. float cos_angle = vec3_dot(&p1->dir, &p2->dir);
  70. if (close_float(cos_angle, 1.0f, precision))
  71. return close_float(p1->dist, p2->dist, precision);
  72. else if (close_float(cos_angle, -1.0f, precision))
  73. return close_float(-p1->dist, p2->dist, precision);
  74. return false;
  75. }
  76. #ifdef __cplusplus
  77. }
  78. #endif