Bugfix #4081: support for OpenBSD platform for scons. Big thanks to Nathan Houghton...
[blender-staging.git] / extern / bullet / LinearMath / SimdTransformUtil.h
1 /*
2 Copyright (c) 2003-2006 Gino van den Bergen / Erwin Coumans  http://continuousphysics.com/Bullet/
3
4 This software is provided 'as-is', without any express or implied warranty.
5 In no event will the authors be held liable for any damages arising from the use of this software.
6 Permission is granted to anyone to use this software for any purpose, 
7 including commercial applications, and to alter it and redistribute it freely, 
8 subject to the following restrictions:
9
10 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
11 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
12 3. This notice may not be removed or altered from any source distribution.
13 */
14
15
16 #ifndef SIMD_TRANSFORM_UTIL_H
17 #define SIMD_TRANSFORM_UTIL_H
18
19 #include "SimdTransform.h"
20 #define ANGULAR_MOTION_TRESHOLD 0.5f*SIMD_HALF_PI
21
22
23
24 #define SIMDSQRT12 SimdScalar(0.7071067811865475244008443621048490)
25
26 #define SimdRecipSqrt(x) ((float)(1.0f/SimdSqrt(float(x))))             /* reciprocal square root */
27
28
29 inline void SimdPlaneSpace1 (const SimdVector3& n, SimdVector3& p, SimdVector3& q)
30 {
31   if (SimdFabs(n[2]) > SIMDSQRT12) {
32     // choose p in y-z plane
33     SimdScalar a = n[1]*n[1] + n[2]*n[2];
34     SimdScalar k = SimdRecipSqrt (a);
35     p[0] = 0;
36     p[1] = -n[2]*k;
37     p[2] = n[1]*k;
38     // set q = n x p
39     q[0] = a*k;
40     q[1] = -n[0]*p[2];
41     q[2] = n[0]*p[1];
42   }
43   else {
44     // choose p in x-y plane
45     SimdScalar a = n[0]*n[0] + n[1]*n[1];
46     SimdScalar k = SimdRecipSqrt (a);
47     p[0] = -n[1]*k;
48     p[1] = n[0]*k;
49     p[2] = 0;
50     // set q = n x p
51     q[0] = -n[2]*p[1];
52     q[1] = n[2]*p[0];
53     q[2] = a*k;
54   }
55 }
56
57
58
59 /// Utils related to temporal transforms
60 class SimdTransformUtil
61 {
62
63 public:
64
65         static void IntegrateTransform(const SimdTransform& curTrans,const SimdVector3& linvel,const SimdVector3& angvel,SimdScalar timeStep,SimdTransform& predictedTransform)
66         {
67                 predictedTransform.setOrigin(curTrans.getOrigin() + linvel * timeStep);
68 //      #define QUATERNION_DERIVATIVE
69         #ifdef QUATERNION_DERIVATIVE
70                 SimdQuaternion orn = curTrans.getRotation();
71                 orn += (angvel * orn) * (timeStep * 0.5f);
72                 orn.normalize();
73         #else
74                 //exponential map
75                 SimdVector3 axis;
76                 SimdScalar      fAngle = angvel.length(); 
77                 //limit the angular motion
78                 if (fAngle*timeStep > ANGULAR_MOTION_TRESHOLD)
79                 {
80                         fAngle = ANGULAR_MOTION_TRESHOLD / timeStep;
81                 }
82
83                 if ( fAngle < 0.001f )
84                 {
85                         // use Taylor's expansions of sync function
86                         axis   = angvel*( 0.5f*timeStep-(timeStep*timeStep*timeStep)*(0.020833333333f)*fAngle*fAngle );
87                 }
88                 else
89                 {
90                         // sync(fAngle) = sin(c*fAngle)/t
91                         axis   = angvel*( SimdSin(0.5f*fAngle*timeStep)/fAngle );
92                 }
93                 SimdQuaternion dorn (axis.x(),axis.y(),axis.z(),SimdCos( fAngle*timeStep*0.5f ));
94                 SimdQuaternion orn0 = curTrans.getRotation();
95
96                 SimdQuaternion predictedOrn = dorn * orn0;
97         #endif
98                 predictedTransform.setRotation(predictedOrn);
99         }
100
101         static void     CalculateVelocity(const SimdTransform& transform0,const SimdTransform& transform1,SimdScalar timeStep,SimdVector3& linVel,SimdVector3& angVel)
102         {
103                 linVel = (transform1.getOrigin() - transform0.getOrigin()) / timeStep;
104 #ifdef USE_QUATERNION_DIFF
105                 SimdQuaternion orn0 = transform0.getRotation();
106                 SimdQuaternion orn1a = transform1.getRotation();
107                 SimdQuaternion orn1 = orn0.farthest(orn1a);
108                 SimdQuaternion dorn = orn1 * orn0.inverse();
109 #else
110                 SimdMatrix3x3 dmat = transform1.getBasis() * transform0.getBasis().inverse();
111                 SimdQuaternion dorn;
112                 dmat.getRotation(dorn);
113 #endif//USE_QUATERNION_DIFF
114
115                 SimdVector3 axis;
116                 SimdScalar  angle;
117                 angle = dorn.getAngle();
118                 axis = SimdVector3(dorn.x(),dorn.y(),dorn.z());
119                 axis[3] = 0.f;
120                 //check for axis length
121                 SimdScalar len = axis.length2();
122                 if (len < SIMD_EPSILON*SIMD_EPSILON)
123                         axis = SimdVector3(1.f,0.f,0.f);
124                 else
125                         axis /= SimdSqrt(len);
126
127                 
128                 angVel = axis * angle / timeStep;
129
130         }
131
132
133 };
134
135 #endif //SIMD_TRANSFORM_UTIL_H
136