plane.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #include "../util/c99defs.h"
  15. #include "matrix3.h"
  16. #include "plane.h"
  17. void plane_from_tri(struct plane *dst, const struct vec3 *v1, const struct vec3 *v2, const struct vec3 *v3)
  18. {
  19. struct vec3 temp;
  20. vec3_sub(&temp, v2, v1);
  21. vec3_sub(&dst->dir, v3, v1);
  22. vec3_cross(&dst->dir, &temp, &dst->dir);
  23. vec3_norm(&dst->dir, &dst->dir);
  24. dst->dist = vec3_dot(v1, &dst->dir);
  25. }
  26. void plane_transform(struct plane *dst, const struct plane *p, const struct matrix4 *m)
  27. {
  28. struct vec3 temp;
  29. vec3_zero(&temp);
  30. vec3_transform(&dst->dir, &p->dir, m);
  31. vec3_norm(&dst->dir, &dst->dir);
  32. vec3_transform(&temp, &temp, m);
  33. dst->dist = p->dist - vec3_dot(&dst->dir, &temp);
  34. }
  35. void plane_transform3x4(struct plane *dst, const struct plane *p, const struct matrix3 *m)
  36. {
  37. struct vec3 temp;
  38. vec3_transform3x4(&dst->dir, &p->dir, m);
  39. vec3_norm(&dst->dir, &dst->dir);
  40. vec3_transform3x4(&temp, &m->t, m);
  41. dst->dist = p->dist - vec3_dot(&dst->dir, &temp);
  42. }
  43. bool plane_intersection_ray(const struct plane *p, const struct vec3 *orig, const struct vec3 *dir, float *t)
  44. {
  45. float c = vec3_dot(&p->dir, dir);
  46. if (fabsf(c) < EPSILON) {
  47. *t = 0.0f;
  48. return false;
  49. } else {
  50. *t = (p->dist - vec3_dot(&p->dir, orig)) / c;
  51. return true;
  52. }
  53. }
  54. bool plane_intersection_line(const struct plane *p, const struct vec3 *v1, const struct vec3 *v2, float *t)
  55. {
  56. float p1_dist, p2_dist, p1_abs_dist, dist2;
  57. bool p1_over, p2_over;
  58. p1_dist = vec3_plane_dist(v1, p);
  59. p2_dist = vec3_plane_dist(v2, p);
  60. if (close_float(p1_dist, 0.0f, EPSILON)) {
  61. if (close_float(p2_dist, 0.0f, EPSILON))
  62. return false;
  63. *t = 0.0f;
  64. return true;
  65. } else if (close_float(p2_dist, 0.0f, EPSILON)) {
  66. *t = 1.0f;
  67. return true;
  68. }
  69. p1_over = (p1_dist > 0.0f);
  70. p2_over = (p2_dist > 0.0f);
  71. if (p1_over == p2_over)
  72. return false;
  73. p1_abs_dist = fabsf(p1_dist);
  74. dist2 = p1_abs_dist + fabsf(p2_dist);
  75. if (dist2 < EPSILON)
  76. return false;
  77. *t = p1_abs_dist / dist2;
  78. return true;
  79. }
  80. bool plane_tri_inside(const struct plane *p, const struct vec3 *v1, const struct vec3 *v2, const struct vec3 *v3,
  81. float precision)
  82. {
  83. /* bit 1: part or all is behind the plane */
  84. /* bit 2: part or all is in front of the plane */
  85. int sides = 0;
  86. float d1 = vec3_plane_dist(v1, p);
  87. float d2 = vec3_plane_dist(v2, p);
  88. float d3 = vec3_plane_dist(v3, p);
  89. if (d1 >= precision)
  90. sides = 2;
  91. else if (d1 <= -precision)
  92. sides = 1;
  93. if (d2 >= precision)
  94. sides |= 2;
  95. else if (d2 <= -precision)
  96. sides |= 1;
  97. if (d3 >= precision)
  98. sides |= 2;
  99. else if (d3 <= -precision)
  100. sides |= 1;
  101. return sides;
  102. }
  103. bool plane_line_inside(const struct plane *p, const struct vec3 *v1, const struct vec3 *v2, float precision)
  104. {
  105. /* bit 1: part or all is behind the plane */
  106. /* bit 2: part or all is in front of the plane */
  107. int sides = 0;
  108. float d1 = vec3_plane_dist(v1, p);
  109. float d2 = vec3_plane_dist(v2, p);
  110. if (d1 >= precision)
  111. sides = 2;
  112. else if (d1 <= -precision)
  113. sides = 1;
  114. if (d2 >= precision)
  115. sides |= 2;
  116. else if (d2 <= -precision)
  117. sides |= 1;
  118. return sides;
  119. }