2 Copyright (c) 2012 Advanced Micro Devices, Inc.
4 This software is provided 'as-is', without any express or implied warranty.
5 In no event will the authors be held liable for any damages arising from the use of this software.
6 Permission is granted to anyone to use this software for any purpose,
7 including commercial applications, and to alter it and redistribute it freely,
8 subject to the following restrictions:
10 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
11 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
12 3. This notice may not be removed or altered from any source distribution.
14 //Originally written by Takahiro Harada
20 #include <AdlPrimitives/Math/Matrix3x3.h>
25 typedef float4 Quaternion;
28 Quaternion qtSet(const float4& axis, float angle);
31 Quaternion qtMul(const Quaternion& a, const Quaternion& b);
34 float4 qtRotate(const Quaternion& q, const float4& vec);
37 float4 qtInvRotate(const Quaternion& q, const float4& vec);
40 Quaternion qtInvert(const Quaternion& q);
43 Matrix3x3 qtGetRotationMatrix(const Quaternion& quat);
46 Quaternion qtNormalize(const Quaternion& q);
49 Quaternion qtGetIdentity() { return make_float4(0,0,0,1); }
52 Quaternion qtSet(const float4& axis, float angle)
54 float4 nAxis = normalize3( axis );
57 q.s[0] = nAxis.s[0]*sin(angle/2);
58 q.s[1] = nAxis.s[1]*sin(angle/2);
59 q.s[2] = nAxis.s[2]*sin(angle/2);
60 q.s[3] = cos(angle/2);
65 Quaternion qtMul(const Quaternion& a, const Quaternion& b)
69 ans += a.s[3]*b + b.s[3]*a;
70 ans.s[3] = a.s[3]*b.s[3] - (a.s[0]*b.s[0]+a.s[1]*b.s[1]+a.s[2]*b.s[2]);
75 float4 qtRotate(const Quaternion& q, const float4& vec)
77 Quaternion vecQ = vec;
79 Quaternion qInv = qtInvert( q );
80 float4 out = qtMul(qtMul(q,vecQ),qInv);
85 float4 qtInvRotate(const Quaternion& q, const float4& vec)
87 return qtRotate( qtInvert( q ), vec );
91 Quaternion qtInvert(const Quaternion& q)
102 Matrix3x3 qtGetRotationMatrix(const Quaternion& quat)
104 float4 quat2 = make_float4(quat.s[0]*quat.s[0], quat.s[1]*quat.s[1], quat.s[2]*quat.s[2], 0.f);
107 out.m_row[0].s[0]=1-2*quat2.s[1]-2*quat2.s[2];
108 out.m_row[0].s[1]=2*quat.s[0]*quat.s[1]-2*quat.s[3]*quat.s[2];
109 out.m_row[0].s[2]=2*quat.s[0]*quat.s[2]+2*quat.s[3]*quat.s[1];
110 out.m_row[0].s[3] = 0.f;
112 out.m_row[1].s[0]=2*quat.s[0]*quat.s[1]+2*quat.s[3]*quat.s[2];
113 out.m_row[1].s[1]=1-2*quat2.s[0]-2*quat2.s[2];
114 out.m_row[1].s[2]=2*quat.s[1]*quat.s[2]-2*quat.s[3]*quat.s[0];
115 out.m_row[1].s[3] = 0.f;
117 out.m_row[2].s[0]=2*quat.s[0]*quat.s[2]-2*quat.s[3]*quat.s[1];
118 out.m_row[2].s[1]=2*quat.s[1]*quat.s[2]+2*quat.s[3]*quat.s[0];
119 out.m_row[2].s[2]=1-2*quat2.s[0]-2*quat2.s[1];
120 out.m_row[2].s[3] = 0.f;
126 Quaternion qtGetQuaternion(const Matrix3x3* m)
129 q.w = sqrtf( m[0].m_row[0].x + m[0].m_row[1].y + m[0].m_row[2].z + 1 ) * 0.5f;
130 float inv4w = 1.f/(4.f*q.w);
131 q.x = (m[0].m_row[2].y-m[0].m_row[1].z)*inv4w;
132 q.y = (m[0].m_row[0].z-m[0].m_row[2].x)*inv4w;
133 q.z = (m[0].m_row[1].x-m[0].m_row[0].y)*inv4w;
139 Quaternion qtNormalize(const Quaternion& q)
141 return normalize4(q);
145 float4 transform(const float4& p, const float4& translation, const Quaternion& orientation)
147 return qtRotate( orientation, p ) + translation;
151 float4 invTransform(const float4& p, const float4& translation, const Quaternion& orientation)
153 return qtRotate( qtInvert( orientation ), p-translation ); // use qtInvRotate