source: trunk/packages/vizservers/nanovis/vrmath/include/vrmath/vrQuaternion.h @ 2842

Last change on this file since 2842 was 2842, checked in by ldelgass, 12 years ago

Some additions to quat/rotation classes in vrmath lib

  • Property svn:eol-style set to native
File size: 2.2 KB
Line 
1/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/**
3 * Quaternion code by BLACKAXE / kolor aka Laurent Schmalen
4 * Use for commercial is strictly prohibited
5 *
6 * I (Insoo Woo) have changed names according to my naming rules
7 */
8#ifndef VRQUATERNION_H
9#define VRQUATERNION_H
10
11#include <vrmath/vrLinmath.h>
12
13class vrRotation;
14
15class vrQuaternion
16{
17public:
18    vrQuaternion();
19
20    explicit vrQuaternion(const vrRotation& rot);
21
22    vrQuaternion(float x, float y = 0, float z = 0, float w = 0);
23
24    void set(float x1, float y1, float z1, float w1);
25
26    const vrQuaternion& set(const vrRotation& rot);
27
28    vrQuaternion conjugate() const
29    {
30        vrQuaternion result;
31        result.w = w;
32        result.x = -x;
33        result.y = -y;
34        result.z = -z;
35        return result;
36    }
37
38    vrQuaternion reciprocal() const
39    {
40        double denom =  w*w + x*x + y*y + z*z;
41        vrQuaternion result = conjugate();
42        result.x /= denom;
43        result.y /= denom;
44        result.z /= denom;
45        result.w /= denom;
46        return result;
47    }
48
49    void slerp(const vrRotation &a,const vrRotation &b, const float t);
50
51    void slerp(const vrQuaternion &a,const vrQuaternion &b, const float t);
52
53    const vrQuaternion& normalize();
54
55    vrQuaternion operator*(const vrQuaternion& q2) const
56    {
57        vrQuaternion result;
58        result.w = (w * q2.w) - (x * q2.x) - (y * q2.y) - (z * q2.z);
59        result.x = (w * q2.x) + (x * q2.w) + (y * q2.z) - (z * q2.y);
60        result.y = (w * q2.y) + (y * q2.w) + (z * q2.x) - (x * q2.z);
61        result.z = (w * q2.z) + (z * q2.w) + (x * q2.y) - (y * q2.x);
62        return result;
63    }
64
65    friend bool operator==(const vrQuaternion& q1, const vrQuaternion& q2);
66
67    float x, y, z, w;
68};
69
70inline bool operator==(const vrQuaternion& q1, const vrQuaternion& q2)
71{
72    return ((q1.x == q2.x) && (q1.y == q2.y) && (q1.z == q2.z) && (q1.w == q2.w));
73}
74
75inline void vrQuaternion::slerp(const vrRotation &a,const vrRotation &b, const float t)
76{
77    slerp(vrQuaternion(a), vrQuaternion(b), t);
78}
79
80inline void vrQuaternion::set(float x1, float y1, float z1, float w1)
81{
82    x = x1;
83    y = y1;
84    z = z1;
85    w = w1;
86}
87
88#endif
Note: See TracBrowser for help on using the repository browser.