1 #ifndef __DALI_VECTOR_4_H__
2 #define __DALI_VECTOR_4_H__
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 #include <dali/public-api/common/dali-common.h>
33 * @brief A four dimensional vector.
35 * Components can be used as position or offset (x,y,z,w); color (r,g,b,a) or texture coords(s,t,p,q)
37 struct DALI_IMPORT_API Vector4
40 // xrs, ygt, zbp and waq must be consecutive in memory.
41 // No other data must be added before xrs member.
42 // No virtual methods must be added to this struct.
47 * @brief Default constructor, initializes the vector to 0.
58 * @brief Conversion constructor from four floats.
60 * @param [in] x (or r/s) component
61 * @param [in] y (or g/t) component
62 * @param [in] z (or b/p) component
63 * @param [in] w (or a/q) component
65 explicit Vector4(float x, float y, float z, float w)
75 * @brief Conversion constructor from an array of four floats.
77 * @param [in] array of either xyzw/rgba/stpq
79 explicit Vector4(const float* array)
88 * @brief Conversion constructor from Vector2.
90 * @param [in] vec2 to copy from, z and w are initialized to 0
92 explicit Vector4( const Vector2& vec2 );
95 * @brief Conversion constructor from Vector3.
97 * @param [in] vec3 to copy from, w is initialized to 0
99 explicit Vector4( const Vector3& vec3 );
102 static const Vector4 ONE; ///< (1.0f,1.0f,1.0f,1.0f)
103 static const Vector4 XAXIS; ///< (1.0f,0.0f,0.0f,0.0f)
104 static const Vector4 YAXIS; ///< (0.0f,1.0f,0.0f,0.0f)
105 static const Vector4 ZAXIS; ///< (0.0f,0.0f,1.0f,0.0f)
106 static const Vector4 ZERO; ///< (0.0f, 0.0f, 0.0f, 0.0f)
111 * @brief Assignment operator.
113 * @param [in] array of floats
116 Vector4& operator=(const float* array)
127 * @brief Assignment operator.
129 * Only sets x and y. z and w are left as they were
130 * @param [in] vec2 to assign from.
133 Vector4& operator=(const Vector2& vec2 );
136 * @brief Assignment operator.
138 * Only sets x and y and z. w is left as it was
139 * @param [in] vec3 to assign from
142 Vector4& operator=(const Vector3& vec3 );
145 * @brief Addition operator.
147 * @param[in] rhs vector to add.
148 * @return A vector containing the result of the addition
150 Vector4 operator+(const Vector4 & rhs) const
158 * @brief Addition assignment operator.
160 * @param[in] rhs vector to add.
163 Vector4& operator+=(const Vector4& rhs)
174 * @brief Subtraction operator.
176 * @param[in] rhs the vector to subtract
177 * @return A vector containing the result of the subtraction
179 Vector4 operator-(const Vector4& rhs) const
189 * @brief Subtraction assignment operator.
191 * @param[in] rhs the vector to subtract
194 Vector4& operator-=(const Vector4& rhs)
205 * @brief Multiplication operator.
207 * @param[in] rhs the vector to multiply
208 * @return A vector containing the result of the multiplication
210 Vector4 operator*(const Vector4& rhs) const
218 * @brief Multiplication operator.
220 * @param[in] rhs the float value to scale the vector
221 * @return A vector containing the result of the scaling
223 Vector4 operator*(float rhs) const
225 return Vector4(x * rhs, y * rhs, z * rhs, w * rhs);
229 * @brief Multiplication assignment operator.
231 * @param[in] rhs the vector to multiply
234 Vector4& operator*=(const Vector4& rhs)
245 * @brief Multiplication assignment operator.
247 * @param[in] rhs the float value to scale the vector
250 Vector4& operator*=(float rhs)
261 * @brief Division operator.
263 * @param[in] rhs the vector to divide
264 * @return A vector containing the result of the division
266 Vector4 operator/(const Vector4 & rhs) const
274 * @brief Division operator.
276 * @param[in] rhs The float value to scale the vector by
277 * @return A vector containing the result of the scaling
279 Vector4 operator/(float rhs) const
281 float oneOver = 1.0f / rhs;
282 return Vector4(x * oneOver, y * oneOver, z * oneOver, w * oneOver);
286 * @brief Division assignment operator.
288 * @param[in] rhs the vector to divide
291 Vector4& operator/=(const Vector4& rhs)
302 * @brief Division assignment operator.
304 * @param[in] rhs the float value to scale the vector by
307 Vector4& operator/=(float rhs)
309 const float oneOver = 1.0f / rhs;
319 * @brief Unary negation operator.
321 * @return the negative value
323 Vector4 operator-() const
325 Vector4 temp(-x, -y, -z, -w);
331 * @brief Equality operator.
333 * Utilises appropriate machine epsilon values.
335 * @param[in] rhs The vector to test against
336 * @return true if the vectors are equal
338 bool operator==(const Vector4 &rhs) const;
341 * @brief Inequality operator.
343 * Utilises appropriate machine epsilon values.
345 * @param[in] rhs The vector to test against
346 * @return true if the vectors are not equal
348 bool operator!=(const Vector4 &rhs) const
350 return !(*this == rhs);
354 * @brief Const array subscript operator overload.
356 * Asserts if index is out of range. Should be 0, 1, 2 or 3
357 * @param[in] index Subscript
358 * @return The float at the given index
360 const float& operator[](const unsigned int index) const
362 DALI_ASSERT_ALWAYS( index < 4 && "Vector element index out of bounds" );
364 return AsFloat()[index];
368 * @brief Mutable array subscript operator overload.
370 * Asserts if index is out of range. Should be 0, 1, 2 or 3
371 * @param[in] index Subscript index
372 * @return The float at the given index
374 float& operator[](const unsigned int index)
376 DALI_ASSERT_ALWAYS( index < 4 && "Vector element index out of bounds" );
378 return AsFloat()[index];
382 * @brief Returns the dot product of this vector (4d) and another vector (3d).
384 * The dot product is the length of one vector in the direction of another vector.
385 * This is great for lighting, threshold testing the angle between two unit vectors,
386 * calculating the distance between two points in a particular direction.
387 * @param [in] other the other vector
388 * @return the dot product
390 float Dot(const Vector3& other) const;
393 * @brief Returns the dot product of this vector and another vector.
395 * The dot product is the length of one vector in the direction of another vector.
396 * This is great for lighting, threshold testing the angle between two unit vectors,
397 * calculating the distance between two points in a particular direction.
398 * @param [in] other the other vector
399 * @return the dot product
401 float Dot(const Vector4& other) const;
404 * @brief Returns the 4d dot product of this vector and another vector.
406 * @param [in] other the other vector
407 * @return the dot product
409 float Dot4(const Vector4& other) const;
412 * @brief Returns the cross produce of this vector and another vector.
414 * The cross produce of two vectors is a vector which is perpendicular to the plane of the
415 * two vectors. This is great for calculating normals and making matrices orthogonal.
417 * @param [in] other the other vector
418 * @return A vector containing the cross product
420 Vector4 Cross(const Vector4& other) const;
423 * @brief Returns the length of the vector.
425 * @return the length.
427 float Length() const;
430 * @brief Returns the length of the vector squared.
432 * This is faster than using Length() when performing
433 * threshold checks as it avoids use of the square root.
434 * @return the length of the vector squared.
436 float LengthSquared() const;
439 * @brief Normalizes the vector.
441 * Sets the vector to unit length whilst maintaining its direction.
446 * @brief Clamps the vector between minimum and maximum vectors.
448 * @param [in] min the minimum vector
449 * @param [in] max the maximum vector
451 void Clamp( const Vector4& min, const Vector4& max );
454 * @brief Returns the contents of the vector as an array of 4 floats.
456 * The order of the values in this array are as follows:
461 * @note inlined for performance reasons (generates less code than a function call)
462 * @return the vector contents as an array of 4 floats.
464 const float* AsFloat() const {return &x;}
467 * @brief Returns the contents of the vector as an array of 4 floats.
469 * The order of the values in this array are as follows:
474 * @note inlined for performance reasons (generates less code than a function call)
475 * @return the vector contents as an array of 4 floats.
477 float* AsFloat() {return &x;}
482 // xrs, ygt, zbp and waq must be consecutive in memory.
483 // No other data must be added before xrs member.
484 // No virtual methods must be added to this struct.
488 float x; ///< x component
489 float r; ///< red component
490 float s; ///< s component
494 float y; ///< y component
495 float g; ///< green component
496 float t; ///< t component
500 float z; ///< z component
501 float b; ///< blue component
502 float p; ///< p component
506 float w; ///< w component
507 float a; ///< alpha component
508 float q; ///< q component
513 * @brief Print a Vector4.
515 * @param [in] o The output stream operator.
516 * @param [in] vector The vector to print.
517 * @return The output stream operator.
519 DALI_IMPORT_API std::ostream& operator<<(std::ostream& o, const Vector4& vector);
522 * @brief Returns a vector with components set to the minimum of the corresponding component in a and b.
524 * If a=0,1,2,3 and b=4,0,1,2 returns a vector of 0,0,1,2
525 * @param [in] a a vector
526 * @param [in] b a vector
527 * @return a vector containing the minimum of each component from a and b
529 inline Vector4 Min( const Vector4& a, const Vector4& b )
531 return Vector4( a.x < b.x ? a.x : b.x,
532 a.y < b.y ? a.y : b.y,
533 a.z < b.z ? a.z : b.z,
534 a.w < b.w ? a.w : b.w );
538 * @brief Returns a vector with components set to the maximum of the corresponding component in a and b.
540 * If a=0,1,2,3 and b=4,0,1,2 returns a vector of 4,1,2,3
541 * @param [in] a a vector
542 * @param [in] b a vector
543 * @return a vector containing the maximum of each component from a and b
545 inline Vector4 Max( const Vector4& a, const Vector4& b )
547 return Vector4( a.x > b.x ? a.x : b.x,
548 a.y > b.y ? a.y : b.y,
549 a.z > b.z ? a.z : b.z,
550 a.w > b.w ? a.w : b.w );
554 * @brief Clamps each of vector v's components between minimum and maximum values.
556 * @param [in] v a vector
557 * @param [in] min the minimum value
558 * @param [in] max the maximum value
559 * @return a vector containing the clamped components of v
561 DALI_IMPORT_API Vector4 Clamp( const Vector4& v, const float& min, const float& max );
565 #endif // __DALI_VECTOR_4_H__