Subversion Repositories gelsvn

Rev

Rev 306 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
48 jrf 1
#ifndef __CGLA_QUATF_H__
2
#define __CGLA_QUATF_H__
3
 
306 jrf 4
#include "ArithQuat.h"
48 jrf 5
#include "Vec3f.h"
6
#include "Vec4f.h"
7
#include "Mat3x3f.h"
8
#include "Mat4x4f.h"
9
 
306 jrf 10
 
48 jrf 11
namespace CGLA {
12
 
89 jab 13
  /** \brief A float based Quaterinion class. 
48 jrf 14
 
89 jab 15
	Quaternions are algebraic entities useful for rotation. */
16
 
306 jrf 17
	class Quatf : public ArithQuat<float,Vec3f,Quatf>
48 jrf 18
  {
19
  public:
306 jrf 20
		Quatf() : ArithQuat<float, Vec3f, Quatf>() {}
48 jrf 21
 
22
    /// Construct quaternion from vector and scalar
306 jrf 23
    Quatf(const Vec3f& imaginary, float real = 1.0f) : ArithQuat<float, Vec3f, Quatf>(imaginary, real) {}
48 jrf 24
 
25
    /// Construct quaternion from four scalars
306 jrf 26
    Quatf(float x, float y, float z, float _qw) :  ArithQuat<float, Vec3f, Quatf>(x,y,z,_qw) {}
48 jrf 27
 
306 jrf 28
		/// Construct quaternion from a 4D vector
29
		explicit Quatf(const Vec4f& v) : ArithQuat<float, Vec3f, Quatf>(v[0], v[1], v[2], v[3]) {}
48 jrf 30
 
306 jrf 31
 
32
		/// Get a 3x3 rotation matrix from a quaternion
33
    Mat3x3f get_Mat3x3f() const
48 jrf 34
    {
49 jrf 35
      float s = 2/norm();
36
      // note that the all q_*q_ are used twice (optimize)
501 jrf 37
      return Mat3x3f(Vec3f(1.0f - s*(qv[1]*qv[1] + qv[2]*qv[2]),
49 jrf 38
			         s*(qv[0]*qv[1] - qw*qv[2]),
39
			         s*(qv[0]*qv[2] + qw*qv[1])),
40
		     Vec3f(      s*(qv[0]*qv[1] + qw*qv[2]),
501 jrf 41
  			   1.0f - s*(qv[0]*qv[0] + qv[2]*qv[2]),
49 jrf 42
			         s*(qv[1]*qv[2] - qw*qv[0])),
43
		     Vec3f(      s*(qv[0]*qv[2] - qw*qv[1]),
44
			         s*(qv[1]*qv[2] + qw*qv[0]),
501 jrf 45
			   1.0f - s*(qv[0]*qv[0] + qv[1]*qv[1])));
49 jrf 46
    }
48 jrf 47
 
48
    /// Get a 4x4 rotation matrix from a quaternion
306 jrf 49
    Mat4x4f get_Mat4x4f() const
49 jrf 50
    {
501 jrf 51
      float s = 2.0f/norm();
49 jrf 52
      // note that the all q_*q_ are used twice (optimize?)
501 jrf 53
      return Mat4x4f(Vec4f(1.0f - s*(qv[1]*qv[1] + qv[2]*qv[2]),
49 jrf 54
			         s*(qv[0]*qv[1] - qw*qv[2]),
55
			         s*(qv[0]*qv[2] + qw*qv[1]),
501 jrf 56
		           0.0f),
49 jrf 57
		     Vec4f(      s*(qv[0]*qv[1] + qw*qv[2]),
501 jrf 58
			   1.0f - s*(qv[0]*qv[0] + qv[2]*qv[2]),
49 jrf 59
			         s*(qv[1]*qv[2] - qw*qv[0]),
501 jrf 60
			   0.0f),
49 jrf 61
		     Vec4f(      s*(qv[0]*qv[2] - qw*qv[1]),
62
			         s*(qv[1]*qv[2] + qw*qv[0]),
501 jrf 63
			   1.0f - s*(qv[0]*qv[0] + qv[1]*qv[1]),
64
			   0.0f),
65
		     Vec4f(0.0f, 0.0f, 0.0f, 1.0f));
49 jrf 66
    }
306 jrf 67
 
68
	/// Create an identity quaternion
48 jrf 69
  inline Quatf identity_Quatf()
70
  {
501 jrf 71
    return Quatf(Vec3f(0.0f));
48 jrf 72
  }
73
 
306 jrf 74
	};
48 jrf 75
 
76
}
77
#endif