Initialize libbullet git in 2.0_beta.
[platform/upstream/libbullet.git] / Extras / RigidBodyGpuPipeline / dynamics / basic_demo / Stubs / AdlMatrix3x3.h
1 /*
2 Copyright (c) 2012 Advanced Micro Devices, Inc.  
3
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:
9
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.
13 */
14 //Originally written by Takahiro Harada
15
16
17 #ifndef MATRIX3X3_H
18 #define MATRIX3X3_H
19
20 #include "AdlMath.h"
21
22 ///////////////////////////////////////
23 //      Matrix3x3
24 ///////////////////////////////////////
25
26 typedef 
27 _MEM_CLASSALIGN16 struct
28 {
29         _MEM_ALIGNED_ALLOCATOR16;
30         float4 m_row[3];
31 }Matrix3x3;
32
33 __inline
34 Matrix3x3 mtZero();
35
36 __inline
37 Matrix3x3 mtIdentity();
38
39 __inline
40 Matrix3x3 mtDiagonal(float a, float b, float c);
41
42 __inline
43 Matrix3x3 mtTranspose(const Matrix3x3& m);
44
45 __inline
46 Matrix3x3 mtMul(const Matrix3x3& a, const Matrix3x3& b);
47
48 __inline
49 float4 mtMul1(const Matrix3x3& a, const float4& b);
50
51 __inline
52 Matrix3x3 mtMul2(float a, const Matrix3x3& b);
53
54 __inline
55 float4 mtMul3(const float4& b, const Matrix3x3& a);
56
57 __inline
58 Matrix3x3 mtInvert(const Matrix3x3& m);
59
60 __inline
61 Matrix3x3 mtZero()
62 {
63         Matrix3x3 m;
64         m.m_row[0] = make_float4(0.f);
65         m.m_row[1] = make_float4(0.f);
66         m.m_row[2] = make_float4(0.f);
67         return m;
68 }
69
70 __inline
71 Matrix3x3 mtIdentity()
72 {
73         Matrix3x3 m;
74         m.m_row[0] = make_float4(1,0,0);
75         m.m_row[1] = make_float4(0,1,0);
76         m.m_row[2] = make_float4(0,0,1);
77         return m;
78 }
79
80 __inline
81 Matrix3x3 mtDiagonal(float a, float b, float c)
82 {
83         Matrix3x3 m;
84         m.m_row[0] = make_float4(a,0,0);
85         m.m_row[1] = make_float4(0,b,0);
86         m.m_row[2] = make_float4(0,0,c);
87         return m;
88 }
89
90 __inline
91 Matrix3x3 mtTranspose(const Matrix3x3& m)
92 {
93         Matrix3x3 out;
94         out.m_row[0] = make_float4(m.m_row[0].s[0], m.m_row[1].s[0], m.m_row[2].s[0], 0.f);
95         out.m_row[1] = make_float4(m.m_row[0].s[1], m.m_row[1].s[1], m.m_row[2].s[1], 0.f);
96         out.m_row[2] = make_float4(m.m_row[0].s[2], m.m_row[1].s[2], m.m_row[2].s[2], 0.f);
97         return out;
98 }
99
100 __inline
101 Matrix3x3 mtMul(const Matrix3x3& a, const Matrix3x3& b)
102 {
103         Matrix3x3 transB;
104         transB = mtTranspose( b );
105         Matrix3x3 ans;
106         for(int i=0; i<3; i++)
107         {
108                 ans.m_row[i].s[0] = dot3F4(a.m_row[i],transB.m_row[0]);
109                 ans.m_row[i].s[1] = dot3F4(a.m_row[i],transB.m_row[1]);
110                 ans.m_row[i].s[2] = dot3F4(a.m_row[i],transB.m_row[2]);
111         }
112         return ans;
113 }
114
115 __inline
116 float4 mtMul1(const Matrix3x3& a, const float4& b)
117 {
118         float4 ans;
119         ans.s[0] = dot3F4( a.m_row[0], b );
120         ans.s[1] = dot3F4( a.m_row[1], b );
121         ans.s[2] = dot3F4( a.m_row[2], b );
122         return ans;
123 }
124
125 __inline
126 Matrix3x3 mtMul2(float a, const Matrix3x3& b)
127 {
128         Matrix3x3 ans;
129         ans.m_row[0] = a*b.m_row[0];
130         ans.m_row[1] = a*b.m_row[1];
131         ans.m_row[2] = a*b.m_row[2];
132         return ans;
133 }
134
135 __inline
136 float4 mtMul3(const float4& a, const Matrix3x3& b)
137 {
138         float4 ans;
139         ans.x = a.x*b.m_row[0].x + a.y*b.m_row[1].x + a.z*b.m_row[2].x;
140         ans.y = a.x*b.m_row[0].y + a.y*b.m_row[1].y + a.z*b.m_row[2].y;
141         ans.z = a.x*b.m_row[0].z + a.y*b.m_row[1].z + a.z*b.m_row[2].z;
142         return ans;
143 }
144
145 __inline
146 Matrix3x3 mtInvert(const Matrix3x3& m)
147 {
148         float det = m.m_row[0].s[0]*m.m_row[1].s[1]*m.m_row[2].s[2]+m.m_row[1].s[0]*m.m_row[2].s[1]*m.m_row[0].s[2]+m.m_row[2].s[0]*m.m_row[0].s[1]*m.m_row[1].s[2]
149         -m.m_row[0].s[0]*m.m_row[2].s[1]*m.m_row[1].s[2]-m.m_row[2].s[0]*m.m_row[1].s[1]*m.m_row[0].s[2]-m.m_row[1].s[0]*m.m_row[0].s[1]*m.m_row[2].s[2];
150
151         CLASSERT( det );
152
153         Matrix3x3 ans;
154         ans.m_row[0].s[0] = m.m_row[1].s[1]*m.m_row[2].s[2] - m.m_row[1].s[2]*m.m_row[2].s[1];
155         ans.m_row[0].s[1] = m.m_row[0].s[2]*m.m_row[2].s[1] - m.m_row[0].s[1]*m.m_row[2].s[2];
156         ans.m_row[0].s[2] = m.m_row[0].s[1]*m.m_row[1].s[2] - m.m_row[0].s[2]*m.m_row[1].s[1];
157         ans.m_row[0].w = 0.f;
158
159         ans.m_row[1].s[0] = m.m_row[1].s[2]*m.m_row[2].s[0] - m.m_row[1].s[0]*m.m_row[2].s[2];
160         ans.m_row[1].s[1] = m.m_row[0].s[0]*m.m_row[2].s[2] - m.m_row[0].s[2]*m.m_row[2].s[0];
161         ans.m_row[1].s[2] = m.m_row[0].s[2]*m.m_row[1].s[0] - m.m_row[0].s[0]*m.m_row[1].s[2];
162         ans.m_row[1].w = 0.f;
163
164         ans.m_row[2].s[0] = m.m_row[1].s[0]*m.m_row[2].s[1] - m.m_row[1].s[1]*m.m_row[2].s[0];
165         ans.m_row[2].s[1] = m.m_row[0].s[1]*m.m_row[2].s[0] - m.m_row[0].s[0]*m.m_row[2].s[1];
166         ans.m_row[2].s[2] = m.m_row[0].s[0]*m.m_row[1].s[1] - m.m_row[0].s[1]*m.m_row[1].s[0];
167         ans.m_row[2].w = 0.f;
168
169         ans = mtMul2((1.0f/det), ans);
170         return ans;
171 }
172
173 __inline
174 Matrix3x3 mtSet( const float4& a, const float4& b, const float4& c )
175 {
176         Matrix3x3 m;
177         m.m_row[0] = a;
178         m.m_row[1] = b;
179         m.m_row[2] = c;
180         return m;
181 }
182
183 __inline
184 Matrix3x3 operator+(const Matrix3x3& a, const Matrix3x3& b)
185 {
186         Matrix3x3 out;
187         out.m_row[0] = a.m_row[0] + b.m_row[0];
188         out.m_row[1] = a.m_row[1] + b.m_row[1];
189         out.m_row[2] = a.m_row[2] + b.m_row[2];
190         return out;
191 }
192
193 #endif
194