1 #ifndef __DALI_VECTOR_3_H__
2 #define __DALI_VECTOR_3_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
40 * @brief A three dimensional vector.
43 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.
66 * @param [in] x (or width) component
67 * @param [in] y (or height) component
68 * @param [in] z (or depth) component
70 explicit Vector3(float x, float y, float z)
78 * @brief Conversion constructor from an array of three floats.
81 * @param [in] array of xyz
83 explicit Vector3(const float* array)
94 * @param [in] vec2 Vector2 to create this vector from
96 explicit Vector3( const Vector2& vec2 );
102 * @param [in] vec4 Vector4 to create this vector from
104 explicit Vector3( const Vector4& vec4 );
108 static const Vector3 ONE; ///< (1.0f,1.0f,1.0f)
109 static const Vector3 XAXIS; ///< Vector representing the X axis
110 static const Vector3 YAXIS; ///< Vector representing the Y axis
111 static const Vector3 ZAXIS; ///< Vector representing the Z axis
112 static const Vector3 NEGATIVE_XAXIS; ///< Vector representing the negative X axis
113 static const Vector3 NEGATIVE_YAXIS; ///< Vector representing the negative Y axis
114 static const Vector3 NEGATIVE_ZAXIS; ///< Vector representing the negative Z axis
115 static const Vector3 ZERO; ///< (0.0f, 0.0f, 0.0f)
120 * @brief Assignment operator.
123 * @param[in] array of floats
126 Vector3& operator=(const float* array)
136 * @brief Assignment operator.
139 * @param[in] rhs vector to assign.
142 Vector3& operator=(const Vector2& rhs);
145 * @brief Assignment operator.
148 * @param[in] rhs vector to assign.
151 Vector3& operator=(const Vector4& rhs);
154 * @brief Addition operator.
157 * @param[in] rhs vector to add.
158 * @return A vector containing the result of the addition
160 Vector3 operator+(const Vector3& rhs) const
168 * @brief Addition assignment operator.
171 * @param[in] rhs vector to add.
174 Vector3& operator+=(const Vector3& rhs)
184 * @brief Subtraction operator.
187 * @param[in] rhs the vector to subtract
188 * @return A vector containing the result of the subtraction
190 Vector3 operator-(const Vector3& rhs) const
198 * @brief Subtraction assignment operator.
201 * @param[in] rhs the vector to subtract
204 Vector3& operator-=(const Vector3& rhs)
214 * @brief Multiplication operator.
217 * @param[in] rhs the vector to multiply
218 * @return A vector containing the result of the multiplication
220 Vector3 operator*(const Vector3& rhs) const
228 * @brief Multiplication operator.
231 * @param[in] rhs the float value to scale the vector
232 * @return A vector containing the result of the scaling
234 Vector3 operator*(float rhs) const
236 return Vector3(x * rhs, y * rhs, z * rhs);
240 * @brief Multiplication assignment operator.
243 * @param[in] rhs the vector to multiply
246 Vector3& operator*=(const Vector3& rhs)
256 * @brief Multiplication assignment operator.
259 * @param[in] rhs the float value to scale the vector
262 Vector3& operator*=(float rhs)
272 * @brief Multiplication assignment operator.
275 * @param[in] rhs the Quaternion value to multiply the vector by
278 Vector3& operator*=(const Quaternion& rhs);
281 * @brief Division operator.
284 * @param[in] rhs the vector to divide
285 * @return A vector containing the result of the division
287 Vector3 operator/(const Vector3& rhs) const
295 * @brief Division operator.
298 * @param[in] rhs The float value to scale the vector by
299 * @return A vector containing the result of the scaling
301 Vector3 operator/(float rhs) const
303 return Vector3(x / rhs, y / rhs, z / rhs);
307 * @brief Division assignment operator.
310 * @param[in] rhs the vector to divide
313 Vector3& operator/=(const Vector3& rhs)
323 * @brief Division assignment operator.
326 * @param[in] rhs the float value to scale the vector by
329 Vector3& operator/=(float rhs)
331 float oneOverRhs = 1.0f / rhs;
340 * @brief Unary negation operator.
343 * @return A vector containg the negation
345 Vector3 operator-() const
347 Vector3 temp(-x, -y, -z);
353 * @brief Equality operator.
355 * utilises appropriate machine epsilon values;
358 * @param[in] rhs The vector to test against
359 * @return true if the vectors are equal
361 bool operator==(const Vector3& rhs) const;
364 * @brief Inequality operator.
366 * utilises appropriate machine epsilon values;
369 * @param[in] rhs The vector to test against
370 * @return true if the vectors are not equal
372 bool operator!=(const Vector3& rhs) const
374 return !(*this == rhs);
378 * @brief Const array subscript operator overload.
380 * Asserts if index is out of range. Should be 0, 1 or 2
382 * @param[in] index Subscript
383 * @return The float at the given index.
385 const float& operator[](const unsigned int index) const
387 DALI_ASSERT_ALWAYS( index < 3 && "Vector element index out of bounds" );
389 return AsFloat()[index];
393 * @brief Mutable array subscript operator overload.
395 * Asserts if index is out of range. Should be 0, 1 or 2
397 * @param[in] index Subscript index
398 * @return The float at the given index.
400 float& operator[](const unsigned int index)
402 DALI_ASSERT_ALWAYS( index < 3 && "Vector element index out of bounds" );
404 return AsFloat()[index];
408 * @brief Returns the dot product of this vector and another vector.
410 * The dot product is the length of one vector in the direction of another vector.
411 * This is great for lighting, threshold testing the angle between two unit vectors,
412 * calculating the distance between two points in a particular direction.
414 * @param [in] other the other vector
415 * @return the dot product
417 float Dot(const Vector3& other) const;
420 * @brief Returns the cross produce of this vector and another vector.
422 * The cross produce of two vectors is a vector which is perpendicular to the plane of the
423 * two vectors. This is great for calculating normals and making matrices orthogonal.
426 * @param [in] other the other vector
427 * @return the cross product
429 Vector3 Cross(const Vector3& other) const;
432 * @brief Returns the length of the vector.
435 * @return the length of the vector
437 float Length() const;
440 * @brief Returns the length of the vector squared.
442 * This is more efficient than Length() for threshold
443 * testing as it avoids the use of a square root.
445 * @return the length of the vector squared.
447 float LengthSquared() const;
450 * @brief Sets the vector to be unit length, whilst maintaining its direction.
457 * @brief Clamps the vector between minimum and maximum vectors.
460 * @param [in] min the minimum vector
461 * @param [in] max the maximum vector
463 void Clamp( const Vector3& min, const Vector3& max );
466 * @brief Returns the contents of the vector as an array of 3 floats.
468 * The order of the values in this array are as follows:
469 * 0: x (or width, or r)
470 * 1: y (or height, or g)
471 * 2: z (or depth, or b)
473 * @return the vector contents as an array of 3 floats.
474 * @note inlined for performance reasons (generates less code than a function call)
476 const float* AsFloat() const {return &x;}
479 * @brief Returns the contents of the vector as an array of 3 floats.
481 * The order of the values in this array are as follows:
482 * 0: x (or width, or r)
483 * 1: y (or height, or g)
484 * 2: z (or depth, or b)
486 * @return the vector contents as an array of 3 floats.
487 * @note inlined for performance reasons (generates less code than a function call)
489 float* AsFloat() {return &x;}
492 * @brief Returns the x & y components (or width & height, or r & g) as a Vector2.
495 * @return the partial vector contents as Vector2 (x,y)
496 * @note inlined for performance reasons (generates less code than a function call)
498 const Vector2& GetVectorXY() const {return reinterpret_cast<const Vector2&>(x);}
501 * @brief Returns the x & y components (or width & height, or r & g) as a Vector2.
504 * @return the partial vector contents as Vector2 (x,y)
505 * @note inlined for performance reasons (generates less code than a function call)
507 Vector2& GetVectorXY() {return reinterpret_cast<Vector2&>(x);}
510 * @brief Returns the y & z components (or height & depth, or g & b) as a Vector2.
513 * @return the partial vector contents as Vector2 (y,z)
514 * @note inlined for performance reasons (generates less code than a function call)
516 const Vector2& GetVectorYZ() const {return reinterpret_cast<const Vector2&>(y);}
519 * @brief Returns the y & z components (or height & depth, or g & b) as a Vector2.
522 * @return the partial vector contents as Vector2 (y,z)
523 * @note inlined for performance reasons (generates less code than a function call)
525 Vector2& GetVectorYZ() {return reinterpret_cast<Vector2&>(y);}
530 // (x width r), (y height g), (z depth b) must be consecutive in memory.
531 // No other data must be added before (x width r) member.
532 // No virtual methods must be added to this struct.
535 float x; ///< x component
536 float width; ///< width component
537 float r; ///< red component
541 float y; ///< y component
542 float height; ///< height component
543 float g; ///< green component
547 float z; ///< z component
548 float depth; ///< depth component
549 float b; ///< blue component
554 * @brief Print a Vector3.
557 * @param [in] o The output stream operator.
558 * @param [in] vector The vector to print.
559 * @return The output stream operator.
561 DALI_IMPORT_API std::ostream& operator<< (std::ostream& o, const Vector3& vector);
564 * @brief Returns a vector with components set to the minimum of the corresponding component in a and b.
566 * If a=0,1,2 and b=2,1,0 returns a vector of 2,1,2.
568 * @param [in] a a vector
569 * @param [in] b a vector
570 * @return a vector containing the minimum of each component from a and b
572 inline Vector3 Min( const Vector3& a, const Vector3& b )
574 return Vector3( a.x < b.x ? a.x : b.x ,
575 a.y < b.y ? a.y : b.y,
576 a.z < b.z ? a.z : b.z );
580 * @brief Returns a vector with components set to the maximum of the corresponding component in a and b.
582 * If a=0,1 and b=1,0 returns a vector of 1,1
584 * @param [in] a a vector
585 * @param [in] b a vector
586 * @return a vector containing the maximum of each component from a and b
588 inline Vector3 Max( const Vector3& a, const Vector3& b )
590 return Vector3( a.x > b.x ? a.x : b.x,
591 a.y > b.y ? a.y : b.y,
592 a.z > b.z ? a.z : b.z );
596 * @brief Clamps each of vector v's components between minimum and maximum values.
599 * @param [in] v a vector
600 * @param [in] min the minimum value
601 * @param [in] max the maximum value
602 * @return a vector containing the clamped components of v
604 DALI_IMPORT_API Vector3 Clamp( const Vector3& v, const float& min, const float& max );
606 // Allow Vector3 to be treated as a POD type
607 template <> struct TypeTraits< Vector3 > : public BasicTypes< Vector3 > { enum { IS_TRIVIAL_TYPE = true }; };
614 #endif // __DALI_VECTOR_3_H__