common.h 11 KB

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