plane.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #ifndef GODOT_PLANE_HPP
  2. #define GODOT_PLANE_HPP
  3. #include <godot_cpp/core/math.hpp>
  4. #include <godot_cpp/variant/vector3.hpp>
  5. #include <godot_cpp/classes/global_constants.hpp>
  6. namespace godot {
  7. class Plane {
  8. public:
  9. _FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; }
  10. Vector3 normal;
  11. real_t d = 0;
  12. void set_normal(const Vector3 &p_normal);
  13. inline Vector3 get_normal() const { return normal; }; ///Point is coplanar, CMP_EPSILON for precision
  14. void normalize();
  15. Plane normalized() const;
  16. /* Plane-Point operations */
  17. inline Vector3 center() const { return normal * d; }
  18. Vector3 get_any_perpendicular_normal() const;
  19. inline bool is_point_over(const Vector3 &p_point) const; ///< Point is over plane
  20. inline real_t distance_to(const Vector3 &p_point) const;
  21. inline bool has_point(const Vector3 &p_point, real_t _epsilon = CMP_EPSILON) const;
  22. /* intersections */
  23. bool intersect_3(const Plane &p_plane1, const Plane &p_plane2, Vector3 *r_result = nullptr) const;
  24. bool intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *p_intersection) const;
  25. bool intersects_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 *p_intersection) const;
  26. inline Vector3 project(const Vector3 &p_point) const {
  27. return p_point - normal * distance_to(p_point);
  28. }
  29. /* misc */
  30. Plane operator-() const { return Plane(-normal, -d); }
  31. bool is_equal_approx(const Plane &p_plane) const;
  32. bool is_equal_approx_any_side(const Plane &p_plane) const;
  33. inline bool operator==(const Plane &p_plane) const;
  34. inline bool operator!=(const Plane &p_plane) const;
  35. operator String() const;
  36. inline Plane() {}
  37. inline Plane(real_t p_a, real_t p_b, real_t p_c, real_t p_d) :
  38. normal(p_a, p_b, p_c),
  39. d(p_d) {}
  40. inline Plane(const Vector3 &p_normal, real_t p_d);
  41. inline Plane(const Vector3 &p_point, const Vector3 &p_normal);
  42. inline Plane(const Vector3 &p_point1, const Vector3 &p_point2, const Vector3 &p_point3, ClockDirection p_dir = CLOCKWISE);
  43. };
  44. bool Plane::is_point_over(const Vector3 &p_point) const {
  45. return (normal.dot(p_point) > d);
  46. }
  47. real_t Plane::distance_to(const Vector3 &p_point) const {
  48. return (normal.dot(p_point) - d);
  49. }
  50. bool Plane::has_point(const Vector3 &p_point, real_t _epsilon) const {
  51. real_t dist = normal.dot(p_point) - d;
  52. dist = Math::abs(dist);
  53. return (dist <= _epsilon);
  54. }
  55. Plane::Plane(const Vector3 &p_normal, real_t p_d) :
  56. normal(p_normal),
  57. d(p_d) {
  58. }
  59. Plane::Plane(const Vector3 &p_point, const Vector3 &p_normal) :
  60. normal(p_normal),
  61. d(p_normal.dot(p_point)) {
  62. }
  63. Plane::Plane(const Vector3 &p_point1, const Vector3 &p_point2, const Vector3 &p_point3, ClockDirection p_dir) {
  64. if (p_dir == CLOCKWISE) {
  65. normal = (p_point1 - p_point3).cross(p_point1 - p_point2);
  66. } else {
  67. normal = (p_point1 - p_point2).cross(p_point1 - p_point3);
  68. }
  69. normal.normalize();
  70. d = normal.dot(p_point1);
  71. }
  72. bool Plane::operator==(const Plane &p_plane) const {
  73. return normal == p_plane.normal && d == p_plane.d;
  74. }
  75. bool Plane::operator!=(const Plane &p_plane) const {
  76. return normal != p_plane.normal || d != p_plane.d;
  77. }
  78. } // namespace godot
  79. #endif // GODOT_PLANE_HPP