1 #ifndef __DALI_VECTOR_3_H__
2 #define __DALI_VECTOR_3_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.
22 * @addtogroup CAPI_DALI_MATH_MODULE
31 #include <dali/public-api/math/math-utils.h>
32 #include <dali/public-api/common/constants.h>
34 namespace Dali DALI_IMPORT_API
42 * @brief A three dimensional vector.
44 struct DALI_IMPORT_API Vector3
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.
65 * @param [in] x (or width) component
66 * @param [in] y (or height) component
67 * @param [in] z (or depth) component
69 explicit Vector3(float x, float y, float z)
77 * @brief Conversion constructor from an array of three floats.
79 * @param [in] array of xyz
81 explicit Vector3(const float* array)
91 * @param [in] vec2 Vector2 to create this vector from
93 explicit Vector3( const Vector2& vec2 );
98 * @param [in] vec4 Vector4 to create this vector from
100 explicit Vector3( const Vector4& vec4 );
104 static const Vector3 ONE; ///< (1.0f,1.0f,1.0f)
105 static const Vector3 XAXIS; ///< Vector representing the X axis
106 static const Vector3 YAXIS; ///< Vector representing the Y axis
107 static const Vector3 ZAXIS; ///< Vector representing the Z axis
108 static const Vector3 NEGATIVE_XAXIS; ///< Vector representing the negative X axis
109 static const Vector3 NEGATIVE_YAXIS; ///< Vector representing the negative Y axis
110 static const Vector3 NEGATIVE_ZAXIS; ///< Vector representing the negative Z axis
111 static const Vector3 ZERO; ///< (0.0f, 0.0f, 0.0f)
116 * @brief Assignment operator.
118 * @param[in] array of floats
121 Vector3& operator=(const float* array)
131 * @brief Assignment operator.
133 * @param[in] rhs vector to assign.
136 Vector3& operator=(const Vector2& rhs);
139 * @brief Assignment operator.
141 * @param[in] rhs vector to assign.
144 Vector3& operator=(const Vector4& rhs);
147 * @brief Addition operator.
149 * @param[in] rhs vector to add.
150 * @return A vector containing the result of the addition
152 Vector3 operator+(const Vector3& rhs) const
160 * @brief Addition assignment operator.
162 * @param[in] rhs vector to add.
165 Vector3& operator+=(const Vector3& rhs)
175 * @brief Subtraction operator.
177 * @param[in] rhs the vector to subtract
178 * @return A vector containing the result of the subtraction
180 Vector3 operator-(const Vector3& rhs) const
188 * @brief Subtraction assignment operator.
190 * @param[in] rhs the vector to subtract
193 Vector3& operator-=(const Vector3& rhs)
203 * @brief Multiplication operator.
205 * @param[in] rhs the vector to multiply
206 * @return A vector containing the result of the multiplication
208 Vector3 operator*(const Vector3& rhs) const
216 * @brief Multiplication operator.
218 * @param[in] rhs the float value to scale the vector
219 * @return A vector containing the result of the scaling
221 Vector3 operator*(float rhs) const
223 return Vector3(x * rhs, y * rhs, z * rhs);
227 * @brief Multiplication assignment operator.
229 * @param[in] rhs the vector to multiply
232 Vector3& operator*=(const Vector3& rhs)
242 * @brief Multiplication assignment operator.
244 * @param[in] rhs the float value to scale the vector
247 Vector3& operator*=(float rhs)
257 * @brief Multiplication assignment operator.
259 * @param[in] rhs the Quaternion value to multiply the vector by
262 Vector3& operator*=(const Quaternion& rhs);
265 * @brief Division operator.
267 * @param[in] rhs the vector to divide
268 * @return A vector containing the result of the division
270 Vector3 operator/(const Vector3& rhs) const
278 * @brief Division operator.
280 * @param[in] rhs The float value to scale the vector by
281 * @return A vector containing the result of the scaling
283 Vector3 operator/(float rhs) const
285 return Vector3(x / rhs, y / rhs, z / rhs);
289 * @brief Division assignment operator.
291 * @param[in] rhs the vector to divide
294 Vector3& operator/=(const Vector3& rhs)
304 * @brief Division assignment operator.
306 * @param[in] rhs the float value to scale the vector by
309 Vector3& operator/=(float rhs)
311 float oneOverRhs = 1.0f / rhs;
320 * @brief Unary negation operator.
322 * @return A vector containg the negation
324 Vector3 operator-() const
326 Vector3 temp(-x, -y, -z);
332 * @brief Equality operator.
334 * utilises appropriate machine epsilon values;
336 * @param[in] rhs The vector to test against
337 * @return true if the vectors are equal
339 bool operator==(const Vector3& rhs) const;
342 * @brief Inequality operator.
344 * utilises appropriate machine epsilon values;
346 * @param[in] rhs The vector to test against
347 * @return true if the vectors are not equal
349 bool operator!=(const Vector3& rhs) const
351 return !(*this == rhs);
355 * @brief Const array subscript operator overload.
357 * Asserts if index is out of range. Should be 0, 1 or 2
358 * @param[in] index Subscript
359 * @return The float at the given index.
361 const float& operator[](const unsigned int index) const
363 DALI_ASSERT_ALWAYS( index < 3 && "Vector element index out of bounds" );
365 return AsFloat()[index];
369 * @brief Mutable array subscript operator overload.
371 * Asserts if index is out of range. Should be 0, 1 or 2
372 * @param[in] index Subscript index
373 * @return The float at the given index.
375 float& operator[](const unsigned int index)
377 DALI_ASSERT_ALWAYS( index < 3 && "Vector element index out of bounds" );
379 return AsFloat()[index];
383 * @brief Returns the dot product of this vector and another vector.
385 * The dot product is the length of one vector in the direction of another vector.
386 * This is great for lighting, threshold testing the angle between two unit vectors,
387 * calculating the distance between two points in a particular direction.
388 * @param [in] other the other vector
389 * @return the dot product
391 float Dot(const Vector3& other) const;
394 * @brief Returns the cross produce of this vector and another vector.
396 * The cross produce of two vectors is a vector which is perpendicular to the plane of the
397 * two vectors. This is great for calculating normals and making matrices orthogonal.
399 * @param [in] other the other vector
400 * @return the cross product
402 Vector3 Cross(const Vector3& other) const;
405 * @brief Returns the length of the vector.
407 * @return the length of the vector
409 float Length() const;
412 * @brief Returns the length of the vector squared.
414 * This is more efficient than Length() for threshold
415 * testing as it avoids the use of a square root.
416 * @return the length of the vector squared.
418 float LengthSquared() const;
421 * @brief Sets the vector to be unit length, whilst maintaining its direction.
427 * @brief Clamps the vector between minimum and maximum vectors.
429 * @param [in] min the minimum vector
430 * @param [in] max the maximum vector
432 void Clamp( const Vector3& min, const Vector3& max );
435 * @brief Returns the contents of the vector as an array of 3 floats.
437 * The order of the values in this array are as follows:
438 * 0: x (or width, or r)
439 * 1: y (or height, or g)
440 * 2: z (or depth, or b)
441 * @note inlined for performance reasons (generates less code than a function call)
442 * @return the vector contents as an array of 3 floats.
444 const float* AsFloat() const {return &x;}
447 * @brief Returns the contents of the vector as an array of 3 floats.
449 * The order of the values in this array are as follows:
450 * 0: x (or width, or r)
451 * 1: y (or height, or g)
452 * 2: z (or depth, or b)
453 * @note inlined for performance reasons (generates less code than a function call)
454 * @return the vector contents as an array of 3 floats.
456 float* AsFloat() {return &x;}
459 * @brief Returns the x & y components (or width & height, or r & g) as a Vector2.
461 * @note inlined for performance reasons (generates less code than a function call)
462 * @return the partial vector contents as Vector2 (x,y)
464 const Vector2& GetVectorXY() const {return reinterpret_cast<const Vector2&>(x);}
467 * @brief Returns the x & y components (or width & height, or r & g) as a Vector2.
469 * @note inlined for performance reasons (generates less code than a function call)
470 * @return the partial vector contents as Vector2 (x,y)
472 Vector2& GetVectorXY() {return reinterpret_cast<Vector2&>(x);}
475 * @brief Returns the y & z components (or height & depth, or g & b) as a Vector2.
477 * @note inlined for performance reasons (generates less code than a function call)
478 * @return the partial vector contents as Vector2 (y,z)
480 const Vector2& GetVectorYZ() const {return reinterpret_cast<const Vector2&>(y);}
483 * @brief Returns the y & z components (or height & depth, or g & b) as a Vector2.
485 * @note inlined for performance reasons (generates less code than a function call)
486 * @return the partial vector contents as Vector2 (y,z)
488 Vector2& GetVectorYZ() {return reinterpret_cast<Vector2&>(y);}
493 // (x width r), (y height g), (z depth b) must be consecutive in memory.
494 // No other data must be added before (x width r) member.
495 // No virtual methods must be added to this struct.
498 float x; ///< x component
499 float width; ///< width component
500 float r; ///< red component
504 float y; ///< y component
505 float height; ///< height component
506 float g; ///< green component
510 float z; ///< z component
511 float depth; ///< depth component
512 float b; ///< blue component
517 * @brief Print a Vector3.
519 * @param [in] o The output stream operator.
520 * @param [in] vector The vector to print.
521 * @return The output stream operator.
523 DALI_IMPORT_API std::ostream& operator<< (std::ostream& o, const Vector3& vector);
526 * @brief Returns a vector with components set to the minimum of the corresponding component in a and b.
528 * If a=0,1,2 and b=2,1,0 returns a vector of 2,1,2.
529 * @param [in] a a vector
530 * @param [in] b a vector
531 * @return a vector containing the minimum of each component from a and b
533 inline Vector3 Min( const Vector3& a, const Vector3& b )
535 return Vector3( std::min(a.x,b.x), std::min(a.y,b.y), std::min(a.z,b.z) );
539 * @brief Returns a vector with components set to the maximum of the corresponding component in a and b.
541 * If a=0,1 and b=1,0 returns a vector of 1,1
542 * @param [in] a a vector
543 * @param [in] b a vector
544 * @return a vector containing the maximum of each component from a and b
546 inline Vector3 Max( const Vector3& a, const Vector3& b )
548 return Vector3( std::max(a.x,b.x), std::max(a.y,b.y), std::max(a.z,b.z) );
552 * @brief Clamps each of vector v's components between minimum and maximum values.
554 * @param [in] v a vector
555 * @param [in] min the minimum value
556 * @param [in] max the maximum value
557 * @return a vector containing the clamped components of v
559 DALI_IMPORT_API Vector3 Clamp( const Vector3& v, const float& min, const float& max );
562 * @brief Scales an Actor, such that it fits within its Parent's Size Keeping the aspect ratio.
564 * f(target, source) = Vector3( min( target.X / source.X, min( target.Y / source.Y, target.Z / source.Z ) )
565 * If any of the source dimensions is zero it will be ignored in the calculation
567 * @param [in] target size
568 * @param [in] source size
569 * @return target scaled inside source
571 DALI_IMPORT_API Vector3 FitKeepAspectRatio( const Vector3& target, const Vector3& source );
574 * @brief Scales an Actor, such that it fill its Parent's Size Keeping the aspect ratio.
576 * f(target, source) = Vector3( max( target.X / source.X, max( target.Y / source.Y, target.Z / source.Z ) )
577 * If any of the source dimensions is zero it will be ignored in the calculation
579 * @param [in] target size
580 * @param [in] source size
581 * @return target scaled inside source
583 DALI_IMPORT_API Vector3 FillKeepAspectRatio( const Vector3& target, const Vector3& source );
586 * @brief Scales an Actor, such that it fill its Parent's Size in the X and Y coordinates Keeping the aspect ratio.
588 * f(target, source) = Vector3( max( target.X / sizeX, target.Y / sizeY ) )
589 * If any of the source dimensions is zero it will be ignored in the calculation
591 * @param [in] target size
592 * @param [in] source size
593 * @return target scaled inside source
595 DALI_IMPORT_API Vector3 FillXYKeepAspectRatio( const Vector3& target, const Vector3& source );
598 * @brief Shrinks source size inside the target size maintaining aspect ratio of source.
600 * If source is smaller than target it returns source
601 * @pre source width and height > 0
602 * @param [in] target size
603 * @param [in] source size
604 * @return target scaled inside source
606 Vector3 ShrinkInsideKeepAspectRatio( const Vector3& target, const Vector3& source );
614 #endif // __DALI_VECTOR_3_H__