1 /*************************************************************************/
2 /* vector3.cpp */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
9 /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /*************************************************************************/
30 #include "vector3.h"
31 #include "matrix3.h"
32
rotate(const Vector3 & p_axis,float p_phi)33 void Vector3::rotate(const Vector3 &p_axis, float p_phi) {
34
35 *this = Matrix3(p_axis, p_phi).xform(*this);
36 }
37
rotated(const Vector3 & p_axis,float p_phi) const38 Vector3 Vector3::rotated(const Vector3 &p_axis, float p_phi) const {
39
40 Vector3 r = *this;
41 r.rotate(p_axis, p_phi);
42 return r;
43 }
44
set_axis(int p_axis,real_t p_value)45 void Vector3::set_axis(int p_axis, real_t p_value) {
46 ERR_FAIL_INDEX(p_axis, 3);
47 coord[p_axis] = p_value;
48 }
get_axis(int p_axis) const49 real_t Vector3::get_axis(int p_axis) const {
50
51 ERR_FAIL_INDEX_V(p_axis, 3, 0);
52 return operator[](p_axis);
53 }
54
min_axis() const55 int Vector3::min_axis() const {
56
57 return x < y ? (x < z ? 0 : 2) : (y < z ? 1 : 2);
58 }
max_axis() const59 int Vector3::max_axis() const {
60
61 return x < y ? (y < z ? 2 : 1) : (x < z ? 2 : 0);
62 }
63
snap(float p_val)64 void Vector3::snap(float p_val) {
65
66 x = Math::stepify(x, p_val);
67 y = Math::stepify(y, p_val);
68 z = Math::stepify(z, p_val);
69 }
snapped(float p_val) const70 Vector3 Vector3::snapped(float p_val) const {
71
72 Vector3 v = *this;
73 v.snap(p_val);
74 return v;
75 }
76
cubic_interpolaten(const Vector3 & p_b,const Vector3 & p_pre_a,const Vector3 & p_post_b,float p_t) const77 Vector3 Vector3::cubic_interpolaten(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, float p_t) const {
78
79 Vector3 p0 = p_pre_a;
80 Vector3 p1 = *this;
81 Vector3 p2 = p_b;
82 Vector3 p3 = p_post_b;
83
84 {
85 //normalize
86
87 float ab = p0.distance_to(p1);
88 float bc = p1.distance_to(p2);
89 float cd = p2.distance_to(p3);
90
91 if (ab > 0)
92 p0 = p1 + (p0 - p1) * (bc / ab);
93 if (cd > 0)
94 p3 = p2 + (p3 - p2) * (bc / cd);
95 }
96
97 float t = p_t;
98 float t2 = t * t;
99 float t3 = t2 * t;
100
101 Vector3 out;
102 out = 0.5f * ((p1 * 2.0f) +
103 (-p0 + p2) * t +
104 (2.0f * p0 - 5.0f * p1 + 4 * p2 - p3) * t2 +
105 (-p0 + 3.0f * p1 - 3.0f * p2 + p3) * t3);
106 return out;
107 }
108
cubic_interpolate(const Vector3 & p_b,const Vector3 & p_pre_a,const Vector3 & p_post_b,float p_t) const109 Vector3 Vector3::cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, float p_t) const {
110
111 Vector3 p0 = p_pre_a;
112 Vector3 p1 = *this;
113 Vector3 p2 = p_b;
114 Vector3 p3 = p_post_b;
115
116 float t = p_t;
117 float t2 = t * t;
118 float t3 = t2 * t;
119
120 Vector3 out;
121 out = 0.5f * ((p1 * 2.0f) +
122 (-p0 + p2) * t +
123 (2.0f * p0 - 5.0f * p1 + 4 * p2 - p3) * t2 +
124 (-p0 + 3.0f * p1 - 3.0f * p2 + p3) * t3);
125 return out;
126 }
127
128 #if 0
129 Vector3 Vector3::cubic_interpolate(const Vector3& p_b,const Vector3& p_pre_a, const Vector3& p_post_b,float p_t) const {
130
131 Vector3 p0=p_pre_a;
132 Vector3 p1=*this;
133 Vector3 p2=p_b;
134 Vector3 p3=p_post_b;
135
136 if (true) {
137
138 float ab = p0.distance_to(p1);
139 float bc = p1.distance_to(p2);
140 float cd = p2.distance_to(p3);
141
142 //if (ab>bc) {
143 if (ab>0)
144 p0 = p1+(p0-p1)*(bc/ab);
145 //}
146
147 //if (cd>bc) {
148 if (cd>0)
149 p3 = p2+(p3-p2)*(bc/cd);
150 //}
151 }
152
153 float t = p_t;
154 float t2 = t * t;
155 float t3 = t2 * t;
156
157 Vector3 out;
158 out.x = 0.5f * ( ( 2.0f * p1.x ) +
159 ( -p0.x + p2.x ) * t +
160 ( 2.0f * p0.x - 5.0f * p1.x + 4 * p2.x - p3.x ) * t2 +
161 ( -p0.x + 3.0f * p1.x - 3.0f * p2.x + p3.x ) * t3 );
162 out.y = 0.5f * ( ( 2.0f * p1.y ) +
163 ( -p0.y + p2.y ) * t +
164 ( 2.0f * p0.y - 5.0f * p1.y + 4 * p2.y - p3.y ) * t2 +
165 ( -p0.y + 3.0f * p1.y - 3.0f * p2.y + p3.y ) * t3 );
166 out.z = 0.5f * ( ( 2.0f * p1.z ) +
167 ( -p0.z + p2.z ) * t +
168 ( 2.0f * p0.z - 5.0f * p1.z + 4 * p2.z - p3.z ) * t2 +
169 ( -p0.z + 3.0f * p1.z - 3.0f * p2.z + p3.z ) * t3 );
170 return out;
171 }
172 #endif
operator String() const173 Vector3::operator String() const {
174
175 return (rtos(x) + ", " + rtos(y) + ", " + rtos(z));
176 }
177