vect.c 7.74 KB
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
/*
 * Copyright 1991, 1992, 1993, 1994, 1995, Silicon Graphics, Inc.
 * All Rights Reserved.
 *
 * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
 * the contents of this file may not be disclosed to third parties, copied or
 * duplicated in any form, in whole or in part, without the prior written
 * permission of Silicon Graphics, Inc.
 *
 * RESTRICTED RIGHTS LEGEND:
 * Use, duplication or disclosure by the Government is subject to restrictions
 * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
 * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
 * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
 * rights reserved under the Copyright Laws of the United States.
 */
/*
 * 	vect -
 *		Various functions to support operations on vectors.
 *
 *	David M. Ciemiewicz, Mark Grossman, Henry Moreton, and Paul Haeberli 
 *
 *	Converted to U-64 by Rob Moore.
 *
 */
#include <ultratypes.h>
#include <rmon.h>
#include "vect.h"
#include "em.h"
#include "gu.h"
#include "stress.h"

void
  vcopy(vect *v1, vect *v2)
{
    *v2 = *v1;
}

void
  vprint( vect *v )
{
    PRINTF("x: %f y: %f z: %f\n",v->x,v->y,v->z);
}

void
  dvprint(dvect *v)
{
    PRINTF("x: %f y: %f z: %f\n",v->x,v->y,v->z);
}

void
  vprint4(vect *v)
{
    PRINTF("x: %f y: %f z: %f w: %f\n",v->x,v->y,v->z,v->w);
}

void
  vset(vect *v, float x, float y, float z)
{
    v->x = x;
    v->y = y;
    v->z = z;
}

void
  vzero(vect *v)
{
    v->x = 0.0;
    v->y = 0.0;
    v->z = 0.0;
    v->w = 0.0;
}

void
  vone(vect *v)
{
    v->x = 1.0;
    v->y = 1.0;
    v->z = 1.0;
    v->w = 1.0;
}

void
  vset4(vect *v, float x, float y, float z, float w)
{
    v->x = x;
    v->y = y;
    v->z = z;
    v->w = w;
}

void
  vnormal(void *v)
{
    float len;

    len = vlength(v);
    if(len>0.0)
	vscale(v,1.0/len);
    else
	vzero(v);
}

float 
  vlength(vect *v) 
{
    return sqrtf(v->x*v->x + v->y*v->y + v->z*v->z);

}

void
  vscale(vect *v, float mul)
{
    v->x *= mul;
    v->y *= mul;
    v->z *= mul;
}

void
  vmult(vect *src1, vect *src2, vect *dst)
{
    dst->x = src1->x * src2->x;
    dst->y = src1->y * src2->y;
    dst->z = src1->z * src2->z;
}

void
  vadd(vect *src1, vect *src2, vect *dst)
{
    dst->x = src1->x + src2->x;
    dst->y = src1->y + src2->y;
    dst->z = src1->z + src2->z;
}

void
  vsub(vect *src1, vect *src2, vect *dst)
{
    dst->x = src1->x - src2->x;
    dst->y = src1->y - src2->y;
    dst->z = src1->z - src2->z;
}

void
  vhalf(vect *v1, vect *v2, vect *half)
{
    float len;

    vadd(v2,v1,half);
    len = vlength(half);
    if(len>0.0001)
	vscale(half,1.0/len);
    else
	*half = *v1;
}

float 
  vdot(vect *v1, vect *v2) 
{
    return v1->x*v2->x + v1->y*v2->y + v1->z*v2->z;
}

double 
  dvdot(dvect *v1, dvect *v2) 
{
    return v1->x*v2->x + v1->y*v2->y + v1->z*v2->z;
}

void
  vcross(vect *v1, vect *v2, vect *cross)
{
    vect temp;

    temp.x = (v1->y * v2->z) - (v1->z * v2->y);
    temp.y = (v1->z * v2->x) - (v1->x * v2->z);
    temp.z = (v1->x * v2->y) - (v1->y * v2->x);
    *cross = temp;
}

void
  vplane(vect *normal, vect *point, vect *plane)
{
    plane->x = normal->x;
    plane->y = normal->y;
    plane->z = normal->z;
    plane->w = - vdot(normal, point);
}

void
  dvplane( dvect *normal,  dvect *point,  dvect *plane)
{
    plane->x = normal->x;
    plane->y = normal->y;
    plane->z = normal->z;
    plane->w = - dvdot(normal, point);
}

void
  vdirection(vect *v1, vect *dir)
{
	*dir = *v1;
	vnormal(dir);
}

void
  makeplane(vect *p1, vect *p2, vect *p3, vect *v)
{
    vect a, b;

    vsub(p1,p2,&a);
    vsub(p3,p2,&b);
    vcross(&a,&b,&b);
    vnormal(&b);
    vplane(&b,p1,v);
}

void
  vreflect(vect *in, vect *mirror, vect *out)
{
    vect temp;

    temp = *mirror;
    vscale(&temp,vdot(mirror,in));
    vsub(&temp,in,out);
    vadd(&temp,out,out);
}


void
  vmatrixcopy( float m1[4][4], float m2[4][4] )
{
    int i,j;
    for(i = 0; i < 4; i++)
      for(j = 0; j < 4; j++)
        m2[i][j] = m1[i][j];
}


void
  vmultmatrix(float m1[4][4], float m2[4][4], float prod[4][4])
{
    int row, col;
    float temp[4][4];

    for(row=0 ; row<4 ; row++) 
        for(col=0 ; col<4 ; col++)
            temp[row][col] = m1[row][0] * m2[0][col]
                           + m1[row][1] * m2[1][col]
                           + m1[row][2] * m2[2][col]
                           + m1[row][3] * m2[3][col];
    for(row=0 ; row<4 ; row++) 
        for(col=0 ; col<4 ; col++)
	    prod[row][col] = temp[row][col];
}

