summaryrefslogtreecommitdiff
path: root/libs/ode-0.16.1/ode/src/rotation.cpp
blob: e813ba37730d83b29cb97e4f0d28d521ed71b908 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*************************************************************************
 *                                                                       *
 * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith.       *
 * All rights reserved.  Email: russ@q12.org   Web: www.q12.org          *
 *                                                                       *
 * This library is free software; you can redistribute it and/or         *
 * modify it under the terms of EITHER:                                  *
 *   (1) The GNU Lesser General Public License as published by the Free  *
 *       Software Foundation; either version 2.1 of the License, or (at  *
 *       your option) any later version. The text of the GNU Lesser      *
 *       General Public License is included with this library in the     *
 *       file LICENSE.TXT.                                               *
 *   (2) The BSD-style license that is included with this library in     *
 *       the file LICENSE-BSD.TXT.                                       *
 *                                                                       *
 * This library is distributed in the hope that it will be useful,       *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files    *
 * LICENSE.TXT and LICENSE-BSD.TXT for more details.                     *
 *                                                                       *
 *************************************************************************/

/*

quaternions have the format: (s,vx,vy,vz) where (vx,vy,vz) is the
"rotation axis" and s is the "rotation angle".

*/

#include <ode/rotation.h>
#include "config.h"
#include "odemath.h"


#define _R(i,j) R[(i)*4+(j)]

#define SET_3x3_IDENTITY \
    _R(0,0) = REAL(1.0); \
    _R(0,1) = REAL(0.0); \
    _R(0,2) = REAL(0.0); \
    _R(0,3) = REAL(0.0); \
    _R(1,0) = REAL(0.0); \
    _R(1,1) = REAL(1.0); \
    _R(1,2) = REAL(0.0); \
    _R(1,3) = REAL(0.0); \
    _R(2,0) = REAL(0.0); \
    _R(2,1) = REAL(0.0); \
    _R(2,2) = REAL(1.0); \
    _R(2,3) = REAL(0.0);


void dRSetIdentity (dMatrix3 R)
{
    dAASSERT (R);
    SET_3x3_IDENTITY;
}


void dRFromAxisAndAngle (dMatrix3 R, dReal ax, dReal ay, dReal az,
                         dReal angle)
{
    dAASSERT (R);
    dQuaternion q;
    dQFromAxisAndAngle (q,ax,ay,az,angle);
    dQtoR (q,R);
}


void dRFromEulerAngles (dMatrix3 R, dReal phi, dReal theta, dReal psi)
{
    dReal sphi,cphi,stheta,ctheta,spsi,cpsi;
    dAASSERT (R);
    sphi = dSin(phi);
    cphi = dCos(phi);
    stheta = dSin(theta);
    ctheta = dCos(theta);
    spsi = dSin(psi);
    cpsi = dCos(psi);
    _R(0,0) = cpsi*ctheta;
    _R(0,1) = spsi*ctheta;
    _R(0,2) =-stheta;
    _R(0,3) = REAL(0.0);
    _R(1,0) = cpsi*stheta*sphi - spsi*cphi;
    _R(1,1) = spsi*stheta*sphi + cpsi*cphi;
    _R(1,2) = ctheta*sphi;
    _R(1,3) = REAL(0.0);
    _R(2,0) = cpsi*stheta*cphi + spsi*sphi;
    _R(2,1) = spsi*stheta*cphi - cpsi*sphi;
    _R(2,2) = ctheta*cphi;
    _R(2,3) = REAL(0.0);
}


void dRFrom2Axes (dMatrix3 R, dReal ax, dReal ay, dReal az,
                  dReal bx, dReal by, dReal bz)
{
    dReal l,k;
    dAASSERT (R);
    l = dSqrt (ax*ax + ay*ay + az*az);
    if (l <= REAL(0.0)) {
        dDEBUGMSG ("zero length vector");
        return;
    }
    l = dRecip(l);
    ax *= l;
    ay *= l;
    az *= l;
    k = ax*bx + ay*by + az*bz;
    bx -= k*ax;
    by -= k*ay;
    bz -= k*az;
    l = dSqrt (bx*bx + by*by + bz*bz);
    if (l <= REAL(0.0)) {
        dDEBUGMSG ("zero length vector");
        return;
    }
    l = dRecip(l);
    bx *= l;
    by *= l;
    bz *= l;
    _R(0,0) = ax;
    _R(1,0) = ay;
    _R(2,0) = az;
    _R(0,1) = bx;
    _R(1,1) = by;
    _R(2,1) = bz;
    _R(0,2) = - by*az + ay*bz;
    _R(1,2) = - bz*ax + az*bx;
    _R(2,2) = - bx*ay + ax*by;
    _R(0,3) = REAL(0.0);
    _R(1,3) = REAL(0.0);
    _R(2,3) = REAL(0.0);
}


