added summerhack
[summerhack] / tools / curve_draw / vmath / quaternion.h
1 /*
2 This file is part of XRay, a photorealistic 3D rendering library.
3 Copyright (C) 2005 John Tsiombikas
4
5 XRay is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 XRay is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with XRay; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18 */
19
20 /**
21  * @file quaternion.h
22  * @author John Tsiombikas
23  * @date 28 June 2005
24  *
25  * Quaternion math.
26  */
27
28 #ifndef QUATERNION_H_
29 #define QUATERNION_H_
30
31 #include <iostream>
32 #include "vmath_types.h"
33 #include "vector.h"
34
35 /** Quaternion */
36 class Quaternion {
37 public:
38         scalar_t s;
39         Vector3 v;
40
41         Quaternion();
42         Quaternion(scalar_t s, const Vector3 &v);
43         Quaternion(scalar_t s, scalar_t x, scalar_t y, scalar_t z);
44         Quaternion(const Vector3 &axis, scalar_t angle);
45
46         Quaternion operator +(const Quaternion &quat) const;
47         Quaternion operator -(const Quaternion &quat) const;
48         Quaternion operator -() const;
49         Quaternion operator *(const Quaternion &quat) const;
50         
51         void operator +=(const Quaternion &quat);
52         void operator -=(const Quaternion &quat);
53         void operator *=(const Quaternion &quat);
54
55         void reset_identity();
56
57         Quaternion conjugate() const;
58
59         scalar_t length() const;
60         scalar_t length_sq() const;
61         
62         void normalize();
63         Quaternion normalized() const;
64
65         Quaternion inverse() const;
66
67         void set_rotation(const Vector3 &axis, scalar_t angle);
68         void rotate(const Vector3 &axis, scalar_t angle);
69         /* note: this is a totally different operation from the above
70          * this treats the quaternion as signifying direction and rotates
71          * it by a rotation quaternion by rot * q * rot'
72          */
73         void rotate(const Quaternion &q);
74
75         Matrix3x3 get_rotation_matrix() const;
76         
77         friend Quaternion slerp(const Quaternion &q1, const Quaternion &q2, scalar_t t);
78         
79         friend std::ostream &operator <<(std::ostream &out, const Quaternion &q);
80 };
81
82
83 #endif  // QUATERNION_H_