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 Quatd.h
-
 
8
 * @brief Double based quaternion class
-
 
9
 */
-
 
10
 
1
#ifndef __CGLA_QUATD_H__
11
#ifndef __CGLA_QUATD_H__
2
#define __CGLA_QUATD_H__
12
#define __CGLA_QUATD_H__
3
 
13
 
4
#include "ArithQuat.h"
14
#include "ArithQuat.h"
5
#include "Vec3d.h"
15
#include "Vec3d.h"
6
#include "Vec4d.h"
16
#include "Vec4d.h"
7
#include "Mat3x3d.h"
17
#include "Mat3x3d.h"
8
#include "Mat4x4d.h"
18
#include "Mat4x4d.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 Quatd : public ArithQuat<double,Vec3d,Quatd>
27
	class Quatd : public ArithQuat<double,Vec3d,Quatd>
18
  {
28
  {
19
  public:
29
  public:
20
		Quatd() : ArithQuat<double, Vec3d, Quatd>() {}
30
		Quatd() : ArithQuat<double, Vec3d, Quatd>() {}
21
 
31
 
22
    /// Construct quaternion from vector and scalar
32
    /// Construct quaternion from vector and scalar
23
    Quatd(const Vec3d& imaginary, double real = 1.0) : ArithQuat<double, Vec3d, Quatd>(imaginary, real) {}
33
    Quatd(const Vec3d& imaginary, double real = 1.0) : ArithQuat<double, Vec3d, Quatd>(imaginary, real) {}
24
 
34
 
25
    /// Construct quaternion from four scalars
35
    /// Construct quaternion from four scalars
26
    Quatd(double x, double y, double z, double _qw) :  ArithQuat<double, Vec3d, Quatd>(x,y,z,_qw) {}
36
    Quatd(double x, double y, double z, double _qw) :  ArithQuat<double, Vec3d, Quatd>(x,y,z,_qw) {}
27
 
37
 
28
		/// Construct quaternion from a 4D vector
38
		/// Construct quaternion from a 4D vector
29
		explicit Quatd(const Vec4d& v) : ArithQuat<double, Vec3d, Quatd>(v[0], v[1], v[2], v[3]) {}
39
		explicit Quatd(const Vec4d& v) : ArithQuat<double, Vec3d, Quatd>(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
    Mat3x3d get_Mat3x3d() const
43
    Mat3x3d get_Mat3x3d() const
34
    {
44
    {
35
      double s = 2.0/norm();
45
      double s = 2.0/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 Mat3x3d(Vec3d(1.0 - s*(qv[1]*qv[1] + qv[2]*qv[2]),
47
      return Mat3x3d(Vec3d(1.0 - 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
		     Vec3d(      s*(qv[0]*qv[1] + qw*qv[2]),
50
		     Vec3d(      s*(qv[0]*qv[1] + qw*qv[2]),
41
  			   1.0 - s*(qv[0]*qv[0] + qv[2]*qv[2]),
51
  			   1.0 - 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
		     Vec3d(      s*(qv[0]*qv[2] - qw*qv[1]),
53
		     Vec3d(      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.0 - s*(qv[0]*qv[0] + qv[1]*qv[1])));
55
			   1.0 - 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
    Mat4x4d get_Mat4x4d() const
59
    Mat4x4d get_Mat4x4d() const
50
    {
60
    {
51
      double s = 2/norm();
61
      double s = 2/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 Mat4x4d(Vec4d(1.0 - s*(qv[1]*qv[1] + qv[2]*qv[2]),
63
      return Mat4x4d(Vec4d(1.0 - 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.0),
66
		           0.0),
57
		     Vec4d(      s*(qv[0]*qv[1] + qw*qv[2]),
67
		     Vec4d(      s*(qv[0]*qv[1] + qw*qv[2]),
58
			   1.0 - s*(qv[0]*qv[0] + qv[2]*qv[2]),
68
			   1.0 - 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.0),
70
			   0.0),
61
		     Vec4d(      s*(qv[0]*qv[2] - qw*qv[1]),
71
		     Vec4d(      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.0 - s*(qv[0]*qv[0] + qv[1]*qv[1]),
73
			   1.0 - s*(qv[0]*qv[0] + qv[1]*qv[1]),
64
			   0.0),
74
			   0.0),
65
		     Vec4d(0.0, 0.0, 0.0, 1.0));
75
		     Vec4d(0.0, 0.0, 0.0, 1.0));
66
    }
76
    }
67
		
77
		
68
	/// Create an identity quaternion
78
	/// Create an identity quaternion
69
  inline Quatd identity_Quatd()
79
  inline Quatd identity_Quatd()
70
  {
80
  {
71
    return Quatd(Vec3d(0.0));
81
    return Quatd(Vec3d(0.0));
72
  }
82
  }
73
 
83
 
74
	};
84
	};
75
 
85
 
76
}
86
}
77
#endif
87
#endif
78
 
88