Subversion Repositories gelsvn

Rev

Rev 89 | Rev 501 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 89 Rev 306
Line 1... Line 1...
1
#ifndef __CGLA_QUATF_H__
1
#ifndef __CGLA_QUATF_H__
2
#define __CGLA_QUATF_H__
2
#define __CGLA_QUATF_H__
3
 
3
 
-
 
4
#include "ArithQuat.h"
4
#include "Vec3f.h"
5
#include "Vec3f.h"
5
#include "Vec4f.h"
6
#include "Vec4f.h"
6
#include "Mat3x3f.h"
7
#include "Mat3x3f.h"
7
#include "Mat4x4f.h"
8
#include "Mat4x4f.h"
8
#include <cmath>
-
 
-
 
9
 
9
 
10
 
10
namespace CGLA {
11
namespace CGLA {
11
 
12
 
12
  /** \brief A float based Quaterinion class. 
13
  /** \brief A float based Quaterinion class. 
13
 
14
 
14
	Quaternions are algebraic entities useful for rotation. */
15
	Quaternions are algebraic entities useful for rotation. */
15
 
16
 
16
  class Quatf
17
	class Quatf : public ArithQuat<float,Vec3f,Quatf>
17
  {
18
  {
18
  public:
19
  public:
19
 
-
 
20
    /// Vector part of quaternion
-
 
21
    Vec3f qv;
-
 
22
 
-
 
23
    /// Scalar part of quaternion
-
 
24
    float qw;
-
 
25
 
-
 
26
    /// Construct undefined quaternion
-
 
27
#ifndef NDEBUG
-
 
28
    Quatf() : qw(CGLA_INIT_VALUE) {}
20
		Quatf() : ArithQuat<float, Vec3f, Quatf>() {}
29
#else
-
 
30
    Quatf() {}
-
 
31
#endif
-
 
32
 
21
 
33
    /// Construct quaternion from vector and scalar
22
    /// Construct quaternion from vector and scalar
34
    Quatf(const Vec3f& imaginary, float real = 1.0f) : qv(imaginary) , qw(real) {}
23
    Quatf(const Vec3f& imaginary, float real = 1.0f) : ArithQuat<float, Vec3f, Quatf>(imaginary, real) {}
35
 
24
 
36
    /// Construct quaternion from four scalars
25
    /// Construct quaternion from four scalars
37
    Quatf(float x, float y, float z, float _qw) : qv(x,y,z), qw(_qw) {}
26
    Quatf(float x, float y, float z, float _qw) :  ArithQuat<float, Vec3f, Quatf>(x,y,z,_qw) {}
38
 
-
 
39
    /// Construct quaternion from a 4D vector
-
 
40
    explicit Quatf(const Vec4f& v) : qv(v[0], v[1], v[2]), qw(v[3]) {}
-
 
41
 
-
 
42
    /// Assign values to a quaternion
-
 
43
    void set(const Vec3f& imaginary, float real=1.0f)
-
 
44
    {
-
 
45
      qv = imaginary;
-
 
46
      qw = real;
-
 
47
    }
-
 
48
 
-
 
49
    void set(float x, float y, float z, float _qw) 
-
 
50
    {
-
 
51
      qv.set(x,y,z);
-
 
52
      qw = _qw;
-
 
53
    }
-
 
54
 
-
 
55
    void set(const Vec4f& v)
-
 
56
    {
-
 
57
      qv.set(v[0], v[1], v[2]);
-
 
58
      qw = v[3];		  
-
 
59
    }
-
 
60
 
27
 
61
    /// Get values from a quaternion
28
		/// Construct quaternion from a 4D vector
62
    void get(float& x, float& y, float& z, float& _qw) const
29
		explicit Quatf(const Vec4f& v) : ArithQuat<float, Vec3f, Quatf>(v[0], v[1], v[2], v[3]) {}
63
    {
-
 
64
      x  = qv[0];
-
 
65
      y  = qv[1];
-
 
66
      z  = qv[2];
-
 
67
      _qw = qw;
-
 
68
    }
-
 
69
 
-
 
70
    /// Get imaginary part of a quaternion
-
 
71
    Vec3f get_imaginary_part() const { return qv; }
-
 
72
 
-
 
73
    /// Get real part of a quaternion
-
 
74
    float get_real_part() const { return qw; }
-
 
75
 
30
 
-
 
31
	
76
    /// Get a 3x3 rotation matrix from a quaternion
32
		/// Get a 3x3 rotation matrix from a quaternion
77
    Mat3x3f get_mat3x3f() const
33
    Mat3x3f get_Mat3x3f() const
78
    {
34
    {
79
      float s = 2/norm();
35
      float s = 2/norm();
80
      // note that the all q_*q_ are used twice (optimize)
36
      // note that the all q_*q_ are used twice (optimize)
81
      return Mat3x3f(Vec3f(1.0 - s*(qv[1]*qv[1] + qv[2]*qv[2]),
37
      return Mat3x3f(Vec3f(1.0 - s*(qv[1]*qv[1] + qv[2]*qv[2]),
82
			         s*(qv[0]*qv[1] - qw*qv[2]),
38
			         s*(qv[0]*qv[1] - qw*qv[2]),
Line 88... Line 44...
88
			         s*(qv[1]*qv[2] + qw*qv[0]),
44
			         s*(qv[1]*qv[2] + qw*qv[0]),
89
			   1.0 - s*(qv[0]*qv[0] + qv[1]*qv[1])));
45
			   1.0 - s*(qv[0]*qv[0] + qv[1]*qv[1])));
90
    }
46
    }
91
 
47
 
92
    /// Get a 4x4 rotation matrix from a quaternion
48
    /// Get a 4x4 rotation matrix from a quaternion
93
    Mat4x4f get_mat4x4f() const
49
    Mat4x4f get_Mat4x4f() const
94
    {
50
    {
95
      float s = 2/norm();
51
      float s = 2/norm();
96
      // note that the all q_*q_ are used twice (optimize?)
52
      // note that the all q_*q_ are used twice (optimize?)
97
      return Mat4x4f(Vec4f(1.0 - s*(qv[1]*qv[1] + qv[2]*qv[2]),
53
      return Mat4x4f(Vec4f(1.0 - s*(qv[1]*qv[1] + qv[2]*qv[2]),
98
			         s*(qv[0]*qv[1] - qw*qv[2]),
54
			         s*(qv[0]*qv[1] - qw*qv[2]),
Line 106... Line 62...
106
			         s*(qv[1]*qv[2] + qw*qv[0]),
62
			         s*(qv[1]*qv[2] + qw*qv[0]),
107
			   1.0 - s*(qv[0]*qv[0] + qv[1]*qv[1]),
63
			   1.0 - s*(qv[0]*qv[0] + qv[1]*qv[1]),
108
			   0.0),
64
			   0.0),
109
		     Vec4f(0.0, 0.0, 0.0, 1.0));
65
		     Vec4f(0.0, 0.0, 0.0, 1.0));
110
    }
66
    }
111
 
-
 
112
    /// Obtain angle of rotation and axis
-
 
113
    void get_rot(float& angle, Vec3f& v)
-
 
114
    {
-
 
115
      angle = 2*std::acos(qw);
-
 
116
 
-
 
117
      if(angle < TINY) 
-
 
118
	v = Vec3f(1.0, 0.0, 0.0);
-
 
119
      else 
-
 
120
	v = qv*(1/std::sin(angle));
-
 
121
 
-
 
122
      if(angle > M_PI)
-
 
123
	v = -v;
-
 
124
      
-
 
125
      v.normalize();      
-
 
126
    }
-
 
127
 
-
 
128
    /// Construct a Quaternion from an angle and axis of rotation.
-
 
129
    void make_rot(float angle, const Vec3f& v)
-
 
130
    {
-
 
131
      angle /= 2.0;
-
 
132
      qv = CGLA::normalize(v)*std::sin(angle);
-
 
133
      qw = std::cos(angle);
-
 
134
    }
-
 
135
 
-
 
136
    /** Construct a Quaternion rotating from the direction given
-
 
137
	by the first argument to the direction given by the second.*/
-
 
138
    void make_rot(const Vec3f& s,const Vec3f& t)
-
 
139
    {
-
 
140
      float tmp = std::sqrt(2*(1 + dot(s, t)));
-
 
141
      qv = cross(s, t)*(1.0/tmp);
-
 
142
      qw = tmp/2.0;    
-
 
143
    }
-
 
144
 
-
 
145
    //----------------------------------------------------------------------
-
 
146
    // Binary operators
-
 
147
    //----------------------------------------------------------------------
-
 
148
    
-
 
149
    bool operator==(const Quatf& q) const
-
 
150
    {
-
 
151
      return qw == q.qw && qv == q.qv;
-
 
152
    }
-
 
153
 
-
 
154
    bool operator!=(const Quatf& q) const
-
 
155
    {
-
 
156
      return qw != q.qw || qv != q.qv;
-
 
157
    }
-
 
158
 
-
 
159
    /// Multiply two quaternions. (Combine their rotation)
-
 
160
    Quatf operator*(const Quatf& q) const
-
 
161
    {
-
 
162
      return Quatf(cross(qv, q.qv) + qv*q.qw + q.qv*qw, 
-
 
163
		         qw*q.qw - dot(qv, q.qv));      
-
 
164
    }
-
 
165
 
-
 
166
    /// Multiply scalar onto quaternion.
-
 
167
    Quatf operator*(float scalar) const
-
 
168
    {
-
 
169
      return Quatf(qv*scalar, qw*scalar);
-
 
170
    }
-
 
171
 
-
 
172
    /// Add two quaternions.
-
 
173
    Quatf operator+(const Quatf& q) const
-
 
174
    {
-
 
175
      return Quatf(qv + q.qv, qw + q.qw);
-
 
176
    }
-
 
177
 
-
 
178
    //----------------------------------------------------------------------
-
 
179
    // Unary operators
-
 
180
    //----------------------------------------------------------------------
-
 
181
 
-
 
182
    /// Compute the additive inverse of the quaternion
-
 
183
    Quatf operator-() const { return Quatf(-qv, -qw); }
-
 
184
 
-
 
185
    /// Compute norm of quaternion
-
 
186
    float norm() const { return dot(qv, qv) + qw*qw; }
-
 
187
 
-
 
188
    /// Return conjugate quaternion
-
 
189
    Quatf conjugate() const { return Quatf(-qv, qw); }
-
 
190
 
-
 
191
    /// Compute the multiplicative inverse of the quaternion
-
 
192
    Quatf inverse() const { return Quatf(conjugate()*(1/norm())); }
-
 
193
    
-
 
194
    /// Normalize quaternion.
-
 
195
    Quatf normalize() { return Quatf((*this)*(1/norm())); }
-
 
196
 
-
 
197
    //----------------------------------------------------------------------
-
 
198
    // Application
-
 
199
    //----------------------------------------------------------------------
-
 
200
 
-
 
201
    /// Rotate vector according to quaternion
-
 
202
    Vec3f apply(const Vec3f& vec) const 
-
 
203
    {
-
 
204
      return ((*this)*Quatf(vec)*inverse()).qv;
-
 
205
    }
-
 
206
 
-
 
207
    /// Rotate vector according to unit quaternion
-
 
208
    Vec3f apply_unit(const Vec3f& vec) const
-
 
209
    {
-
 
210
      return ((*this)*Quatf(vec)*conjugate()).qv;
-
 
211
    }
-
 
212
  };
-
 
213
 
-
 
214
  inline Quatf operator*(float scalar, const Quatf& q)
-
 
215
  {
-
 
216
    return q*scalar;
-
 
217
  }
-
 
218
 
-
 
219
  /** Perform linear interpolation of two quaternions. 
-
 
220
      The last argument is the parameter used to interpolate
-
 
221
      between the two first. SLERP - invented by Shoemake -
-
 
222
      is a good way to interpolate because the interpolation
-
 
223
      is performed on the unit sphere. 	
-
 
224
  */
-
 
225
  inline Quatf slerp(Quatf q0, Quatf q1, float t)
-
 
226
  {
-
 
227
    float angle = std::acos(q0.qv[0]*q1.qv[0] + q0.qv[1]*q1.qv[1] 
-
 
228
			    + q0.qv[2]*q1.qv[2] + q0.qw*q1.qw);
-
 
229
    return (q0*std::sin((1 - t)*angle) + q1*std::sin(t*angle))*(1/std::sin(angle));
-
 
230
  }
-
 
231
 
67
		
232
  /// Create an identity quaternion
68
	/// Create an identity quaternion
233
  inline Quatf identity_Quatf()
69
  inline Quatf identity_Quatf()
234
  {
70
  {
235
    return Quatf(Vec3f(0.0));
71
    return Quatf(Vec3f(0.0));
236
  }
72
  }
237
 
73
 
238
  /// Print quaternion to stream.
-
 
239
  inline std::ostream& operator<<(std::ostream&os, const Quatf v)
-
 
240
  {
74
	};
241
    os << "[ ";
-
 
242
    for(unsigned int i=0;i<3;i++) os << v.qv[i] << " ";
-
 
243
    os << "~ " << v.qw << " ";
-
 
244
    os << "]";
-
 
245
 
75
 
246
    return os;
-
 
247
  }
-
 
248
}
76
}
249
#endif
77
#endif