common.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*************************************************************************
  2. * *
  3. * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith. *
  4. * All rights reserved. Email: [email protected] Web: www.q12.org *
  5. * *
  6. * This library is free software; you can redistribute it and/or *
  7. * modify it under the terms of EITHER: *
  8. * (1) The GNU Lesser General Public License as published by the Free *
  9. * Software Foundation; either version 2.1 of the License, or (at *
  10. * your option) any later version. The text of the GNU Lesser *
  11. * General Public License is included with this library in the *
  12. * file LICENSE.TXT. *
  13. * (2) The BSD-style license that is included with this library in *
  14. * the file LICENSE-BSD.TXT. *
  15. * *
  16. * This library is distributed in the hope that it will be useful, *
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files *
  19. * LICENSE.TXT and LICENSE-BSD.TXT for more details. *
  20. * *
  21. *************************************************************************/
  22. #ifndef _ODE_COMMON_H_
  23. #define _ODE_COMMON_H_
  24. #include <ode/odeconfig.h>
  25. #include <ode/error.h>
  26. #include <math.h>
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. #define PURE_INLINE static __inline
  31. /* configuration stuff */
  32. /* constants */
  33. /* pi and 1/sqrt(2) are defined here if necessary because they don't get
  34. * defined in <math.h> on some platforms (like MS-Windows)
  35. */
  36. #ifndef M_PI
  37. #define M_PI REAL(3.1415926535897932384626433832795029)
  38. #endif
  39. #ifndef M_SQRT1_2
  40. #define M_SQRT1_2 REAL(0.7071067811865475244008443621048490)
  41. #endif
  42. /* debugging:
  43. * IASSERT is an internal assertion, i.e. a consistency check. if it fails
  44. * we want to know where.
  45. * UASSERT is a user assertion, i.e. if it fails a nice error message
  46. * should be printed for the user.
  47. * AASSERT is an arguments assertion, i.e. if it fails "bad argument(s)"
  48. * is printed.
  49. * DEBUGMSG just prints out a message
  50. */
  51. # if defined(__STDC__) && __STDC_VERSION__ >= 199901L
  52. # define __FUNCTION__ __func__
  53. # endif
  54. #ifndef dNODEBUG
  55. # ifdef __GNUC__
  56. # define dIASSERT(a) { if (!(a)) { dDebug (d_ERR_IASSERT, \
  57. "assertion \"" #a "\" failed in %s() [%s:%u]",__FUNCTION__,__FILE__,__LINE__); } }
  58. # define dUASSERT(a,msg) { if (!(a)) { dDebug (d_ERR_UASSERT, \
  59. msg " in %s()", __FUNCTION__); } }
  60. # define dDEBUGMSG(msg) { dMessage (d_ERR_UASSERT, \
  61. msg " in %s() [%s:%u]", __FUNCTION__,__FILE__,__LINE__); }
  62. # else // not __GNUC__
  63. # define dIASSERT(a) { if (!(a)) { dDebug (d_ERR_IASSERT, \
  64. "assertion \"" #a "\" failed in %s:%u",__FILE__,__LINE__); } }
  65. # define dUASSERT(a,msg) { if (!(a)) { dDebug (d_ERR_UASSERT, \
  66. msg " (%s:%u)", __FILE__,__LINE__); } }
  67. # define dDEBUGMSG(msg) { dMessage (d_ERR_UASSERT, \
  68. msg " (%s:%u)", __FILE__,__LINE__); }
  69. # endif
  70. # define dIVERIFY(a) dIASSERT(a)
  71. #else
  72. # define dIASSERT(a) ((void)0)
  73. # define dUASSERT(a,msg) ((void)0)
  74. # define dDEBUGMSG(msg) ((void)0)
  75. # define dIVERIFY(a) ((void)(a))
  76. #endif
  77. # ifdef __GNUC__
  78. # define dICHECK(a) { if (!(a)) { dDebug (d_ERR_IASSERT, \
  79. "assertion \"" #a "\" failed in %s() [%s:%u]",__FUNCTION__,__FILE__,__LINE__); *(int *)0 = 0; } }
  80. # else // not __GNUC__
  81. # define dICHECK(a) { if (!(a)) { dDebug (d_ERR_IASSERT, \
  82. "assertion \"" #a "\" failed in %s:%u",__FILE__,__LINE__); *(int *)0 = 0; } }
  83. # endif
  84. // Argument assert is a special case of user assert
  85. #define dAASSERT(a) dUASSERT(a,"Bad argument(s)")
  86. /* floating point data type, vector, matrix and quaternion types */
  87. #if defined(dSINGLE)
  88. typedef float dReal;
  89. #ifdef dDOUBLE
  90. #error You can only #define dSINGLE or dDOUBLE, not both.
  91. #endif // dDOUBLE
  92. #elif defined(dDOUBLE)
  93. typedef double dReal;
  94. #else
  95. #error You must #define dSINGLE or dDOUBLE
  96. #endif
  97. // Detect if we've got both trimesh engines enabled.
  98. #if dTRIMESH_ENABLED
  99. #if dTRIMESH_OPCODE && dTRIMESH_GIMPACT
  100. #error You can only #define dTRIMESH_OPCODE or dTRIMESH_GIMPACT, not both.
  101. #endif
  102. #endif // dTRIMESH_ENABLED
  103. // Define a type for indices, either 16 or 32 bit, based on build option
  104. // TODO: Currently GIMPACT only supports 32 bit indices.
  105. #if dTRIMESH_16BIT_INDICES
  106. #if dTRIMESH_GIMPACT
  107. typedef uint32 dTriIndex;
  108. #else // dTRIMESH_GIMPACT
  109. typedef uint16 dTriIndex;
  110. #endif // dTRIMESH_GIMPACT
  111. #else // dTRIMESH_16BIT_INDICES
  112. typedef uint32 dTriIndex;
  113. #endif // dTRIMESH_16BIT_INDICES
  114. /* round an integer up to a multiple of 4, except that 0 and 1 are unmodified
  115. * (used to compute matrix leading dimensions)
  116. */
  117. #define dPAD(a) (((a) > 1) ? ((((a)-1)|3)+1) : (a))
  118. /* these types are mainly just used in headers */
  119. typedef dReal dVector3[4];
  120. typedef dReal dVector4[4];
  121. typedef dReal dMatrix3[4*3];
  122. typedef dReal dMatrix4[4*4];
  123. typedef dReal dMatrix6[8*6];
  124. typedef dReal dQuaternion[4];
  125. /* precision dependent scalar math functions */
  126. #if defined(dSINGLE)
  127. #define REAL(x) (x ## f) /* form a constant */
  128. #define dRecip(x) ((1.0f/(x))) /* reciprocal */
  129. #define dSqrt(x) (sqrtf(x)) /* square root */
  130. #define dRecipSqrt(x) ((1.0f/sqrtf(x))) /* reciprocal square root */
  131. #define dSin(x) (sinf(x)) /* sine */
  132. #define dCos(x) (cosf(x)) /* cosine */
  133. #define dFabs(x) (fabsf(x)) /* absolute value */
  134. #define dAtan2(y,x) (atan2f(y,x)) /* arc tangent with 2 args */
  135. #define dFMod(a,b) (fmodf(a,b)) /* modulo */
  136. #define dFloor(x) floorf(x) /* floor */
  137. #define dCeil(x) ceilf(x) /* floor */
  138. #define dCopySign(a,b) ((dReal)copysignf(a,b)) /* copy value sign */
  139. #define dNextAfter(x, y) nextafterf(x, y) /* next value after */
  140. #if defined(_ODE__NEXTAFTERF_REQUIRED)
  141. float _nextafterf(float x, float y);
  142. #endif
  143. #ifdef HAVE___ISNANF
  144. #define dIsNan(x) (__isnanf(x))
  145. #elif defined(HAVE__ISNANF)
  146. #define dIsNan(x) (_isnanf(x))
  147. #elif defined(HAVE_ISNANF)
  148. #define dIsNan(x) (isnanf(x))
  149. #else
  150. /*
  151. fall back to _isnan which is the VC way,
  152. this may seem redundant since we already checked
  153. for _isnan before, but if isnan is detected by
  154. configure but is not found during compilation
  155. we should always make sure we check for __isnanf,
  156. _isnanf and isnanf in that order before falling
  157. back to a default
  158. */
  159. #define dIsNan(x) (_isnan(x))
  160. #endif
  161. #elif defined(dDOUBLE)
  162. #define REAL(x) (x)
  163. #define dRecip(x) (1.0/(x))
  164. #define dSqrt(x) sqrt(x)
  165. #define dRecipSqrt(x) (1.0/sqrt(x))
  166. #define dSin(x) sin(x)
  167. #define dCos(x) cos(x)
  168. #define dFabs(x) fabs(x)
  169. #define dAtan2(y,x) atan2((y),(x))
  170. #define dFMod(a,b) (fmod((a),(b)))
  171. #define dFloor(x) floor(x)
  172. #define dCeil(x) ceil(x)
  173. #define dCopySign(a,b) (copysign((a),(b)))
  174. #define dNextAfter(x, y) nextafter(x, y)
  175. #undef _ODE__NEXTAFTERF_REQUIRED
  176. #ifdef HAVE___ISNAN
  177. #define dIsNan(x) (__isnan(x))
  178. #elif defined(HAVE__ISNAN)
  179. #define dIsNan(x) (_isnan(x))
  180. #elif defined(HAVE_ISNAN)
  181. #define dIsNan(x) (isnan(x))
  182. #else
  183. #define dIsNan(x) (_isnan(x))
  184. #endif
  185. #else
  186. #error You must #define dSINGLE or dDOUBLE
  187. #endif
  188. /* internal object types (all prefixed with `dx') */
  189. struct dxWorld; /* dynamics world */
  190. struct dxSpace; /* collision space */
  191. struct dxBody; /* rigid body (dynamics object) */
  192. struct dxGeom; /* geometry (collision object) */
  193. struct dxJoint;
  194. struct dxJointNode;
  195. struct dxJointGroup;
  196. struct dxWorldProcessThreadingManager;
  197. typedef struct dxWorld *dWorldID;
  198. typedef struct dxSpace *dSpaceID;
  199. typedef struct dxBody *dBodyID;
  200. typedef struct dxGeom *dGeomID;
  201. typedef struct dxJoint *dJointID;
  202. typedef struct dxJointGroup *dJointGroupID;
  203. typedef struct dxWorldProcessThreadingManager *dWorldStepThreadingManagerID;
  204. /* error numbers */
  205. enum {
  206. d_ERR_UNKNOWN = 0, /* unknown error */
  207. d_ERR_IASSERT, /* internal assertion failed */
  208. d_ERR_UASSERT, /* user assertion failed */
  209. d_ERR_LCP /* user assertion failed */
  210. };
  211. /* joint type numbers */
  212. typedef enum {
  213. dJointTypeNone = 0, /* or "unknown" */
  214. dJointTypeBall,
  215. dJointTypeHinge,
  216. dJointTypeSlider,
  217. dJointTypeContact,
  218. dJointTypeUniversal,
  219. dJointTypeHinge2,
  220. dJointTypeFixed,
  221. dJointTypeNull,
  222. dJointTypeAMotor,
  223. dJointTypeLMotor,
  224. dJointTypePlane2D,
  225. dJointTypePR,
  226. dJointTypePU,
  227. dJointTypePiston
  228. } dJointType;
  229. /* an alternative way of setting joint parameters, using joint parameter
  230. * structures and member constants. we don't actually do this yet.
  231. */
  232. /*
  233. typedef struct dLimot {
  234. int mode;
  235. dReal lostop, histop;
  236. dReal vel, fmax;
  237. dReal fudge_factor;
  238. dReal bounce, soft;
  239. dReal suspension_erp, suspension_cfm;
  240. } dLimot;
  241. enum {
  242. dLimotLoStop = 0x0001,
  243. dLimotHiStop = 0x0002,
  244. dLimotVel = 0x0004,
  245. dLimotFMax = 0x0008,
  246. dLimotFudgeFactor = 0x0010,
  247. dLimotBounce = 0x0020,
  248. dLimotSoft = 0x0040
  249. };
  250. */
  251. /* standard joint parameter names. why are these here? - because we don't want
  252. * to include all the joint function definitions in joint.cpp. hmmmm.
  253. * MSVC complains if we call D_ALL_PARAM_NAMES_X with a blank second argument,
  254. * which is why we have the D_ALL_PARAM_NAMES macro as well. please copy and
  255. * paste between these two.
  256. */
  257. #define D_ALL_PARAM_NAMES(start) \
  258. /* parameters for limits and motors */ \
  259. dParamLoStop = start, \
  260. dParamHiStop, \
  261. dParamVel, \
  262. dParamFMax, \
  263. dParamFudgeFactor, \
  264. dParamBounce, \
  265. dParamCFM, \
  266. dParamStopERP, \
  267. dParamStopCFM, \
  268. /* parameters for suspension */ \
  269. dParamSuspensionERP, \
  270. dParamSuspensionCFM, \
  271. dParamERP, \
  272. //////////////////////////////////////////////////////////////////////////////
  273. /// \enum D_ALL_PARAM_NAMES_X
  274. ///
  275. /// \var dParamGroup This is the starting value of the different group
  276. /// (i.e. dParamGroup1, dParamGroup2, dParamGroup3)
  277. /// It also helps in the use of parameter
  278. /// (dParamGroup2 | dParamFMax) == dParamFMax2
  279. //////////////////////////////////////////////////////////////////////////////
  280. #define D_ALL_PARAM_NAMES_X(start,x) \
  281. dParamGroup ## x = start, \
  282. /* parameters for limits and motors */ \
  283. dParamLoStop ## x = start, \
  284. dParamHiStop ## x, \
  285. dParamVel ## x, \
  286. dParamFMax ## x, \
  287. dParamFudgeFactor ## x, \
  288. dParamBounce ## x, \
  289. dParamCFM ## x, \
  290. dParamStopERP ## x, \
  291. dParamStopCFM ## x, \
  292. /* parameters for suspension */ \
  293. dParamSuspensionERP ## x, \
  294. dParamSuspensionCFM ## x, \
  295. dParamERP ## x,
  296. enum {
  297. D_ALL_PARAM_NAMES(0)
  298. dParamsInGroup, ///< Number of parameter in a group
  299. D_ALL_PARAM_NAMES_X(0x000,1)
  300. D_ALL_PARAM_NAMES_X(0x100,2)
  301. D_ALL_PARAM_NAMES_X(0x200,3)
  302. /* add a multiple of this constant to the basic parameter numbers to get
  303. * the parameters for the second, third etc axes.
  304. */
  305. dParamGroup=0x100
  306. };
  307. /* angular motor mode numbers */
  308. enum {
  309. dAMotorUser = 0,
  310. dAMotorEuler = 1
  311. };
  312. /* joint force feedback information */
  313. typedef struct dJointFeedback {
  314. dVector3 f1; /* force applied to body 1 */
  315. dVector3 t1; /* torque applied to body 1 */
  316. dVector3 f2; /* force applied to body 2 */
  317. dVector3 t2; /* torque applied to body 2 */
  318. } dJointFeedback;
  319. /* private functions that must be implemented by the collision library:
  320. * (1) indicate that a geom has moved, (2) get the next geom in a body list.
  321. * these functions are called whenever the position of geoms connected to a
  322. * body have changed, e.g. with dBodySetPosition(), dBodySetRotation(), or
  323. * when the ODE step function updates the body state.
  324. */
  325. void dGeomMoved (dGeomID);
  326. dGeomID dGeomGetBodyNext (dGeomID);
  327. /**
  328. * dGetConfiguration returns the specific ODE build configuration as
  329. * a string of tokens. The string can be parsed in a similar way to
  330. * the OpenGL extension mechanism, the naming convention should be
  331. * familiar too. The following extensions are reported:
  332. *
  333. * ODE
  334. * ODE_single_precision
  335. * ODE_double_precision
  336. * ODE_EXT_no_debug
  337. * ODE_EXT_trimesh
  338. * ODE_EXT_opcode
  339. * ODE_EXT_gimpact
  340. * ODE_EXT_malloc_not_alloca
  341. * ODE_EXT_gyroscopic
  342. * ODE_OPC_16bit_indices
  343. * ODE_OPC_new_collider
  344. */
  345. ODE_API const char* dGetConfiguration (void);
  346. /**
  347. * Helper to check for a token in the ODE configuration string.
  348. * Caution, this function is case sensitive.
  349. *
  350. * @param token A configuration token, see dGetConfiguration for details
  351. *
  352. * @return 1 if exact token is present, 0 if not present
  353. */
  354. ODE_API int dCheckConfiguration( const char* token );
  355. #ifdef __cplusplus
  356. }
  357. #endif
  358. #endif