plane.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 "math_funcs.h"
  32. #define _PLANE_EQ_DOT_EPSILON 0.999
  33. #define _PLANE_EQ_D_EPSILON 0.0001
  34. void Plane::set_normal(const Vector3 &p_normal) {
  35. normal = p_normal;
  36. }
  37. void Plane::normalize() {
  38. real_t l = normal.length();
  39. if (l == 0) {
  40. *this = Plane(0, 0, 0, 0);
  41. return;
  42. }
  43. normal /= l;
  44. d /= l;
  45. }
  46. Plane Plane::normalized() const {
  47. Plane p = *this;
  48. p.normalize();
  49. return p;
  50. }
  51. Vector3 Plane::get_any_point() const {
  52. return get_normal() * d;
  53. }
  54. Vector3 Plane::get_any_perpendicular_normal() const {
  55. static const Vector3 p1 = Vector3(1, 0, 0);
  56. static const Vector3 p2 = Vector3(0, 1, 0);
  57. Vector3 p;
  58. if (ABS(normal.dot(p1)) > 0.99) // if too similar to p1
  59. p = p2; // use p2
  60. else
  61. p = p1; // use p1
  62. p -= normal * normal.dot(p);
  63. p.normalize();
  64. return p;
  65. }
  66. /* intersections */
  67. bool Plane::intersect_3(const Plane &p_plane1, const Plane &p_plane2, Vector3 *r_result) const {
  68. const Plane &p_plane0 = *this;
  69. Vector3 normal0 = p_plane0.normal;
  70. Vector3 normal1 = p_plane1.normal;
  71. Vector3 normal2 = p_plane2.normal;
  72. real_t denom = vec3_cross(normal0, normal1).dot(normal2);
  73. if (ABS(denom) <= CMP_EPSILON)
  74. return false;
  75. if (r_result) {
  76. *r_result = ((vec3_cross(normal1, normal2) * p_plane0.d) +
  77. (vec3_cross(normal2, normal0) * p_plane1.d) +
  78. (vec3_cross(normal0, normal1) * p_plane2.d)) /
  79. denom;
  80. }
  81. return true;
  82. }
  83. bool Plane::intersects_ray(Vector3 p_from, Vector3 p_dir, Vector3 *p_intersection) const {
  84. Vector3 segment = p_dir;
  85. real_t den = normal.dot(segment);
  86. //printf("den is %i\n",den);
  87. if (Math::abs(den) <= CMP_EPSILON) {
  88. return false;
  89. }
  90. real_t dist = (normal.dot(p_from) - d) / den;
  91. //printf("dist is %i\n",dist);
  92. if (dist > CMP_EPSILON) { //this is a ray, before the emiting pos (p_from) doesnt exist
  93. return false;
  94. }
  95. dist = -dist;
  96. *p_intersection = p_from + segment * dist;
  97. return true;
  98. }
  99. bool Plane::intersects_segment(Vector3 p_begin, Vector3 p_end, Vector3 *p_intersection) const {
  100. Vector3 segment = p_begin - p_end;
  101. real_t den = normal.dot(segment);
  102. //printf("den is %i\n",den);
  103. if (Math::abs(den) <= CMP_EPSILON) {
  104. return false;
  105. }
  106. real_t dist = (normal.dot(p_begin) - d) / den;
  107. //printf("dist is %i\n",dist);
  108. if (dist < -CMP_EPSILON || dist > (1.0 + CMP_EPSILON)) {
  109. return false;
  110. }
  111. dist = -dist;
  112. *p_intersection = p_begin + segment * dist;
  113. return true;
  114. }
  115. /* misc */
  116. bool Plane::is_almost_like(const Plane &p_plane) const {
  117. return (normal.dot(p_plane.normal) > _PLANE_EQ_DOT_EPSILON && Math::absd(d - p_plane.d) < _PLANE_EQ_D_EPSILON);
  118. }
  119. Plane::operator String() const {
  120. return normal.operator String() + ", " + rtos(d);
  121. }