[37] | 1 | /* |
---|
| 2 | |
---|
| 3 | Brian Curless |
---|
| 4 | |
---|
| 5 | Computer Graphics Laboratory |
---|
| 6 | Stanford University |
---|
| 7 | |
---|
| 8 | --------------------------------------------------------------------- |
---|
| 9 | |
---|
| 10 | Copyright (1997) The Board of Trustees of the Leland Stanford Junior |
---|
| 11 | University. Except for commercial resale, lease, license or other |
---|
| 12 | commercial transactions, permission is hereby given to use, copy, |
---|
| 13 | modify this software for academic purposes only. No part of this |
---|
| 14 | software or any derivatives thereof may be used in the production of |
---|
| 15 | computer models for resale or for use in a commercial |
---|
| 16 | product. STANFORD MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND |
---|
| 17 | CONCERNING THIS SOFTWARE. No support is implied or provided. |
---|
| 18 | |
---|
| 19 | */ |
---|
| 20 | |
---|
| 21 | #include "defines.h" |
---|
| 22 | #include "Matrix2f.h" |
---|
| 23 | #include <math.h> |
---|
| 24 | #include <stdio.h> |
---|
| 25 | #include <unistd.h> |
---|
| 26 | |
---|
| 27 | /*#include "NR.h"*/ |
---|
| 28 | |
---|
| 29 | void |
---|
| 30 | Matrix2f::setValue(float a[][2]) |
---|
| 31 | { |
---|
| 32 | m[0][0] = a[0][0]; m[0][1] = a[0][1]; |
---|
| 33 | m[1][0] = a[1][0]; m[1][1] = a[1][1]; |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | void |
---|
| 37 | Matrix2f::setValue(const Matrix2f &a) |
---|
| 38 | { |
---|
| 39 | m[0][0] = a.m[0][0]; m[0][1] = a.m[0][1]; |
---|
| 40 | m[1][0] = a.m[1][0]; m[1][1] = a.m[1][1]; |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | void |
---|
| 44 | Matrix2f::setValue(float *a0, float *a1) |
---|
| 45 | { |
---|
| 46 | m[0][0] = a0[0]; m[0][1] = a0[1]; |
---|
| 47 | m[1][0] = a1[0]; m[1][1] = a1[1]; |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | void |
---|
| 51 | Matrix2f::setValue(float a00, float a01, float a10, float a11) |
---|
| 52 | { |
---|
| 53 | m[0][0] = a00; m[0][1] = a01; |
---|
| 54 | m[1][0] = a10; m[1][1] = a11; |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | |
---|
| 58 | void |
---|
| 59 | Matrix2f::multArray(float *in, float *out) |
---|
| 60 | { |
---|
| 61 | out[0] = m[0][0] * in[0] + m[0][1] * in[1]; |
---|
| 62 | out[1] = m[1][0] * in[0] + m[1][1] * in[1]; |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | void |
---|
| 66 | Matrix2f::setScale(float s) |
---|
| 67 | { |
---|
| 68 | setScale(s, s); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | |
---|
| 72 | void |
---|
| 73 | Matrix2f::setScale(float sx, float sy) |
---|
| 74 | { |
---|
| 75 | m[0][0] = sx; m[0][1] = 0; |
---|
| 76 | m[1][0] = 0; m[1][1] = sy; |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | void |
---|
| 80 | Matrix2f::makeIdentity() |
---|
| 81 | { |
---|
| 82 | setValue(1,0, |
---|
| 83 | 0,1); |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | Matrix2f |
---|
| 88 | Matrix2f::inverse() const |
---|
| 89 | { |
---|
| 90 | Matrix2f result; |
---|
| 91 | int i, j, k; |
---|
| 92 | double temp; |
---|
| 93 | double bigm[4][2]; |
---|
| 94 | /* Declare identity matrix */ |
---|
| 95 | |
---|
| 96 | result.makeIdentity(); |
---|
| 97 | for (i = 0; i < 2; i++) { |
---|
| 98 | for (j = 0; j < 2; j++) { |
---|
| 99 | bigm[i][j] = m[i][j]; |
---|
| 100 | bigm[i+2][j] = result.m[i][j]; |
---|
| 101 | } |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | /* Work across by columns */ |
---|
| 105 | for (i = 0; i < 2; i++) { |
---|
| 106 | for (j = i; (bigm[i][j] == 0.0) && (j < 2); j++) |
---|
| 107 | ; |
---|
| 108 | if (j == 2) { |
---|
| 109 | fprintf (stderr, "error: cannot do inverse matrix\n"); |
---|
| 110 | exit (2); |
---|
| 111 | } |
---|
| 112 | else if (i != j) { |
---|
| 113 | for (k = 0; k < 4; k++) { |
---|
| 114 | temp = bigm[k][i]; |
---|
| 115 | bigm[k][i] = bigm[k][j]; |
---|
| 116 | bigm[k][j] = temp; |
---|
| 117 | } |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | /* Divide original row */ |
---|
| 121 | for (j = 3; j >= i; j--) |
---|
| 122 | bigm[j][i] /= bigm[i][i]; |
---|
| 123 | |
---|
| 124 | /* Subtract other rows */ |
---|
| 125 | for (j = 0; j < 2; j++) |
---|
| 126 | if (i != j) |
---|
| 127 | for (k = 3; k >= i; k--) |
---|
| 128 | bigm[k][j] -= bigm[k][i] * bigm[i][j]; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | for (i = 0; i < 2; i++) |
---|
| 132 | for (j = 0; j < 2; j++) |
---|
| 133 | result.m[i][j] = bigm[i+2][j]; |
---|
| 134 | |
---|
| 135 | return result; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | |
---|
| 139 | |
---|
| 140 | /* |
---|
| 141 | Matrix2f |
---|
| 142 | Matrix2f::inverse() const |
---|
| 143 | { |
---|
| 144 | Matrix2f temp, result; |
---|
| 145 | int i; |
---|
| 146 | |
---|
| 147 | temp.setValue(*this); |
---|
| 148 | float *nrmat[2]; |
---|
| 149 | |
---|
| 150 | toNR(temp, nrmat); |
---|
| 151 | |
---|
| 152 | // See NRC book |
---|
| 153 | float d, col[2]; |
---|
| 154 | int indx[2]; |
---|
| 155 | ludcmp(nrmat-1, 2, indx-1, &d); |
---|
| 156 | for (int j = 0; j < 2; j++) { |
---|
| 157 | for (i = 0; i < 2; i++) col[i] = 0; |
---|
| 158 | col[j] = 1.0; |
---|
| 159 | lubksb(nrmat-1, 2, indx-1, col-1); |
---|
| 160 | for(i = 0; i < 2; i++) result.m[i][j] = col[i]; |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | return Matrix2f(result); |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | |
---|
| 167 | void |
---|
| 168 | Matrix2f::toNR(Matrix2f &in, float **nrmat) const |
---|
| 169 | { |
---|
| 170 | nrmat[0] = in.m[0] - 1; |
---|
| 171 | nrmat[1] = in.m[1] - 1; |
---|
| 172 | } |
---|
| 173 | */ |
---|
| 174 | |
---|