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
|
/*************************************************************************
* *
* 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. *
* *
*************************************************************************/
#include <ode/common.h>
#include "config.h"
#include "odemath.h"
#undef dSafeNormalize3
#undef dSafeNormalize4
#undef dNormalize3
#undef dNormalize4
#undef dPlaneSpace
#undef dOrthogonalizeR
int dSafeNormalize3 (dVector3 a)
{
return dxSafeNormalize3(a);
}
int dSafeNormalize4 (dVector4 a)
{
return dxSafeNormalize4(a);
}
void dNormalize3(dVector3 a)
{
dxNormalize3(a);
}
void dNormalize4(dVector4 a)
{
dxNormalize4(a);
}
void dPlaneSpace(const dVector3 n, dVector3 p, dVector3 q)
{
return dxPlaneSpace(n, p, q);
}
int dOrthogonalizeR(dMatrix3 m)
{
return dxOrthogonalizeR(m);
}
/*extern */
bool dxCouldBeNormalized3(const dVector3 a)
{
dAASSERT (a);
bool ret = false;
for (unsigned axis = dV3E__AXES_MIN; axis != dV3E__AXES_MAX; ++axis) {
if (a[axis] != REAL(0.0)) {
ret = true;
break;
}
}
return ret;
}
// this may be called for vectors `a' with extremely small magnitude, for
// example the result of a cross product on two nearly perpendicular vectors.
// we must be robust to these small vectors. to prevent numerical error,
// first find the component a[i] with the largest magnitude and then scale
// all the components by 1/a[i]. then we can compute the length of `a' and
// scale the components by 1/l. this has been verified to work with vectors
// containing the smallest representable numbers.
/*extern */
bool dxSafeNormalize3 (dVector3 a)
{
dAASSERT (a);
bool ret = false;
do {
dReal abs_a0 = dFabs(a[dV3E_X]);
dReal abs_a1 = dFabs(a[dV3E_Y]);
dReal abs_a2 = dFabs(a[dV3E_Z]);
dVec3Element idx;
if (abs_a1 > abs_a0) {
if (abs_a2 > abs_a1) { // abs_a2 is the largest
idx = dV3E_Z;
}
else { // abs_a1 is the largest
idx = dV3E_Y;
}
}
else if (abs_a2 > abs_a0) {// abs_a2 is the largest
idx = dV3E_Z;
}
else { // aa[0] might be the largest
if (!(abs_a0 > REAL(0.0))) {
// if all a's are zero, this is where we'll end up.
// return the vector unchanged.
break;
}
// abs_a0 is the largest
idx = dV3E_X;
}
if (idx == dV3E_X) {
dReal aa0_recip = dRecip(abs_a0);
dReal a1 = a[dV3E_Y] * aa0_recip;
dReal a2 = a[dV3E_Z] * aa0_recip;
dReal l = dRecipSqrt(REAL(1.0) + a1 * a1 + a2 * a2);
a[dV3E_Y] = a1 * l;
a[dV3E_Z] = a2 * l;
a[dV3E_X] = dCopySign(l, a[dV3E_X]);
}
else if (idx == dV3E_Y) {
dReal aa1_recip = dRecip(abs_a1);
dReal a0 = a[dV3E_X] * aa1_recip;
dReal a2 = a[dV3E_Z] * aa1_recip;
dReal l = dRecipSqrt(REAL(1.0) + a0 * a0 + a2 * a2);
a[dV3E_X] = a0 * l;
a[dV3E_Z] = a2 * l;
a[dV3E_Y] = dCopySign(l, a[dV3E_Y]);
}
else {
dReal aa2_recip = dRecip(abs_a2);
dReal a0 = a[dV3E_X] * aa2_recip;
dReal a1 = a[dV3E_Y] * aa2_recip;
dReal l = dRecipSqrt(REAL(1.0) + a0 * a0 + a1 * a1);
a[dV3E_X] = a0 * l;
a[dV3E_Y] = a1 * l;
a[dV3E_Z] = dCopySign(l, a[dV3E_Z]);
}
ret = true;
}
while (false);
return ret;
}
/* OLD VERSION */
/*
void dNormalize3 (dVector3 a)
{
dIASSERT (a);
dReal l = dCalcVectorDot3(a,a);
if (l > 0) {
l = dRecipSqrt(l);
a[0] *= l;
a[1] *= l;
a[2] *= l;
}
else {
a[0] = 1;
a[1] = 0;
a[2] = 0;
}
}
*/
/*extern */
bool dxCouldBeNormalized4(const dVector4 a)
{
dAASSERT (a);
bool ret = false;
for (unsigned axis = dV4E__MIN; axis != dV4E__MAX; ++axis) {
if (a[axis] != REAL(0.0)) {
ret = true;
break;
}
}
return ret;
}
/*extern */
bool dxSafeNormalize4 (dVector4 a)
{
dAASSERT (a);
bool ret = false;
dReal l = a[dV4E_X] * a[dV4E_X] + a[dV4E_Y] * a[dV4E_Y] + a[dV4E_Z] * a[dV4E_Z] + a[dV4E_O] * a[dV4E_O];
if (l > 0) {
l = dRecipSqrt(l);
a[dV4E_X] *= l;
a[dV4E_Y] *= l;
a[dV4E_Z] *= l;
a[dV4E_O] *= l;
ret = true;
}
return ret;
}
void dxPlaneSpace (const dVector3 n, dVector3 p, dVector3 q)
{
dAASSERT (n && p && q);
if (dFabs(n[2]) > M_SQRT1_2) {
// choose p in y-z plane
dReal a = n[1]*n[1] + n[2]*n[2];
dReal k = dRecipSqrt (a);
p[0] = 0;
p[1] = -n[2]*k;
p[2] = n[1]*k;
// set q = n x p
q[0] = a*k;
q[1] = -n[0]*p[2];
q[2] = n[0]*p[1];
}
else {
// choose p in x-y plane
dReal a = n[0]*n[0] + n[1]*n[1];
dReal k = dRecipSqrt (a);
p[0] = -n[1]*k;
p[1] = n[0]*k;
p[2] = 0;
// set q = n x p
q[0] = -n[2]*p[1];
q[1] = n[2]*p[0];
q[2] = a*k;
}
}
/*
* This takes what is supposed to be a rotation matrix,
* and make sure it is correct.
* Note: this operates on rows, not columns, because for rotations
* both ways give equivalent results.
*/
bool dxOrthogonalizeR(dMatrix3 m)
{
bool ret = false;
do {
if (!dxCouldBeNormalized3(m + dM3E__X_MIN)) {
break;
}
dReal n0 = dCalcVectorLengthSquare3(m + dM3E__X_MIN);
dVector3 row2_store;
dReal *row2 = m + dM3E__Y_MIN;
// project row[0] on row[1], should be zero
dReal proj = dCalcVectorDot3(m + dM3E__X_MIN, m + dM3E__Y_MIN);
if (proj != 0) {
// Gram-Schmidt step on row[1]
dReal proj_div_n0 = proj / n0;
row2_store[dV3E_X] = m[dM3E__Y_MIN + dV3E_X] - proj_div_n0 * m[dM3E__X_MIN + dV3E_X] ;
row2_store[dV3E_Y] = m[dM3E__Y_MIN + dV3E_Y] - proj_div_n0 * m[dM3E__X_MIN + dV3E_Y];
row2_store[dV3E_Z] = m[dM3E__Y_MIN + dV3E_Z] - proj_div_n0 * m[dM3E__X_MIN + dV3E_Z];
row2 = row2_store;
}
if (!dxCouldBeNormalized3(row2)) {
break;
}
if (n0 != REAL(1.0)) {
bool row0_norm_fault = !dxSafeNormalize3(m + dM3E__X_MIN);
dIVERIFY(!row0_norm_fault);
}
dReal n1 = dCalcVectorLengthSquare3(row2);
if (n1 != REAL(1.0)) {
bool row1_norm_fault = !dxSafeNormalize3(row2);
dICHECK(!row1_norm_fault);
}
dIASSERT(dFabs(dCalcVectorDot3(m + dM3E__X_MIN, row2)) < 1e-6);
/* just overwrite row[2], this makes sure the matrix is not
a reflection */
dCalcVectorCross3(m + dM3E__Z_MIN, m + dM3E__X_MIN, row2);
m[dM3E_XPAD] = m[dM3E_YPAD] = m[dM3E_ZPAD] = 0;
ret = true;
}
while (false);
return ret;
}
|