godot/core/math/plane.h

136 lines
5.1 KiB
C++
Raw Normal View History

2014-02-10 09:10:30 +08:00
/*************************************************************************/
/* plane.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
2014-02-10 09:10:30 +08:00
/*************************************************************************/
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
2014-02-10 09:10:30 +08: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. */
/*************************************************************************/
2014-02-10 09:10:30 +08:00
#ifndef PLANE_H
#define PLANE_H
#include "core/math/vector3.h"
2014-02-10 09:10:30 +08:00
class Plane {
public:
Vector3 normal;
real_t d;
void set_normal(const Vector3 &p_normal);
2014-02-10 09:10:30 +08:00
_FORCE_INLINE_ Vector3 get_normal() const { return normal; }; ///Point is coplanar, CMP_EPSILON for precision
void normalize();
Plane normalized() const;
/* Plane-Point operations */
2014-02-10 09:10:30 +08:00
_FORCE_INLINE_ Vector3 center() const { return normal * d; }
2014-02-10 09:10:30 +08:00
Vector3 get_any_point() const;
Vector3 get_any_perpendicular_normal() const;
_FORCE_INLINE_ bool is_point_over(const Vector3 &p_point) const; ///< Point is over plane
_FORCE_INLINE_ real_t distance_to(const Vector3 &p_point) const;
_FORCE_INLINE_ bool has_point(const Vector3 &p_point, real_t _epsilon = CMP_EPSILON) const;
2014-02-10 09:10:30 +08:00
/* intersections */
2014-02-10 09:10:30 +08:00
bool intersect_3(const Plane &p_plane1, const Plane &p_plane2, Vector3 *r_result = 0) const;
2017-08-12 19:04:30 +08:00
bool intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *p_intersection) const;
bool intersects_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 *p_intersection) const;
2014-02-10 09:10:30 +08:00
_FORCE_INLINE_ Vector3 project(const Vector3 &p_point) const {
2016-03-09 07:00:52 +08:00
2014-02-10 09:10:30 +08:00
return p_point - normal * distance_to(p_point);
2016-03-09 07:00:52 +08:00
}
2014-02-10 09:10:30 +08:00
/* misc */
2014-02-10 09:10:30 +08:00
Plane operator-() const { return Plane(-normal, -d); }
bool is_almost_like(const Plane &p_plane) const;
2014-02-10 09:10:30 +08:00
_FORCE_INLINE_ bool operator==(const Plane &p_plane) const;
_FORCE_INLINE_ bool operator!=(const Plane &p_plane) const;
2014-02-10 09:10:30 +08:00
operator String() const;
_FORCE_INLINE_ Plane() { d = 0; }
_FORCE_INLINE_ Plane(real_t p_a, real_t p_b, real_t p_c, real_t p_d) :
normal(p_a, p_b, p_c),
d(p_d){};
2014-02-10 09:10:30 +08:00
_FORCE_INLINE_ Plane(const Vector3 &p_normal, real_t p_d);
_FORCE_INLINE_ Plane(const Vector3 &p_point, const Vector3 &p_normal);
_FORCE_INLINE_ Plane(const Vector3 &p_point1, const Vector3 &p_point2, const Vector3 &p_point3, ClockDirection p_dir = CLOCKWISE);
2014-02-10 09:10:30 +08:00
};
bool Plane::is_point_over(const Vector3 &p_point) const {
return (normal.dot(p_point) > d);
}
real_t Plane::distance_to(const Vector3 &p_point) const {
return (normal.dot(p_point) - d);
2014-02-10 09:10:30 +08:00
}
bool Plane::has_point(const Vector3 &p_point, real_t _epsilon) const {
2014-02-10 09:10:30 +08:00
real_t dist = normal.dot(p_point) - d;
dist = ABS(dist);
return (dist <= _epsilon);
2014-02-10 09:10:30 +08:00
}
Plane::Plane(const Vector3 &p_normal, real_t p_d) :
normal(p_normal),
d(p_d) {
2014-02-10 09:10:30 +08:00
}
Plane::Plane(const Vector3 &p_point, const Vector3 &p_normal) :
normal(p_normal),
d(p_normal.dot(p_point)) {
2014-02-10 09:10:30 +08:00
}
Plane::Plane(const Vector3 &p_point1, const Vector3 &p_point2, const Vector3 &p_point3, ClockDirection p_dir) {
2014-02-10 09:10:30 +08:00
if (p_dir == CLOCKWISE)
normal = (p_point1 - p_point3).cross(p_point1 - p_point2);
2014-02-10 09:10:30 +08:00
else
normal = (p_point1 - p_point2).cross(p_point1 - p_point3);
2014-02-10 09:10:30 +08:00
normal.normalize();
d = normal.dot(p_point1);
}
bool Plane::operator==(const Plane &p_plane) const {
2014-02-10 09:10:30 +08:00
return normal == p_plane.normal && d == p_plane.d;
2014-02-10 09:10:30 +08:00
}
bool Plane::operator!=(const Plane &p_plane) const {
2014-02-10 09:10:30 +08:00
return normal != p_plane.normal || d != p_plane.d;
2014-02-10 09:10:30 +08:00
}
#endif // PLANE_H