Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/math/plane.cpp
9903 views
1
/**************************************************************************/
2
/* plane.cpp */
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
#include "plane.h"
32
33
#include "core/math/math_funcs.h"
34
#include "core/variant/variant.h"
35
36
void Plane::set_normal(const Vector3 &p_normal) {
37
normal = p_normal;
38
}
39
40
void Plane::normalize() {
41
real_t l = normal.length();
42
if (l == 0) {
43
*this = Plane(0, 0, 0, 0);
44
return;
45
}
46
normal /= l;
47
d /= l;
48
}
49
50
Plane Plane::normalized() const {
51
Plane p = *this;
52
p.normalize();
53
return p;
54
}
55
56
Vector3 Plane::get_any_perpendicular_normal() const {
57
static const Vector3 p1 = Vector3(1, 0, 0);
58
static const Vector3 p2 = Vector3(0, 1, 0);
59
Vector3 p;
60
61
if (Math::abs(normal.dot(p1)) > 0.99f) { // if too similar to p1
62
p = p2; // use p2
63
} else {
64
p = p1; // use p1
65
}
66
67
p -= normal * normal.dot(p);
68
p.normalize();
69
70
return p;
71
}
72
73
/* intersections */
74
75
bool Plane::intersect_3(const Plane &p_plane1, const Plane &p_plane2, Vector3 *r_result) const {
76
const Plane &p_plane0 = *this;
77
Vector3 normal0 = p_plane0.normal;
78
Vector3 normal1 = p_plane1.normal;
79
Vector3 normal2 = p_plane2.normal;
80
81
real_t denom = vec3_cross(normal0, normal1).dot(normal2);
82
83
if (Math::is_zero_approx(denom)) {
84
return false;
85
}
86
87
if (r_result) {
88
*r_result = ((vec3_cross(normal1, normal2) * p_plane0.d) +
89
(vec3_cross(normal2, normal0) * p_plane1.d) +
90
(vec3_cross(normal0, normal1) * p_plane2.d)) /
91
denom;
92
}
93
94
return true;
95
}
96
97
bool Plane::intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *p_intersection) const {
98
Vector3 segment = p_dir;
99
real_t den = normal.dot(segment);
100
101
if (Math::is_zero_approx(den)) {
102
return false;
103
}
104
105
real_t dist = (normal.dot(p_from) - d) / den;
106
107
if (dist > (real_t)CMP_EPSILON) { //this is a ray, before the emitting pos (p_from) doesn't exist
108
109
return false;
110
}
111
112
dist = -dist;
113
*p_intersection = p_from + segment * dist;
114
115
return true;
116
}
117
118
bool Plane::intersects_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 *p_intersection) const {
119
Vector3 segment = p_begin - p_end;
120
real_t den = normal.dot(segment);
121
122
if (Math::is_zero_approx(den)) {
123
return false;
124
}
125
126
real_t dist = (normal.dot(p_begin) - d) / den;
127
128
if (dist < (real_t)-CMP_EPSILON || dist > (1.0f + (real_t)CMP_EPSILON)) {
129
return false;
130
}
131
132
dist = -dist;
133
*p_intersection = p_begin + segment * dist;
134
135
return true;
136
}
137
138
Variant Plane::intersect_3_bind(const Plane &p_plane1, const Plane &p_plane2) const {
139
Vector3 inters;
140
if (intersect_3(p_plane1, p_plane2, &inters)) {
141
return inters;
142
} else {
143
return Variant();
144
}
145
}
146
147
Variant Plane::intersects_ray_bind(const Vector3 &p_from, const Vector3 &p_dir) const {
148
Vector3 inters;
149
if (intersects_ray(p_from, p_dir, &inters)) {
150
return inters;
151
} else {
152
return Variant();
153
}
154
}
155
156
Variant Plane::intersects_segment_bind(const Vector3 &p_begin, const Vector3 &p_end) const {
157
Vector3 inters;
158
if (intersects_segment(p_begin, p_end, &inters)) {
159
return inters;
160
} else {
161
return Variant();
162
}
163
}
164
165
/* misc */
166
167
bool Plane::is_equal_approx_any_side(const Plane &p_plane) const {
168
return (normal.is_equal_approx(p_plane.normal) && Math::is_equal_approx(d, p_plane.d)) || (normal.is_equal_approx(-p_plane.normal) && Math::is_equal_approx(d, -p_plane.d));
169
}
170
171
bool Plane::is_equal_approx(const Plane &p_plane) const {
172
return normal.is_equal_approx(p_plane.normal) && Math::is_equal_approx(d, p_plane.d);
173
}
174
175
bool Plane::is_same(const Plane &p_plane) const {
176
return normal.is_same(p_plane.normal) && Math::is_same(d, p_plane.d);
177
}
178
179
bool Plane::is_finite() const {
180
return normal.is_finite() && Math::is_finite(d);
181
}
182
183
Plane::operator String() const {
184
return "[N: " + normal.operator String() + ", D: " + String::num_real(d, false) + "]";
185
}
186
187