voxelTerrain
 All Classes Functions Variables Typedefs Enumerations Pages
vector2.hpp
1 #ifndef BLUB_VECTOR2_HPP
2 #define BLUB_VECTOR2_HPP
3 
4 #include "blub/core/globals.hpp"
5 #include "blub/math/predecl.hpp"
6 #include "blub/serialization/access.hpp"
7 #include "blub/serialization/nameValuePair.hpp"
8 
9 
10 namespace Ogre
11 {
12  class Vector2;
13 }
14 
15 
16 namespace blub
17 {
18 
19 
20 class vector2
21 {
22 public:
23 #ifndef BLUB_NO_OGRE3D
24  vector2(const Ogre::Vector2 &vec);
25  operator Ogre::Vector2() const;
26 #endif
27 
28  vector2()
29  : x(0.)
30  , y(0.)
31  {
32  }
33 
34  vector2(const real& fX, const real& fY)
35  : x(fX)
36  , y(fY)
37  {
38  }
39 
40  vector2(const real& scaler)
41  : x(scaler)
42  , y(scaler)
43  {
44  }
45 
46  vector2(const vector2int32& cast);
47 
48 
61  inline vector2& operator = ( const real fScalar)
62  {
63  x = fScalar;
64  y = fScalar;
65 
66  return *this;
67  }
68 
69  inline bool operator == ( const vector2& rkVector ) const
70  {
71  return ( x == rkVector.x && y == rkVector.y );
72  }
73 
74  inline bool operator != ( const vector2& rkVector ) const
75  {
76  return ( x != rkVector.x || y != rkVector.y );
77  }
78 
79  // arithmetic operations
80  inline vector2 operator + ( const vector2& rkVector ) const
81  {
82  return vector2(
83  x + rkVector.x,
84  y + rkVector.y);
85  }
86 
87  inline vector2 operator - ( const vector2& rkVector ) const
88  {
89  return vector2(
90  x - rkVector.x,
91  y - rkVector.y);
92  }
93 
94  inline vector2 operator * ( const real fScalar ) const
95  {
96  return vector2(
97  x * fScalar,
98  y * fScalar);
99  }
100 
101  inline vector2 operator * ( const vector2& rhs) const
102  {
103  return vector2(
104  x * rhs.x,
105  y * rhs.y);
106  }
107 
108  inline vector2 operator / ( const real fScalar ) const
109  {
110  BASSERT( fScalar != 0.0 );
111 
112  real fInv = 1.0f / fScalar;
113 
114  return vector2(
115  x * fInv,
116  y * fInv);
117  }
118 
119  inline vector2 operator / ( const vector2& rhs) const
120  {
121  return vector2(
122  x / rhs.x,
123  y / rhs.y);
124  }
125 
126  inline const vector2& operator + () const
127  {
128  return *this;
129  }
130 
131  inline vector2 operator - () const
132  {
133  return vector2(-x, -y);
134  }
135 
136  // overloaded operators to help vector2
137  inline friend vector2 operator * ( const real fScalar, const vector2& rkVector )
138  {
139  return vector2(
140  fScalar * rkVector.x,
141  fScalar * rkVector.y);
142  }
143 
144  inline friend vector2 operator / ( const real fScalar, const vector2& rkVector )
145  {
146  return vector2(
147  fScalar / rkVector.x,
148  fScalar / rkVector.y);
149  }
150 
151  inline friend vector2 operator + (const vector2& lhs, const real rhs)
152  {
153  return vector2(
154  lhs.x + rhs,
155  lhs.y + rhs);
156  }
157 
158  inline friend vector2 operator + (const real lhs, const vector2& rhs)
159  {
160  return vector2(
161  lhs + rhs.x,
162  lhs + rhs.y);
163  }
164 
165  inline friend vector2 operator - (const vector2& lhs, const real rhs)
166  {
167  return vector2(
168  lhs.x - rhs,
169  lhs.y - rhs);
170  }
171 
172  inline friend vector2 operator - (const real lhs, const vector2& rhs)
173  {
174  return vector2(
175  lhs - rhs.x,
176  lhs - rhs.y);
177  }
178 
179  // arithmetic updates
180  inline vector2& operator += ( const vector2& rkVector )
181  {
182  x += rkVector.x;
183  y += rkVector.y;
184 
185  return *this;
186  }
187 
188  inline vector2& operator += ( const real fScaler )
189  {
190  x += fScaler;
191  y += fScaler;
192 
193  return *this;
194  }
195 
196  inline vector2& operator -= ( const vector2& rkVector )
197  {
198  x -= rkVector.x;
199  y -= rkVector.y;
200 
201  return *this;
202  }
203 
204  inline vector2& operator -= ( const real fScaler )
205  {
206  x -= fScaler;
207  y -= fScaler;
208 
209  return *this;
210  }
211 
212  inline vector2& operator *= ( const real fScalar )
213  {
214  x *= fScalar;
215  y *= fScalar;
216 
217  return *this;
218  }
219 
220  inline vector2& operator *= ( const vector2& rkVector )
221  {
222  x *= rkVector.x;
223  y *= rkVector.y;
224 
225  return *this;
226  }
227 
228  inline vector2& operator /= ( const real fScalar )
229  {
230  BASSERT( fScalar != 0.0 );
231 
232  real fInv = 1.0f / fScalar;
233 
234  x *= fInv;
235  y *= fInv;
236 
237  return *this;
238  }
239 
240  inline vector2& operator /= ( const vector2& rkVector )
241  {
242  x /= rkVector.x;
243  y /= rkVector.y;
244 
245  return *this;
246  }
247 
248 public:
249  real x, y;
250 
251 protected:
252  BLUB_SERIALIZATION_ACCESS
253  template<typename Archive>
254  void serialize(Archive & readWrite, const unsigned int version)
255  {
256  (void)version;
257 
258  readWrite & BLUB_SERIALIZATION_NAMEVALUEPAIR(x);
259  readWrite & BLUB_SERIALIZATION_NAMEVALUEPAIR(y);
260  }
261 
262 };
263 
264 
265 std::ostream& operator<< (std::ostream& ostr, const vector2& toCast);
266 std::size_t hash_value(const vector2& value);
267 
268 
269 }
270 
271 #endif // BLUB_VECTOR2_HPP
Definition: vector2.hpp:20
Definition: predecl.hpp:25
Definition: deadlineTimer.hpp:10
Definition: axisAlignedBox.hpp:10
vector2 & operator=(const real fScalar)
Definition: vector2.hpp:61