2014-02-10 01:10:30 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* vector3.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-04 23:50:27 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "vector3.h"
|
2018-09-11 16:13:45 +00:00
|
|
|
|
2019-02-09 05:24:02 +00:00
|
|
|
#include "core/math/basis.h"
|
2022-02-19 15:47:24 +00:00
|
|
|
#include "core/math/vector2.h"
|
|
|
|
#include "core/math/vector3i.h"
|
|
|
|
#include "core/string/ustring.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2024-02-17 22:24:59 +00:00
|
|
|
void Vector3::rotate(const Vector3 &p_axis, real_t p_angle) {
|
2022-05-05 11:25:04 +00:00
|
|
|
*this = Basis(p_axis, p_angle).xform(*this);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2024-02-17 22:24:59 +00:00
|
|
|
Vector3 Vector3::rotated(const Vector3 &p_axis, real_t p_angle) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
Vector3 r = *this;
|
2022-05-05 11:25:04 +00:00
|
|
|
r.rotate(p_axis, p_angle);
|
2014-02-10 01:10:30 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2021-02-01 05:10:52 +00:00
|
|
|
Vector3 Vector3::clamp(const Vector3 &p_min, const Vector3 &p_max) const {
|
|
|
|
return Vector3(
|
|
|
|
CLAMP(x, p_min.x, p_max.x),
|
|
|
|
CLAMP(y, p_min.y, p_max.y),
|
|
|
|
CLAMP(z, p_min.z, p_max.z));
|
|
|
|
}
|
|
|
|
|
2024-02-17 22:24:59 +00:00
|
|
|
void Vector3::snap(const Vector3 &p_step) {
|
2020-12-21 18:02:57 +00:00
|
|
|
x = Math::snapped(x, p_step.x);
|
|
|
|
y = Math::snapped(y, p_step.y);
|
|
|
|
z = Math::snapped(z, p_step.z);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2024-02-17 22:24:59 +00:00
|
|
|
Vector3 Vector3::snapped(const Vector3 &p_step) const {
|
2016-10-01 18:54:31 +00:00
|
|
|
Vector3 v = *this;
|
2020-12-21 18:02:57 +00:00
|
|
|
v.snap(p_step);
|
2016-10-01 18:54:31 +00:00
|
|
|
return v;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2024-02-17 22:24:59 +00:00
|
|
|
Vector3 Vector3::limit_length(real_t p_len) const {
|
2021-02-01 05:42:00 +00:00
|
|
|
const real_t l = length();
|
|
|
|
Vector3 v = *this;
|
|
|
|
if (l > 0 && p_len < l) {
|
|
|
|
v /= l;
|
|
|
|
v *= p_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2024-02-17 22:24:59 +00:00
|
|
|
Vector3 Vector3::move_toward(const Vector3 &p_to, real_t p_delta) const {
|
2019-04-07 21:40:56 +00:00
|
|
|
Vector3 v = *this;
|
|
|
|
Vector3 vd = p_to - v;
|
|
|
|
real_t len = vd.length();
|
2022-02-24 07:17:00 +00:00
|
|
|
return len <= p_delta || len < (real_t)CMP_EPSILON ? p_to : v + vd / len * p_delta;
|
2019-04-07 21:40:56 +00:00
|
|
|
}
|
|
|
|
|
2022-02-19 15:47:24 +00:00
|
|
|
Vector2 Vector3::octahedron_encode() const {
|
|
|
|
Vector3 n = *this;
|
|
|
|
n /= Math::abs(n.x) + Math::abs(n.y) + Math::abs(n.z);
|
|
|
|
Vector2 o;
|
|
|
|
if (n.z >= 0.0f) {
|
|
|
|
o.x = n.x;
|
|
|
|
o.y = n.y;
|
|
|
|
} else {
|
|
|
|
o.x = (1.0f - Math::abs(n.y)) * (n.x >= 0.0f ? 1.0f : -1.0f);
|
|
|
|
o.y = (1.0f - Math::abs(n.x)) * (n.y >= 0.0f ? 1.0f : -1.0f);
|
|
|
|
}
|
|
|
|
o.x = o.x * 0.5f + 0.5f;
|
|
|
|
o.y = o.y * 0.5f + 0.5f;
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector3 Vector3::octahedron_decode(const Vector2 &p_oct) {
|
|
|
|
Vector2 f(p_oct.x * 2.0f - 1.0f, p_oct.y * 2.0f - 1.0f);
|
|
|
|
Vector3 n(f.x, f.y, 1.0f - Math::abs(f.x) - Math::abs(f.y));
|
2024-03-29 23:44:12 +00:00
|
|
|
const real_t t = CLAMP(-n.z, 0.0f, 1.0f);
|
2022-02-19 15:47:24 +00:00
|
|
|
n.x += n.x >= 0 ? -t : t;
|
|
|
|
n.y += n.y >= 0 ? -t : t;
|
|
|
|
return n.normalized();
|
|
|
|
}
|
|
|
|
|
2024-02-17 22:24:59 +00:00
|
|
|
Vector2 Vector3::octahedron_tangent_encode(float p_sign) const {
|
2024-03-29 23:44:12 +00:00
|
|
|
const real_t bias = 1.0f / (real_t)32767.0f;
|
2024-01-28 20:51:39 +00:00
|
|
|
Vector2 res = octahedron_encode();
|
2023-02-14 03:24:14 +00:00
|
|
|
res.y = MAX(res.y, bias);
|
2022-02-22 23:10:09 +00:00
|
|
|
res.y = res.y * 0.5f + 0.5f;
|
2024-02-17 22:24:59 +00:00
|
|
|
res.y = p_sign >= 0.0f ? res.y : 1 - res.y;
|
2022-02-22 23:10:09 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2024-02-17 22:24:59 +00:00
|
|
|
Vector3 Vector3::octahedron_tangent_decode(const Vector2 &p_oct, float *r_sign) {
|
2022-02-22 23:10:09 +00:00
|
|
|
Vector2 oct_compressed = p_oct;
|
|
|
|
oct_compressed.y = oct_compressed.y * 2 - 1;
|
2024-02-17 22:24:59 +00:00
|
|
|
*r_sign = oct_compressed.y >= 0.0f ? 1.0f : -1.0f;
|
2022-02-22 23:10:09 +00:00
|
|
|
oct_compressed.y = Math::abs(oct_compressed.y);
|
|
|
|
Vector3 res = Vector3::octahedron_decode(oct_compressed);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2021-11-29 17:02:42 +00:00
|
|
|
Basis Vector3::outer(const Vector3 &p_with) const {
|
2022-08-31 04:28:36 +00:00
|
|
|
Basis basis;
|
|
|
|
basis.rows[0] = Vector3(x * p_with.x, x * p_with.y, x * p_with.z);
|
|
|
|
basis.rows[1] = Vector3(y * p_with.x, y * p_with.y, y * p_with.z);
|
|
|
|
basis.rows[2] = Vector3(z * p_with.x, z * p_with.y, z * p_with.z);
|
|
|
|
return basis;
|
2019-10-07 14:26:27 +00:00
|
|
|
}
|
|
|
|
|
2019-10-14 20:33:45 +00:00
|
|
|
bool Vector3::is_equal_approx(const Vector3 &p_v) const {
|
|
|
|
return Math::is_equal_approx(x, p_v.x) && Math::is_equal_approx(y, p_v.y) && Math::is_equal_approx(z, p_v.z);
|
|
|
|
}
|
|
|
|
|
2022-09-02 00:32:33 +00:00
|
|
|
bool Vector3::is_zero_approx() const {
|
|
|
|
return Math::is_zero_approx(x) && Math::is_zero_approx(y) && Math::is_zero_approx(z);
|
|
|
|
}
|
|
|
|
|
2022-08-11 08:12:27 +00:00
|
|
|
bool Vector3::is_finite() const {
|
|
|
|
return Math::is_finite(x) && Math::is_finite(y) && Math::is_finite(z);
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
Vector3::operator String() const {
|
2021-02-25 14:54:50 +00:00
|
|
|
return "(" + String::num_real(x, false) + ", " + String::num_real(y, false) + ", " + String::num_real(z, false) + ")";
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2022-02-19 15:47:24 +00:00
|
|
|
|
|
|
|
Vector3::operator Vector3i() const {
|
|
|
|
return Vector3i(x, y, z);
|
|
|
|
}
|