geometry.h

00001 #ifndef GEOMETRY_H
00002 #define GEOMETRY_H
00003 /*
00004  * 3-D geometry (matrix, vector, quaternion) functions for partiview.
00005  *
00006  * Stuart Levy, slevy@ncsa.uiuc.edu
00007  * National Center for Supercomputing Applications,
00008  * University of Illinois 2001.
00009  * This file is part of partiview, released under the
00010  * Illinois Open Source License; see the file LICENSE.partiview for details.
00011  */
00012 
00013 #ifdef __cplusplus
00014 extern "C" {
00015 #endif
00016 
00017 #ifndef CONST
00018 # ifdef __cplusplus
00019 #  define CONST const
00020 # else
00021 #  define CONST
00022 # endif
00023 #endif
00024 
00025 #ifdef unix
00026 # include <alloca.h>
00027 #endif
00028 
00029 typedef struct { float x[3]; } Point;
00030 typedef struct { float q[4]; } Quat;  /* {r,i,j,k} => {0,1,2,3} */
00031 
00032 typedef struct { float m[4*4]; } Matrix;
00033 
00034 #define NewA(type, count)  (type *)alloca((count) * sizeof(type))
00035 
00036 #ifndef COUNT
00037 # define COUNT(array)  (sizeof(array) / sizeof((array)[0]))
00038 #endif
00039 
00040 /*
00041  * Global Constants
00042  */
00043 
00044 extern Matrix Tidentity;
00045 
00046 
00047 /*
00048  * Global variables
00049  */
00050 
00051 void arena_init( int nbytes );
00052 
00053 void vsub( Point *dst, CONST Point *a, CONST Point *b );
00054 void vadd( Point *dst, CONST Point *a, CONST Point *b );
00055 void vcross( Point *dst, CONST Point *a, CONST Point *b );
00056 float vdot( CONST Point *a, CONST Point *b );
00057 void vscale( Point *dst, float s, CONST Point *src );
00058 void vsadd( Point *dst, CONST Point *a, float sb, CONST Point *b );
00059 void vlerp( Point *dst, float frac, CONST Point *vfrom, CONST Point *vto );
00060 void vcomb( Point *dst, float sa, CONST Point *a, float sb, CONST Point *b );
00061 float vunit( Point *dst, CONST Point *src );
00062 void vproj( Point *along, Point *perp, CONST Point *vec, CONST Point *onto );
00063 
00064 float qdot( CONST Quat *q1, CONST Quat *q2 );
00065 void qnorm( Quat *qdst, CONST Quat *qsrc );
00066 void qcomb( Quat *qdst, float sa, CONST Quat *qa, float sb, CONST Quat *qb );
00067 
00068         /* Distance between two 3-D points */
00069 float vdist( CONST Point *p1, CONST Point *p2 );
00070         /* Distance between two quaternions */
00071 float iqdist( CONST Point *q1, CONST Point *q2 );
00072 float qdist( CONST Quat *q1, CONST Quat *q2 );
00073         /* Distance between two matrices */
00074 float tdist( CONST Matrix *t1, CONST Matrix *t2 );
00075 
00076         /* vector magnitude */
00077 float vlength( CONST Point *v );
00078 
00079         /* Transform a 3-D point by a matrix: dst = src*T. */
00080 void vtfmpoint( Point *dst,  CONST Point *src, CONST Matrix *T );
00081 
00082         /* Transform a vector by a (Euclidean) matrix: dst = src*T */
00083 void vtfmvector( Point *dst,  CONST Point *src, CONST Matrix *T );
00084 void vuntfmvector( Point *dst,  CONST Point *src, CONST Matrix *T );
00085 
00086         /* Get translation part of matrix */
00087 void vgettranslation( Point *dst, CONST Matrix *T );
00088         /* Set translation part of matrix, leave 3x3 submatrix alone */
00089 void vsettranslation( Matrix *T, CONST Point *src );
00090 
00091 void vrotxy( Point *dst, CONST Point *src, CONST float cs[2] );
00092 void mcopy( Matrix *dst, CONST Matrix *src );
00093 
00094         /* 4x4 matrix multiply */
00095 void mmmul( Matrix *dst, CONST Matrix *a, CONST Matrix *b );
00096 
00097         /* Matrix inverse for Euclidean similarities: matrices are
00098          * a product of pure translation/rotation/uniform-scaling --
00099          */
00100 void eucinv( Matrix *dst, CONST Matrix *src );
00101 
00102         /* matrix conjugation: see its use in Gview.C */
00103 void mconjugate( Matrix *To2wout, CONST Matrix *To2win, CONST Matrix *Tincrf,
00104                  CONST Matrix *Tf2w, CONST Matrix *Tw2f,
00105                  CONST Point *pcenw, CONST Point *pcenf );
00106 
00107 void grotation( Matrix *Trot, CONST Point *fromaxis, CONST Point *toaxis );
00108 
00109         /* matrix to 3-component quaternion (real-part is sqrt(1-iquat.iquat),
00110          * and real part (in range 0 .. +1) is also returned as func value.
00111          */
00112 float tfm2iquat( Point *iquat, CONST Matrix *src );     /* returns real part */
00113 void tfm2quat( Quat *quat, CONST Matrix *src );
00114 void iquat2tfm( Matrix *dst, CONST Point *iquat );
00115 void quat2tfm( Matrix *dst, CONST Quat *quat );
00116 
00117         /* quaternion interpolation */
00118 void iquat_lerp( Point *dquat, float frac, CONST Point *qfrom, CONST Point *qto );
00119 void quat_lerp( Quat *dquat, float frac, CONST Quat *qfrom, CONST Quat *qto );
00120 
00121         /* axis/angle rotation -> quaternion */
00122 void rot2quat( Quat *quat, float degrees, CONST Point *axis );
00123 void rot2iquat( Point *iquat, float degrees, CONST Point *axis );
00124 
00125         /* construct matrix from axis/angle rotation */
00126 void rot2tfm( Matrix *dst, float degrees, CONST Point *axis );
00127 
00128         /* Construct a matrix that's a pure X/Y/Z rotation/scaling/translation */
00129 void mrotation( Matrix *dst, float degrees, char xyzaxis );
00130 void mscaling( Matrix *dst, float sx, float sy, float sz );
00131 void mtranslation( Matrix *dst, float tx, float ty, float tz );
00132 
00133         /* cam2world <-> az(Y) el(X) roll(Z) + translation */
00134 float tfm2xyzaer( Point *xyz, float aer[3], CONST Matrix *c2w ); /* => scale */
00135 void xyzaer2tfm( Matrix *c2w, CONST Point *xyz, CONST float aer[3] );
00136 
00137         /* perspective frustum */
00138 void mfrustum( Matrix *Tproj, float nl, float nr, float nd, float nu, float cnear, float cfar );
00139 
00140 
00141 #ifdef __cplusplus
00142 }
00143 #endif
00144 
00145 
00146 #endif