Subversion Repository Public Repository

Nextrek

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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
 * Copyright (C) 2010- Peer internet solutions
 * 
 * This file was an original part of mixare.
 * 
 * This program is free software: you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version. 
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 * for more details. 
 * 
 * You should have received a copy of the GNU General Public License along with 
 * this program. If not, see <http://www.gnu.org/licenses/>
 */
package nextrek.math;


/**
 * A Matrix representation which adds many of the mathematical operations involved in Matrices.
 * 
 * This file was adapted from Mixare <http://www.mixare.org/>
 * 
 * @author Daniele Gobbetti <info@mixare.org>
 * @author Justin Wetherell <phishman3579@gmail.com>
 */
public class Matrix {
    private static final Vector worldUp = new Vector(0, 1, 0);
    private static final Vector dir = new Vector();
    private static final Vector right = new Vector();
    private static final Vector up = new Vector();
    private static final Matrix tmp = new Matrix();

    private static final float[] dirArray = new float[3];
    private static final float[] rightArray = new float[3];
    private static final float[] upArray = new float[3];
    
    private volatile float a1=0f, a2=0f, a3=0f;
    private volatile float b1=0f, b2=0f, b3=0f;
    private volatile float c1=0f, c2=0f, c3=0f;
    
    public Matrix() { }
    
    public Matrix(Matrix m) {
        if (m==null) throw new NullPointerException();

        set(m.a1,m. a2, m.a3, m.b1, m.b2, m.b3, m.c1, m.c2, m.c3);
    }
    
    public synchronized float getA1() {
        return a1;
    }
    public synchronized void setA1(float a1) {
        this.a1 = a1;
    }

    public synchronized float getA2() {
        return a2;
    }
    public synchronized void setA2(float a2) {
        this.a2 = a2;
    }

    public synchronized float getA3() {
        return a3;
    }
    public synchronized void setA3(float a3) {
        this.a3 = a3;
    }

    public synchronized float getB1() {
        return b1;
    }
    public synchronized void setB1(float b1) {
        this.b1 = b1;
    }

    public synchronized float getB2() {
        return b2;
    }
    public synchronized void setB2(float b2) {
        this.b2 = b2;
    }

    public synchronized float getB3() {
        return b3;
    }
    public synchronized void setB3(float b3) {
        this.b3 = b3;
    }

    public synchronized float getC1() {
        return c1;
    }
    public synchronized void setC1(float c1) {
        this.c1 = c1;
    }

    public synchronized float getC2() {
        return c2;
    }
    public synchronized void setC2(float c2) {
        this.c2 = c2;
    }

    public synchronized float getC3() {
        return c3;
    }
    public synchronized void setC3(float c3) {
        this.c3 = c3;
    }

    /**
     * Get the Matrix values.
     * 
     *  array[0] = a1
     *  array[1] = a2
     *  array[2] = a3
     *  array[3] = b1
     *  array[4] = b2
     *  array[5] = b3
     *  array[6] = c1
     *  array[7] = c2
     *  array[8] = c3
     * 
     * @param array float[] array of size containing the matrix data.
     */
    public synchronized void get(float[] array) {
        if (array==null || array.length!=9) 
            throw new IllegalArgumentException("get() array must be non-NULL and size of 9");
        
        array[0] = this.a1;
        array[1] = this.a2;
        array[2] = this.a3;

        array[3] = this.b1;
        array[4] = this.b2;
        array[5] = this.b3;

        array[6] = this.c1;
        array[7] = this.c2;
        array[8] = this.c3;
    }
    
    /**
     * Set the Matrix from a given Matrix.
     * @param m Matrix to use values from.
     */
    public void set(Matrix m) {
        if (m==null) throw new NullPointerException();

        set(m.a1,m. a2, m.a3, m.b1, m.b2, m.b3, m.c1, m.c2, m.c3);
    }

