common.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. /* configuration stuff */
  30. /* constants */
  31. /* pi and 1/sqrt(2) are defined here if necessary because they don't get
  32. * defined in <math.h> on some platforms (like MS-Windows)
  33. */
  34. #ifndef M_PI
  35. #define M_PI REAL(3.1415926535897932384626433832795029)
  36. #endif
  37. #ifndef M_PI_2
  38. #define M_PI_2 REAL(1.5707963267948966192313216916398)
  39. #endif
  40. #ifndef M_SQRT1_2
  41. #define M_SQRT1_2 REAL(0.7071067811865475244008443621048490)
  42. #endif
  43. /* floating point data type, vector, matrix and quaternion types */
  44. #if defined(dSINGLE)
  45. typedef float dReal;
  46. #ifdef dDOUBLE
  47. #error You can only #define dSINGLE or dDOUBLE, not both.
  48. #endif /* dDOUBLE */
  49. #elif defined(dDOUBLE)
  50. typedef double dReal;
  51. #else
  52. #error You must #define dSINGLE or dDOUBLE
  53. #endif
  54. /* Detect if we've got both trimesh engines enabled. */
  55. #if dTRIMESH_ENABLED
  56. #if dTRIMESH_OPCODE && dTRIMESH_GIMPACT
  57. #error You can only #define dTRIMESH_OPCODE or dTRIMESH_GIMPACT, not both.
  58. #endif
  59. #endif /* dTRIMESH_ENABLED */
  60. /*
  61. * Define a type for indices, either 16 or 32 bit, based on build option
  62. * TODO: Currently GIMPACT only supports 32 bit indices.
  63. */
  64. #if dTRIMESH_16BIT_INDICES
  65. #if dTRIMESH_GIMPACT
  66. typedef duint32 dTriIndex;
  67. #else /* dTRIMESH_GIMPACT */
  68. typedef duint16 dTriIndex;
  69. #endif /* dTRIMESH_GIMPACT */
  70. #else /* dTRIMESH_16BIT_INDICES */
  71. typedef duint32 dTriIndex;
  72. #endif /* dTRIMESH_16BIT_INDICES */
  73. /* round an integer up to a multiple of 4, except that 0 and 1 are unmodified
  74. * (used to compute matrix leading dimensions)
  75. */
  76. #define dPAD(a) (((a) > 1) ? (((a) + 3) & (int)(~3)) : (a))
  77. typedef enum {
  78. dSA__MIN,
  79. dSA_X = dSA__MIN,
  80. dSA_Y,
  81. dSA_Z,
  82. dSA__MAX,
  83. } dSpaceAxis;
  84. typedef enum {
  85. dMD__MIN,
  86. dMD_LINEAR = dMD__MIN,
  87. dMD_ANGULAR,
  88. dMD__MAX,
  89. } dMotionDynamics;
  90. typedef enum {
  91. dDA__MIN,
  92. dDA__L_MIN = dDA__MIN + dMD_LINEAR * dSA__MAX,
  93. dDA_LX = dDA__L_MIN + dSA_X,
  94. dDA_LY = dDA__L_MIN + dSA_Y,
  95. dDA_LZ = dDA__L_MIN + dSA_Z,
  96. dDA__L_MAX = dDA__L_MIN + dSA__MAX,
  97. dDA__A_MIN = dDA__MIN + dMD_ANGULAR * dSA__MAX,
  98. dDA_AX = dDA__A_MIN + dSA_X,
  99. dDA_AY = dDA__A_MIN + dSA_Y,
  100. dDA_AZ = dDA__A_MIN + dSA_Z,
  101. dDA__A_MAX = dDA__A_MIN + dSA__MAX,
  102. dDA__MAX = dDA__MIN + dMD__MAX * dSA__MAX,
  103. } dDynamicsAxis;
  104. typedef enum {
  105. dV3E__MIN,
  106. dV3E__AXES_MIN = dV3E__MIN,
  107. dV3E_X = dV3E__AXES_MIN + dSA_X,
  108. dV3E_Y = dV3E__AXES_MIN + dSA_Y,
  109. dV3E_Z = dV3E__AXES_MIN + dSA_Z,
  110. dV3E__AXES_MAX = dV3E__AXES_MIN + dSA__MAX,
  111. dV3E_PAD = dV3E__AXES_MAX,
  112. dV3E__MAX,
  113. dV3E__AXES_COUNT = dV3E__AXES_MAX - dV3E__AXES_MIN,
  114. } dVec3Element;
  115. typedef enum {
  116. dV4E__MIN,
  117. dV4E_X = dV4E__MIN + dSA_X,
  118. dV4E_Y = dV4E__MIN + dSA_Y,
  119. dV4E_Z = dV4E__MIN + dSA_Z,
  120. dV4E_O = dV4E__MIN + dSA__MAX,
  121. dV4E__MAX,
  122. } dVec4Element;
  123. typedef enum {
  124. dM3E__MIN,
  125. dM3E__X_MIN = dM3E__MIN + dSA_X * dV3E__MAX,
  126. dM3E__X_AXES_MIN = dM3E__X_MIN + dV3E__AXES_MIN,
  127. dM3E_XX = dM3E__X_MIN + dV3E_X,
  128. dM3E_XY = dM3E__X_MIN + dV3E_Y,
  129. dM3E_XZ = dM3E__X_MIN + dV3E_Z,
  130. dM3E__X_AXES_MAX = dM3E__X_MIN + dV3E__AXES_MAX,
  131. dM3E_XPAD = dM3E__X_MIN + dV3E_PAD,
  132. dM3E__X_MAX = dM3E__X_MIN + dV3E__MAX,
  133. dM3E__Y_MIN = dM3E__MIN + dSA_Y * dV3E__MAX,
  134. dM3E__Y_AXES_MIN = dM3E__Y_MIN + dV3E__AXES_MIN,
  135. dM3E_YX = dM3E__Y_MIN + dV3E_X,
  136. dM3E_YY = dM3E__Y_MIN + dV3E_Y,
  137. dM3E_YZ = dM3E__Y_MIN + dV3E_Z,
  138. dM3E__Y_AXES_MAX = dM3E__Y_MIN + dV3E__AXES_MAX,
  139. dM3E_YPAD = dM3E__Y_MIN + dV3E_PAD,
  140. dM3E__Y_MAX = dM3E__Y_MIN + dV3E__MAX,
  141. dM3E__Z_MIN = dM3E__MIN + dSA_Z * dV3E__MAX,
  142. dM3E__Z_AXES_MIN = dM3E__Z_MIN + dV3E__AXES_MIN,
  143. dM3E_ZX = dM3E__Z_MIN + dV3E_X,
  144. dM3E_ZY = dM3E__Z_MIN + dV3E_Y,
  145. dM3E_ZZ = dM3E__Z_MIN + dV3E_Z,
  146. dM3E__Z_AXES_MAX = dM3E__Z_MIN + dV3E__AXES_MAX,
  147. dM3E_ZPAD = dM3E__Z_MIN + dV3E_PAD,
  148. dM3E__Z_MAX = dM3E__Z_MIN + dV3E__MAX,
  149. dM3E__MAX = dM3E__MIN + dSA__MAX * dV3E__MAX,
  150. } dMat3Element;
  151. typedef enum {
  152. dM4E__MIN,
  153. dM4E__X_MIN = dM4E__MIN + dV4E_X * dV4E__MAX,
  154. dM4E_XX = dM4E__X_MIN + dV4E_X,
  155. dM4E_XY = dM4E__X_MIN + dV4E_Y,
  156. dM4E_XZ = dM4E__X_MIN + dV4E_Z,
  157. dM4E_XO = dM4E__X_MIN + dV4E_O,
  158. dM4E__X_MAX = dM4E__X_MIN + dV4E__MAX,
  159. dM4E__Y_MIN = dM4E__MIN + dV4E_Y * dV4E__MAX,
  160. dM4E_YX = dM4E__Y_MIN + dV4E_X,
  161. dM4E_YY = dM4E__Y_MIN + dV4E_Y,
  162. dM4E_YZ = dM4E__Y_MIN + dV4E_Z,
  163. dM4E_YO = dM4E__Y_MIN + dV4E_O,
  164. dM4E__Y_MAX = dM4E__Y_MIN + dV4E__MAX,
  165. dM4E__Z_MIN = dM4E__MIN + dV4E_Z * dV4E__MAX,
  166. dM4E_ZX = dM4E__Z_MIN + dV4E_X,
  167. dM4E_ZY = dM4E__Z_MIN + dV4E_Y,
  168. dM4E_ZZ = dM4E__Z_MIN + dV4E_Z,
  169. dM4E_ZO = dM4E__Z_MIN + dV4E_O,
  170. dM4E__Z_MAX = dM4E__Z_MIN + dV4E__MAX,
  171. dM4E__O_MIN = dM4E__MIN + dV4E_O * dV4E__MAX,
  172. dM4E_OX = dM4E__O_MIN + dV4E_X,
  173. dM4E_OY = dM4E__O_MIN + dV4E_Y,
  174. dM4E_OZ = dM4E__O_MIN + dV4E_Z,
  175. dM4E_OO = dM4E__O_MIN + dV4E_O,
  176. dM4E__O_MAX = dM4E__O_MIN + dV4E__MAX,
  177. dM4E__MAX = dM4E__MIN + dV4E__MAX * dV4E__MAX,
  178. } dMat4Element;
  179. typedef enum {
  180. dQUE__MIN,
  181. dQUE_R = dQUE__MIN,
  182. dQUE__AXIS_MIN,
  183. dQUE_I = dQUE__AXIS_MIN + dSA_X,
  184. dQUE_J = dQUE__AXIS_MIN + dSA_Y,
  185. dQUE_K = dQUE__AXIS_MIN + dSA_Z,
  186. dQUE__AXIS_MAX = dQUE__AXIS_MIN + dSA__MAX,
  187. dQUE__MAX = dQUE__AXIS_MAX,
  188. } dQuatElement;
  189. /* these types are mainly just used in headers */
  190. typedef dReal dVector3[dV3E__MAX];
  191. typedef dReal dVector4[dV4E__MAX];
  192. typedef dReal dMatrix3[dM3E__MAX];
  193. typedef dReal dMatrix4[dM4E__MAX];
  194. typedef dReal dMatrix6[(dMD__MAX * dV3E__MAX) * (dMD__MAX * dSA__MAX)];
  195. typedef dReal dQuaternion[dQUE__MAX];
  196. /* precision dependent scalar math functions */
  197. #if defined(dSINGLE)
  198. #define REAL(x) (x##f) /* form a constant */
  199. #define dRecip(x) ((1.0f/(x))) /* reciprocal */
  200. #define dSqrt(x) (sqrtf(x)) /* square root */
  201. #define dRecipSqrt(x) ((1.0f/sqrtf(x))) /* reciprocal square root */
  202. #define dSin(x) (sinf(x)) /* sine */
  203. #define dCos(x) (cosf(x)) /* cosine */
  204. #define dFabs(x) (fabsf(x)) /* absolute value */
  205. #define dAtan2(y,x) (atan2f(y,x)) /* arc tangent with 2 args */
  206. #define dAsin(x) (asinf(x))
  207. #define dAcos(x) (acosf(x))
  208. #define dFMod(a,b) (fmodf(a,b)) /* modulo */
  209. #define dFloor(x) floorf(x) /* floor */
  210. #define dCeil(x) ceilf(x) /* ceil */
  211. #define dCopySign(a,b) _ode_copysignf(a, b) /* copy value sign */
  212. #define dNextAfter(x, y) _ode_nextafterf(x, y) /* next value after */
  213. #ifdef HAVE___ISNANF
  214. #define dIsNan(x) (__isnanf(x))
  215. #elif defined(HAVE__ISNANF)
  216. #define dIsNan(x) (_isnanf(x))
  217. #elif defined(HAVE_ISNANF)
  218. #define dIsNan(x) (isnanf(x))
  219. #else
  220. /*
  221. fall back to _isnan which is the VC way,
  222. this may seem redundant since we already checked
  223. for _isnan before, but if isnan is detected by
  224. configure but is not found during compilation
  225. we should always make sure we check for __isnanf,
  226. _isnanf and isnanf in that order before falling
  227. back to a default
  228. */
  229. #define dIsNan(x) (_isnan(x))
  230. #endif
  231. #elif defined(dDOUBLE)
  232. #define REAL(x) (x)
  233. #define dRecip(x) (1.0/(x))
  234. #define dSqrt(x) sqrt(x)
  235. #define dRecipSqrt(x) (1.0/sqrt(x))
  236. #define dSin(x) sin(x)
  237. #define dCos(x) cos(x)
  238. #define dFabs(x) fabs(x)
  239. #define dAtan2(y,x) atan2((y),(x))
  240. #define dAsin(x) asin(x)
  241. #define dAcos(x) acos(x)
  242. #define dFMod(a,b) (fmod((a),(b)))
  243. #define dFloor(x) floor(x)
  244. #define dCeil(x) ceil(x)
  245. #define dCopySign(a,b) _ode_copysign(a, b)
  246. #define dNextAfter(x, y) _ode_nextafter(x, y)
  247. #ifdef HAVE___ISNAN
  248. #define dIsNan(x) (__isnan(x))
  249. #elif defined(HAVE__ISNAN)
  250. #define dIsNan(x) (_isnan(x))
  251. #elif defined(HAVE_ISNAN)
  252. #define dIsNan(x) (isnan(x))
  253. #else
  254. #define dIsNan(x) (_isnan(x))
  255. #endif
  256. #else
  257. #error You must #define dSINGLE or dDOUBLE
  258. #endif
  259. ODE_PURE_INLINE dReal dMin(dReal x, dReal y) { return x <= y ? x : y; }
  260. ODE_PURE_INLINE dReal dMax(dReal x, dReal y) { return x <= y ? y : x; }
  261. /* internal object types (all prefixed with `dx') */
  262. struct dxWorld; /* dynamics world */
  263. struct dxSpace; /* collision space */
  264. struct dxBody; /* rigid body (dynamics object) */
  265. struct dxGeom; /* geometry (collision object) */
  266. struct dxJoint;
  267. struct dxJointNode;
  268. struct dxJointGroup;
  269. struct dxWorldProcessThreadingManager;
  270. typedef struct dxWorld *dWorldID;
  271. typedef struct dxSpace *dSpaceID;
  272. typedef struct dxBody *dBodyID;
  273. typedef struct dxGeom *dGeomID;
  274. typedef struct dxJoint *dJointID;
  275. typedef struct dxJointGroup *dJointGroupID;
  276. typedef struct dxWorldProcessThreadingManager *dWorldStepThreadingManagerID;
  277. /* error numbers */
  278. enum {
  279. d_ERR_UNKNOWN = 0, /* unknown error */
  280. d_ERR_IASSERT, /* internal assertion failed */
  281. d_ERR_UASSERT, /* user assertion failed */
  282. d_ERR_LCP /* user assertion failed */
  283. };
  284. /* joint type numbers */
  285. typedef enum {
  286. dJointTypeNone = 0, /* or "unknown" */
  287. dJointTypeBall,
  288. dJointTypeHinge,
  289. dJointTypeSlider,
  290. dJointTypeContact,
  291. dJointTypeUniversal,
  292. dJointTypeHinge2,
  293. dJointTypeFixed,
  294. dJointTypeNull,
  295. dJointTypeAMotor,
  296. dJointTypeLMotor,
  297. dJointTypePlane2D,
  298. dJointTypePR,
  299. dJointTypePU,
  300. dJointTypePiston,
  301. dJointTypeDBall,
  302. dJointTypeDHinge,
  303. dJointTypeTransmission,
  304. } dJointType;
  305. /* an alternative way of setting joint parameters, using joint parameter
  306. * structures and member constants. we don't actually do this yet.
  307. */
  308. /*
  309. typedef struct dLimot {
  310. int mode;
  311. dReal lostop, histop;
  312. dReal vel, fmax;
  313. dReal fudge_factor;
  314. dReal bounce, soft;
  315. dReal suspension_erp, suspension_cfm;
  316. } dLimot;
  317. enum {
  318. dLimotLoStop = 0x0001,
  319. dLimotHiStop = 0x0002,
  320. dLimotVel = 0x0004,
  321. dLimotFMax = 0x0008,
  322. dLimotFudgeFactor = 0x0010,
  323. dLimotBounce = 0x0020,
  324. dLimotSoft = 0x0040
  325. };
  326. */
  327. /* standard joint parameter names. why are these here? - because we don't want
  328. * to include all the joint function definitions in joint.cpp. hmmmm.
  329. * MSVC complains if we call D_ALL_PARAM_NAMES_X with a blank second argument,
  330. * which is why we have the D_ALL_PARAM_NAMES macro as well. please copy and
  331. * paste between these two.
  332. */
  333. #define D_ALL_PARAM_NAMES(start) \
  334. /* parameters for limits and motors */ \
  335. dParamLoStop = start, \
  336. dParamHiStop, \
  337. dParamVel, \
  338. dParamLoVel, \
  339. dParamHiVel, \
  340. dParamFMax, \
  341. dParamFudgeFactor, \
  342. dParamBounce, \
  343. dParamCFM, \
  344. dParamStopERP, \
  345. dParamStopCFM, \
  346. /* parameters for suspension */ \
  347. dParamSuspensionERP, \
  348. dParamSuspensionCFM, \
  349. dParamERP, \
  350. /*
  351. * \enum D_ALL_PARAM_NAMES_X
  352. *
  353. * \var dParamGroup This is the starting value of the different group
  354. * (i.e. dParamGroup1, dParamGroup2, dParamGroup3)
  355. * It also helps in the use of parameter
  356. * (dParamGroup2 | dParamFMax) == dParamFMax2
  357. */
  358. #define D_ALL_PARAM_NAMES_X(start,x) \
  359. dParamGroup ## x = start, \
  360. /* parameters for limits and motors */ \
  361. dParamLoStop ## x = start, \
  362. dParamHiStop ## x, \
  363. dParamVel ## x, \
  364. dParamLoVel ## x, \
  365. dParamHiVel ## x, \
  366. dParamFMax ## x, \
  367. dParamFudgeFactor ## x, \
  368. dParamBounce ## x, \
  369. dParamCFM ## x, \
  370. dParamStopERP ## x, \
  371. dParamStopCFM ## x, \
  372. /* parameters for suspension */ \
  373. dParamSuspensionERP ## x, \
  374. dParamSuspensionCFM ## x, \
  375. dParamERP ## x,
  376. enum {
  377. D_ALL_PARAM_NAMES(0)
  378. dParamsInGroup, /* < Number of parameter in a group */
  379. D_ALL_PARAM_NAMES_X(0x000,1)
  380. D_ALL_PARAM_NAMES_X(0x100,2)
  381. D_ALL_PARAM_NAMES_X(0x200,3)
  382. /* add a multiple of this constant to the basic parameter numbers to get
  383. * the parameters for the second, third etc axes.
  384. */
  385. dParamGroup=0x100
  386. };
  387. /* angular motor mode numbers */
  388. enum {
  389. dAMotorUser = 0,
  390. dAMotorEuler = 1
  391. };
  392. /* transmission joint mode numbers */
  393. enum {
  394. dTransmissionParallelAxes = 0,
  395. dTransmissionIntersectingAxes = 1,
  396. dTransmissionChainDrive = 2
  397. };
  398. /* joint force feedback information */
  399. typedef struct dJointFeedback {
  400. dVector3 f1; /* force applied to body 1 */
  401. dVector3 t1; /* torque applied to body 1 */
  402. dVector3 f2; /* force applied to body 2 */
  403. dVector3 t2; /* torque applied to body 2 */
  404. } dJointFeedback;
  405. /* private functions that must be implemented by the collision library:
  406. * (1) indicate that a geom has moved, (2) get the next geom in a body list.
  407. * these functions are called whenever the position of geoms connected to a
  408. * body have changed, e.g. with dBodySetPosition(), dBodySetRotation(), or
  409. * when the ODE step function updates the body state.
  410. */
  411. void dGeomMoved (dGeomID);
  412. dGeomID dGeomGetBodyNext (dGeomID);
  413. /**
  414. * dGetConfiguration returns the specific ODE build configuration as
  415. * a string of tokens. The string can be parsed in a similar way to
  416. * the OpenGL extension mechanism, the naming convention should be
  417. * familiar too. The following extensions are reported:
  418. *
  419. * ODE
  420. * ODE_single_precision
  421. * ODE_double_precision
  422. * ODE_EXT_no_debug
  423. * ODE_EXT_trimesh
  424. * ODE_EXT_opcode
  425. * ODE_EXT_gimpact
  426. * ODE_OPC_16bit_indices
  427. * ODE_OPC_new_collider
  428. * ODE_EXT_mt_collisions
  429. * ODE_EXT_threading
  430. * ODE_THR_builtin_impl
  431. */
  432. ODE_API const char* dGetConfiguration (void);
  433. /**
  434. * Helper to check for a token in the ODE configuration string.
  435. * Caution, this function is case sensitive.
  436. *
  437. * @param token A configuration token, see dGetConfiguration for details
  438. *
  439. * @return 1 if exact token is present, 0 if not present
  440. */
  441. ODE_API int dCheckConfiguration( const char* token );
  442. #ifdef __cplusplus
  443. }
  444. #endif
  445. #endif