common.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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)-1)|3)+1) : (a))
  77. /* these types are mainly just used in headers */
  78. typedef dReal dVector3[4];
  79. typedef dReal dVector4[4];
  80. typedef dReal dMatrix3[4*3];
  81. typedef dReal dMatrix4[4*4];
  82. typedef dReal dMatrix6[8*6];
  83. typedef dReal dQuaternion[4];
  84. /* precision dependent scalar math functions */
  85. #if defined(dSINGLE)
  86. #define REAL(x) (x##f) /* form a constant */
  87. #define dRecip(x) ((1.0f/(x))) /* reciprocal */
  88. #define dSqrt(x) (sqrtf(x)) /* square root */
  89. #define dRecipSqrt(x) ((1.0f/sqrtf(x))) /* reciprocal square root */
  90. #define dSin(x) (sinf(x)) /* sine */
  91. #define dCos(x) (cosf(x)) /* cosine */
  92. #define dFabs(x) (fabsf(x)) /* absolute value */
  93. #define dAtan2(y,x) (atan2f(y,x)) /* arc tangent with 2 args */
  94. #define dAcos(x) (acosf(x))
  95. #define dFMod(a,b) (fmodf(a,b)) /* modulo */
  96. #define dFloor(x) floorf(x) /* floor */
  97. #define dCeil(x) ceilf(x) /* ceil */
  98. #define dCopySign(a,b) _ode_copysignf(a, b) /* copy value sign */
  99. #define dNextAfter(x, y) _ode_nextafterf(x, y) /* next value after */
  100. #ifdef HAVE___ISNANF
  101. #define dIsNan(x) (__isnanf(x))
  102. #elif defined(HAVE__ISNANF)
  103. #define dIsNan(x) (_isnanf(x))
  104. #elif defined(HAVE_ISNANF)
  105. #define dIsNan(x) (isnanf(x))
  106. #else
  107. /*
  108. fall back to _isnan which is the VC way,
  109. this may seem redundant since we already checked
  110. for _isnan before, but if isnan is detected by
  111. configure but is not found during compilation
  112. we should always make sure we check for __isnanf,
  113. _isnanf and isnanf in that order before falling
  114. back to a default
  115. */
  116. #define dIsNan(x) (_isnan(x))
  117. #endif
  118. #elif defined(dDOUBLE)
  119. #define REAL(x) (x)
  120. #define dRecip(x) (1.0/(x))
  121. #define dSqrt(x) sqrt(x)
  122. #define dRecipSqrt(x) (1.0/sqrt(x))
  123. #define dSin(x) sin(x)
  124. #define dCos(x) cos(x)
  125. #define dFabs(x) fabs(x)
  126. #define dAtan2(y,x) atan2((y),(x))
  127. #define dAcos(x) acos(x)
  128. #define dFMod(a,b) (fmod((a),(b)))
  129. #define dFloor(x) floor(x)
  130. #define dCeil(x) ceil(x)
  131. #define dCopySign(a,b) _ode_copysign(a, b)
  132. #define dNextAfter(x, y) _ode_nextafter(x, y)
  133. #ifdef HAVE___ISNAN
  134. #define dIsNan(x) (__isnan(x))
  135. #elif defined(HAVE__ISNAN)
  136. #define dIsNan(x) (_isnan(x))
  137. #elif defined(HAVE_ISNAN)
  138. #define dIsNan(x) (isnan(x))
  139. #else
  140. #define dIsNan(x) (_isnan(x))
  141. #endif
  142. #else
  143. #error You must #define dSINGLE or dDOUBLE
  144. #endif
  145. /* internal object types (all prefixed with `dx') */
  146. struct dxWorld; /* dynamics world */
  147. struct dxSpace; /* collision space */
  148. struct dxBody; /* rigid body (dynamics object) */
  149. struct dxGeom; /* geometry (collision object) */
  150. struct dxJoint;
  151. struct dxJointNode;
  152. struct dxJointGroup;
  153. struct dxWorldProcessThreadingManager;
  154. typedef struct dxWorld *dWorldID;
  155. typedef struct dxSpace *dSpaceID;
  156. typedef struct dxBody *dBodyID;
  157. typedef struct dxGeom *dGeomID;
  158. typedef struct dxJoint *dJointID;
  159. typedef struct dxJointGroup *dJointGroupID;
  160. typedef struct dxWorldProcessThreadingManager *dWorldStepThreadingManagerID;
  161. /* error numbers */
  162. enum {
  163. d_ERR_UNKNOWN = 0, /* unknown error */
  164. d_ERR_IASSERT, /* internal assertion failed */
  165. d_ERR_UASSERT, /* user assertion failed */
  166. d_ERR_LCP /* user assertion failed */
  167. };
  168. /* joint type numbers */
  169. typedef enum {
  170. dJointTypeNone = 0, /* or "unknown" */
  171. dJointTypeBall,
  172. dJointTypeHinge,
  173. dJointTypeSlider,
  174. dJointTypeContact,
  175. dJointTypeUniversal,
  176. dJointTypeHinge2,
  177. dJointTypeFixed,
  178. dJointTypeNull,
  179. dJointTypeAMotor,
  180. dJointTypeLMotor,
  181. dJointTypePlane2D,
  182. dJointTypePR,
  183. dJointTypePU,
  184. dJointTypePiston,
  185. dJointTypeDBall,
  186. dJointTypeDHinge,
  187. dJointTypeTransmission,
  188. } dJointType;
  189. /* an alternative way of setting joint parameters, using joint parameter
  190. * structures and member constants. we don't actually do this yet.
  191. */
  192. /*
  193. typedef struct dLimot {
  194. int mode;
  195. dReal lostop, histop;
  196. dReal vel, fmax;
  197. dReal fudge_factor;
  198. dReal bounce, soft;
  199. dReal suspension_erp, suspension_cfm;
  200. } dLimot;
  201. enum {
  202. dLimotLoStop = 0x0001,
  203. dLimotHiStop = 0x0002,
  204. dLimotVel = 0x0004,
  205. dLimotFMax = 0x0008,
  206. dLimotFudgeFactor = 0x0010,
  207. dLimotBounce = 0x0020,
  208. dLimotSoft = 0x0040
  209. };
  210. */
  211. /* standard joint parameter names. why are these here? - because we don't want
  212. * to include all the joint function definitions in joint.cpp. hmmmm.
  213. * MSVC complains if we call D_ALL_PARAM_NAMES_X with a blank second argument,
  214. * which is why we have the D_ALL_PARAM_NAMES macro as well. please copy and
  215. * paste between these two.
  216. */
  217. #define D_ALL_PARAM_NAMES(start) \
  218. /* parameters for limits and motors */ \
  219. dParamLoStop = start, \
  220. dParamHiStop, \
  221. dParamVel, \
  222. dParamLoVel, \
  223. dParamHiVel, \
  224. dParamFMax, \
  225. dParamFudgeFactor, \
  226. dParamBounce, \
  227. dParamCFM, \
  228. dParamStopERP, \
  229. dParamStopCFM, \
  230. /* parameters for suspension */ \
  231. dParamSuspensionERP, \
  232. dParamSuspensionCFM, \
  233. dParamERP, \
  234. /*
  235. * \enum D_ALL_PARAM_NAMES_X
  236. *
  237. * \var dParamGroup This is the starting value of the different group
  238. * (i.e. dParamGroup1, dParamGroup2, dParamGroup3)
  239. * It also helps in the use of parameter
  240. * (dParamGroup2 | dParamFMax) == dParamFMax2
  241. */
  242. #define D_ALL_PARAM_NAMES_X(start,x) \
  243. dParamGroup ## x = start, \
  244. /* parameters for limits and motors */ \
  245. dParamLoStop ## x = start, \
  246. dParamHiStop ## x, \
  247. dParamVel ## x, \
  248. dParamLoVel ## x, \
  249. dParamHiVel ## x, \
  250. dParamFMax ## x, \
  251. dParamFudgeFactor ## x, \
  252. dParamBounce ## x, \
  253. dParamCFM ## x, \
  254. dParamStopERP ## x, \
  255. dParamStopCFM ## x, \
  256. /* parameters for suspension */ \
  257. dParamSuspensionERP ## x, \
  258. dParamSuspensionCFM ## x, \
  259. dParamERP ## x,
  260. enum {
  261. D_ALL_PARAM_NAMES(0)
  262. dParamsInGroup, /* < Number of parameter in a group */
  263. D_ALL_PARAM_NAMES_X(0x000,1)
  264. D_ALL_PARAM_NAMES_X(0x100,2)
  265. D_ALL_PARAM_NAMES_X(0x200,3)
  266. /* add a multiple of this constant to the basic parameter numbers to get
  267. * the parameters for the second, third etc axes.
  268. */
  269. dParamGroup=0x100
  270. };
  271. /* angular motor mode numbers */
  272. enum {
  273. dAMotorUser = 0,
  274. dAMotorEuler = 1
  275. };
  276. /* transmission joint mode numbers */
  277. enum {
  278. dTransmissionParallelAxes = 0,
  279. dTransmissionIntersectingAxes = 1,
  280. dTransmissionChainDrive = 2
  281. };
  282. /* joint force feedback information */
  283. typedef struct dJointFeedback {
  284. dVector3 f1; /* force applied to body 1 */
  285. dVector3 t1; /* torque applied to body 1 */
  286. dVector3 f2; /* force applied to body 2 */
  287. dVector3 t2; /* torque applied to body 2 */
  288. } dJointFeedback;
  289. /* private functions that must be implemented by the collision library:
  290. * (1) indicate that a geom has moved, (2) get the next geom in a body list.
  291. * these functions are called whenever the position of geoms connected to a
  292. * body have changed, e.g. with dBodySetPosition(), dBodySetRotation(), or
  293. * when the ODE step function updates the body state.
  294. */
  295. void dGeomMoved (dGeomID);
  296. dGeomID dGeomGetBodyNext (dGeomID);
  297. /**
  298. * dGetConfiguration returns the specific ODE build configuration as
  299. * a string of tokens. The string can be parsed in a similar way to
  300. * the OpenGL extension mechanism, the naming convention should be
  301. * familiar too. The following extensions are reported:
  302. *
  303. * ODE
  304. * ODE_single_precision
  305. * ODE_double_precision
  306. * ODE_EXT_no_debug
  307. * ODE_EXT_trimesh
  308. * ODE_EXT_opcode
  309. * ODE_EXT_gimpact
  310. * ODE_OPC_16bit_indices
  311. * ODE_OPC_new_collider
  312. * ODE_EXT_mt_collisions
  313. * ODE_EXT_threading
  314. * ODE_THR_builtin_impl
  315. */
  316. ODE_API const char* dGetConfiguration (void);
  317. /**
  318. * Helper to check for a token in the ODE configuration string.
  319. * Caution, this function is case sensitive.
  320. *
  321. * @param token A configuration token, see dGetConfiguration for details
  322. *
  323. * @return 1 if exact token is present, 0 if not present
  324. */
  325. ODE_API int dCheckConfiguration( const char* token );
  326. #ifdef __cplusplus
  327. }
  328. #endif
  329. #endif