123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- /**
- Copyright 2013 BlackBerry Inc.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- Original file from GamePlay3D: http://gameplay3d.org
- This file was modified to fit the cocos2d-x project
- */
- #include "math/Quaternion.h"
- #include <float.h>
- NS_CC_MATH_BEGIN
- inline Quaternion Quaternion::operator*(const Quaternion& q) const
- {
- Quaternion result(*this);
- result.multiply(q);
- return result;
- }
- inline Quaternion& Quaternion::operator*=(const Quaternion& q)
- {
- multiply(q);
- return *this;
- }
- inline Vec3 Quaternion::operator*(const Vec3& v) const
- {
- Vec3 uv, uuv;
- Vec3 qvec(x, y, z);
- Vec3::cross(qvec, v, &uv);
- Vec3::cross(qvec, uv, &uuv);
- uv *= (2.0f * w);
- uuv *= 2.0f;
- return v + uv + uuv;
- }
- /* --- --- --- --- Add by XuJJ start --- --- --- --- */
- float Dot( const Quaternion& q1, const Quaternion& q2 );
- inline float Magnitude(const Quaternion& q)
- {
- // return SqrtImpl (SqrMagnitude (q));
- return sqrt (SqrMagnitude (q));
- }
- inline float SqrMagnitude(const Quaternion& q)
- {
- return Dot (q, q);
- }
- inline float Dot( const Quaternion& q1, const Quaternion& q2 )
- {
- return (q1.x*q2.x + q1.y*q2.y + q1.z*q2.z + q1.w*q2.w);
- }
- inline Quaternion NormalizeSafe (const Quaternion& q)
- {
- float mag = Magnitude (q);
- if (mag < FLT_EPSILON)
- return Quaternion::identity ();
- else
- return q / mag;
- }
- inline float& Quaternion::operator[] (int i)
- {
- // DebugAssertIf (i < 0 || i > 3);
- CC_ASSERT(!(i < 0 || i > 3));
- return (&x)[i];
- }
- inline const float& Quaternion::operator[] (int i)const
- {
- // DebugAssertIf (i < 0 || i > 3);
- CC_ASSERT(!(i < 0 || i > 3));
- return (&x)[i];
- }
- inline Quaternion& Quaternion::operator+= (const Quaternion& aQuat)
- {
- x += aQuat.x;
- y += aQuat.y;
- z += aQuat.z;
- w += aQuat.w;
- return *this;
- }
- inline Quaternion& Quaternion::operator-= (const Quaternion& aQuat)
- {
- x -= aQuat.x;
- y -= aQuat.y;
- z -= aQuat.z;
- w -= aQuat.w;
- return *this;
- }
- inline Quaternion& Quaternion::operator *= (float aScalar)
- {
- x *= aScalar;
- y *= aScalar;
- z *= aScalar;
- w *= aScalar;
- return *this;
- }
- inline Quaternion& Quaternion::operator /= (const float aScalar)
- {
- // AssertIf (CompareApproximately (aScalar, 0.0F));
- x /= aScalar;
- y /= aScalar;
- z /= aScalar;
- w /= aScalar;
- return *this;
- }
- //inline Quaternion& Quaternion::operator *= (const Quaternion& rhs)
- //{
- // float tempx = w*rhs.x + x*rhs.w + y*rhs.z - z*rhs.y;
- // float tempy = w*rhs.y + y*rhs.w + z*rhs.x - x*rhs.z;
- // float tempz = w*rhs.z + z*rhs.w + x*rhs.y - y*rhs.x;
- // float tempw = w*rhs.w - x*rhs.x - y*rhs.y - z*rhs.z;
- // x = tempx; y = tempy; z = tempz; w = tempw;
- // return *this;
- //}
- inline bool IsFinite (const Quaternion& f)
- {
- unsigned int intval = *reinterpret_cast<const unsigned int*>(&(f.x));
- bool flag = ((intval & 0x7f800000) != 0x7f800000);
- intval = *reinterpret_cast<const unsigned int*>(&(f.y));
- flag = flag & ((intval & 0x7f800000) != 0x7f800000);
- intval = *reinterpret_cast<const unsigned int*>(&(f.z));
- flag = flag & ((intval & 0x7f800000) != 0x7f800000);
- intval = *reinterpret_cast<const unsigned int*>(&(f.w));
- flag = flag & ((intval & 0x7f800000) != 0x7f800000);
- return flag;
- // return RRP::IsFinite(f.x) & RRP::IsFinite(f.y) & RRP::IsFinite(f.z) & RRP::IsFinite(f.w);
- }
- /* --- --- --- --- Add by XuJJ end --- --- --- --- */
- NS_CC_MATH_END
|