-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathaffine_transform.h
146 lines (129 loc) · 4.7 KB
/
affine_transform.h
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
* affine_transform.h
*
* By Sebastian Raaphorst, 2018.
*/
#pragma once
#include <array>
#include "matrix.h"
#include "vec.h"
namespace raytracer {
using Transformation = SquareMatrix<4>;
constexpr Transformation notransform() {
return predefined_matrices::I<double, 4>;
}
constexpr Transformation translation(double x, double y, double z) {
std::array<double, 3> point{x, y, z};
Transformation::matrix_type m{};
for (size_t i = 0; i < 4; ++i)
for (size_t j = 0; j < 4; ++j)
if (i == j)
m[i][j] = 1;
else if (j == 3)
m[i][3] = point[i];
else
m[i][j] = 0;
return Transformation{m};
}
constexpr Transformation scale(double x, double y, double z) {
Transformation::matrix_type m{};
for (size_t i = 0; i < 4; ++i)
for (size_t j = 0; j < 4; ++j)
m[i][j] = 0;
m[0][0] = x;
m[1][1] = y;
m[2][2] = z;
m[3][3] = 1;
return Transformation{m};
}
constexpr Transformation rotation_x(double theta) {
Transformation::matrix_type m{};
for (size_t i = 0; i < 4; ++i)
for (size_t j = 0; j < 4; ++j)
if ((i == 1 && j == 1) || (i == 2 && j == 2))
m[i][j] = const_cos(theta);
else if (i == 1 && j == 2)
m[1][2] = -const_sin(theta);
else if (i == 2 && j == 1)
m[2][1] = const_sin(theta);
else if (i == j)
m[i][j] = 1;
else
m[i][j] = 0;
return Transformation{m};
}
constexpr Transformation rotation_y(double theta) {
Transformation::matrix_type m{};
for (size_t i = 0; i < 4; ++i)
for (size_t j = 0; j < 4; ++j)
if ((i == 0 && j == 0) || (i == 2 && j == 2))
m[i][j] = const_cos(theta);
else if (i == 0 && j == 2)
m[i][j] = const_sin(theta);
else if (i == 2 && j == 0)
m[i][j] = -const_sin(theta);
else if (i == j)
m[i][j] = 1;
else m[i][j] = 0;
return Transformation{m};
}
constexpr Transformation rotation_z(double theta) {
Transformation::matrix_type m{};
for (size_t i = 0; i < 4; ++i)
for (size_t j = 0; j < 4; ++j)
if ((i == 0 && j == 0) || (i == 1 && j == 1))
m[i][j] = const_cos(theta);
else if (i == 0 && j == 1)
m[i][j] = -const_sin(theta);
else if (i == 1 && j == 0)
m[i][j] = const_sin(theta);
else if (i == j)
m[i][j] = 1;
else m[i][j] = 0;
return Transformation{m};
}
constexpr Transformation skew(double x_y, double x_z, double y_x, double y_z, double z_x, double z_y) {
Transformation::matrix_type m{};
for (size_t i = 0; i < 4; ++i)
for (size_t j = 0; j < 4; ++j)
if (i == 0 && j == 1)
m[i][j] = x_y;
else if (i == 0 && j == 2)
m[i][j] = x_z;
else if (i == 1 && j == 0)
m[i][j] = y_x;
else if (i == 1 && j == 2)
m[i][j] = y_z;
else if (i == 2 && j == 0)
m[i][j] = z_x;
else if (i == 2 && j == 1)
m[i][j] = z_y;
else if (i == j)
m[i][j] = 1;
else m[i][j] = 0;
return Transformation{m};
}
constexpr Transformation view_transform(const Tuple &from, const Tuple &to, const Tuple &upv) {
Transformation::matrix_type m{};
const auto forward = (to - from).normalize();
const auto upn = upv.normalize();
const auto left = forward.cross_product(upn);
const auto true_up = left.cross_product(forward);
// Set the rightmost column and bottom-most row to (0, 0, 0, 1).
for (size_t i = 0; i < 3; ++i) {
m[i][3] = 0;
m[3][i] = 0;
}
m[3][3] = 1;
// Set the rest of the matrix.
for (size_t i = 0; i < 3; ++i) {
m[0][i] = left[i];
m[1][i] = true_up[i];
m[2][i] = -forward[i];
}
const auto trans = translation(-from[0], -from[1], -from[2]);
const auto transm = Transformation{m};
const auto result = transm * trans;
return Transformation{m} * translation(-from[0], -from[1], -from[2]);
}
}