File: dgMatrix.h

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (371 lines) | stat: -rw-r--r-- 15,969 bytes parent folder | download | duplicates (3)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/* Copyright (c) <2003-2011> <Julio Jerez, Newton Game Dynamics>
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 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.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*/

#ifndef __dgMatrix__
#define __dgMatrix__


#include "dgStdafx.h"
#include "dgDebug.h"
#include "dgVector.h"
#include "dgPlane.h"
#include "dgSimd_Instrutions.h"
#include <math.h>

class dgMatrix;
class dgQuaternion;

const dgMatrix &dgGetZeroMatrix();
const dgMatrix &dgGetIdentityMatrix();


DG_MSC_VECTOR_ALIGMENT
class dgMatrix {
public:
	DG_CLASS_ALLOCATOR(allocator)

	dgMatrix();
	constexpr dgMatrix(const dgVector &front, const dgVector &up, const dgVector &right, const dgVector &posit);
	dgMatrix(const dgQuaternion &rotation, const dgVector &position);

	// create a orthonormal normal vector basis
	dgMatrix(const dgVector &front);




	dgVector &operator[](dgInt32 i);
	const dgVector &operator[](dgInt32 i) const;

	dgMatrix Inverse() const;
	dgMatrix Inverse4x4() const;
	dgMatrix Transpose() const;
	dgMatrix Transpose4X4() const;
	dgMatrix Symetric3by3Inverse() const;
	dgVector RotateVector(const dgVector &v) const;
	dgVector UnrotateVector(const dgVector &v) const;
	dgVector TransformVector(const dgVector &v) const;
	dgVector UntransformVector(const dgVector &v) const;
	dgPlane TransformPlane(const dgPlane &localPlane) const;
	dgPlane UntransformPlane(const dgPlane &globalPlane) const;
	void TransformBBox(const dgVector &p0local, const dgVector &p1local, dgVector &p0, dgVector &p1) const;

	dgVector CalcPitchYawRoll() const;


	void TransformTriplex(dgFloat32 *const dst, dgInt32 dstStrideInBytes,
	                      const dgFloat32 *const src, dgInt32 srcStrideInBytes, dgInt32 count) const;

#ifndef __USE_DOUBLE_PRECISION__
	void TransformTriplex(dgFloat64 *const dst, dgInt32 dstStrideInBytes,
	                      const dgFloat64 *const src, dgInt32 srcStrideInBytes, dgInt32 count) const;
	void TransformTriplex(dgFloat64 *const dst, dgInt32 dstStrideInBytes,
	                      const dgFloat32 *const src, dgInt32 srcStrideInBytes, dgInt32 count) const;
#endif

	dgMatrix operator* (const dgMatrix &B) const;


	// this function can not be a member of dgMatrix, because
	// dgMatrix a define to handle only orthogonal matrices
	// and this function take a parameter to a symmetric matrix
	void EigenVectors(dgVector &eigenValues, const dgMatrix &initialGuess = dgGetIdentityMatrix());
	void EigenVectors(const dgMatrix &initialGuess = dgGetIdentityMatrix());


	// simd operations
	dgMatrix InverseSimd() const;
	dgMatrix MultiplySimd(const dgMatrix &B) const;
	dgVector RotateVectorSimd(const dgVector &v) const;
	dgVector UnrotateVectorSimd(const dgVector &v) const;
	dgVector TransformVectorSimd(const dgVector &v) const;
	void TransformVectorsSimd(dgVector *const dst, const dgVector *const src, dgInt32 count) const;

	dgVector m_front;
	dgVector m_up;
	dgVector m_right;
	dgVector m_posit;
} DG_GCC_VECTOR_ALIGMENT;





DG_INLINE dgMatrix::dgMatrix() {
}

constexpr DG_INLINE dgMatrix::dgMatrix(const dgVector &front, const dgVector &up, const dgVector &right, const dgVector &posit)
	: m_front(front), m_up(up), m_right(right), m_posit(posit) {
}

