1 #ifndef __DALI_VECTOR_4_H__
2 #define __DALI_VECTOR_4_H__
5 * Copyright (c) 2015 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>
26 #include <dali/public-api/common/type-traits.h>
31 * @addtogroup dali_core_math
39 * @brief A four dimensional vector.
41 * Components can be used as position or offset (x,y,z,w); color (r,g,b,a) or texture coords(s,t,p,q)
43 struct DALI_IMPORT_API Vector4
46 // xrs, ygt, zbp and waq must be consecutive in memory.
47 // No other data must be added before xrs member.
48 // No virtual methods must be added to this struct.
53 * @brief Default constructor, initializes the vector to 0.
64 * @brief Conversion constructor from four floats.
66 * @param [in] x (or r/s) component
67 * @param [in] y (or g/t) component
68 * @param [in] z (or b/p) component
69 * @param [in] w (or a/q) component
71 explicit Vector4(float x, float y, float z, float w)
81 * @brief Conversion constructor from an array of four floats.
83 * @param [in] array of either xyzw/rgba/stpq
85 explicit Vector4(const float* array)
94 * @brief Conversion constructor from Vector2.
96 * @param [in] vec2 to copy from, z and w are initialized to 0
98 explicit Vector4( const Vector2& vec2 );
101 * @brief Conversion constructor from Vector3.
103 * @param [in] vec3 to copy from, w is initialized to 0
105 explicit Vector4( const Vector3& vec3 );
108 static const Vector4 ONE; ///< (1.0f,1.0f,1.0f,1.0f)
109 static const Vector4 XAXIS; ///< (1.0f,0.0f,0.0f,0.0f)
110 static const Vector4 YAXIS; ///< (0.0f,1.0f,0.0f,0.0f)
111 static const Vector4 ZAXIS; ///< (0.0f,0.0f,1.0f,0.0f)
112 static const Vector4 ZERO; ///< (0.0f, 0.0f, 0.0f, 0.0f)
117 * @brief Assignment operator.
119 * @param [in] array of floats
122 Vector4& operator=(const float* array)
133 * @brief Assignment operator.
135 * Only sets x and y. z and w are left as they were
136 * @param [in] vec2 to assign from.
139 Vector4& operator=(const Vector2& vec2 );
142 * @brief Assignment operator.
144 * Only sets x and y and z. w is left as it was
145 * @param [in] vec3 to assign from
148 Vector4& operator=(const Vector3& vec3 );
151 * @brief Addition operator.
153 * @param[in] rhs vector to add.
154 * @return A vector containing the result of the addition
156 Vector4 operator+(const Vector4 & rhs) const
164 * @brief Addition assignment operator.
166 * @param[in] rhs vector to add.
169 Vector4& operator+=(const Vector4& rhs)
180 * @brief Subtraction operator.
182 * @param[in] rhs the vector to subtract
183 * @return A vector containing the result of the subtraction
185 Vector4 operator-(const Vector4& rhs) const
195 * @brief Subtraction assignment operator.
197 * @param[in] rhs the vector to subtract
200 Vector4& operator-=(const Vector4& rhs)
211 * @brief Multiplication operator.
213 * @param[in] rhs the vector to multiply
214 * @return A vector containing the result of the multiplication
216 Vector4 operator*(const Vector4& rhs) const
224 * @brief Multiplication operator.
226 * @param[in] rhs the float value to scale the vector
227 * @return A vector containing the result of the scaling
229 Vector4 operator*(float rhs) const
231 return Vector4(x * rhs, y * rhs, z * rhs, w * rhs);
235 * @brief Multiplication assignment operator.
237 * @param[in] rhs the vector to multiply
240 Vector4& operator*=(const Vector4& rhs)
251 * @brief Multiplication assignment operator.
253 * @param[in] rhs the float value to scale the vector
256 Vector4& operator*=(float rhs)
267 * @brief Division operator.
269 * @param[in] rhs the vector to divide
270 * @return A vector containing the result of the division
272 Vector4 operator/(const Vector4 & rhs) const
280 * @brief Division operator.
282 * @param[in] rhs The float value to scale the vector by
283 * @return A vector containing the result of the scaling
285 Vector4 operator/(float rhs) const
287 float oneOver = 1.0f / rhs;
288 return Vector4(x * oneOver, y * oneOver, z * oneOver, w * oneOver);
292 * @brief Division assignment operator.
294 * @param[in] rhs the vector to divide
297 Vector4& operator/=(const Vector4& rhs)
308 * @brief Division assignment operator.
310 * @param[in] rhs the float value to scale the vector by
313 Vector4& operator/=(float rhs)
315 const float oneOver = 1.0f / rhs;
325 * @brief Unary negation operator.
327 * @return the negative value
329 Vector4 operator-() const
331 Vector4 temp(-x, -y, -z, -w);
337 * @brief Equality operator.
339 * Utilises appropriate machine epsilon values.
341 * @param[in] rhs The vector to test against
342 * @return true if the vectors are equal
344 bool operator==(const Vector4 &rhs) const;
347 * @brief Inequality operator.
349 * Utilises appropriate machine epsilon values.
351 * @param[in] rhs The vector to test against
352 * @return true if the vectors are not equal
354 bool operator!=(const Vector4 &rhs) const
356 return !(*this == rhs);
360 * @brief Const array subscript operator overload.
362 * Asserts if index is out of range. Should be 0, 1, 2 or 3
363 * @param[in] index Subscript
364 * @return The float at the given index
366 const float& operator[](const unsigned int index) const
368 DALI_ASSERT_ALWAYS( index < 4 && "Vector element index out of bounds" );
370 return AsFloat()[index];
374 * @brief Mutable array subscript operator overload.
376 * Asserts if index is out of range. Should be 0, 1, 2 or 3
377 * @param[in] index Subscript index
378 * @return The float at the given index
380 float& operator[](const unsigned int index)
382 DALI_ASSERT_ALWAYS( index < 4 && "Vector element index out of bounds" );
384 return AsFloat()[index];
388 * @brief Returns the dot product of this vector (4d) and another vector (3d).
390 * The dot product is the length of one vector in the direction of another vector.
391 * This is great for lighting, threshold testing the angle between two unit vectors,
392 * calculating the distance between two points in a particular direction.
393 * @param [in] other the other vector
394 * @return the dot product
396 float Dot(const Vector3& other) const;
399 * @brief Returns the dot product of this vector and another vector.
401 * The dot product is the length of one vector in the direction of another vector.
402 * This is great for lighting, threshold testing the angle between two unit vectors,
403 * calculating the distance between two points in a particular direction.
404 * @param [in] other the other vector
405 * @return the dot product
407 float Dot(const Vector4& other) const;
410 * @brief Returns the 4d dot product of this vector and another vector.
412 * @param [in] other the other vector
413 * @return the dot product
415 float Dot4(const Vector4& other) const;
418 * @brief Returns the cross produce of this vector and another vector.
420 * The cross produce of two vectors is a vector which is perpendicular to the plane of the
421 * two vectors. This is great for calculating normals and making matrices orthogonal.
423 * @param [in] other the other vector
424 * @return A vector containing the cross product
426 Vector4 Cross(const Vector4& other) const;
429 * @brief Returns the length of the vector.
431 * @return the length.
433 float Length() const;
436 * @brief Returns the length of the vector squared.
438 * This is faster than using Length() when performing
439 * threshold checks as it avoids use of the square root.
440 * @return the length of the vector squared.
442 float LengthSquared() const;
445 * @brief Normalizes the vector.
447 * Sets the vector to unit length whilst maintaining its direction.
452 * @brief Clamps the vector between minimum and maximum vectors.
454 * @param [in] min the minimum vector
455 * @param [in] max the maximum vector
457 void Clamp( const Vector4& min, const Vector4& max );
460 * @brief Returns the contents of the vector as an array of 4 floats.
462 * The order of the values in this array are as follows:
467 * @note inlined for performance reasons (generates less code than a function call)
468 * @return the vector contents as an array of 4 floats.
470 const float* AsFloat() const {return &x;}
473 * @brief Returns the contents of the vector as an array of 4 floats.
475 * The order of the values in this array are as follows:
480 * @note inlined for performance reasons (generates less code than a function call)
481 * @return the vector contents as an array of 4 floats.
483 float* AsFloat() {return &x;}
488 // xrs, ygt, zbp and waq must be consecutive in memory.
489 // No other data must be added before xrs member.
490 // No virtual methods must be added to this struct.
494 float x; ///< x component
495 float r; ///< red component
496 float s; ///< s component
500 float y; ///< y component
501 float g; ///< green component
502 float t; ///< t component
506 float z; ///< z component
507 float b; ///< blue component
508 float p; ///< p component
512 float w; ///< w component
513 float a; ///< alpha component
514 float q; ///< q component
519 * @brief Print a Vector4.
521 * @param [in] o The output stream operator.
522 * @param [in] vector The vector to print.
523 * @return The output stream operator.
525 DALI_IMPORT_API std::ostream& operator<<(std::ostream& o, const Vector4& vector);
528 * @brief Returns a vector with components set to the minimum of the corresponding component in a and b.
530 * If a=0,1,2,3 and b=4,0,1,2 returns a vector of 0,0,1,2
531 * @param [in] a a vector
532 * @param [in] b a vector
533 * @return a vector containing the minimum of each component from a and b
535 inline Vector4 Min( const Vector4& a, const Vector4& b )
537 return Vector4( a.x < b.x ? a.x : b.x,
538 a.y < b.y ? a.y : b.y,
539 a.z < b.z ? a.z : b.z,
540 a.w < b.w ? a.w : b.w );
544 * @brief Returns a vector with components set to the maximum of the corresponding component in a and b.
546 * If a=0,1,2,3 and b=4,0,1,2 returns a vector of 4,1,2,3
547 * @param [in] a a vector
548 * @param [in] b a vector
549 * @return a vector containing the maximum of each component from a and b
551 inline Vector4 Max( const Vector4& a, const Vector4& b )
553 return Vector4( a.x > b.x ? a.x : b.x,
554 a.y > b.y ? a.y : b.y,
555 a.z > b.z ? a.z : b.z,
556 a.w > b.w ? a.w : b.w );
560 * @brief Clamps each of vector v's components between minimum and maximum values.
562 * @param [in] v a vector
563 * @param [in] min the minimum value
564 * @param [in] max the maximum value
565 * @return a vector containing the clamped components of v
567 DALI_IMPORT_API Vector4 Clamp( const Vector4& v, const float& min, const float& max );
569 // Allow Vector4 to be treated as a POD type
570 template <> struct TypeTraits< Vector4 > : public BasicTypes< Vector4 > { enum { IS_TRIVIAL_TYPE = true }; };
577 #endif // __DALI_VECTOR_4_H__