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.
26 #include <dali/public-api/math/math-utils.h>
27 #include <dali/public-api/common/constants.h>
29 namespace Dali DALI_IMPORT_API
37 * @brief A three dimensional vector.
39 struct DALI_IMPORT_API Vector3
47 // (x width r), (y height g), (z depth b) must be consecutive in memory.
48 // No other data must be added before (x width r) member.
49 // No virtual methods must be added to this struct.
60 * @param [in] x (or width) component
61 * @param [in] y (or height) component
62 * @param [in] z (or depth) component
64 explicit Vector3(float x, float y, float z)
72 * @brief Conversion constructor from an array of three floats.
74 * @param [in] array of xyz
76 explicit Vector3(const float* array)
86 * @param [in] vec2 Vector2 to create this vector from
88 explicit Vector3( const Vector2& vec2 );
93 * @param [in] vec4 Vector4 to create this vector from
95 explicit Vector3( const Vector4& vec4 );
99 static const Vector3 ONE; ///< (1.0f,1.0f,1.0f)
100 static const Vector3 XAXIS; ///< Vector representing the X axis
101 static const Vector3 YAXIS; ///< Vector representing the Y axis
102 static const Vector3 ZAXIS; ///< Vector representing the Z axis
103 static const Vector3 NEGATIVE_XAXIS; ///< Vector representing the negative X axis
104 static const Vector3 NEGATIVE_YAXIS; ///< Vector representing the negative Y axis
105 static const Vector3 NEGATIVE_ZAXIS; ///< Vector representing the negative Z axis
106 static const Vector3 ZERO; ///< (0.0f, 0.0f, 0.0f)
111 * @brief Assignment operator.
113 * @param[in] array of floats
116 Vector3& operator=(const float* array)
126 * @brief Assignment operator.
128 * @param[in] rhs vector to assign.
131 Vector3& operator=(const Vector2& rhs);
134 * @brief Assignment operator.
136 * @param[in] rhs vector to assign.
139 Vector3& operator=(const Vector4& rhs);
142 * @brief Addition operator.
144 * @param[in] rhs vector to add.
145 * @return A vector containing the result of the addition
147 Vector3 operator+(const Vector3& rhs) const
155 * @brief Addition assignment operator.
157 * @param[in] rhs vector to add.
160 Vector3& operator+=(const Vector3& rhs)
170 * @brief Subtraction operator.
172 * @param[in] rhs the vector to subtract
173 * @return A vector containing the result of the subtraction
175 Vector3 operator-(const Vector3& rhs) const
183 * @brief Subtraction assignment operator.
185 * @param[in] rhs the vector to subtract
188 Vector3& operator-=(const Vector3& rhs)
198 * @brief Multiplication operator.
200 * @param[in] rhs the vector to multiply
201 * @return A vector containing the result of the multiplication
203 Vector3 operator*(const Vector3& rhs) const
211 * @brief Multiplication operator.
213 * @param[in] rhs the float value to scale the vector
214 * @return A vector containing the result of the scaling
216 Vector3 operator*(float rhs) const
218 return Vector3(x * rhs, y * rhs, z * rhs);
222 * @brief Multiplication assignment operator.
224 * @param[in] rhs the vector to multiply
227 Vector3& operator*=(const Vector3& rhs)
237 * @brief Multiplication assignment operator.
239 * @param[in] rhs the float value to scale the vector
242 Vector3& operator*=(float rhs)
252 * @brief Multiplication assignment operator.
254 * @param[in] rhs the Quaternion value to multiply the vector by
257 Vector3& operator*=(const Quaternion& rhs);
260 * @brief Division operator.
262 * @param[in] rhs the vector to divide
263 * @return A vector containing the result of the division
265 Vector3 operator/(const Vector3& rhs) const
273 * @brief Division operator.
275 * @param[in] rhs The float value to scale the vector by
276 * @return A vector containing the result of the scaling
278 Vector3 operator/(float rhs) const
280 return Vector3(x / rhs, y / rhs, z / rhs);
284 * @brief Division assignment operator.
286 * @param[in] rhs the vector to divide
289 Vector3& operator/=(const Vector3& rhs)
299 * @brief Division assignment operator.
301 * @param[in] rhs the float value to scale the vector by
304 Vector3& operator/=(float rhs)
306 float oneOverRhs = 1.0f / rhs;
315 * @brief Unary negation operator.
317 * @return A vector containg the negation
319 Vector3 operator-() const
321 Vector3 temp(-x, -y, -z);
327 * @brief Equality operator.
329 * utilises appropriate machine epsilon values;
331 * @param[in] rhs The vector to test against
332 * @return true if the vectors are equal
334 bool operator==(const Vector3& rhs) const;
337 * @brief Inequality operator.
339 * utilises appropriate machine epsilon values;
341 * @param[in] rhs The vector to test against
342 * @return true if the vectors are not equal
344 bool operator!=(const Vector3& rhs) const
346 return !(*this == rhs);
350 * @brief Const array subscript operator overload.
352 * Asserts if index is out of range. Should be 0, 1 or 2
353 * @param[in] index Subscript
354 * @return The float at the given index.
356 const float& operator[](const unsigned int index) const
358 DALI_ASSERT_ALWAYS( index < 3 && "Vector element index out of bounds" );
360 return AsFloat()[index];
364 * @brief Mutable array subscript operator overload.
366 * Asserts if index is out of range. Should be 0, 1 or 2
367 * @param[in] index Subscript index
368 * @return The float at the given index.
370 float& operator[](const unsigned int index)
372 DALI_ASSERT_ALWAYS( index < 3 && "Vector element index out of bounds" );
374 return AsFloat()[index];
378 * @brief Returns the dot product of this vector and another vector.
380 * The dot product is the length of one vector in the direction of another vector.
381 * This is great for lighting, threshold testing the angle between two unit vectors,
382 * calculating the distance between two points in a particular direction.
383 * @param [in] other the other vector
384 * @return the dot product
386 float Dot(const Vector3& other) const;
389 * @brief Returns the cross produce of this vector and another vector.
391 * The cross produce of two vectors is a vector which is perpendicular to the plane of the
392 * two vectors. This is great for calculating normals and making matrices orthogonal.
394 * @param [in] other the other vector
395 * @return the cross product
397 Vector3 Cross(const Vector3& other) const;
400 * @brief Returns the length of the vector.
402 * @return the length of the vector
404 float Length() const;
407 * @brief Returns the length of the vector squared.
409 * This is more efficient than Length() for threshold
410 * testing as it avoids the use of a square root.
411 * @return the length of the vector squared.
413 float LengthSquared() const;
416 * @brief Sets the vector to be unit length, whilst maintaining its direction.
422 * @brief Clamps the vector between minimum and maximum vectors.
424 * @param [in] min the minimum vector
425 * @param [in] max the maximum vector
427 void Clamp( const Vector3& min, const Vector3& max );
430 * @brief Returns the contents of the vector as an array of 3 floats.
432 * The order of the values in this array are as follows:
433 * 0: x (or width, or r)
434 * 1: y (or height, or g)
435 * 2: z (or depth, or b)
436 * @note inlined for performance reasons (generates less code than a function call)
437 * @return the vector contents as an array of 3 floats.
439 const float* AsFloat() const {return &x;}
442 * @brief Returns the contents of the vector as an array of 3 floats.
444 * The order of the values in this array are as follows:
445 * 0: x (or width, or r)
446 * 1: y (or height, or g)
447 * 2: z (or depth, or b)
448 * @note inlined for performance reasons (generates less code than a function call)
449 * @return the vector contents as an array of 3 floats.
451 float* AsFloat() {return &x;}
454 * @brief Returns the x & y components (or width & height, or r & g) as a Vector2.
456 * @note inlined for performance reasons (generates less code than a function call)
457 * @return the partial vector contents as Vector2 (x,y)
459 const Vector2& GetVectorXY() const {return reinterpret_cast<const Vector2&>(x);}
462 * @brief Returns the x & y components (or width & height, or r & g) as a Vector2.
464 * @note inlined for performance reasons (generates less code than a function call)
465 * @return the partial vector contents as Vector2 (x,y)
467 Vector2& GetVectorXY() {return reinterpret_cast<Vector2&>(x);}
470 * @brief Returns the y & z components (or height & depth, or g & b) as a Vector2.
472 * @note inlined for performance reasons (generates less code than a function call)
473 * @return the partial vector contents as Vector2 (y,z)
475 const Vector2& GetVectorYZ() const {return reinterpret_cast<const Vector2&>(y);}
478 * @brief Returns the y & z components (or height & depth, or g & b) as a Vector2.
480 * @note inlined for performance reasons (generates less code than a function call)
481 * @return the partial vector contents as Vector2 (y,z)
483 Vector2& GetVectorYZ() {return reinterpret_cast<Vector2&>(y);}
488 // (x width r), (y height g), (z depth b) must be consecutive in memory.
489 // No other data must be added before (x width r) member.
490 // No virtual methods must be added to this struct.
493 float x; ///< x component
494 float width; ///< width component
495 float r; ///< red component
499 float y; ///< y component
500 float height; ///< height component
501 float g; ///< green component
505 float z; ///< z component
506 float depth; ///< depth component
507 float b; ///< blue component
512 * @brief Print a Vector3.
514 * @param [in] o The output stream operator.
515 * @param [in] vector The vector to print.
516 * @return The output stream operator.
518 DALI_IMPORT_API std::ostream& operator<< (std::ostream& o, const Vector3& vector);
521 * @brief Returns a vector with components set to the minimum of the corresponding component in a and b.
523 * If a=0,1,2 and b=2,1,0 returns a vector of 2,1,2.
524 * @param [in] a a vector
525 * @param [in] b a vector
526 * @return a vector containing the minimum of each component from a and b
528 inline Vector3 Min( const Vector3& a, const Vector3& b )
530 return Vector3( std::min(a.x,b.x), std::min(a.y,b.y), std::min(a.z,b.z) );
534 * @brief Returns a vector with components set to the maximum of the corresponding component in a and b.
536 * If a=0,1 and b=1,0 returns a vector of 1,1
537 * @param [in] a a vector
538 * @param [in] b a vector
539 * @return a vector containing the maximum of each component from a and b
541 inline Vector3 Max( const Vector3& a, const Vector3& b )
543 return Vector3( std::max(a.x,b.x), std::max(a.y,b.y), std::max(a.z,b.z) );
547 * @brief Clamps each of vector v's components between minimum and maximum values.
549 * @param [in] v a vector
550 * @param [in] min the minimum value
551 * @param [in] max the maximum value
552 * @return a vector containing the clamped components of v
554 DALI_IMPORT_API Vector3 Clamp( const Vector3& v, const float& min, const float& max );
557 * @brief Scales an Actor, such that it fits within its Parent's Size Keeping the aspect ratio.
559 * f(target, source) = Vector3( min( target.X / source.X, min( target.Y / source.Y, target.Z / source.Z ) )
560 * If any of the source dimensions is zero it will be ignored in the calculation
562 * @param [in] target size
563 * @param [in] source size
564 * @return target scaled inside source
566 DALI_IMPORT_API Vector3 FitKeepAspectRatio( const Vector3& target, const Vector3& source );
569 * @brief Scales an Actor, such that it fill its Parent's Size Keeping the aspect ratio.
571 * f(target, source) = Vector3( max( target.X / source.X, max( target.Y / source.Y, target.Z / source.Z ) )
572 * If any of the source dimensions is zero it will be ignored in the calculation
574 * @param [in] target size
575 * @param [in] source size
576 * @return target scaled inside source
578 DALI_IMPORT_API Vector3 FillKeepAspectRatio( const Vector3& target, const Vector3& source );
581 * @brief Scales an Actor, such that it fill its Parent's Size in the X and Y coordinates Keeping the aspect ratio.
583 * f(target, source) = Vector3( max( target.X / sizeX, target.Y / sizeY ) )
584 * If any of the source dimensions is zero it will be ignored in the calculation
586 * @param [in] target size
587 * @param [in] source size
588 * @return target scaled inside source
590 DALI_IMPORT_API Vector3 FillXYKeepAspectRatio( const Vector3& target, const Vector3& source );
593 * @brief Shrinks source size inside the target size maintaining aspect ratio of source.
595 * If source is smaller than target it returns source
596 * @pre source width and height > 0
597 * @param [in] target size
598 * @param [in] source size
599 * @return target scaled inside source
601 Vector3 ShrinkInsideKeepAspectRatio( const Vector3& target, const Vector3& source );
606 #endif // __DALI_VECTOR_3_H__