rev |
line source |
nuclear@10
|
1 /*
|
nuclear@10
|
2 libvmath - a vector math library
|
nuclear@10
|
3 Copyright (C) 2004-2015 John Tsiombikas <nuclear@member.fsf.org>
|
nuclear@10
|
4
|
nuclear@10
|
5 This program is free software: you can redistribute it and/or modify
|
nuclear@10
|
6 it under the terms of the GNU Lesser General Public License as published
|
nuclear@10
|
7 by the Free Software Foundation, either version 3 of the License, or
|
nuclear@10
|
8 (at your option) any later version.
|
nuclear@10
|
9
|
nuclear@10
|
10 This program is distributed in the hope that it will be useful,
|
nuclear@10
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
nuclear@10
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
nuclear@10
|
13 GNU Lesser General Public License for more details.
|
nuclear@10
|
14
|
nuclear@10
|
15 You should have received a copy of the GNU Lesser General Public License
|
nuclear@10
|
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
|
nuclear@10
|
17 */
|
nuclear@10
|
18 #include "basis.h"
|
nuclear@10
|
19 #include "vmath.h"
|
nuclear@10
|
20
|
nuclear@10
|
21 Basis::Basis()
|
nuclear@10
|
22 {
|
nuclear@10
|
23 i = Vector3(1, 0, 0);
|
nuclear@10
|
24 j = Vector3(0, 1, 0);
|
nuclear@10
|
25 k = Vector3(0, 0, 1);
|
nuclear@10
|
26 }
|
nuclear@10
|
27
|
nuclear@10
|
28 Basis::Basis(const Vector3 &i, const Vector3 &j, const Vector3 &k)
|
nuclear@10
|
29 {
|
nuclear@10
|
30 this->i = i;
|
nuclear@10
|
31 this->j = j;
|
nuclear@10
|
32 this->k = k;
|
nuclear@10
|
33 }
|
nuclear@10
|
34
|
nuclear@10
|
35 Basis::Basis(const Vector3 &dir, bool left_handed)
|
nuclear@10
|
36 {
|
nuclear@10
|
37 k = dir;
|
nuclear@10
|
38 j = Vector3(0, 1, 0);
|
nuclear@10
|
39 i = cross_product(j, k);
|
nuclear@10
|
40 j = cross_product(k, i);
|
nuclear@10
|
41 }
|
nuclear@10
|
42
|
nuclear@10
|
43 /** Rotate with euler angles */
|
nuclear@10
|
44 void Basis::rotate(scalar_t x, scalar_t y, scalar_t z) {
|
nuclear@10
|
45 Matrix4x4 RotMat;
|
nuclear@10
|
46 RotMat.set_rotation(Vector3(x, y, z));
|
nuclear@10
|
47 i.transform(RotMat);
|
nuclear@10
|
48 j.transform(RotMat);
|
nuclear@10
|
49 k.transform(RotMat);
|
nuclear@10
|
50 }
|
nuclear@10
|
51
|
nuclear@10
|
52 /** Rotate by axis-angle specification */
|
nuclear@10
|
53 void Basis::rotate(const Vector3 &axis, scalar_t angle) {
|
nuclear@10
|
54 Quaternion q;
|
nuclear@10
|
55 q.set_rotation(axis, angle);
|
nuclear@10
|
56 i.transform(q);
|
nuclear@10
|
57 j.transform(q);
|
nuclear@10
|
58 k.transform(q);
|
nuclear@10
|
59 }
|
nuclear@10
|
60
|
nuclear@10
|
61 /** Rotate with a 4x4 matrix */
|
nuclear@10
|
62 void Basis::rotate(const Matrix4x4 &mat) {
|
nuclear@10
|
63 i.transform(mat);
|
nuclear@10
|
64 j.transform(mat);
|
nuclear@10
|
65 k.transform(mat);
|
nuclear@10
|
66 }
|
nuclear@10
|
67
|
nuclear@10
|
68 /** Rotate with a quaternion */
|
nuclear@10
|
69 void Basis::rotate(const Quaternion &quat) {
|
nuclear@10
|
70 i.transform(quat);
|
nuclear@10
|
71 j.transform(quat);
|
nuclear@10
|
72 k.transform(quat);
|
nuclear@10
|
73 }
|
nuclear@10
|
74
|
nuclear@10
|
75 /** Extract a rotation matrix from the orthogonal basis */
|
nuclear@10
|
76 Matrix3x3 Basis::create_rotation_matrix() const {
|
nuclear@10
|
77 return Matrix3x3( i.x, j.x, k.x,
|
nuclear@10
|
78 i.y, j.y, k.y,
|
nuclear@10
|
79 i.z, j.z, k.z);
|
nuclear@10
|
80 }
|