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.
27 #include <dali/public-api/common/dali-common.h>
35 * @brief A four dimensional vector.
37 * Components can be used as position or offset (x,y,z,w); color (r,g,b,a) or texture coords(s,t,p,q)
39 struct DALI_IMPORT_API Vector4
42 // xrs, ygt, zbp and waq must be consecutive in memory.
43 // No other data must be added before xrs member.
44 // No virtual methods must be added to this struct.
49 * @brief Default constructor, initializes the vector to 0.
61 * @brief Conversion constructor from four floats.
63 * @param [in] x (or r/s) component
64 * @param [in] y (or g/t) component
65 * @param [in] z (or b/p) component
66 * @param [in] w (or a/q) component
68 explicit Vector4(float x, float y, float z, float w)
78 * @brief Conversion constructor from an array of four floats.
80 * @param [in] array of either xyzw/rgba/stpq
82 explicit Vector4(const float* array)
91 * @brief Conversion constructor from Vector2.
93 * @param [in] vec2 to copy from, z and w are initialized to 0
95 explicit Vector4( const Vector2& vec2 );
98 * @brief Conversion constructor from Vector3.
100 * @param [in] vec3 to copy from, w is initialized to 0
102 explicit Vector4( const Vector3& vec3 );
105 static const Vector4 ONE; ///< (1.0f,1.0f,1.0f,1.0f)
106 static const Vector4 XAXIS; ///< (1.0f,0.0f,0.0f,0.0f)
107 static const Vector4 YAXIS; ///< (0.0f,1.0f,0.0f,0.0f)
108 static const Vector4 ZAXIS; ///< (0.0f,0.0f,1.0f,0.0f)
109 static const Vector4 ZERO; ///< (0.0f, 0.0f, 0.0f, 0.0f)
114 * @brief Assignment operator.
116 * @param [in] array of floats
119 Vector4& operator=(const float* array)
130 * @brief Assignment operator.
132 * Only sets x and y. z and w are left as they were
133 * @param [in] vec2 to assign from.
136 Vector4& operator=(const Vector2& vec2 );
139 * @brief Assignment operator.
141 * Only sets x and y and z. w is left as it was
142 * @param [in] vec3 to assign from
145 Vector4& operator=(const Vector3& vec3 );
148 * @brief Addition operator.
150 * @param[in] rhs vector to add.
151 * @return A vector containing the result of the addition
153 Vector4 operator+(const Vector4 & rhs) const
161 * @brief Addition assignment operator.
163 * @param[in] rhs vector to add.
166 Vector4& operator+=(const Vector4& rhs)
177 * @brief Subtraction operator.
179 * @param[in] rhs the vector to subtract
180 * @return A vector containing the result of the subtraction
182 Vector4 operator-(const Vector4& rhs) const
192 * @brief Subtraction assignment operator.
194 * @param[in] rhs the vector to subtract
197 Vector4& operator-=(const Vector4& rhs)
208 * @brief Multiplication operator.
210 * @param[in] rhs the vector to multiply
211 * @return A vector containing the result of the multiplication
213 Vector4 operator*(const Vector4& rhs) const
221 * @brief Multiplication operator.
223 * @param[in] rhs the float value to scale the vector
224 * @return A vector containing the result of the scaling
226 Vector4 operator*(float rhs) const
228 return Vector4(x * rhs, y * rhs, z * rhs, w * rhs);
232 * @brief Multiplication assignment operator.
234 * @param[in] rhs the vector to multiply
237 Vector4& operator*=(const Vector4& rhs)
248 * @brief Multiplication assignment operator.
250 * @param[in] rhs the float value to scale the vector
253 Vector4& operator*=(float rhs)
264 * @brief Division operator.
266 * @param[in] rhs the vector to divide
267 * @return A vector containing the result of the division
269 Vector4 operator/(const Vector4 & rhs) const
277 * @brief Division operator.
279 * @param[in] rhs The float value to scale the vector by
280 * @return A vector containing the result of the scaling
282 Vector4 operator/(float rhs) const
284 float oneOver = 1.0f / rhs;
285 return Vector4(x * oneOver, y * oneOver, z * oneOver, w * oneOver);
289 * @brief Division assignment operator.
291 * @param[in] rhs the vector to divide
294 Vector4& operator/=(const Vector4& rhs)
305 * @brief Division assignment operator.
307 * @param[in] rhs the float value to scale the vector by
310 Vector4& operator/=(float rhs)
312 const float oneOver = 1.0f / rhs;
322 * @brief Unary negation operator.
324 * @return the negative value
326 Vector4 operator-() const
328 Vector4 temp(-x, -y, -z, -w);
334 * @brief Equality operator.
336 * Utilises appropriate machine epsilon values.
338 * @param[in] rhs The vector to test against
339 * @return true if the vectors are equal
341 bool operator==(const Vector4 &rhs) const;
344 * @brief Inequality operator.
346 * Utilises appropriate machine epsilon values.
348 * @param[in] rhs The vector to test against
349 * @return true if the vectors are not equal
351 bool operator!=(const Vector4 &rhs) const
353 return !(*this == rhs);
357 * @brief Const array subscript operator overload.
359 * Asserts if index is out of range. Should be 0, 1, 2 or 3
360 * @param[in] index Subscript
361 * @return The float at the given index
363 const float& operator[](const unsigned int index) const
365 DALI_ASSERT_ALWAYS( index < 4 && "Vector element index out of bounds" );
367 return AsFloat()[index];
371 * @brief Mutable array subscript operator overload.
373 * Asserts if index is out of range. Should be 0, 1, 2 or 3
374 * @param[in] index Subscript index
375 * @return The float at the given index
377 float& operator[](const unsigned int index)
379 DALI_ASSERT_ALWAYS( index < 4 && "Vector element index out of bounds" );
381 return AsFloat()[index];
385 * @brief Returns the dot product of this vector (4d) and another vector (3d).
387 * The dot product is the length of one vector in the direction of another vector.
388 * This is great for lighting, threshold testing the angle between two unit vectors,
389 * calculating the distance between two points in a particular direction.
390 * @param [in] other the other vector
391 * @return the dot product
393 float Dot(const Vector3& other) const;
396 * @brief Returns the dot product of this vector and another vector.
398 * The dot product is the length of one vector in the direction of another vector.
399 * This is great for lighting, threshold testing the angle between two unit vectors,
400 * calculating the distance between two points in a particular direction.
401 * @param [in] other the other vector
402 * @return the dot product
404 float Dot(const Vector4& other) const;
407 * @brief Returns the 4d dot product of this vector and another vector.
409 * @param [in] other the other vector
410 * @return the dot product
412 float Dot4(const Vector4& other) const;
415 * @brief Returns the cross produce of this vector and another vector.
417 * The cross produce of two vectors is a vector which is perpendicular to the plane of the
418 * two vectors. This is great for calculating normals and making matrices orthogonal.
420 * @param [in] other the other vector
421 * @return A vector containing the cross product
423 Vector4 Cross(const Vector4& other) const;
426 * @brief Returns the length of the vector.
428 * @return the length.
430 float Length() const;
433 * @brief Returns the length of the vector squared.
435 * This is faster than using Length() when performing
436 * threshold checks as it avoids use of the square root.
437 * @return the length of the vector squared.
439 float LengthSquared() const;
442 * @brief Normalizes the vector.
444 * Sets the vector to unit length whilst maintaining its direction.
449 * @brief Clamps the vector between minimum and maximum vectors.
451 * @param [in] min the minimum vector
452 * @param [in] max the maximum vector
454 void Clamp( const Vector4& min, const Vector4& max );
457 * @brief Returns the contents of the vector as an array of 4 floats.
459 * The order of the values in this array are as follows:
464 * @note inlined for performance reasons (generates less code than a function call)
465 * @return the vector contents as an array of 4 floats.
467 const float* AsFloat() const {return &x;}
470 * @brief Returns the contents of the vector as an array of 4 floats.
472 * The order of the values in this array are as follows:
477 * @note inlined for performance reasons (generates less code than a function call)
478 * @return the vector contents as an array of 4 floats.
480 float* AsFloat() {return &x;}
485 // xrs, ygt, zbp and waq must be consecutive in memory.
486 // No other data must be added before xrs member.
487 // No virtual methods must be added to this struct.
491 float x; ///< x component
492 float r; ///< red component
493 float s; ///< s component
497 float y; ///< y component
498 float g; ///< green component
499 float t; ///< t component
503 float z; ///< z component
504 float b; ///< blue component
505 float p; ///< p component
509 float w; ///< w component
510 float a; ///< alpha component
511 float q; ///< q component
516 * @brief Print a Vector4.
518 * @param [in] o The output stream operator.
519 * @param [in] vector The vector to print.
520 * @return The output stream operator.
522 DALI_IMPORT_API std::ostream& operator<<(std::ostream& o, const Vector4& vector);
525 * @brief Returns a vector with components set to the minimum of the corresponding component in a and b.
527 * If a=0,1,2,3 and b=4,0,1,2 returns a vector of 0,0,1,2
528 * @param [in] a a vector
529 * @param [in] b a vector
530 * @return a vector containing the minimum of each component from a and b
532 inline Vector4 Min( const Vector4& a, const Vector4& b )
534 return Vector4( std::min(a.x,b.x), std::min(a.y,b.y), std::min(a.z,b.z), std::min(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( std::max(a.x,b.x), std::max(a.y,b.y), std::max(a.z,b.z), std::max(a.w,b.w) );
551 * @brief Clamps each of vector v's components between minimum and maximum values.
553 * @param [in] v a vector
554 * @param [in] min the minimum value
555 * @param [in] max the maximum value
556 * @return a vector containing the clamped components of v
558 DALI_IMPORT_API Vector4 Clamp( const Vector4& v, const float& min, const float& max );
562 #endif // __DALI_VECTOR_4_H__