Subversion Repositories gelsvn

Rev

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

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