Subversion Repositories gelsvn

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 bj 1
#include "Vec3f.h"
2
#include "Quaternion.h"
3
 
4
namespace CGLA {
5
	void Quaternion::make_rot(float angle, const Vec3f& v)
6
	{
7
		angle = angle/2;
8
		qv = CGLA::normalize(v);
9
		qv *= sin(angle);
10
		qw  = cos(angle);
11
	};
12
 
13
	void Quaternion::make_rot(const Vec3f& _v0, const Vec3f& _v1)
14
	{
15
		Vec3f v0 = CGLA::normalize(_v0);
16
		Vec3f v1 = CGLA::normalize(_v1);
17
		qv = cross(v0, v1);
18
		float l = qv.length();
19
		if(l<TINY)
20
			qv = Vec3f(1,0,0);
21
		else
22
			qv.normalize();
23
		float a = acos(dot(v0,v1))/2;
24
		qw  = cos(a);
25
		qv *= sin(a);	
26
	};
27
 
28
	void Quaternion::get_rot(float& angle, Vec3f& v) 
29
	{
30
		angle=2*acos(qw);
31
 
32
		if (angle<TINY) 
33
			v = Vec3f(1,0,0);
34
		else 
35
			v = qv / sin(angle);
36
 
37
		if (angle>M_PI)
38
			v = -v;
39
 
40
		v.normalize();
41
	}
42
 
43
	Mat3x3f Quaternion::get_mat3x3f() const
44
	{
45
		float s=2/norm();
46
		float m[9] = {1 - s*(qv[1]*qv[1]+qv[2]*qv[2]), 
47
									s*(qv[0]*qv[1]-qw*qv[2]), 
48
									s*(qv[0]*qv[2]+qw*qv[1]), 
49
									s*(qv[0]*qv[1]+qw*qv[2]), 
50
									1 - s*(qv[0]*qv[0]+qv[2]*qv[2]), 
51
         s*(qv[1]*qv[2]-qw*qv[0]), 
52
									s*(qv[0]*qv[2]-qw*qv[1]), 
53
									s*(qv[1]*qv[2]+qw*qv[0]), 
54
									1 - s*(qv[0]*qv[0]+qv[1]*qv[1])};
55
		Mat3x3f mat;
56
		mat.set(m);
57
		return mat;
58
	}
59
 
60
 
61
	//This function just need to call the right initialiser
62
 
63
	Mat4x4f Quaternion::get_mat4x4f() const
64
	{
65
		float s=2/norm();
66
		float m[16] = {1 - s*(qv[1]*qv[1]+qv[2]*qv[2]), 
67
									 s*(qv[0]*qv[1]-qw*qv[2]), 
68
									 s*(qv[0]*qv[2]+qw*qv[1]), 
69
									 float(0),
70
									 s*(qv[0]*qv[1]+qw*qv[2]), 
71
									 1 - s*(qv[0]*qv[0]+qv[2]*qv[2]), 
72
									 s*(qv[1]*qv[2]-qw*qv[0]), 
73
									 float(0),
74
									 s*(qv[0]*qv[2]-qw*qv[1]), 
75
									 s*(qv[1]*qv[2]+qw*qv[0]), 
76
									 1 - s*(qv[0]*qv[0]+qv[1]*qv[1]), 
77
									 float(0),
78
									 float(0),             
79
									 float(0),                   
80
									 float(0),                
81
									 float(1)};
82
		Mat4x4f mat;
83
		mat.set(m);
84
		return mat;
85
	}
86
 
87
 
88
	Vec3f Quaternion::apply(const Vec3f& vec) const
89
	{
90
		return Vec3f((*this)*Quaternion(vec)*inverse());
91
	}
92
 
93
 
94
}