Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Vector.h
Go to the documentation of this file.
1 //
2 // C++ Interface: Audio::Codec
3 //
4 #ifndef __AUDIO_VECTOR_H__INCLUDED__
5 #define __AUDIO_VECTOR_H__INCLUDED__
6 
7 #include <math.h>
8 
9 namespace Audio {
10 
11  template<typename T> class TVector3 {
12  public:
13  T x,y,z;
14 
15  TVector3() {}
16  TVector3(T xx, T yy, T zz) : x(xx), y(yy), z(zz) {}
17  explicit TVector3(T s) : x(s), y(s), z(s) {}
18 
19  template<typename Y>
20  TVector3(const TVector3<Y> &other) : x(T(other.x)), y(T(other.y)), z(T(other.z)) {}
21 
22  template<typename Y>
24  {
25  x = T(other.x);
26  y = T(other.y);
27  z = T(other.z);
28  return *this;
29  }
30 
32  {
33  x += other.x;
34  y += other.y;
35  z += other.z;
36  return *this;
37  }
38 
40  {
41  x -= other.x;
42  y -= other.y;
43  z -= other.z;
44  return *this;
45  }
46 
48  {
49  x *= other.x;
50  y *= other.y;
51  z *= other.z;
52  return *this;
53  }
54 
56  {
57  x *= t;
58  y *= t;
59  z *= t;
60  return *this;
61  }
62 
64  {
65  x /= other.x;
66  y /= other.y;
67  z /= other.z;
68  return *this;
69  }
70 
72  {
73  x /= t;
74  y /= t;
75  z /= t;
76  return *this;
77  }
78 
79  TVector3<T> operator+(const TVector3<T> &other) const
80  {
81  return TVector3<T>(x+other.x, y+other.y, z+other.z);
82  }
83 
84  TVector3<T> operator-(const TVector3<T> &other) const
85  {
86  return TVector3<T>(x-other.x, y-other.y, z-other.z);
87  }
88 
90  {
91  return TVector3<T>(-x, -y, -z);
92  }
93 
94  TVector3<T> operator*(const TVector3<T> &other) const
95  {
96  return TVector3<T>(x*other.x, y*other.y, z*other.z);
97  }
98 
99  TVector3<T> operator/(const TVector3<T> &other) const
100  {
101  return TVector3<T>(x/other.x, y/other.y, z/other.z);
102  }
103 
105  {
106  return TVector3<T>(x*t, y*t, z*t);
107  }
108 
110  {
111  return TVector3<T>(x/t, y/t, z/t);
112  }
113 
114  T dot(TVector3<T> other) const
115  {
116  return x*other.x + y*other.y + z*other.z;
117  }
118 
119  T normSquared() const
120  {
121  return dot(*this);
122  }
123 
124  T norm() const
125  {
126  return sqrt(normSquared());
127  }
128 
129  T lengthSquared() const
130  {
131  return normSquared();
132  }
133 
134  T length() const
135  {
136  return norm();
137  }
138 
139  T distanceSquared(const TVector3<T> &other) const
140  {
141  return (other - *this).normSquared();
142  }
143 
144  T distance(const TVector3<T> &other) const
145  {
146  return sqrt(distanceSquared(other));
147  }
148 
150  {
151  return TVector3<T>(
152  y * v.z - z * v.y,
153  z * v.x - x * v.z,
154  x * v.y - y * v.x
155  );
156  }
157 
158  void normalize()
159  {
160  *this /= norm();
161  }
162 
164  {
165  return *this / norm();
166  }
167  };
168 
169 };
170 
171 #endif//__AUDIO_VECTOR_H__INCLUDED__