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>
36 * @brief A three dimensional vector.
38 struct DALI_IMPORT_API Vector3
46 // (x width r), (y height g), (z depth b) must be consecutive in memory.
47 // No other data must be added before (x width r) member.
48 // No virtual methods must be added to this struct.
59 * @param [in] x (or width) component
60 * @param [in] y (or height) component
61 * @param [in] z (or depth) component
63 explicit Vector3(float x, float y, float z)
71 * @brief Conversion constructor from an array of three floats.
73 * @param [in] array of xyz
75 explicit Vector3(const float* array)
85 * @param [in] vec2 Vector2 to create this vector from
87 explicit Vector3( const Vector2& vec2 );
92 * @param [in] vec4 Vector4 to create this vector from
94 explicit Vector3( const Vector4& vec4 );
98 static const Vector3 ONE; ///< (1.0f,1.0f,1.0f)
99 static const Vector3 XAXIS; ///< Vector representing the X axis
100 static const Vector3 YAXIS; ///< Vector representing the Y axis
101 static const Vector3 ZAXIS; ///< Vector representing the Z axis
102 static const Vector3 NEGATIVE_XAXIS; ///< Vector representing the negative X axis
103 static const Vector3 NEGATIVE_YAXIS; ///< Vector representing the negative Y axis
104 static const Vector3 NEGATIVE_ZAXIS; ///< Vector representing the negative Z axis
105 static const Vector3 ZERO; ///< (0.0f, 0.0f, 0.0f)
110 * @brief Assignment operator.
112 * @param[in] array of floats
115 Vector3& operator=(const float* array)
125 * @brief Assignment operator.
127 * @param[in] rhs vector to assign.
130 Vector3& operator=(const Vector2& rhs);
133 * @brief Assignment operator.
135 * @param[in] rhs vector to assign.
138 Vector3& operator=(const Vector4& rhs);
141 * @brief Addition operator.
143 * @param[in] rhs vector to add.
144 * @return A vector containing the result of the addition
146 Vector3 operator+(const Vector3& rhs) const
154 * @brief Addition assignment operator.
156 * @param[in] rhs vector to add.
159 Vector3& operator+=(const Vector3& rhs)
169 * @brief Subtraction operator.
171 * @param[in] rhs the vector to subtract
172 * @return A vector containing the result of the subtraction
174 Vector3 operator-(const Vector3& rhs) const
182 * @brief Subtraction assignment operator.
184 * @param[in] rhs the vector to subtract
187 Vector3& operator-=(const Vector3& rhs)
197 * @brief Multiplication operator.
199 * @param[in] rhs the vector to multiply
200 * @return A vector containing the result of the multiplication
202 Vector3 operator*(const Vector3& rhs) const
210 * @brief Multiplication operator.
212 * @param[in] rhs the float value to scale the vector
213 * @return A vector containing the result of the scaling
215 Vector3 operator*(float rhs) const
217 return Vector3(x * rhs, y * rhs, z * rhs);
221 * @brief Multiplication assignment operator.
223 * @param[in] rhs the vector to multiply
226 Vector3& operator*=(const Vector3& rhs)
236 * @brief Multiplication assignment operator.
238 * @param[in] rhs the float value to scale the vector
241 Vector3& operator*=(float rhs)
251 * @brief Multiplication assignment operator.
253 * @param[in] rhs the Quaternion value to multiply the vector by
256 Vector3& operator*=(const Quaternion& rhs);
259 * @brief Division operator.
261 * @param[in] rhs the vector to divide
262 * @return A vector containing the result of the division
264 Vector3 operator/(const Vector3& rhs) const
272 * @brief Division operator.
274 * @param[in] rhs The float value to scale the vector by
275 * @return A vector containing the result of the scaling
277 Vector3 operator/(float rhs) const
279 return Vector3(x / rhs, y / rhs, z / rhs);
283 * @brief Division assignment operator.
285 * @param[in] rhs the vector to divide
288 Vector3& operator/=(const Vector3& rhs)
298 * @brief Division assignment operator.
300 * @param[in] rhs the float value to scale the vector by
303 Vector3& operator/=(float rhs)
305 float oneOverRhs = 1.0f / rhs;
314 * @brief Unary negation operator.
316 * @return A vector containg the negation
318 Vector3 operator-() const
320 Vector3 temp(-x, -y, -z);
326 * @brief Equality operator.
328 * utilises appropriate machine epsilon values;
330 * @param[in] rhs The vector to test against
331 * @return true if the vectors are equal
333 bool operator==(const Vector3& rhs) const;
336 * @brief Inequality operator.
338 * utilises appropriate machine epsilon values;
340 * @param[in] rhs The vector to test against
341 * @return true if the vectors are not equal
343 bool operator!=(const Vector3& rhs) const
345 return !(*this == rhs);
349 * @brief Const array subscript operator overload.
351 * Asserts if index is out of range. Should be 0, 1 or 2
352 * @param[in] index Subscript
353 * @return The float at the given index.
355 const float& operator[](const unsigned int index) const
357 DALI_ASSERT_ALWAYS( index < 3 && "Vector element index out of bounds" );
359 return AsFloat()[index];
363 * @brief Mutable array subscript operator overload.
365 * Asserts if index is out of range. Should be 0, 1 or 2
366 * @param[in] index Subscript index
367 * @return The float at the given index.
369 float& operator[](const unsigned int index)
371 DALI_ASSERT_ALWAYS( index < 3 && "Vector element index out of bounds" );
373 return AsFloat()[index];
377 * @brief Returns the dot product of this vector and another vector.
379 * The dot product is the length of one vector in the direction of another vector.
380 * This is great for lighting, threshold testing the angle between two unit vectors,
381 * calculating the distance between two points in a particular direction.
382 * @param [in] other the other vector
383 * @return the dot product
385 float Dot(const Vector3& other) const;
388 * @brief Returns the cross produce of this vector and another vector.
390 * The cross produce of two vectors is a vector which is perpendicular to the plane of the
391 * two vectors. This is great for calculating normals and making matrices orthogonal.
393 * @param [in] other the other vector
394 * @return the cross product
396 Vector3 Cross(const Vector3& other) const;
399 * @brief Returns the length of the vector.
401 * @return the length of the vector
403 float Length() const;
406 * @brief Returns the length of the vector squared.
408 * This is more efficient than Length() for threshold
409 * testing as it avoids the use of a square root.
410 * @return the length of the vector squared.
412 float LengthSquared() const;
415 * @brief Sets the vector to be unit length, whilst maintaining its direction.
421 * @brief Clamps the vector between minimum and maximum vectors.
423 * @param [in] min the minimum vector
424 * @param [in] max the maximum vector
426 void Clamp( const Vector3& min, const Vector3& max );
429 * @brief Returns the contents of the vector as an array of 3 floats.
431 * The order of the values in this array are as follows:
432 * 0: x (or width, or r)
433 * 1: y (or height, or g)
434 * 2: z (or depth, or b)
435 * @note inlined for performance reasons (generates less code than a function call)
436 * @return the vector contents as an array of 3 floats.
438 const float* AsFloat() const {return &x;}
441 * @brief Returns the contents of the vector as an array of 3 floats.
443 * The order of the values in this array are as follows:
444 * 0: x (or width, or r)
445 * 1: y (or height, or g)
446 * 2: z (or depth, or b)
447 * @note inlined for performance reasons (generates less code than a function call)
448 * @return the vector contents as an array of 3 floats.
450 float* AsFloat() {return &x;}
453 * @brief Returns the x & y components (or width & height, or r & g) as a Vector2.
455 * @note inlined for performance reasons (generates less code than a function call)
456 * @return the partial vector contents as Vector2 (x,y)
458 const Vector2& GetVectorXY() const {return reinterpret_cast<const Vector2&>(x);}
461 * @brief Returns the x & y components (or width & height, or r & g) as a Vector2.
463 * @note inlined for performance reasons (generates less code than a function call)
464 * @return the partial vector contents as Vector2 (x,y)
466 Vector2& GetVectorXY() {return reinterpret_cast<Vector2&>(x);}
469 * @brief Returns the y & z components (or height & depth, or g & b) as a Vector2.
471 * @note inlined for performance reasons (generates less code than a function call)
472 * @return the partial vector contents as Vector2 (y,z)
474 const Vector2& GetVectorYZ() const {return reinterpret_cast<const Vector2&>(y);}
477 * @brief Returns the y & z components (or height & depth, or g & b) as a Vector2.
479 * @note inlined for performance reasons (generates less code than a function call)
480 * @return the partial vector contents as Vector2 (y,z)
482 Vector2& GetVectorYZ() {return reinterpret_cast<Vector2&>(y);}
487 // (x width r), (y height g), (z depth b) must be consecutive in memory.
488 // No other data must be added before (x width r) member.
489 // No virtual methods must be added to this struct.
492 float x; ///< x component
493 float width; ///< width component
494 float r; ///< red component
498 float y; ///< y component
499 float height; ///< height component
500 float g; ///< green component
504 float z; ///< z component
505 float depth; ///< depth component
506 float b; ///< blue component
511 * @brief Print a Vector3.
513 * @param [in] o The output stream operator.
514 * @param [in] vector The vector to print.
515 * @return The output stream operator.
517 DALI_IMPORT_API std::ostream& operator<< (std::ostream& o, const Vector3& vector);
520 * @brief Returns a vector with components set to the minimum of the corresponding component in a and b.
522 * If a=0,1,2 and b=2,1,0 returns a vector of 2,1,2.
523 * @param [in] a a vector
524 * @param [in] b a vector
525 * @return a vector containing the minimum of each component from a and b
527 inline Vector3 Min( const Vector3& a, const Vector3& b )
529 return Vector3( a.x < b.x ? a.x : b.x ,
530 a.y < b.y ? a.y : b.y,
531 a.z < b.z ? a.z : b.z );
535 * @brief Returns a vector with components set to the maximum of the corresponding component in a and b.
537 * If a=0,1 and b=1,0 returns a vector of 1,1
538 * @param [in] a a vector
539 * @param [in] b a vector
540 * @return a vector containing the maximum of each component from a and b
542 inline Vector3 Max( const Vector3& a, const Vector3& b )
544 return Vector3( a.x > b.x ? a.x : b.x,
545 a.y > b.y ? a.y : b.y,
546 a.z > b.z ? a.z : b.z );
550 * @brief Clamps each of vector v's components between minimum and maximum values.
552 * @param [in] v a vector
553 * @param [in] min the minimum value
554 * @param [in] max the maximum value
555 * @return a vector containing the clamped components of v
557 DALI_IMPORT_API Vector3 Clamp( const Vector3& v, const float& min, const float& max );
559 // Allow Vector3 to be treated as a POD type
560 template <> struct TypeTraits< Vector3 > : public BasicTypes< Vector3 > { enum { IS_TRIVIAL_TYPE = true }; };
564 #endif // __DALI_VECTOR_3_H__