2018-08-11 06:40:44 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* rect2.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2022-01-03 21:27:34 +01:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2018-08-11 06:40:44 +02: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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
2022-02-04 13:28:02 +01:00
|
|
|
#include "rect2.h"
|
|
|
|
|
|
|
|
#include "core/math/rect2i.h"
|
|
|
|
#include "core/math/transform_2d.h"
|
|
|
|
#include "core/string/ustring.h"
|
2018-08-11 06:40:44 +02:00
|
|
|
|
2019-10-14 22:33:45 +02:00
|
|
|
bool Rect2::is_equal_approx(const Rect2 &p_rect) const {
|
|
|
|
return position.is_equal_approx(p_rect.position) && size.is_equal_approx(p_rect.size);
|
|
|
|
}
|
|
|
|
|
2022-08-11 10:12:27 +02:00
|
|
|
bool Rect2::is_finite() const {
|
|
|
|
return position.is_finite() && size.is_finite();
|
|
|
|
}
|
|
|
|
|
2018-08-11 06:40:44 +02:00
|
|
|
bool Rect2::intersects_segment(const Point2 &p_from, const Point2 &p_to, Point2 *r_pos, Point2 *r_normal) const {
|
2020-04-06 10:34:18 +02:00
|
|
|
#ifdef MATH_CHECKS
|
|
|
|
if (unlikely(size.x < 0 || size.y < 0)) {
|
|
|
|
ERR_PRINT("Rect2 size is negative, this is not supported. Use Rect2.abs() to get a Rect2 with a positive size.");
|
|
|
|
}
|
|
|
|
#endif
|
2018-08-11 06:40:44 +02:00
|
|
|
real_t min = 0, max = 1;
|
|
|
|
int axis = 0;
|
|
|
|
real_t sign = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
real_t seg_from = p_from[i];
|
|
|
|
real_t seg_to = p_to[i];
|
|
|
|
real_t box_begin = position[i];
|
|
|
|
real_t box_end = box_begin + size[i];
|
|
|
|
real_t cmin, cmax;
|
|
|
|
real_t csign;
|
|
|
|
|
|
|
|
if (seg_from < seg_to) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (seg_from > box_end || seg_to < box_begin) {
|
2018-08-11 06:40:44 +02:00
|
|
|
return false;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-08-11 06:40:44 +02: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) {
|
2018-08-11 06:40:44 +02:00
|
|
|
return false;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-08-11 06:40:44 +02: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) {
|
2018-08-11 06:40:44 +02:00
|
|
|
max = cmax;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (max < min) {
|
2018-08-11 06:40:44 +02:00
|
|
|
return false;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-08-11 06:40:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector2 rel = p_to - p_from;
|
|
|
|
|
|
|
|
if (r_normal) {
|
|
|
|
Vector2 normal;
|
|
|
|
normal[axis] = sign;
|
|
|
|
*r_normal = normal;
|
|
|
|
}
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (r_pos) {
|
2018-08-11 06:40:44 +02:00
|
|
|
*r_pos = p_from + rel * min;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-08-11 06:40:44 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Rect2::intersects_transformed(const Transform2D &p_xform, const Rect2 &p_rect) const {
|
2020-04-06 10:34:18 +02:00
|
|
|
#ifdef MATH_CHECKS
|
|
|
|
if (unlikely(size.x < 0 || size.y < 0 || p_rect.size.x < 0 || p_rect.size.y < 0)) {
|
|
|
|
ERR_PRINT("Rect2 size is negative, this is not supported. Use Rect2.abs() to get a Rect2 with a positive size.");
|
|
|
|
}
|
|
|
|
#endif
|
2018-08-11 06:40:44 +02:00
|
|
|
//SAT intersection between local and transformed rect2
|
|
|
|
|
|
|
|
Vector2 xf_points[4] = {
|
|
|
|
p_xform.xform(p_rect.position),
|
|
|
|
p_xform.xform(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y)),
|
|
|
|
p_xform.xform(Vector2(p_rect.position.x, p_rect.position.y + p_rect.size.y)),
|
|
|
|
p_xform.xform(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y + p_rect.size.y)),
|
|
|
|
};
|
|
|
|
|
|
|
|
real_t low_limit;
|
|
|
|
|
|
|
|
//base rect2 first (faster)
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (xf_points[0].y > position.y) {
|
2018-08-11 06:40:44 +02:00
|
|
|
goto next1;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (xf_points[1].y > position.y) {
|
2018-08-11 06:40:44 +02:00
|
|
|
goto next1;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (xf_points[2].y > position.y) {
|
2018-08-11 06:40:44 +02:00
|
|
|
goto next1;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (xf_points[3].y > position.y) {
|
2018-08-11 06:40:44 +02:00
|
|
|
goto next1;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-08-11 06:40:44 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
next1:
|
|
|
|
|
|
|
|
low_limit = position.y + size.y;
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (xf_points[0].y < low_limit) {
|
2018-08-11 06:40:44 +02:00
|
|
|
goto next2;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (xf_points[1].y < low_limit) {
|
2018-08-11 06:40:44 +02:00
|
|
|
goto next2;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (xf_points[2].y < low_limit) {
|
2018-08-11 06:40:44 +02:00
|
|
|
goto next2;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (xf_points[3].y < low_limit) {
|
2018-08-11 06:40:44 +02:00
|
|
|
goto next2;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-08-11 06:40:44 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
next2:
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (xf_points[0].x > position.x) {
|
2018-08-11 06:40:44 +02:00
|
|
|
goto next3;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (xf_points[1].x > position.x) {
|
2018-08-11 06:40:44 +02:00
|
|
|
goto next3;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (xf_points[2].x > position.x) {
|
2018-08-11 06:40:44 +02:00
|
|
|
goto next3;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (xf_points[3].x > position.x) {
|
2018-08-11 06:40:44 +02:00
|
|
|
goto next3;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-08-11 06:40:44 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
next3:
|
|
|
|
|
|
|
|
low_limit = position.x + size.x;
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (xf_points[0].x < low_limit) {
|
2018-08-11 06:40:44 +02:00
|
|
|
goto next4;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (xf_points[1].x < low_limit) {
|
2018-08-11 06:40:44 +02:00
|
|
|
goto next4;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (xf_points[2].x < low_limit) {
|
2018-08-11 06:40:44 +02:00
|
|
|
goto next4;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (xf_points[3].x < low_limit) {
|
2018-08-11 06:40:44 +02:00
|
|
|
goto next4;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-08-11 06:40:44 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
next4:
|
|
|
|
|
|
|
|
Vector2 xf_points2[4] = {
|
|
|
|
position,
|
|
|
|
Vector2(position.x + size.x, position.y),
|
|
|
|
Vector2(position.x, position.y + size.y),
|
|
|
|
Vector2(position.x + size.x, position.y + size.y),
|
|
|
|
};
|
|
|
|
|
2022-04-24 23:59:24 +02:00
|
|
|
real_t maxa = p_xform.columns[0].dot(xf_points2[0]);
|
2018-08-11 06:40:44 +02:00
|
|
|
real_t mina = maxa;
|
|
|
|
|
2022-04-24 23:59:24 +02:00
|
|
|
real_t dp = p_xform.columns[0].dot(xf_points2[1]);
|
2018-08-11 06:40:44 +02:00
|
|
|
maxa = MAX(dp, maxa);
|
|
|
|
mina = MIN(dp, mina);
|
|
|
|
|
2022-04-24 23:59:24 +02:00
|
|
|
dp = p_xform.columns[0].dot(xf_points2[2]);
|
2018-08-11 06:40:44 +02:00
|
|
|
maxa = MAX(dp, maxa);
|
|
|
|
mina = MIN(dp, mina);
|
|
|
|
|
2022-04-24 23:59:24 +02:00
|
|
|
dp = p_xform.columns[0].dot(xf_points2[3]);
|
2018-08-11 06:40:44 +02:00
|
|
|
maxa = MAX(dp, maxa);
|
|
|
|
mina = MIN(dp, mina);
|
|
|
|
|
2022-04-24 23:59:24 +02:00
|
|
|
real_t maxb = p_xform.columns[0].dot(xf_points[0]);
|
2018-08-11 06:40:44 +02:00
|
|
|
real_t minb = maxb;
|
|
|
|
|
2022-04-24 23:59:24 +02:00
|
|
|
dp = p_xform.columns[0].dot(xf_points[1]);
|
2018-08-11 06:40:44 +02:00
|
|
|
maxb = MAX(dp, maxb);
|
|
|
|
minb = MIN(dp, minb);
|
|
|
|
|
2022-04-24 23:59:24 +02:00
|
|
|
dp = p_xform.columns[0].dot(xf_points[2]);
|
2018-08-11 06:40:44 +02:00
|
|
|
maxb = MAX(dp, maxb);
|
|
|
|
minb = MIN(dp, minb);
|
|
|
|
|
2022-04-24 23:59:24 +02:00
|
|
|
dp = p_xform.columns[0].dot(xf_points[3]);
|
2018-08-11 06:40:44 +02:00
|
|
|
maxb = MAX(dp, maxb);
|
|
|
|
minb = MIN(dp, minb);
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (mina > maxb) {
|
2018-08-11 06:40:44 +02:00
|
|
|
return false;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (minb > maxa) {
|
2018-08-11 06:40:44 +02:00
|
|
|
return false;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-08-11 06:40:44 +02:00
|
|
|
|
2022-04-24 23:59:24 +02:00
|
|
|
maxa = p_xform.columns[1].dot(xf_points2[0]);
|
2018-08-11 06:40:44 +02:00
|
|
|
mina = maxa;
|
|
|
|
|
2022-04-24 23:59:24 +02:00
|
|
|
dp = p_xform.columns[1].dot(xf_points2[1]);
|
2018-08-11 06:40:44 +02:00
|
|
|
maxa = MAX(dp, maxa);
|
|
|
|
mina = MIN(dp, mina);
|
|
|
|
|
2022-04-24 23:59:24 +02:00
|
|
|
dp = p_xform.columns[1].dot(xf_points2[2]);
|
2018-08-11 06:40:44 +02:00
|
|
|
maxa = MAX(dp, maxa);
|
|
|
|
mina = MIN(dp, mina);
|
|
|
|
|
2022-04-24 23:59:24 +02:00
|
|
|
dp = p_xform.columns[1].dot(xf_points2[3]);
|
2018-08-11 06:40:44 +02:00
|
|
|
maxa = MAX(dp, maxa);
|
|
|
|
mina = MIN(dp, mina);
|
|
|
|
|
2022-04-24 23:59:24 +02:00
|
|
|
maxb = p_xform.columns[1].dot(xf_points[0]);
|
2018-08-11 06:40:44 +02:00
|
|
|
minb = maxb;
|
|
|
|
|
2022-04-24 23:59:24 +02:00
|
|
|
dp = p_xform.columns[1].dot(xf_points[1]);
|
2018-08-11 06:40:44 +02:00
|
|
|
maxb = MAX(dp, maxb);
|
|
|
|
minb = MIN(dp, minb);
|
|
|
|
|
2022-04-24 23:59:24 +02:00
|
|
|
dp = p_xform.columns[1].dot(xf_points[2]);
|
2018-08-11 06:40:44 +02:00
|
|
|
maxb = MAX(dp, maxb);
|
|
|
|
minb = MIN(dp, minb);
|
|
|
|
|
2022-04-24 23:59:24 +02:00
|
|
|
dp = p_xform.columns[1].dot(xf_points[3]);
|
2018-08-11 06:40:44 +02:00
|
|
|
maxb = MAX(dp, maxb);
|
|
|
|
minb = MIN(dp, minb);
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (mina > maxb) {
|
2018-08-11 06:40:44 +02:00
|
|
|
return false;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
if (minb > maxa) {
|
2018-08-11 06:40:44 +02:00
|
|
|
return false;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-08-11 06:40:44 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2021-02-25 15:54:50 +01:00
|
|
|
|
|
|
|
Rect2::operator String() const {
|
|
|
|
return "[P: " + position.operator String() + ", S: " + size + "]";
|
|
|
|
}
|
|
|
|
|
2022-02-04 13:28:02 +01:00
|
|
|
Rect2::operator Rect2i() const {
|
|
|
|
return Rect2i(position, size);
|
2021-02-25 15:54:50 +01:00
|
|
|
}
|