    /**
     * Set the Matrix values.
     * 
     *  array[0] = a1
     *  array[1] = a2
     *  array[2] = a3
     *  array[3] = b1
     *  array[4] = b2
     *  array[5] = b3
     *  array[6] = c1
     *  array[7] = c2
     *  array[8] = c3
     * 
     * @param array float[] array of size containing the matrix data.
     */
    public void set(float[] array) {
        if (array==null || array.length!=9) 
            throw new IllegalArgumentException("get() array must be non-NULL and size of 9");
        
        set(array[0], array[1], array[2],
            array[3], array[4], array[5],
            array[6], array[7], array[8]);
    }
    
    /**
     * Set the Matrix values.
     * @param a1 float representing the top row left value.
     * @param a2 float representing the top row middle value.
     * @param a3 float representing the top row right value.
     * @param b1 float representing the middle row left value.
     * @param b2 float representing the middle row left value.
     * @param b3 float representing the middle row left value.
     * @param c1 float representing the bottom row left value. 
     * @param c2 float representing the bottom row left value.
     * @param c3 float representing the bottom row left value.
     */
    public synchronized void set(float a1, float a2, float a3, float b1, float b2, float b3, float c1, float c2, float c3) {
        this.a1 = a1;
        this.a2 = a2;
        this.a3 = a3;

        this.b1 = b1;
        this.b2 = b2;
        this.b3 = b3;

        this.c1 = c1;
        this.c2 = c2;
        this.c3 = c3;
    }

    public void toIdentity() {
        set(1, 0, 0, 0, 1, 0, 0, 0, 1);
    }

    public void toXRot(float angleX) {
        set(  1f, 
                0f, 
                0f, 
                0f, 
                (float) Math.cos(angleX), 
                (float) -Math.sin(angleX), 
                0f, 
                (float) Math.sin(angleX), 
                (float) Math.cos(angleX)
        );
    }

    public void toYRot(float angleY) {
        set(  (float) Math.cos(angleY), 
                0f, (float) 
                Math.sin(angleY), 
                0f, 
                1f, 
                0f, 
                (float) -Math.sin(angleY), 
                0f, 
                (float) Math.cos(angleY)
        );
    }

    public void toZRot(float angleZ) {
        set(  (float) Math.cos(angleZ), 
                (float) -Math.sin(angleZ), 
                0f, 
                (float) Math.sin(angleZ), 
                (float) Math.cos(angleZ), 
                0f, 
                0f, 
                0f, 
                1f
        );
    }

    public void toScale(float scale) {
        set(scale, 0, 0, 0, scale, 0, 0, 0, scale);
    }

    public void toAt(Vector cam, Vector obj) {
        if (cam==null || obj==null) throw new NullPointerException();

        dir.set(0, 0, 0);
        dir.set(obj);
        dir.sub(cam);
        dir.mult(-1f);
        dir.norm();
        dir.get(dirArray);

        right.set(0, 0, 0);
        right.cross(worldUp, dir);
        right.norm();
        right.get(rightArray);

        up.set(0, 0, 0);
        up.cross(dir, right);
        up.norm();
        up.get(upArray);

        set(rightArray[0], rightArray[1], rightArray[2], upArray[0], upArray[1], upArray[2], dirArray[0], dirArray[1], dirArray[2]);
    }

    public synchronized void adj() {
        float a11 = this.a1;
        float a12 = this.a2;
        float a13 = this.a3;

        float a21 = this.b1;
        float a22 = this.b2;
        float a23 = this.b3;

        float a31 = this.c1;
        float a32 = this.c2;
        float a33 = this.c3;

        this.a1 = det2x2(a22, a23, a32, a33);
        this.a2 = det2x2(a13, a12, a33, a32);
        this.a3 = det2x2(a12, a13, a22, a23);

        this.b1 = det2x2(a23, a21, a33, a31);
        this.b2 = det2x2(a11, a13, a31, a33);
        this.b3 = det2x2(a13, a11, a23, a21);

        this.c1 = det2x2(a21, a22, a31, a32);
        this.c2 = det2x2(a12, a11, a32, a31);
        this.c3 = det2x2(a11, a12, a21, a22);
    }

    public void invert() {
        float det = this.det();

        adj();
        mult(1 / det);
    }

