plane.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**************************************************************************/
  2. /* plane.hpp */
  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. #ifndef GODOT_PLANE_HPP
  31. #define GODOT_PLANE_HPP
  32. #include <godot_cpp/classes/global_constants.hpp>
  33. #include <godot_cpp/variant/vector3.hpp>
  34. namespace godot {
  35. class Variant;
  36. struct _NO_DISCARD_ Plane {
  37. Vector3 normal;
  38. real_t d = 0;
  39. void set_normal(const Vector3 &p_normal);
  40. _FORCE_INLINE_ Vector3 get_normal() const { return normal; }
  41. void normalize();
  42. Plane normalized() const;
  43. /* Plane-Point operations */
  44. _FORCE_INLINE_ Vector3 center() const { return normal * d; }
  45. Vector3 get_any_perpendicular_normal() const;
  46. _FORCE_INLINE_ bool is_point_over(const Vector3 &p_point) const; ///< Point is over plane
  47. _FORCE_INLINE_ real_t distance_to(const Vector3 &p_point) const;
  48. _FORCE_INLINE_ bool has_point(const Vector3 &p_point, real_t p_tolerance = CMP_EPSILON) const;
  49. /* intersections */
  50. bool intersect_3(const Plane &p_plane1, const Plane &p_plane2, Vector3 *r_result = nullptr) const;
  51. bool intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *p_intersection) const;
  52. bool intersects_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 *p_intersection) const;
  53. // For Variant bindings.
  54. Variant intersect_3_bind(const Plane &p_plane1, const Plane &p_plane2) const;
  55. Variant intersects_ray_bind(const Vector3 &p_from, const Vector3 &p_dir) const;
  56. Variant intersects_segment_bind(const Vector3 &p_begin, const Vector3 &p_end) const;
  57. _FORCE_INLINE_ Vector3 project(const Vector3 &p_point) const {
  58. return p_point - normal * distance_to(p_point);
  59. }
  60. /* misc */
  61. Plane operator-() const { return Plane(-normal, -d); }
  62. bool is_equal_approx(const Plane &p_plane) const;
  63. bool is_equal_approx_any_side(const Plane &p_plane) const;
  64. bool is_finite() const;
  65. _FORCE_INLINE_ bool operator==(const Plane &p_plane) const;
  66. _FORCE_INLINE_ bool operator!=(const Plane &p_plane) const;
  67. operator String() const;
  68. _FORCE_INLINE_ Plane() {}
  69. _FORCE_INLINE_ Plane(real_t p_a, real_t p_b, real_t p_c, real_t p_d) :
  70. normal(p_a, p_b, p_c),
  71. d(p_d) {}
  72. _FORCE_INLINE_ Plane(const Vector3 &p_normal, real_t p_d = 0.0);
  73. _FORCE_INLINE_ Plane(const Vector3 &p_normal, const Vector3 &p_point);
  74. _FORCE_INLINE_ Plane(const Vector3 &p_point1, const Vector3 &p_point2, const Vector3 &p_point3, ClockDirection p_dir = CLOCKWISE);
  75. };
  76. bool Plane::is_point_over(const Vector3 &p_point) const {
  77. return (normal.dot(p_point) > d);
  78. }
  79. real_t Plane::distance_to(const Vector3 &p_point) const {
  80. return (normal.dot(p_point) - d);
  81. }
  82. bool Plane::has_point(const Vector3 &p_point, real_t p_tolerance) const {
  83. real_t dist = normal.dot(p_point) - d;
  84. dist = Math::abs(dist);
  85. return (dist <= p_tolerance);
  86. }
  87. Plane::Plane(const Vector3 &p_normal, real_t p_d) :
  88. normal(p_normal),
  89. d(p_d) {
  90. }
  91. Plane::Plane(const Vector3 &p_normal, const Vector3 &p_point) :
  92. normal(p_normal),
  93. d(p_normal.dot(p_point)) {
  94. }
  95. Plane::Plane(const Vector3 &p_point1, const Vector3 &p_point2, const Vector3 &p_point3, ClockDirection p_dir) {
  96. if (p_dir == CLOCKWISE) {
  97. normal = (p_point1 - p_point3).cross(p_point1 - p_point2);
  98. } else {
  99. normal = (p_point1 - p_point2).cross(p_point1 - p_point3);
  100. }
  101. normal.normalize();
  102. d = normal.dot(p_point1);
  103. }
  104. bool Plane::operator==(const Plane &p_plane) const {
  105. return normal == p_plane.normal && d == p_plane.d;
  106. }
  107. bool Plane::operator!=(const Plane &p_plane) const {
  108. return normal != p_plane.normal || d != p_plane.d;
  109. }
  110. } // namespace godot
  111. #endif // GODOT_PLANE_HPP