Crazy Eddie's GUI System  0.8.3
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
Vector.h
1 /***********************************************************************
2  filename: CEGUIVector.h
3  created: 13/2/2011
4  author: Martin Preisler (reworked from code by Paul D Turner)
5 
6  purpose: Defines interfaces for Vector classes
7 *************************************************************************/
8 /***************************************************************************
9  * Copyright (C) 2004 - 2011 Paul D Turner & The CEGUI Development Team
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining
12  * a copy of this software and associated documentation files (the
13  * "Software"), to deal in the Software without restriction, including
14  * without limitation the rights to use, copy, modify, merge, publish,
15  * distribute, sublicense, and/or sell copies of the Software, and to
16  * permit persons to whom the Software is furnished to do so, subject to
17  * the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be
20  * included in all copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
26  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28  * OTHER DEALINGS IN THE SOFTWARE.
29  ***************************************************************************/
30 #ifndef _CEGUIVector_h_
31 #define _CEGUIVector_h_
32 
33 #include "CEGUI/UDim.h"
34 #include <typeinfo>
35 #include <ostream>
36 
37 // Start of CEGUI namespace section
38 namespace CEGUI
39 {
40 
53 template<typename T>
54 class Vector2:
55  public AllocatedObject<Vector2<T> >
56 {
57 public:
58  typedef T value_type;
59 
60  inline Vector2()
61  {}
62 
63  inline Vector2(const T x, const T y):
64  d_x(x),
65  d_y(y)
66  {}
67 
68  inline Vector2(const Vector2& v):
69  d_x(v.d_x),
70  d_y(v.d_y)
71  {}
72 
73  inline Vector2& operator*=(const Vector2& vec)
74  {
75  d_x *= vec.d_x;
76  d_y *= vec.d_y;
77 
78  return *this;
79  }
80 
81  inline Vector2& operator/=(const Vector2& vec)
82  {
83  d_x /= vec.d_x;
84  d_y /= vec.d_y;
85 
86  return *this;
87  }
88 
89  inline Vector2& operator+=(const Vector2& vec)
90  {
91  d_x += vec.d_x;
92  d_y += vec.d_y;
93 
94  return *this;
95  }
96 
97  inline Vector2& operator-=(const Vector2& vec)
98  {
99  d_x -= vec.d_x;
100  d_y -= vec.d_y;
101 
102  return *this;
103  }
104 
105  inline Vector2 operator+(const Vector2& vec) const
106  {
107  return Vector2(d_x + vec.d_x, d_y + vec.d_y);
108  }
109 
110  inline Vector2 operator-(const Vector2& vec) const
111  {
112  return Vector2(d_x - vec.d_x, d_y - vec.d_y);
113  }
114 
115  inline Vector2 operator*(const Vector2& vec) const
116  {
117  return Vector2(d_x * vec.d_x, d_y * vec.d_y);
118  }
119 
120  inline Vector2 operator/(const Vector2& vec) const
121  {
122  return Vector2(d_x / vec.d_x, d_y / vec.d_y);
123  }
124 
125  inline Vector2 operator*(const T c) const
126  {
127  return Vector2(d_x * c, d_y * c);
128  }
129 
130  inline Vector2& operator*=(const T c)
131  {
132  d_x *= c;
133  d_y *= c;
134 
135  return *this;
136  }
137 
138  inline Vector2 operator/(const T c) const
139  {
140  return Vector2(d_x / c, d_y / c);
141  }
142 
143  inline bool operator==(const Vector2& vec) const
144  {
145  return ((d_x == vec.d_x) && (d_y == vec.d_y));
146  }
147 
148  inline bool operator!=(const Vector2& vec) const
149  {
150  return !(operator==(vec));
151  }
152 
156  inline friend std::ostream& operator << (std::ostream& s, const Vector2& v)
157  {
158  s << "CEGUI::Vector2<" << typeid(T).name() << ">(" << v.d_x << ", " << v.d_y << ")";
159  return s;
160  }
161 
163  inline static Vector2 zero()
164  {
165  return Vector2(TypeSensitiveZero<T>(), TypeSensitiveZero<T>());
166  }
167 
169  inline static Vector2 one()
170  {
171  return Vector2(TypeSensitiveOne<T>(), TypeSensitiveOne<T>());
172  }
173 
175  inline static Vector2 one_x()
176  {
177  return Vector2(TypeSensitiveOne<T>(), TypeSensitiveZero<T>());
178  }
179 
181  inline static Vector2 one_y()
182  {
183  return Vector2(TypeSensitiveZero<T>(), TypeSensitiveOne<T>());
184  }
185 
186  T d_x;
187  T d_y;
188 };
189 
190 // the main reason for this is to keep C++ API in sync with other languages
191 typedef Vector2<float> Vector2f;
192 
193 // we need to allow UVector2 to be multiplied by floats, this is the most elegant way to do that
194 inline Vector2<UDim> operator * (const Vector2<UDim>& v, const float c)
195 {
196  return Vector2<UDim>(v.d_x * c, v.d_y * c);
197 }
198 
199 typedef Vector2<UDim> UVector2;
200 
213 template<typename T>
214 class Vector3:
215  public AllocatedObject<Vector3<T> >
216 {
217 public:
218  typedef T value_type;
219 
220  inline Vector3()
221  {}
222 
223  inline Vector3(const T x, const T y, const T z):
224  d_x(x),
225  d_y(y),
226  d_z(z)
227  {}
228 
229  inline explicit Vector3(const Vector2<T>& v, const T z):
230  d_x(v.d_x),
231  d_y(v.d_y),
232  d_z(z)
233  {}
234 
235  inline Vector3(const Vector3& v):
236  d_x(v.d_x),
237  d_y(v.d_y),
238  d_z(v.d_z)
239  {}
240 
241  inline bool operator==(const Vector3& vec) const
242  {
243  return ((d_x == vec.d_x) && (d_y == vec.d_y) && (d_z == vec.d_z));
244  }
245 
246  inline bool operator!=(const Vector3& vec) const
247  {
248  return !(operator==(vec));
249  }
250 
251  inline Vector3 operator*(const T c) const
252  {
253  return Vector3(d_x * c, d_y * c, d_z * c);
254  }
255 
256  inline Vector3 operator+(const Vector3& v) const
257  {
258  return Vector3(d_x + v.d_x, d_y + v.d_y, d_z + v.d_z);
259  }
260 
261  inline Vector3 operator-(const Vector3& v) const
262  {
263  return Vector3(d_x - v.d_x, d_y - v.d_y, d_z - v.d_z);
264  }
265 
269  inline friend std::ostream& operator << (std::ostream& s, const Vector3& v)
270  {
271  s << "CEGUI::Vector3<" << typeid(T).name() << ">(" << v.d_x << ", " << v.d_y << ", " << v.d_z << ")";
272  return s;
273  }
274 
276  inline static Vector3 zero()
277  {
278  return Vector3(TypeSensitiveZero<T>(), TypeSensitiveZero<T>(), TypeSensitiveZero<T>());
279  }
280 
282  inline static Vector3 one()
283  {
284  return Vector3(TypeSensitiveOne<T>(), TypeSensitiveOne<T>(), TypeSensitiveOne<T>());
285  }
286 
288  inline static Vector3 one_x()
289  {
290  return Vector3(TypeSensitiveOne<T>(), TypeSensitiveZero<T>(), TypeSensitiveZero<T>());
291  }
292 
294  inline static Vector3 one_y()
295  {
296  return Vector3(TypeSensitiveZero<T>(), TypeSensitiveOne<T>(), TypeSensitiveZero<T>());
297  }
298 
300  inline static Vector3 one_z()
301  {
302  return Vector3(TypeSensitiveZero<T>(), TypeSensitiveZero<T>(), TypeSensitiveOne<T>());
303  }
304 
305  T d_x;
306  T d_y;
307  T d_z;
308 };
309 
310 // the main reason for this is to keep C++ API in sync with other languages
311 typedef Vector3<float> Vector3f;
312 
313 } // End of CEGUI namespace section
314 
315 #endif // end of guard _CEGUIVector_h_