plane.h 2.9 KB

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