~ppsspp/ppsspp/ppsspp_1.3.0

« back to all changes in this revision

Viewing changes to ext/native/math/lin/quat.h

  • Committer: Sérgio Benjamim
  • Date: 2017-01-02 00:12:05 UTC
  • Revision ID: sergio_br2@yahoo.com.br-20170102001205-cxbta9za203nmjwm
1.3.0 source (from ppsspp_1.3.0-r160.p5.l1762.a165.t83~56~ubuntu16.04.1.tar.xz).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef _MATH_LIN_QUAT_H
 
2
#define _MATH_LIN_QUAT_H
 
3
 
 
4
#include "math/lin/vec3.h"
 
5
 
 
6
class Matrix4x4;
 
7
 
 
8
class Quaternion
 
9
{
 
10
public:
 
11
        float x,y,z,w;
 
12
 
 
13
        Quaternion() { }
 
14
        Quaternion(const float _x, const float _y, const float _z, const float _w) {
 
15
                x=_x; y=_y; z=_z; w=_w;
 
16
        }
 
17
        void setIdentity()
 
18
        {
 
19
                x=y=z=0; w=1.0f;
 
20
        }
 
21
        void setXRotation(const float r) { w = cosf(r / 2); x = sinf(r / 2); y = z = 0; }
 
22
        void setYRotation(const float r) { w = cosf(r / 2); y = sinf(r / 2); x = z = 0; }
 
23
        void setZRotation(const float r) { w = cosf(r / 2); z = sinf(r / 2); x = y = 0; }
 
24
        void toMatrix(Matrix4x4 *out) const;
 
25
        static Quaternion fromMatrix(Matrix4x4 &m);
 
26
 
 
27
        Quaternion operator *(Quaternion &q) const
 
28
        {
 
29
                return Quaternion(
 
30
                        (w * q.w) - (x * q.x) - (y * q.y) - (z * q.z),
 
31
                        (w * q.x) + (x * q.w) + (y * q.z) - (z * q.y),
 
32
                        (w * q.y) + (y * q.w) + (z * q.x) - (x * q.z),
 
33
                        (w * q.z) + (z * q.w) + (x * q.y) - (y * q.x)
 
34
                        );
 
35
        }
 
36
        Quaternion operator -()
 
37
        {
 
38
                return Quaternion(-x,-y,-z,-w);
 
39
        }
 
40
        void setRotation(Vec3 axis, float angle)
 
41
        {
 
42
                axis /= axis.length();
 
43
                angle *= .5f;
 
44
                float sine = sinf(angle);
 
45
                w = cosf(angle);
 
46
                x = sine * axis.x;
 
47
                y = sine * axis.y;
 
48
                z = sine * axis.z;
 
49
        }
 
50
        void toAxisAngle(Vec3 &v, float &angle)
 
51
        {
 
52
                normalize();
 
53
                if (w==1.0f && x==0.0f && y==0.0f && z==0.0f)
 
54
                {
 
55
                        v = Vec3(0,1,0);
 
56
                        angle = 0.0f;
 
57
                        return;
 
58
                }
 
59
                float cos_a = w;
 
60
                angle = acosf(cos_a) * 2;
 
61
                float sin_a = sqrtf( 1.0f - cos_a * cos_a );
 
62
                if (fabsf(sin_a) < 0.00005f) sin_a = 1;
 
63
                float inv_sin_a=1.0f/sin_a;
 
64
                v.x = x * inv_sin_a;
 
65
                v.y = y * inv_sin_a;
 
66
                v.z = z * inv_sin_a;
 
67
        }
 
68
        enum {
 
69
                QUAT_SHORT,
 
70
                QUAT_LONG,
 
71
                QUAT_CW,
 
72
                QUAT_CCW
 
73
        };
 
74
        Quaternion slerp(const Quaternion &to, const float a) const;
 
75
        Quaternion multiply(const Quaternion &q) const;
 
76
        float &operator [] (int i) {
 
77
                return *((&x) + i);
 
78
        }
 
79
        float operator [] (int i) const {
 
80
                return *((&x) + i);
 
81
        }
 
82
        //not sure about this, maybe mag is supposed to sqrt
 
83
        float magnitude() const {
 
84
                return x*x + y*y + z*z + w*w;
 
85
        }
 
86
        void normalize()        {
 
87
                float f = 1.0f/sqrtf(magnitude());
 
88
                x*=f; y*=f; z*=f; w*=f;
 
89
        }
 
90
};
 
91
 
 
92
#endif  // _MATH_LIN_QUAT_H