DG_INLINE dgMatrix::dgMatrix(const dgVector &front) {
	m_front = front;
	if (dgAbsf(front.m_z) > dgFloat32(0.577f)) {
		m_right = front * dgVector(-front.m_y, front.m_z, dgFloat32(0.0f), dgFloat32(0.0f));
	} else {
		m_right = front * dgVector(-front.m_y, front.m_x, dgFloat32(0.0f), dgFloat32(0.0f));
	}
	m_right = m_right.Scale(dgRsqrt(m_right % m_right));
	m_up = m_right * m_front;

	m_front.m_w = dgFloat32(0.0f);
	m_up.m_w = dgFloat32(0.0f);
	m_right.m_w = dgFloat32(0.0f);
	m_posit = dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(1.0f));

	NEWTON_ASSERT((dgAbsf(m_front % m_front) - dgFloat32(1.0f)) < dgFloat32(1.0e-5f));
	NEWTON_ASSERT((dgAbsf(m_up % m_up) - dgFloat32(1.0f)) < dgFloat32(1.0e-5f));
	NEWTON_ASSERT((dgAbsf(m_right % m_right) - dgFloat32(1.0f)) < dgFloat32(1.0e-5f));
	NEWTON_ASSERT((dgAbsf(m_right % (m_front * m_up)) - dgFloat32(1.0f)) < dgFloat32(1.0e-5f));
}


DG_INLINE dgVector &dgMatrix::operator[](dgInt32  i) {
	NEWTON_ASSERT(i < 4);
	NEWTON_ASSERT(i >= 0);
	return (&m_front)[i];
}

DG_INLINE const dgVector &dgMatrix::operator[](dgInt32  i) const {
	NEWTON_ASSERT(i < 4);
	NEWTON_ASSERT(i >= 0);
	return (&m_front)[i];
}


DG_INLINE dgMatrix dgMatrix::Transpose() const {
	return dgMatrix(dgVector(m_front.m_x, m_up.m_x, m_right.m_x, dgFloat32(0.0f)),
	                dgVector(m_front.m_y, m_up.m_y, m_right.m_y, dgFloat32(0.0f)),
	                dgVector(m_front.m_z, m_up.m_z, m_right.m_z, dgFloat32(0.0f)),
	                dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(1.0f)));
}

DG_INLINE dgMatrix dgMatrix::Transpose4X4() const {
	return dgMatrix(dgVector(m_front.m_x, m_up.m_x, m_right.m_x, m_posit.m_x),
	                dgVector(m_front.m_y, m_up.m_y, m_right.m_y, m_posit.m_y),
	                dgVector(m_front.m_z, m_up.m_z, m_right.m_z, m_posit.m_z),
	                dgVector(m_front.m_w, m_up.m_w, m_right.m_w, m_posit.m_w));

}

DG_INLINE dgVector dgMatrix::RotateVector(const dgVector &v) const {
	return dgVector(v.m_x * m_front.m_x + v.m_y * m_up.m_x + v.m_z * m_right.m_x,
	                v.m_x * m_front.m_y + v.m_y * m_up.m_y + v.m_z * m_right.m_y,
	                v.m_x * m_front.m_z + v.m_y * m_up.m_z + v.m_z * m_right.m_z, v.m_w);
}


DG_INLINE dgVector dgMatrix::UnrotateVector(const dgVector &v) const {
	return dgVector(v % m_front, v % m_up, v % m_right, v.m_w);
}


DG_INLINE dgVector dgMatrix::TransformVector(const dgVector &v) const {
//	return m_posit + RotateVector(v);
	return dgVector(v.m_x * m_front.m_x + v.m_y * m_up.m_x + v.m_z * m_right.m_x + m_posit.m_x,
	                v.m_x * m_front.m_y + v.m_y * m_up.m_y + v.m_z * m_right.m_y + m_posit.m_y,
	                v.m_x * m_front.m_z + v.m_y * m_up.m_z + v.m_z * m_right.m_z + m_posit.m_z, v.m_w);

}

