2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* plane.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 21:01:57 +00:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#include "plane.h"
|
|
|
|
|
|
|
|
#include "math_funcs.h"
|
|
|
|
|
|
|
|
#define _PLANE_EQ_DOT_EPSILON 0.999
|
|
|
|
#define _PLANE_EQ_D_EPSILON 0.0001
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void Plane::set_normal(const Vector3 &p_normal) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
normal = p_normal;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Plane::normalize() {
|
|
|
|
|
|
|
|
real_t l = normal.length();
|
2017-03-05 15:44:50 +00:00
|
|
|
if (l == 0) {
|
|
|
|
*this = Plane(0, 0, 0, 0);
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
normal /= l;
|
|
|
|
d /= l;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Plane Plane::normalized() const {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
Plane p = *this;
|
|
|
|
p.normalize();
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector3 Plane::get_any_point() const {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return get_normal() * d;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector3 Plane::get_any_perpendicular_normal() const {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
static const Vector3 p1 = Vector3(1, 0, 0);
|
|
|
|
static const Vector3 p2 = Vector3(0, 1, 0);
|
2014-02-10 01:10:30 +00:00
|
|
|
Vector3 p;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (ABS(normal.dot(p1)) > 0.99) // if too similar to p1
|
2017-03-05 15:44:50 +00:00
|
|
|
p = p2; // use p2
|
2014-02-10 01:10:30 +00:00
|
|
|
else
|
2017-03-05 15:44:50 +00:00
|
|
|
p = p1; // use p1
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
p -= normal * normal.dot(p);
|
2014-02-10 01:10:30 +00:00
|
|
|
p.normalize();
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* intersections */
|
|
|
|
|
|
|
|
bool Plane::intersect_3(const Plane &p_plane1, const Plane &p_plane2, Vector3 *r_result) const {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
const Plane &p_plane0 = *this;
|
|
|
|
Vector3 normal0 = p_plane0.normal;
|
|
|
|
Vector3 normal1 = p_plane1.normal;
|
|
|
|
Vector3 normal2 = p_plane2.normal;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
real_t denom = vec3_cross(normal0, normal1).dot(normal2);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (ABS(denom) <= CMP_EPSILON)
|
2014-02-10 01:10:30 +00:00
|
|
|
return false;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (r_result) {
|
|
|
|
*r_result = ((vec3_cross(normal1, normal2) * p_plane0.d) +
|
|
|
|
(vec3_cross(normal2, normal0) * p_plane1.d) +
|
|
|
|
(vec3_cross(normal0, normal1) * p_plane2.d)) /
|
|
|
|
denom;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool Plane::intersects_ray(Vector3 p_from, Vector3 p_dir, Vector3 *p_intersection) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector3 segment = p_dir;
|
|
|
|
real_t den = normal.dot(segment);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
//printf("den is %i\n",den);
|
2017-03-05 15:44:50 +00:00
|
|
|
if (Math::abs(den) <= CMP_EPSILON) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
real_t dist = (normal.dot(p_from) - d) / den;
|
2014-02-10 01:10:30 +00:00
|
|
|
//printf("dist is %i\n",dist);
|
|
|
|
|
2017-03-24 20:45:31 +00:00
|
|
|
if (dist > CMP_EPSILON) { //this is a ray, before the emitting pos (p_from) doesn't exist
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
dist = -dist;
|
2014-02-10 01:10:30 +00:00
|
|
|
*p_intersection = p_from + segment * dist;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool Plane::intersects_segment(Vector3 p_begin, Vector3 p_end, Vector3 *p_intersection) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector3 segment = p_begin - p_end;
|
|
|
|
real_t den = normal.dot(segment);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
//printf("den is %i\n",den);
|
2017-03-05 15:44:50 +00:00
|
|
|
if (Math::abs(den) <= CMP_EPSILON) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
real_t dist = (normal.dot(p_begin) - d) / den;
|
2014-02-10 01:10:30 +00:00
|
|
|
//printf("dist is %i\n",dist);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (dist < -CMP_EPSILON || dist > (1.0 + CMP_EPSILON)) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
dist = -dist;
|
2014-02-10 01:10:30 +00:00
|
|
|
*p_intersection = p_begin + segment * dist;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* misc */
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool Plane::is_almost_like(const Plane &p_plane) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return (normal.dot(p_plane.normal) > _PLANE_EQ_DOT_EPSILON && Math::absd(d - p_plane.d) < _PLANE_EQ_D_EPSILON);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Plane::operator String() const {
|
|
|
|
|
|
|
|
return normal.operator String() + ", " + rtos(d);
|
|
|
|
}
|