Blender V5.0
BLI_math_vector_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "testing/testing.h"
6
7#include "BLI_math_vector.h"
8#include "BLI_math_vector.hh"
9#include "BLI_vector.hh"
10
11namespace blender::tests {
12
13TEST(math_vector, ClampVecWithFloats)
14{
15 const float min = 0.0f;
16 const float max = 1.0f;
17
18 float a[2] = {-1.0f, -1.0f};
19 clamp_v2(a, min, max);
20 EXPECT_FLOAT_EQ(0.0f, a[0]);
21 EXPECT_FLOAT_EQ(0.0f, a[1]);
22
23 float b[2] = {0.5f, 0.5f};
24 clamp_v2(b, min, max);
25 EXPECT_FLOAT_EQ(0.5f, b[0]);
26 EXPECT_FLOAT_EQ(0.5f, b[1]);
27
28 float c[2] = {2.0f, 2.0f};
29 clamp_v2(c, min, max);
30 EXPECT_FLOAT_EQ(1.0f, c[0]);
31 EXPECT_FLOAT_EQ(1.0f, c[1]);
32}
33
34TEST(math_vector, test_invert_v3_safe)
35{
36 float v3_with_zeroes[3] = {0.0f, 2.0f, 3.0f};
37 invert_v3_safe(v3_with_zeroes);
38 EXPECT_FLOAT_EQ(0.0f, v3_with_zeroes[0]);
39 EXPECT_FLOAT_EQ(0.5f, v3_with_zeroes[1]);
40 EXPECT_FLOAT_EQ(0.33333333333f, v3_with_zeroes[2]);
41
42 float v3_without_zeroes[3] = {1.0f, 2.0f, 3.0f};
43 float inverted_unsafe[3] = {1.0f, 2.0f, 3.0f};
44 invert_v3_safe(v3_without_zeroes);
45 invert_v3(inverted_unsafe);
46
47 EXPECT_FLOAT_EQ(inverted_unsafe[0], v3_without_zeroes[0]);
48 EXPECT_FLOAT_EQ(inverted_unsafe[1], v3_without_zeroes[1]);
49 EXPECT_FLOAT_EQ(inverted_unsafe[2], v3_without_zeroes[2]);
50}
51
52TEST(math_vector, Clamp)
53{
54 const int3 value(0, 100, -100);
55 const int3 min(5, 40, -95);
56 const int3 max(7, 45, 5);
57
58 const int3 result = math::clamp(value, min, max);
59 EXPECT_EQ(result.x, 5);
60 EXPECT_EQ(result.y, 45);
61 EXPECT_EQ(result.z, -95);
62
63 const int3 result_2 = math::clamp(value, -50, 50);
64 EXPECT_EQ(result_2.x, 0);
65 EXPECT_EQ(result_2.y, 50);
66 EXPECT_EQ(result_2.z, -50);
67}
68
69TEST(math_vector, MinList)
70{
71 EXPECT_EQ(float3(1.0, 2.0, 3.0), math::min({float3(1.0, 2.0, 3.0)}));
72 EXPECT_EQ(float3(0.0, 2.0, 2.0), math::min({float3(1.0, 2.0, 3.0), float3(0.0, 5.0, 2.0)}));
73 EXPECT_EQ(float3(0.0, 2.0, 1.5),
74 math::min({float3(1.0, 2.0, 3.0), float3(0.0, 5.0, 2.0), float3(2.0, 4.0, 1.5)}));
75
76 const float inf = std::numeric_limits<float>::infinity();
77 EXPECT_EQ(float3(0.0, -inf, -inf),
78 math::min({float3(inf, 2.0, 3.0), float3(0.0, -inf, inf), float3(2.0, 4.0, -inf)}));
79
80 const float nan = std::numeric_limits<float>::quiet_NaN();
81 const float3 result = math::min(
82 {float3(nan, 2.0, 3.0), float3(0.0, nan, 2.0), float3(2.0, 4.0, nan)});
83 EXPECT_TRUE(std::isnan(result.x));
84 EXPECT_EQ(result.y, 2.0);
85 EXPECT_EQ(result.z, 2.0);
86}
87
88TEST(math_vector, MaxList)
89{
90 EXPECT_EQ(float3(1.0, 2.0, 3.0), math::max({float3(1.0, 2.0, 3.0)}));
91 EXPECT_EQ(float3(1.0, 5.0, 3.0), math::max({float3(1.0, 2.0, 3.0), float3(0.0, 5.0, 2.0)}));
92 EXPECT_EQ(float3(2.0, 5.0, 3.0),
93 math::max({float3(1.0, 2.0, 3.0), float3(0.0, 5.0, 2.0), float3(2.0, 4.0, 1.5)}));
94
95 const float inf = std::numeric_limits<float>::infinity();
96 EXPECT_EQ(float3(inf, 4.0, inf),
97 math::max({float3(inf, 2.0, 3.0), float3(0.0, -inf, inf), float3(2.0, 4.0, -inf)}));
98
99 const float nan = std::numeric_limits<float>::quiet_NaN();
100 const float3 result = math::max(
101 {float3(nan, 2.0, 3.0), float3(0.0, nan, 2.0), float3(2.0, 4.0, nan)});
102 EXPECT_TRUE(std::isnan(result.x));
103 EXPECT_EQ(result.y, 4.0);
104 EXPECT_EQ(result.z, 3.0);
105}
106
107TEST(math_vector, InterpolateInt)
108{
109 const int3 a(0, -100, 50);
110 const int3 b(0, 100, 100);
111 const int3 result = math::interpolate(a, b, 0.75);
112 EXPECT_EQ(result.x, 0);
113 EXPECT_EQ(result.y, 50);
114 EXPECT_EQ(result.z, 87);
115}
116
117TEST(math_vector, InterpolateFloat)
118{
119 const float3 a(40.0f, -100.0f, 50.0f);
120 const float3 b(20.0f, 100.0f, 100.0f);
121 const float3 result = math::interpolate(a, b, 0.5);
122 EXPECT_FLOAT_EQ(result.x, 30.0f);
123 EXPECT_FLOAT_EQ(result.y, 0.0f);
124 EXPECT_FLOAT_EQ(result.z, 75.0f);
125}
126
127TEST(math_vector, CeilToMultiple)
128{
129 const int3 a(21, 16, 0);
130 const int3 b(8, 16, 15);
132 EXPECT_FLOAT_EQ(result.x, 24);
133 EXPECT_FLOAT_EQ(result.y, 16);
134 EXPECT_FLOAT_EQ(result.z, 0);
135}
136
137TEST(math_vector, DivideCeil)
138{
139 const int3 a(21, 16, 0);
140 const int3 b(8, 16, 15);
141 const int3 result = math::divide_ceil(a, b);
142 EXPECT_FLOAT_EQ(result.x, 3);
143 EXPECT_FLOAT_EQ(result.y, 1);
144 EXPECT_FLOAT_EQ(result.z, 0);
145}
146
147TEST(math_vector, Sign)
148{
149 const int3 a(-21, 16, 0);
150 const int3 result = math::sign(a);
151 EXPECT_FLOAT_EQ(result.x, -1);
152 EXPECT_FLOAT_EQ(result.y, 1);
153 EXPECT_FLOAT_EQ(result.z, 0);
154}
155
156TEST(math_vector, sqrt)
157{
158 const float3 a(1.0f, 4.0f, 9.0f);
159 const float3 result = math::sqrt(a);
160 EXPECT_NEAR(result.x, 1.0f, 1e-6f);
161 EXPECT_NEAR(result.y, 2.0f, 1e-6f);
162 EXPECT_NEAR(result.z, 3.0f, 1e-6f);
163}
164
165TEST(math_vector, safe_sqrt)
166{
167 const float3 a(1.0f, -4.0f, 9.0f);
168 const float3 result = math::safe_sqrt(a);
169 EXPECT_NEAR(result.x, 1.0f, 1e-6f);
170 EXPECT_NEAR(result.y, 0.0f, 1e-6f);
171 EXPECT_NEAR(result.z, 3.0f, 1e-6f);
172}
173
174TEST(math_vector, rcp)
175{
176 const float3 a(1.0f, 2.0f, 4.0f);
177 const float3 result = math::rcp(a);
178 EXPECT_NEAR(result.x, 1.0f, 1e-6f);
179 EXPECT_NEAR(result.y, 0.5f, 1e-6f);
180 EXPECT_NEAR(result.z, 0.25f, 1e-6f);
181}
182
183TEST(math_vector, safe_rcp)
184{
185 const float3 a(1.0f, 0.0f, 4.0f);
186 const float3 result = math::safe_rcp(a);
187 EXPECT_NEAR(result.x, 1.0f, 1e-6f);
188 EXPECT_NEAR(result.y, 0.0f, 1e-6f);
189 EXPECT_NEAR(result.z, 0.25f, 1e-6f);
190}
191
192TEST(math_vector, exp)
193{
194 const float3 a(1.0f, 2.0f, 3.0f);
195 const float3 result = math::exp(a);
196 EXPECT_NEAR(result.x, 2.718281828459045f, 1e-6f);
197 EXPECT_NEAR(result.y, 7.38905609893065f, 1e-6f);
198 EXPECT_NEAR(result.z, 20.085536923187668f, 1e-6f);
199}
200
201TEST(math_vector, square)
202{
203 const float3 a(1.0f, 2.0f, 3.0f);
204 const float3 result = math::square(a);
205 EXPECT_NEAR(result.x, 1.0f, 1e-6f);
206 EXPECT_NEAR(result.y, 4.0f, 1e-6f);
207 EXPECT_NEAR(result.z, 9.0f, 1e-6f);
208}
209
210} // namespace blender::tests
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
MINLINE void invert_v3_safe(float r[3])
MINLINE void clamp_v2(float vec[2], float min, float max)
MINLINE void invert_v3(float r[3])
static double Clamp(const double x, const double min, const double max)
Definition IK_Math.h:30
static T Sign(const T &x)
T clamp(const T &a, const T &min, const T &max)
T safe_rcp(const T &a)
T sqrt(const T &a)
T sign(const T &a)
VecBase< T, Size > divide_ceil(const VecBase< T, Size > &a, const VecBase< T, Size > &b)
T exp(const T &x)
T min(const T &a, const T &b)
T interpolate(const T &a, const T &b, const FactorT &t)
T rcp(const T &a)
T square(const T &a)
VecBase< T, Size > ceil_to_multiple(const VecBase< T, Size > &a, const VecBase< T, Size > &b)
T max(const T &a, const T &b)
VecBase< T, Size > safe_sqrt(const VecBase< T, Size > &a)
TEST(blf_load, load)
Definition BLF_tests.cc:34
VecBase< int32_t, 3 > int3
VecBase< float, 3 > float3
#define min(a, b)
Definition sort.cc:36
max
Definition text_draw.cc:251