Dali 3D User Interface Engine
vector3.h
Go to the documentation of this file.
1 #ifndef __DALI_VECTOR_3_H__
2 #define __DALI_VECTOR_3_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 Vector2;
36 struct Vector4;
37 class Quaternion;
38 
44 {
45 // Construction
46 
51  // NOTE
52  // (x width r), (y height g), (z depth b) must be consecutive in memory.
53  // No other data must be added before (x width r) member.
54  // No virtual methods must be added to this struct.
56  : x(0.0f),
57  y(0.0f),
58  z(0.0f)
59  {
60  }
61 
70  explicit Vector3(float x, float y, float z)
71  : x(x),
72  y(y),
73  z(z)
74  {
75  }
76 
83  explicit Vector3(const float* array)
84  : x(array[0]),
85  y(array[1]),
86  z(array[2])
87  {
88  }
89 
96  explicit Vector3( const Vector2& vec2 );
97 
104  explicit Vector3( const Vector4& vec4 );
105 
106 // Constants
107 
108  static const Vector3 ONE;
109  static const Vector3 XAXIS;
110  static const Vector3 YAXIS;
111  static const Vector3 ZAXIS;
112  static const Vector3 NEGATIVE_XAXIS;
113  static const Vector3 NEGATIVE_YAXIS;
114  static const Vector3 NEGATIVE_ZAXIS;
115  static const Vector3 ZERO;
116 
117 // API
118 
126  Vector3& operator=(const float* array)
127  {
128  x = array[0];
129  y = array[1];
130  z = array[2];
131 
132  return *this;
133  }
134 
142  Vector3& operator=(const Vector2& rhs);
143 
151  Vector3& operator=(const Vector4& rhs);
152 
160  Vector3 operator+(const Vector3& rhs) const
161  {
162  Vector3 temp(*this);
163 
164  return temp += rhs;
165  }
166 
174  Vector3& operator+=(const Vector3& rhs)
175  {
176  x += rhs.x;
177  y += rhs.y;
178  z += rhs.z;
179 
180  return *this;
181  }
182 
190  Vector3 operator-(const Vector3& rhs) const
191  {
192  Vector3 temp(*this);
193 
194  return temp -= rhs;
195  }
196 
204  Vector3& operator-=(const Vector3& rhs)
205  {
206  x -= rhs.x;
207  y -= rhs.y;
208  z -= rhs.z;
209 
210  return *this;
211  }
212 
220  Vector3 operator*(const Vector3& rhs) const
221  {
222  Vector3 temp(*this);
223 
224  return temp *= rhs;
225  }
226 
234  Vector3 operator*(float rhs) const
235  {
236  return Vector3(x * rhs, y * rhs, z * rhs);
237  }
238 
246  Vector3& operator*=(const Vector3& rhs)
247  {
248  x *= rhs.x;
249  y *= rhs.y;
250  z *= rhs.z;
251 
252  return *this;
253  }
254 
262  Vector3& operator*=(float rhs)
263  {
264  x *= rhs;
265  y *= rhs;
266  z *= rhs;
267 
268  return *this;
269  }
270 
278  Vector3& operator*=(const Quaternion& rhs);
279 
287  Vector3 operator/(const Vector3& rhs) const
288  {
289  Vector3 temp(*this);
290 
291  return temp /= rhs;
292  }
293 
301  Vector3 operator/(float rhs) const
302  {
303  return Vector3(x / rhs, y / rhs, z / rhs);
304  }
305 
313  Vector3& operator/=(const Vector3& rhs)
314  {
315  x /= rhs.x;
316  y /= rhs.y;
317  z /= rhs.z;
318 
319  return *this;
320  }
321 
329  Vector3& operator/=(float rhs)
330  {
331  float oneOverRhs = 1.0f / rhs;
332  x *= oneOverRhs;
333  y *= oneOverRhs;
334  z *= oneOverRhs;
335 
336  return *this;
337  }
338 
346  {
347  Vector3 temp(-x, -y, -z);
348 
349  return temp;
350  }
351 
361  bool operator==(const Vector3& rhs) const;
362 
372  bool operator!=(const Vector3& rhs) const
373  {
374  return !(*this == rhs);
375  }
376 
385  const float& operator[](const unsigned int index) const
386  {
387  DALI_ASSERT_ALWAYS( index < 3 && "Vector element index out of bounds" );
388 
389  return AsFloat()[index];
390  }
391 
400  float& operator[](const unsigned int index)
401  {
402  DALI_ASSERT_ALWAYS( index < 3 && "Vector element index out of bounds" );
403 
404  return AsFloat()[index];
405  }
406 
417  float Dot(const Vector3& other) const;
418 
429  Vector3 Cross(const Vector3& other) const;
430 
437  float Length() const;
438 
447  float LengthSquared() const;
448 
454  void Normalize();
455 
463  void Clamp( const Vector3& min, const Vector3& max );
464 
476  const float* AsFloat() const {return &x;}
477 
489  float* AsFloat() {return &x;}
490 
498  const Vector2& GetVectorXY() const {return reinterpret_cast<const Vector2&>(x);}
499 
507  Vector2& GetVectorXY() {return reinterpret_cast<Vector2&>(x);}
508 
516  const Vector2& GetVectorYZ() const {return reinterpret_cast<const Vector2&>(y);}
517 
525  Vector2& GetVectorYZ() {return reinterpret_cast<Vector2&>(y);}
526 
527 // Data
528 
529  // NOTE
530  // (x width r), (y height g), (z depth b) must be consecutive in memory.
531  // No other data must be added before (x width r) member.
532  // No virtual methods must be added to this struct.
533  union
534  {
535  float x;
536  float width;
537  float r;
538  };
539  union
540  {
541  float y;
542  float height;
543  float g;
544  };
545  union
546  {
547  float z;
548  float depth;
549  float b;
550  };
551 };
552 
561 DALI_IMPORT_API std::ostream& operator<< (std::ostream& o, const Vector3& vector);
562 
572 inline Vector3 Min( const Vector3& a, const Vector3& b )
573 {
574  return Vector3( a.x < b.x ? a.x : b.x ,
575  a.y < b.y ? a.y : b.y,
576  a.z < b.z ? a.z : b.z );
577 }
578 
588 inline Vector3 Max( const Vector3& a, const Vector3& b )
589 {
590  return Vector3( a.x > b.x ? a.x : b.x,
591  a.y > b.y ? a.y : b.y,
592  a.z > b.z ? a.z : b.z );
593 }
594 
604 DALI_IMPORT_API Vector3 Clamp( const Vector3& v, const float& min, const float& max );
605 
606 // Allow Vector3 to be treated as a POD type
607 template <> struct TypeTraits< Vector3 > : public BasicTypes< Vector3 > { enum { IS_TRIVIAL_TYPE = true }; };
608 
612 } // namespace Dali
613 
614 #endif // __DALI_VECTOR_3_H__
Dali Docs Home
Read more about Dali