DG_INLINE dgVector dgMatrix::UntransformVector(const dgVector &v) const {
	return UnrotateVector(v - m_posit);
}

DG_INLINE dgPlane dgMatrix::TransformPlane(const dgPlane &localPlane) const {
	return dgPlane(RotateVector(localPlane), localPlane.m_w - (localPlane % UnrotateVector(m_posit)));
}

DG_INLINE dgPlane dgMatrix::UntransformPlane(const dgPlane &globalPlane) const {
	return dgPlane(UnrotateVector(globalPlane), globalPlane.Evalue(m_posit));
}

DG_INLINE void dgMatrix::EigenVectors(const dgMatrix &initialGuess) {
	dgVector eigenValues;
	EigenVectors(eigenValues, initialGuess);
}


DG_INLINE dgMatrix dgPitchMatrix(dgFloat32 ang) {
	dgFloat32 cosAng;
	dgFloat32 sinAng;
	sinAng = dgSin(ang);
	cosAng = dgCos(ang);
	return dgMatrix(dgVector(dgFloat32(1.0f),  dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f)),
	                dgVector(dgFloat32(0.0f),  cosAng,          sinAng,          dgFloat32(0.0f)),
	                dgVector(dgFloat32(0.0f), -sinAng,          cosAng,          dgFloat32(0.0f)),
	                dgVector(dgFloat32(0.0f),  dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(1.0f)));

}

DG_INLINE dgMatrix dgYawMatrix(dgFloat32 ang) {
	dgFloat32 cosAng;
	dgFloat32 sinAng;
	sinAng = dgSin(ang);
	cosAng = dgCos(ang);
	return dgMatrix(dgVector(cosAng,          dgFloat32(0.0f), -sinAng,          dgFloat32(0.0f)),
	                dgVector(dgFloat32(0.0f), dgFloat32(1.0f),  dgFloat32(0.0f), dgFloat32(0.0f)),
	                dgVector(sinAng,          dgFloat32(0.0f),  cosAng,          dgFloat32(0.0f)),
	                dgVector(dgFloat32(0.0f), dgFloat32(0.0f),  dgFloat32(0.0f), dgFloat32(1.0f)));
}

DG_INLINE dgMatrix dgRollMatrix(dgFloat32 ang) {
	dgFloat32 cosAng;
	dgFloat32 sinAng;
	sinAng = dgSin(ang);
	cosAng = dgCos(ang);
	return dgMatrix(dgVector(cosAng,          sinAng,          dgFloat32(0.0f), dgFloat32(0.0f)),
	                dgVector(-sinAng,          cosAng,          dgFloat32(0.0f), dgFloat32(0.0f)),
	                dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(1.0f), dgFloat32(0.0f)),
	                dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(1.0f)));
}


DG_INLINE dgMatrix dgMatrix::Inverse() const {
	return dgMatrix(dgVector(m_front.m_x, m_up.m_x, m_right.m_x, dgFloat32(0.0f)),
	                dgVector(m_front.m_y, m_up.m_y, m_right.m_y, dgFloat32(0.0f)),
	                dgVector(m_front.m_z, m_up.m_z, m_right.m_z, dgFloat32(0.0f)),
	                dgVector(- (m_posit % m_front), - (m_posit % m_up), - (m_posit % m_right), dgFloat32(1.0f)));
}

DG_INLINE dgVector dgMatrix::TransformVectorSimd(const dgVector &v) const {
#ifdef DG_BUILD_SIMD_CODE
	const dgMatrix &source = *this;
	return dgVector(simd_mul_add_v(
	                    simd_mul_add_v(
	                        simd_mul_add_v((simd_type &) source[3], (simd_type &) source[0], simd_permut_v((simd_type &) v, (simd_type &) v, PURMUT_MASK(0, 0, 0, 0))),
	                        (simd_type &) source[1], simd_permut_v((simd_type &) v, (simd_type &) v, PURMUT_MASK(1, 1, 1, 1))),
	                    (simd_type &) source[2], simd_permut_v((simd_type &) v, (simd_type &) v, PURMUT_MASK(2, 2, 2, 2))));
#else
	return dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));
