aboutsummaryrefslogtreecommitdiff
path: root/sp/src/public/nmatrix.h
blob: 2b8d206ec8a432d3c330a8fdd3617056b7702737 (plain) (blame)
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================//

#ifndef NMATRIX_H
#define NMATRIX_H
#ifdef _WIN32
#pragma once
#endif


#include <assert.h>
#include "nvector.h"


#define NMatrixMN NMatrix<M,N>


template<int M, int N>
class NMatrix
{
public:

						NMatrixMN() {}
	
	static NMatrixMN	SetupNMatrixNull();					// Return a matrix of all zeros.
	static NMatrixMN	SetupNMatrixIdentity();				// Return an identity matrix.

	NMatrixMN const&	operator=( NMatrixMN const &other );

	NMatrixMN			operator+( NMatrixMN const &v ) const;
	NMatrixMN const&	operator+=( NMatrixMN const &v );

	NMatrixMN			operator-() const;
	NMatrixMN			operator-( NMatrixMN const &v ) const;

	// Multiplies the column vector on the right-hand side.
	NVector<M>			operator*( NVector<N> const &v ) const;

	// Can't get the compiler to work with a real MxN * NxR matrix multiply...
	NMatrix<M,M>		operator*( NMatrix<N,M> const &b ) const;

	NMatrixMN			operator*( float val ) const;
	
	bool				InverseGeneral( NMatrixMN &mInverse ) const;
	NMatrix<N,M>		Transpose() const;


public:

	float				m[M][N];
};



// Return the matrix generated by multiplying a column vector 'a' by row vector 'b'.
template<int N>
inline NMatrix<N,N> OuterProduct( NVectorN const &a, NVectorN const &b )
{
	NMatrix<N,N> ret;

	for( int i=0; i < N; i++ )
		for( int j=0; j < N; j++ )
			ret.m[i][j] = a.v[i] * b.v[j];

	return ret;
}


// -------------------------------------------------------------------------------- //
// NMatrix inlines.
// -------------------------------------------------------------------------------- //

template<int M, int N>
inline NMatrixMN NMatrixMN::SetupNMatrixNull()
{
	NMatrix ret;
	memset( ret.m, 0, sizeof(float)*M*N );
	return ret;
}


template<int M, int N>
inline NMatrixMN NMatrixMN::SetupNMatrixIdentity()
{
	assert( M == N );	// Identity matrices must be square.

	NMatrix ret;
	memset( ret.m, 0, sizeof(float)*M*N );
	for( int i=0; i < N; i++ )
		ret.m[i][i] = 1;
	return ret;
}


template<int M, int N>
inline NMatrixMN const &NMatrixMN::operator=( NMatrixMN const &v )
{
	memcpy( m, v.m, sizeof(float)*M*N );
	return *this;
}


template<int M, int N>
inline NMatrixMN NMatrixMN::operator+( NMatrixMN const &v ) const
{
	NMatrixMN ret;
	for( int i=0; i < M; i++ )
		for( int j=0; j < N; j++ )
			ret.m[i][j] = m[i][j] + v.m[i][j];

	return ret;
}


template<int M, int N>
inline NMatrixMN const &NMatrixMN::operator+=( NMatrixMN const &v )
{
	for( int i=0; i < M; i++ )
		for( int j=0; j < N; j++ )
			m[i][j] += v.m[i][j];

	return *this;
}


template<int M, int N>
inline NMatrixMN NMatrixMN::operator-() const
{
	NMatrixMN ret;
	
	for( int i=0; i < M*N; i++ )
		((float*)ret.m)[i] = -((float*)m)[i];
	
	return ret;
}


template<int M, int N>
inline NMatrixMN NMatrixMN::operator-( NMatrixMN const &v ) const
{
	NMatrixMN ret;
	for( int i=0; i < M; i++ )
		for( int j=0; j < N; j++ )
			ret.m[i][j] = m[i][j] - v.m[i][j];
	return ret;
}


template<int M, int N>
inline NVector<M> NMatrixMN::operator*( NVectorN const &v ) const
{
	NVectorN ret;

	for( int i=0; i < M; i++ )
	{
		ret.v[i] = 0;

		for( int j=0; j < N; j++ )
			ret.v[i] += m[i][j] * v.v[j];
	}
	
	return ret;
}


