2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* vector3.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 22:01:57 +01:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-08 00:11:42 +02:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 02:10:30 +01: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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#include "vector3.h"
|
2015-04-18 19:19:33 +02:00
|
|
|
#include "matrix3.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void Vector3::rotate(const Vector3 &p_axis, real_t p_phi) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
*this = Basis(p_axis, p_phi).xform(*this);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector3 Vector3::rotated(const Vector3 &p_axis, real_t p_phi) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Vector3 r = *this;
|
2017-03-05 16:44:50 +01:00
|
|
|
r.rotate(p_axis, p_phi);
|
2014-02-10 02:10:30 +01:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void Vector3::set_axis(int p_axis, real_t p_value) {
|
|
|
|
ERR_FAIL_INDEX(p_axis, 3);
|
|
|
|
coord[p_axis] = p_value;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
real_t Vector3::get_axis(int p_axis) const {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_INDEX_V(p_axis, 3, 0);
|
2016-10-01 20:54:31 +02:00
|
|
|
return operator[](p_axis);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int Vector3::min_axis() const {
|
|
|
|
|
2016-10-01 20:54:31 +02:00
|
|
|
return x < y ? (x < z ? 0 : 2) : (y < z ? 1 : 2);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
int Vector3::max_axis() const {
|
|
|
|
|
|
|
|
return x < y ? (y < z ? 2 : 1) : (x < z ? 2 : 0);
|
|
|
|
}
|
|
|
|
|
2017-06-30 20:47:17 +02:00
|
|
|
void Vector3::snap(Vector3 p_val) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-06-30 20:47:17 +02:00
|
|
|
x = Math::stepify(x, p_val.x);
|
|
|
|
y = Math::stepify(y, p_val.y);
|
|
|
|
z = Math::stepify(z, p_val.z);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-06-30 20:47:17 +02:00
|
|
|
Vector3 Vector3::snapped(Vector3 p_val) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector3 v = *this;
|
2016-10-01 20:54:31 +02:00
|
|
|
v.snap(p_val);
|
|
|
|
return v;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector3 Vector3::cubic_interpolaten(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_t) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector3 p0 = p_pre_a;
|
|
|
|
Vector3 p1 = *this;
|
|
|
|
Vector3 p2 = p_b;
|
|
|
|
Vector3 p3 = p_post_b;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
//normalize
|
|
|
|
|
2017-01-14 21:35:39 +01:00
|
|
|
real_t ab = p0.distance_to(p1);
|
|
|
|
real_t bc = p1.distance_to(p2);
|
|
|
|
real_t cd = p2.distance_to(p3);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (ab > 0)
|
|
|
|
p0 = p1 + (p0 - p1) * (bc / ab);
|
|
|
|
if (cd > 0)
|
|
|
|
p3 = p2 + (p3 - p2) * (bc / cd);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-01-14 21:35:39 +01:00
|
|
|
real_t t = p_t;
|
|
|
|
real_t t2 = t * t;
|
|
|
|
real_t t3 = t2 * t;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Vector3 out;
|
2017-03-05 16:44:50 +01:00
|
|
|
out = 0.5 * ((p1 * 2.0) +
|
|
|
|
(-p0 + p2) * t +
|
|
|
|
(2.0 * p0 - 5.0 * p1 + 4 * p2 - p3) * t2 +
|
|
|
|
(-p0 + 3.0 * p1 - 3.0 * p2 + p3) * t3);
|
2014-02-10 02:10:30 +01:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector3 Vector3::cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_t) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector3 p0 = p_pre_a;
|
|
|
|
Vector3 p1 = *this;
|
|
|
|
Vector3 p2 = p_b;
|
|
|
|
Vector3 p3 = p_post_b;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-14 21:35:39 +01:00
|
|
|
real_t t = p_t;
|
|
|
|
real_t t2 = t * t;
|
|
|
|
real_t t3 = t2 * t;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Vector3 out;
|
2017-03-05 16:44:50 +01:00
|
|
|
out = 0.5 * ((p1 * 2.0) +
|
|
|
|
(-p0 + p2) * t +
|
|
|
|
(2.0 * p0 - 5.0 * p1 + 4 * p2 - p3) * t2 +
|
|
|
|
(-p0 + 3.0 * p1 - 3.0 * p2 + p3) * t3);
|
2014-02-10 02:10:30 +01:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
2017-01-14 21:35:39 +01:00
|
|
|
Vector3 Vector3::cubic_interpolate(const Vector3& p_b,const Vector3& p_pre_a, const Vector3& p_post_b,real_t p_t) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Vector3 p0=p_pre_a;
|
|
|
|
Vector3 p1=*this;
|
|
|
|
Vector3 p2=p_b;
|
|
|
|
Vector3 p3=p_post_b;
|
|
|
|
|
|
|
|
if (true) {
|
|
|
|
|
2017-01-14 21:35:39 +01:00
|
|
|
real_t ab = p0.distance_to(p1);
|
|
|
|
real_t bc = p1.distance_to(p2);
|
|
|
|
real_t cd = p2.distance_to(p3);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
//if (ab>bc) {
|
|
|
|
if (ab>0)
|
|
|
|
p0 = p1+(p0-p1)*(bc/ab);
|
|
|
|
//}
|
|
|
|
|
|
|
|
//if (cd>bc) {
|
|
|
|
if (cd>0)
|
|
|
|
p3 = p2+(p3-p2)*(bc/cd);
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
|
2017-01-14 21:35:39 +01:00
|
|
|
real_t t = p_t;
|
|
|
|
real_t t2 = t * t;
|
|
|
|
real_t t3 = t2 * t;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Vector3 out;
|
2017-01-14 21:35:39 +01:00
|
|
|
out.x = 0.5 * ( ( 2.0 * p1.x ) +
|
2014-02-10 02:10:30 +01:00
|
|
|
( -p0.x + p2.x ) * t +
|
2017-01-14 21:35:39 +01:00
|
|
|
( 2.0 * p0.x - 5.0 * p1.x + 4 * p2.x - p3.x ) * t2 +
|
|
|
|
( -p0.x + 3.0 * p1.x - 3.0 * p2.x + p3.x ) * t3 );
|
|
|
|
out.y = 0.5 * ( ( 2.0 * p1.y ) +
|
2014-02-10 02:10:30 +01:00
|
|
|
( -p0.y + p2.y ) * t +
|
2017-01-14 21:35:39 +01:00
|
|
|
( 2.0 * p0.y - 5.0 * p1.y + 4 * p2.y - p3.y ) * t2 +
|
|
|
|
( -p0.y + 3.0 * p1.y - 3.0 * p2.y + p3.y ) * t3 );
|
|
|
|
out.z = 0.5 * ( ( 2.0 * p1.z ) +
|
2014-02-10 02:10:30 +01:00
|
|
|
( -p0.z + p2.z ) * t +
|
2017-01-14 21:35:39 +01:00
|
|
|
( 2.0 * p0.z - 5.0 * p1.z + 4 * p2.z - p3.z ) * t2 +
|
|
|
|
( -p0.z + 3.0 * p1.z - 3.0 * p2.z + p3.z ) * t3 );
|
2014-02-10 02:10:30 +01:00
|
|
|
return out;
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
#endif
|
2014-02-10 02:10:30 +01:00
|
|
|
Vector3::operator String() const {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return (rtos(x) + ", " + rtos(y) + ", " + rtos(z));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|