~ubuntu-branches/ubuntu/precise/rss-glx/precise

« back to all changes in this revision

Viewing changes to src/rsMath/rsMatrix.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Steve Langasek
  • Date: 2009-06-03 18:41:32 UTC
  • mfrom: (1.1.5 upstream) (2.2.1 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090603184132-znjy66pq9xom7hac
Tags: 0.9.0-2ubuntu1
* Merge from debian unstable, remaining changes:
  - Drop dependency on openal. It is not enabled by default anyway, and
    allows openal to migrate to universe.
  - src/skyrocket.{cpp,xml}: Disable sound by default.
  - Add --disable-sound configure flag to debian/rules, as we don't keep 
    the dependencies on openal nor freeglut (both universe packages).
* Dropped changes, merged upstream:
  - other_src/Makefile.am: fix the upstream build rules to actually use
    the ImageMagick CFLAGS returned by pkg-config.
  - Move the unconditional ImageMagick check up in configure.in so that
    our first PKG_CHECK_MODULES() call isn't hidden behind a shell
    conditional.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 1999-2005  Terence M. Welsh
 
3
 *
 
4
 * This file is part of rsMath.
 
5
 *
 
6
 * rsMath is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU Lesser General Public
 
8
 * License version 2.1 as published by the Free Software Foundation.
 
9
 *
 
10
 * rsMath is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU Lesser General Public
 
16
 * License along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 */
 
19
 
 
20
 
 
21
#include <rsMath/rsMath.h>
 
22
 
 
23
 
 
24
 
 
25
rsMatrix::rsMatrix(){
 
26
}
 
27
 
 
28
 
 
29
rsMatrix::~rsMatrix(){
 
30
}
 
31
 
 
32
 
 
33
void rsMatrix::identity(){
 
34
        m[0] = 1.0f;
 
35
        m[1] = 0.0f;
 
36
        m[2] = 0.0f;
 
37
        m[3] = 0.0f;
 
38
        m[4] = 0.0f;
 
39
        m[5] = 1.0f;
 
40
        m[6] = 0.0f;
 
41
        m[7] = 0.0f;
 
42
        m[8] = 0.0f;
 
43
        m[9] = 0.0f;
 
44
        m[10] = 1.0f;
 
45
        m[11] = 0.0f;
 
46
        m[12] = 0.0f;
 
47
        m[13] = 0.0f;
 
48
        m[14] = 0.0f;
 
49
        m[15] = 1.0f;
 
50
}
 
51
 
 
52
 
 
53
void rsMatrix::set(const float * const mat){
 
54
        int i;
 
55
        for(i=0; i<16; i++)
 
56
                m[i] = mat[i];
 
57
}
 
58
 
 
59
 
 
60
void rsMatrix::get(float* mat) const{
 
61
        int i;
 
62
        for(i=0; i<16; i++)
 
63
                mat[i] = m[i];
 
64
}
 
65
 
 
66
 
 
67
void rsMatrix::copy(const rsMatrix &mat){
 
68
        int i;
 
69
        for(i=0; i<16; i++)
 
70
                m[i] = mat[i];
 
71
}
 
72
 
 
73
 
 
74
void rsMatrix::preMult(const rsMatrix &postMat){
 
75
        float preMat[16];
 
76
 
 
77
        preMat[0] = m[0];
 
78
        preMat[1] = m[1];
 
79
        preMat[2] = m[2];
 
80
        preMat[3] = m[3];
 
81
        preMat[4] = m[4];
 
82
        preMat[5] = m[5];
 
83
        preMat[6] = m[6];
 
84
        preMat[7] = m[7];
 
85
        preMat[8] = m[8];
 
86
        preMat[9] = m[9];
 
87
        preMat[10] = m[10];
 
88
        preMat[11] = m[11];
 
89
        preMat[12] = m[12];
 
90
        preMat[13] = m[13];
 
91
        preMat[14] = m[14];
 
92
        preMat[15] = m[15];
 
93
 
 
94
        m[0] = preMat[0]*postMat[0] + preMat[4]*postMat[1] + preMat[8]*postMat[2] + preMat[12]*postMat[3];
 
95
        m[1] = preMat[1]*postMat[0] + preMat[5]*postMat[1] + preMat[9]*postMat[2] + preMat[13]*postMat[3];
 
96
        m[2] = preMat[2]*postMat[0] + preMat[6]*postMat[1] + preMat[10]*postMat[2] + preMat[14]*postMat[3];
 
97
        m[3] = preMat[3]*postMat[0] + preMat[7]*postMat[1] + preMat[11]*postMat[2] + preMat[15]*postMat[3];
 
98
        m[4] = preMat[0]*postMat[4] + preMat[4]*postMat[5] + preMat[8]*postMat[6] + preMat[12]*postMat[7];
 
99
        m[5] = preMat[1]*postMat[4] + preMat[5]*postMat[5] + preMat[9]*postMat[6] + preMat[13]*postMat[7];
 
100
        m[6] = preMat[2]*postMat[4] + preMat[6]*postMat[5] + preMat[10]*postMat[6] + preMat[14]*postMat[7];
 
101
        m[7] = preMat[3]*postMat[4] + preMat[7]*postMat[5] + preMat[11]*postMat[6] + preMat[15]*postMat[7];
 
102
        m[8] = preMat[0]*postMat[8] + preMat[4]*postMat[9] + preMat[8]*postMat[10] + preMat[12]*postMat[11];
 
103
        m[9] = preMat[1]*postMat[8] + preMat[5]*postMat[9] + preMat[9]*postMat[10] + preMat[13]*postMat[11];
 
104
        m[10] = preMat[2]*postMat[8] + preMat[6]*postMat[9] + preMat[10]*postMat[10] + preMat[14]*postMat[11];
 
105
        m[11] = preMat[3]*postMat[8] + preMat[7]*postMat[9] + preMat[11]*postMat[10] + preMat[15]*postMat[11];
 
106
        m[12] = preMat[0]*postMat[12] + preMat[4]*postMat[13] + preMat[8]*postMat[14] + preMat[12]*postMat[15];
 
107
        m[13] = preMat[1]*postMat[12] + preMat[5]*postMat[13] + preMat[9]*postMat[14] + preMat[13]*postMat[15];
 
108
        m[14] = preMat[2]*postMat[12] + preMat[6]*postMat[13] + preMat[10]*postMat[14] + preMat[14]*postMat[15];
 
109
        m[15] = preMat[3]*postMat[12] + preMat[7]*postMat[13] + preMat[11]*postMat[14] + preMat[15]*postMat[15];
 
110
}
 
111
 
 
112
 
 
113
void rsMatrix::postMult(const rsMatrix &preMat){
 
114
        float postMat[16];
 
115
 
 
116
        postMat[0] = m[0];
 
117
        postMat[1] = m[1];
 
118
        postMat[2] = m[2];
 
119
        postMat[3] = m[3];
 
120
        postMat[4] = m[4];
 
121
        postMat[5] = m[5];
 
122
        postMat[6] = m[6];
 
123
        postMat[7] = m[7];
 
124
        postMat[8] = m[8];
 
125
        postMat[9] = m[9];
 
126
        postMat[10] = m[10];
 
127
        postMat[11] = m[11];
 
128
        postMat[12] = m[12];
 
129
        postMat[13] = m[13];
 
130
        postMat[14] = m[14];
 
131
        postMat[15] = m[15];
 
132
 
 
133
        m[0] = preMat[0]*postMat[0] + preMat[4]*postMat[1] + preMat[8]*postMat[2] + preMat[12]*postMat[3];
 
134
        m[1] = preMat[1]*postMat[0] + preMat[5]*postMat[1] + preMat[9]*postMat[2] + preMat[13]*postMat[3];
 
135
        m[2] = preMat[2]*postMat[0] + preMat[6]*postMat[1] + preMat[10]*postMat[2] + preMat[14]*postMat[3];
 
136
        m[3] = preMat[3]*postMat[0] + preMat[7]*postMat[1] + preMat[11]*postMat[2] + preMat[15]*postMat[3];
 
137
        m[4] = preMat[0]*postMat[4] + preMat[4]*postMat[5] + preMat[8]*postMat[6] + preMat[12]*postMat[7];
 
138
        m[5] = preMat[1]*postMat[4] + preMat[5]*postMat[5] + preMat[9]*postMat[6] + preMat[13]*postMat[7];
 
139
        m[6] = preMat[2]*postMat[4] + preMat[6]*postMat[5] + preMat[10]*postMat[6] + preMat[14]*postMat[7];
 
140
        m[7] = preMat[3]*postMat[4] + preMat[7]*postMat[5] + preMat[11]*postMat[6] + preMat[15]*postMat[7];
 
141
        m[8] = preMat[0]*postMat[8] + preMat[4]*postMat[9] + preMat[8]*postMat[10] + preMat[12]*postMat[11];
 
142
        m[9] = preMat[1]*postMat[8] + preMat[5]*postMat[9] + preMat[9]*postMat[10] + preMat[13]*postMat[11];
 
143
        m[10] = preMat[2]*postMat[8] + preMat[6]*postMat[9] + preMat[10]*postMat[10] + preMat[14]*postMat[11];
 
144
        m[11] = preMat[3]*postMat[8] + preMat[7]*postMat[9] + preMat[11]*postMat[10] + preMat[15]*postMat[11];
 
145
        m[12] = preMat[0]*postMat[12] + preMat[4]*postMat[13] + preMat[8]*postMat[14] + preMat[12]*postMat[15];
 
146
        m[13] = preMat[1]*postMat[12] + preMat[5]*postMat[13] + preMat[9]*postMat[14] + preMat[13]*postMat[15];
 
147
        m[14] = preMat[2]*postMat[12] + preMat[6]*postMat[13] + preMat[10]*postMat[14] + preMat[14]*postMat[15];
 
148
        m[15] = preMat[3]*postMat[12] + preMat[7]*postMat[13] + preMat[11]*postMat[14] + preMat[15]*postMat[15];
 
149
}
 
150
 
 
151
 
 
152
float rsMatrix::determinant() const{
 
153
        return(m[0] * m[5] * m[10] * m[15]
 
154
                + m[4] * m[9] * m[14] * m[3]
 
155
                + m[8] * m[13] * m[2] * m[7]
 
156
                + m[12] * m[1] * m[6] * m[11]
 
157
                - m[3] * m[6] * m[9] * m[12]
 
158
                - m[7] * m[10] * m[13] * m[0]
 
159
                - m[11] * m[14] * m[1] * m[4]
 
160
                - m[15] * m[2] * m[5] * m[8]);
 
161
}
 
162
 
 
163
 
 
164
void rsMatrix::makeTrans(const float &x, const float &y, const float &z){
 
165
        m[0] = 1.0f;
 
166
        m[1] = 0.0f;
 
167
        m[2] = 0.0f;
 
168
        m[3] = 0.0f;
 
169
        m[4] = 0.0f;
 
170
        m[5] = 1.0f;
 
171
        m[6] = 0.0f;
 
172
        m[7] = 0.0f;
 
173
        m[8] = 0.0f;
 
174
        m[9] = 0.0f;
 
175
        m[10] = 1.0f;
 
176
        m[11] = 0.0f;
 
177
        m[12] = x;
 
178
        m[13] = y;
 
179
        m[14] = z;
 
180
        m[15] = 1.0f;
 
181
}
 
182
 
 
183
void rsMatrix::makeTrans(const float * const p){
 
184
        m[0] = 1.0f;
 
185
        m[1] = 0.0f;
 
186
        m[2] = 0.0f;
 
187
        m[3] = 0.0f;
 
188
        m[4] = 0.0f;
 
189
        m[5] = 1.0f;
 
190
        m[6] = 0.0f;
 
191
        m[7] = 0.0f;
 
192
        m[8] = 0.0f;
 
193
        m[9] = 0.0f;
 
194
        m[10] = 1.0f;
 
195
        m[11] = 0.0f;
 
196
        m[12] = p[0];
 
197
        m[13] = p[1];
 
198
        m[14] = p[2];
 
199
        m[15] = 1.0f;
 
200
}
 
201
 
 
202
void rsMatrix::makeTrans(const rsVec &vec){
 
203
        m[0] = 1.0f;
 
204
        m[1] = 0.0f;
 
205
        m[2] = 0.0f;
 
206
        m[3] = 0.0f;
 
207
        m[4] = 0.0f;
 
208
        m[5] = 1.0f;
 
209
        m[6] = 0.0f;
 
210
        m[7] = 0.0f;
 
211
        m[8] = 0.0f;
 
212
        m[9] = 0.0f;
 
213
        m[10] = 1.0f;
 
214
        m[11] = 0.0f;
 
215
        m[12] = vec[0];
 
216
        m[13] = vec[1];
 
217
        m[14] = vec[2];
 
218
        m[15] = 1.0f;
 
219
}
 
220
 
 
221
 
 
222
void rsMatrix::makeScale(const float &s){
 
223
        m[0] = s;
 
224
        m[1] = 0.0f;
 
225
        m[2] = 0.0f;
 
226
        m[3] = 0.0f;
 
227
        m[4] = 0.0f;
 
228
        m[5] = s;
 
229
        m[6] = 0.0f;
 
230
        m[7] = 0.0f;
 
231
        m[8] = 0.0f;
 
232
        m[9] = 0.0f;
 
233
        m[10] = s;
 
234
        m[11] = 0.0f;
 
235
        m[12] = 0.0f;
 
236
        m[13] = 0.0f;
 
237
        m[14] = 0.0f;
 
238
        m[15] = 1.0f;
 
239
}
 
240
 
 
241
void rsMatrix::makeScale(const float &x, const float &y, const float &z){
 
242
        m[0] = x;
 
243
        m[1] = 0.0f;
 
244
        m[2] = 0.0f;
 
245
        m[3] = 0.0f;
 
246
        m[4] = 0.0f;
 
247
        m[5] = y;
 
248
        m[6] = 0.0f;
 
249
        m[7] = 0.0f;
 
250
        m[8] = 0.0f;
 
251
        m[9] = 0.0f;
 
252
        m[10] = z;
 
253
        m[11] = 0.0f;
 
254
        m[12] = 0.0f;
 
255
        m[13] = 0.0f;
 
256
        m[14] = 0.0f;
 
257
        m[15] = 1.0f;
 
258
}
 
259
 
 
260
void rsMatrix::makeScale(const float * const s){
 
261
        m[0] = s[0];
 
262
        m[1] = 0.0f;
 
263
        m[2] = 0.0f;
 
264
        m[3] = 0.0f;
 
265
        m[4] = 0.0f;
 
266
        m[5] = s[1];
 
267
        m[6] = 0.0f;
 
268
        m[7] = 0.0f;
 
269
        m[8] = 0.0f;
 
270
        m[9] = 0.0f;
 
271
        m[10] = s[2];
 
272
        m[11] = 0.0f;
 
273
        m[12] = 0.0f;
 
274
        m[13] = 0.0f;
 
275
        m[14] = 0.0f;
 
276
        m[15] = 1.0f;
 
277
}
 
278
 
 
279
void rsMatrix::makeScale(const rsVec &vec){
 
280
        m[0] = vec[0];
 
281
        m[1] = 0.0f;
 
282
        m[2] = 0.0f;
 
283
        m[3] = 0.0f;
 
284
        m[4] = 0.0f;
 
285
        m[5] = vec[1];
 
286
        m[6] = 0.0f;
 
287
        m[7] = 0.0f;
 
288
        m[8] = 0.0f;
 
289
        m[9] = 0.0f;
 
290
        m[10] = vec[2];
 
291
        m[11] = 0.0f;
 
292
        m[12] = 0.0f;
 
293
        m[13] = 0.0f;
 
294
        m[14] = 0.0f;
 
295
        m[15] = 1.0f;
 
296
}
 
297
 
 
298
 
 
299
void rsMatrix::makeRot(float a, const float &x, const float &y, const float &z){
 
300
        rsQuat q;
 
301
        q.make(a, x, y, z);
 
302
        q.toMat(m);
 
303
}
 
304
 
 
305
void rsMatrix::makeRot(float a, const rsVec &v){
 
306
        rsQuat q;
 
307
        q.make(a, v);
 
308
        q.toMat(m);
 
309
}
 
310
 
 
311
void rsMatrix::makeRot(const rsQuat &q){
 
312
        q.toMat(m);
 
313
}
 
314
 
 
315
 
 
316
int rsMatrix::invert(const rsMatrix &mat){
 
317
        float rmat[4][8];
 
318
        float a, b;
 
319
        int i, j, k;
 
320
 
 
321
        // initialize reduction matrix
 
322
        rmat[0][0] = mat[0];
 
323
        rmat[1][0] = mat[1];
 
324
        rmat[2][0] = mat[2];
 
325
        rmat[3][0] = mat[3];
 
326
        rmat[0][1] = mat[4];
 
327
        rmat[1][1] = mat[5];
 
328
        rmat[2][1] = mat[6];
 
329
        rmat[3][1] = mat[7];
 
330
        rmat[0][2] = mat[8];
 
331
        rmat[1][2] = mat[9];
 
332
        rmat[2][2] = mat[10];
 
333
        rmat[3][2] = mat[11];
 
334
        rmat[0][3] = mat[12];
 
335
        rmat[1][3] = mat[13];
 
336
        rmat[2][3] = mat[14];
 
337
        rmat[3][3] = mat[15];
 
338
        rmat[0][4] = 1.0f;
 
339
        rmat[1][4] = 0.0f;
 
340
        rmat[2][4] = 0.0f;
 
341
        rmat[3][4] = 0.0f;
 
342
        rmat[0][5] = 0.0f;
 
343
        rmat[1][5] = 1.0f;
 
344
        rmat[2][5] = 0.0f;
 
345
        rmat[3][5] = 0.0f;
 
346
        rmat[0][6] = 0.0f;
 
347
        rmat[1][6] = 0.0f;
 
348
        rmat[2][6] = 1.0f;
 
349
        rmat[3][6] = 0.0f;
 
350
        rmat[0][7] = 0.0f;
 
351
        rmat[1][7] = 0.0f;
 
352
        rmat[2][7] = 0.0f;
 
353
        rmat[3][7] = 1.0f;
 
354
 
 
355
        // perform reductions
 
356
        for(i=0; i<4; i++){
 
357
                a = rmat[i][i];
 
358
                if(a == 0.0f)  // matrix is singular, can't be reduced
 
359
                        return 0;
 
360
                else{
 
361
                        a = 1.0f / a;
 
362
                        for(j=0; j<8; j++)
 
363
                                rmat[i][j] = rmat[i][j] * a;
 
364
                        for(k=0; k<4; k++){
 
365
                                if((k-i) != 0){
 
366
                                        b = rmat[k][i];
 
367
                                        for(j=0; j<8; j++)
 
368
                                                rmat[k][j] = rmat[k][j] - b * rmat[i][j];
 
369
                                }
 
370
                        }
 
371
                }
 
372
        }
 
373
 
 
374
        // extract the inverted matrix
 
375
        m[0] = rmat[0][4];
 
376
        m[1] = rmat[1][4];
 
377
        m[2] = rmat[2][4];
 
378
        m[3] = rmat[3][4];
 
379
        m[4] = rmat[0][5];
 
380
        m[5] = rmat[1][5];
 
381
        m[6] = rmat[2][5];
 
382
        m[7] = rmat[3][5];
 
383
        m[8] = rmat[0][6];
 
384
        m[9] = rmat[1][6];
 
385
        m[10] = rmat[2][6];
 
386
        m[11] = rmat[3][6];
 
387
        m[12] = rmat[0][7];
 
388
        m[13] = rmat[1][7];
 
389
        m[14] = rmat[2][7];
 
390
        m[15] = rmat[3][7];
 
391
 
 
392
        return 1;
 
393
}
 
394
 
 
395
 
 
396
void rsMatrix::rotationInvert(const rsMatrix &mat){
 
397
        float det = mat[0] * mat[5] * mat[10]
 
398
                + mat[4] * mat[9] * mat[2]
 
399
                + mat[8] * mat[1] * mat[6]
 
400
                - mat[2] * mat[5] * mat[8]
 
401
                - mat[6] * mat[9] * mat[0]
 
402
                - mat[10] * mat[1] * mat[4];
 
403
 
 
404
        m[0] = (mat[5] * mat[10] - mat[6] * mat[9]) / det;
 
405
        m[1] = (mat[6] * mat[8] - mat[4] * mat[10]) / det;
 
406
        m[2] = (mat[4] * mat[9] - mat[5] * mat[8]) / det;
 
407
        m[4] = (mat[9] * mat[2] - mat[10] * mat[1]) / det;
 
408
        m[5] = (mat[10] * mat[0] - mat[8] * mat[2]) / det;
 
409
        m[6] = (mat[8] * mat[1] - mat[9] * mat[0]) / det;
 
410
        m[8] = (mat[1] * mat[6] - mat[2] * mat[5]) / det;
 
411
        m[9] = (mat[2] * mat[4] - mat[0] * mat[6]) / det;
 
412
        m[10] = (mat[0] * mat[5] - mat[1] * mat[4]) / det;
 
413
        m[3] = m[7] = m[11] = m[12] = m[13] = m[14] = 0.0f;
 
414
        m[15] = 1.0f;
 
415
}
 
416
 
 
417
 
 
418
void rsMatrix::fromQuat(const rsQuat &q){
 
419
        // must have an axis
 
420
        if(q[0] == 0.0f && q[1] == 0.0f && q[2] == 0.0f){
 
421
                identity();
 
422
                return;
 
423
        }
 
424
 
 
425
        const float s = 2.0f / (q[0] * q[0] + q[1] * q[1] + q[2] * q[2] + q[3] * q[3]);
 
426
        const float xs = q[0] * s;
 
427
        const float ys = q[1] * s;
 
428
        const float zs = q[2] * s;
 
429
        const float wx = q[3] * xs;
 
430
        const float wy = q[3] * ys;
 
431
        const float wz = q[3] * zs;
 
432
        const float xx = q[0] * xs;
 
433
        const float xy = q[0] * ys;
 
434
        const float xz = q[0] * zs;
 
435
        const float yy = q[1] * ys;
 
436
        const float yz = q[1] * zs;
 
437
        const float zz = q[2] * zs;
 
438
 
 
439
        m[0] = 1.0f - yy - zz;
 
440
        m[1] = xy + wz;
 
441
        m[2] = xz - wy;
 
442
        m[3] = 0.0f;
 
443
        m[4] = xy - wz;
 
444
        m[5] = 1.0f - xx - zz;
 
445
        m[6] = yz + wx;
 
446
        m[7] = 0.0f;
 
447
        m[8] = xz + wy;
 
448
        m[9] = yz - wx;
 
449
        m[10] = 1.0f - xx - yy;
 
450
        m[11] = 0.0f;
 
451
        m[12] = 0.0f;
 
452
        m[13] = 0.0f;
 
453
        m[14] = 0.0f;
 
454
        m[15] = 1.0f;
 
455
}
 
456
 
 
457
 
 
458
 
 
459
rsMatrix & rsMatrix::operator = (const rsMatrix &mat){
 
460
        m[0]=mat[0]; m[1]=mat[1]; m[2]=mat[2]; m[3]=mat[3];
 
461
        m[4]=mat[4]; m[5]=mat[5]; m[6]=mat[6]; m[7]=mat[7];
 
462
        m[8]=mat[8]; m[9]=mat[9]; m[10]=mat[10]; m[11]=mat[11];
 
463
        m[12]=mat[12]; m[13]=mat[13]; m[14]=mat[14]; m[15]=mat[15];
 
464
        return *this;
 
465
}