plane.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 matrix3 *m)
  31. {
  32. struct vec3 temp;
  33. vec3_transform(&dst->dir, &p->dir, m);
  34. vec3_norm(&dst->dir, &dst->dir);
  35. vec3_transform(&temp, &m->t, m);
  36. dst->dist = p->dist - vec3_dot(&dst->dir, &temp);
  37. }
  38. bool plane_intersection_ray(const struct plane *p, const struct vec3 *orig,
  39. const struct vec3 *dir, float *t)
  40. {
  41. float c = vec3_dot(&p->dir, dir);
  42. if (fabsf(c) < EPSILON) {
  43. *t = 0.0f;
  44. return false;
  45. } else {
  46. *t = (p->dist - vec3_dot(&p->dir, orig)) / c;
  47. return true;
  48. }
  49. }
  50. bool plane_intersection_line(const struct plane *p, const struct vec3 *v1,
  51. const struct vec3 *v2, float *t)
  52. {
  53. float p1_dist, p2_dist, p1_abs_dist, dist2;
  54. bool p1_over, p2_over;
  55. p1_dist = vec3_plane_dist(v1, p);
  56. p2_dist = vec3_plane_dist(v2, p);
  57. if (close_float(p1_dist, 0.0f, EPSILON)) {
  58. if (close_float(p2_dist, 0.0f, EPSILON))
  59. return false;
  60. *t = 0.0f;
  61. return true;
  62. } else if (close_float(p2_dist, 0.0f, EPSILON)) {
  63. *t = 1.0f;
  64. return true;
  65. }
  66. p1_over = (p1_dist > 0.0f);
  67. p2_over = (p2_dist > 0.0f);
  68. if (p1_over == p2_over)
  69. return false;
  70. p1_abs_dist = fabsf(p1_dist);
  71. dist2 = p1_abs_dist + fabsf(p2_dist);
  72. if (dist2 < EPSILON)
  73. return false;
  74. *t = p1_abs_dist / dist2;
  75. return true;
  76. }
  77. bool plane_tri_inside(const struct plane *p,
  78. const struct vec3 *v1,
  79. const struct vec3 *v2,
  80. 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,
  104. const struct vec3 *v2, float precision)
  105. {
  106. /* bit 1: part or all is behind the plane */
  107. /* bit 2: part or all is in front of the plane */
  108. int sides = 0;
  109. float d1 = vec3_plane_dist(v1, p);
  110. float d2 = vec3_plane_dist(v2, p);
  111. if (d1 >= precision)
  112. sides = 2;
  113. else if (d1 <= -precision)
  114. sides = 1;
  115. if (d2 >= precision)
  116. sides |= 2;
  117. else if (d2 <= -precision)
  118. sides |= 1;
  119. return sides;
  120. }