Blender V4.3
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
10namespace blender::tests {
11
12TEST(math_vector, ClampVecWithFloats)
13{
14 const float min = 0.0f;
15 const float max = 1.0f;
16
17 float a[2] = {-1.0f, -1.0f};
18 clamp_v2(a, min, max);
19 EXPECT_FLOAT_EQ(0.0f, a[0]);
20 EXPECT_FLOAT_EQ(0.0f, a[1]);
21
22 float b[2] = {0.5f, 0.5f};
23 clamp_v2(b, min, max);
24 EXPECT_FLOAT_EQ(0.5f, b[0]);
25 EXPECT_FLOAT_EQ(0.5f, b[1]);
26
27 float c[2] = {2.0f, 2.0f};
28 clamp_v2(c, min, max);
29 EXPECT_FLOAT_EQ(1.0f, c[0]);
30 EXPECT_FLOAT_EQ(1.0f, c[1]);
31}
32
33TEST(math_vector, ClampVecWithVecs)
34{
35 const float min[2] = {0.0f, 2.0f};
36 const float max[2] = {1.0f, 3.0f};
37
38 float a[2] = {-1.0f, -1.0f};
39 clamp_v2_v2v2(a, min, max);
40 EXPECT_FLOAT_EQ(0.0f, a[0]);
41 EXPECT_FLOAT_EQ(2.0f, a[1]);
42
43 float b[2] = {0.5f, 2.5f};
44 clamp_v2_v2v2(b, min, max);
45 EXPECT_FLOAT_EQ(0.5f, b[0]);
46 EXPECT_FLOAT_EQ(2.5f, b[1]);
47
48 float c[2] = {2.0f, 4.0f};
49 clamp_v2_v2v2(c, min, max);
50 EXPECT_FLOAT_EQ(1.0f, c[0]);
51 EXPECT_FLOAT_EQ(3.0f, c[1]);
52}
53
54TEST(math_vector, test_invert_v3_safe)
55{
56 float v3_with_zeroes[3] = {0.0f, 2.0f, 3.0f};
57 invert_v3_safe(v3_with_zeroes);
58 EXPECT_FLOAT_EQ(0.0f, v3_with_zeroes[0]);
59 EXPECT_FLOAT_EQ(0.5f, v3_with_zeroes[1]);
60 EXPECT_FLOAT_EQ(0.33333333333f, v3_with_zeroes[2]);
61
62 float v3_without_zeroes[3] = {1.0f, 2.0f, 3.0f};
63 float inverted_unsafe[3] = {1.0f, 2.0f, 3.0f};
64 invert_v3_safe(v3_without_zeroes);
65 invert_v3(inverted_unsafe);
66
67 EXPECT_FLOAT_EQ(inverted_unsafe[0], v3_without_zeroes[0]);
68 EXPECT_FLOAT_EQ(inverted_unsafe[1], v3_without_zeroes[1]);
69 EXPECT_FLOAT_EQ(inverted_unsafe[2], v3_without_zeroes[2]);
70}
71
72TEST(math_vector, Clamp)
73{
74 const int3 value(0, 100, -100);
75 const int3 min(5, 40, -95);
76 const int3 max(7, 45, 5);
77
78 const int3 result = math::clamp(value, min, max);
79 EXPECT_EQ(result.x, 5);
80 EXPECT_EQ(result.y, 45);
81 EXPECT_EQ(result.z, -95);
82
83 const int3 result_2 = math::clamp(value, -50, 50);
84 EXPECT_EQ(result_2.x, 0);
85 EXPECT_EQ(result_2.y, 50);
86 EXPECT_EQ(result_2.z, -50);
87}
88
89TEST(math_vector, InterpolateInt)
90{
91 const int3 a(0, -100, 50);
92 const int3 b(0, 100, 100);
93 const int3 result = math::interpolate(a, b, 0.75);
94 EXPECT_EQ(result.x, 0);
95 EXPECT_EQ(result.y, 50);
96 EXPECT_EQ(result.z, 87);
97}
98
99TEST(math_vector, InterpolateFloat)
100{
101 const float3 a(40.0f, -100.0f, 50.0f);
102 const float3 b(20.0f, 100.0f, 100.0f);
103 const float3 result = math::interpolate(a, b, 0.5);
104 EXPECT_FLOAT_EQ(result.x, 30.0f);
105 EXPECT_FLOAT_EQ(result.y, 0.0f);
106 EXPECT_FLOAT_EQ(result.z, 75.0f);
107}
108
109TEST(math_vector, CeilToMultiple)
110{
111 const int3 a(21, 16, 0);
112 const int3 b(8, 16, 15);
113 const int3 result = math::ceil_to_multiple(a, b);
114 EXPECT_FLOAT_EQ(result.x, 24);
115 EXPECT_FLOAT_EQ(result.y, 16);
116 EXPECT_FLOAT_EQ(result.z, 0);
117}
118
119TEST(math_vector, DivideCeil)
120{
121 const int3 a(21, 16, 0);
122 const int3 b(8, 16, 15);
123 const int3 result = math::divide_ceil(a, b);
124 EXPECT_FLOAT_EQ(result.x, 3);
125 EXPECT_FLOAT_EQ(result.y, 1);
126 EXPECT_FLOAT_EQ(result.z, 0);
127}
128
129TEST(math_vector, Sign)
130{
131 const int3 a(-21, 16, 0);
132 const int3 result = math::sign(a);
133 EXPECT_FLOAT_EQ(result.x, -1);
134 EXPECT_FLOAT_EQ(result.y, 1);
135 EXPECT_FLOAT_EQ(result.z, 0);
136}
137
138TEST(math_vector, sqrt)
139{
140 const float3 a(1.0f, 4.0f, 9.0f);
141 const float3 result = math::sqrt(a);
142 EXPECT_NEAR(result.x, 1.0f, 1e-6f);
143 EXPECT_NEAR(result.y, 2.0f, 1e-6f);
144 EXPECT_NEAR(result.z, 3.0f, 1e-6f);
145}
146
147TEST(math_vector, safe_sqrt)
148{
149 const float3 a(1.0f, -4.0f, 9.0f);
150 const float3 result = math::safe_sqrt(a);
151 EXPECT_NEAR(result.x, 1.0f, 1e-6f);
152 EXPECT_NEAR(result.y, 0.0f, 1e-6f);
153 EXPECT_NEAR(result.z, 3.0f, 1e-6f);
154}
155
156TEST(math_vector, rcp)
157{
158 const float3 a(1.0f, 2.0f, 4.0f);
159 const float3 result = math::rcp(a);
160 EXPECT_NEAR(result.x, 1.0f, 1e-6f);
161 EXPECT_NEAR(result.y, 0.5f, 1e-6f);
162 EXPECT_NEAR(result.z, 0.25f, 1e-6f);
163}
164
165TEST(math_vector, safe_rcp)
166{
167 const float3 a(1.0f, 0.0f, 4.0f);
168 const float3 result = math::safe_rcp(a);
169 EXPECT_NEAR(result.x, 1.0f, 1e-6f);
170 EXPECT_NEAR(result.y, 0.0f, 1e-6f);
171 EXPECT_NEAR(result.z, 0.25f, 1e-6f);
172}
173
174TEST(math_vector, exp)
175{
176 const float3 a(1.0f, 2.0f, 3.0f);
177 const float3 result = math::exp(a);
178 EXPECT_NEAR(result.x, 2.718281828459045f, 1e-6f);
179 EXPECT_NEAR(result.y, 7.38905609893065f, 1e-6f);
180 EXPECT_NEAR(result.z, 20.085536923187668f, 1e-6f);
181}
182
183TEST(math_vector, square)
184{
185 const float3 a(1.0f, 2.0f, 3.0f);
186 const float3 result = math::square(a);
187 EXPECT_NEAR(result.x, 1.0f, 1e-6f);
188 EXPECT_NEAR(result.y, 4.0f, 1e-6f);
189 EXPECT_NEAR(result.z, 9.0f, 1e-6f);
190}
191
192} // 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 clamp_v2_v2v2(float vec[2], const float min[2], const float max[2])
MINLINE void invert_v3(float r[3])
Group Output data from inside of a node group A color picker Mix two input colors RGB to Convert a color s luminance to a grayscale value Generate a normal vector and a dot product Brightness Control the brightness and contrast of the input color Vector Map input vector components with curves Camera Retrieve information about the camera and how it relates to the current shading point s position Clamp
static T Sign(const T &x)
local_group_size(16, 16) .push_constant(Type b
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 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)
VecBase< T, Size > safe_sqrt(const VecBase< T, Size > &a)
TEST(any, DefaultConstructor)
#define min(a, b)
Definition sort.c:32
float max