| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- /*
- Copyright (c) 2013 Daniele Bartolini, Michele Rossi
- Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
- Permission is hereby granted, free of charge, to any person
- obtaining a copy of this software and associated documentation
- files (the "Software"), to deal in the Software without
- restriction, including without limitation the rights to use,
- copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the
- Software is furnished to do so, subject to the following
- conditions:
- The above copyright notice and this permission notice shall be
- included in all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
- HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
- WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- OTHER DEALINGS IN THE SOFTWARE.
- */
- #pragma once
- #include "types.h"
- #include "vector3.h"
- #include "math_types.h"
- #include "matrix3x3.h"
- namespace crown
- {
- /// Negates the quaternion @a q and returns the result.
- Quaternion operator-(const Quaternion& q);
- /// Multiplies the quaternions @a and @a b. (i.e. rotates first by @a a then by @a b).
- Quaternion operator*(Quaternion a, const Quaternion& b);
- /// Multiplies the quaternion @a a by the scalar @a k.
- Quaternion operator*(const Quaternion& a, float k);
- /// Functions to manipulate Quaternion.
- ///
- /// @ingroup Math
- namespace quaternion
- {
- const Quaternion IDENTITY = Quaternion(0.0, 0.0, 0.0, 1.0);
- /// Returns the dot product between quaternions @a a and @a b.
- float dot(const Quaternion& a, const Quaternion& b);
- /// Returns the length of @a q.
- float length(const Quaternion& q);
- /// Returns the conjugate of quaternion @a q.
- Quaternion conjugate(const Quaternion& q);
- /// Returns the inverse of quaternion @a q.
- Quaternion inverse(const Quaternion& q);
- /// Returns the quaternion @a q raised to the power of @a exp.
- Quaternion power(const Quaternion& q, float exp);
- /// Returns the Matrix3x3 representation of the quaternion @a q.
- Matrix3x3 to_matrix3x3(const Quaternion& q);
- /// Returns the Matrix4x4 representation of the quaternion @a q.
- Matrix4x4 to_matrix4x4(const Quaternion& q);
- } // namespace quaternion
- inline Quaternion operator-(const Quaternion& q)
- {
- return Quaternion(-q.x, -q.y, -q.z, -q.w);
- }
- inline Quaternion operator*(Quaternion a, const Quaternion& b)
- {
- a *= b;
- return a;
- }
- inline Quaternion operator*(const Quaternion& a, float k)
- {
- return Quaternion(a.x * k, a.y * k, a.z * k, a.w * k);
- }
- namespace quaternion
- {
- //-----------------------------------------------------------------------------
- inline float dot(const Quaternion& a, const Quaternion& b)
- {
- return a.w * b.w + a.x * b.x + a.y * b.y + a.z * b.z;
- }
- //-----------------------------------------------------------------------------
- inline float length(const Quaternion& q)
- {
- return math::sqrt(q.w * q.w + q.x * q.x + q.y * q.y + q.z * q.z);
- }
- inline Quaternion conjugate(const Quaternion& q)
- {
- return Quaternion(-q.x, -q.y, -q.z, q.w);
- }
- inline Quaternion inverse(const Quaternion& q)
- {
- return conjugate(q) * (1.0f / length(q));
- }
- inline Quaternion power(const Quaternion& q, float exp)
- {
- if (math::abs(q.w) < 0.9999)
- {
- Quaternion tmp;
- float alpha = math::acos(q.w); // alpha = theta/2
- float new_alpha = alpha * exp;
- tmp.w = math::cos(new_alpha);
- float mult = math::sin(new_alpha) / math::sin(alpha);
- tmp.x = q.x * mult;
- tmp.y = q.y * mult;
- tmp.z = q.z * mult;
- return tmp;
- }
- return q;
- }
- inline Matrix3x3 to_matrix3x3(const Quaternion& q)
- {
- return Matrix3x3(q);
- }
- inline Matrix4x4 to_matrix4x4(const Quaternion& q)
- {
- return Matrix4x4(q, Vector3(0, 0, 0));
- }
- } // namespace quaternion
- //-----------------------------------------------------------------------------
- inline Quaternion::Quaternion()
- {
- // Do not initialize
- }
- //-----------------------------------------------------------------------------
- inline Quaternion::Quaternion(float nx, float ny, float nz, float nw)
- : x(nx)
- , y(ny)
- , z(nz)
- , w(nw)
- {
- }
- //-----------------------------------------------------------------------------
- inline Quaternion::Quaternion(const Vector3& axis, float angle)
- : x(axis.x * math::sin(angle * 0.5f))
- , y(axis.y * math::sin(angle * 0.5f))
- , z(axis.z * math::sin(angle * 0.5f))
- , w(math::cos(angle * 0.5f))
- {
- }
- //-----------------------------------------------------------------------------
- inline float& Quaternion::operator[](uint32_t i)
- {
- CE_ASSERT(i < 4, "Index out of bounds");
- return (&x)[i];
- }
- //-----------------------------------------------------------------------------
- inline const float& Quaternion::operator[](uint32_t i) const
- {
- CE_ASSERT(i < 4, "Index out of bounds");
- return (&x)[i];
- }
- //-----------------------------------------------------------------------------
- inline Quaternion& Quaternion::operator*=(const Quaternion& a)
- {
- const float t_x = w * a.x + x * a.w + z * a.y - y * a.z;
- const float t_y = w * a.y + y * a.w + x * a.z - z * a.x;
- const float t_z = w * a.z + z * a.w + y * a.x - x * a.y;
- const float t_w = w * a.w - x * a.x - y * a.y - z * a.z;
- x = t_x;
- y = t_y;
- z = t_z;
- w = t_w;
- return *this;
- }
- } // namespace crown
|