    public synchronized void transpose() {
        float a11 = this.a1;
        float a12 = this.a2;
        float a13 = this.a3;

        float a21 = this.b1;
        float a22 = this.b2;
        float a23 = this.b3;

        float a31 = this.c1;
        float a32 = this.c2;
        float a33 = this.c3;

        this.b1 = a12;
        this.a2 = a21;
        this.b3 = a32;
        this.c2 = a23;
        this.c1 = a13;
        this.a3 = a31;

        this.a1 = a11;
        this.b2 = a22;
        this.c3 = a33;
    }

    private float det2x2(float a, float b, float c, float d) {
        return (a * d) - (b * c);
    }

    public synchronized float det() {
        return (this.a1 * this.b2 * this.c3) - (this.a1 * this.b3 * this.c2) - (this.a2 * this.b1 * this.c3) +
        (this.a2 * this.b3 * this.c1) + (this.a3 * this.b1 * this.c2) - (this.a3 * this.b2 * this.c1);
    }

    public synchronized void mult(float c) {
        this.a1 = this.a1 * c;
        this.a2 = this.a2 * c;
        this.a3 = this.a3 * c;

        this.b1 = this.b1 * c;
        this.b2 = this.b2 * c;
        this.b3 = this.b3 * c;

        this.c1 = this.c1 * c;
        this.c2 = this.c2 * c;
        this.c3 = this.c3 * c;
    }

    public synchronized void add(Matrix n) {
        if (n==null) throw new NullPointerException();

        this.a1 += n.a1;
        this.a2 += n.a2;
        this.a3 += n.a3;

        this.b1 += n.b1;
        this.b2 += n.b2;
        this.b3 += n.b3;

        this.c1 += n.c1;
        this.c2 += n.c2;
        this.c3 += n.c3;
    }

    public synchronized void prod(Matrix n) {
        if (n==null) throw new NullPointerException();

        tmp.set(this);
        this.a1 = (tmp.a1 * n.a1) + (tmp.a2 * n.b1) + (tmp.a3 * n.c1);
        this.a2 = (tmp.a1 * n.a2) + (tmp.a2 * n.b2) + (tmp.a3 * n.c2);
        this.a3 = (tmp.a1 * n.a3) + (tmp.a2 * n.b3) + (tmp.a3 * n.c3);

        this.b1 = (tmp.b1 * n.a1) + (tmp.b2 * n.b1) + (tmp.b3 * n.c1);
        this.b2 = (tmp.b1 * n.a2) + (tmp.b2 * n.b2) + (tmp.b3 * n.c2);
        this.b3 = (tmp.b1 * n.a3) + (tmp.b2 * n.b3) + (tmp.b3 * n.c3);

        this.c1 = (tmp.c1 * n.a1) + (tmp.c2 * n.b1) + (tmp.c3 * n.c1);
        this.c2 = (tmp.c1 * n.a2) + (tmp.c2 * n.b2) + (tmp.c3 * n.c2);
        this.c3 = (tmp.c1 * n.a3) + (tmp.c2 * n.b3) + (tmp.c3 * n.c3);
    }

    public synchronized boolean equals(Matrix n) {
        if (n==null) return false;

        if (this.a1 != n.a1) return false;
        if (this.a2 != n.a2) return false;
        if (this.a3 != n.a3) return false;

        if (this.b1 != n.b1) return false;
        if (this.b2 != n.b2) return false;
        if (this.b3 != n.b3) return false;

        if (this.c1 != n.c1) return false;
        if (this.c2 != n.c2) return false;
        if (this.c3 != n.c3) return false;

        return true;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public synchronized String toString() {
        return "[ (" + this.a1 + "," + this.a2 + "," + this.a3 + ")"+
               " (" + this.b1 + "," + this.b2 + "," + this.b3 + ")"+
               " (" + this.c1 + "," + this.c2 + "," + this.c3 + ") ]";
    }
}

Commits for Nextrek/Android/LibrerieNextrek/src/nextrek/math/Matrix.java

Diff revisions: vs.
Revision Author Commited Message
4 FMMortaroli picture FMMortaroli Fri 19 Apr, 2013 16:54:38 +0000