aboutsummaryrefslogtreecommitdiff
path: root/src/vec4ops/rotations4d.cpp
blob: 1ff43a7a1cfab04167492e560209e7bbb8022353 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "raytracer/raytracer.h"
#include "vec4ops/vec4ops.h"

glm::mat4 Vec4Ops::getRotationMatrix4XY(
        float angleRadians) {
    glm::mat4 result;
    result[0][0] = cos(angleRadians);
    result[0][1] = -sin(angleRadians);
    result[1][0] = sin(angleRadians);
    result[1][1] = cos(angleRadians);
    result[2][2] = 1;
    result[3][3] = 1;
    return result;
}

glm::mat4 Vec4Ops::getRotationMatrix4YZ(
        float angleRadians) {
    glm::mat4 result;
    result[1][1] = cos(angleRadians);
    result[1][2] = -sin(angleRadians);
    result[2][1] = sin(angleRadians);
    result[2][2] = cos(angleRadians);
    result[0][0] = 1;
    result[3][3] = 1;
    return result;
}

glm::mat4 Vec4Ops::getRotationMatrix4ZX(
        float angleRadians) {
    glm::mat4 result;
    result[2][2] = cos(angleRadians);
    result[2][0] = -sin(angleRadians);
    result[0][2] = sin(angleRadians);
    result[0][0] = cos(angleRadians);
    result[1][1] = 1;
    result[3][3] = 1;
    return result;
}

glm::mat4 Vec4Ops::getRotationMatrix4XW(
        float angleRadians) {
    glm::mat4 result;
    result[0][0] = cos(angleRadians);
    result[0][3] = -sin(angleRadians);
    result[3][0] = sin(angleRadians);
    result[3][3] = cos(angleRadians);
    result[1][1] = 1;
    result[2][2] = 1;
    return result;
}

glm::mat4 Vec4Ops::getRotationMatrix4YW(
        float angleRadians) {
    glm::mat4 result;
    result[1][1] = cos(angleRadians);
    result[1][3] = -sin(angleRadians);
    result[3][1] = sin(angleRadians);
    result[3][3] = cos(angleRadians);
    result[0][0] = 1;
    result[2][2] = 1;
    return result;
}

glm::mat4 Vec4Ops::getRotationMatrix4ZW(
        float angleRadians) {
    glm::mat4 result;
    result[2][2] = cos(angleRadians);
    result[2][3] = -sin(angleRadians);
    result[3][2] = sin(angleRadians);
    result[3][3] = cos(angleRadians);
    result[0][0] = 1;
    result[1][1] = 1;
    return result;
}

glm::mat4 Vec4Ops::getRotationMatrix4(
        std::vector<float> anglesRadians)
{
    // make the identity
    glm::mat4 result = glm::mat4(0.f);
    result[0][0] = 1.f;
    result[1][1] = 1.f;
    result[2][2] = 1.f;
    result[3][3] = 1.f;

    // apply the rotations, starting with the last angle, which corresponds to ZW
    if (anglesRadians.size() != 6) {
        throw std::runtime_error("invalid rotation angle array");
    }
    for (int i = 5; i >= 0; i--) {
        switch (i) {
            case 0:
                result *= getRotationMatrix4XY(anglesRadians[i]) * result;
                break;
            case 1:
                result *= getRotationMatrix4ZX(anglesRadians[i]) * result;
                break;
            case 2:
                result *= getRotationMatrix4YZ(anglesRadians[i]) * result;
                break;
            case 3:
                result *= getRotationMatrix4XW(anglesRadians[i]) * result;
                break;
            case 4:
                result *= getRotationMatrix4YW(anglesRadians[i]) * result;
                break;
            case 5:
                result *= getRotationMatrix4ZW(anglesRadians[i]) * result;
                break;
            default:
                throw std::runtime_error("invalid rotation matrix");
        }
    }

    return result;
}