|
1 /************************************************************************* |
|
2 * * |
|
3 * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith. * |
|
4 * All rights reserved. Email: russ@q12.org 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 |
|
23 #ifndef _ODE_COMMON_H_ |
|
24 #define _ODE_COMMON_H_ |
|
25 #include <ode/config.h> |
|
26 #include <ode/error.h> |
|
27 #include <math.h> |
|
28 |
|
29 #include <ode/lookup_tables.h> |
|
30 |
|
31 #ifdef __cplusplus |
|
32 extern "C" { |
|
33 #endif |
|
34 |
|
35 |
|
36 /* configuration stuff */ |
|
37 |
|
38 /* the efficient alignment. most platforms align data structures to some |
|
39 * number of bytes, but this is not always the most efficient alignment. |
|
40 * for example, many x86 compilers align to 4 bytes, but on a pentium it |
|
41 * is important to align doubles to 8 byte boundaries (for speed), and |
|
42 * the 4 floats in a SIMD register to 16 byte boundaries. many other |
|
43 * platforms have similar behavior. setting a larger alignment can waste |
|
44 * a (very) small amount of memory. NOTE: this number must be a power of |
|
45 * two. this is set to 16 by default. |
|
46 */ |
|
47 #define EFFICIENT_ALIGNMENT 16 |
|
48 #define QFACTOR 16 // 16 |
|
49 |
|
50 |
|
51 /* constants */ |
|
52 |
|
53 /* pi and 1/sqrt(2) are defined here if necessary because they don't get |
|
54 * defined in <math.h> on some platforms (like MS-Windows) |
|
55 */ |
|
56 |
|
57 #define dPI REAL(3.1415926535897932384626433832795029) |
|
58 #define dSQRT1_2 REAL(0.7071067811865475244008443621048490) |
|
59 |
|
60 #define dInfinity REAL(1e+14) //REAL(1e+14) |
|
61 |
|
62 #define dEpsilon REAL(2e-5) |
|
63 |
|
64 |
|
65 /* floating point data type, vector, matrix and quaternion types */ |
|
66 |
|
67 #ifdef dSINGLE |
|
68 typedef int64 dReal; |
|
69 #endif |
|
70 |
|
71 /* round an integer up to a multiple of 4, except that 0 and 1 are unmodified |
|
72 * (used to compute matrix leading dimensions) |
|
73 */ |
|
74 #define dPAD(a) (((a) > 1) ? ((((a)-1)|3)+1) : (a)) |
|
75 |
|
76 /* these types are mainly just used in headers */ |
|
77 typedef dReal dVector3[4]; |
|
78 typedef dReal dVector4[4]; |
|
79 typedef dReal dMatrix3[4*3]; |
|
80 typedef dReal dMatrix4[4*4]; |
|
81 typedef dReal dMatrix6[8*6]; |
|
82 typedef dReal dQuaternion[4]; |
|
83 |
|
84 |
|
85 /* precision dependent scalar math functions */ |
|
86 |
|
87 |
|
88 #if defined(dSINGLE) |
|
89 |
|
90 #define dMUL(x,y) ( (dReal)(((x)*(y))>>QFACTOR) ) |
|
91 #define dDIV(x,y) ( (dReal)(((x)<<QFACTOR)/(y)) ) |
|
92 #define REAL(x) ( (dReal)( (x)*(1<<QFACTOR) ) ) |
|
93 #define dFLOAT(x) ( ((float)(x))/((float)(1<<QFACTOR)) ) |
|
94 #define dRecip(x) ( (dReal)(REAL(1/dFLOAT(x))) ) |
|
95 #define dSqrt(x) ( (dReal)( sqrt( (x)<<QFACTOR ) ) ) |
|
96 #define dRecipSqrt(x) ( (dReal)( REAL(1/(sqrt(dFLOAT(x)))) ) ) |
|
97 #define dSin(x) ( (dReal)(sin_table[((x)>>QFACTOR) + ((x)!=0)]) )//sin(x) |
|
98 #define dCos(x) ( (dReal)(sin_table[((x)>>QFACTOR) + ((x)!=0) + 90]) )//cos(x) |
|
99 #define dFabs(x) ( (dReal)(abs(x)) ) |
|
100 |
|
101 #define dIsNan(x) (_isnan(dFLOAT(x))) |
|
102 #define dCopySign(a,b) ( (dReal)(copysign((a),(b))) ) |
|
103 |
|
104 #endif |
|
105 |
|
106 |
|
107 /* utility */ |
|
108 |
|
109 /* round something up to be a multiple of the EFFICIENT_ALIGNMENT */ |
|
110 |
|
111 #define dEFFICIENT_SIZE(x) ((((x)-1)|(EFFICIENT_ALIGNMENT-1))+1) |
|
112 |
|
113 |
|
114 /* alloca aligned to the EFFICIENT_ALIGNMENT. note that this can waste |
|
115 * up to 15 bytes per allocation, depending on what alloca() returns. |
|
116 */ |
|
117 |
|
118 #define dALLOCA16(n) \ |
|
119 ((char*)dEFFICIENT_SIZE(((size_t)(malloc/*alloca*/((n)+(EFFICIENT_ALIGNMENT-1)))))) |
|
120 |
|
121 |
|
122 // Use the error-checking memory allocation system. Because this system uses heap |
|
123 // (malloc) instead of stack (alloca), it is slower. However, it allows you to |
|
124 // simulate larger scenes, as well as handle out-of-memory errors in a somewhat |
|
125 // graceful manner |
|
126 |
|
127 #define dUSE_MALLOC_FOR_ALLOCA |
|
128 |
|
129 #ifdef dUSE_MALLOC_FOR_ALLOCA |
|
130 enum { |
|
131 d_MEMORY_OK = 0, /* no memory errors */ |
|
132 d_MEMORY_OUT_OF_MEMORY /* malloc failed due to out of memory error */ |
|
133 }; |
|
134 |
|
135 #endif |
|
136 |
|
137 |
|
138 |
|
139 /* internal object types (all prefixed with `dx') */ |
|
140 |
|
141 struct dxWorld; /* dynamics world */ |
|
142 struct dxSpace; /* collision space */ |
|
143 struct dxBody; /* rigid body (dynamics object) */ |
|
144 struct dxGeom; /* geometry (collision object) */ |
|
145 struct dxJoint; |
|
146 struct dxJointNode; |
|
147 struct dxJointGroup; |
|
148 |
|
149 typedef struct dxWorld *dWorldID; |
|
150 typedef struct dxSpace *dSpaceID; |
|
151 typedef struct dxBody *dBodyID; |
|
152 typedef struct dxGeom *dGeomID; |
|
153 typedef struct dxJoint *dJointID; |
|
154 typedef struct dxJointGroup *dJointGroupID; |
|
155 |
|
156 |
|
157 /* error numbers */ |
|
158 |
|
159 enum { |
|
160 d_ERR_UNKNOWN = 0, /* unknown error */ |
|
161 d_ERR_IASSERT, /* internal assertion failed */ |
|
162 d_ERR_UASSERT, /* user assertion failed */ |
|
163 d_ERR_LCP /* user assertion failed */ |
|
164 }; |
|
165 |
|
166 |
|
167 /* joint type numbers */ |
|
168 |
|
169 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 }; |
|
184 |
|
185 |
|
186 /* standard joint parameter names. why are these here? - because we don't want |
|
187 * to include all the joint function definitions in joint.cpp. hmmmm. |
|
188 * MSVC complains if we call D_ALL_PARAM_NAMES_X with a blank second argument, |
|
189 * which is why we have the D_ALL_PARAM_NAMES macro as well. please copy and |
|
190 * paste between these two. |
|
191 */ |
|
192 |
|
193 #define D_ALL_PARAM_NAMES(start) \ |
|
194 /* parameters for limits and motors */ \ |
|
195 dParamLoStop = start, \ |
|
196 dParamHiStop, \ |
|
197 dParamVel, \ |
|
198 dParamFMax, \ |
|
199 dParamFudgeFactor, \ |
|
200 dParamBounce, \ |
|
201 dParamCFM, \ |
|
202 dParamStopERP, \ |
|
203 dParamStopCFM, \ |
|
204 /* parameters for suspension */ \ |
|
205 dParamSuspensionERP, \ |
|
206 dParamSuspensionCFM, |
|
207 |
|
208 #define D_ALL_PARAM_NAMES_X(start,x) \ |
|
209 /* parameters for limits and motors */ \ |
|
210 dParamLoStop ## x = start, \ |
|
211 dParamHiStop ## x, \ |
|
212 dParamVel ## x, \ |
|
213 dParamFMax ## x, \ |
|
214 dParamFudgeFactor ## x, \ |
|
215 dParamBounce ## x, \ |
|
216 dParamCFM ## x, \ |
|
217 dParamStopERP ## x, \ |
|
218 dParamStopCFM ## x, \ |
|
219 /* parameters for suspension */ \ |
|
220 dParamSuspensionERP ## x, \ |
|
221 dParamSuspensionCFM ## x, |
|
222 |
|
223 enum { |
|
224 D_ALL_PARAM_NAMES(0) |
|
225 D_ALL_PARAM_NAMES_X(0x100,2) |
|
226 D_ALL_PARAM_NAMES_X(0x200,3) |
|
227 |
|
228 /* add a multiple of this constant to the basic parameter numbers to get |
|
229 * the parameters for the second, third etc axes. |
|
230 */ |
|
231 dParamGroup=0x100 |
|
232 }; |
|
233 |
|
234 |
|
235 /* angular motor mode numbers */ |
|
236 |
|
237 enum{ |
|
238 dAMotorUser = 0, |
|
239 dAMotorEuler = 1 |
|
240 }; |
|
241 |
|
242 |
|
243 /* joint force feedback information */ |
|
244 |
|
245 typedef struct dJointFeedback { |
|
246 dVector3 f1; /* force applied to body 1 */ |
|
247 dVector3 t1; /* torque applied to body 1 */ |
|
248 dVector3 f2; /* force applied to body 2 */ |
|
249 dVector3 t2; /* torque applied to body 2 */ |
|
250 } dJointFeedback; |
|
251 |
|
252 |
|
253 /* private functions that must be implemented by the collision library: |
|
254 * (1) indicate that a geom has moved, (2) get the next geom in a body list. |
|
255 * these functions are called whenever the position of geoms connected to a |
|
256 * body have changed, e.g. with dBodySetPosition(), dBodySetRotation(), or |
|
257 * when the ODE step function updates the body state. |
|
258 */ |
|
259 |
|
260 void dGeomMoved (dGeomID); |
|
261 dGeomID dGeomGetBodyNext (dGeomID); |
|
262 |
|
263 |
|
264 #ifdef __cplusplus |
|
265 } |
|
266 #endif |
|
267 |
|
268 #endif |