void
  vtransform(vect *v, float mat[4][4], vect *vt)
{
    vect t;

    t.x = v->x*mat[0][0] + v->y*mat[1][0] + v->z*mat[2][0] + v->w*mat[3][0];
    t.y = v->x*mat[0][1] + v->y*mat[1][1] + v->z*mat[2][1] + v->w*mat[3][1];
    t.z = v->x*mat[0][2] + v->y*mat[1][2] + v->z*mat[2][2] + v->w*mat[3][2];
    t.w = v->x*mat[0][3] + v->y*mat[1][3] + v->z*mat[2][3] + v->w*mat[3][3];
    *vt = t;
}

void
  vlerp(vect *v0, vect *v1, vect *v, float p)
{
    v->x = flerp(v0->x,v1->x,p);
    v->y = flerp(v0->y,v1->y,p);
    v->z = flerp(v0->z,v1->z,p);
    v->w = flerp(v0->w,v1->w,p);
}

void
  dvlerp(dvect *v0, dvect *v1, dvect *v, float p)
{
    v->x = dlerp(v0->x,v1->x,p);
    v->y = dlerp(v0->y,v1->y,p);
    v->z = dlerp(v0->z,v1->z,p);
    v->w = dlerp(v0->w,v1->w,p);
}

float 
  flerp(float f0, float f1, float p)
{
    return ((f0*(1.0-p))+(f1*p));
}

double 
  dlerp(double f0, double f1, double p)
{
    return ((f0*(1.0-p))+(f1*p));
}

int
  lerp(int i0, int i1, float p)
{
    return (int)((i0*(1.0-p))+(i1*p));
}

void
  vclamp(vect *v)
{
    if(v->x<0.0)
	v->x = 0.0;
    else if(v->x>1.0)
	v->x = 1.0;
    if(v->y<0.0)
	v->y = 0.0;
    else if(v->y>1.0)
	v->y = 1.0;
    if(v->z<0.0)
	v->z = 0.0;
    else if(v->z>1.0)
	v->z = 1.0;
}

int 
  trinormal(vect *p00, vect *p01, vect *p10, vect *n, float tol )
{
    double tx, ty, tz;
    double Xj, Yj, Zj;
    double Xi, Yi, Zi;
    double mag;

    Xj = p10->x;
    Yj = p10->y;
    Zj = p10->z;
    tx = ty = tz = 0.0;

    Xi = Xj; Xj = p00->x;
    Yi = Yj; Yj = p00->y;
    Zi = Zj; Zj = p00->z;
    tx += (Yi - Yj) * (Zi + Zj);
    ty += (Zi - Zj) * (Xi + Xj);
    tz += (Xi - Xj) * (Yi + Yj);
	
    Xi = Xj; Xj = p01->x;
    Yi = Yj; Yj = p01->y;
    Zi = Zj; Zj = p01->z;
    tx += (Yi - Yj) * (Zi + Zj);
    ty += (Zi - Zj) * (Xi + Xj);
    tz += (Xi - Xj) * (Yi + Yj);
	
    Xi = Xj; Xj = p10->x;
    Yi = Yj; Yj = p10->y;
    Zi = Zj; Zj = p10->z;
    tx += (Yi - Yj) * (Zi + Zj);
    ty += (Zi - Zj) * (Xi + Xj);
    tz += (Xi - Xj) * (Yi + Yj);

    mag = sqrtf(tx*tx + ty*ty + tz*tz);
    if( mag < tol) {
	n->x = 0.0;
	n->y = 0.0;
	n->z = 0.0;
	return 0;
    } else {
	n->x = tx/mag;
	n->y = ty/mag;
	n->z = tz/mag;
	return 1;
    }
}

int 
  dtrinormal( dvect *p00, dvect *p01, dvect *p10, dvect *n, double tol )
{
    double tx, ty, tz;
    double Xj, Yj, Zj;
    double Xi, Yi, Zi;
    double mag;

    Xj = p10->x;
    Yj = p10->y;
    Zj = p10->z;
    tx = ty = tz = 0.0;

    Xi = Xj; Xj = p00->x;
    Yi = Yj; Yj = p00->y;
    Zi = Zj; Zj = p00->z;
    tx += (Yi - Yj) * (Zi + Zj);
    ty += (Zi - Zj) * (Xi + Xj);
    tz += (Xi - Xj) * (Yi + Yj);
	
    Xi = Xj; Xj = p01->x;
    Yi = Yj; Yj = p01->y;
    Zi = Zj; Zj = p01->z;
    tx += (Yi - Yj) * (Zi + Zj);
    ty += (Zi - Zj) * (Xi + Xj);
    tz += (Xi - Xj) * (Yi + Yj);
	
    Xi = Xj; Xj = p10->x;
    Yi = Yj; Yj = p10->y;
    Zi = Zj; Zj = p10->z;
    tx += (Yi - Yj) * (Zi + Zj);
    ty += (Zi - Zj) * (Xi + Xj);
    tz += (Xi - Xj) * (Yi + Yj);

    mag = sqrtf(tx*tx + ty*ty + tz*tz);
    if( mag < tol) {
	n->x = 0.0;
	n->y = 0.0;
	n->z = 0.0;
	return 0;
    } else {
	n->x = tx/mag;
	n->y = ty/mag;
	n->z = tz/mag;
	return 1;
    }
}

static float   idmat[4][4] = {
    1.0, 0.0, 0.0, 0.0,
    0.0, 1.0, 0.0, 0.0,
    0.0, 0.0, 1.0, 0.0,
    0.0, 0.0, 0.0, 1.0
};

void
  myidentity( float mat[4][4] )
{
  vmatrixcopy(idmat, mat);
}