author | Radek Brich <radek.brich@devl.cz> |
Mon, 17 Dec 2007 22:03:50 +0100 | |
branch | pyrit |
changeset 40 | 929aad02c5f2 |
parent 34 | 28f6e8b9d5d1 |
child 44 | 3763b26244f0 |
permissions | -rw-r--r-- |
34
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
1 |
/* |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
2 |
* Pyrit Ray Tracer |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
3 |
* file: quaternion.h |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
4 |
* |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
5 |
* Radek Brich, 2006-2007 |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
6 |
*/ |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
7 |
|
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
8 |
#ifndef QUATERNION_H |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
9 |
#define QUATER_H |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
10 |
|
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
11 |
class Quaternion |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
12 |
{ |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
13 |
public: |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
14 |
union { |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
15 |
struct { |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
16 |
Float a,b,c,d; |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
17 |
}; |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
18 |
struct { |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
19 |
Float t,x,y,z; |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
20 |
}; |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
21 |
}; |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
22 |
Quaternion(): a(0), b(0), c(0), d(0) {}; |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
23 |
Quaternion(const Float aa, const Float ab, const Float ac, const Float ad): |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
24 |
a(aa), b(ab), c(ac), d(ad) {}; |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
25 |
Quaternion(const Vector3& v): a(1), b(v.x), c(v.y), d(v.z) {}; |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
26 |
|
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
27 |
Vector3 toVector() { return Vector3(b/a, c/a, d/a); }; |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
28 |
|
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
29 |
Quaternion normalize() |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
30 |
{ |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
31 |
Float f = 1.0f / sqrtf(a * a + b * b + c * c + d * d); |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
32 |
a *= f; |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
33 |
b *= f; |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
34 |
c *= f; |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
35 |
d *= f; |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
36 |
return *this; |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
37 |
}; |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
38 |
Float mag2() const |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
39 |
{ |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
40 |
return (a*a + b*b + c*c + d*d); |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
41 |
}; |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
42 |
Float mag() const |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
43 |
{ |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
44 |
return sqrtf(mag()); |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
45 |
}; |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
46 |
friend Quaternion operator*(const Quaternion &q1, const Quaternion &q2) |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
47 |
{ |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
48 |
return Quaternion( |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
49 |
q1.a*q2.a - q1.b*q2.b - q1.c*q2.c - q1.d*q2.d, |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
50 |
q1.a*q2.b + q1.b*q2.a + q1.c*q2.d - q1.d*q2.c, |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
51 |
q1.a*q2.c - q1.b*q2.d + q1.c*q2.a + q1.d*q2.b, |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
52 |
q1.a*q2.d + q1.b*q2.c - q1.c*q2.b + q1.d*q2.a); |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
53 |
}; |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
54 |
friend Float dot(const Quaternion &q1, const Quaternion &q2) |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
55 |
{ |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
56 |
return q1.a*q2.a + q1.b*q2.b + q1.c*q2.c + q1.d*q2.d; |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
57 |
}; |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
58 |
friend Quaternion conjugate(const Quaternion &q) |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
59 |
{ |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
60 |
return Quaternion(q.a, -q.b, -q.c, -q.d); |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
61 |
}; |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
62 |
}; |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
63 |
|
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
64 |
#endif |