Subversion Repositories gelsvn

Rev

Rev 595 | Details | Compare with Previous | Last modification | View Log | RSS feed

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