Subversion Repositories gelsvn

Rev

Rev 501 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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