Subversion Repositories gelsvn

Rev

Rev 306 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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