common.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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. #define dMax(a, b) _ode_fmaxf(a, b)
  214. #define dMin(a, b) _ode_fminf(a, b)
  215. #ifdef HAVE___ISNANF
  216. #define dIsNan(x) (__isnanf(x))
  217. #elif defined(HAVE__ISNANF)
  218. #define dIsNan(x) (_isnanf(x))
  219. #elif defined(HAVE_ISNANF)
  220. #define dIsNan(x) (isnanf(x))
  221. #else
  222. /*
  223. fall back to _isnan which is the VC way,
  224. this may seem redundant since we already checked
  225. for _isnan before, but if isnan is detected by
  226. configure but is not found during compilation
  227. we should always make sure we check for __isnanf,
  228. _isnanf and isnanf in that order before falling
  229. back to a default
  230. */
  231. #define dIsNan(x) (_isnan(x))
  232. #endif
  233. #elif defined(dDOUBLE)
  234. #define REAL(x) (x)
  235. #define dRecip(x) (1.0/(x))
  236. #define dSqrt(x) sqrt(x)
  237. #define dRecipSqrt(x) (1.0/sqrt(x))
  238. #define dSin(x) sin(x)
  239. #define dCos(x) cos(x)
  240. #define dFabs(x) fabs(x)
  241. #define dAtan2(y,x) atan2((y),(x))
  242. #define dAsin(x) asin(x)
  243. #define dAcos(x) acos(x)
  244. #define dFMod(a,b) (fmod((a),(b)))
  245. #define dFloor(x) floor(x)
  246. #define dCeil(x) ceil(x)
  247. #define dCopySign(a,b) _ode_copysign(a, b)
  248. #define dNextAfter(x, y) _ode_nextafter(x, y)
  249. #define dMax(a, b) _ode_fmax(a, b)
  250. #define dMin(a, b) _ode_fmin(a, b)
  251. #ifdef HAVE___ISNAN
  252. #define dIsNan(x) (__isnan(x))
  253. #elif defined(HAVE__ISNAN)
  254. #define dIsNan(x) (_isnan(x))
  255. #elif defined(HAVE_ISNAN)
  256. #define dIsNan(x) (isnan(x))
  257. #else
  258. #define dIsNan(x) (_isnan(x))
  259. #endif
  260. #else
  261. #error You must #define dSINGLE or dDOUBLE
  262. #endif
  263. /* internal object types (all prefixed with `dx') */
  264. struct dxWorld; /* dynamics world */
  265. struct dxSpace; /* collision space */
  266. struct dxBody; /* rigid body (dynamics object) */
  267. struct dxGeom; /* geometry (collision object) */
  268. struct dxJoint; /* joint */
  269. struct dxJointGroup;/* joint group */
  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. /* error numbers */
  277. enum {
  278. d_ERR_UNKNOWN = 0, /* unknown error */
  279. d_ERR_IASSERT, /* internal assertion failed */
  280. d_ERR_UASSERT, /* user assertion failed */
  281. d_ERR_LCP /* user assertion failed */
  282. };
  283. /* joint type numbers */
  284. typedef enum {
  285. dJointTypeNone = 0, /* or "unknown" */
  286. dJointTypeBall,
  287. dJointTypeHinge,
  288. dJointTypeSlider,
  289. dJointTypeContact,
  290. dJointTypeUniversal,
  291. dJointTypeHinge2,
  292. dJointTypeFixed,
  293. dJointTypeNull,
  294. dJointTypeAMotor,
  295. dJointTypeLMotor,
  296. dJointTypePlane2D,
  297. dJointTypePR,
  298. dJointTypePU,
  299. dJointTypePiston,
  300. dJointTypeDBall,
  301. dJointTypeDHinge,
  302. dJointTypeTransmission,
  303. } dJointType;
  304. /* an alternative way of setting joint parameters, using joint parameter
  305. * structures and member constants. we don't actually do this yet.
  306. */
  307. /*
  308. typedef struct dLimot {
  309. int mode;
  310. dReal lostop, histop;
  311. dReal vel, fmax;
  312. dReal fudge_factor;
  313. dReal bounce, soft;
  314. dReal suspension_erp, suspension_cfm;
  315. } dLimot;
  316. enum {
  317. dLimotLoStop = 0x0001,
  318. dLimotHiStop = 0x0002,
  319. dLimotVel = 0x0004,
  320. dLimotFMax = 0x0008,
  321. dLimotFudgeFactor = 0x0010,
  322. dLimotBounce = 0x0020,
  323. dLimotSoft = 0x0040
  324. };
  325. */
  326. /* standard joint parameter names. why are these here? - because we don't want
  327. * to include all the joint function definitions in joint.cpp. hmmmm.
  328. * MSVC complains if we call D_ALL_PARAM_NAMES_X with a blank second argument,
  329. * which is why we have the D_ALL_PARAM_NAMES macro as well. please copy and
  330. * paste between these two.
  331. */
  332. #define D_ALL_PARAM_NAMES(start) \
  333. /* parameters for limits and motors */ \
  334. dParamLoStop = start, \
  335. dParamHiStop, \
  336. dParamVel, \
  337. dParamLoVel, \
  338. dParamHiVel, \
  339. dParamFMax, \
  340. dParamFudgeFactor, \
  341. dParamBounce, \
  342. dParamCFM, \
  343. dParamStopERP, \
  344. dParamStopCFM, \
  345. /* parameters for suspension */ \
  346. dParamSuspensionERP, \
  347. dParamSuspensionCFM, \
  348. dParamERP, \
  349. /*
  350. * \enum D_ALL_PARAM_NAMES_X
  351. *
  352. * \var dParamGroup This is the starting value of the different group
  353. * (i.e. dParamGroup1, dParamGroup2, dParamGroup3)
  354. * It also helps in the use of parameter
  355. * (dParamGroup2 | dParamFMax) == dParamFMax2
  356. */
  357. #define D_ALL_PARAM_NAMES_X(start,x) \
  358. dParamGroup ## x = start, \
  359. /* parameters for limits and motors */ \
  360. dParamLoStop ## x = start, \
  361. dParamHiStop ## x, \
  362. dParamVel ## x, \
  363. dParamLoVel ## x, \
  364. dParamHiVel ## x, \
  365. dParamFMax ## x, \
  366. dParamFudgeFactor ## x, \
  367. dParamBounce ## x, \
  368. dParamCFM ## x, \
  369. dParamStopERP ## x, \
  370. dParamStopCFM ## x, \
  371. /* parameters for suspension */ \
  372. dParamSuspensionERP ## x, \
  373. dParamSuspensionCFM ## x, \
  374. dParamERP ## x,
  375. enum {
  376. D_ALL_PARAM_NAMES(0)
  377. dParamsInGroup, /* < Number of parameter in a group */
  378. D_ALL_PARAM_NAMES_X(0x000,1)
  379. D_ALL_PARAM_NAMES_X(0x100,2)
  380. D_ALL_PARAM_NAMES_X(0x200,3)
  381. /* add a multiple of this constant to the basic parameter numbers to get
  382. * the parameters for the second, third etc axes.
  383. */
  384. dParamGroup=0x100
  385. };
  386. /* angular motor mode numbers */
  387. enum {
  388. dAMotorUser = 0,
  389. dAMotorEuler = 1
  390. };
  391. /* transmission joint mode numbers */
  392. enum {
  393. dTransmissionParallelAxes = 0,
  394. dTransmissionIntersectingAxes = 1,
  395. dTransmissionChainDrive = 2
  396. };
  397. /* joint force feedback information */
  398. typedef struct dJointFeedback {
  399. dVector3 f1; /* force applied to body 1 */
  400. dVector3 t1; /* torque applied to body 1 */
  401. dVector3 f2; /* force applied to body 2 */
  402. dVector3 t2; /* torque applied to body 2 */
  403. } dJointFeedback;
  404. /* private functions that must be implemented by the collision library:
  405. * (1) indicate that a geom has moved, (2) get the next geom in a body list.
  406. * these functions are called whenever the position of geoms connected to a
  407. * body have changed, e.g. with dBodySetPosition(), dBodySetRotation(), or
  408. * when the ODE step function updates the body state.
  409. */
  410. void dGeomMoved (dGeomID);
  411. dGeomID dGeomGetBodyNext (dGeomID);
  412. /**
  413. * dGetConfiguration returns the specific ODE build configuration as
  414. * a string of tokens. The string can be parsed in a similar way to
  415. * the OpenGL extension mechanism, the naming convention should be
  416. * familiar too. The following extensions are reported:
  417. *
  418. * ODE
  419. * ODE_single_precision
  420. * ODE_double_precision
  421. * ODE_EXT_no_debug
  422. * ODE_EXT_trimesh
  423. * ODE_EXT_opcode
  424. * ODE_EXT_gimpact
  425. * ODE_OPC_16bit_indices
  426. * ODE_OPC_new_collider
  427. * ODE_EXT_libccd
  428. * ODE_CCD_IMPL_internal
  429. * ODE_CCD_COLL_box_cyl
  430. * ODE_CCD_COLL_cyl_cyl
  431. * ODE_CCD_COLL_cap_cyl
  432. * ODE_CCD_COLL_box_conv
  433. * ODE_CCD_COLL_cap_conv
  434. * ODE_CCD_COLL_conv_cyl
  435. * ODE_CCD_COLL_conv_sph
  436. * ODE_CCD_COLL_conv_conv
  437. * ODE_EXT_mt_collisions
  438. * ODE_EXT_threading
  439. * ODE_THR_builtin_impl
  440. */
  441. ODE_API const char* dGetConfiguration (void);
  442. /**
  443. * Helper to check for a token in the ODE configuration string.
  444. * Caution, this function is case sensitive.
  445. *
  446. * @param token A configuration token, see dGetConfiguration for details
  447. *
  448. * @return 1 if exact token is present, 0 if not present
  449. */
  450. ODE_API int dCheckConfiguration( const char* token );
  451. #ifdef __cplusplus
  452. }
  453. #endif
  454. #endif