author | Radek Brich <radek.brich@devl.cz> |
Mon, 14 Apr 2008 12:51:50 +0200 | |
branch | pyrit |
changeset 69 | 303583d2fb97 |
parent 60 | a23b5089b9c3 |
child 91 | 9d66d323c354 |
permissions | -rw-r--r-- |
34
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
1 |
/* |
44 | 2 |
* quaternion.h: Quaternion class |
3 |
* |
|
4 |
* This file is part of Pyrit Ray Tracer. |
|
5 |
* |
|
6 |
* Copyright 2007 Radek Brich |
|
34
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
7 |
* |
44 | 8 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
9 |
* of this software and associated documentation files (the "Software"), to deal |
|
10 |
* in the Software without restriction, including without limitation the rights |
|
11 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
12 |
* copies of the Software, and to permit persons to whom the Software is |
|
13 |
* furnished to do so, subject to the following conditions: |
|
14 |
* |
|
15 |
* The above copyright notice and this permission notice shall be included in |
|
16 |
* all copies or substantial portions of the Software. |
|
17 |
* |
|
18 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
19 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
20 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
21 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
22 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
23 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
24 |
* THE SOFTWARE. |
|
34
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
25 |
*/ |
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 |
#ifndef QUATERNION_H |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
28 |
#define QUATER_H |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
29 |
|
46 | 30 |
/** |
31 |
* helper quaternion class |
|
32 |
*/ |
|
34
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
33 |
class Quaternion |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
34 |
{ |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
35 |
public: |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
36 |
union { |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
37 |
struct { |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
38 |
Float a,b,c,d; |
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 |
struct { |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
41 |
Float t,x,y,z; |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
42 |
}; |
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 |
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
|
45 |
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
|
46 |
a(aa), b(ab), c(ac), d(ad) {}; |
44 | 47 |
Quaternion(const Vector3& v): a(0), b(v.x), c(v.y), d(v.z) {}; |
34
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
48 |
|
44 | 49 |
Vector3 toVector() { return Vector3(b, c, d); }; |
34
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
50 |
|
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
51 |
Quaternion normalize() |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
52 |
{ |
60
a23b5089b9c3
moving to SCons build system
Radek Brich <radek.brich@devl.cz>
parents:
46
diff
changeset
|
53 |
Float f = (Float)1.0f / sqrtf(a * a + b * b + c * c + d * d); |
34
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
54 |
a *= f; |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
55 |
b *= f; |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
56 |
c *= f; |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
57 |
d *= f; |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
58 |
return *this; |
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 |
Float mag2() const |
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 |
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
|
63 |
}; |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
64 |
Float mag() const |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
65 |
{ |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
66 |
return sqrtf(mag()); |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
67 |
}; |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
68 |
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
|
69 |
{ |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
70 |
return Quaternion( |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
71 |
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
|
72 |
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
|
73 |
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
|
74 |
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
|
75 |
}; |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
76 |
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
|
77 |
{ |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
78 |
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
|
79 |
}; |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
80 |
friend Quaternion conjugate(const Quaternion &q) |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
81 |
{ |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
82 |
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
|
83 |
}; |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
84 |
}; |
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
85 |
|
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
86 |
#endif |