Debian Codegraph @

source/unix/odesrc/joints/joint.h

raw
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
23 #ifndef _ODE_JOINT_H_
24 #define _ODE_JOINT_H_
25
26
27 #include <ode/contact.h>
28 #include "../common.h"
29 #include "../objects.h"
30 #include "../obstack.h"
31
32
33 // joint flags
34 enum
35 {
36 // if this flag is set, the joint was allocated in a joint group
37 dJOINT_INGROUP = 1,
38
39 // if this flag is set, the joint was attached with arguments (0,body).
40 // our convention is to treat all attaches as (body,0), i.e. so node[0].body
41 // is always nonzero, so this flag records the fact that the arguments were
42 // swapped.
43 dJOINT_REVERSE = 2,
44
45 // if this flag is set, the joint can not have just one body attached to it,
46 // it must have either zero or two bodies attached.
47 dJOINT_TWOBODIES = 4,
48
49 dJOINT_DISABLED = 8
50 };
51
52
53 enum dJointConnectedBody
54 {
55 dJCB__MIN,
56
57 dJCB_FIRST_BODY = dJCB__MIN,
58 dJCB_SECOND_BODY,
59
60 dJCB__MAX,
61
62 };
63
64 static inline
65 dJointConnectedBody EncodeJointOtherConnectedBody(dJointConnectedBody cbBodyKind)
66 {
67 dIASSERT(dIN_RANGE(cbBodyKind, dJCB__MIN, dJCB__MAX));
68 dSASSERT(dJCB__MAX == 2);
69
70 return (dJointConnectedBody)(dJCB_FIRST_BODY + dJCB_SECOND_BODY - cbBodyKind);
71 }
72
73 /* joint body relativity enumeration */
74 enum dJointBodyRelativity
75 {
76 dJBR__MIN,
77
78 dJBR_GLOBAL = dJBR__MIN,
79
80 dJBR__BODIES_MIN,
81
82 dJBR_BODY1 = dJBR__BODIES_MIN + dJCB_FIRST_BODY,
83 dJBR_BODY2 = dJBR__BODIES_MIN + dJCB_SECOND_BODY,
84
85 dJBR__BODIES_MAX = dJBR__BODIES_MIN + dJCB__MAX,
86
87 dJBR__MAX,
88
89 dJBR__DEFAULT = dJBR_GLOBAL,
90 dJBR__BODIES_COUNT = dJBR__BODIES_MAX - dJBR__BODIES_MIN,
91
92 };
93
94 ODE_PURE_INLINE int dJBREncodeBodyRelativityStatus(int relativity)
95 {
96 return dIN_RANGE(relativity, dJBR__BODIES_MIN, dJBR__BODIES_MAX);
97 }
98
99 ODE_PURE_INLINE dJointBodyRelativity dJBRSwapBodyRelativity(int relativity)
100 {
101 dIASSERT(dIN_RANGE(relativity, dJBR__BODIES_MIN, dJBR__BODIES_MAX));
102 return (dJointBodyRelativity)(dJBR_BODY1 + dJBR_BODY2 - relativity);
103 }
104
105
106
107
108 // there are two of these nodes in the joint, one for each connection to a
109 // body. these are node of a linked list kept by each body of it's connecting
110 // joints. but note that the body pointer in each node points to the body that
111 // makes use of the *other* node, not this node. this trick makes it a bit
112 // easier to traverse the body/joint graph.
113
114 struct dxJointNode
115 {
116 dxJoint *joint; // pointer to enclosing dxJoint object
117 dxBody *body; // *other* body this joint is connected to
118 dxJointNode *next; // next node in body's list of connected joints
119 };
120
121
122 struct dxJoint : public dObject
123 {
124 // naming convention: the "first" body this is connected to is node[0].body,
125 // and the "second" body is node[1].body. if this joint is only connected
126 // to one body then the second body is 0.
127
128 // info returned by getInfo1 function. the constraint dimension is m (<=6).
129 // i.e. that is the total number of rows in the jacobian. `nub' is the
130 // number of unbounded variables (which have lo,hi = -/+ infinity).
131
132 struct Info1
133 {
134 // Structure size should not exceed sizeof(pointer) bytes to have
135 // to have good memory pattern in dxQuickStepper()
136 uint8 m, nub;
137 };
138
139 // info returned by getInfo2 function
140
141 enum
142 {
143 GI2__J_MIN,
144 GI2__JL_MIN = GI2__J_MIN + dDA__L_MIN,
145
146 GI2_JLX = GI2__J_MIN + dDA_LX,
147 GI2_JLY = GI2__J_MIN + dDA_LY,
148 GI2_JLZ = GI2__J_MIN + dDA_LZ,
149
150 GI2__JL_MAX = GI2__J_MIN + dDA__L_MAX,
151
152 GI2__JA_MIN = GI2__J_MIN + dDA__A_MIN,
153
154 GI2_JAX = GI2__J_MIN + dDA_AX,
155 GI2_JAY = GI2__J_MIN + dDA_AY,
156 GI2_JAZ = GI2__J_MIN + dDA_AZ,
157
158 GI2__JA_MAX = GI2__J_MIN + dDA__A_MAX,
159 GI2__J_MAX = GI2__J_MIN + dDA__MAX,
160 };
161
162 enum
163 {
164 GI2_RHS,
165 GI2_CFM,
166 GI2__RHS_CFM_MAX,
167 };
168
169 enum
170 {
171 GI2_LO,
172 GI2_HI,
173 GI2__LO_HI_MAX,
174 };
175
176 // info returned by getSureMaxInfo function.
177 // The information is used for memory reservation in calculations.
178
179 struct SureMaxInfo
180 {
181 // The value of `max_m' must ALWAYS be not less than the value of `m'
182 // the getInfo1 call can generate in current joint state. Another
183 // requirement is that the value should be provided very quickly,
184 // without the excessive calculations.
185 // If it is hard/impossible to quickly predict the maximal value of `m'
186 // (which is the case for most joint types) the maximum for current
187 // joint type in general should be returned. If it can be known the `m'
188 // will be smaller, it can save a bit of memory from being reserved
189 // for calculations if that smaller value is returned.
190
191 uint8 max_m; // Estimate of maximal `m' in Info1
192 };
193
194
195 unsigned flags; // dJOINT_xxx flags
196 dxJointNode node[2]; // connections to bodies. node[1].body can be 0
197 dJointFeedback *feedback; // optional feedback structure
198 dReal lambda[6]; // lambda generated by last step
199
200
201 dxJoint( dxWorld *w );
202 virtual ~dxJoint();
203
204 bool GetIsJointReverse() const { return (this->flags & dJOINT_REVERSE) != 0; }
205
206 virtual void getInfo1( Info1* info ) = 0;
207
208 // integrator parameters
209 virtual void getInfo2(
210 // fps=frames per second (1/stepsize), erp=default error reduction parameter (0..1)
211 dReal worldFPS, dReal worldERP,
212 // elements to jump from one row to the next in J's
213 int rowskip,
214 // for the first and second body, pointers to two (linear and angular)
215 // n*3 jacobian sub matrices, stored by rows. these matrices will have
216 // been initialized to 0 on entry. if the second body is zero then the
217 // J2xx pointers may be 0.
218 dReal *J1, dReal *J2,
219 // elements to jump from one pair of scalars to the next
220 int pairskip,
221 // right hand sides of the equation J*v = c + cfm * lambda. cfm is the
222 // "constraint force mixing" vector. c is set to zero on entry, cfm is
223 // set to a constant value (typically very small or zero) value on entry.
224 dReal *pairRhsCfm,
225 // lo and hi limits for variables (set to -/+ infinity on entry).
226 dReal *pairLoHi,
227 // findex vector for variables. see the LCP solver interface for a
228 // description of what this does. this is set to -1 on entry.
229 // note that the returned indexes are relative to the first index of
230 // the constraint.
231 int *findex) = 0;
232 // This call quickly!!! estimates maximum value of "m" that could be returned by getInfo1()
233 // See comments at definition of SureMaxInfo for details.
234 virtual void getSureMaxInfo( SureMaxInfo* info ) = 0;
235 virtual dJointType type() const = 0;
236 virtual size_t size() const = 0;
237
238 /// Set values which are relative with respect to bodies.
239 /// Each dxJoint should redefine it if needed.
240 virtual void setRelativeValues();
241
242 // Test if this joint should be used in the simulation step
243 // (has the enabled flag set, and is attached to at least one dynamic body)
244 bool isEnabled() const;
245 };
246
247
248 // joint group. NOTE: any joints in the group that have their world destroyed
249 // will have their world pointer set to 0.
250
251 struct dxJointGroup : public dBase
252 {
253 dxJointGroup(): m_num(0), m_stack() {}
254
255 template<class T>
256 T *alloc(dWorldID w)
257 {
258 T *j = (T *)m_stack.alloc(sizeof(T));
259 if (j != NULL) {
260 ++m_num;
261 new(j) T(w);
262 j->flags |= dJOINT_INGROUP;
263 }
264 return j;
265 }
266
267 size_t getJointCount() const { return m_num; }
268 size_t exportJoints(dxJoint **jlist);
269
270 void *beginEnum() { return m_stack.rewind(); }
271 void *continueEnum(size_t num_bytes) { return m_stack.next(num_bytes); }
272
273 void freeAll();
274
275 private:
276 size_t m_num; // number of joints on the stack
277 dObStack m_stack; // a stack of (possibly differently sized) dxJoint objects.
278 };
279
280 // common limit and motor information for a single joint axis of movement
281 struct dxJointLimitMotor
282 {
283 dReal vel, fmax; // powered joint: velocity, max force
284 dReal lostop, histop; // joint limits, relative to initial position
285 dReal fudge_factor; // when powering away from joint limits
286 dReal normal_cfm; // cfm to use when not at a stop
287 dReal stop_erp, stop_cfm; // erp and cfm for when at joint limit
288 dReal bounce; // restitution factor
289 // variables used between getInfo1() and getInfo2()
290 int limit; // 0=free, 1=at lo limit, 2=at hi limit
291 dReal limit_err; // if at limit, amount over limit
292
293 void init( dxWorld * );
294 void set( int num, dReal value );
295 dReal get( int num ) const;
296 bool testRotationalLimit( dReal angle );
297
298 enum
299 {
300 GI2__JL_MIN = dxJoint::GI2__JL_MIN,
301 GI2__JA_MIN = dxJoint::GI2__JA_MIN,
302 GI2_JAX = dxJoint::GI2_JAX,
303 GI2_JAY = dxJoint::GI2_JAY,
304 GI2_JAZ = dxJoint::GI2_JAZ,
305 GI2_RHS = dxJoint::GI2_RHS,
306 GI2_CFM = dxJoint::GI2_CFM,
307 GI2_LO = dxJoint::GI2_LO,
308 GI2_HI = dxJoint::GI2_HI,
309 };
310
311 bool addLimot( dxJoint *joint, dReal fps,
312 dReal *J1, dReal *J2, dReal *pairRhsCfm, dReal *pairLoHi,
313 const dVector3 ax1, int rotational );
314 bool addTwoPointLimot( dxJoint *joint, dReal fps,
315 dReal *J1, dReal *J2, dReal *pairRhsCfm, dReal *pairLoHi,
316 const dVector3 ax1, const dVector3 pt1, const dVector3 pt2 );
317 };
318
319
320 #endif
321
322
323 // Local Variables:
324 // mode:c++
325 // c-basic-offset:4
326 // End: