OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
quat.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // This file is part of the OpenStructure project <www.openstructure.org>
3 //
4 // Copyright (C) 2008-2011 by the OpenStructure authors
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License as published by the Free
8 // Software Foundation; either version 3.0 of the License, or (at your option)
9 // any later version.
10 // This library is distributed in the hope that it will be useful, but WITHOUT
11 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 // details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this library; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 //------------------------------------------------------------------------------
19 #ifndef GEOM_QUAT_HH
20 #define GEOM_QUAT_HH
21 
22 #include <iostream>
23 #include <boost/operators.hpp>
24 
26 
27 /*
28  Unit Quaternion
29 
30  math and code assembled from various places, among them:
31 
32  E.B. Dam, M. Koch, M. Lillholm, Technical Report DIKU-TR-98/5,
33  Department of Computer Science, University of Copenhagen
34 
35  boost quaternion example, (C) Copyright Hubert Holin 2001.
36  Distributed under the Boost Software License, Version 1.0.
37 
38  Python Computer Graphics Kit, original code by Matthias Baas (C) 2004
39  Distributed under the GPL v2.0
40 */
41 
42 namespace geom {
43 
44 class Mat3;
45 class Vec3;
46 
48 
55  private boost::additive<Quat>,
56  private boost::multiplicative<Quat, Real>,
57  private boost::multiplicative<Quat,Quat>
58 {
59 public:
60  Quat();
61 
62  Quat(Real w, Real x, Real y, Real z);
63 
64  // initialize with a rotation (in rad) around a given axis
65  Quat(Real angle, const geom::Vec3& axis);
66 
67  // initialize from a rotation matrix
68  Quat(const Mat3& rotmat);
69 
70  // return 3x3 rotation matrix
71  Mat3 ToRotationMatrix() const;
72 
73  Vec3 GetAxis() const;
74 
75  //get angle component
76  Real GetAngle() const;
77 
78  // operators
79 
80  // negateable
81  Quat operator-();
82  // addable
83  Quat& operator+=(const Quat& q);
84  // subtractable
85  Quat& operator-=(const Quat& q);
86  // multipliable with scalar
87  Quat& operator*=(Real s);
88  // multipliable with other quat
89  Quat& operator*=(const Quat& q);
90  // dividable with scalar
91  Quat& operator/=(Real s);
92  // dividable with other quat
93  Quat& operator/=(const Quat& q);
94  // comparable
95  bool operator==(const Quat& q) const;
96  bool operator!=(const Quat& q) const { return !this->operator==(q); }
97 
98  // Apply rotation to vector.
99  Vec3 Rotate(const Vec3& vec) const;
100 
101 
102  // these are public for direct value access
103  Real w,x,y,z;
104 };
105 
106 
108 
109 // inner product
110 Real DLLEXPORT_OST_GEOM Dot(const Quat& q0, const Quat& q1);
111 
112 // spherical linear interpolation, with t in range [0,1]
113 Quat DLLEXPORT_OST_GEOM Slerp(const Quat& q0, const Quat& q1, Real t);
114 
115 Quat DLLEXPORT_OST_GEOM Inv(const Quat& q);
116 Quat DLLEXPORT_OST_GEOM Exp(const Quat& q);
117 Quat DLLEXPORT_OST_GEOM Log(const Quat& q);
118 
119 //normalize quaternion
121 
122 DLLEXPORT_OST_GEOM std::ostream& operator<<(std::ostream& str, const Quat& q);
123 
124 } // ns
125 
126 #endif