#endif
}

DG_INLINE void dgMatrix::TransformVectorsSimd(dgVector *const dst, const dgVector *const src, dgInt32 count) const {
#ifdef DG_BUILD_SIMD_CODE
	const dgMatrix &source = *this;
	for (dgInt32 i = 0; i < count; i ++) {
		(simd_type &)dst[i] = simd_mul_add_v(
		                          simd_mul_add_v(
		                              simd_mul_add_v((simd_type &) source[3],
		                                      (simd_type &) source[0], simd_permut_v((simd_type &) src[i], (simd_type &) src[i], PURMUT_MASK(0, 0, 0, 0))),
		                              (simd_type &) source[1], simd_permut_v((simd_type &) src[i], (simd_type &) src[i], PURMUT_MASK(1, 1, 1, 1))),
		                          (simd_type &) source[2], simd_permut_v((simd_type &) src[i], (simd_type &) src[i], PURMUT_MASK(2, 2, 2, 2)));
	}
#endif
}


DG_INLINE dgVector dgMatrix::RotateVectorSimd(const dgVector &v) const {
#ifdef DG_BUILD_SIMD_CODE
	const dgMatrix &source = *this;
	return dgVector(simd_mul_add_v(
	                    simd_mul_add_v(
	                        simd_mul_v((simd_type &) source[0], simd_permut_v((simd_type &) v, (simd_type &) v, PURMUT_MASK(0, 0, 0, 0))),
	                        (simd_type &) source[1], simd_permut_v((simd_type &) v, (simd_type &) v, PURMUT_MASK(1, 1, 1, 1))),
	                    (simd_type &) source[2], simd_permut_v((simd_type &) v, (simd_type &) v, PURMUT_MASK(2, 2, 2, 2))));

#else
	return dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));
#endif
}

DG_INLINE dgVector dgMatrix::UnrotateVectorSimd(const dgVector &v) const {
#ifdef DG_BUILD_SIMD_CODE
	return dgVector(v.DotProductSimd(m_front), v.DotProductSimd(m_up), v.DotProductSimd(m_right), v.m_w);
#else
	return dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));
#endif
}

DG_INLINE dgMatrix dgMatrix::InverseSimd() const {
#ifdef DG_BUILD_SIMD_CODE
	simd_type r0;
	simd_type r1;
	simd_type r2;
	dgMatrix matrix;
	const dgMatrix &source = *this;

	NEWTON_ASSERT((dgUnsigned64(this) & 0x0f) == 0);

	r2 = simd_set1(dgFloat32(0.0f));
	r0 = simd_pack_lo_v((simd_type &) source[0], (simd_type &) source[1]);
	r1 = simd_pack_lo_v((simd_type &) source[2], r2);
	(simd_type &) matrix[0] = simd_move_lh_v(r0, r1);
	(simd_type &) matrix[1] = simd_move_hl_v(r1, r0);
	r0 = simd_pack_hi_v((simd_type &) source[0], (simd_type &) source[1]);
	r1 = simd_pack_hi_v((simd_type &) source[2], r2);
	(simd_type &) matrix[2] = simd_move_lh_v(r0, r1);

	(simd_type &) matrix[3] = simd_sub_v(r2,
	                                     simd_mul_add_v(
	                                             simd_mul_add_v(simd_mul_v((simd_type &) matrix[0], simd_permut_v((simd_type &) source[3], (simd_type &) source[3], PURMUT_MASK(3, 0, 0, 0))),
	                                                     (simd_type &) matrix[1], simd_permut_v((simd_type &) source[3], (simd_type &) source[3], PURMUT_MASK(3, 1, 1, 1))),
	                                             (simd_type &) matrix[2], simd_permut_v((simd_type &) source[3], (simd_type &) source[3], PURMUT_MASK(3, 2, 2, 2))));
	matrix[3][3] = dgFloat32(1.0f);
	return matrix;


#else
	return dgGetIdentityMatrix();
#endif
}

