Main Page | Modules | Data Structures | File List | Data Fields

common.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_COMMON_H_
00024 #define _ODE_COMMON_H_
00025 #include <ode/config.h>
00026 #include <ode/error.h>
00027 #include <math.h>
00028 
00029 #ifdef __cplusplus
00030 extern "C" {
00031 #endif
00032 
00033 
00034 /* configuration stuff */
00035 
00036 /* the efficient alignment. most platforms align data structures to some
00037  * number of bytes, but this is not always the most efficient alignment.
00038  * for example, many x86 compilers align to 4 bytes, but on a pentium it
00039  * is important to align doubles to 8 byte boundaries (for speed), and
00040  * the 4 floats in a SIMD register to 16 byte boundaries. many other
00041  * platforms have similar behavior. setting a larger alignment can waste
00042  * a (very) small amount of memory. NOTE: this number must be a power of
00043  * two. this is set to 16 by default.
00044  */
00045 #define EFFICIENT_ALIGNMENT 16
00046 
00047 
00048 /* constants */
00049 
00050 /* pi and 1/sqrt(2) are defined here if necessary because they don't get
00051  * defined in <math.h> on some platforms (like MS-Windows)
00052  */
00053 
00054 #ifndef M_PI
00055 #define M_PI REAL(3.1415926535897932384626433832795029)
00056 #endif
00057 #ifndef M_SQRT1_2
00058 #define M_SQRT1_2 REAL(0.7071067811865475244008443621048490)
00059 #endif
00060 
00061 
00062 /* debugging:
00063  *   IASSERT  is an internal assertion, i.e. a consistency check. if it fails
00064  *            we want to know where.
00065  *   UASSERT  is a user assertion, i.e. if it fails a nice error message
00066  *            should be printed for the user.
00067  *   AASSERT  is an arguments assertion, i.e. if it fails "bad argument(s)"
00068  *            is printed.
00069  *   DEBUGMSG just prints out a message
00070  */
00071 
00072 #ifndef dNODEBUG
00073 #ifdef __GNUC__
00074 #define dIASSERT(a) if (!(a)) dDebug (d_ERR_IASSERT, \
00075   "assertion \"" #a "\" failed in %s() [%s]",__FUNCTION__,__FILE__);
00076 #define dUASSERT(a,msg) if (!(a)) dDebug (d_ERR_UASSERT, \
00077   msg " in %s()", __FUNCTION__);
00078 #define dDEBUGMSG(msg) dMessage (d_ERR_UASSERT,          \
00079 msg " in %s() File %s Line %d", __FUNCTION__, __FILE__,__LINE__);
00080 #else
00081 #define dIASSERT(a) if (!(a)) dDebug (d_ERR_IASSERT, \
00082   "assertion \"" #a "\" failed in %s:%d",__FILE__,__LINE__);
00083 #define dUASSERT(a,msg) if (!(a)) dDebug (d_ERR_UASSERT, \
00084   msg " (%s:%d)", __FILE__,__LINE__);
00085 #define dDEBUGMSG(msg) dMessage (d_ERR_UASSERT, \
00086   msg " (%s:%d)", __FILE__,__LINE__);
00087 #endif
00088 #else
00089 #define dIASSERT(a) ;
00090 #define dUASSERT(a,msg) ;
00091 #define dDEBUGMSG(msg) ;
00092 #endif
00093 #define dAASSERT(a) dUASSERT(a,"Bad argument(s)")
00094 
00095 // Macro used to suppress unused variable warning
00096 #define dVARIABLEUSED(a) ((void)a)
00097 
00098 /* floating point data type, vector, matrix and quaternion types */
00099 
00100 #if defined(dSINGLE)
00101 typedef float dReal;
00102 #ifdef dDOUBLE
00103 #error You can only #define dSINGLE or dDOUBLE, not both.
00104 #endif // dDOUBLE
00105 #elif defined(dDOUBLE)
00106 typedef double dReal;
00107 #else
00108 #error You must #define dSINGLE or dDOUBLE
00109 #endif
00110 
00111 // Detect if we've got both trimesh engines enabled.
00112 #if dTRIMESH_ENABLED
00113 #if dTRIMESH_OPCODE && dTRIMESH_GIMPACT
00114 #error You can only #define dTRIMESH_OPCODE or dTRIMESH_GIMPACT, not both.
00115 #endif
00116 #endif // dTRIMESH_ENABLED
00117 
00118 // Define a type for indices, either 16 or 32 bit, based on build option
00119 // TODO: Currently GIMPACT only supports 32 bit indices.
00120 #if dTRIMESH_16BIT_INDICES
00121 #if dTRIMESH_GIMPACT
00122 typedef uint32 dTriIndex;
00123 #else // dTRIMESH_GIMPACT
00124 typedef uint16 dTriIndex;
00125 #endif // dTRIMESH_GIMPACT
00126 #else // dTRIMESH_16BIT_INDICES
00127 typedef uint32 dTriIndex;
00128 #endif // dTRIMESH_16BIT_INDICES
00129 
00130 /* round an integer up to a multiple of 4, except that 0 and 1 are unmodified
00131  * (used to compute matrix leading dimensions)
00132  */
00133 #define dPAD(a) (((a) > 1) ? ((((a)-1)|3)+1) : (a))
00134 
00135 /* these types are mainly just used in headers */
00136 typedef dReal dVector3[4];
00137 typedef dReal dVector4[4];
00138 typedef dReal dMatrix3[4*3];
00139 typedef dReal dMatrix4[4*4];
00140 typedef dReal dMatrix6[8*6];
00141 typedef dReal dQuaternion[4];
00142 
00143 
00144 /* precision dependent scalar math functions */
00145 
00146 #if defined(dSINGLE)
00147 
00148 #define REAL(x) (x ## f)               /* form a constant */
00149 #define dRecip(x) ((1.0f/(x)))            /* reciprocal */
00150 #define dSqrt(x) (sqrtf(x))         /* square root */
00151 #define dRecipSqrt(x) ((1.0f/sqrtf(x)))      /* reciprocal square root */
00152 #define dSin(x) (sinf(x))           /* sine */
00153 #define dCos(x) (cosf(x))           /* cosine */
00154 #define dFabs(x) (fabsf(x))         /* absolute value */
00155 #define dAtan2(y,x) (atan2f(y,x))      /* arc tangent with 2 args */
00156 #define dFMod(a,b) (fmodf(a,b))     /* modulo */
00157 #define dFloor(x) floorf(x)         /* floor */
00158 
00159 #ifdef HAVE___ISNANF
00160 #define dIsNan(x) (__isnanf(x))
00161 #elif defined(HAVE__ISNANF)
00162 #define dIsNan(x) (_isnanf(x))
00163 #elif defined(HAVE_ISNANF)
00164 #define dIsNan(x) (isnanf(x))
00165 #else
00166   /*
00167      fall back to _isnan which is the VC way,
00168      this may seem redundant since we already checked
00169      for _isnan before, but if isnan is detected by
00170      configure but is not found during compilation
00171      we should always make sure we check for __isnanf,
00172      _isnanf and isnanf in that order before falling
00173      back to a default
00174   */
00175 #define dIsNan(x) (_isnan(x))
00176 #endif
00177 
00178 #define dCopySign(a,b) ((dReal)copysignf(a,b))
00179 
00180 #elif defined(dDOUBLE)
00181 
00182 #define REAL(x) (x)
00183 #define dRecip(x) (1.0/(x))
00184 #define dSqrt(x) sqrt(x)
00185 #define dRecipSqrt(x) (1.0/sqrt(x))
00186 #define dSin(x) sin(x)
00187 #define dCos(x) cos(x)
00188 #define dFabs(x) fabs(x)
00189 #define dAtan2(y,x) atan2((y),(x))
00190 #define dFMod(a,b) (fmod((a),(b)))
00191 #define dFloor(x) floor(x)
00192 
00193 #ifdef HAVE___ISNAN
00194 #define dIsNan(x) (__isnan(x))
00195 #elif defined(HAVE__ISNAN)
00196 #define dIsNan(x) (_isnan(x))
00197 #elif defined(HAVE_ISNAN)
00198 #define dIsNan(x) (isnan(x))
00199 #else
00200 #define dIsNan(x) (_isnan(x))
00201 #endif
00202 
00203 #define dCopySign(a,b) (copysign((a),(b)))
00204 
00205 #else
00206 #error You must #define dSINGLE or dDOUBLE
00207 #endif
00208 
00209 
00210 /* utility */
00211 
00212 
00213 /* round something up to be a multiple of the EFFICIENT_ALIGNMENT */
00214 
00215 #define dEFFICIENT_SIZE(x) ((((x)-1)|(EFFICIENT_ALIGNMENT-1))+1)
00216 
00217 
00218 /* alloca aligned to the EFFICIENT_ALIGNMENT. note that this can waste
00219  * up to 15 bytes per allocation, depending on what alloca() returns.
00220  */
00221 
00222 #define dALLOCA16(n) \
00223   ((char*)dEFFICIENT_SIZE(((size_t)(alloca((n)+(EFFICIENT_ALIGNMENT-1))))))
00224 
00225 
00226 // Use the error-checking memory allocation system.  Because this system uses heap
00227 //  (malloc) instead of stack (alloca), it is slower.  However, it allows you to
00228 //  simulate larger scenes, as well as handle out-of-memory errors in a somewhat
00229 //  graceful manner
00230 
00231 // #define dUSE_MALLOC_FOR_ALLOCA
00232 
00233 #ifdef dUSE_MALLOC_FOR_ALLOCA
00234 enum {
00235   d_MEMORY_OK = 0,      /* no memory errors */
00236   d_MEMORY_OUT_OF_MEMORY   /* malloc failed due to out of memory error */
00237 };
00238 
00239 #endif
00240 
00241 
00242 
00243 /* internal object types (all prefixed with `dx') */
00244 
00245 struct dxWorld;      /* dynamics world */
00246 struct dxSpace;      /* collision space */
00247 struct dxBody;    /* rigid body (dynamics object) */
00248 struct dxGeom;    /* geometry (collision object) */
00249 struct dxJoint;
00250 struct dxJointNode;
00251 struct dxJointGroup;
00252 
00253 typedef struct dxWorld *dWorldID;
00254 typedef struct dxSpace *dSpaceID;
00255 typedef struct dxBody *dBodyID;
00256 typedef struct dxGeom *dGeomID;
00257 typedef struct dxJoint *dJointID;
00258 typedef struct dxJointGroup *dJointGroupID;
00259 
00260 
00261 /* error numbers */
00262 
00263 enum {
00264   d_ERR_UNKNOWN = 0,    /* unknown error */
00265   d_ERR_IASSERT,     /* internal assertion failed */
00266   d_ERR_UASSERT,     /* user assertion failed */
00267   d_ERR_LCP       /* user assertion failed */
00268 };
00269 
00270 
00271 /* joint type numbers */
00272 
00273 enum {
00274   dJointTypeNone = 0,      /* or "unknown" */
00275   dJointTypeBall,
00276   dJointTypeHinge,
00277   dJointTypeSlider,
00278   dJointTypeContact,
00279   dJointTypeUniversal,
00280   dJointTypeHinge2,
00281   dJointTypeFixed,
00282   dJointTypeNull,
00283   dJointTypeAMotor,
00284   dJointTypeLMotor,
00285   dJointTypePlane2D,
00286   dJointTypePR,
00287   dJointTypePiston
00288 };
00289 
00290 
00291 /* an alternative way of setting joint parameters, using joint parameter
00292  * structures and member constants. we don't actually do this yet.
00293  */
00294 
00295 /*
00296 typedef struct dLimot {
00297   int mode;
00298   dReal lostop, histop;
00299   dReal vel, fmax;
00300   dReal fudge_factor;
00301   dReal bounce, soft;
00302   dReal suspension_erp, suspension_cfm;
00303 } dLimot;
00304 
00305 enum {
00306   dLimotLoStop    = 0x0001,
00307   dLimotHiStop    = 0x0002,
00308   dLimotVel    = 0x0004,
00309   dLimotFMax      = 0x0008,
00310   dLimotFudgeFactor  = 0x0010,
00311   dLimotBounce    = 0x0020,
00312   dLimotSoft      = 0x0040
00313 };
00314 */
00315 
00316 
00317 /* standard joint parameter names. why are these here? - because we don't want
00318  * to include all the joint function definitions in joint.cpp. hmmmm.
00319  * MSVC complains if we call D_ALL_PARAM_NAMES_X with a blank second argument,
00320  * which is why we have the D_ALL_PARAM_NAMES macro as well. please copy and
00321  * paste between these two.
00322  */
00323 
00324 #define D_ALL_PARAM_NAMES(start) \
00325   /* parameters for limits and motors */ \
00326   dParamLoStop = start, \
00327   dParamHiStop, \
00328   dParamVel, \
00329   dParamFMax, \
00330   dParamFudgeFactor, \
00331   dParamBounce, \
00332   dParamCFM, \
00333   dParamStopERP, \
00334   dParamStopCFM, \
00335   /* parameters for suspension */ \
00336   dParamSuspensionERP, \
00337   dParamSuspensionCFM, \
00338   dParamERP, \
00339 
00348 #define D_ALL_PARAM_NAMES_X(start,x) \
00349   dParamGroup ## x = start, \
00350   /* parameters for limits and motors */ \
00351   dParamLoStop ## x = start, \
00352   dParamHiStop ## x, \
00353   dParamVel ## x, \
00354   dParamFMax ## x, \
00355   dParamFudgeFactor ## x, \
00356   dParamBounce ## x, \
00357   dParamCFM ## x, \
00358   dParamStopERP ## x, \
00359   dParamStopCFM ## x, \
00360   /* parameters for suspension */ \
00361   dParamSuspensionERP ## x, \
00362   dParamSuspensionCFM ## x, \
00363   dParamERP ## x,
00364 
00365 enum {
00366   D_ALL_PARAM_NAMES(0)
00367   dParamsInGroup,     
00368   D_ALL_PARAM_NAMES_X(0x000,1)
00369   D_ALL_PARAM_NAMES_X(0x100,2)
00370   D_ALL_PARAM_NAMES_X(0x200,3)
00371 
00372   /* add a multiple of this constant to the basic parameter numbers to get
00373    * the parameters for the second, third etc axes.
00374    */
00375   dParamGroup=0x100
00376 };
00377 
00378 
00379 /* angular motor mode numbers */
00380 
00381 enum{
00382   dAMotorUser = 0,
00383   dAMotorEuler = 1
00384 };
00385 
00386 
00387 /* joint force feedback information */
00388 
00389 typedef struct dJointFeedback {
00390   dVector3 f1;    /* force applied to body 1 */
00391   dVector3 t1;    /* torque applied to body 1 */
00392   dVector3 f2;    /* force applied to body 2 */
00393   dVector3 t2;    /* torque applied to body 2 */
00394 } dJointFeedback;
00395 
00396 
00397 /* private functions that must be implemented by the collision library:
00398  * (1) indicate that a geom has moved, (2) get the next geom in a body list.
00399  * these functions are called whenever the position of geoms connected to a
00400  * body have changed, e.g. with dBodySetPosition(), dBodySetRotation(), or
00401  * when the ODE step function updates the body state.
00402  */
00403 
00404 void dGeomMoved (dGeomID);
00405 dGeomID dGeomGetBodyNext (dGeomID);
00406 
00407 
00408 #ifdef __cplusplus
00409 }
00410 #endif
00411 
00412 #endif

Generated on Sun Dec 2 22:00:27 2007 for Open Dynamics Engine by  doxygen 1.3.9.1