vector3.h
1 /*************************************************************************/
2 /* vector3.h */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* http://www.godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
9 /* */
10 /* Permission is hereby granted, free of charge, to any person obtaining */
11 /* a copy of this software and associated documentation files (the */
12 /* "Software"), to deal in the Software without restriction, including */
13 /* without limitation the rights to use, copy, modify, merge, publish, */
14 /* distribute, sublicense, and/or sell copies of the Software, and to */
15 /* permit persons to whom the Software is furnished to do so, subject to */
16 /* the following conditions: */
17 /* */
18 /* The above copyright notice and this permission notice shall be */
19 /* included in all copies or substantial portions of the Software. */
20 /* */
21 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
22 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
23 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
24 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
25 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
26 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
27 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
28 /*************************************************************************/
29 #ifndef VECTOR3_H
30 #define VECTOR3_H
31 
32 #include "typedefs.h"
33 #include "math_defs.h"
34 #include "math_funcs.h"
35 #include "ustring.h"
36 
37 
38 struct Vector3 {
39 
40  enum Axis {
41  AXIS_X,
42  AXIS_Y,
43  AXIS_Z,
44  };
45 
46  union {
47 
48 #ifdef USE_QUAD_VECTORS
49 
50  struct {
51  real_t x;
52  real_t y;
53  real_t z;
54  real_t _unused;
55  };
56  real_t coord[4];
57 #else
58 
59  struct {
60  real_t x;
61  real_t y;
62  real_t z;
63  };
64 
65  real_t coord[3];
66 #endif
67  };
68 
69  _FORCE_INLINE_ const real_t& operator[](int p_axis) const {
70 
71  return coord[p_axis];
72  }
73 
74  _FORCE_INLINE_ real_t& operator[](int p_axis) {
75 
76  return coord[p_axis];
77  }
78 
79  void set_axis(int p_axis,real_t p_value);
80  real_t get_axis(int p_axis) const;
81 
82  int min_axis() const;
83  int max_axis() const;
84 
85  _FORCE_INLINE_ real_t length() const;
86  _FORCE_INLINE_ real_t length_squared() const;
87 
88  _FORCE_INLINE_ void normalize();
89  _FORCE_INLINE_ Vector3 normalized() const;
90  _FORCE_INLINE_ Vector3 inverse() const;
91 
92  _FORCE_INLINE_ void zero();
93 
94  void snap(float p_val);
95  Vector3 snapped(float p_val) const;
96 
97  void rotate(const Vector3& p_axis,float p_phi);
98  Vector3 rotated(const Vector3& p_axis,float p_phi) const;
99 
100  /* Static Methods between 2 vector3s */
101 
102  _FORCE_INLINE_ Vector3 linear_interpolate(const Vector3& p_b,float p_t) const;
103  Vector3 cubic_interpolate(const Vector3& p_b,const Vector3& p_pre_a, const Vector3& p_post_b,float p_t) const;
104  Vector3 cubic_interpolaten(const Vector3& p_b,const Vector3& p_pre_a, const Vector3& p_post_b,float p_t) const;
105 
106  _FORCE_INLINE_ Vector3 cross(const Vector3& p_b) const;
107  _FORCE_INLINE_ real_t dot(const Vector3& p_b) const;
108 
109  _FORCE_INLINE_ Vector3 abs() const;
110  _FORCE_INLINE_ Vector3 floor() const;
111  _FORCE_INLINE_ Vector3 ceil() const;
112 
113  _FORCE_INLINE_ real_t distance_to(const Vector3& p_b) const;
114  _FORCE_INLINE_ real_t distance_squared_to(const Vector3& p_b) const;
115 
116 
117 
118  _FORCE_INLINE_ Vector3 slide(const Vector3& p_vec) const;
119  _FORCE_INLINE_ Vector3 reflect(const Vector3& p_vec) const;
120 
121 
122  /* Operators */
123 
124  _FORCE_INLINE_ Vector3& operator+=(const Vector3& p_v);
125  _FORCE_INLINE_ Vector3 operator+(const Vector3& p_v) const;
126  _FORCE_INLINE_ Vector3& operator-=(const Vector3& p_v);
127  _FORCE_INLINE_ Vector3 operator-(const Vector3& p_v) const;
128  _FORCE_INLINE_ Vector3& operator*=(const Vector3& p_v);
129  _FORCE_INLINE_ Vector3 operator*(const Vector3& p_v) const;
130  _FORCE_INLINE_ Vector3& operator/=(const Vector3& p_v);
131  _FORCE_INLINE_ Vector3 operator/(const Vector3& p_v) const;
132 
133 
134  _FORCE_INLINE_ Vector3& operator*=(real_t p_scalar);
135  _FORCE_INLINE_ Vector3 operator*(real_t p_scalar) const;
136  _FORCE_INLINE_ Vector3& operator/=(real_t p_scalar);
137  _FORCE_INLINE_ Vector3 operator/(real_t p_scalar) const;
138 
139  _FORCE_INLINE_ Vector3 operator-() const;
140 
141  _FORCE_INLINE_ bool operator==(const Vector3& p_v) const;
142  _FORCE_INLINE_ bool operator!=(const Vector3& p_v) const;
143  _FORCE_INLINE_ bool operator<(const Vector3& p_v) const;
144  _FORCE_INLINE_ bool operator<=(const Vector3& p_v) const;
145 
146  operator String() const;
147 
148  _FORCE_INLINE_ Vector3() { x=y=z=0; }
149  _FORCE_INLINE_ Vector3(real_t p_x,real_t p_y,real_t p_z) { x=p_x; y=p_y; z=p_z; }
150 
151 };
152 
153 #ifdef VECTOR3_IMPL_OVERRIDE
154 
155 #include "vector3_inline.h"
156 
157 #else
158 
159 Vector3 Vector3::cross(const Vector3& p_b) const {
160 
161  Vector3 ret (
162  (y * p_b.z) - (z * p_b.y),
163  (z * p_b.x) - (x * p_b.z),
164  (x * p_b.y) - (y * p_b.x)
165  );
166 
167  return ret;
168 }
169 real_t Vector3::dot(const Vector3& p_b) const {
170 
171  return x*p_b.x + y*p_b.y + z*p_b.z;
172 }
173 
174 Vector3 Vector3::abs() const {
175 
176  return Vector3( Math::abs(x), Math::abs(y), Math::abs(z) );
177 }
178 
179 Vector3 Vector3::floor() const {
180 
181  return Vector3( Math::floor(x), Math::floor(y), Math::floor(z) );
182 }
183 
184 Vector3 Vector3::ceil() const {
185 
186  return Vector3( Math::ceil(x), Math::ceil(y), Math::ceil(z) );
187 }
188 
189 Vector3 Vector3::linear_interpolate(const Vector3& p_b,float p_t) const {
190 
191  return Vector3(
192  x+(p_t * (p_b.x-x)),
193  y+(p_t * (p_b.y-y)),
194  z+(p_t * (p_b.z-z))
195  );
196 
197 }
198 
199 
200 
201 real_t Vector3::distance_to(const Vector3& p_b) const {
202  return (p_b-*this).length();
203 }
204 real_t Vector3::distance_squared_to(const Vector3& p_b) const {
205 
206  return (p_b-*this).length_squared();
207 }
208 
209 /* Operators */
210 
211 Vector3& Vector3::operator+=(const Vector3& p_v) {
212 
213  x+=p_v.x;
214  y+=p_v.y;
215  z+=p_v.z;
216  return *this;
217 }
218 Vector3 Vector3::operator+(const Vector3& p_v) const {
219 
220  return Vector3(x+p_v.x, y+p_v.y, z+ p_v.z);
221 }
222 
223 Vector3& Vector3::operator-=(const Vector3& p_v) {
224 
225  x-=p_v.x;
226  y-=p_v.y;
227  z-=p_v.z;
228  return *this;
229 }
230 Vector3 Vector3::operator-(const Vector3& p_v) const {
231 
232  return Vector3(x-p_v.x, y-p_v.y, z- p_v.z);
233 }
234 
235 
236 
237 Vector3& Vector3::operator*=(const Vector3& p_v) {
238 
239  x*=p_v.x;
240  y*=p_v.y;
241  z*=p_v.z;
242  return *this;
243 }
244 Vector3 Vector3::operator*(const Vector3& p_v) const {
245 
246  return Vector3(x*p_v.x, y*p_v.y, z* p_v.z);
247 }
248 
249 Vector3& Vector3::operator/=(const Vector3& p_v) {
250 
251  x/=p_v.x;
252  y/=p_v.y;
253  z/=p_v.z;
254  return *this;
255 }
256 Vector3 Vector3::operator/(const Vector3& p_v) const {
257 
258  return Vector3(x/p_v.x, y/p_v.y, z/ p_v.z);
259 }
260 
261 Vector3& Vector3::operator*=(real_t p_scalar) {
262  x*=p_scalar;
263  y*=p_scalar;
264  z*=p_scalar;
265  return *this;
266 
267 }
268 
269 _FORCE_INLINE_ Vector3 operator*(real_t p_scalar, const Vector3& p_vec) {
270  return p_vec * p_scalar;
271 }
272 
273 Vector3 Vector3::operator*(real_t p_scalar) const {
274 
275  return Vector3( x*p_scalar, y*p_scalar, z*p_scalar);
276 }
277 
278 Vector3& Vector3::operator/=(real_t p_scalar) {
279  x/=p_scalar;
280  y/=p_scalar;
281  z/=p_scalar;
282  return *this;
283 
284 }
285 
286 Vector3 Vector3::operator/(real_t p_scalar) const {
287 
288  return Vector3( x/p_scalar, y/p_scalar, z/p_scalar);
289 }
290 
291 Vector3 Vector3::operator-() const {
292 
293  return Vector3( -x, -y, -z );
294 }
295 
296 
297 bool Vector3::operator==(const Vector3& p_v) const {
298 
299  return (x==p_v.x && y==p_v.y && z==p_v.z);
300 }
301 
302 bool Vector3::operator!=(const Vector3& p_v) const {
303 
304  return (x!=p_v.x || y!=p_v.y || z!=p_v.z);
305 }
306 
307 bool Vector3::operator<(const Vector3& p_v) const {
308 
309  if (x==p_v.x) {
310  if (y==p_v.y)
311  return z<p_v.z;
312  else
313  return y<p_v.y;
314  } else
315  return x<p_v.x;
316 
317 }
318 
319 bool Vector3::operator<=(const Vector3& p_v) const {
320 
321  if (x==p_v.x) {
322  if (y==p_v.y)
323  return z<=p_v.z;
324  else
325  return y<p_v.y;
326  } else
327  return x<p_v.x;
328 
329 }
330 
331 _FORCE_INLINE_ Vector3 vec3_cross(const Vector3& p_a, const Vector3& p_b) {
332 
333  return p_a.cross(p_b);
334 }
335 
336 _FORCE_INLINE_ real_t vec3_dot(const Vector3& p_a, const Vector3& p_b) {
337 
338  return p_a.dot(p_b);
339 }
340 
341 real_t Vector3::length() const {
342 
343  real_t x2=x*x;
344  real_t y2=y*y;
345  real_t z2=z*z;
346 
347  return Math::sqrt(x2+y2+z2);
348 }
349 real_t Vector3::length_squared() const {
350 
351  real_t x2=x*x;
352  real_t y2=y*y;
353  real_t z2=z*z;
354 
355  return x2+y2+z2;
356 
357 }
358 
359 void Vector3::normalize() {
360 
361  real_t l=length();
362  if (l==0) {
363 
364  x=y=z=0;
365  } else {
366 
367  x/=l;
368  y/=l;
369  z/=l;
370  }
371 }
372 Vector3 Vector3::normalized() const {
373 
374  Vector3 v=*this;
375  v.normalize();
376  return v;
377 }
378 
379 Vector3 Vector3::inverse() const {
380 
381  return Vector3( 1.0/x, 1.0/y, 1.0/z );
382 }
383 
384 void Vector3::zero() {
385 
386  x=y=z=0;
387 }
388 
389 Vector3 Vector3::slide(const Vector3& p_vec) const {
390 
391  return p_vec - *this * this->dot(p_vec);
392 }
393 Vector3 Vector3::reflect(const Vector3& p_vec) const {
394 
395  return p_vec - *this * this->dot(p_vec) * 2.0;
396 
397 }
398 
399 #endif
400 
401 #endif // VECTOR3_H
Definition: vector3.h:38
Definition: ustring.h:64