plane.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /**************************************************************************/
  2. /* plane.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "plane.h"
  31. #include "core/math/math_funcs.h"
  32. #include "core/variant/variant.h"
  33. void Plane::set_normal(const Vector3 &p_normal) {
  34. normal = p_normal;
  35. }
  36. void Plane::normalize() {
  37. real_t l = normal.length();
  38. if (l == 0) {
  39. *this = Plane(0, 0, 0, 0);
  40. return;
  41. }
  42. normal /= l;
  43. d /= l;
  44. }
  45. Plane Plane::normalized() const {
  46. Plane p = *this;
  47. p.normalize();
  48. return p;
  49. }
  50. Vector3 Plane::get_any_perpendicular_normal() const {
  51. static const Vector3 p1 = Vector3(1, 0, 0);
  52. static const Vector3 p2 = Vector3(0, 1, 0);
  53. Vector3 p;
  54. if (ABS(normal.dot(p1)) > 0.99f) { // if too similar to p1
  55. p = p2; // use p2
  56. } else {
  57. p = p1; // use p1
  58. }
  59. p -= normal * normal.dot(p);
  60. p.normalize();
  61. return p;
  62. }
  63. /* intersections */
  64. bool Plane::intersect_3(const Plane &p_plane1, const Plane &p_plane2, Vector3 *r_result) const {
  65. const Plane &p_plane0 = *this;
  66. Vector3 normal0 = p_plane0.normal;
  67. Vector3 normal1 = p_plane1.normal;
  68. Vector3 normal2 = p_plane2.normal;
  69. real_t denom = vec3_cross(normal0, normal1).dot(normal2);
  70. if (Math::is_zero_approx(denom)) {
  71. return false;
  72. }
  73. if (r_result) {
  74. *r_result = ((vec3_cross(normal1, normal2) * p_plane0.d) +
  75. (vec3_cross(normal2, normal0) * p_plane1.d) +
  76. (vec3_cross(normal0, normal1) * p_plane2.d)) /
  77. denom;
  78. }
  79. return true;
  80. }
  81. bool Plane::intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *p_intersection) const {
  82. Vector3 segment = p_dir;
  83. real_t den = normal.dot(segment);
  84. if (Math::is_zero_approx(den)) {
  85. return false;
  86. }
  87. real_t dist = (normal.dot(p_from) - d) / den;
  88. if (dist > (real_t)CMP_EPSILON) { //this is a ray, before the emitting pos (p_from) doesn't exist
  89. return false;
  90. }
  91. dist = -dist;
  92. *p_intersection = p_from + segment * dist;
  93. return true;
  94. }
  95. bool Plane::intersects_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 *p_intersection) const {
  96. Vector3 segment = p_begin - p_end;
  97. real_t den = normal.dot(segment);
  98. if (Math::is_zero_approx(den)) {
  99. return false;
  100. }
  101. real_t dist = (normal.dot(p_begin) - d) / den;
  102. if (dist < (real_t)-CMP_EPSILON || dist > (1.0f + (real_t)CMP_EPSILON)) {
  103. return false;
  104. }
  105. dist = -dist;
  106. *p_intersection = p_begin + segment * dist;
  107. return true;
  108. }
  109. Variant Plane::intersect_3_bind(const Plane &p_plane1, const Plane &p_plane2) const {
  110. Vector3 inters;
  111. if (intersect_3(p_plane1, p_plane2, &inters)) {
  112. return inters;
  113. } else {
  114. return Variant();
  115. }
  116. }
  117. Variant Plane::intersects_ray_bind(const Vector3 &p_from, const Vector3 &p_dir) const {
  118. Vector3 inters;
  119. if (intersects_ray(p_from, p_dir, &inters)) {
  120. return inters;
  121. } else {
  122. return Variant();
  123. }
  124. }
  125. Variant Plane::intersects_segment_bind(const Vector3 &p_begin, const Vector3 &p_end) const {
  126. Vector3 inters;
  127. if (intersects_segment(p_begin, p_end, &inters)) {
  128. return inters;
  129. } else {
  130. return Variant();
  131. }
  132. }
  133. /* misc */
  134. bool Plane::is_equal_approx_any_side(const Plane &p_plane) const {
  135. return (normal.is_equal_approx(p_plane.normal) && Math::is_equal_approx(d, p_plane.d)) || (normal.is_equal_approx(-p_plane.normal) && Math::is_equal_approx(d, -p_plane.d));
  136. }
  137. bool Plane::is_equal_approx(const Plane &p_plane) const {
  138. return normal.is_equal_approx(p_plane.normal) && Math::is_equal_approx(d, p_plane.d);
  139. }
  140. bool Plane::is_finite() const {
  141. return normal.is_finite() && Math::is_finite(d);
  142. }
  143. Plane::operator String() const {
  144. return "[N: " + normal.operator String() + ", D: " + String::num_real(d, false) + "]";
  145. }