Crazy Eddie's GUI System  0.8.4
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
Vector.h
1 /***********************************************************************
2  created: 13/2/2011
3  author: Martin Preisler (reworked from code by Paul D Turner)
4 
5  purpose: Defines interfaces for Vector classes
6 *************************************************************************/
7 /***************************************************************************
8  * Copyright (C) 2004 - 2011 Paul D Turner & The CEGUI Development Team
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 BE LIABLE FOR ANY CLAIM, DAMAGES OR
25  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27  * OTHER DEALINGS IN THE SOFTWARE.
28  ***************************************************************************/
29 #ifndef _CEGUIVector_h_
30 #define _CEGUIVector_h_
31 
32 #include "CEGUI/UDim.h"
33 #include <typeinfo>
34 #include <ostream>
35 
36 // Start of CEGUI namespace section
37 namespace CEGUI
38 {
39 
52 template<typename T>
53 class Vector2:
54  public AllocatedObject<Vector2<T> >
55 {
56 public:
57  typedef T value_type;
58 
59  inline Vector2()
60  {}
61 
62  inline Vector2(const T x, const T y):
63  d_x(x),
64  d_y(y)
65  {}
66 
67  inline Vector2(const Vector2& v):
68  d_x(v.d_x),
69  d_y(v.d_y)
70  {}
71 
72  inline Vector2& operator*=(const Vector2& vec)
73  {
74  d_x *= vec.d_x;
75  d_y *= vec.d_y;
76 
77  return *this;
78  }
79 
80  inline Vector2& operator/=(const Vector2& vec)
81  {
82  d_x /= vec.d_x;
83  d_y /= vec.d_y;
84 
85  return *this;
86  }
87 
88  inline Vector2& operator+=(const Vector2& vec)
89  {
90  d_x += vec.d_x;
91  d_y += vec.d_y;
92 
93  return *this;
94  }
95 
96  inline Vector2& operator-=(const Vector2& vec)
97  {
98  d_x -= vec.d_x;
99  d_y -= vec.d_y;
100 
101  return *this;
102  }
103 
104  inline Vector2 operator+(const Vector2& vec) const
105  {
106  return Vector2(d_x + vec.d_x, d_y + vec.d_y);
107  }
108 
109  inline Vector2 operator-(const Vector2& vec) const
110  {
111  return Vector2(d_x - vec.d_x, d_y - vec.d_y);
112  }
113 
114  inline Vector2 operator*(const Vector2& vec) const
115  {
116  return Vector2(d_x * vec.d_x, d_y * vec.d_y);
117  }
118 
119  inline Vector2 operator/(const Vector2& vec) const
120  {
121  return Vector2(d_x / vec.d_x, d_y / vec.d_y);
122  }
123 
124  inline Vector2 operator*(const T c) const
125  {
126  return Vector2(d_x * c, d_y * c);
127  }
128 
129  inline Vector2& operator*=(const T c)
130  {
131  d_x *= c;
132  d_y *= c;
133 
134  return *this;
135  }
136 
137  inline Vector2 operator/(const T c) const
138  {
139  return Vector2(d_x / c, d_y / c);
140  }
141 
142  inline bool operator==(const Vector2& vec) const
143  {
144  return ((d_x == vec.d_x) && (d_y == vec.d_y));
145  }
146 
147  inline bool operator!=(const Vector2& vec) const
148  {
149  return !(operator==(vec));
150  }
151 
155  inline friend std::ostream& operator << (std::ostream& s, const Vector2& v)
156  {
157  s << "CEGUI::Vector2<" << typeid(T).name() << ">(" << v.d_x << ", " << v.d_y << ")";
158  return s;
159  }
160 
162  inline static Vector2 zero()
163  {
164  return Vector2(TypeSensitiveZero<T>(), TypeSensitiveZero<T>());
165  }
166 
168  inline static Vector2 one()
169  {
170  return Vector2(TypeSensitiveOne<T>(), TypeSensitiveOne<T>());
171  }
172 
174  inline static Vector2 one_x()
175  {
176  return Vector2(TypeSensitiveOne<T>(), TypeSensitiveZero<T>());
177  }
178 
180  inline static Vector2 one_y()
181  {
182  return Vector2(TypeSensitiveZero<T>(), TypeSensitiveOne<T>());
183  }
184 
185  T d_x;
186  T d_y;
187 };
188 
189 // the main reason for this is to keep C++ API in sync with other languages
190 typedef Vector2<float> Vector2f;
191 
192 // we need to allow UVector2 to be multiplied by floats, this is the most elegant way to do that
193 inline Vector2<UDim> operator * (const Vector2<UDim>& v, const float c)
194 {
195  return Vector2<UDim>(v.d_x * c, v.d_y * c);
196 }
197 
198 typedef Vector2<UDim> UVector2;
199 
212 template<typename T>
213 class Vector3:
214  public AllocatedObject<Vector3<T> >
215 {
216 public:
217  typedef T value_type;
218 
219  inline Vector3()
220  {}
221 
222  inline Vector3(const T x, const T y, const T z):
223  d_x(x),
224  d_y(y),
225  d_z(z)
226  {}
227 
228  inline explicit Vector3(const Vector2<T>& v, const T z):
229  d_x(v.d_x),
230  d_y(v.d_y),
231  d_z(z)
232  {}
233 
234  inline Vector3(const Vector3& v):
235  d_x(v.d_x),
236  d_y(v.d_y),
237  d_z(v.d_z)
238  {}
239 
240  inline bool operator==(const Vector3& vec) const
241  {
242  return ((d_x == vec.d_x) && (d_y == vec.d_y) && (d_z == vec.d_z));
243  }
244 
245  inline bool operator!=(const Vector3& vec) const
246  {
247  return !(operator==(vec));
248  }
249 
250  inline Vector3 operator*(const T c) const
251  {
252  return Vector3(d_x * c, d_y * c, d_z * c);
253  }
254 
255  inline Vector3 operator+(const Vector3& v) const
256  {
257  return Vector3(d_x + v.d_x, d_y + v.d_y, d_z + v.d_z);
258  }
259 
260  inline Vector3 operator-(const Vector3& v) const
261  {
262  return Vector3(d_x - v.d_x, d_y - v.d_y, d_z - v.d_z);
263  }
264 
268  inline friend std::ostream& operator << (std::ostream& s, const Vector3& v)
269  {
270  s << "CEGUI::Vector3<" << typeid(T).name() << ">(" << v.d_x << ", " << v.d_y << ", " << v.d_z << ")";
271  return s;
272  }
273 
275  inline static Vector3 zero()
276  {
277  return Vector3(TypeSensitiveZero<T>(), TypeSensitiveZero<T>(), TypeSensitiveZero<T>());
278  }
279 
281  inline static Vector3 one()
282  {
283  return Vector3(TypeSensitiveOne<T>(), TypeSensitiveOne<T>(), TypeSensitiveOne<T>());
284  }
285 
287  inline static Vector3 one_x()
288  {
289  return Vector3(TypeSensitiveOne<T>(), TypeSensitiveZero<T>(), TypeSensitiveZero<T>());
290  }
291 
293  inline static Vector3 one_y()
294  {
295  return Vector3(TypeSensitiveZero<T>(), TypeSensitiveOne<T>(), TypeSensitiveZero<T>());
296  }
297 
299  inline static Vector3 one_z()
300  {
301  return Vector3(TypeSensitiveZero<T>(), TypeSensitiveZero<T>(), TypeSensitiveOne<T>());
302  }
303 
304  T d_x;
305  T d_y;
306  T d_z;
307 };
308 
309 // the main reason for this is to keep C++ API in sync with other languages
310 typedef Vector3<float> Vector3f;
311 
312 } // End of CEGUI namespace section
313 
314 #endif // end of guard _CEGUIVector_h_