1 | //************************************ bs::framework - Copyright 2018 Marko Pintera **************************************// |
2 | //*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********// |
3 | #pragma once |
4 | |
5 | #include "Prerequisites/BsPrerequisitesUtil.h" |
6 | #include "Math/BsVector3.h" |
7 | |
8 | namespace bs |
9 | { |
10 | /** @addtogroup Math |
11 | * @{ |
12 | */ |
13 | |
14 | /** A plane represented by a normal and a distance. */ |
15 | class BS_UTILITY_EXPORT Plane |
16 | { |
17 | public: |
18 | /** |
19 | * The "positive side" of the plane is the half space to which the plane normal points. The "negative side" is the |
20 | * other half space. The flag "no side" indicates the plane itself. |
21 | */ |
22 | enum Side |
23 | { |
24 | NO_SIDE, |
25 | POSITIVE_SIDE, |
26 | NEGATIVE_SIDE, |
27 | BOTH_SIDE |
28 | }; |
29 | |
30 | public: |
31 | Plane() = default; |
32 | Plane(const Plane& copy) = default; |
33 | Plane(const Vector3& normal, float d); |
34 | Plane(float a, float b, float c, float d); |
35 | Plane(const Vector3& normal, const Vector3& point); |
36 | Plane(const Vector3& point0, const Vector3& point1, const Vector3& point2); |
37 | |
38 | Plane& operator= (const Plane& rhs) = default; |
39 | |
40 | /** |
41 | * Returns the side of the plane where the point is located on. |
42 | * |
43 | * @note NO_SIDE signifies the point is on the plane. |
44 | */ |
45 | Side getSide(const Vector3& point, float epsilon = 0.0f) const; |
46 | |
47 | /** |
48 | * Returns the side where the alignedBox is. The flag BOTH_SIDE indicates an intersecting box. |
49 | * One corner ON the plane is sufficient to consider the box and the plane intersecting. |
50 | */ |
51 | Side getSide(const AABox& box) const; |
52 | |
53 | /** Returns the side where the sphere is. The flag BOTH_SIDE indicates an intersecting sphere. */ |
54 | Side getSide(const Sphere& sphere) const; |
55 | |
56 | /** |
57 | * Returns a distance from point to plane. |
58 | * |
59 | * @note The sign of the return value is positive if the point is on the |
60 | * positive side of the plane, negative if the point is on the negative |
61 | * side, and zero if the point is on the plane. |
62 | */ |
63 | float getDistance(const Vector3& point) const; |
64 | |
65 | /** Project a vector onto the plane. */ |
66 | Vector3 projectVector(const Vector3& v) const; |
67 | |
68 | /** Normalizes the plane's normal and the length scale of d. */ |
69 | float normalize(); |
70 | |
71 | /** Box/plane intersection. */ |
72 | bool intersects(const AABox& box) const; |
73 | |
74 | /** Sphere/plane intersection. */ |
75 | bool intersects(const Sphere& sphere) const; |
76 | |
77 | /** Ray/plane intersection, returns boolean result and distance to intersection point. */ |
78 | std::pair<bool, float> intersects(const Ray& ray) const; |
79 | |
80 | bool operator==(const Plane& rhs) const |
81 | { |
82 | return (rhs.d == d && rhs.normal == normal); |
83 | } |
84 | bool operator!=(const Plane& rhs) const |
85 | { |
86 | return (rhs.d != d || rhs.normal != normal); |
87 | } |
88 | |
89 | public: |
90 | Vector3 normal{BsZero}; |
91 | float d = 0.0f; |
92 | }; |
93 | |
94 | BS_ALLOW_MEMCPY_SERIALIZATION(Plane) |
95 | |
96 | /** @} */ |
97 | } |
98 | |