FreeFOAM The Cross-Platform CFD Toolkit
quaternion.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::quaternion
26 
27 Description
28  Quaternion class used to perform rotations in 3D space.
29 
30 SourceFiles
31  quaternionI.H
32  quaternion.C
33 
34 \*---------------------------------------------------------------------------*/
35 
36 #ifndef quaternion_H
37 #define quaternion_H
38 
39 #include <OpenFOAM/scalar.H>
40 #include <OpenFOAM/vector.H>
41 #include <OpenFOAM/tensor.H>
42 #include <OpenFOAM/word.H>
43 #include <OpenFOAM/contiguous.H>
44 
45 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
46 
47 namespace Foam
48 {
49 
50 // Forward declaration of friend functions and operators
51 
52 class quaternion;
53 Istream& operator>>(Istream& is, quaternion&);
54 Ostream& operator<<(Ostream& os, const quaternion& C);
55 
56 
57 /*---------------------------------------------------------------------------*\
58  Class quaternion Declaration
59 \*---------------------------------------------------------------------------*/
60 
62 {
63  // private data
64 
65  //- Scalar part of the quaternion ( = cos(theta/2) for rotation)
66  scalar w_;
67 
68  //- Vector part of the quaternion ( = axis of rotation)
69  vector v_;
70 
71 
72  //- Multiply vector v by quaternion as if v is a pure quaternion
73  inline quaternion mulq0v(const vector& v) const;
74 
75 
76 public:
77 
78  // Static data members
79 
80  static const char* const typeName;
81 
82  static const quaternion zero;
83  static const quaternion I;
84 
85 
86  // Constructors
87 
88  //- Construct null
89  inline quaternion();
90 
91  //- Construct given scalar and vector parts
92  inline quaternion(const scalar w, const vector& v);
93 
94  //- Construct a rotation quaternion given the direction d
95  // and angle theta
96  inline quaternion(const vector& d, const scalar theta);
97 
98  //- Construct given scalar part, the vector part = vector::zero
99  inline explicit quaternion(const scalar w);
100 
101  //- Construct a pure quaternion given the vector part, scalar part = 0
102  inline explicit quaternion(const vector& v);
103 
104  //- Construct a quaternion given the three Euler angles
105  inline quaternion
106  (
107  const scalar angleX,
108  const scalar angleY,
109  const scalar angleZ
110  );
111 
112  //- Construct from Istream
114 
115 
116  // Member functions
117 
118  // Access
119 
120  //- Scalar part of the quaternion ( = cos(theta/2) for rotation)
121  inline scalar w() const;
122 
123  //- Vector part of the quaternion ( = axis of rotation)
124  inline const vector& v() const;
125 
126  //- The rotation tensor corresponding the quaternion
127  inline tensor R() const;
128 
129 
130  // Edit
131 
132  //- Scalar part of the quaternion ( = cos(theta/2) for rotation)
133  inline scalar& w();
134 
135  //- Vector part of the quaternion ( = axis of rotation)
136  inline vector& v();
137 
138  inline void normalize();
139 
140 
141  // Transform
142 
143  //- Rotate the given vector
144  inline vector transform(const vector& v) const;
145 
146  //- Rotate the given vector anti-clockwise
147  inline vector invTransform(const vector& v) const;
148 
149  //- Rotate the given quaternion (and normalize)
150  inline quaternion transform(const quaternion& q) const;
151 
152  //- Rotate the given quaternion anti-clockwise (and normalize)
153  inline quaternion invTransform(const quaternion& q) const;
154 
155 
156  // Member operators
157 
158  inline void operator=(const quaternion&);
159  inline void operator+=(const quaternion&);
160  inline void operator-=(const quaternion&);
161  inline void operator*=(const quaternion&);
162  inline void operator/=(const quaternion&);
163 
164  inline void operator=(const scalar);
165 
166  inline void operator=(const vector&);
167 
168  inline void operator*=(const scalar);
169  inline void operator/=(const scalar);
170 
171 
172  // IOstream operators
173 
174  friend Istream& operator>>(Istream& is, quaternion&);
175  friend Ostream& operator<<(Ostream& os, const quaternion& C);
176 };
177 
178 
179 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
180 
181 inline scalar magSqr(const quaternion& q);
182 inline scalar mag(const quaternion& q);
183 
184 //- Return the conjugate of the given quaternion
185 inline quaternion conjugate(const quaternion& q);
186 
187 //- Return the normailzed (unit) quaternion of the given quaternion
188 inline quaternion normalize(const quaternion& q);
189 
190 //- Return the inverse of the given quaternion
191 inline quaternion inv(const quaternion& q);
192 
193 //- Return a string representation of a quaternion
194 word name(const quaternion&);
195 
196 //- Data associated with quaternion type are contiguous
197 template<>
198 inline bool contiguous<quaternion>() {return true;}
199 
200 
201 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
202 
203 inline bool operator==(const quaternion& q1, const quaternion& q2);
204 inline bool operator!=(const quaternion& q1, const quaternion& q2);
205 inline quaternion operator+(const quaternion& q1, const quaternion& q2);
206 inline quaternion operator-(const quaternion& q);
207 inline quaternion operator-(const quaternion& q1, const quaternion& q2);
208 inline scalar operator&(const quaternion& q1, const quaternion& q2);
209 inline quaternion operator*(const quaternion& q1, const quaternion& q2);
210 inline quaternion operator/(const quaternion& q1, const quaternion& q2);
211 inline quaternion operator*(const scalar s, const quaternion& q);
212 inline quaternion operator*(const quaternion& q, const scalar s);
213 inline quaternion operator/(const quaternion& q, const scalar s);
214 
215 
216 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
217 
218 } // End namespace Foam
219 
220 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
221 
222 #include <OpenFOAM/quaternionI.H>
223 
224 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
225 
226 #endif
227 
228 // ************************ vim: set sw=4 sts=4 et: ************************ //