odemath.h

00001 /*************************************************************************
00002  *                                                                       *
00003  * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith.       *
00004  * All rights reserved.  Email: russ@q12.org   Web: www.q12.org          *
00005  *                                                                       *
00006  * This library is free software; you can redistribute it and/or         *
00007  * modify it under the terms of EITHER:                                  *
00008  *   (1) The GNU Lesser General Public License as published by the Free  *
00009  *       Software Foundation; either version 2.1 of the License, or (at  *
00010  *       your option) any later version. The text of the GNU Lesser      *
00011  *       General Public License is included with this library in the     *
00012  *       file LICENSE.TXT.                                               *
00013  *   (2) The BSD-style license that is included with this library in     *
00014  *       the file LICENSE-BSD.TXT.                                       *
00015  *                                                                       *
00016  * This library is distributed in the hope that it will be useful,       *
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files    *
00019  * LICENSE.TXT and LICENSE-BSD.TXT for more details.                     *
00020  *                                                                       *
00021  *************************************************************************/
00022 
00023 #ifndef _ODE_ODEMATH_H_
00024 #define _ODE_ODEMATH_H_
00025 
00026 #include <ode/common.h>
00027 
00028 #ifdef __GNUC__
00029 #define PURE_INLINE extern inline
00030 #else
00031 #define PURE_INLINE inline
00032 #endif
00033 
00034 /*
00035  * macro to access elements i,j in an NxM matrix A, independent of the
00036  * matrix storage convention.
00037  */
00038 #define dACCESS33(A,i,j) ((A)[(i)*4+(j)])
00039 
00040 /*
00041  * Macro to test for valid floating point values
00042  */
00043 #define dVALIDVEC3(v) (!(dIsNan(v[0]) || dIsNan(v[1]) || dIsNan(v[2])))
00044 #define dVALIDVEC4(v) (!(dIsNan(v[0]) || dIsNan(v[1]) || dIsNan(v[2]) || dIsNan(v[3])))
00045 #define dVALIDMAT3(m) (!(dIsNan(m[0]) || dIsNan(m[1]) || dIsNan(m[2]) || dIsNan(m[3]) || dIsNan(m[4]) || dIsNan(m[5]) || dIsNan(m[6]) || dIsNan(m[7]) || dIsNan(m[8]) || dIsNan(m[9]) || dIsNan(m[10]) || dIsNan(m[11])))
00046 #define dVALIDMAT4(m) (!(dIsNan(m[0]) || dIsNan(m[1]) || dIsNan(m[2]) || dIsNan(m[3]) || dIsNan(m[4]) || dIsNan(m[5]) || dIsNan(m[6]) || dIsNan(m[7]) || dIsNan(m[8]) || dIsNan(m[9]) || dIsNan(m[10]) || dIsNan(m[11]) || dIsNan(m[12]) || dIsNan(m[13]) || dIsNan(m[14]) || dIsNan(m[15]) ))
00047 
00048 
00049 
00050 /*
00051  * General purpose vector operations with other vectors or constants.
00052  */
00053 
00054 #define dOP(a,op,b,c) \
00055     (a)[0] = ((b)[0]) op ((c)[0]); \
00056     (a)[1] = ((b)[1]) op ((c)[1]); \
00057     (a)[2] = ((b)[2]) op ((c)[2]);
00058 #define dOPC(a,op,b,c) \
00059     (a)[0] = ((b)[0]) op (c); \
00060     (a)[1] = ((b)[1]) op (c); \
00061     (a)[2] = ((b)[2]) op (c);
00062 #define dOPE(a,op,b) \
00063     (a)[0] op ((b)[0]); \
00064     (a)[1] op ((b)[1]); \
00065     (a)[2] op ((b)[2]);
00066 #define dOPEC(a,op,c) \
00067     (a)[0] op (c); \
00068     (a)[1] op (c); \
00069     (a)[2] op (c);
00070 
00071 
00072 /*
00073  * Length, and squared length helpers. dLENGTH returns the length of a dVector3.
00074  * dLENGTHSQUARED return the squared length of a dVector3.
00075  */
00076 
00077 #define dLENGTHSQUARED(a) (((a)[0])*((a)[0]) + ((a)[1])*((a)[1]) + ((a)[2])*((a)[2]))
00078 
00079 #ifdef __cplusplus
00080 
00081 PURE_INLINE dReal dLENGTH (const dReal *a) { return dSqrt(dLENGTHSQUARED(a)); }
00082 
00083 #else
00084 
00085 #define dLENGTH(a) ( dSqrt( ((a)[0])*((a)[0]) + ((a)[1])*((a)[1]) + ((a)[2])*((a)[2]) ) )
00086 
00087 #endif /* __cplusplus */
00088 
00089 
00090 
00091 
00092 
00093 /*
00094  * 3-way dot product. dDOTpq means that elements of `a' and `b' are spaced
00095  * p and q indexes apart respectively. dDOT() means dDOT11.
00096  * in C++ we could use function templates to get all the versions of these
00097  * functions - but on some compilers this will result in sub-optimal code.
00098  */
00099 
00100 #define dDOTpq(a,b,p,q) ((a)[0]*(b)[0] + (a)[p]*(b)[q] + (a)[2*(p)]*(b)[2*(q)])
00101 
00102 #ifdef __cplusplus
00103 
00104 PURE_INLINE dReal dDOT   (const dReal *a, const dReal *b) { return dDOTpq(a,b,1,1); }
00105 PURE_INLINE dReal dDOT13 (const dReal *a, const dReal *b) { return dDOTpq(a,b,1,3); }
00106 PURE_INLINE dReal dDOT31 (const dReal *a, const dReal *b) { return dDOTpq(a,b,3,1); }
00107 PURE_INLINE dReal dDOT33 (const dReal *a, const dReal *b) { return dDOTpq(a,b,3,3); }
00108 PURE_INLINE dReal dDOT14 (const dReal *a, const dReal *b) { return dDOTpq(a,b,1,4); }
00109 PURE_INLINE dReal dDOT41 (const dReal *a, const dReal *b) { return dDOTpq(a,b,4,1); }
00110 PURE_INLINE dReal dDOT44 (const dReal *a, const dReal *b) { return dDOTpq(a,b,4,4); }
00111 
00112 #else
00113 
00114 #define dDOT(a,b)   dDOTpq(a,b,1,1)
00115 #define dDOT13(a,b) dDOTpq(a,b,1,3)
00116 #define dDOT31(a,b) dDOTpq(a,b,3,1)
00117 #define dDOT33(a,b) dDOTpq(a,b,3,3)
00118 #define dDOT14(a,b) dDOTpq(a,b,1,4)
00119 #define dDOT41(a,b) dDOTpq(a,b,4,1)
00120 #define dDOT44(a,b) dDOTpq(a,b,4,4)
00121 
00122 #endif /* __cplusplus */
00123 
00124 
00125 /*
00126  * cross product, set a = b x c. dCROSSpqr means that elements of `a', `b'
00127  * and `c' are spaced p, q and r indexes apart respectively.
00128  * dCROSS() means dCROSS111. `op' is normally `=', but you can set it to
00129  * +=, -= etc to get other effects.
00130  */
00131 
00132 #define dCROSS(a,op,b,c) \
00133 do { \
00134   (a)[0] op ((b)[1]*(c)[2] - (b)[2]*(c)[1]); \
00135   (a)[1] op ((b)[2]*(c)[0] - (b)[0]*(c)[2]); \
00136   (a)[2] op ((b)[0]*(c)[1] - (b)[1]*(c)[0]); \
00137 } while(0)
00138 #define dCROSSpqr(a,op,b,c,p,q,r) \
00139 do { \
00140   (a)[  0] op ((b)[  q]*(c)[2*r] - (b)[2*q]*(c)[  r]); \
00141   (a)[  p] op ((b)[2*q]*(c)[  0] - (b)[  0]*(c)[2*r]); \
00142   (a)[2*p] op ((b)[  0]*(c)[  r] - (b)[  q]*(c)[  0]); \
00143 } while(0)
00144 #define dCROSS114(a,op,b,c) dCROSSpqr(a,op,b,c,1,1,4)
00145 #define dCROSS141(a,op,b,c) dCROSSpqr(a,op,b,c,1,4,1)
00146 #define dCROSS144(a,op,b,c) dCROSSpqr(a,op,b,c,1,4,4)
00147 #define dCROSS411(a,op,b,c) dCROSSpqr(a,op,b,c,4,1,1)
00148 #define dCROSS414(a,op,b,c) dCROSSpqr(a,op,b,c,4,1,4)
00149 #define dCROSS441(a,op,b,c) dCROSSpqr(a,op,b,c,4,4,1)
00150 #define dCROSS444(a,op,b,c) dCROSSpqr(a,op,b,c,4,4,4)
00151 
00152 
00153 /*
00154  * set a 3x3 submatrix of A to a matrix such that submatrix(A)*b = a x b.
00155  * A is stored by rows, and has `skip' elements per row. the matrix is
00156  * assumed to be already zero, so this does not write zero elements!
00157  * if (plus,minus) is (+,-) then a positive version will be written.
00158  * if (plus,minus) is (-,+) then a negative version will be written.
00159  */
00160 
00161 #define dCROSSMAT(A,a,skip,plus,minus) \
00162 do { \
00163   (A)[1] = minus (a)[2]; \
00164   (A)[2] = plus (a)[1]; \
00165   (A)[(skip)+0] = plus (a)[2]; \
00166   (A)[(skip)+2] = minus (a)[0]; \
00167   (A)[2*(skip)+0] = minus (a)[1]; \
00168   (A)[2*(skip)+1] = plus (a)[0]; \
00169 } while(0)
00170 
00171 
00172 /*
00173  * compute the distance between two 3D-vectors
00174  */
00175 
00176 #ifdef __cplusplus
00177 PURE_INLINE dReal dDISTANCE (const dVector3 a, const dVector3 b)
00178    { return dSqrt( (a[0]-b[0])*(a[0]-b[0]) + (a[1]-b[1])*(a[1]-b[1]) + (a[2]-b[2])*(a[2]-b[2]) ); }
00179 #else
00180 #define dDISTANCE(a,b) \
00181    (dSqrt( ((a)[0]-(b)[0])*((a)[0]-(b)[0]) + ((a)[1]-(b)[1])*((a)[1]-(b)[1]) + ((a)[2]-(b)[2])*((a)[2]-(b)[2]) ))
00182 #endif
00183 
00184 
00185 /*
00186  * special case matrix multipication, with operator selection
00187  */
00188 
00189 #define dMULTIPLYOP0_331(A,op,B,C) \
00190 do { \
00191   (A)[0] op dDOT((B),(C)); \
00192   (A)[1] op dDOT((B+4),(C)); \
00193   (A)[2] op dDOT((B+8),(C)); \
00194 } while(0)
00195 #define dMULTIPLYOP1_331(A,op,B,C) \
00196 do { \
00197   (A)[0] op dDOT41((B),(C)); \
00198   (A)[1] op dDOT41((B+1),(C)); \
00199   (A)[2] op dDOT41((B+2),(C)); \
00200 } while(0)
00201 #define dMULTIPLYOP0_133(A,op,B,C) \
00202 do { \
00203   (A)[0] op dDOT14((B),(C)); \
00204   (A)[1] op dDOT14((B),(C+1)); \
00205   (A)[2] op dDOT14((B),(C+2)); \
00206 } while(0)
00207 #define dMULTIPLYOP0_333(A,op,B,C) \
00208 do { \
00209   (A)[0] op dDOT14((B),(C)); \
00210   (A)[1] op dDOT14((B),(C+1)); \
00211   (A)[2] op dDOT14((B),(C+2)); \
00212   (A)[4] op dDOT14((B+4),(C)); \
00213   (A)[5] op dDOT14((B+4),(C+1)); \
00214   (A)[6] op dDOT14((B+4),(C+2)); \
00215   (A)[8] op dDOT14((B+8),(C)); \
00216   (A)[9] op dDOT14((B+8),(C+1)); \
00217   (A)[10] op dDOT14((B+8),(C+2)); \
00218 } while(0)
00219 #define dMULTIPLYOP1_333(A,op,B,C) \
00220 do { \
00221   (A)[0] op dDOT44((B),(C)); \
00222   (A)[1] op dDOT44((B),(C+1)); \
00223   (A)[2] op dDOT44((B),(C+2)); \
00224   (A)[4] op dDOT44((B+1),(C)); \
00225   (A)[5] op dDOT44((B+1),(C+1)); \
00226   (A)[6] op dDOT44((B+1),(C+2)); \
00227   (A)[8] op dDOT44((B+2),(C)); \
00228   (A)[9] op dDOT44((B+2),(C+1)); \
00229   (A)[10] op dDOT44((B+2),(C+2)); \
00230 } while(0)
00231 #define dMULTIPLYOP2_333(A,op,B,C) \
00232 do { \
00233   (A)[0] op dDOT((B),(C)); \
00234   (A)[1] op dDOT((B),(C+4)); \
00235   (A)[2] op dDOT((B),(C+8)); \
00236   (A)[4] op dDOT((B+4),(C)); \
00237   (A)[5] op dDOT((B+4),(C+4)); \
00238   (A)[6] op dDOT((B+4),(C+8)); \
00239   (A)[8] op dDOT((B+8),(C)); \
00240   (A)[9] op dDOT((B+8),(C+4)); \
00241   (A)[10] op dDOT((B+8),(C+8)); \
00242 } while(0)
00243 
00244 #ifdef __cplusplus
00245 
00246 #define DECL template <class TA, class TB, class TC> PURE_INLINE void
00247 
00248 DECL dMULTIPLY0_331(TA *A, const TB *B, const TC *C) { dMULTIPLYOP0_331(A,=,B,C); }
00249 DECL dMULTIPLY1_331(TA *A, const TB *B, const TC *C) { dMULTIPLYOP1_331(A,=,B,C); }
00250 DECL dMULTIPLY0_133(TA *A, const TB *B, const TC *C) { dMULTIPLYOP0_133(A,=,B,C); }
00251 DECL dMULTIPLY0_333(TA *A, const TB *B, const TC *C) { dMULTIPLYOP0_333(A,=,B,C); }
00252 DECL dMULTIPLY1_333(TA *A, const TB *B, const TC *C) { dMULTIPLYOP1_333(A,=,B,C); }
00253 DECL dMULTIPLY2_333(TA *A, const TB *B, const TC *C) { dMULTIPLYOP2_333(A,=,B,C); }
00254 
00255 DECL dMULTIPLYADD0_331(TA *A, const TB *B, const TC *C) { dMULTIPLYOP0_331(A,+=,B,C); }
00256 DECL dMULTIPLYADD1_331(TA *A, const TB *B, const TC *C) { dMULTIPLYOP1_331(A,+=,B,C); }
00257 DECL dMULTIPLYADD0_133(TA *A, const TB *B, const TC *C) { dMULTIPLYOP0_133(A,+=,B,C); }
00258 DECL dMULTIPLYADD0_333(TA *A, const TB *B, const TC *C) { dMULTIPLYOP0_333(A,+=,B,C); }
00259 DECL dMULTIPLYADD1_333(TA *A, const TB *B, const TC *C) { dMULTIPLYOP1_333(A,+=,B,C); }
00260 DECL dMULTIPLYADD2_333(TA *A, const TB *B, const TC *C) { dMULTIPLYOP2_333(A,+=,B,C); }
00261 
00262 #undef DECL
00263 
00264 #else
00265 
00266 #define dMULTIPLY0_331(A,B,C) dMULTIPLYOP0_331(A,=,B,C)
00267 #define dMULTIPLY1_331(A,B,C) dMULTIPLYOP1_331(A,=,B,C)
00268 #define dMULTIPLY0_133(A,B,C) dMULTIPLYOP0_133(A,=,B,C)
00269 #define dMULTIPLY0_333(A,B,C) dMULTIPLYOP0_333(A,=,B,C)
00270 #define dMULTIPLY1_333(A,B,C) dMULTIPLYOP1_333(A,=,B,C)
00271 #define dMULTIPLY2_333(A,B,C) dMULTIPLYOP2_333(A,=,B,C)
00272 
00273 #define dMULTIPLYADD0_331(A,B,C) dMULTIPLYOP0_331(A,+=,B,C)
00274 #define dMULTIPLYADD1_331(A,B,C) dMULTIPLYOP1_331(A,+=,B,C)
00275 #define dMULTIPLYADD0_133(A,B,C) dMULTIPLYOP0_133(A,+=,B,C)
00276 #define dMULTIPLYADD0_333(A,B,C) dMULTIPLYOP0_333(A,+=,B,C)
00277 #define dMULTIPLYADD1_333(A,B,C) dMULTIPLYOP1_333(A,+=,B,C)
00278 #define dMULTIPLYADD2_333(A,B,C) dMULTIPLYOP2_333(A,+=,B,C)
00279 
00280 #endif
00281 
00282 
00283 #ifdef __cplusplus
00284 extern "C" {
00285 #endif
00286 
00287 /*
00288  * normalize 3x1 and 4x1 vectors (i.e. scale them to unit length)
00289  */
00290 ODE_API int  dSafeNormalize3 (dVector3 a);
00291 ODE_API int  dSafeNormalize4 (dVector4 a);
00292 
00293 // For some reason demo_chain1.c does not understand "inline" keyword.
00294 static __inline void _dNormalize3(dVector3 a)
00295 {
00296    int bNormalizationResult = dSafeNormalize3(a);
00297    dIASSERT(bNormalizationResult);
00298    dVARIABLEUSED(bNormalizationResult);
00299 }
00300 
00301 static __inline void _dNormalize4(dVector4 a)
00302 {
00303    int bNormalizationResult = dSafeNormalize4(a);
00304    dIASSERT(bNormalizationResult);
00305    dVARIABLEUSED(bNormalizationResult);
00306 }
00307 
00308 // For DLL export
00309 ODE_API void dNormalize3 (dVector3 a); // Potentially asserts on zero vec
00310 ODE_API void dNormalize4 (dVector4 a); // Potentially asserts on zero vec
00311 
00312 // For internal use
00313 #define dNormalize3(a) _dNormalize3(a)
00314 #define dNormalize4(a) _dNormalize4(a)
00315 
00316 /*
00317  * given a unit length "normal" vector n, generate vectors p and q vectors
00318  * that are an orthonormal basis for the plane space perpendicular to n.
00319  * i.e. this makes p,q such that n,p,q are all perpendicular to each other.
00320  * q will equal n x p. if n is not unit length then p will be unit length but
00321  * q wont be.
00322  */
00323 
00324 ODE_API void dPlaneSpace (const dVector3 n, dVector3 p, dVector3 q);
00325 
00326 #ifdef __cplusplus
00327 }
00328 #endif
00329 
00330 #endif

Generated on Fri Oct 12 08:36:51 2007 for Open Dynamics Engine by  doxygen 1.5.3