| 
									
										
										
										
											2023-01-05 13:25:55 +01:00
										 |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /*  plane.h                                                               */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /*                         This file is part of:                          */ | 
					
						
							|  |  |  | /*                             GODOT ENGINE                               */ | 
					
						
							|  |  |  | /*                        https://godotengine.org                         */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | 
					
						
							|  |  |  | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */ | 
					
						
							|  |  |  | /*                                                                        */ | 
					
						
							|  |  |  | /* 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.                 */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							| 
									
										
										
										
											2018-01-05 00:50:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | #ifndef PLANE_H
 | 
					
						
							|  |  |  | #define PLANE_H
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 18:13:45 +02:00
										 |  |  | #include "core/math/vector3.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-13 15:59:37 -03:00
										 |  |  | class Variant; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-04 14:30:17 +01:00
										 |  |  | struct _NO_DISCARD_ Plane { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	Vector3 normal; | 
					
						
							| 
									
										
										
										
											2020-05-12 17:01:17 +02:00
										 |  |  | 	real_t d = 0; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	void set_normal(const Vector3 &p_normal); | 
					
						
							| 
									
										
										
										
											2022-02-04 14:30:17 +01:00
										 |  |  | 	_FORCE_INLINE_ Vector3 get_normal() const { return normal; }; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	void normalize(); | 
					
						
							|  |  |  | 	Plane normalized() const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Plane-Point operations */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-15 16:51:43 +01:00
										 |  |  | 	_FORCE_INLINE_ Vector3 get_center() const { return normal * d; } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	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; | 
					
						
							| 
									
										
										
										
											2022-07-21 19:34:09 +03:00
										 |  |  | 	_FORCE_INLINE_ bool has_point(const Vector3 &p_point, real_t p_tolerance = CMP_EPSILON) const; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* intersections */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 10:15:48 +02:00
										 |  |  | 	bool intersect_3(const Plane &p_plane1, const Plane &p_plane2, Vector3 *r_result = nullptr) const; | 
					
						
							| 
									
										
										
										
											2017-08-12 07:04:30 -04: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-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-13 15:59:37 -03:00
										 |  |  | 	// For Variant bindings.
 | 
					
						
							|  |  |  | 	Variant intersect_3_bind(const Plane &p_plane1, const Plane &p_plane2) const; | 
					
						
							|  |  |  | 	Variant intersects_ray_bind(const Vector3 &p_from, const Vector3 &p_dir) const; | 
					
						
							|  |  |  | 	Variant intersects_segment_bind(const Vector3 &p_begin, const Vector3 &p_end) const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	_FORCE_INLINE_ Vector3 project(const Vector3 &p_point) const { | 
					
						
							|  |  |  | 		return p_point - normal * distance_to(p_point); | 
					
						
							| 
									
										
										
										
											2016-03-09 00:00:52 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* misc */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-10 16:47:11 +02:00
										 |  |  | 	Plane operator-() const { return Plane(-normal, -d); } | 
					
						
							| 
									
										
										
										
											2019-10-14 16:33:45 -04:00
										 |  |  | 	bool is_equal_approx(const Plane &p_plane) const; | 
					
						
							| 
									
										
										
										
											2020-05-01 09:34:23 -03:00
										 |  |  | 	bool is_equal_approx_any_side(const Plane &p_plane) const; | 
					
						
							| 
									
										
										
										
											2022-08-11 16:12:27 +08:00
										 |  |  | 	bool is_finite() const; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	_FORCE_INLINE_ bool operator==(const Plane &p_plane) const; | 
					
						
							|  |  |  | 	_FORCE_INLINE_ bool operator!=(const Plane &p_plane) const; | 
					
						
							|  |  |  | 	operator String() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-12 17:01:17 +02:00
										 |  |  | 	_FORCE_INLINE_ Plane() {} | 
					
						
							| 
									
										
										
										
											2017-12-06 21:36:34 +01:00
										 |  |  | 	_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), | 
					
						
							| 
									
										
										
										
											2020-05-10 16:47:11 +02:00
										 |  |  | 			d(p_d) {} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-08 16:26:14 +02:00
										 |  |  | 	_FORCE_INLINE_ Plane(const Vector3 &p_normal, real_t p_d = 0.0); | 
					
						
							|  |  |  | 	_FORCE_INLINE_ Plane(const Vector3 &p_normal, const Vector3 &p_point); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	_FORCE_INLINE_ Plane(const Vector3 &p_point1, const Vector3 &p_point2, const Vector3 &p_point3, ClockDirection p_dir = CLOCKWISE); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool Plane::is_point_over(const Vector3 &p_point) const { | 
					
						
							| 
									
										
										
										
											2020-05-10 16:47:11 +02:00
										 |  |  | 	return (normal.dot(p_point) > d); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | real_t Plane::distance_to(const Vector3 &p_point) const { | 
					
						
							| 
									
										
										
										
											2020-05-10 16:47:11 +02:00
										 |  |  | 	return (normal.dot(p_point) - d); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-21 19:34:09 +03:00
										 |  |  | bool Plane::has_point(const Vector3 &p_point, real_t p_tolerance) const { | 
					
						
							| 
									
										
										
										
											2020-05-10 16:47:11 +02:00
										 |  |  | 	real_t dist = normal.dot(p_point) - d; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	dist = ABS(dist); | 
					
						
							| 
									
										
										
										
											2022-07-21 19:34:09 +03:00
										 |  |  | 	return (dist <= p_tolerance); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-10 16:47:11 +02:00
										 |  |  | Plane::Plane(const Vector3 &p_normal, real_t p_d) : | 
					
						
							| 
									
										
										
										
											2017-12-06 21:36:34 +01:00
										 |  |  | 		normal(p_normal), | 
					
						
							| 
									
										
										
										
											2020-05-10 16:47:11 +02:00
										 |  |  | 		d(p_d) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-08 16:26:14 +02:00
										 |  |  | Plane::Plane(const Vector3 &p_normal, const Vector3 &p_point) : | 
					
						
							| 
									
										
										
										
											2017-12-06 21:36:34 +01:00
										 |  |  | 		normal(p_normal), | 
					
						
							| 
									
										
										
										
											2020-05-10 16:47:11 +02:00
										 |  |  | 		d(p_normal.dot(p_point)) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Plane::Plane(const Vector3 &p_point1, const Vector3 &p_point2, const Vector3 &p_point3, ClockDirection p_dir) { | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (p_dir == CLOCKWISE) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		normal = (p_point1 - p_point3).cross(p_point1 - p_point2); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		normal = (p_point1 - p_point2).cross(p_point1 - p_point3); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	normal.normalize(); | 
					
						
							| 
									
										
										
										
											2020-05-10 16:47:11 +02:00
										 |  |  | 	d = normal.dot(p_point1); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool Plane::operator==(const Plane &p_plane) const { | 
					
						
							| 
									
										
										
										
											2020-05-10 16:47:11 +02:00
										 |  |  | 	return normal == p_plane.normal && d == p_plane.d; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool Plane::operator!=(const Plane &p_plane) const { | 
					
						
							| 
									
										
										
										
											2020-05-10 16:47:11 +02:00
										 |  |  | 	return normal != p_plane.normal || d != p_plane.d; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif // PLANE_H
 |