1 /*************************************************************************/
2 /* plane.h */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
9 /* Copyright (c) 2014-2019 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 #ifndef PLANE_H
31 #define PLANE_H
32
33 #include "vector3.h"
34
35 class Plane {
36 public:
37 Vector3 normal;
38 real_t d;
39
40 void set_normal(const Vector3 &p_normal);
get_normal()41 _FORCE_INLINE_ Vector3 get_normal() const { return normal; }; ///Point is coplanar, CMP_EPSILON for precision
42
43 void normalize();
44 Plane normalized() const;
45
46 /* Plane-Point operations */
47
center()48 _FORCE_INLINE_ Vector3 center() const { return normal * d; }
49 Vector3 get_any_point() const;
50 Vector3 get_any_perpendicular_normal() const;
51
52 _FORCE_INLINE_ bool is_point_over(const Vector3 &p_point) const; ///< Point is over plane
53 _FORCE_INLINE_ real_t distance_to(const Vector3 &p_point) const;
54 _FORCE_INLINE_ bool has_point(const Vector3 &p_point, real_t _epsilon = CMP_EPSILON) const;
55
56 /* intersections */
57
58 bool intersect_3(const Plane &p_plane1, const Plane &p_plane2, Vector3 *r_result = 0) const;
59 bool intersects_ray(Vector3 p_from, Vector3 p_dir, Vector3 *p_intersection) const;
60 bool intersects_segment(Vector3 p_begin, Vector3 p_end, Vector3 *p_intersection) const;
61
project(const Vector3 & p_point)62 _FORCE_INLINE_ Vector3 project(const Vector3 &p_point) const {
63
64 return p_point - normal * distance_to(p_point);
65 }
66
67 /* misc */
68
69 Plane operator-() const { return Plane(-normal, -d); }
70 bool is_almost_like(const Plane &p_plane) const;
71
72 _FORCE_INLINE_ bool operator==(const Plane &p_plane) const;
73 _FORCE_INLINE_ bool operator!=(const Plane &p_plane) const;
74 operator String() const;
75
Plane()76 _FORCE_INLINE_ Plane() { d = 0; }
Plane(real_t p_a,real_t p_b,real_t p_c,real_t p_d)77 _FORCE_INLINE_ Plane(real_t p_a, real_t p_b, real_t p_c, real_t p_d) :
78 normal(p_a, p_b, p_c),
79 d(p_d){};
80
81 _FORCE_INLINE_ Plane(const Vector3 &p_normal, real_t p_d);
82 _FORCE_INLINE_ Plane(const Vector3 &p_point, const Vector3 &p_normal);
83 _FORCE_INLINE_ Plane(const Vector3 &p_point1, const Vector3 &p_point2, const Vector3 &p_point3, ClockDirection p_dir = CLOCKWISE);
84 };
85
is_point_over(const Vector3 & p_point)86 bool Plane::is_point_over(const Vector3 &p_point) const {
87
88 return (normal.dot(p_point) > d);
89 }
90
distance_to(const Vector3 & p_point)91 real_t Plane::distance_to(const Vector3 &p_point) const {
92
93 return (normal.dot(p_point) - d);
94 }
95
has_point(const Vector3 & p_point,real_t _epsilon)96 bool Plane::has_point(const Vector3 &p_point, real_t _epsilon) const {
97
98 float dist = normal.dot(p_point) - d;
99 dist = ABS(dist);
100 return (dist <= _epsilon);
101 }
102
Plane(const Vector3 & p_normal,real_t p_d)103 Plane::Plane(const Vector3 &p_normal, real_t p_d) {
104
105 normal = p_normal;
106 d = p_d;
107 }
108
Plane(const Vector3 & p_point,const Vector3 & p_normal)109 Plane::Plane(const Vector3 &p_point, const Vector3 &p_normal) {
110
111 normal = p_normal;
112 d = p_normal.dot(p_point);
113 }
114
Plane(const Vector3 & p_point1,const Vector3 & p_point2,const Vector3 & p_point3,ClockDirection p_dir)115 Plane::Plane(const Vector3 &p_point1, const Vector3 &p_point2, const Vector3 &p_point3, ClockDirection p_dir) {
116
117 if (p_dir == CLOCKWISE)
118 normal = (p_point1 - p_point3).cross(p_point1 - p_point2);
119 else
120 normal = (p_point1 - p_point2).cross(p_point1 - p_point3);
121
122 normal.normalize();
123 d = normal.dot(p_point1);
124 }
125
126 bool Plane::operator==(const Plane &p_plane) const {
127
128 return normal == p_plane.normal && d == p_plane.d;
129 }
130
131 bool Plane::operator!=(const Plane &p_plane) const {
132
133 return normal != p_plane.normal || d != p_plane.d;
134 }
135
136 #endif // PLANE_H
137