plane.c 3.9 KB

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