void dRFromZAxis (dMatrix3 R, dReal ax, dReal ay, dReal az)
{
    dVector3 n,p,q;
    n[0] = ax;
    n[1] = ay;
    n[2] = az;
    dNormalize3 (n);
    dPlaneSpace (n,p,q);
    _R(0,0) = p[0];
    _R(1,0) = p[1];
    _R(2,0) = p[2];
    _R(0,1) = q[0];
    _R(1,1) = q[1];
    _R(2,1) = q[2];
    _R(0,2) = n[0];
    _R(1,2) = n[1];
    _R(2,2) = n[2];
    _R(0,3) = REAL(0.0);
    _R(1,3) = REAL(0.0);
    _R(2,3) = REAL(0.0);
}


void dQSetIdentity (dQuaternion q)
{
    dAASSERT (q);
    q[0] = 1;
    q[1] = 0;
    q[2] = 0;
    q[3] = 0;
}


void dQFromAxisAndAngle (dQuaternion q, dReal ax, dReal ay, dReal az,
                         dReal angle)
{
    dAASSERT (q);
    dReal l = ax*ax + ay*ay + az*az;
    if (l > REAL(0.0)) {
        angle *= REAL(0.5);
        q[0] = dCos (angle);
        l = dSin(angle) * dRecipSqrt(l);
        q[1] = ax*l;
        q[2] = ay*l;
        q[3] = az*l;
    }
    else {
        q[0] = 1;
        q[1] = 0;
        q[2] = 0;
        q[3] = 0;
    }
}


void dQMultiply0 (dQuaternion qa, const dQuaternion qb, const dQuaternion qc)
{
    dAASSERT (qa && qb && qc);
    qa[0] = qb[0]*qc[0] - qb[1]*qc[1] - qb[2]*qc[2] - qb[3]*qc[3];
    qa[1] = qb[0]*qc[1] + qb[1]*qc[0] + qb[2]*qc[3] - qb[3]*qc[2];
    qa[2] = qb[0]*qc[2] + qb[2]*qc[0] + qb[3]*qc[1] - qb[1]*qc[3];
    qa[3] = qb[0]*qc[3] + qb[3]*qc[0] + qb[1]*qc[2] - qb[2]*qc[1];
}


void dQMultiply1 (dQuaternion qa, const dQuaternion qb, const dQuaternion qc)
{
    dAASSERT (qa && qb && qc);
    qa[0] = qb[0]*qc[0] + qb[1]*qc[1] + qb[2]*qc[2] + qb[3]*qc[3];
    qa[1] = qb[0]*qc[1] - qb[1]*qc[0] - qb[2]*qc[3] + qb[3]*qc[2];
    qa[2] = qb[0]*qc[2] - qb[2]*qc[0] - qb[3]*qc[1] + qb[1]*qc[3];
    qa[3] = qb[0]*qc[3] - qb[3]*qc[0] - qb[1]*qc[2] + qb[2]*qc[1];
}


void dQMultiply2 (dQuaternion qa, const dQuaternion qb, const dQuaternion qc)
{
    dAASSERT (qa && qb && qc);
    qa[0] =  qb[0]*qc[0] + qb[1]*qc[1] + qb[2]*qc[2] + qb[3]*qc[3];
    qa[1] = -qb[0]*qc[1] + qb[1]*qc[0] - qb[2]*qc[3] + qb[3]*qc[2];
    qa[2] = -qb[0]*qc[2] + qb[2]*qc[0] - qb[3]*qc[1] + qb[1]*qc[3];
    qa[3] = -qb[0]*qc[3] + qb[3]*qc[0] - qb[1]*qc[2] + qb[2]*qc[1];
}


void dQMultiply3 (dQuaternion qa, const dQuaternion qb, const dQuaternion qc)
{
    dAASSERT (qa && qb && qc);
    qa[0] =  qb[0]*qc[0] - qb[1]*qc[1] - qb[2]*qc[2] - qb[3]*qc[3];
    qa[1] = -qb[0]*qc[1] - qb[1]*qc[0] + qb[2]*qc[3] - qb[3]*qc[2];
    qa[2] = -qb[0]*qc[2] - qb[2]*qc[0] + qb[3]*qc[1] - qb[1]*qc[3];
    qa[3] = -qb[0]*qc[3] - qb[3]*qc[0] + qb[1]*qc[2] - qb[2]*qc[1];
}


