Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
Plane.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018 DIVIDE-Studio
3 Copyright (c) 2009 Ionut Cava
4
5 This file is part of DIVIDE Framework.
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software
9 and associated documentation files (the "Software"), to deal in the Software
10 without restriction,
11 including without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense,
13 and/or sell copies of the Software, and to permit persons to whom the
14 Software is furnished to do so,
15 subject to the following conditions:
16
17 The above copyright notice and this permission notice shall be included in
18 all copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED,
22 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23 PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 DAMAGES OR OTHER LIABILITY,
26 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
27 IN CONNECTION WITH THE SOFTWARE
28 OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 */
31
32// This file is based on material originally from:
33// Geometric Tools, LLC
34// Copyright (c) 1998-2010
35// Distributed under the Boost Software License, Version 1.0.
36// http://www.boost.org/LICENSE_1_0.txt
37// http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
38// This code is HEAVILY inspired by Ogre3D and Torque3D
39
40
41#pragma once
42#ifndef DVD_CORE_MATH_PLANE_H_
43#define DVD_CORE_MATH_PLANE_H_
44
45#include "MathUtil.h"
46
47namespace Divide {
48
55template <typename T>
56class Plane {
57 public:
63 enum class Side : I8 {
64 NO_SIDE,
67 };
68
69 Plane(const Plane& rhs) noexcept
70 : _normal(rhs._normal), _distance(rhs._distance)
71 {
72 }
73
75 Plane(const vec3<T>& normal, T distance) noexcept
76 : _normal(normal), _distance(distance)
77 {
78 }
79
81 Plane(const T a, const T b, const T c, const T distance) noexcept
82 : _normal(a, b, c), _distance(distance)
83 {
84 }
85
86 Plane(const vec4<T>& plane) noexcept
87 : _normal(plane.xyz()), _distance(plane.w)
88 {
89 }
90
91 Plane(const vec3<T>& normal, const vec3<T>& point)
92 {
93 redefine(normal, point);
94 }
95
96 Plane(const vec3<T>& point0, const vec3<T>& point1, const vec3<T>& point2)
97 {
98 redefine(point0, point1, point2);
99 }
100
101 Plane() noexcept
102 : Plane(0, 0, 0, 0)
103 {
104 }
105
106 Plane& operator=(const Plane& other) noexcept {
107 _normal.set(other._normal);
108 _distance = other._distance;
109
110 return *this;
111 }
112
113 [[nodiscard]] Side classifyPoint(const vec3<F32>& point) const noexcept {
114 const F32 result = signedDistanceToPoint(point);
115 return result > 0 ? Side::POSITIVE_SIDE
116 : result < 0 ? Side::NEGATIVE_SIDE : Side::NO_SIDE;
117 }
118
119 [[nodiscard]] T signedDistanceToPoint(const vec3<T>& point) const noexcept {
120 return _normal.dot(point) + _distance;
121 }
122
123 [[nodiscard]] vec3<T> closestPointOnPlaneToPoint(const vec3<T>& point) const {
124 return (point - signedDistanceToPoint(point)) * _normal;
125 }
126
127 void set(const vec4<T>& equation) noexcept {
128 set(equation.xyz, equation.w);
129 }
130
131 void set(const vec3<T>& normal, T distance) noexcept {
132 _normal = normal;
133 _distance = distance;
134 }
135
136 void set(T a, T b, T c, T distance) noexcept {
137 set(vec3<T>(a, b, c), distance);
138 }
139
140 void redefine(const vec3<T>& point0, const vec3<T>& point1, const vec3<T>& point2) {
141 const vec3<T> edge1 = point1 - point0;
142 const vec3<T> edge2 = point2 - point0;
143 _normal = edge1.cross(edge2);
144 _normal.normalize();
145 _distance = _normal.dot(point0);
146 }
147
148 void redefine(const vec3<T>& normal, const vec3<T>& point) {
149 _normal = normal;
150 _distance = _normal.dot(point);
151 }
152
154 bool operator==(const Plane& rhs) const noexcept {
155 return COMPARE(rhs._distance, _distance) && rhs._normal == _normal;
156 }
157
158 bool operator!=(const Plane& rhs) const noexcept {
159 return !COMPARE(rhs._distance, _distance) || rhs._normal != _normal;
160 }
161
162 bool compare(const Plane& rhs, F32 epsilon) {
163 return COMPARE_TOLERANCE(rhs._distance, _distance, epsilon) && rhs._normal.compare(_normal, epsilon);
164 }
165
166 T normalize() noexcept {
167 T length = _normal.length();
168 if (length > static_cast<T>(0))
169 {
170 const F32 invLength = 1.f / length;
171 _normal *= invLength;
172 _distance *= invLength;
173 }
174
175 return length;
176 }
177
178 union {
179 struct {
182 };
183
185 };
186};
187
188template<size_t N>
189using PlaneList = std::array<Plane<F32>, N>;
191
192template<typename T>
193FORCE_INLINE vec3<T> GetIntersection(const Plane<T>& a, const Plane<T>& b, const Plane<T>& c) noexcept {
194 const T denom = Dot(Cross(a._normal, b._normal), c._normal);
195 assert(!IS_ZERO(denom));
196 return (-(a._distance * Cross(b._normal, c._normal)) -
197 b._distance * Cross(c._normal, a._normal) -
198 c._distance * Cross(a._normal, b._normal)) / denom;
199}
200} // namespace Divide
201
202#endif //DVD_CORE_MATH_PLANE_H_
#define FORCE_INLINE
Plane() noexcept
Definition: Plane.h:101
bool compare(const Plane &rhs, F32 epsilon)
Definition: Plane.h:162
T normalize() noexcept
Definition: Plane.h:166
Plane(const vec3< T > &normal, T distance) noexcept
distance is stored as the negative of the specified value
Definition: Plane.h:75
bool operator!=(const Plane &rhs) const noexcept
Definition: Plane.h:158
void set(T a, T b, T c, T distance) noexcept
Definition: Plane.h:136
Plane & operator=(const Plane &other) noexcept
Definition: Plane.h:106
Plane(const vec3< T > &point0, const vec3< T > &point1, const vec3< T > &point2)
Definition: Plane.h:96
void set(const vec4< T > &equation) noexcept
Definition: Plane.h:127
vec3< T > _normal
Definition: Plane.h:180
vec3< T > closestPointOnPlaneToPoint(const vec3< T > &point) const
Definition: Plane.h:123
Plane(const Plane &rhs) noexcept
Definition: Plane.h:69
Plane(const T a, const T b, const T c, const T distance) noexcept
distance is stored as the negative of the specified value
Definition: Plane.h:81
Plane(const vec3< T > &normal, const vec3< T > &point)
Definition: Plane.h:91
Plane(const vec4< T > &plane) noexcept
Definition: Plane.h:86
void redefine(const vec3< T > &point0, const vec3< T > &point1, const vec3< T > &point2)
Definition: Plane.h:140
bool operator==(const Plane &rhs) const noexcept
Comparison operator.
Definition: Plane.h:154
vec4< T > _equation
Definition: Plane.h:184
T signedDistanceToPoint(const vec3< T > &point) const noexcept
Definition: Plane.h:119
Side classifyPoint(const vec3< F32 > &point) const noexcept
Definition: Plane.h:113
void set(const vec3< T > &normal, T distance) noexcept
Definition: Plane.h:131
void redefine(const vec3< T > &normal, const vec3< T > &point)
Definition: Plane.h:148
void cross(const vec3 &v1, const vec3 &v2) noexcept
set this vector to be equal to the cross of the 2 specified vectors
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
std::array< Plane< F32 >, N > PlaneList
Definition: Plane.h:189
bool IS_ZERO(const T X) noexcept
FORCE_INLINE vec3< T > GetIntersection(const Plane< T > &a, const Plane< T > &b, const Plane< T > &c) noexcept
Definition: Plane.h:193
T Dot(vec2< T > a, vec2< T > b) noexcept
general vec2 dot product
eastl::vector< Type > vector
Definition: Vector.h:42
bool COMPARE_TOLERANCE(const T X, const U Y, const T TOLERANCE) noexcept
vector< Plane< F32 > > PlaneDynamicList
Definition: Plane.h:190
vec2< T > Cross(vec2< T > v1, vec2< T > v2) noexcept
general vec2 cross function
Definition: MathVectors.inl:80
bool COMPARE(T X, U Y) noexcept