summaryrefslogtreecommitdiff
path: root/thirdparty/bullet/Bullet3Common/shared/b3Mat3x3.h
blob: ce6482b5a6c47f5cd09f77faeb662cd9d9f4f961 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157

#ifndef B3_MAT3x3_H
#define B3_MAT3x3_H

#include "Bullet3Common/shared/b3Quat.h"

#ifdef __cplusplus

#include "Bullet3Common/b3Matrix3x3.h"

#define b3Mat3x3 b3Matrix3x3
#define b3Mat3x3ConstArg const b3Matrix3x3&

inline b3Mat3x3 b3QuatGetRotationMatrix(b3QuatConstArg quat)
{
	return b3Mat3x3(quat);
}

inline b3Mat3x3 b3AbsoluteMat3x3(b3Mat3x3ConstArg mat)
{
	return mat.absolute();
}

#define b3GetRow(m, row) m.getRow(row)

__inline b3Float4 mtMul3(b3Float4ConstArg a, b3Mat3x3ConstArg b)
{
	return b * a;
}

#else

typedef struct
{
	b3Float4 m_row[3];
} b3Mat3x3;

#define b3Mat3x3ConstArg const b3Mat3x3
#define b3GetRow(m, row) (m.m_row[row])

inline b3Mat3x3 b3QuatGetRotationMatrix(b3Quat quat)
{
	b3Float4 quat2 = (b3Float4)(quat.x * quat.x, quat.y * quat.y, quat.z * quat.z, 0.f);
	b3Mat3x3 out;

	out.m_row[0].x = 1 - 2 * quat2.y - 2 * quat2.z;
	out.m_row[0].y = 2 * quat.x * quat.y - 2 * quat.w * quat.z;
	out.m_row[0].z = 2 * quat.x * quat.z + 2 * quat.w * quat.y;
	out.m_row[0].w = 0.f;

	out.m_row[1].x = 2 * quat.x * quat.y + 2 * quat.w * quat.z;
	out.m_row[1].y = 1 - 2 * quat2.x - 2 * quat2.z;
	out.m_row[1].z = 2 * quat.y * quat.z - 2 * quat.w * quat.x;
	out.m_row[1].w = 0.f;

	out.m_row[2].x = 2 * quat.x * quat.z - 2 * quat.w * quat.y;
	out.m_row[2].y = 2 * quat.y * quat.z + 2 * quat.w * quat.x;
	out.m_row[2].z = 1 - 2 * quat2.x - 2 * quat2.y;
	out.m_row[2].w = 0.f;

	return out;
}

inline b3Mat3x3 b3AbsoluteMat3x3(b3Mat3x3ConstArg matIn)
{
	b3Mat3x3 out;
	out.m_row[0] = fabs(matIn.m_row[0]);
	out.m_row[1] = fabs(matIn.m_row[1]);
	out.m_row[2] = fabs(matIn.m_row[2]);
	return out;
}

__inline b3Mat3x3 mtZero();

__inline b3Mat3x3 mtIdentity();

__inline b3Mat3x3 mtTranspose(b3Mat3x3 m);

__inline b3Mat3x3 mtMul(b3Mat3x3 a, b3Mat3x3 b);

__inline b3Float4 mtMul1(b3Mat3x3 a, b3Float4 b);

__inline b3Float4 mtMul3(b3Float4 a, b3Mat3x3 b);

__inline b3Mat3x3 mtZero()
{
	b3Mat3x3 m;
	m.m_row[0] = (b3Float4)(0.f);
	m.m_row[1] = (b3Float4)(0.f);
	m.m_row[2] = (b3Float4)(0.f);
	return m;
}

__inline b3Mat3x3 mtIdentity()
{
	b3Mat3x3 m;
	m.m_row[0] = (b3Float4)(1, 0, 0, 0);
	m.m_row[1] = (b3Float4)(0, 1, 0, 0);
	m.m_row[2] = (b3Float4)(0, 0, 1, 0);
	return m;
}

__inline b3Mat3x3 mtTranspose(b3Mat3x3 m)
{
	b3Mat3x3 out;
	out.m_row[0] = (b3Float4)(m.m_row[0].x, m.m_row[1].x, m.m_row[2].x, 0.f);
	out.m_row[1] = (b3Float4)(m.m_row[0].y, m.m_row[1].y, m.m_row[2].y, 0.f);
	out.m_row[2] = (b3Float4)(m.m_row[0].z, m.m_row[1].z, m.m_row[2].z, 0.f);
	return out;
}

__inline b3Mat3x3 mtMul(b3Mat3x3 a, b3Mat3x3 b)
{
	b3Mat3x3 transB;
	transB = mtTranspose(b);
	b3Mat3x3 ans;
	//	why this doesn't run when 0ing in the for{}
	a.m_row[0].w = 0.f;
	a.m_row[1].w = 0.f;
	a.m_row[2].w = 0.f;
	for (int i = 0; i < 3; i++)
	{
		//	a.m_row[i].w = 0.f;
		ans.m_row[i].x = b3Dot3F4(a.m_row[i], transB.m_row[0]);
		ans.m_row[i].y = b3Dot3F4(a.m_row[i], transB.m_row[1]);
		ans.m_row[i].z = b3Dot3F4(a.m_row[i], transB.m_row[2]);
		ans.m_row[i].w = 0.f;
	}
	return ans;
}

__inline b3Float4 mtMul1(b3Mat3x3 a, b3Float4 b)
{
	b3Float4 ans;
	ans.x = b3Dot3F4(a.m_row[0], b);
	ans.y = b3Dot3F4(a.m_row[1], b);
	ans.z = b3Dot3F4(a.m_row[2], b);
	ans.w = 0.f;
	return ans;
}

__inline b3Float4 mtMul3(b3Float4 a, b3Mat3x3 b)
{
	b3Float4 colx = b3MakeFloat4(b.m_row[0].x, b.m_row[1].x, b.m_row[2].x, 0);
	b3Float4 coly = b3MakeFloat4(b.m_row[0].y, b.m_row[1].y, b.m_row[2].y, 0);
	b3Float4 colz = b3MakeFloat4(b.m_row[0].z, b.m_row[1].z, b.m_row[2].z, 0);

	b3Float4 ans;
	ans.x = b3Dot3F4(a, colx);
	ans.y = b3Dot3F4(a, coly);
	ans.z = b3Dot3F4(a, colz);
	return ans;
}

#endif

#endif  //B3_MAT3x3_H