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
|
/*
* vp_linalg.c
*
* A simple linear algebra package.
*
* Copyright (c) 1994 The Board of Trustees of The Leland Stanford
* Junior University. All rights reserved.
*
* Permission to use, copy, modify and distribute this software and its
* documentation for any purpose is hereby granted without fee, provided
* that the above copyright notice and this permission notice appear in
* all copies of this software and that you do not sell the software.
* Commercial licensing is available by contacting the author.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*
* Author:
* Phil Lacroute
* Computer Systems Laboratory
* Electrical Engineering Dept.
* Stanford University
*/
/*
* $Date: 1994/12/30 23:52:38 $
* $Revision: 1.27 $
*/
#include "vp_global.h"
static void MatrixMult ANSI_ARGS((double* p, double *a, double *b,
int l, int m, int n));
/*
* vpIdentity3
*
* Initialize a Matrix3 to the identity.
*/
void
vpIdentity3(m)
vpMatrix3 m;
{
m[0][0] = 1.; m[0][1] = 0.; m[0][2] = 0.;
m[1][0] = 0.; m[1][1] = 1.; m[1][2] = 0.;
m[2][0] = 0.; m[2][1] = 0.; m[2][2] = 1.;
}
/*
* vpIdentity4
*
* Initialize a Matrix4 to the identity.
*/
void
vpIdentity4(m)
vpMatrix4 m;
{
m[0][0] = 1.; m[0][1] = 0.; m[0][2] = 0.; m[0][3] = 0.;
m[1][0] = 0.; m[1][1] = 1.; m[1][2] = 0.; m[1][3] = 0.;
m[2][0] = 0.; m[2][1] = 0.; m[2][2] = 1.; m[2][3] = 0.;
m[3][0] = 0.; m[3][1] = 0.; m[3][2] = 0.; m[3][3] = 1.;
}
/*
* vpNormalize3
*
* Normalize a vector (divide it by its magnitude). Return VPERROR_SINGULAR
* if the magnitude is too small.
*/
vpResult
vpNormalize3(v)
vpVector3 v;
{
double magsqr, invmag;
int i;
magsqr = 0.;
for (i = 0; i < 3; i++)
magsqr += v[i]*v[i];
if (fabs(magsqr) < VP_EPS)
return(VPERROR_SINGULAR);
invmag = 1. / sqrt(magsqr);
for (i = 0; i < 3; i++)
v[i] *= invmag;
return(VP_OK);
}
/*
* vpMatrixVectorMult4
*
* Perform the matrix-vector multiplication v2 = m*v1.
*/
void
vpMatrixVectorMult4(v2, m, v1)
vpVector4 v2, v1;
vpMatrix4 m;
{
int i, j;
for (i = 0; i < 4; i++) {
v2[i] = 0;
for (j = 0; j < 4; j++)
v2[i] += m[i][j] * v1[j];
}
}
/*
* vpMatrixMult4
*
* Perform the matrix multiplication m3 = m2 * m1.
*/
void
vpMatrixMult4(m3, m2, m1)
vpMatrix4 m3, m2, m1;
{
MatrixMult((double *)m3, (double *)m2, (double *)m1, 4, 4, 4);
}
/*
* MatrixMult
*
* Perform the matrix multiplication p = a * b.
*/
static void
MatrixMult(p, a, b, l, m, n)
double *p; /* result matrix, size l by n */
double *a; /* first factor, size l by m */
double *b; /* second factor, size m by n */
int l, m, n;
{
int i, j, k;
if (l <= 0 || m <= 0 || n <= 0)
VPBug("MatrixMult called with non-positive matrix size");
for (i = 0; i < l; i++) {
for (j = 0; j < n; j++) {
p[i*n+j] = 0;
for (k = 0; k < m; k++)
p[i*n+j] += a[i*n+k] * b[k*n+j];
}
}
}
/*
* vpCrossProduct
*
* Compute the cross product p = v * w.
*/
void
vpCrossProduct(p, v, w)
vpVector3 p, v, w;
{
p[0] = v[1]*w[2] - v[2]*w[1];
p[1] = v[2]*w[0] - v[0]*w[2];
p[2] = v[0]*w[1] - v[1]*w[0];
}
/*
* vpSolveSystem4
*
* Solve the linear system a*xi = bi where a is a 4-by-4 matrix and bi
* is a column of the 4-by-m matrix b. Each column bi in b is replaced
* by the corresponding solution vector xi. The matrix a is destroyed.
* The method used is Gauss-Jordan elimination with partial pivoting and
* implicit scaling (based on the discussion in Numerical Recipes in C
* by Press, Flannery, Teukolsky and Vetterling).
*
* Return VPERROR_SINGULAR if matrix is singular.
*/
vpResult
vpSolveSystem4(a, b, m)
vpMatrix4 a; /* linear system matrix */
double **b; /* RHS vectors on input, solution vectors on output;
b[i] is a Vector4 */
int m; /* number of vectors in b */
{
vpVector4 row_scale_factor; /* normalization for each row */
int ipivot; /* row containing pivot */
int pivot[4]; /* after the reduction loop, row i has
been pivoted to row pivot[i] */
int i, j, k, l; /* loop indices */
double *aptr; /* pointer into a */
double entry; /* entry in a */
double max_entry; /* maximum entry in row */
double inv_entry; /* inverse of an entry in a */
vpVector4 tmpv; /* temporary vector for undoing row
interchange in solution vectors */
/* initialize */
for (i = 0; i < 4; i++)
pivot[i] = -1;
/* find the largest element in each row and compute normalization
for implicit scaling */
aptr = &a[0][0];
for (i = 0; i < 4; i++) {
max_entry = 0.;
for (j = 0; j < 4; j++) {
if (*aptr < 0) {
if (-*aptr > max_entry)
max_entry = -*aptr;
} else {
if (*aptr > max_entry)
max_entry = *aptr;
}
aptr++;
}
if (fabs(max_entry) < VP_EPS)
return(VPERROR_SINGULAR);
row_scale_factor[i] = 1. / max_entry;
}
/* loop over the columns of a */
for (j = 0; j < 4; j++) {
/* loop over the rows of a and choose a pivot element in the
current column, ignoring rows containing previous pivots */
max_entry = 0.;
for (i = 0; i < 4; i++) {
if (pivot[i] < 0) {
entry = a[i][j] * row_scale_factor[i];
if (entry < 0) {
if (-entry > max_entry) {
max_entry = -entry;
ipivot = i;
}
} else {
if (entry > max_entry) {
max_entry = entry;
ipivot = i;
}
}
}
}
if (fabs(max_entry) < VP_EPS)
return(VPERROR_SINGULAR);
pivot[ipivot] = j;
inv_entry = 1. / a[ipivot][j];
/* scale the pivot row by the pivot element */
for (l = j+1; l < 4; l++)
a[ipivot][l] *= inv_entry;
for (l = 0; l < m; l++)
b[l][ipivot] *= inv_entry;
/* subtract a multiple of the pivot row from the other rows */
for (k = 0; k < 4; k++) {
if (k != ipivot) {
entry = a[k][j];
for (l = j+1; l < 4; l++)
a[k][l] -= a[ipivot][l] * entry;
for (l = 0; l < m; l++)
b[l][k] -= b[l][ipivot] * entry;
}
}
}
/* undo row interchanges in solution vectors */
for (j = 0; j < m; j++) {
for (i = 0; i < 4; i++)
tmpv[pivot[i]] = b[j][i];
for (i = 0; i < 4; i++)
b[j][i] = tmpv[i];
}
return(VP_OK);
}
/*
* VPLoadTranslation
*
* Load a translation matrix.
*/
void
VPLoadTranslation(m, tx, ty, tz)
vpMatrix4 m;
double tx, ty, tz;
{
vpIdentity4(m);
m[0][3] = tx;
m[1][3] = ty;
m[2][3] = tz;
}
/*
* VPLoadRotation
*
* Load a rotation matrix.
*/
void
VPLoadRotation(m, axis, degrees)
vpMatrix4 m;
int axis;
double degrees;
{
vpMatrix4 tmp;
double radians, sintheta, costheta;
radians = degrees * M_PI / 180.;
sintheta = sin(radians);
costheta = cos(radians);
vpIdentity4(m);
switch (axis) {
case VP_X_AXIS:
m[1][1] = costheta;
m[1][2] = sintheta;
m[2][1] = -sintheta;
m[2][2] = costheta;
break;
case VP_Y_AXIS:
m[0][0] = costheta;
m[0][2] = -sintheta;
m[2][0] = sintheta;
m[2][2] = costheta;
break;
case VP_Z_AXIS:
m[0][0] = costheta;
m[0][1] = sintheta;
m[1][0] = -sintheta;
m[1][1] = costheta;
break;
default:
VPBug("bad axis in VPLoadRotation");
}
}
/*
* VPLoadScale
*
* Load a scale matrix.
*/
void
VPLoadScale(m, sx, sy, sz)
vpMatrix4 m;
double sx, sy, sz;
{
vpIdentity4(m);
m[0][0] = sx;
m[1][1] = sy;
m[2][2] = sz;
}
|