Blender V5.3
util_math_quaternion_test.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2026 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5/* Note: These fixtures test default micro-architecture optimization defined in the
6 * util/optimization.h. */
7
9
10#include <gmock/gmock.h>
11#include <gtest/gtest.h>
12
13#include "util/log.h"
14#include "util/transform.h"
15
16#include <iostream>
17
19
20using testing::Not;
21
22/* Use as:
23 * Quaternion q1;
24 * Quaternion q2;
25 * EXPECT_THAT(q1, IsNearQuaternion(q2, eps)); */
26MATCHER_P2(IsNearQuaternion, expected, eps, "")
27{
28 if (fabsf(arg.w - expected.w) > eps) {
29 *result_listener << "component w should be " << expected.w;
30 return false;
31 }
32 if (fabsf(arg.x - expected.x) > eps) {
33 *result_listener << "component x should be " << expected.x;
34 return false;
35 }
36 if (fabsf(arg.y - expected.y) > eps) {
37 *result_listener << "component y should be " << expected.y;
38 return false;
39 }
40 if (fabsf(arg.z - expected.z) > eps) {
41 *result_listener << "component z should be " << expected.z;
42 return false;
43 }
44 return true;
45}
46
47/* TODO(sergey): Move to log.h? */
48std::ostream &operator<<(std::ostream &os, const Quaternion &q)
49{
50 os << "(" << q.w << ", " << q.x << ", " << q.y << ", " << q.z << ")";
51 return os;
52}
53
54class QuaternionTest : public ::testing::Test {
55 void SetUp() override
56 {
57 /* The micro-architecture check is not needed here, but use it here as a demonstration of how
58 * it can be implemented in a clear way. */
59 // GTEST_SKIP() << "Test skipped due to uarch capability";
60 }
61};
62
63/* Basic test for user-defined matcher. */
64TEST_F(QuaternionTest, IsNearQuaternion)
65{
66 const Quaternion identity = identity_quaternion();
67 const Quaternion other = make_quaternion(1.0f, 2.0f, 3.0f, 4.0f);
68
69 EXPECT_THAT(identity, IsNearQuaternion(identity, 1e-6f));
70 EXPECT_THAT(other, Not(IsNearQuaternion(identity, 1e-6f)));
71}
72
74{
75 EXPECT_THAT(-make_quaternion(1.0f, -2.0f, 3.0f, -4.0f),
76 IsNearQuaternion(make_quaternion(-1.0f, 2.0f, -3.0f, 4.0), 1e-6f));
77}
78
79TEST_F(QuaternionTest, MultiplyScalar)
80{
81 EXPECT_THAT(make_quaternion(1.0f, -2.0f, 3.0f, -4.0f) * 0.5f,
82 IsNearQuaternion(make_quaternion(0.5f, -1.0f, 1.5f, -2.0f), 1e-6f));
83 EXPECT_THAT(0.5f * make_quaternion(1.0f, -2.0f, 3.0f, -4.0f),
84 IsNearQuaternion(make_quaternion(0.5f, -1.0f, 1.5f, -2.0f), 1e-6f));
85}
86
88{
89 EXPECT_THAT(make_quaternion(1.0f, -2.0f, 3.0f, -4.0f) +
90 make_quaternion(0.1f, -0.2f, 0.3f, -0.4f),
91 IsNearQuaternion(make_quaternion(1.1f, -2.2f, 3.3f, -4.4f), 1e-6f));
92
93 {
94 Quaternion a = make_quaternion(1.0f, -2.0f, 3.0f, -4.0f);
95 a += make_quaternion(0.1f, -0.2f, 0.3f, -0.4f);
96 EXPECT_THAT(a, IsNearQuaternion(make_quaternion(1.1f, -2.2f, 3.3f, -4.4f), 1e-6f));
97 }
98}
99
101{
102 EXPECT_THAT(make_quaternion(1.1f, -2.2f, 3.3f, -4.4f) -
103 make_quaternion(0.1f, -0.2f, 0.3f, -0.4f),
104 IsNearQuaternion(make_quaternion(1.0f, -2.0f, 3.0f, -4.0f), 1e-6f));
105
106 {
107 Quaternion a = make_quaternion(1.1f, -2.2f, 3.3f, -4.4f);
108 a -= make_quaternion(0.1f, -0.2f, 0.3f, -0.4f);
109 EXPECT_THAT(a, IsNearQuaternion(make_quaternion(1.0f, -2.0f, 3.0f, -4.0f), 1e-6f));
110 }
111}
112
114{
116 dot(make_quaternion(1.0f, -2.0f, 3.0f, -4.0f), make_quaternion(0.1f, -0.2f, 0.3f, -0.4f)),
117 3.0f,
118 1e-6f);
119}
120
122{
123 EXPECT_THAT(normalized(make_quaternion(1.0f, 0.0f, 0.0f, 0.0f)),
124 IsNearQuaternion(make_quaternion(1.0f, 0.0f, 0.0f, 0.0f), 1e-6f));
125
126 EXPECT_THAT(
127 normalized(make_quaternion(1.2f, 2.3f, 3.4f, 4.5f)),
128 IsNearQuaternion(make_quaternion(0.193297f, 0.370486f, 0.547675f, 0.724864f), 1e-5f));
129}
130
132{
133 EXPECT_THAT(
134 lerp(make_quaternion(1.2f, 2.3f, 3.4f, 4.5f), make_quaternion(6.7f, 8.9f, 1.0f, 2.3f), 0.2f),
135 IsNearQuaternion(make_quaternion(0.349109f, 0.549467f, 0.443217f, 0.616253f), 1e-5f));
136 EXPECT_THAT(
137 lerp(
138 make_quaternion(1.2f, 2.3f, 3.4f, 4.5f), -make_quaternion(6.7f, 8.9f, 1.0f, 2.3f), 0.2f),
139 IsNearQuaternion(make_quaternion(0.349109f, 0.549467f, 0.443217f, 0.616253f), 1e-5f));
140}
141
static btMatrix3x3 Add(const btMatrix3x3 &a, const btMatrix3x3 &b)
#define CCL_NAMESPACE_END
std::ostream & operator<<(std::ostream &os, DeviceKernel kernel)
TEST_F(FFmpegTest, libaom_av1_AV_PIX_FMT_YUV420P)
float dot(VecOp< float, D >, VecOp< float, D >) RET
#define EXPECT_NEAR(result, expect, threshold)
ccl_device_inline Quaternion normalized(const Quaternion q)
const btScalar eps
Definition poly34.cpp:11
#define fabsf
#define lerp(a, b, t)
Definition smaa.cc:318
ccl_device_inline Quaternion identity_quaternion()
ccl_device_inline Quaternion make_quaternion(const float w, const float x, const float y, const float z)
MATCHER_P2(IsNearFloat3, expected, eps, "")