plane.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*************************************************************************/
  2. /* plane.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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. void Plane::set_normal(const Vector3 &p_normal) {
  33. normal = p_normal;
  34. }
  35. void Plane::normalize() {
  36. real_t l = normal.length();
  37. if (l == 0) {
  38. *this = Plane(0, 0, 0, 0);
  39. return;
  40. }
  41. normal /= l;
  42. d /= l;
  43. }
  44. Plane Plane::normalized() const {
  45. Plane p = *this;
  46. p.normalize();
  47. return p;
  48. }
  49. Vector3 Plane::get_any_point() const {
  50. return get_normal() * d;
  51. }
  52. Vector3 Plane::get_any_perpendicular_normal() const {
  53. static const Vector3 p1 = Vector3(1, 0, 0);
  54. static const Vector3 p2 = Vector3(0, 1, 0);
  55. Vector3 p;
  56. if (ABS(normal.dot(p1)) > 0.99) // if too similar to p1
  57. p = p2; // use p2
  58. else
  59. p = p1; // use p1
  60. p -= normal * normal.dot(p);
  61. p.normalize();
  62. return p;
  63. }
  64. /* intersections */
  65. bool Plane::intersect_3(const Plane &p_plane1, const Plane &p_plane2, Vector3 *r_result) const {
  66. const Plane &p_plane0 = *this;
  67. Vector3 normal0 = p_plane0.normal;
  68. Vector3 normal1 = p_plane1.normal;
  69. Vector3 normal2 = p_plane2.normal;
  70. real_t denom = vec3_cross(normal0, normal1).dot(normal2);
  71. if (Math::is_zero_approx(denom))
  72. return false;
  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. //printf("den is %i\n",den);
  85. if (Math::is_zero_approx(den)) {
  86. return false;
  87. }
  88. real_t dist = (normal.dot(p_from) - d) / den;
  89. //printf("dist is %i\n",dist);
  90. if (dist > CMP_EPSILON) { //this is a ray, before the emitting pos (p_from) doesn't exist
  91. return false;
  92. }
  93. dist = -dist;
  94. *p_intersection = p_from + segment * dist;
  95. return true;
  96. }
  97. bool Plane::intersects_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 *p_intersection) const {
  98. Vector3 segment = p_begin - p_end;
  99. real_t den = normal.dot(segment);
  100. //printf("den is %i\n",den);
  101. if (Math::is_zero_approx(den)) {
  102. return false;
  103. }
  104. real_t dist = (normal.dot(p_begin) - d) / den;
  105. //printf("dist is %i\n",dist);
  106. if (dist < -CMP_EPSILON || dist > (1.0 + CMP_EPSILON)) {
  107. return false;
  108. }
  109. dist = -dist;
  110. *p_intersection = p_begin + segment * dist;
  111. return true;
  112. }
  113. /* misc */
  114. bool Plane::is_equal_approx(const Plane &p_plane) const {
  115. return normal.is_equal_approx(p_plane.normal) && Math::is_equal_approx(d, p_plane.d);
  116. }
  117. Plane::operator String() const {
  118. return normal.operator String() + ", " + rtos(d);
  119. }