| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/math/vector3.h" |
| 34 |
|
| 35 |
class Variant; |
| 36 |
|
| 37 |
struct [[nodiscard]] Plane { |
| 38 |
static const Plane PLANE_YZ; |
| 39 |
static const Plane PLANE_XZ; |
| 40 |
static const Plane PLANE_XY; |
| 41 |
|
| 42 |
Vector3 normal; |
| 43 |
real_t d = 0; |
| 44 |
|
| 45 |
void set_normal(const Vector3 &p_normal); |
| 46 |
_FORCE_INLINE_ Vector3 get_normal() const { return normal; } |
| 47 |
|
| 48 |
void normalize(); |
| 49 |
Plane normalized() const; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
_FORCE_INLINE_ Vector3 get_center() const { return normal * d; } |
| 54 |
Vector3 get_any_perpendicular_normal() const; |
| 55 |
|
| 56 |
_FORCE_INLINE_ bool is_point_over(const Vector3 &p_point) const; |
| 57 |
_FORCE_INLINE_ real_t distance_to(const Vector3 &p_point) const; |
| 58 |
_FORCE_INLINE_ bool has_point(const Vector3 &p_point, real_t p_tolerance = CMP_EPSILON) const; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
bool intersect_3(const Plane &p_plane1, const Plane &p_plane2, Vector3 *r_result = nullptr) const; |
| 63 |
bool intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *p_intersection) const; |
| 64 |
bool intersects_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 *p_intersection) const; |
| 65 |
|
| 66 |
|
| 67 |
Variant intersect_3_bind(const Plane &p_plane1, const Plane &p_plane2) const; |
| 68 |
Variant intersects_ray_bind(const Vector3 &p_from, const Vector3 &p_dir) const; |
| 69 |
Variant intersects_segment_bind(const Vector3 &p_begin, const Vector3 &p_end) const; |
| 70 |
|
| 71 |
_FORCE_INLINE_ Vector3 project(const Vector3 &p_point) const { |
| 72 |
return p_point - normal * distance_to(p_point); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
constexpr Plane operator-() const { return Plane(-normal, -d); } |
| 78 |
bool is_equal_approx(const Plane &p_plane) const; |
| 79 |
bool is_same(const Plane &p_plane) const; |
| 80 |
bool is_equal_approx_any_side(const Plane &p_plane) const; |
| 81 |
bool is_finite() const; |
| 82 |
|
| 83 |
constexpr bool operator==(const Plane &p_plane) const; |
| 84 |
constexpr bool operator!=(const Plane &p_plane) const; |
| 85 |
explicit operator String() const; |
| 86 |
|
| 87 |
Plane() = default; |
| 88 |
constexpr Plane(real_t p_a, real_t p_b, real_t p_c, real_t p_d) : |
| 89 |
normal(p_a, p_b, p_c), |
| 90 |
d(p_d) {} |
| 91 |
|
| 92 |
constexpr Plane(const Vector3 &p_normal, real_t p_d = 0.0); |
| 93 |
_FORCE_INLINE_ Plane(const Vector3 &p_normal, const Vector3 &p_point); |
| 94 |
_FORCE_INLINE_ Plane(const Vector3 &p_point1, const Vector3 &p_point2, const Vector3 &p_point3, ClockDirection p_dir = CLOCKWISE); |
| 95 |
}; |
| 96 |
|
| 97 |
inline constexpr Plane Plane::PLANE_YZ = { 1, 0, 0, 0 }; |
| 98 |
inline constexpr Plane Plane::PLANE_XZ = { 0, 1, 0, 0 }; |
| 99 |
inline constexpr Plane Plane::PLANE_XY = { 0, 0, 1, 0 }; |
| 100 |
|
| 101 |
bool Plane::is_point_over(const Vector3 &p_point) const { |
| 102 |
return (normal.dot(p_point) > d); |
| 103 |
} |
| 104 |
|
| 105 |
real_t Plane::distance_to(const Vector3 &p_point) const { |
| 106 |
return (normal.dot(p_point) - d); |
| 107 |
} |
| 108 |
|
| 109 |
bool Plane::has_point(const Vector3 &p_point, real_t p_tolerance) const { |
| 110 |
real_t dist = normal.dot(p_point) - d; |
| 111 |
dist = Math::abs(dist); |
| 112 |
return (dist <= p_tolerance); |
| 113 |
} |
| 114 |
|
| 115 |
constexpr Plane::Plane(const Vector3 &p_normal, real_t p_d) : |
| 116 |
normal(p_normal), |
| 117 |
d(p_d) { |
| 118 |
} |
| 119 |
|
| 120 |
Plane::Plane(const Vector3 &p_normal, const Vector3 &p_point) : |
| 121 |
normal(p_normal), |
| 122 |
d(p_normal.dot(p_point)) { |
| 123 |
} |
| 124 |
|
| 125 |
Plane::Plane(const Vector3 &p_point1, const Vector3 &p_point2, const Vector3 &p_point3, ClockDirection p_dir) { |
| 126 |
if (p_dir == CLOCKWISE) { |
| 127 |
normal = (p_point1 - p_point3).cross(p_point1 - p_point2); |
| 128 |
} else { |
| 129 |
normal = (p_point1 - p_point2).cross(p_point1 - p_point3); |
| 130 |
} |
| 131 |
|
| 132 |
normal.normalize(); |
| 133 |
d = normal.dot(p_point1); |
| 134 |
} |
| 135 |
|
| 136 |
constexpr bool Plane::operator==(const Plane &p_plane) const { |
| 137 |
return normal == p_plane.normal && d == p_plane.d; |
| 138 |
} |
| 139 |
|
| 140 |
constexpr bool Plane::operator!=(const Plane &p_plane) const { |
| 141 |
return normal != p_plane.normal || d != p_plane.d; |
| 142 |
} |
| 143 |
|
| 144 |
template <> |
| 145 |
struct is_zero_constructible<Plane> : std::true_type {}; |