Debian Codegraph @

source/unix/odesrc/joints/slider.cpp

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
24 #include <ode/odeconfig.h>
25 #include "config.h"
26 #include "slider.h"
27 #include "joint_internal.h"
28
29
30
31 //****************************************************************************
32 // slider
33
34 dxJointSlider::dxJointSlider ( dxWorld *w ) :
35 dxJoint ( w )
36 {
37 dSetZero ( axis1, 4 );
38 axis1[0] = 1;
39 dSetZero ( qrel, 4 );
40 dSetZero ( offset, 4 );
41 limot.init ( world );
42 }
43
44
45 dReal dJointGetSliderPosition ( dJointID j )
46 {
47 dxJointSlider* joint = ( dxJointSlider* ) j;
48 dUASSERT ( joint, "bad joint argument" );
49 checktype ( joint, Slider );
50
51 // get axis1 in global coordinates
52 dVector3 ax1, q;
53 dMultiply0_331 ( ax1, joint->node[0].body->posr.R, joint->axis1 );
54
55 if ( joint->node[1].body )
56 {
57 // get body2 + offset point in global coordinates
58 dMultiply0_331 ( q, joint->node[1].body->posr.R, joint->offset );
59 for ( int i = 0; i < 3; i++ )
60 q[i] = joint->node[0].body->posr.pos[i]
61 - q[i]
62 - joint->node[1].body->posr.pos[i];
63 }
64 else
65 {
66 q[0] = joint->node[0].body->posr.pos[0] - joint->offset[0];
67 q[1] = joint->node[0].body->posr.pos[1] - joint->offset[1];
68 q[2] = joint->node[0].body->posr.pos[2] - joint->offset[2];
69
70 if ( joint->flags & dJOINT_REVERSE )
71 {
72 // N.B. it could have been simplier to only inverse the sign of
73 // the dCalcVectorDot3 result but this case is exceptional and doing
74 // the check for all case can decrease the performance.
75 ax1[0] = -ax1[0];
76 ax1[1] = -ax1[1];
77 ax1[2] = -ax1[2];
78 }
79 }
80
81 return dCalcVectorDot3 ( ax1, q );
82 }
83
84
85 dReal dJointGetSliderPositionRate ( dJointID j )
86 {
87 dxJointSlider* joint = ( dxJointSlider* ) j;
88 dUASSERT ( joint, "bad joint argument" );
89 checktype ( joint, Slider );
90
91 // get axis1 in global coordinates
92 dVector3 ax1;
93 dMultiply0_331 ( ax1, joint->node[0].body->posr.R, joint->axis1 );
94
95 if ( joint->node[1].body )
96 {
97 return dCalcVectorDot3 ( ax1, joint->node[0].body->lvel ) -
98 dCalcVectorDot3 ( ax1, joint->node[1].body->lvel );
99 }
100 else
101 {
102 dReal rate = dCalcVectorDot3 ( ax1, joint->node[0].body->lvel );
103 if ( joint->flags & dJOINT_REVERSE ) rate = - rate;
104 return rate;
105 }
106 }
107
108
109 void
110 dxJointSlider::getSureMaxInfo( SureMaxInfo* info )
111 {
112 info->max_m = 6;
113 }
114
115
116 void
117 dxJointSlider::getInfo1 ( dxJoint::Info1 *info )
118 {
119 info->nub = 5;
120
121 // see if joint is powered
122 if ( limot.fmax > 0 )
123 info->m = 6; // powered slider needs an extra constraint row
124 else info->m = 5;
125
126 // see if we're at a joint limit.
127 limot.limit = 0;
128 if ( ( limot.lostop > -dInfinity || limot.histop < dInfinity ) &&
129 limot.lostop <= limot.histop )
130 {
131 // measure joint position
132 dReal pos = dJointGetSliderPosition ( this );
133 if ( pos <= limot.lostop )
134 {
135 limot.limit = 1;
136 limot.limit_err = pos - limot.lostop;
137 info->m = 6;
138 }
139 else if ( pos >= limot.histop )
140 {
141 limot.limit = 2;
142 limot.limit_err = pos - limot.histop;
143 info->m = 6;
144 }
145 }
146 }
147
148
149 void
150 dxJointSlider::getInfo2 ( dReal worldFPS, dReal worldERP,
151 int rowskip, dReal *J1, dReal *J2,
152 int pairskip, dReal *pairRhsCfm, dReal *pairLoHi,
153 int *findex )
154 {
155 // 3 rows to make body rotations equal
156 setFixedOrientation ( this, worldFPS, worldERP, rowskip, J1, J2, pairskip, pairRhsCfm, qrel );
157
158 // pull out pos and R for both bodies. also get the `connection'
159 // vector pos2-pos1.
160 dVector3 c;
161 dReal *pos2 = NULL, *R2 = NULL;
162
163 dReal *pos1 = node[0].body->posr.pos;
164 dReal *R1 = node[0].body->posr.R;
165
166 dVector3 ax1; // joint axis in global coordinates (unit length)
167 dVector3 p, q; // plane space of ax1
168 dMultiply0_331 ( ax1, R1, axis1 );
169 dPlaneSpace ( ax1, p, q );
170
171 dxBody *body1 = node[1].body;
172
173 if ( body1 )
174 {
175 R2 = body1->posr.R;
176 pos2 = body1->posr.pos;
177 dSubtractVectors3( c, pos2, pos1 );
178 }
179
180 // remaining two rows. we want: vel2 = vel1 + w1 x c ... but this would
181 // result in three equations, so we project along the planespace vectors
182 // so that sliding along the slider axis is disregarded. for symmetry we
183 // also substitute (w1+w2)/2 for w1, as w1 is supposed to equal w2.
184 int currRowSkip = 3 * rowskip, currPairSkip = 3 * pairskip;
185 {
186 dCopyVector3( J1 + currRowSkip + GI2__JL_MIN, p );
187
188 if ( body1 )
189 {
190 dVector3 tmp;
191
192 dCopyNegatedVector3(J2 + currRowSkip + GI2__JL_MIN, p);
193
194 dCalcVectorCross3( tmp, c, p );
195 dCopyScaledVector3( J1 + currRowSkip + GI2__JA_MIN, tmp, REAL(0.5) );
196 dCopyVector3( J2 + currRowSkip + GI2__JA_MIN, J1 + currRowSkip + GI2__JA_MIN );
197 }
198 }
199
200 currRowSkip += rowskip;
201 {
202 dCopyVector3( J1 + currRowSkip + GI2__JL_MIN, q );
203
204 if ( body1 )
205 {
206 dVector3 tmp;
207
208 dCopyNegatedVector3(J2 + currRowSkip + GI2__JL_MIN, q);
209
210 dCalcVectorCross3( tmp, c, q );
211 dCopyScaledVector3( J1 + currRowSkip + GI2__JA_MIN, tmp, REAL(0.5) );
212 dCopyVector3( J2 + currRowSkip + GI2__JA_MIN, J1 + currRowSkip + GI2__JA_MIN );
213 }
214 }
215
216 // compute last two elements of right hand side. we want to align the offset
217 // point (in body 2's frame) with the center of body 1.
218 dReal k = worldFPS * worldERP;
219
220 if ( body1 )
221 {
222 dVector3 ofs; // offset point in global coordinates
223 dMultiply0_331 ( ofs, R2, offset );
224 dAddVectors3(c, c, ofs);
225
226 pairRhsCfm[currPairSkip + GI2_RHS] = k * dCalcVectorDot3 ( p, c );
227
228 currPairSkip += pairskip;
229 pairRhsCfm[currPairSkip + GI2_RHS] = k * dCalcVectorDot3 ( q, c );
230 }
231 else
232 {
233 dVector3 ofs; // offset point in global coordinates
234 dSubtractVectors3(ofs, offset, pos1);
235
236 pairRhsCfm[currPairSkip + GI2_RHS] = k * dCalcVectorDot3 ( p, ofs );
237
238 currPairSkip += pairskip;
239 pairRhsCfm[currPairSkip + GI2_RHS] = k * dCalcVectorDot3 ( q, ofs );
240
241 if ( (flags & dJOINT_REVERSE) != 0 )
242 {
243 dNegateVector3(ax1);
244 }
245 }
246
247 // if the slider is powered, or has joint limits, add in the extra row
248 currRowSkip += rowskip; currPairSkip += pairskip;
249 limot.addLimot ( this, worldFPS, J1 + currRowSkip, J2 + currRowSkip, pairRhsCfm + currPairSkip, pairLoHi + currPairSkip, ax1, 0 );
250 }
251
252
253 void dJointSetSliderAxis ( dJointID j, dReal x, dReal y, dReal z )
254 {
255 dxJointSlider* joint = ( dxJointSlider* ) j;
256 dUASSERT ( joint, "bad joint argument" );
257 checktype ( joint, Slider );
258 setAxes ( joint, x, y, z, joint->axis1, 0 );
259
260 joint->computeOffset();
261
262 joint->computeInitialRelativeRotation();
263 }
264
265
266 void dJointSetSliderAxisDelta ( dJointID j, dReal x, dReal y, dReal z, dReal dx, dReal dy, dReal dz )
267 {
268 dxJointSlider* joint = ( dxJointSlider* ) j;
269 dUASSERT ( joint, "bad joint argument" );
270 checktype ( joint, Slider );
271 setAxes ( joint, x, y, z, joint->axis1, 0 );
272
273 joint->computeOffset();
274
275 // compute initial relative rotation body1 -> body2, or env -> body1
276 // also compute center of body1 w.r.t body 2
277 if ( !(joint->node[1].body) )
278 {
279 joint->offset[0] += dx;
280 joint->offset[1] += dy;
281 joint->offset[2] += dz;
282 }
283
284 joint->computeInitialRelativeRotation();
285 }
286
287
288
289 void dJointGetSliderAxis ( dJointID j, dVector3 result )
290 {
291 dxJointSlider* joint = ( dxJointSlider* ) j;
292 dUASSERT ( joint, "bad joint argument" );
293 dUASSERT ( result, "bad result argument" );
294 checktype ( joint, Slider );
295 getAxis ( joint, result, joint->axis1 );
296 }
297
298
299 void dJointSetSliderParam ( dJointID j, int parameter, dReal value )
300 {
301 dxJointSlider* joint = ( dxJointSlider* ) j;
302 dUASSERT ( joint, "bad joint argument" );
303 checktype ( joint, Slider );
304 joint->limot.set ( parameter, value );
305 }
306
307
308 dReal dJointGetSliderParam ( dJointID j, int parameter )
309 {
310 dxJointSlider* joint = ( dxJointSlider* ) j;
311 dUASSERT ( joint, "bad joint argument" );
312 checktype ( joint, Slider );
313 return joint->limot.get ( parameter );
314 }
315
316
317 void dJointAddSliderForce ( dJointID j, dReal force )
318 {
319 dxJointSlider* joint = ( dxJointSlider* ) j;
320 dVector3 axis;
321 dUASSERT ( joint, "bad joint argument" );
322 checktype ( joint, Slider );
323
324 if ( joint->flags & dJOINT_REVERSE )
325 force = -force;
326
327 getAxis ( joint, axis, joint->axis1 );
328 axis[0] *= force;
329 axis[1] *= force;
330 axis[2] *= force;
331
332 if ( joint->node[0].body != 0 )
333 dBodyAddForce ( joint->node[0].body, axis[0], axis[1], axis[2] );
334 if ( joint->node[1].body != 0 )
335 dBodyAddForce ( joint->node[1].body, -axis[0], -axis[1], -axis[2] );
336
337 if ( joint->node[0].body != 0 && joint->node[1].body != 0 )
338 {
339 // linear torque decoupling:
340 // we have to compensate the torque, that this slider force may generate
341 // if body centers are not aligned along the slider axis
342
343 dVector3 ltd; // Linear Torque Decoupling vector (a torque)
344
345 dVector3 c;
346 c[0] = REAL ( 0.5 ) * ( joint->node[1].body->posr.pos[0] - joint->node[0].body->posr.pos[0] );
347 c[1] = REAL ( 0.5 ) * ( joint->node[1].body->posr.pos[1] - joint->node[0].body->posr.pos[1] );
348 c[2] = REAL ( 0.5 ) * ( joint->node[1].body->posr.pos[2] - joint->node[0].body->posr.pos[2] );
349 dCalcVectorCross3( ltd, c, axis );
350
351 dBodyAddTorque ( joint->node[0].body, ltd[0], ltd[1], ltd[2] );
352 dBodyAddTorque ( joint->node[1].body, ltd[0], ltd[1], ltd[2] );
353 }
354 }
355
356
357 dJointType
358 dxJointSlider::type() const
359 {
360 return dJointTypeSlider;
361 }
362
363
364 size_t
365 dxJointSlider::size() const
366 {
367 return sizeof ( *this );
368 }
369
370
371 void
372 dxJointSlider::setRelativeValues()
373 {
374 computeOffset();
375 computeInitialRelativeRotation();
376 }
377
378
379
380 /// Compute initial relative rotation body1 -> body2, or env -> body1
381 void
382 dxJointSlider::computeInitialRelativeRotation()
383 {
384 if ( node[0].body )
385 {
386 // compute initial relative rotation body1 -> body2, or env -> body1
387 // also compute center of body1 w.r.t body 2
388 if ( node[1].body )
389 {
390 dQMultiply1 ( qrel, node[0].body->q, node[1].body->q );
391 }
392 else
393 {
394 // set qrel to the transpose of the first body's q
395 qrel[0] = node[0].body->q[0];
396 qrel[1] = -node[0].body->q[1];
397 qrel[2] = -node[0].body->q[2];
398 qrel[3] = -node[0].body->q[3];
399 }
400 }
401 }
402
403
404 /// Compute center of body1 w.r.t body 2
405 void
406 dxJointSlider::computeOffset()
407 {
408 if ( node[1].body )
409 {
410 dVector3 c;
411 c[0] = node[0].body->posr.pos[0] - node[1].body->posr.pos[0];
412 c[1] = node[0].body->posr.pos[1] - node[1].body->posr.pos[1];
413 c[2] = node[0].body->posr.pos[2] - node[1].body->posr.pos[2];
414
415 dMultiply1_331 ( offset, node[1].body->posr.R, c );
416 }
417 else if ( node[0].body )
418 {
419 offset[0] = node[0].body->posr.pos[0];
420 offset[1] = node[0].body->posr.pos[1];
421 offset[2] = node[0].body->posr.pos[2];
422 }
423 }