Back to index

radiance  4R0+20100331
fvect.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: fvect.c,v 1.4 2003/02/28 20:11:29 greg Exp $";
00003 #endif
00004 /*
00005  * Routines for 3-d vectors
00006  */
00007 
00008 #include <stdio.h>
00009 #include <stdlib.h>
00010 #include <math.h>
00011 #include "parser.h"
00012 
00013 
00014 double
00015 normalize(v)                /* normalize a vector, return old magnitude */
00016 register FVECT  v;
00017 {
00018        static double  len;
00019        
00020        len = DOT(v, v);
00021        
00022        if (len <= 0.0)
00023               return(0.0);
00024        
00025        if (len <= 1.0+FTINY && len >= 1.0-FTINY)
00026               len = 0.5 + 0.5*len; /* first order approximation */
00027        else
00028               len = sqrt(len);
00029 
00030        v[0] /= len;
00031        v[1] /= len;
00032        v[2] /= len;
00033 
00034        return(len);
00035 }
00036 
00037 
00038 void
00039 fcross(vres, v1, v2)        /* vres = v1 X v2 */
00040 register FVECT  vres, v1, v2;
00041 {
00042        vres[0] = v1[1]*v2[2] - v1[2]*v2[1];
00043        vres[1] = v1[2]*v2[0] - v1[0]*v2[2];
00044        vres[2] = v1[0]*v2[1] - v1[1]*v2[0];
00045 }