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