| 
									
										
										
										
											2023-01-05 13:25:55 +01:00
										 |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /*  aabb.cpp                                                              */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /*                         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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-16 21:09:00 -05:00
										 |  |  | #include "aabb.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-06 16:35:54 -05:00
										 |  |  | #include "core/string/ustring.h"
 | 
					
						
							| 
									
										
										
										
											2020-11-07 19:33:38 -03:00
										 |  |  | #include "core/variant/variant.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-04 21:01:59 -04:00
										 |  |  | real_t AABB::get_volume() const { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	return size.x * size.y * size.z; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-16 21:09:00 -05:00
										 |  |  | bool AABB::operator==(const AABB &p_rval) const { | 
					
						
							| 
									
										
										
										
											2017-06-06 20:33:51 +02:00
										 |  |  | 	return ((position == p_rval.position) && (size == p_rval.size)); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-16 21:09:00 -05:00
										 |  |  | bool AABB::operator!=(const AABB &p_rval) const { | 
					
						
							| 
									
										
										
										
											2017-06-06 20:33:51 +02:00
										 |  |  | 	return ((position != p_rval.position) || (size != p_rval.size)); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-16 21:09:00 -05:00
										 |  |  | void AABB::merge_with(const AABB &p_aabb) { | 
					
						
							| 
									
										
										
										
											2020-04-06 04:34:18 -04:00
										 |  |  | #ifdef MATH_CHECKS
 | 
					
						
							|  |  |  | 	if (unlikely(size.x < 0 || size.y < 0 || size.z < 0 || p_aabb.size.x < 0 || p_aabb.size.y < 0 || p_aabb.size.z < 0)) { | 
					
						
							|  |  |  | 		ERR_PRINT("AABB size is negative, this is not supported. Use AABB.abs() to get an AABB with a positive size."); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	Vector3 beg_1, beg_2; | 
					
						
							|  |  |  | 	Vector3 end_1, end_2; | 
					
						
							|  |  |  | 	Vector3 min, max; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-06 20:33:51 +02:00
										 |  |  | 	beg_1 = position; | 
					
						
							|  |  |  | 	beg_2 = p_aabb.position; | 
					
						
							| 
									
										
										
										
											2021-09-23 20:43:43 +05:45
										 |  |  | 	end_1 = size + beg_1; | 
					
						
							|  |  |  | 	end_2 = p_aabb.size + beg_2; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	min.x = (beg_1.x < beg_2.x) ? beg_1.x : beg_2.x; | 
					
						
							|  |  |  | 	min.y = (beg_1.y < beg_2.y) ? beg_1.y : beg_2.y; | 
					
						
							|  |  |  | 	min.z = (beg_1.z < beg_2.z) ? beg_1.z : beg_2.z; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	max.x = (end_1.x > end_2.x) ? end_1.x : end_2.x; | 
					
						
							|  |  |  | 	max.y = (end_1.y > end_2.y) ? end_1.y : end_2.y; | 
					
						
							|  |  |  | 	max.z = (end_1.z > end_2.z) ? end_1.z : end_2.z; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-06 20:33:51 +02:00
										 |  |  | 	position = min; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	size = max - min; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-14 16:33:45 -04:00
										 |  |  | bool AABB::is_equal_approx(const AABB &p_aabb) const { | 
					
						
							|  |  |  | 	return position.is_equal_approx(p_aabb.position) && size.is_equal_approx(p_aabb.size); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-11 16:12:27 +08:00
										 |  |  | bool AABB::is_finite() const { | 
					
						
							|  |  |  | 	return position.is_finite() && size.is_finite(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-16 21:09:00 -05:00
										 |  |  | AABB AABB::intersection(const AABB &p_aabb) const { | 
					
						
							| 
									
										
										
										
											2020-04-06 04:34:18 -04:00
										 |  |  | #ifdef MATH_CHECKS
 | 
					
						
							|  |  |  | 	if (unlikely(size.x < 0 || size.y < 0 || size.z < 0 || p_aabb.size.x < 0 || p_aabb.size.y < 0 || p_aabb.size.z < 0)) { | 
					
						
							|  |  |  | 		ERR_PRINT("AABB size is negative, this is not supported. Use AABB.abs() to get an AABB with a positive size."); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2017-06-06 20:33:51 +02:00
										 |  |  | 	Vector3 src_min = position; | 
					
						
							|  |  |  | 	Vector3 src_max = position + size; | 
					
						
							|  |  |  | 	Vector3 dst_min = p_aabb.position; | 
					
						
							|  |  |  | 	Vector3 dst_max = p_aabb.position + p_aabb.size; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	Vector3 min, max; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (src_min.x > dst_max.x || src_max.x < dst_min.x) { | 
					
						
							| 
									
										
										
										
											2017-11-16 21:09:00 -05:00
										 |  |  | 		return AABB(); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		min.x = (src_min.x > dst_min.x) ? src_min.x : dst_min.x; | 
					
						
							|  |  |  | 		max.x = (src_max.x < dst_max.x) ? src_max.x : dst_max.x; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (src_min.y > dst_max.y || src_max.y < dst_min.y) { | 
					
						
							| 
									
										
										
										
											2017-11-16 21:09:00 -05:00
										 |  |  | 		return AABB(); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		min.y = (src_min.y > dst_min.y) ? src_min.y : dst_min.y; | 
					
						
							|  |  |  | 		max.y = (src_max.y < dst_max.y) ? src_max.y : dst_max.y; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (src_min.z > dst_max.z || src_max.z < dst_min.z) { | 
					
						
							| 
									
										
										
										
											2017-11-16 21:09:00 -05:00
										 |  |  | 		return AABB(); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		min.z = (src_min.z > dst_min.z) ? src_min.z : dst_min.z; | 
					
						
							|  |  |  | 		max.z = (src_max.z < dst_max.z) ? src_max.z : dst_max.z; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-16 21:09:00 -05:00
										 |  |  | 	return AABB(min, max - min); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-16 21:09:00 -05:00
										 |  |  | bool AABB::intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *r_clip, Vector3 *r_normal) const { | 
					
						
							| 
									
										
										
										
											2020-04-06 04:34:18 -04:00
										 |  |  | #ifdef MATH_CHECKS
 | 
					
						
							|  |  |  | 	if (unlikely(size.x < 0 || size.y < 0 || size.z < 0)) { | 
					
						
							|  |  |  | 		ERR_PRINT("AABB size is negative, this is not supported. Use AABB.abs() to get an AABB with a positive size."); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	Vector3 c1, c2; | 
					
						
							| 
									
										
										
										
											2017-06-06 20:33:51 +02:00
										 |  |  | 	Vector3 end = position + size; | 
					
						
							| 
									
										
										
										
											2017-01-14 14:35:39 -06:00
										 |  |  | 	real_t near = -1e20; | 
					
						
							|  |  |  | 	real_t far = 1e20; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	int axis = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (int i = 0; i < 3; i++) { | 
					
						
							|  |  |  | 		if (p_dir[i] == 0) { | 
					
						
							| 
									
										
										
										
											2017-06-06 20:33:51 +02:00
										 |  |  | 			if ((p_from[i] < position[i]) || (p_from[i] > end[i])) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 				return false; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} else { // ray not parallel to planes in this direction
 | 
					
						
							| 
									
										
										
										
											2017-06-06 20:33:51 +02:00
										 |  |  | 			c1[i] = (position[i] - p_from[i]) / p_dir[i]; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 			c2[i] = (end[i] - p_from[i]) / p_dir[i]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (c1[i] > c2[i]) { | 
					
						
							|  |  |  | 				SWAP(c1, c2); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if (c1[i] > near) { | 
					
						
							|  |  |  | 				near = c1[i]; | 
					
						
							|  |  |  | 				axis = i; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if (c2[i] < far) { | 
					
						
							|  |  |  | 				far = c2[i]; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if ((near > far) || (far < 0)) { | 
					
						
							|  |  |  | 				return false; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (r_clip) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		*r_clip = c1; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	if (r_normal) { | 
					
						
							|  |  |  | 		*r_normal = Vector3(); | 
					
						
							|  |  |  | 		(*r_normal)[axis] = p_dir[axis] ? -1 : 1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-16 21:09:00 -05:00
										 |  |  | bool AABB::intersects_segment(const Vector3 &p_from, const Vector3 &p_to, Vector3 *r_clip, Vector3 *r_normal) const { | 
					
						
							| 
									
										
										
										
											2020-04-06 04:34:18 -04:00
										 |  |  | #ifdef MATH_CHECKS
 | 
					
						
							|  |  |  | 	if (unlikely(size.x < 0 || size.y < 0 || size.z < 0)) { | 
					
						
							|  |  |  | 		ERR_PRINT("AABB size is negative, this is not supported. Use AABB.abs() to get an AABB with a positive size."); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2017-01-11 00:52:51 -03:00
										 |  |  | 	real_t min = 0, max = 1; | 
					
						
							|  |  |  | 	int axis = 0; | 
					
						
							|  |  |  | 	real_t sign = 0; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	for (int i = 0; i < 3; i++) { | 
					
						
							|  |  |  | 		real_t seg_from = p_from[i]; | 
					
						
							|  |  |  | 		real_t seg_to = p_to[i]; | 
					
						
							| 
									
										
										
										
											2017-06-06 20:33:51 +02:00
										 |  |  | 		real_t box_begin = position[i]; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		real_t box_end = box_begin + size[i]; | 
					
						
							|  |  |  | 		real_t cmin, cmax; | 
					
						
							| 
									
										
										
										
											2017-01-14 14:35:39 -06:00
										 |  |  | 		real_t csign; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (seg_from < seg_to) { | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 			if (seg_from > box_end || seg_to < box_begin) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 				return false; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 			real_t length = seg_to - seg_from; | 
					
						
							|  |  |  | 			cmin = (seg_from < box_begin) ? ((box_begin - seg_from) / length) : 0; | 
					
						
							|  |  |  | 			cmax = (seg_to > box_end) ? ((box_end - seg_from) / length) : 1; | 
					
						
							|  |  |  | 			csign = -1.0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 			if (seg_to > box_end || seg_from < box_begin) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 				return false; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 			real_t length = seg_to - seg_from; | 
					
						
							|  |  |  | 			cmin = (seg_from > box_end) ? (box_end - seg_from) / length : 0; | 
					
						
							|  |  |  | 			cmax = (seg_to < box_begin) ? (box_begin - seg_from) / length : 1; | 
					
						
							|  |  |  | 			csign = 1.0; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (cmin > min) { | 
					
						
							|  |  |  | 			min = cmin; | 
					
						
							|  |  |  | 			axis = i; | 
					
						
							|  |  |  | 			sign = csign; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		if (cmax < max) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 			max = cmax; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		if (max < min) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 			return false; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Vector3 rel = p_to - p_from; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (r_normal) { | 
					
						
							|  |  |  | 		Vector3 normal; | 
					
						
							|  |  |  | 		normal[axis] = sign; | 
					
						
							|  |  |  | 		*r_normal = normal; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (r_clip) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		*r_clip = p_from + rel * min; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-16 21:09:00 -05:00
										 |  |  | bool AABB::intersects_plane(const Plane &p_plane) const { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	Vector3 points[8] = { | 
					
						
							| 
									
										
										
										
											2017-06-06 20:33:51 +02:00
										 |  |  | 		Vector3(position.x, position.y, position.z), | 
					
						
							|  |  |  | 		Vector3(position.x, position.y, position.z + size.z), | 
					
						
							|  |  |  | 		Vector3(position.x, position.y + size.y, position.z), | 
					
						
							|  |  |  | 		Vector3(position.x, position.y + size.y, position.z + size.z), | 
					
						
							|  |  |  | 		Vector3(position.x + size.x, position.y, position.z), | 
					
						
							|  |  |  | 		Vector3(position.x + size.x, position.y, position.z + size.z), | 
					
						
							|  |  |  | 		Vector3(position.x + size.x, position.y + size.y, position.z), | 
					
						
							|  |  |  | 		Vector3(position.x + size.x, position.y + size.y, position.z + size.z), | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bool over = false; | 
					
						
							|  |  |  | 	bool under = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (int i = 0; i < 8; i++) { | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		if (p_plane.distance_to(points[i]) > 0) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 			over = true; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 			under = true; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return under && over; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-16 21:09:00 -05:00
										 |  |  | Vector3 AABB::get_longest_axis() const { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	Vector3 axis(1, 0, 0); | 
					
						
							|  |  |  | 	real_t max_size = size.x; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (size.y > max_size) { | 
					
						
							|  |  |  | 		axis = Vector3(0, 1, 0); | 
					
						
							|  |  |  | 		max_size = size.y; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (size.z > max_size) { | 
					
						
							|  |  |  | 		axis = Vector3(0, 0, 1); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return axis; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-16 21:09:00 -05:00
										 |  |  | int AABB::get_longest_axis_index() const { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	int axis = 0; | 
					
						
							|  |  |  | 	real_t max_size = size.x; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (size.y > max_size) { | 
					
						
							|  |  |  | 		axis = 1; | 
					
						
							|  |  |  | 		max_size = size.y; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (size.z > max_size) { | 
					
						
							|  |  |  | 		axis = 2; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return axis; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-16 21:09:00 -05:00
										 |  |  | Vector3 AABB::get_shortest_axis() const { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	Vector3 axis(1, 0, 0); | 
					
						
							| 
									
										
										
										
											2021-12-19 11:51:14 +08:00
										 |  |  | 	real_t min_size = size.x; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-19 11:51:14 +08:00
										 |  |  | 	if (size.y < min_size) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		axis = Vector3(0, 1, 0); | 
					
						
							| 
									
										
										
										
											2021-12-19 11:51:14 +08:00
										 |  |  | 		min_size = size.y; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-19 11:51:14 +08:00
										 |  |  | 	if (size.z < min_size) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		axis = Vector3(0, 0, 1); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return axis; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-16 21:09:00 -05:00
										 |  |  | int AABB::get_shortest_axis_index() const { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	int axis = 0; | 
					
						
							| 
									
										
										
										
											2021-12-19 11:51:14 +08:00
										 |  |  | 	real_t min_size = size.x; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-19 11:51:14 +08:00
										 |  |  | 	if (size.y < min_size) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		axis = 1; | 
					
						
							| 
									
										
										
										
											2021-12-19 11:51:14 +08:00
										 |  |  | 		min_size = size.y; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-19 11:51:14 +08:00
										 |  |  | 	if (size.z < min_size) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		axis = 2; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return axis; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-16 21:09:00 -05:00
										 |  |  | AABB AABB::merge(const AABB &p_with) const { | 
					
						
							|  |  |  | 	AABB aabb = *this; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	aabb.merge_with(p_with); | 
					
						
							|  |  |  | 	return aabb; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-16 21:09:00 -05:00
										 |  |  | AABB AABB::expand(const Vector3 &p_vector) const { | 
					
						
							|  |  |  | 	AABB aabb = *this; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	aabb.expand_to(p_vector); | 
					
						
							|  |  |  | 	return aabb; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-14 14:29:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-16 21:09:00 -05:00
										 |  |  | AABB AABB::grow(real_t p_by) const { | 
					
						
							|  |  |  | 	AABB aabb = *this; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	aabb.grow_by(p_by); | 
					
						
							|  |  |  | 	return aabb; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-16 21:09:00 -05:00
										 |  |  | void AABB::get_edge(int p_edge, Vector3 &r_from, Vector3 &r_to) const { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	ERR_FAIL_INDEX(p_edge, 12); | 
					
						
							|  |  |  | 	switch (p_edge) { | 
					
						
							|  |  |  | 		case 0: { | 
					
						
							| 
									
										
										
										
											2017-06-06 20:33:51 +02:00
										 |  |  | 			r_from = Vector3(position.x + size.x, position.y, position.z); | 
					
						
							|  |  |  | 			r_to = Vector3(position.x, position.y, position.z); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		} break; | 
					
						
							|  |  |  | 		case 1: { | 
					
						
							| 
									
										
										
										
											2017-06-06 20:33:51 +02:00
										 |  |  | 			r_from = Vector3(position.x + size.x, position.y, position.z + size.z); | 
					
						
							|  |  |  | 			r_to = Vector3(position.x + size.x, position.y, position.z); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		} break; | 
					
						
							|  |  |  | 		case 2: { | 
					
						
							| 
									
										
										
										
											2017-06-06 20:33:51 +02:00
										 |  |  | 			r_from = Vector3(position.x, position.y, position.z + size.z); | 
					
						
							|  |  |  | 			r_to = Vector3(position.x + size.x, position.y, position.z + size.z); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		} break; | 
					
						
							|  |  |  | 		case 3: { | 
					
						
							| 
									
										
										
										
											2017-06-06 20:33:51 +02:00
										 |  |  | 			r_from = Vector3(position.x, position.y, position.z); | 
					
						
							|  |  |  | 			r_to = Vector3(position.x, position.y, position.z + size.z); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		} break; | 
					
						
							|  |  |  | 		case 4: { | 
					
						
							| 
									
										
										
										
											2017-06-06 20:33:51 +02:00
										 |  |  | 			r_from = Vector3(position.x, position.y + size.y, position.z); | 
					
						
							|  |  |  | 			r_to = Vector3(position.x + size.x, position.y + size.y, position.z); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		} break; | 
					
						
							|  |  |  | 		case 5: { | 
					
						
							| 
									
										
										
										
											2017-06-06 20:33:51 +02:00
										 |  |  | 			r_from = Vector3(position.x + size.x, position.y + size.y, position.z); | 
					
						
							|  |  |  | 			r_to = Vector3(position.x + size.x, position.y + size.y, position.z + size.z); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		} break; | 
					
						
							|  |  |  | 		case 6: { | 
					
						
							| 
									
										
										
										
											2017-06-06 20:33:51 +02:00
										 |  |  | 			r_from = Vector3(position.x + size.x, position.y + size.y, position.z + size.z); | 
					
						
							|  |  |  | 			r_to = Vector3(position.x, position.y + size.y, position.z + size.z); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		} break; | 
					
						
							|  |  |  | 		case 7: { | 
					
						
							| 
									
										
										
										
											2017-06-06 20:33:51 +02:00
										 |  |  | 			r_from = Vector3(position.x, position.y + size.y, position.z + size.z); | 
					
						
							|  |  |  | 			r_to = Vector3(position.x, position.y + size.y, position.z); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		} break; | 
					
						
							|  |  |  | 		case 8: { | 
					
						
							| 
									
										
										
										
											2017-06-06 20:33:51 +02:00
										 |  |  | 			r_from = Vector3(position.x, position.y, position.z + size.z); | 
					
						
							|  |  |  | 			r_to = Vector3(position.x, position.y + size.y, position.z + size.z); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		} break; | 
					
						
							|  |  |  | 		case 9: { | 
					
						
							| 
									
										
										
										
											2017-06-06 20:33:51 +02:00
										 |  |  | 			r_from = Vector3(position.x, position.y, position.z); | 
					
						
							|  |  |  | 			r_to = Vector3(position.x, position.y + size.y, position.z); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		} break; | 
					
						
							|  |  |  | 		case 10: { | 
					
						
							| 
									
										
										
										
											2017-06-06 20:33:51 +02:00
										 |  |  | 			r_from = Vector3(position.x + size.x, position.y, position.z); | 
					
						
							|  |  |  | 			r_to = Vector3(position.x + size.x, position.y + size.y, position.z); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		} break; | 
					
						
							|  |  |  | 		case 11: { | 
					
						
							| 
									
										
										
										
											2017-06-06 20:33:51 +02:00
										 |  |  | 			r_from = Vector3(position.x + size.x, position.y, position.z + size.z); | 
					
						
							|  |  |  | 			r_to = Vector3(position.x + size.x, position.y + size.y, position.z + size.z); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		} break; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-13 15:59:37 -03:00
										 |  |  | Variant AABB::intersects_segment_bind(const Vector3 &p_from, const Vector3 &p_to) const { | 
					
						
							|  |  |  | 	Vector3 inters; | 
					
						
							|  |  |  | 	if (intersects_segment(p_from, p_to, &inters)) { | 
					
						
							|  |  |  | 		return inters; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return Variant(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-10-05 22:00:15 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-13 15:59:37 -03:00
										 |  |  | Variant AABB::intersects_ray_bind(const Vector3 &p_from, const Vector3 &p_dir) const { | 
					
						
							|  |  |  | 	Vector3 inters; | 
					
						
							|  |  |  | 	if (intersects_ray(p_from, p_dir, &inters)) { | 
					
						
							|  |  |  | 		return inters; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return Variant(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-16 21:09:00 -05:00
										 |  |  | AABB::operator String() const { | 
					
						
							| 
									
										
										
										
											2021-02-25 09:54:50 -05:00
										 |  |  | 	return "[P: " + position.operator String() + ", S: " + size + "]"; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | } |