00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <assert.h>
00023 #include <math.h>
00024
00025 #include "vec3d.h"
00026 #include "xform3d.h"
00027 #include "matrix33.h"
00028
00029 void Matrix33_MakeCrossProductMatrix33(const geVec3d* v,
00030 Matrix33* m)
00031 {
00032 assert(v != NULL);
00033 assert(m != NULL);
00034
00035 m->x[0][0] = m->x[1][1] = m->x[2][2] = 0.0f;
00036 m->x[0][1] = -v->Z; m->x[0][2] = v->Y;
00037 m->x[1][0] = v->Z; m->x[1][2] = -v->X;
00038 m->x[2][0] = -v->Y; m->x[2][1] = v->X;
00039 }
00040
00041 void Matrix33_ExtractFromXForm3d(const geXForm3d* xform, Matrix33* m)
00042 {
00043 assert(xform != NULL);
00044 assert(m != NULL);
00045
00046 m->x[0][0] = xform->AX; m->x[0][1] = xform->AY; m->x[0][2] = xform->AZ;
00047 m->x[1][0] = xform->BX; m->x[1][1] = xform->BY; m->x[1][2] = xform->BZ;
00048 m->x[2][0] = xform->CX; m->x[2][1] = xform->CY; m->x[2][2] = xform->CZ;
00049 }
00050
00051 void Matrix33_MultiplyVec3d(const Matrix33* m, const geVec3d* v, geVec3d* res)
00052 {
00053 assert(m != NULL);
00054 assert(v != NULL);
00055 assert(res != NULL);
00056
00057 res->X = m->x[0][0] * v->X + m->x[0][1] * v->Y + m->x[0][2] * v->Z;
00058 res->Y = m->x[1][0] * v->X + m->x[1][1] * v->Y + m->x[1][2] * v->Z;
00059 res->Z = m->x[2][0] * v->X + m->x[2][1] * v->Y + m->x[2][2] * v->Z;
00060 }
00061
00062 void Matrix33_Multiply(const Matrix33* m1, const Matrix33* m2, Matrix33* res)
00063 {
00064 int i, j, k;
00065
00066 assert(m1 != NULL);
00067 assert(m2 != NULL);
00068 assert(res != NULL);
00069
00070 for (i = 0; i < 3; i++)
00071 {
00072 for (j = 0; j < 3; j++)
00073 {
00074 res->x[i][j] = 0.0f;
00075
00076 for (k = 0; k < 3; k++)
00077 res->x[i][j] += m1->x[i][k] * m2->x[k][j];
00078 }
00079 }
00080 }
00081
00082 void Matrix33_GetTranspose(const Matrix33* m, Matrix33* t)
00083 {
00084 int i, j;
00085
00086 assert(m != NULL);
00087 assert(t != NULL);
00088
00089 for (i = 0; i < 3; i++)
00090 for (j = 0; j < 3; j++)
00091 t->x[j][i] = m->x[i][j];
00092 }
00093
00094 void Matrix33_Copy(const Matrix33* m, Matrix33* c)
00095 {
00096 int i, j;
00097
00098 assert(m != NULL);
00099 assert(c != NULL);
00100
00101 for (i = 0; i < 3; i++)
00102 for (j = 0; j < 3; j++)
00103 c->x[i][j] = m->x[i][j];
00104 }
00105
00106 void Matrix33_SetIdentity(Matrix33* m)
00107 {
00108 int i, j;
00109
00110 assert(m != NULL);
00111
00112 for (i = 0; i < 3; i++)
00113 for (j = 0; j < 3; j++)
00114 {
00115 if (i == j)
00116 m->x[i][j] = 1.f;
00117 else m->x[i][j] = 0.f;
00118 }
00119 }
00120
00121 void Matrix33_Add(const Matrix33* m1, const Matrix33* m2, Matrix33* res)
00122 {
00123 int i, j;
00124
00125 assert(m1 != NULL);
00126 assert(m2 != NULL);
00127 assert(res != NULL);
00128
00129 for (i = 0; i < 3; i++)
00130 for (j = 0; j < 3; j++)
00131 res->x[i][j] = m1->x[i][j] + m2->x[i][j];
00132 }
00133
00134 void Matrix33_Subtract(const Matrix33* m1, const Matrix33* m2, Matrix33* res)
00135 {
00136 int i, j;
00137
00138 assert(m1 != NULL);
00139 assert(m2 != NULL);
00140 assert(res != NULL);
00141
00142 for (i = 0; i < 3; i++)
00143 for (j = 0; j < 3; j++)
00144 res->x[i][j] = m1->x[i][j] - m2->x[i][j];
00145 }
00146
00147 void Matrix33_MultiplyScalar(geFloat s, const Matrix33* m, Matrix33* res)
00148 {
00149 int i, j;
00150
00151 assert(m != NULL);
00152 assert(res != NULL);
00153
00154 for (i = 0; i < 3; i++)
00155 for (j = 0; j < 3; j++)
00156 res->x[i][j] = s * m->x[i][j];
00157 }
00158
00159 void Matrix33_GetInverse(const Matrix33* m, Matrix33* inv)
00160 {
00161 int i, j, k;
00162 Matrix33 copy;
00163
00164 assert(m != NULL);
00165 assert(inv != NULL);
00166
00167 Matrix33_Copy(m, ©);
00168 Matrix33_SetIdentity(inv);
00169
00170 for (i = 0; i < 3; i++)
00171 {
00172 if (copy.x[i][i] != 1.0f)
00173 {
00174 geFloat divby = copy.x[i][i];
00175
00176 assert(fabs(divby) >= 1e-5);
00177
00178 for (j = 0; j < 3; j++)
00179 {
00180 inv->x[i][j] /= divby;
00181 copy.x[i][j] /= divby;
00182 }
00183 }
00184 for (j = 0; j < 3; j++)
00185 {
00186 if (j != i)
00187 {
00188 if (copy.x[j][i] != 0.0f)
00189 {
00190 geFloat mulby = copy.x[j][i];
00191 for (k = 0; k < 3; k++)
00192 {
00193 copy.x[j][k] -= mulby * copy.x[i][k];
00194 inv->x[j][k] -= mulby * inv->x[i][k];
00195 }
00196 }
00197 }
00198 }
00199 }
00200 }
00201