Blender  V3.3
BLI_math_vector_test.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0 */
2 
3 #include "testing/testing.h"
4 
5 #include "BLI_math.h"
6 
7 #include "BLI_math_vector.hh"
8 
9 namespace blender::tests {
10 
11 TEST(math_vector, ClampVecWithFloats)
12 {
13  const float min = 0.0f;
14  const float max = 1.0f;
15 
16  float a[2] = {-1.0f, -1.0f};
17  clamp_v2(a, min, max);
18  EXPECT_FLOAT_EQ(0.0f, a[0]);
19  EXPECT_FLOAT_EQ(0.0f, a[1]);
20 
21  float b[2] = {0.5f, 0.5f};
22  clamp_v2(b, min, max);
23  EXPECT_FLOAT_EQ(0.5f, b[0]);
24  EXPECT_FLOAT_EQ(0.5f, b[1]);
25 
26  float c[2] = {2.0f, 2.0f};
27  clamp_v2(c, min, max);
28  EXPECT_FLOAT_EQ(1.0f, c[0]);
29  EXPECT_FLOAT_EQ(1.0f, c[1]);
30 }
31 
32 TEST(math_vector, ClampVecWithVecs)
33 {
34  const float min[2] = {0.0f, 2.0f};
35  const float max[2] = {1.0f, 3.0f};
36 
37  float a[2] = {-1.0f, -1.0f};
39  EXPECT_FLOAT_EQ(0.0f, a[0]);
40  EXPECT_FLOAT_EQ(2.0f, a[1]);
41 
42  float b[2] = {0.5f, 2.5f};
44  EXPECT_FLOAT_EQ(0.5f, b[0]);
45  EXPECT_FLOAT_EQ(2.5f, b[1]);
46 
47  float c[2] = {2.0f, 4.0f};
49  EXPECT_FLOAT_EQ(1.0f, c[0]);
50  EXPECT_FLOAT_EQ(3.0f, c[1]);
51 }
52 
53 TEST(math_vector, test_invert_v3_safe)
54 {
55  float v3_with_zeroes[3] = {0.0f, 2.0f, 3.0f};
56  invert_v3_safe(v3_with_zeroes);
57  EXPECT_FLOAT_EQ(0.0f, v3_with_zeroes[0]);
58  EXPECT_FLOAT_EQ(0.5f, v3_with_zeroes[1]);
59  EXPECT_FLOAT_EQ(0.33333333333f, v3_with_zeroes[2]);
60 
61  float v3_without_zeroes[3] = {1.0f, 2.0f, 3.0f};
62  float inverted_unsafe[3] = {1.0f, 2.0f, 3.0f};
63  invert_v3_safe(v3_without_zeroes);
64  invert_v3(inverted_unsafe);
65 
66  EXPECT_FLOAT_EQ(inverted_unsafe[0], v3_without_zeroes[0]);
67  EXPECT_FLOAT_EQ(inverted_unsafe[1], v3_without_zeroes[1]);
68  EXPECT_FLOAT_EQ(inverted_unsafe[2], v3_without_zeroes[2]);
69 }
70 
71 TEST(math_vector, Clamp)
72 {
73  const int3 value(0, 100, -100);
74  const int3 min(5, 40, -95);
75  const int3 max(7, 45, 5);
76 
77  const int3 result = math::clamp(value, min, max);
78  EXPECT_EQ(result.x, 5);
79  EXPECT_EQ(result.y, 45);
80  EXPECT_EQ(result.z, -95);
81 
82  const int3 result_2 = math::clamp(value, -50, 50);
83  EXPECT_EQ(result_2.x, 0);
84  EXPECT_EQ(result_2.y, 50);
85  EXPECT_EQ(result_2.z, -50);
86 }
87 
88 TEST(math_vector, InterpolateInt)
89 {
90  const int3 a(0, -100, 50);
91  const int3 b(0, 100, 100);
92  const int3 result = math::interpolate(a, b, 0.75);
93  EXPECT_EQ(result.x, 0);
94  EXPECT_EQ(result.y, 50);
95  EXPECT_EQ(result.z, 87);
96 }
97 
98 TEST(math_vector, InterpolateFloat)
99 {
100  const float3 a(40.0f, -100.0f, 50.0f);
101  const float3 b(20.0f, 100.0f, 100.0f);
102  const float3 result = math::interpolate(a, b, 0.5);
103  EXPECT_FLOAT_EQ(result.x, 30.0f);
104  EXPECT_FLOAT_EQ(result.y, 0.0f);
105  EXPECT_FLOAT_EQ(result.z, 75.0f);
106 }
107 
108 TEST(math_vector, CeilToMultiple)
109 {
110  const int3 a(21, 16, 0);
111  const int3 b(8, 16, 15);
113  EXPECT_FLOAT_EQ(result.x, 24);
114  EXPECT_FLOAT_EQ(result.y, 16);
115  EXPECT_FLOAT_EQ(result.z, 0);
116 }
117 
118 TEST(math_vector, DivideCeil)
119 {
120  const int3 a(21, 16, 0);
121  const int3 b(8, 16, 15);
122  const int3 result = math::divide_ceil(a, b);
123  EXPECT_FLOAT_EQ(result.x, 3);
124  EXPECT_FLOAT_EQ(result.y, 1);
125  EXPECT_FLOAT_EQ(result.z, 0);
126 }
127 
128 } // 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 Bright Control the brightness and contrast of the input color Vector Map an input vectors to used to fine tune the interpolation of the input Camera Retrieve information about the camera and how it relates to the current shading point s position Clamp
static unsigned c
Definition: RandGen.cpp:83
static unsigned a[3]
Definition: RandGen.cpp:78
T clamp(const T &a, const T &min, const T &max)
vec_base< T, Size > ceil_to_multiple(const vec_base< T, Size > &a, const vec_base< T, Size > &b)
vec_base< T, Size > divide_ceil(const vec_base< T, Size > &a, const vec_base< T, Size > &b)
T interpolate(const T &a, const T &b, const FactorT &t)
TEST(any, DefaultConstructor)
Definition: BLI_any_test.cc:10
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
#define min(a, b)
Definition: sort.c:35
float max