xcal
基于 C++23 的现代图形渲染引擎
载入中...
搜索中...
未找到
function.cc
浏览该文件的文档.
1#include <gtest/gtest.h>
2
3#include <xcmath/xcmath.hpp>
4
5using namespace xcmath;
6
7TEST(Function, RadiansAndDegrees) {
8 // 测试 radians 函数
9 EXPECT_DOUBLE_EQ(xcmath::radians(180.0), xcmath::PI);
10 EXPECT_DOUBLE_EQ(xcmath::radians(90.0), xcmath::PI / 2.0);
11 EXPECT_DOUBLE_EQ(xcmath::radians(0.0), 0.0);
12
13 // 测试 degrees 函数
14 EXPECT_DOUBLE_EQ(xcmath::degrees(xcmath::PI), 180.0);
15 EXPECT_DOUBLE_EQ(xcmath::degrees(xcmath::PI / 2.0), 90.0);
16 EXPECT_DOUBLE_EQ(xcmath::degrees(0.0), 0.0);
17}
18
19TEST(Function, Rotate) {
20 // 测试 2D 旋转矩阵
22 xcmath::mat<float, 3, 3> rotated = xcmath::rotate(m, 90.0f);
23 xcmath::mat<float, 3, 3> expected{{(float)cos(xcmath::radians(90.f)), -1.0f, 0.0f},
24 {1.0f, (float)cos(xcmath::radians(90.0f)), 0.0f},
25 {0.0f, 0.0f, 1.0f}};
26 for (size_t i = 0; i < 3; ++i) {
27 for (size_t j = 0; j < 3; ++j) {
28 EXPECT_FLOAT_EQ(rotated[i][j], expected[i][j]);
29 }
30 }
31
32 // 测试 3D 旋转矩阵
34 xcmath::vec3<float> axis(0.0f, 0.0f, 1.0f);
35 xcmath::mat<float, 4, 4> rotated3d = xcmath::rotate(m3d, 90.0f, axis);
36 xcmath::mat<float, 4, 4> expected3d{
37 {(float)cos(xcmath::radians(90.0f)), -1.0f, 0.0f, 0.0f},
38 {1.0f, (float)cos(xcmath::radians(90.0f)), 0.0f, 0.0f},
39 {0.0f, 0.0f, 1.0f, 0.0f},
40 {0.0f, 0.0f, 0.0f, 1.0f}};
41 for (size_t i = 0; i < 4; ++i) {
42 for (size_t j = 0; j < 4; ++j) {
43 EXPECT_FLOAT_EQ(rotated3d[i][j], expected3d[i][j]);
44 }
45 }
46}
47
48TEST(Function, Translate) {
50 xcmath::vec3<float> v(1.0f, 2.0f, 3.0f);
52 xcmath::mat<float, 4, 4> expected{{1, 0, 0, 1}, //
53 {0, 1, 0, 2},
54 {0, 0, 1, 3},
55 {0, 0, 0, 1}};
56 for (size_t i = 0; i < 4; ++i) {
57 for (size_t j = 0; j < 4; ++j) {
58 EXPECT_FLOAT_EQ(translated[i][j], expected[i][j]);
59 }
60 }
61 auto m1 = xcmath::mat3f::eye();
62 const xcmath::vec3f v1(1.0f, 2.0f);
63 auto translated1 = xcmath::translate(m, v);
64 xcmath::mat3f expected1{{1, 0, 1}, //
65 {0, 1, 2},
66 {0, 0, 1}};
67 for (size_t i = 0; i < 3; ++i) {
68 for (size_t j = 0; j < 3; ++j) {
69 EXPECT_FLOAT_EQ(translated[i][j], expected[i][j]);
70 }
71 }
72}
73
74TEST(Function, Scale) {
76 xcmath::vec3<float> v(2.0f, 3.0f, 4.0f);
78 xcmath::mat<float, 4, 4> expected{{2, 0, 0, 0}, //
79 {0, 3, 0, 0},
80 {0, 0, 4, 0},
81 {0, 0, 0, 1}};
82 for (size_t i = 0; i < 4; ++i) {
83 for (size_t j = 0; j < 4; ++j) {
84 EXPECT_FLOAT_EQ(scaled[i][j], expected[i][j]);
85 }
86 }
87}
88
89int main(int argc, char **argv) {
90 testing::InitGoogleTest(&argc, argv);
91 return RUN_ALL_TESTS();
92}
Matrix class template
Definition mat.hpp:27
static constexpr mat< _Tp, _rows, _cols > eye()
Create an identity matrix
Definition mat.hpp:168
Vector class template
Definition vec.hpp:206
int main()
Definition axis.cc:9
TEST(Function, RadiansAndDegrees)
Definition function.cc:7
Compiler-specific type information handling for MSVC
Definition complex.hpp:12
constexpr long double PI
Pi constant
constexpr T radians(T degrees)
Convert degrees to radians
Definition function.hpp:25
constexpr mat< _Tp, _len, _len > translate(const mat< _Tp, _len, _len > &m, const vec< _Tp, _vlen > &v)
Apply translation for matrix
Definition function.hpp:125
constexpr T degrees(T radians)
Convert radians to degrees
Definition function.hpp:37
mat< _Tp, _dim, _dim > rotate(const mat< _Tp, _dim, _dim > &m, _Tp angle, const vec< _Tp, _length > &axis)
Apply rotation for transform or rotation matrix around an axis
Definition function.hpp:54
constexpr mat< _Tp, _dim, _dim > scale(const mat< _Tp, _dim, _dim > &m, const vec< _ATp, _dim - 1 > &v)
Apply translation to a matrix
Definition function.hpp:160