common.h 12 KB

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