Dali 3D User Interface Engine
vector2.h
Go to the documentation of this file.
1 #ifndef __DALI_VECTOR_2_H__
2 #define __DALI_VECTOR_2_H__
3 
4 /*
5  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20 
21 // EXTERNAL INCLUDES
22 #include <iosfwd>
23 
24 // INTERNAL INCLUDES
27 
28 namespace Dali
29 {
35 struct Vector3;
36 struct Vector4;
37 
43 {
44 // (x width) and (y height) must be consecutive in memory.
45 // No other data must be added before (x width) member.
46 // No virtual methods must be added to this struct.
47 
48 public:
49 
55  : x(0.0f),
56  y(0.0f)
57  {
58  }
59 
67  explicit Vector2(float x, float y)
68  : x(x), y(y)
69  {
70  }
71 
78  explicit Vector2(const float* array)
79  : x(array[0]),
80  y(array[1])
81  {
82  }
83 
90  explicit Vector2(const Vector3& vec3);
91 
98  explicit Vector2(const Vector4& vec4);
99 
100 // Constants
101 
102  static const Vector2 ONE;
103  static const Vector2 XAXIS;
104  static const Vector2 YAXIS;
105  static const Vector2 NEGATIVE_XAXIS;
106  static const Vector2 NEGATIVE_YAXIS;
107  static const Vector2 ZERO;
108 
109 // API
110 
118  Vector2& operator=(const float* array)
119  {
120  x = array[0];
121  y = array[1];
122 
123  return *this;
124  }
125 
133  Vector2& operator=(const Vector3& rhs);
134 
142  Vector2& operator=(const Vector4& rhs);
143 
151  Vector2 operator+(const Vector2& rhs) const
152  {
153  Vector2 temp(*this);
154 
155  return temp += rhs;
156  }
157 
165  Vector2& operator+=(const Vector2& rhs)
166  {
167  x += rhs.x;
168  y += rhs.y;
169 
170  return *this;
171  }
172 
180  Vector2 operator-(const Vector2& rhs) const
181  {
182  Vector2 temp(*this);
183 
184  return temp -= rhs;
185  }
186 
194  Vector2& operator-=(const Vector2& rhs)
195  {
196  x -= rhs.x;
197  y -= rhs.y;
198 
199  return *this;
200  }
201 
209  Vector2 operator*(const Vector2& rhs) const
210  {
211  return Vector2(x * rhs.x, y * rhs.y);
212  }
213 
221  Vector2 operator*(float rhs) const
222  {
223  return Vector2(x * rhs, y * rhs);
224  }
225 
233  Vector2& operator*=(const Vector2& rhs)
234  {
235  x *= rhs.x;
236  y *= rhs.y;
237 
238  return *this;
239  }
240 
248  Vector2& operator*=(float rhs)
249  {
250  x *= rhs;
251  y *= rhs;
252 
253  return *this;
254  }
255 
263  Vector2 operator/(const Vector2& rhs) const
264  {
265  return Vector2(x / rhs.x, y / rhs.y);
266  }
267 
275  Vector2 operator/(float rhs) const
276  {
277  return Vector2(x / rhs, y / rhs);
278  }
279 
280 
288  Vector2& operator/=(const Vector2& rhs)
289  {
290  x /= rhs.x;
291  y /= rhs.y;
292 
293  return *this;
294  }
295 
303  Vector2& operator/=(float rhs)
304  {
305  x /= rhs;
306  y /= rhs;
307 
308  return *this;
309  }
310 
318  {
319  Vector2 temp(-x, -y);
320 
321  return temp;
322  }
323 
333  bool operator==(const Vector2& rhs) const;
334 
344  bool operator!=(const Vector2& rhs) const
345  {
346  return !(*this == rhs);
347  }
348 
357  const float& operator[](const unsigned int index) const
358  {
359  DALI_ASSERT_ALWAYS( index < 2 && "Vector element index out of bounds" );
360 
361  return AsFloat()[index];
362  }
363 
372  float& operator[](const unsigned int index)
373  {
374  DALI_ASSERT_ALWAYS( index < 2 && "Vector element index out of bounds" );
375 
376  return AsFloat()[index];
377  }
378 
385  float Length() const;
386 
395  float LengthSquared() const;
396 
402  void Normalize();
403 
411  void Clamp( const Vector2& min, const Vector2& max );
412 
423  const float* AsFloat() const {return &x;}
424 
435  float* AsFloat() {return &x;}
436 
437 public: // Data
438 
439  // NOTE
440  // (x width) and (y height) must be consecutive in memory.
441  // No other data must be added before (x width) member.
442  // No virtual methods must be added to this struct.
443  union
444  {
445  float x;
446  float width;
447  };
448 
449  union
450  {
451  float y;
452  float height;
453  };
454 
455 };
456 
461 typedef Vector2 Size;
462 
471 DALI_IMPORT_API std::ostream& operator<< (std::ostream& o, const Vector2& vector);
472 
482 inline Vector2 Min( const Vector2& a, const Vector2& b )
483 {
484  return Vector2( a.x < b.x ? a.x : b.x , a.y < b.y ? a.y : b.y );
485 }
486 
496 inline Vector2 Max( const Vector2& a, const Vector2& b )
497 {
498  return Vector2( a.x > b.x ? a.x : b.x , a.y > b.y ? a.y : b.y );
499 }
500 
510 DALI_IMPORT_API Vector2 Clamp( const Vector2& v, const float& min, const float& max );
511 
512 // Allow Vector2 to be treated as a POD type
513 template <> struct TypeTraits< Vector2 > : public BasicTypes< Vector2 > { enum { IS_TRIVIAL_TYPE = true }; };
514 
518 } // namespace Dali
519 
520 #endif // __DALI_VECTOR_2_H__
Dali Docs Home
Read more about Dali