RioEngine  0.1
My first attempt to create a 3D WYSIWYG Game Engine
ik_vec3d.h
Go to the documentation of this file.
1 // Copyright (C) 2002-2014 Nikolaus Gebhardt
2 // This file is part of the "irrKlang" library.
3 // For conditions of distribution and use, see copyright notice in irrKlang.h
4 
5 #ifndef __IRR_IRRKLANG_VEC_3D_H_INCLUDED__
6 #define __IRR_IRRKLANG_VEC_3D_H_INCLUDED__
7 
8 #include <math.h>
9 #include "ik_irrKlangTypes.h"
10 
11 
12 namespace irrklang
13 {
14 
16  template <class T>
17  class vec3d
18  {
19  public:
20 
21  vec3d(): X(0), Y(0), Z(0) {};
22  vec3d(T nx, T ny, T nz) : X(nx), Y(ny), Z(nz) {};
23  vec3d(const vec3d<T>& other) :X(other.X), Y(other.Y), Z(other.Z) {};
24 
26  #ifdef __IRR_POINT_3D_H_INCLUDED__
27  template<class B>
28  vec3d(const B& other) :X(other.X), Y(other.Y), Z(other.Z) {};
29  #endif // __IRR_POINT_3D_H_INCLUDED__
30 
31  // operators
32 
33  vec3d<T> operator-() const { return vec3d<T>(-X, -Y, -Z); }
34 
35  vec3d<T>& operator=(const vec3d<T>& other) { X = other.X; Y = other.Y; Z = other.Z; return *this; }
36 
37  vec3d<T> operator+(const vec3d<T>& other) const { return vec3d<T>(X + other.X, Y + other.Y, Z + other.Z); }
38  vec3d<T>& operator+=(const vec3d<T>& other) { X+=other.X; Y+=other.Y; Z+=other.Z; return *this; }
39 
40  vec3d<T> operator-(const vec3d<T>& other) const { return vec3d<T>(X - other.X, Y - other.Y, Z - other.Z); }
41  vec3d<T>& operator-=(const vec3d<T>& other) { X-=other.X; Y-=other.Y; Z-=other.Z; return *this; }
42 
43  vec3d<T> operator*(const vec3d<T>& other) const { return vec3d<T>(X * other.X, Y * other.Y, Z * other.Z); }
44  vec3d<T>& operator*=(const vec3d<T>& other) { X*=other.X; Y*=other.Y; Z*=other.Z; return *this; }
45  vec3d<T> operator*(const T v) const { return vec3d<T>(X * v, Y * v, Z * v); }
46  vec3d<T>& operator*=(const T v) { X*=v; Y*=v; Z*=v; return *this; }
47 
48  vec3d<T> operator/(const vec3d<T>& other) const { return vec3d<T>(X / other.X, Y / other.Y, Z / other.Z); }
49  vec3d<T>& operator/=(const vec3d<T>& other) { X/=other.X; Y/=other.Y; Z/=other.Z; return *this; }
50  vec3d<T> operator/(const T v) const { T i=(T)1.0/v; return vec3d<T>(X * i, Y * i, Z * i); }
51  vec3d<T>& operator/=(const T v) { T i=(T)1.0/v; X*=i; Y*=i; Z*=i; return *this; }
52 
53  bool operator<=(const vec3d<T>&other) const { return X<=other.X && Y<=other.Y && Z<=other.Z;};
54  bool operator>=(const vec3d<T>&other) const { return X>=other.X && Y>=other.Y && Z>=other.Z;};
55 
56  bool operator==(const vec3d<T>& other) const { return other.X==X && other.Y==Y && other.Z==Z; }
57  bool operator!=(const vec3d<T>& other) const { return other.X!=X || other.Y!=Y || other.Z!=Z; }
58 
59  // functions
60 
62  bool equals(const vec3d<T>& other)
63  {
64  return equalsfloat(X, other.X) &&
65  equalsfloat(Y, other.Y) &&
66  equalsfloat(Z, other.Z);
67  }
68 
69  void set(const T nx, const T ny, const T nz) {X=nx; Y=ny; Z=nz; }
70  void set(const vec3d<T>& p) { X=p.X; Y=p.Y; Z=p.Z;}
71 
73  ik_f64 getLength() const { return sqrt(X*X + Y*Y + Z*Z); }
74 
76 
78  ik_f64 getLengthSQ() const { return X*X + Y*Y + Z*Z; }
79 
81  T dotProduct(const vec3d<T>& other) const
82  {
83  return X*other.X + Y*other.Y + Z*other.Z;
84  }
85 
87 
88  ik_f64 getDistanceFrom(const vec3d<T>& other) const
89  {
90  ik_f64 vx = X - other.X; ik_f64 vy = Y - other.Y; ik_f64 vz = Z - other.Z;
91  return sqrt(vx*vx + vy*vy + vz*vz);
92  }
93 
95 
96  ik_f32 getDistanceFromSQ(const vec3d<T>& other) const
97  {
98  ik_f32 vx = X - other.X; ik_f32 vy = Y - other.Y; ik_f32 vz = Z - other.Z;
99  return (vx*vx + vy*vy + vz*vz);
100  }
101 
104  {
105  return vec3d<T>(Y * p.Z - Z * p.Y, Z * p.X - X * p.Z, X * p.Y - Y * p.X);
106  }
107 
109 
110  bool isBetweenPoints(const vec3d<T>& begin, const vec3d<T>& end) const
111  {
112  ik_f32 f = (ik_f32)(end - begin).getLengthSQ();
113  return (ik_f32)getDistanceFromSQ(begin) < f &&
114  (ik_f32)getDistanceFromSQ(end) < f;
115  }
116 
119  {
120  T l = (T)getLength();
121  if (l == 0)
122  return *this;
123 
124  l = (T)1.0 / l;
125  X *= l;
126  Y *= l;
127  Z *= l;
128  return *this;
129  }
130 
132  void setLength(T newlength)
133  {
134  normalize();
135  *this *= newlength;
136  }
137 
139  void invert()
140  {
141  X *= -1.0f;
142  Y *= -1.0f;
143  Z *= -1.0f;
144  }
145 
150  void rotateXZBy(ik_f64 degrees, const vec3d<T>& center)
151  {
152  degrees *= IK_DEGTORAD64;
153  T cs = (T)cos(degrees);
154  T sn = (T)sin(degrees);
155  X -= center.X;
156  Z -= center.Z;
157  set(X*cs - Z*sn, Y, X*sn + Z*cs);
158  X += center.X;
159  Z += center.Z;
160  }
161 
166  void rotateXYBy(ik_f64 degrees, const vec3d<T>& center)
167  {
168  degrees *= IK_DEGTORAD64;
169  T cs = (T)cos(degrees);
170  T sn = (T)sin(degrees);
171  X -= center.X;
172  Y -= center.Y;
173  set(X*cs - Y*sn, X*sn + Y*cs, Z);
174  X += center.X;
175  Y += center.Y;
176  }
177 
182  void rotateYZBy(ik_f64 degrees, const vec3d<T>& center)
183  {
184  degrees *= IK_DEGTORAD64;
185  T cs = (T)cos(degrees);
186  T sn = (T)sin(degrees);
187  Z -= center.Z;
188  Y -= center.Y;
189  set(X, Y*cs - Z*sn, Y*sn + Z*cs);
190  Z += center.Z;
191  Y += center.Y;
192  }
193 
195 
197  vec3d<T> getInterpolated(const vec3d<T>& other, ik_f32 d) const
198  {
199  ik_f32 inv = 1.0f - d;
200  return vec3d<T>(other.X*inv + X*d,
201  other.Y*inv + Y*d,
202  other.Z*inv + Z*d);
203  }
204 
206 
210  {
211  vec3d<T> angle;
212 
213  angle.Y = (T)atan2(X, Z);
214  angle.Y *= (ik_f32)IK_RADTODEG;
215 
216  if (angle.Y < 0.0f) angle.Y += 360.0f;
217  if (angle.Y >= 360.0f) angle.Y -= 360.0f;
218 
219  ik_f32 z1 = (T)sqrt(X*X + Z*Z);
220 
221  angle.X = (T)atan2(z1, Y);
222  angle.X *= (ik_f32)IK_RADTODEG;
223  angle.X -= 90.0f;
224 
225  if (angle.X < 0.0f) angle.X += 360.0f;
226  if (angle.X >= 360) angle.X -= 360.0f;
227 
228  return angle;
229  }
230 
232 
234  void getAs4Values(T* array)
235  {
236  array[0] = X;
237  array[1] = Y;
238  array[2] = Z;
239  array[3] = 0;
240  }
241 
242 
243  // member variables
244 
245  T X, Y, Z;
246  };
247 
248 
251 
254 
255  template<class S, class T> vec3d<T> operator*(const S scalar, const vec3d<T>& vector) { return vector*scalar; }
256 
257 } // end namespace irrklang
258 
259 
260 #endif
261 
vec3d< T > operator+(const vec3d< T > &other) const
Definition: ik_vec3d.h:37
a 3d vector template class for representing vectors and points in 3d
Definition: ik_vec3d.h:17
vec3d< T > getHorizontalAngle()
Gets the Y and Z rotations of a vector.
Definition: ik_vec3d.h:209
T dotProduct(const vec3d< T > &other) const
Returns the dot product with another vector.
Definition: ik_vec3d.h:81
vec3d< T > operator/(const vec3d< T > &other) const
Definition: ik_vec3d.h:48
bool operator==(const vec3d< T > &other) const
Definition: ik_vec3d.h:56
vec3d< T > & operator*=(const vec3d< T > &other)
Definition: ik_vec3d.h:44
void invert()
Inverts the vector.
Definition: ik_vec3d.h:139
vec3d< T > operator/(const T v) const
Definition: ik_vec3d.h:50
const ik_f32 IK_RADTODEG
bool operator!=(const vec3d< T > &other) const
Definition: ik_vec3d.h:57
double ik_f64
64 bit floating point variable.
void rotateYZBy(ik_f64 degrees, const vec3d< T > &center)
Definition: ik_vec3d.h:182
ik_f64 getLength() const
Returns length of the vector.
Definition: ik_vec3d.h:73
Everything in the irrKlang Sound Engine can be found in this namespace.
bool equals(const vec3d< T > &other)
returns if this vector equalsfloat the other one, taking floating point rounding errors into account ...
Definition: ik_vec3d.h:62
vec3d< T > & operator-=(const vec3d< T > &other)
Definition: ik_vec3d.h:41
vec3d< ik_f32 > vec3df
Typedef for a ik_f32 3d vector, a vector using floats for X, Y and Z.
Definition: ik_vec3d.h:250
bool isBetweenPoints(const vec3d< T > &begin, const vec3d< T > &end) const
Returns if this vector interpreted as a point is on a line between two other points.
Definition: ik_vec3d.h:110
vec3d< T > & operator*=(const T v)
Definition: ik_vec3d.h:46
bool operator>=(const vec3d< T > &other) const
Definition: ik_vec3d.h:54
vec3d< T > crossProduct(const vec3d< T > &p) const
Calculates the cross product with another vector.
Definition: ik_vec3d.h:103
const ik_f64 IK_DEGTORAD64
void rotateXZBy(ik_f64 degrees, const vec3d< T > &center)
Definition: ik_vec3d.h:150
vec3d< T > getInterpolated(const vec3d< T > &other, ik_f32 d) const
Returns interpolated vector.
Definition: ik_vec3d.h:197
vec3d< T > operator*(const T v) const
Definition: ik_vec3d.h:45
void set(const T nx, const T ny, const T nz)
Definition: ik_vec3d.h:69
bool equalsfloat(const ik_f32 a, const ik_f32 b, const ik_f32 tolerance=IK_ROUNDING_ERROR_32)
ik_f32 getDistanceFromSQ(const vec3d< T > &other) const
Returns squared distance from an other point.
Definition: ik_vec3d.h:96
ik_f64 getDistanceFrom(const vec3d< T > &other) const
Returns distance from an other point.
Definition: ik_vec3d.h:88
vec3d< T > operator-(const vec3d< T > &other) const
Definition: ik_vec3d.h:40
float ik_f32
32 bit floating point variable.
vec3d< T > operator*(const vec3d< T > &other) const
Definition: ik_vec3d.h:43
void setLength(T newlength)
Sets the lenght of the vector to a new value.
Definition: ik_vec3d.h:132
vec3d< T > & normalize()
Normalizes the vector.
Definition: ik_vec3d.h:118
vec3d< T > & operator/=(const vec3d< T > &other)
Definition: ik_vec3d.h:49
void rotateXYBy(ik_f64 degrees, const vec3d< T > &center)
Definition: ik_vec3d.h:166
vec3d< T > & operator/=(const T v)
Definition: ik_vec3d.h:51
vec3d< T > operator*(const S scalar, const vec3d< T > &vector)
Definition: ik_vec3d.h:255
vec3d< ik_s32 > vec3di
Typedef for an integer 3d vector, a vector using ints for X, Y and Z.
Definition: ik_vec3d.h:253
vec3d< T > operator-() const
constructor creating an irrklang vec3d from an irrlicht vector.
Definition: ik_vec3d.h:33
vec3d(T nx, T ny, T nz)
Definition: ik_vec3d.h:22
void getAs4Values(T *array)
Fills an array of 4 values with the vector data (usually floats).
Definition: ik_vec3d.h:234
vec3d< T > & operator+=(const vec3d< T > &other)
Definition: ik_vec3d.h:38
ik_f64 getLengthSQ() const
Returns squared length of the vector.
Definition: ik_vec3d.h:78
vec3d(const vec3d< T > &other)
Definition: ik_vec3d.h:23
vec3d< T > & operator=(const vec3d< T > &other)
Definition: ik_vec3d.h:35
void set(const vec3d< T > &p)
Definition: ik_vec3d.h:70