1 /*************************************************************************
3 * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith. *
4 * All rights reserved. Email: russ@q12.org Web: www.q12.org *
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 *
13 * (2) The BSD-style license that is included with this library in *
14 * the file LICENSE-BSD.TXT. *
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. *
21 *************************************************************************/
23 #include <ode/common.h>
24 #include <ode/odemath.h>
26 // get some math functions under windows
29 #ifndef CYGWIN // added by andy for cygwin
31 #define copysign(a,b) ((dReal)_copysign(a,b))
32 #endif // added by andy for cygwin
35 #undef dSafeNormalize3
36 #undef dSafeNormalize4
41 // this may be called for vectors `a' with extremely small magnitude, for
42 // example the result of a cross product on two nearly perpendicular vectors.
43 // we must be robust to these small vectors. to prevent numerical error,
44 // first find the component a[i] with the largest magnitude and then scale
45 // all the components by 1/a[i]. then we can compute the length of `a' and
46 // scale the components by 1/l. this has been verified to work with vectors
47 // containing the smallest representable numbers.
49 int _dSafeNormalize3 (dVector3 a
)
60 if (aa
[2] > aa
[1]) { // aa[2] is largest
63 else { // aa[1] is largest
68 if (aa
[2] > aa
[0]) {// aa[2] is largest
71 else { // aa[0] might be the largest
72 if (aa
[0] <= 0) { // aa[0] might is largest
73 a
[0] = 1; // if all a's are zero, this is where we'll end up.
74 a
[1] = 0; // return a default unit length vector.
87 l
= dRecipSqrt (a
[0]*a
[0] + a
[1]*a
[1] + a
[2]*a
[2]);
97 void dNormalize3 (dVector3 a)
115 int dSafeNormalize3 (dVector3 a
)
117 return _dSafeNormalize3(a
);
120 void dNormalize3(dVector3 a
)
126 int _dSafeNormalize4 (dVector4 a
)
129 dReal l
= dDOT(a
,a
)+a
[3]*a
[3];
147 int dSafeNormalize4 (dVector4 a
)
149 return _dSafeNormalize4(a
);
152 void dNormalize4(dVector4 a
)
158 void dPlaneSpace (const dVector3 n
, dVector3 p
, dVector3 q
)
160 dAASSERT (n
&& p
&& q
);
161 if (dFabs(n
[2]) > M_SQRT1_2
) {
162 // choose p in y-z plane
163 dReal a
= n
[1]*n
[1] + n
[2]*n
[2];
164 dReal k
= dRecipSqrt (a
);
174 // choose p in x-y plane
175 dReal a
= n
[0]*n
[0] + n
[1]*n
[1];
176 dReal k
= dRecipSqrt (a
);