// dRfromQ(), dQfromR() and dDQfromW() are derived from equations in "An Introduction
// to Physically Based Modeling: Rigid Body Simulation - 1: Unconstrained
// Rigid Body Dynamics" by David Baraff, Robotics Institute, Carnegie Mellon
// University, 1997.

void dRfromQ (dMatrix3 R, const dQuaternion q)
{
    dAASSERT (q && R);
    // q = (s,vx,vy,vz)
    dReal qq1 = 2*q[1]*q[1];
    dReal qq2 = 2*q[2]*q[2];
    dReal qq3 = 2*q[3]*q[3];
    _R(0,0) = 1 - qq2 - qq3;
    _R(0,1) = 2*(q[1]*q[2] - q[0]*q[3]);
    _R(0,2) = 2*(q[1]*q[3] + q[0]*q[2]);
    _R(0,3) = REAL(0.0);
    _R(1,0) = 2*(q[1]*q[2] + q[0]*q[3]);
    _R(1,1) = 1 - qq1 - qq3;
    _R(1,2) = 2*(q[2]*q[3] - q[0]*q[1]);
    _R(1,3) = REAL(0.0);
    _R(2,0) = 2*(q[1]*q[3] - q[0]*q[2]);
    _R(2,1) = 2*(q[2]*q[3] + q[0]*q[1]);
    _R(2,2) = 1 - qq1 - qq2;
    _R(2,3) = REAL(0.0);
}


void dQfromR (dQuaternion q, const dMatrix3 R)
{
    dAASSERT (q && R);
    dReal tr,s;
    tr = _R(0,0) + _R(1,1) + _R(2,2);
    if (tr >= 0) {
        s = dSqrt (tr + 1);
        q[0] = REAL(0.5) * s;
        s = REAL(0.5) * dRecip(s);
        q[1] = (_R(2,1) - _R(1,2)) * s;
        q[2] = (_R(0,2) - _R(2,0)) * s;
        q[3] = (_R(1,0) - _R(0,1)) * s;
    }
    else {
        // find the largest diagonal element and jump to the appropriate case
        if (_R(1,1) > _R(0,0)) {
            if (_R(2,2) > _R(1,1)) goto case_2;
            goto case_1;
        }
        if (_R(2,2) > _R(0,0)) goto case_2;
        goto case_0;

case_0:
        s = dSqrt((_R(0,0) - (_R(1,1) + _R(2,2))) + 1);
        q[1] = REAL(0.5) * s;
        s = REAL(0.5) * dRecip(s);
        q[2] = (_R(0,1) + _R(1,0)) * s;
        q[3] = (_R(2,0) + _R(0,2)) * s;
        q[0] = (_R(2,1) - _R(1,2)) * s;
        return;

case_1:
        s = dSqrt((_R(1,1) - (_R(2,2) + _R(0,0))) + 1);
        q[2] = REAL(0.5) * s;
        s = REAL(0.5) * dRecip(s);
        q[3] = (_R(1,2) + _R(2,1)) * s;
        q[1] = (_R(0,1) + _R(1,0)) * s;
        q[0] = (_R(0,2) - _R(2,0)) * s;
        return;

case_2:
        s = dSqrt((_R(2,2) - (_R(0,0) + _R(1,1))) + 1);
        q[3] = REAL(0.5) * s;
        s = REAL(0.5) * dRecip(s);
        q[1] = (_R(2,0) + _R(0,2)) * s;
        q[2] = (_R(1,2) + _R(2,1)) * s;
        q[0] = (_R(1,0) - _R(0,1)) * s;
        return;
    }
}


void dDQfromW (dReal dq[4], const dVector3 w, const dQuaternion q)
{
    dAASSERT (w && q && dq);
    dq[0] = REAL(0.5)*(- w[0]*q[1] - w[1]*q[2] - w[2]*q[3]);
    dq[1] = REAL(0.5)*(  w[0]*q[0] + w[1]*q[3] - w[2]*q[2]);
    dq[2] = REAL(0.5)*(- w[0]*q[3] + w[1]*q[0] + w[2]*q[1]);
    dq[3] = REAL(0.5)*(  w[0]*q[2] - w[1]*q[1] + w[2]*q[0]);
}