summaryrefslogtreecommitdiff
path: root/thirdparty/thekla_atlas/nvmath/Matrix.cpp
blob: 29bd19f5f857bfecea2867ad8c49ed72b0035069 (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
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// This code is in the public domain -- castanyo@yahoo.es

#include "Matrix.inl"
#include "Vector.inl"

#include "nvcore/Array.inl"

#include <float.h>

#if !NV_CC_MSVC && !NV_OS_ORBIS
#include <alloca.h>
#endif

using namespace nv;


// Given a matrix a[1..n][1..n], this routine replaces it by the LU decomposition of a rowwise
// permutation of itself. a and n are input. a is output, arranged as in equation (2.3.14) above;
// indx[1..n] is an output vector that records the row permutation effected by the partial
// pivoting; d is output as -1 depending on whether the number of row interchanges was even
// or odd, respectively. This routine is used in combination with lubksb to solve linear equations
// or invert a matrix.
static bool ludcmp(float **a, int n, int *indx, float *d)
{
    const float TINY = 1.0e-20f;

    float * vv = (float*)alloca(sizeof(float) * n);    // vv stores the implicit scaling of each row.

    *d = 1.0; // No row interchanges yet.
    for (int i = 0; i < n; i++) { // Loop over rows to get the implicit scaling information.
    
        float big = 0.0;
        for (int j = 0; j < n; j++) {
            big = max(big, fabsf(a[i][j]));
        }
        if (big == 0) {
            return false;   // Singular matrix
        }
        
        // No nonzero largest element.
        vv[i] = 1.0f / big; // Save the scaling.
    }

    for (int j = 0; j < n; j++) {       // This is the loop over columns of Crout's method.
        for (int i = 0; i < j; i++) {   // This is equation (2.3.12) except for i = j.
            float sum = a[i][j];
            for (int k = 0; k < i; k++) sum -= a[i][k]*a[k][j];
            a[i][j] = sum;
        }

        int imax = -1;
        float big = 0.0;                // Initialize for the search for largest pivot element.
        for (int i = j; i < n; i++) {   // This is i = j of equation (2.3.12) and i = j+ 1 : : : N
            float sum = a[i][j];              // of equation (2.3.13).
            for (int k = 0; k < j; k++) {
                sum -= a[i][k]*a[k][j];
            }
            a[i][j]=sum;

            float dum = vv[i]*fabs(sum);
            if (dum >= big) {
                // Is the figure of merit for the pivot better than the best so far?
                big = dum;
                imax = i;
            }
        }
        nvDebugCheck(imax != -1);

        if (j != imax) {                // Do we need to interchange rows?
            for (int k = 0; k < n; k++) {   // Yes, do so...
                swap(a[imax][k], a[j][k]);
            }
            *d = -(*d); // ...and change the parity of d.
            vv[imax]=vv[j]; // Also interchange the scale factor.
        }

        indx[j]=imax;
        if (a[j][j] == 0.0) a[j][j] = TINY;
        
        // If the pivot element is zero the matrix is singular (at least to the precision of the
        // algorithm). For some applications on singular matrices, it is desirable to substitute
        // TINY for zero.
        if (j != n-1) { // Now, finally, divide by the pivot element.
            float dum = 1.0f / a[j][j];
            for (int i = j+1; i < n; i++) a[i][j] *= dum;
        }
    } // Go back for the next column in the reduction.

    return true;
}


// Solves the set of n linear equations Ax = b. Here a[1..n][1..n] is input, not as the matrix
// A but rather as its LU decomposition, determined by the routine ludcmp. indx[1..n] is input
// as the permutation vector returned by ludcmp. b[1..n] is input as the right-hand side vector
// B, and returns with the solution vector X. a, n, and indx are not modified by this routine
// and can be left in place for successive calls with different right-hand sides b. This routine takes
// into account the possibility that b will begin with many zero elements, so it is efficient for use
// in matrix inversion.
static void lubksb(float **a, int n, int *indx, float b[])
{
    int ii = 0;
    for (int i=0; i<n; i++) {   // When ii is set to a positive value, it will become 
        int ip = indx[i];       // the index of the first nonvanishing element of b. We now 
        float sum = b[ip];      // do the forward substitution, equation (2.3.6). The 
        b[ip] = b[i];           // only new wrinkle is to unscramble the permutation as we go.
        if (ii != 0) {
            for (int j = ii-1; j < i; j++) sum -= a[i][j]*b[j];
        }
        else if (sum != 0.0f) {
            ii = i+1;             // A nonzero element was encountered, so from now on we 
        }
        b[i] = sum;             // will have to do the sums in the loop above.
    }
    for (int i=n-1; i>=0; i--) {  // Now we do the backsubstitution, equation (2.3.7).
        float sum = b[i];
        for (int j = i+1; j < n; j++) {
            sum -= a[i][j]*b[j];
        }
        b[i] = sum/a[i][i];     // Store a component of the solution vector X.
    } // All done!
}


bool nv::solveLU(const Matrix & A, const Vector4 & b, Vector4 * x)
{
    nvDebugCheck(x != NULL);

    float m[4][4];
    float *a[4] = {m[0], m[1], m[2], m[3]};
    int idx[4];
    float d;

    for (int y = 0; y < 4; y++) {
        for (int x = 0; x < 4; x++) {
            a[x][y] = A(x, y);
        }
    }

    // Create LU decomposition.
    if (!ludcmp(a, 4, idx, &d)) {
        // Singular matrix.
        return false;
    }

    // Init solution.
    *x = b;

    // Do back substitution.
    lubksb(a, 4, idx, x->component);

    return true;
}

// @@ Not tested.
Matrix nv::inverseLU(const Matrix & A)
{
    Vector4 Ai[4];

    solveLU(A, Vector4(1, 0, 0, 0), &Ai[0]);
    solveLU(A, Vector4(0, 1, 0, 0), &Ai[1]);
    solveLU(A, Vector4(0, 0, 1, 0), &Ai[2]);
    solveLU(A, Vector4(0, 0, 0, 1), &Ai[3]);

    return Matrix(Ai[0], Ai[1], Ai[2], Ai[3]);
}



bool nv::solveLU(const Matrix3 & A, const Vector3 & b, Vector3 * x)
{
    nvDebugCheck(x != NULL);

    float m[3][3];
    float *a[3] = {m[0], m[1], m[2]};
    int idx[3];
    float d;

    for (int y = 0; y < 3; y++) {
        for (int x = 0; x < 3; x++) {
            a[x][y] = A(x, y);
        }
    }

    // Create LU decomposition.
    if (!ludcmp(a, 3, idx, &d)) {
        // Singular matrix.
        return false;
    }

    // Init solution.
    *x = b;

    // Do back substitution.
    lubksb(a, 3, idx, x->component);

    return true;
}


bool nv::solveCramer(const Matrix & A, const Vector4 & b, Vector4 * x)
{
    nvDebugCheck(x != NULL);

    *x = transform(inverseCramer(A), b);
    
    return true; // @@ Return false if determinant(A) == 0 !
}

bool nv::solveCramer(const Matrix3 & A, const Vector3 & b, Vector3 * x)
{
    nvDebugCheck(x != NULL);

    const float det = A.determinant();
    if (equal(det, 0.0f)) {   // @@ Use input epsilon.
        return false;
    }

    Matrix3 Ai = inverseCramer(A);

    *x = transform(Ai, b);
    
    return true;
}



// Inverse using gaussian elimination. From Jon's code.
Matrix nv::inverse(const Matrix & m) {

    Matrix A = m;
    Matrix B(identity);

    int i, j, k;
    float max, t, det, pivot;

    det = 1.0;
    for (i=0; i<4; i++) {               /* eliminate in column i, below diag */
        max = -1.;
        for (k=i; k<4; k++)             /* find pivot for column i */
            if (fabs(A(k, i)) > max) {
                max = fabs(A(k, i));
                j = k;
            }
        if (max<=0.) return B;         /* if no nonzero pivot, PUNT */
        if (j!=i) {                     /* swap rows i and j */
            for (k=i; k<4; k++)
                swap(A(i, k), A(j, k));
            for (k=0; k<4; k++)
                swap(B(i, k), B(j, k));
            det = -det;
        }
        pivot = A(i, i);
        det *= pivot;
        for (k=i+1; k<4; k++)           /* only do elems to right of pivot */
            A(i, k) /= pivot;
        for (k=0; k<4; k++)
            B(i, k) /= pivot;
        /* we know that A(i, i) will be set to 1, so don't bother to do it */

        for (j=i+1; j<4; j++) {         /* eliminate in rows below i */
            t = A(j, i);                /* we're gonna zero this guy */
            for (k=i+1; k<4; k++)       /* subtract scaled row i from row j */
                A(j, k) -= A(i, k)*t;   /* (ignore k<=i, we know they're 0) */
            for (k=0; k<4; k++)
                B(j, k) -= B(i, k)*t;
        }
    }

    /*---------- backward elimination ----------*/

    for (i=4-1; i>0; i--) {             /* eliminate in column i, above diag */
        for (j=0; j<i; j++) {           /* eliminate in rows above i */
            t = A(j, i);                /* we're gonna zero this guy */
            for (k=0; k<4; k++)         /* subtract scaled row i from row j */
                B(j, k) -= B(i, k)*t;
        }
    }

    return B;
}


Matrix3 nv::inverse(const Matrix3 & m) {

    Matrix3 A = m;
    Matrix3 B(identity);

    int i, j, k;
    float max, t, det, pivot;

    det = 1.0;
    for (i=0; i<3; i++) {               /* eliminate in column i, below diag */
        max = -1.;
        for (k=i; k<3; k++)             /* find pivot for column i */
            if (fabs(A(k, i)) > max) {
                max = fabs(A(k, i));
                j = k;
            }
        if (max<=0.) return B;         /* if no nonzero pivot, PUNT */
        if (j!=i) {                     /* swap rows i and j */
            for (k=i; k<3; k++)
                swap(A(i, k), A(j, k));
            for (k=0; k<3; k++)
                swap(B(i, k), B(j, k));
            det = -det;
        }
        pivot = A(i, i);
        det *= pivot;
        for (k=i+1; k<3; k++)           /* only do elems to right of pivot */
            A(i, k) /= pivot;
        for (k=0; k<3; k++)
            B(i, k) /= pivot;
        /* we know that A(i, i) will be set to 1, so don't bother to do it */

        for (j=i+1; j<3; j++) {         /* eliminate in rows below i */
            t = A(j, i);                /* we're gonna zero this guy */
            for (k=i+1; k<3; k++)       /* subtract scaled row i from row j */
                A(j, k) -= A(i, k)*t;   /* (ignore k<=i, we know they're 0) */
            for (k=0; k<3; k++)
                B(j, k) -= B(i, k)*t;
        }
    }

    /*---------- backward elimination ----------*/

    for (i=3-1; i>0; i--) {             /* eliminate in column i, above diag */
        for (j=0; j<i; j++) {           /* eliminate in rows above i */
            t = A(j, i);                /* we're gonna zero this guy */
            for (k=0; k<3; k++)         /* subtract scaled row i from row j */
                B(j, k) -= B(i, k)*t;
        }
    }

    return B;
}





#if 0 

// Copyright (C) 1999-2004 Michael Garland.
// 
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, provided that the above
// copyright notice(s) and this permission notice appear in all copies of
// the Software and that both the above copyright notice(s) and this
// permission notice appear in supporting documentation.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
// OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
// HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
// INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
// FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
// NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
// WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
// 
// Except as contained in this notice, the name of a copyright holder
// shall not be used in advertising or otherwise to promote the sale, use
// or other dealings in this Software without prior written authorization
// of the copyright holder.


// Matrix inversion code for 4x4 matrices using Gaussian elimination
// with partial pivoting.  This is a specialized version of a
// procedure originally due to Paul Heckbert <ph@cs.cmu.edu>.
//
// Returns determinant of A, and B=inverse(A)
// If matrix A is singular, returns 0 and leaves trash in B.
//
#define SWAP(a, b, t)   {t = a; a = b; b = t;}
double invert(Mat4& B, const Mat4& m)
{
    Mat4 A = m;
    int i, j, k;
    double max, t, det, pivot;

    /*---------- forward elimination ----------*/

    for (i=0; i<4; i++)                 /* put identity matrix in B */
        for (j=0; j<4; j++)
            B(i, j) = (double)(i==j);

    det = 1.0;
    for (i=0; i<4; i++) {               /* eliminate in column i, below diag */
        max = -1.;
        for (k=i; k<4; k++)             /* find pivot for column i */
            if (fabs(A(k, i)) > max) {
                max = fabs(A(k, i));
                j = k;
            }
        if (max<=0.) return 0.;         /* if no nonzero pivot, PUNT */
        if (j!=i) {                     /* swap rows i and j */
            for (k=i; k<4; k++)
                SWAP(A(i, k), A(j, k), t);
            for (k=0; k<4; k++)
                SWAP(B(i, k), B(j, k), t);
            det = -det;
        }
        pivot = A(i, i);
        det *= pivot;
        for (k=i+1; k<4; k++)           /* only do elems to right of pivot */
            A(i, k) /= pivot;
        for (k=0; k<4; k++)
            B(i, k) /= pivot;
        /* we know that A(i, i) will be set to 1, so don't bother to do it */

        for (j=i+1; j<4; j++) {         /* eliminate in rows below i */
            t = A(j, i);                /* we're gonna zero this guy */
            for (k=i+1; k<4; k++)       /* subtract scaled row i from row j */
                A(j, k) -= A(i, k)*t;   /* (ignore k<=i, we know they're 0) */
            for (k=0; k<4; k++)
                B(j, k) -= B(i, k)*t;
        }
    }

    /*---------- backward elimination ----------*/

    for (i=4-1; i>0; i--) {             /* eliminate in column i, above diag */
        for (j=0; j<i; j++) {           /* eliminate in rows above i */
            t = A(j, i);                /* we're gonna zero this guy */
            for (k=0; k<4; k++)         /* subtract scaled row i from row j */
                B(j, k) -= B(i, k)*t;
        }
    }

    return det;
}

#endif // 0