Plane.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*************************************************************************/
  2. /* Plane.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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.hpp"
  31. #include "Vector3.hpp"
  32. #include <cmath>
  33. namespace godot {
  34. void Plane::set_normal(const Vector3 &p_normal) {
  35. this->normal = p_normal;
  36. }
  37. Vector3 Plane::project(const Vector3 &p_point) const {
  38. return p_point - normal * distance_to(p_point);
  39. }
  40. void Plane::normalize() {
  41. real_t l = normal.length();
  42. if (l == 0) {
  43. *this = Plane(0, 0, 0, 0);
  44. return;
  45. }
  46. normal /= l;
  47. d /= l;
  48. }
  49. Plane Plane::normalized() const {
  50. Plane p = *this;
  51. p.normalize();
  52. return p;
  53. }
  54. Vector3 Plane::get_any_point() const {
  55. return get_normal() * d;
  56. }
  57. Vector3 Plane::get_any_perpendicular_normal() const {
  58. static const Vector3 p1 = Vector3(1, 0, 0);
  59. static const Vector3 p2 = Vector3(0, 1, 0);
  60. Vector3 p;
  61. if (::fabs(normal.dot(p1)) > 0.99) // if too similar to p1
  62. p = p2; // use p2
  63. else
  64. p = p1; // use p1
  65. p -= normal * normal.dot(p);
  66. p.normalize();
  67. return p;
  68. }
  69. /* intersections */
  70. bool Plane::intersect_3(const Plane &p_plane1, const Plane &p_plane2, Vector3 *r_result) const {
  71. const Plane &p_plane0 = *this;
  72. Vector3 normal0 = p_plane0.normal;
  73. Vector3 normal1 = p_plane1.normal;
  74. Vector3 normal2 = p_plane2.normal;
  75. real_t denom = vec3_cross(normal0, normal1).dot(normal2);
  76. if (::fabs(denom) <= CMP_EPSILON)
  77. return false;
  78. if (r_result) {
  79. *r_result = ((vec3_cross(normal1, normal2) * p_plane0.d) +
  80. (vec3_cross(normal2, normal0) * p_plane1.d) +
  81. (vec3_cross(normal0, normal1) * p_plane2.d)) /
  82. denom;
  83. }
  84. return true;
  85. }
  86. bool Plane::intersects_ray(Vector3 p_from, Vector3 p_dir, Vector3 *p_intersection) const {
  87. Vector3 segment = p_dir;
  88. real_t den = normal.dot(segment);
  89. //printf("den is %i\n",den);
  90. if (::fabs(den) <= CMP_EPSILON) {
  91. return false;
  92. }
  93. real_t dist = (normal.dot(p_from) - d) / den;
  94. //printf("dist is %i\n",dist);
  95. if (dist > CMP_EPSILON) { //this is a ray, before the emiting pos (p_from) doesnt exist
  96. return false;
  97. }
  98. dist = -dist;
  99. *p_intersection = p_from + segment * dist;
  100. return true;
  101. }
  102. bool Plane::intersects_segment(Vector3 p_begin, Vector3 p_end, Vector3 *p_intersection) const {
  103. Vector3 segment = p_begin - p_end;
  104. real_t den = normal.dot(segment);
  105. //printf("den is %i\n",den);
  106. if (::fabs(den) <= CMP_EPSILON) {
  107. return false;
  108. }
  109. real_t dist = (normal.dot(p_begin) - d) / den;
  110. //printf("dist is %i\n",dist);
  111. if (dist < -CMP_EPSILON || dist > (1.0 + CMP_EPSILON)) {
  112. return false;
  113. }
  114. dist = -dist;
  115. *p_intersection = p_begin + segment * dist;
  116. return true;
  117. }
  118. /* misc */
  119. bool Plane::is_almost_like(const Plane &p_plane) const {
  120. return (normal.dot(p_plane.normal) > _PLANE_EQ_DOT_EPSILON && ::fabs(d - p_plane.d) < _PLANE_EQ_D_EPSILON);
  121. }
  122. Plane::operator String() const {
  123. // return normal.operator String() + ", " + rtos(d);
  124. return String(); // @Todo
  125. }
  126. bool Plane::is_point_over(const Vector3 &p_point) const {
  127. return (normal.dot(p_point) > d);
  128. }
  129. real_t Plane::distance_to(const Vector3 &p_point) const {
  130. return (normal.dot(p_point) - d);
  131. }
  132. bool Plane::has_point(const Vector3 &p_point, real_t _epsilon) const {
  133. real_t dist = normal.dot(p_point) - d;
  134. dist = ::fabs(dist);
  135. return (dist <= _epsilon);
  136. }
  137. Plane::Plane(const Vector3 &p_normal, real_t p_d) {
  138. normal = p_normal;
  139. d = p_d;
  140. }
  141. Plane::Plane(const Vector3 &p_point, const Vector3 &p_normal) {
  142. normal = p_normal;
  143. d = p_normal.dot(p_point);
  144. }
  145. Plane::Plane(const Vector3 &p_point1, const Vector3 &p_point2, const Vector3 &p_point3, ClockDirection p_dir) {
  146. if (p_dir == CLOCKWISE)
  147. normal = (p_point1 - p_point3).cross(p_point1 - p_point2);
  148. else
  149. normal = (p_point1 - p_point2).cross(p_point1 - p_point3);
  150. normal.normalize();
  151. d = normal.dot(p_point1);
  152. }
  153. bool Plane::operator==(const Plane &p_plane) const {
  154. return normal == p_plane.normal && d == p_plane.d;
  155. }
  156. bool Plane::operator!=(const Plane &p_plane) const {
  157. return normal != p_plane.normal || d != p_plane.d;
  158. }
  159. } // namespace godot