template<int M, int N>
inline NMatrix<M,M> NMatrixMN::operator*( NMatrix<N,M> const &b ) const
{
	NMatrix<M,M> ret;

	for( int myRow=0; myRow < M; myRow++ )
	{
		for( int otherCol=0; otherCol < M; otherCol++ )
		{
			ret[myRow][otherCol] = 0;
			for( int i=0; i < N; i++ )
				ret[myRow][otherCol] += a.m[myRow][i] * b.m[i][otherCol];
		}
	}

	return ret;
}


template<int M, int N>
inline NMatrixMN NMatrixMN::operator*( float val ) const
{
	NMatrixMN ret;

	for( int i=0; i < N*M; i++ )
		((float*)ret.m)[i] = ((float*)m)[i] * val;

	return ret;
}


template<int M, int N>
bool NMatrixMN::InverseGeneral( NMatrixMN &mInverse ) const
{
	int iRow, i, j, iTemp, iTest;
	float mul, fTest, fLargest;
	float mat[N][2*N];
	int rowMap[N], iLargest;
	float *pOut, *pRow, *pScaleRow;

	
	// Can only invert square matrices.
	if( M != N )
	{
		assert( !"Tried to invert a non-square matrix" );
		return false;
	}


	// How it's done.
	// AX = I
	// A = this
	// X = the matrix we're looking for
	// I = identity

	// Setup AI
	for(i=0; i < N; i++)
	{
		const float *pIn = m[i];
		pOut = mat[i];

		for(j=0; j < N; j++)
		{
			pOut[j] = pIn[j];
		}

		for(j=N; j < 2*N; j++)
			pOut[j] = 0;
		
		pOut[i+N] = 1.0f;

		rowMap[i] = i;
	}

	// Use row operations to get to reduced row-echelon form using these rules:
	// 1. Multiply or divide a row by a nonzero number.
	// 2. Add a multiple of one row to another.
	// 3. Interchange two rows.

	for(iRow=0; iRow < N; iRow++)
	{
		// Find the row with the largest element in this column.
		fLargest = 0.001f;
		iLargest = -1;
		for(iTest=iRow; iTest < N; iTest++)
		{
			fTest = (float)fabs(mat[rowMap[iTest]][iRow]);
			if(fTest > fLargest)
			{
				iLargest = iTest;
				fLargest = fTest;
			}
		}

		// They're all too small.. sorry.
		if(iLargest == -1)
		{
			return false;
		}

		// Swap the rows.
		iTemp = rowMap[iLargest];
		rowMap[iLargest] = rowMap[iRow];
		rowMap[iRow] = iTemp;

		pRow = mat[rowMap[iRow]];

		// Divide this row by the element.
		mul = 1.0f / pRow[iRow];
		for(j=0; j < 2*N; j++)
			pRow[j] *= mul;

		pRow[iRow] = 1.0f; // Preserve accuracy...
		
		// Eliminate this element from the other rows using operation 2.
		for(i=0; i < N; i++)
		{
			if(i == iRow)
				continue;

			pScaleRow = mat[rowMap[i]];
		
			// Multiply this row by -(iRow*the element).
			mul = -pScaleRow[iRow];
			for(j=0; j < 2*N; j++)
			{
				pScaleRow[j] += pRow[j] * mul;
			}

			pScaleRow[iRow] = 0.0f; // Preserve accuracy...
		}
	}

	// The inverse is on the right side of AX now (the identity is on the left).
	for(i=0; i < N; i++)
	{
		const float *pIn = mat[rowMap[i]] + N;
		pOut = mInverse.m[i];

		for(j=0; j < N; j++)
		{
			pOut[j] = pIn[j];
		}
	}

	return true;
}


template<int M, int N>
inline NMatrix<N,M> NMatrixMN::Transpose() const
{
	NMatrix<N,M> ret;
	
	for( int i=0; i < M; i++ )
		for( int j=0; j < N; j++ )
			ret.m[j][i] = m[i][j];
	
	return ret;
}

#endif // NMATRIX_H