2022-03-15 13:29:32 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* vector3.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
|
|
|
#include "vector3.h"
|
|
|
|
|
|
|
|
#include "core/math/basis.h"
|
|
|
|
|
|
|
|
void Vector3::rotate(const Vector3 &p_axis, real_t p_phi) {
|
|
|
|
*this = Basis(p_axis, p_phi).xform(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector3 Vector3::rotated(const Vector3 &p_axis, real_t p_phi) const {
|
|
|
|
Vector3 r = *this;
|
|
|
|
r.rotate(p_axis, p_phi);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Vector3::set_axis(int p_axis, real_t p_value) {
|
|
|
|
ERR_FAIL_INDEX(p_axis, 3);
|
|
|
|
coord[p_axis] = p_value;
|
|
|
|
}
|
|
|
|
real_t Vector3::get_axis(int p_axis) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_axis, 3, 0);
|
|
|
|
return operator[](p_axis);
|
|
|
|
}
|
|
|
|
|
2023-05-27 11:12:31 +02:00
|
|
|
void Vector3::snap(const Vector3 &p_val) {
|
2022-03-15 13:29:32 +01:00
|
|
|
x = Math::stepify(x, p_val.x);
|
|
|
|
y = Math::stepify(y, p_val.y);
|
|
|
|
z = Math::stepify(z, p_val.z);
|
|
|
|
}
|
2023-05-27 11:12:31 +02:00
|
|
|
Vector3 Vector3::snapped(const Vector3 &p_val) const {
|
2022-03-15 13:29:32 +01:00
|
|
|
Vector3 v = *this;
|
|
|
|
v.snap(p_val);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector3 Vector3::limit_length(const real_t p_len) const {
|
|
|
|
const real_t l = length();
|
|
|
|
Vector3 v = *this;
|
|
|
|
if (l > 0 && p_len < l) {
|
|
|
|
v /= l;
|
|
|
|
v *= p_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector3 Vector3::move_toward(const Vector3 &p_to, const real_t p_delta) const {
|
|
|
|
Vector3 v = *this;
|
|
|
|
Vector3 vd = p_to - v;
|
|
|
|
real_t len = vd.length();
|
|
|
|
return len <= p_delta || len < (real_t)CMP_EPSILON ? p_to : v + vd / len * p_delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
Basis Vector3::outer(const Vector3 &p_b) const {
|
|
|
|
Vector3 row0(x * p_b.x, x * p_b.y, x * p_b.z);
|
|
|
|
Vector3 row1(y * p_b.x, y * p_b.y, y * p_b.z);
|
|
|
|
Vector3 row2(z * p_b.x, z * p_b.y, z * p_b.z);
|
|
|
|
|
|
|
|
return Basis(row0, row1, row2);
|
|
|
|
}
|
|
|
|
|
|
|
|
Basis Vector3::to_diagonal_matrix() const {
|
|
|
|
return Basis(x, 0, 0,
|
|
|
|
0, y, 0,
|
|
|
|
0, 0, z);
|
|
|
|
}
|
|
|
|
|
2022-08-15 16:13:21 +02: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));
|
|
|
|
}
|
|
|
|
|
2022-03-15 13:29:32 +01: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector3::operator String() const {
|
2022-08-15 16:13:21 +02:00
|
|
|
return "(" + String::num_real(x) + ", " + String::num_real(y) + ", " + String::num_real(z) + ")";
|
2022-03-15 13:29:32 +01:00
|
|
|
}
|