plane.c 4.0 KB

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