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