DG_INLINE dgMatrix dgMatrix::MultiplySimd(const dgMatrix &B) const {
#ifdef DG_BUILD_SIMD_CODE
	const dgMatrix &A = *this;
	return dgMatrix(dgVector(simd_mul_add_v(
	                             simd_mul_add_v(
	                                 simd_mul_add_v(simd_mul_v((simd_type &) B[0], simd_permut_v((simd_type &) A[0], (simd_type &) A[0], PURMUT_MASK(0, 0, 0, 0))),
	                                         (simd_type &) B[1], simd_permut_v((simd_type &) A[0], (simd_type &) A[0], PURMUT_MASK(1, 1, 1, 1))),
	                                 (simd_type &) B[2], simd_permut_v((simd_type &) A[0], (simd_type &) A[0], PURMUT_MASK(2, 2, 2, 2))),
	                             (simd_type &) B[3], simd_permut_v((simd_type &) A[0], (simd_type &) A[0], PURMUT_MASK(3, 3, 3, 3)))),

	                dgVector(simd_mul_add_v(
	                             simd_mul_add_v(
	                                 simd_mul_add_v(simd_mul_v((simd_type &) B[0], simd_permut_v((simd_type &) A[1], (simd_type &) A[1], PURMUT_MASK(0, 0, 0, 0))),
	                                         (simd_type &) B[1], simd_permut_v((simd_type &) A[1], (simd_type &) A[1], PURMUT_MASK(1, 1, 1, 1))),
	                                 (simd_type &) B[2], simd_permut_v((simd_type &) A[1], (simd_type &) A[1], PURMUT_MASK(2, 2, 2, 2))),
	                             (simd_type &) B[3], simd_permut_v((simd_type &) A[1], (simd_type &) A[1], PURMUT_MASK(3, 3, 3, 3)))),

	                dgVector(simd_mul_add_v(
	                             simd_mul_add_v(
	                                 simd_mul_add_v(simd_mul_v((simd_type &) B[0], simd_permut_v((simd_type &) A[2], (simd_type &) A[2], PURMUT_MASK(0, 0, 0, 0))),
	                                         (simd_type &) B[1], simd_permut_v((simd_type &) A[2], (simd_type &) A[2], PURMUT_MASK(1, 1, 1, 1))),
	                                 (simd_type &) B[2], simd_permut_v((simd_type &) A[2], (simd_type &) A[2], PURMUT_MASK(2, 2, 2, 2))),
	                             (simd_type &) B[3], simd_permut_v((simd_type &) A[2], (simd_type &) A[2], PURMUT_MASK(3, 3, 3, 3)))),


	                dgVector(simd_mul_add_v(
	                             simd_mul_add_v(
	                                 simd_mul_add_v(simd_mul_v((simd_type &) B[0], simd_permut_v((simd_type &) A[3], (simd_type &) A[3], PURMUT_MASK(0, 0, 0, 0))),
	                                         (simd_type &) B[1], simd_permut_v((simd_type &) A[3], (simd_type &) A[3], PURMUT_MASK(1, 1, 1, 1))),
	                                 (simd_type &) B[2], simd_permut_v((simd_type &) A[3], (simd_type &) A[3], PURMUT_MASK(2, 2, 2, 2))),
	                             (simd_type &) B[3], simd_permut_v((simd_type &) A[3], (simd_type &) A[3], PURMUT_MASK(3, 3, 3, 3)))));
#else
	return dgGetIdentityMatrix();

#endif
}


#endif