1 module radeonrays.math.quaternion;
2 
3 /**********************************************************************
4 Copyright (c) 2016 Advanced Micro Devices, Inc. All rights reserved.
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23 ********************************************************************/
24 
25 import radeonrays.math.matrix;
26 import core.stdc.math;
27 
28 extern(C++,"RadeonRays")
29 {
30 	struct quaternion
31 	{
32 	public:
33 		//quaternion (float xx = 0.f, float yy = 0.f, float zz = 0.f, float ww = 1.f) : x(xx), y(yy), z(zz), w(ww) {}
34 		
35 		//explicit               quaternion( const vector<T,4>& v );
36 		/// create quaternion from a orthogonal(!) matrix
37 		/// make sure the matrix is ORTHOGONAL
38 		//explicit               quaternion( const matrix& m );
39 		
40 		/// convert quaternion to matrix
41 		//void                   to_matrix( matrix<T,4,4>& pM ) const;
42 		void to_matrix(ref matrix m) const;
43 		
44 		/*quaternion      operator -() const { return quaternion(-x, -y, -z, -w); }
45 		quaternion&     operator +=( quaternion const& o ) { x+=o.x; y+=o.y; z+=o.z; w+=o.w; return *this; }
46 		quaternion&     operator -=( quaternion const& o ) { x-=o.x; y-=o.y; z-=o.z; w-=o.w; return *this; }
47 		quaternion&     operator *=( quaternion const& o ) { x*=o.x; y*=o.y; z*=o.z; w*=o.w; return *this; }
48 		quaternion&     operator *=( float a ) { x*=a; y*=a; z*=a; w*=a; return *this; }
49 		quaternion&     operator /=( float a ) { float inva = 1.f/a; x*=inva; y*=inva; z*=inva; w*=inva; return *this; }*/
50 		
51 		quaternion      conjugate() const { return quaternion(-x, -y, -z, w); }
52 		quaternion      inverse()   const; 
53 		
54 		float  sqnorm() const { return x * x + y * y + z * z + w * w; }
55 		float  norm()   const { return sqrt(sqnorm()); }
56 		
57 		float x, y, z, w = 1;
58 	};
59 	
60 	/*inline quaternion::quaternion (const matrix& m)
61 	{
62 		float tr = m.m00 + m.m11 + m.m22;
63 		
64 		if (tr > 0) 
65 		{ 
66 			float S = sqrt(tr+1.f) * 2;
67 			w = 0.25f * S;
68 			x = (m.m21 - m.m12) / S;
69 			y = (m.m02 - m.m20) / S; 
70 			z = (m.m10 - m.m01) / S; 
71 		} 
72 		else if ((m.m00 > m.m11)&(m.m00 > m.m22)) 
73 		{ 
74 			float S = sqrt(1.f + m.m00 - m.m11 - m.m22) * 2;
75 			w = (m.m21 - m.m12) / S;
76 			x = 0.25f * S;
77 			y = (m.m01 + m.m10) / S; 
78 			z = (m.m02 + m.m20) / S; 
79 		} 
80 		else if (m.m11 > m.m22) 
81 		{ 
82 			float S = sqrt(1.f + m.m11 - m.m00 - m.m22) * 2; // S=4*qy
83 			w = (m.m02 - m.m20) / S;
84 			x = (m.m01 + m.m10) / S; 
85 			y = 0.25f * S;
86 			z = (m.m12 + m.m21) / S; 
87 		} 
88 		else 
89 		{ 
90 			float S = sqrt(1.f + m.m22 - m.m00 - m.m11) * 2; // S=4*qz
91 			w = (m.m10 - m.m01) / S;
92 			x = (m.m02 + m.m20) / S;
93 			y = (m.m12 + m.m21) / S;
94 			z = 0.25f * S;
95 		}
96 	}
97 	
98 	inline quaternion      quaternion::inverse()   const
99 	{
100 		if (sqnorm() == 0)
101 		{
102 			return quaternion();
103 		}
104 		else
105 		{
106 			quaternion q = conjugate();
107 			q /= sqnorm();
108 			return q;
109 		}
110 	}
111 	
112 	inline quaternion operator * (quaternion const& q1,  quaternion const& q2)
113 	{
114 		quaternion res;
115 		res.x = q1.y*q2.z - q1.z*q2.y + q2.w*q1.x + q1.w*q2.x;
116 		res.y = q1.z*q2.x - q1.x*q2.z + q2.w*q1.y + q1.w*q2.y;
117 		res.z = q1.x*q2.y - q2.x*q1.y + q2.w*q1.z + q1.w*q2.z;
118 		res.w = q1.w*q2.w - q1.x*q2.x - q1.y*q2.y - q1.z*q2.z;
119 		return res;
120 	}
121 	
122 	inline quaternion operator / ( const quaternion& q,  float a )
123 	{
124 		quaternion res = q;
125 		return res /= a;
126 	}
127 	
128 	inline quaternion operator + ( const quaternion& q1,  const quaternion& q2 )
129 	{
130 		quaternion res = q1;
131 		return res += q2;
132 	}
133 	
134 	inline quaternion operator - ( const quaternion& q1,  const quaternion& q2 )
135 	{
136 		quaternion res = q1;
137 		return res -= q2;
138 	}
139 	
140 	inline quaternion operator * ( const quaternion& q,  float a )
141 	{
142 		quaternion res = q;
143 		return res *= a;
144 	}
145 	
146 	inline quaternion operator * ( float a, quaternion const& q )
147 	{
148 		quaternion res = q;
149 		return res *= a;
150 	}
151 	
152 	inline quaternion normalize( quaternion const& q )
153 	{
154 		float norm = q.norm();
155 		return q / norm;
156 	}
157 	
158 	inline void quaternion::to_matrix( matrix& m ) const
159 	{
160 		float s = 2.f/norm();
161 		m.m00 = 1 - s*(y*y + z*z); m.m01 = s * (x*y - w*z);        m.m02 = s * (x*z + w*y);     m.m03 = 0;
162 		m.m10 = s * (x*y + w*z);   m.m11 = 1 - s * (x*x + z*z); m.m12 = s * (y*z - w*x);     m.m13 = 0;
163 		m.m20 = s * (x*z - w*y);   m.m21 = s * (y*z + w*x);        m.m22 = 1 - s * (x*x + y*y); m.m23 = 0;
164 		m.m30 =                    m.m31 =                      m.m32;                       m.m33 = 1;
165 	}*/
166 }