Blender V5.3
anim_draw_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6
7#include "ED_anim_api.hh"
8
9#include "DNA_anim_types.h"
10
11#include "BKE_effect.h"
12#include "BKE_fcurve.hh"
13#include "BKE_gtest_base.hh"
14#include "BKE_idprop.hh"
15#include "BKE_idtype.hh"
16#include "BKE_lib_id.hh"
17#include "BKE_main.hh"
18#include "BKE_unit.hh"
19
20#include "RNA_define.hh"
21#include "RNA_types.hh"
22
23#include "testing/testing.h"
24
26
28 public:
31
32 void SetUp() override
33 {
34 this->bmain = BKE_main_new();
35 this->object = BKE_id_new<Object>(this->bmain, "OBTestObject");
36 this->object->id.properties = bke::idprop::create_group("IDPropertyGroup").release();
37
38 IDProperty *time_prop = bke::idprop::create("time", 1.0f).release();
40 IDProperty *mass_prop = bke::idprop::create("mass", 1.0f).release();
42
45 }
46
47 void TearDown() override
48 {
49 BKE_main_free(this->bmain);
50 RNA_exit();
51 }
52};
53
54TEST_F(AnimDrawTest, anim_unit_mapping_get_factor_not_normalizing)
55{
56 FCurve *fcurve = BKE_fcurve_create();
57 fcurve->array_index = 0;
58
59 /* Avoid creating a Scene via BKE_id_new<Scene>(this->bmain, "SCTestScene"); as that requires
60 * much more setup (appdirs, imbuf for color management, and maybe more). This test doesn't
61 * actually need a full Scene, it just needs its `units` field. */
62 Scene scene = {};
63 scene.unit.scale_length = 1.0f;
64
65 { /* Rotation: Degrees. */
67 fcurve->rna_path_set("rotation_euler");
68
69 EXPECT_FLOAT_EQ(RAD2DEGF(1.0f),
70 ANIM_unit_mapping_get_factor(&scene, &this->object->id, fcurve, 0, nullptr));
71 EXPECT_FLOAT_EQ(1.0f / RAD2DEGF(1.0f),
73 &scene, &this->object->id, fcurve, ANIM_UNITCONV_RESTORE, nullptr));
74 }
75
76 { /* Rotation: Radians. */
78 fcurve->rna_path_set("rotation_euler");
79
80 EXPECT_FLOAT_EQ(1.0f,
81 ANIM_unit_mapping_get_factor(&scene, &this->object->id, fcurve, 0, nullptr));
82 EXPECT_FLOAT_EQ(1.0f,
84 &scene, &this->object->id, fcurve, ANIM_UNITCONV_RESTORE, nullptr));
85 }
86
87 { /* Length */
88 fcurve->rna_path_set("location");
89 const auto test_unit_scalar = [&](const int unit_system,
90 const int unit_idx,
91 const float expected_scalar,
92 const float expected_restore_scalar) {
93 scene.unit.system = unit_system;
94 scene.unit.length_unit = unit_idx;
95
96 EXPECT_FLOAT_EQ(expected_scalar,
97 ANIM_unit_mapping_get_factor(&scene, &this->object->id, fcurve, 0, nullptr));
98 EXPECT_FLOAT_EQ(expected_restore_scalar,
100 &scene, &this->object->id, fcurve, ANIM_UNITCONV_RESTORE, nullptr));
101 };
102
103 /* No unit system */
104 test_unit_scalar(USER_UNIT_NONE, 0, 1.0f, 1.0f);
105
106 /* Metric */
107 test_unit_scalar(USER_UNIT_METRIC, 0 /* km */, 1.0f / 1000.0f, 1000.0f);
108 test_unit_scalar(USER_UNIT_METRIC, 3 /* m */, 1.0f, 1.0f);
109 test_unit_scalar(USER_UNIT_METRIC, 5 /* cm */, 1.0f / 0.01f, 0.01f);
110 test_unit_scalar(USER_UNIT_METRIC, 6 /* mm */, 1.0f / 0.001f, 0.001f);
111 test_unit_scalar(USER_UNIT_METRIC, 7 /* um */, 1.0f / 0.000001f, 0.000001f);
112 /* Adaptive - meter */
113 test_unit_scalar(USER_UNIT_METRIC, USER_UNIT_ADAPTIVE, 1.0f, 1.0f);
114
115 /* Imperial */
116 test_unit_scalar(USER_UNIT_IMPERIAL, 0 /* mi */, 1.0f / 1609.344f, 1609.344f);
117 test_unit_scalar(USER_UNIT_IMPERIAL, 4 /* ft */, 1.0f / 0.3048f, 0.3048f);
118 test_unit_scalar(USER_UNIT_IMPERIAL, 5 /* in */, 1.0f / 0.0254f, 0.0254f);
119 test_unit_scalar(USER_UNIT_IMPERIAL, 6 /* thou */, 1.0f / 0.0000254f, 0.0000254f);
120 test_unit_scalar(USER_UNIT_IMPERIAL, USER_UNIT_ADAPTIVE, 1.0f / 0.3048f, 0.3048f);
121
122 /* Unit Scale = 2.0 */
123 scene.unit.scale_length = 2.0f;
124 test_unit_scalar(USER_UNIT_METRIC, 3 /* m */, 2.0f, 0.5f);
125 test_unit_scalar(USER_UNIT_IMPERIAL, 4 /* ft */, 2.0f / 0.3048f, 0.3048f / 2.0f);
126 scene.unit.scale_length = 1.0f;
127 }
128
129 { /* Mass */
130 fcurve->rna_path_set("[\"mass\"]");
131 const auto test_unit_scalar = [&](const int unit_system,
132 const int unit_idx,
133 const float expected_scalar,
134 const float expected_restore_scalar) {
135 scene.unit.system = unit_system;
136 scene.unit.mass_unit = unit_idx;
137
138 EXPECT_FLOAT_EQ(expected_scalar,
139 ANIM_unit_mapping_get_factor(&scene, &this->object->id, fcurve, 0, nullptr));
140 EXPECT_FLOAT_EQ(expected_restore_scalar,
142 &scene, &this->object->id, fcurve, ANIM_UNITCONV_RESTORE, nullptr));
143 };
144
145 /* Metric */
146 test_unit_scalar(USER_UNIT_METRIC, 0 /* t */, 1.0f / 1000.0f, 1000.0f);
147 test_unit_scalar(USER_UNIT_METRIC, 2 /* kg */, 1.0f, 1.0f);
148 test_unit_scalar(USER_UNIT_METRIC, 5 /* g */, 1.0f / 0.001f, 0.001f);
149 test_unit_scalar(USER_UNIT_METRIC, 6 /* mg */, 1.0f / 0.000001f, 0.000001f);
150 test_unit_scalar(USER_UNIT_METRIC, USER_UNIT_ADAPTIVE, 1.0f, 1.0f);
151
152 /* Imperial */
153 test_unit_scalar(USER_UNIT_IMPERIAL, 0 /* tn */, 1.0f / 907.18474f, 907.18474f);
154 test_unit_scalar(USER_UNIT_IMPERIAL, 1 /* cwt */, 1.0f / 45.359237f, 45.359237f);
155 test_unit_scalar(USER_UNIT_IMPERIAL, 2 /* st */, 1.0f / 6.35029318f, 6.35029318f);
156 test_unit_scalar(USER_UNIT_IMPERIAL, 3 /* lb */, 1.0f / 0.45359237f, 0.45359237f);
157 test_unit_scalar(USER_UNIT_IMPERIAL, 4 /* oz */, 1.0f / 0.028349523125f, 0.028349523125f);
158 test_unit_scalar(USER_UNIT_IMPERIAL, USER_UNIT_ADAPTIVE, 1.0f / 0.45359237f, 0.45359237f);
159
160 /* Unit Scale = 2.0 */
161 scene.unit.scale_length = 2.0f;
162 test_unit_scalar(USER_UNIT_METRIC, 2 /* kg */, 8.0f, 1.0f / 8.0f);
163 test_unit_scalar(USER_UNIT_IMPERIAL, 3 /* lb */, 8.0f / 0.45359237f, 0.45359237f / 8.0f);
164 scene.unit.scale_length = 1.0f;
165 }
166
167 { /* Time */
168 fcurve->rna_path_set("[\"time\"]");
169 const auto test_unit_scalar = [&](const int unit_idx,
170 const float expected_scalar,
171 const float expected_restore_scalar) {
172 scene.unit.time_unit = unit_idx;
173
174 EXPECT_FLOAT_EQ(expected_scalar,
175 ANIM_unit_mapping_get_factor(&scene, &this->object->id, fcurve, 0, nullptr));
176 EXPECT_FLOAT_EQ(expected_restore_scalar,
178 &scene, &this->object->id, fcurve, ANIM_UNITCONV_RESTORE, nullptr));
179 };
180
181 test_unit_scalar(0 /* d */, 1.0f / 86400.0f, 86400.0f);
182 test_unit_scalar(1 /* h */, 1.0f / 3600.0f, 3600.0f);
183 test_unit_scalar(2 /* min */, 1.0f / 60.0f, 60.0f);
184 test_unit_scalar(3 /* s */, 1.0f, 1.0f);
185 test_unit_scalar(4 /* ms */, 1.0f / 0.001f, 0.001f);
186 test_unit_scalar(5 /* us */, 1.0f / 0.000001f, 0.000001f);
187 test_unit_scalar(USER_UNIT_ADAPTIVE, 1.0f, 1.0f);
188 }
189
190 BKE_fcurve_free(fcurve);
191}
192
193} // namespace blender::animrig::tests
#define RAD2DEGF(_rad)
#define USER_UNIT_ADAPTIVE
TEST_F(ActionIteratorsTest, iterate_all_fcurves_of_slot)
std::unique_ptr< IDProperty, IDPropertyDeleter > create(StringRef prop_name, int32_t value, eIDPropertyFlag flags={})
Allocate a new IDProperty of type IDP_INT, set its name and value.
std::unique_ptr< IDProperty, IDPropertyDeleter > create_group(StringRef prop_name, eIDPropertyFlag flags={})
Allocate a new IDProperty of type IDP_GROUP.
@ ANIM_UNITCONV_RESTORE
bool IDP_AddToGroup(IDProperty *group, IDProperty *prop) ATTR_NONNULL()
Definition idprop.cc:788
Main * BKE_main_new()
Definition main.cc:93
void RNA_exit()
void BKE_main_free(Main *bmain)
Definition main.cc:202
void BKE_fcurve_free(FCurve *fcu)
float ANIM_unit_mapping_get_factor(Scene *scene, ID *id, FCurve *fcu, short flag, float *r_offset)
Definition anim_draw.cc:636
IDPropertyUIData * IDP_ui_data_ensure(IDProperty *prop)
Definition idprop.cc:2034
void * BKE_id_new(Main *bmain, short type, const char *name)
Definition lib_id.cc:1612
FCurve * BKE_fcurve_create()
IDProperty * properties
Definition DNA_ID.h:481
struct UnitSettings unit