Subversion Repositories gelsvn

Rev

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