Blender V5.3
evaluation_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "ANIM_action.hh"
6#include "ANIM_evaluation.hh"
8
9#include "BKE_action.hh"
10#include "BKE_animsys.hh"
11#include "BKE_gtest_base.hh"
12#include "BKE_idtype.hh"
13#include "BKE_lib_id.hh"
14#include "BKE_main.hh"
15#include "BKE_mesh.hh"
16#include "BKE_object.hh"
17
18#include "DNA_mesh_types.h"
19#include "DNA_object_types.h"
20
21#include "RNA_access.hh"
22#include "RNA_prototypes.hh"
23
24#include "BLI_math_base_c.hh"
25
26#include <optional>
27
28#include "testing/testing.h"
29
31
32using namespace blender::animrig::internal;
33
35 protected:
41
45
46 public:
47 void SetUp() override
48 {
50 action = BKE_id_new<Action>(bmain, "ACÄnimåtië");
51
53
54 slot = &action->slot_add();
56
57 layer = &action->layer_add("Kübus layer");
58
59 /* Make it easier to predict test values. */
61
62 cube_rna_ptr = RNA_pointer_create_discrete(&cube->id, RNA_Object, &cube->id);
63 }
64
65 void TearDown() override
66 {
68 }
69
72 const int array_index,
73 const float eval_time)
74 {
75 anim_eval_context.eval_time = eval_time;
78
79 std::optional<ParsedRNAPath<>> parsed_rna_path = ParsedRNAPath<>::from_string(rna_path);
80 if (!parsed_rna_path) {
81 return {};
82 }
83
84 const AnimatedProperty *loc0_result = result.lookup_ptr(
85 PropIdentifier(*parsed_rna_path, array_index));
86 if (!loc0_result) {
87 return {};
88 }
89 return loc0_result->value;
90 }
91
93 testing::AssertionResult test_evaluate_layer(const StringRefNull rna_path,
94 const int array_index,
95 const float2 eval_time__expect_value)
96 {
97 const float eval_time = eval_time__expect_value[0];
98 const float expect_value = eval_time__expect_value[1];
99
100 const std::optional<float> opt_eval_value = evaluate_single_property(
101 rna_path, array_index, eval_time);
102 if (!opt_eval_value) {
103 return testing::AssertionFailure()
104 << rna_path << "[" << array_index << "] should have been animated";
105 }
106
107 const float eval_value = *opt_eval_value;
108 const uint diff_ulps = ulp_diff_ff(expect_value, eval_value);
109 if (diff_ulps >= 4) {
110 return testing::AssertionFailure()
111 << std::endl
112 << " " << rna_path << "[" << array_index
113 << "] evaluation did not produce the expected result:" << std::endl
114 << " evaluated to: " << testing::PrintToString(eval_value) << std::endl
115 << " expected : " << testing::PrintToString(expect_value) << std::endl;
116 }
117
118 return testing::AssertionSuccess();
119 };
120
123 const int array_index,
124 const float eval_time)
125 {
126 const std::optional<float> eval_value = evaluate_single_property(
127 rna_path, array_index, eval_time);
128 if (eval_value) {
129 return testing::AssertionFailure()
130 << std::endl
131 << " " << rna_path << "[" << array_index
132 << "] evaluation should NOT produce a value:" << std::endl
133 << " evaluated to: " << testing::PrintToString(*eval_value) << std::endl;
134 }
135
136 return testing::AssertionSuccess();
137 }
138};
139
140TEST_F(AnimationEvaluationTest, evaluate_layer__keyframes)
141{
142 Strip &strip = layer->strip_add(*action, Strip::Type::Keyframe);
143 StripKeyframeData &strip_data = strip.data<StripKeyframeData>(*action);
144
145 /* Set some keys. */
146 strip_data.keyframe_insert(bmain, *slot, {"location", 0}, {1.0f, 47.1f}, settings);
147 strip_data.keyframe_insert(bmain, *slot, {"location", 0}, {5.0f, 47.5f}, settings);
148 strip_data.keyframe_insert(bmain, *slot, {"rotation_euler", 1}, {1.0f, 0.0f}, settings);
149 strip_data.keyframe_insert(bmain, *slot, {"rotation_euler", 1}, {5.0f, 3.14f}, settings);
150
151 /* Set the animated properties to some values. These should not be overwritten
152 * by the evaluation itself. */
153 cube->loc[0] = 3.0f;
154 cube->loc[1] = 2.0f;
155 cube->loc[2] = 7.0f;
156 cube->rot[0] = 3.0f;
157 cube->rot[1] = 2.0f;
158 cube->rot[2] = 7.0f;
159
160 /* Evaluate. */
161 anim_eval_context.eval_time = 3.0f;
163 cube_rna_ptr, *action, *layer, slot->handle, anim_eval_context);
164
165 std::optional<ParsedRNAPath<>> parsed_rna_path = ParsedRNAPath<>::from_string("location");
166 EXPECT_TRUE(parsed_rna_path.has_value());
167
168 /* Check the result. */
169 ASSERT_FALSE(result.is_empty());
170 AnimatedProperty *loc0_result = result.lookup_ptr(PropIdentifier(*parsed_rna_path, 0));
171 ASSERT_NE(nullptr, loc0_result) << "location[0] should have been animated";
172 EXPECT_EQ(47.3f, loc0_result->value);
173
174 EXPECT_EQ(3.0f, cube->loc[0]) << "Evaluation should not modify the animated ID";
175 EXPECT_EQ(2.0f, cube->loc[1]) << "Evaluation should not modify the animated ID";
176 EXPECT_EQ(7.0f, cube->loc[2]) << "Evaluation should not modify the animated ID";
177 EXPECT_EQ(3.0f, cube->rot[0]) << "Evaluation should not modify the animated ID";
178 EXPECT_EQ(2.0f, cube->rot[1]) << "Evaluation should not modify the animated ID";
179 EXPECT_EQ(7.0f, cube->rot[2]) << "Evaluation should not modify the animated ID";
180}
181
182TEST_F(AnimationEvaluationTest, evaluate_layer__mesh_vertices)
183{
184 Mesh *mesh = BKE_mesh_add(bmain, "Mesh");
185 constexpr int vertex_count = 200;
186 mesh->verts_num = vertex_count;
188
189 Action *mesh_action = BKE_id_new<Action>(bmain, "MeshAction");
190 Slot &mesh_slot = mesh_action->slot_add();
191 ASSERT_EQ(assign_action_and_slot(mesh_action, &mesh_slot, mesh->id),
193
194 Layer &mesh_layer = mesh_action->layer_add("Mesh layer");
195 Strip &strip = mesh_layer.strip_add(*mesh_action, Strip::Type::Keyframe);
196 StripKeyframeData &strip_data = strip.data<StripKeyframeData>(*mesh_action);
197
198 for (const int vertex_index : IndexRange(vertex_count)) {
199 const std::string rna_path = "vertices[" + std::to_string(vertex_index) + "].co";
200 for (const int component : IndexRange(3)) {
201 strip_data.keyframe_insert(
202 bmain, mesh_slot, {rna_path, component}, {1.0f, float(vertex_index)}, settings);
203 }
204 }
205
206 PointerRNA mesh_rna_ptr = RNA_id_pointer_create(&mesh->id);
207 anim_eval_context.eval_time = 1.0f;
208
210 mesh_rna_ptr, *mesh_action, mesh_layer, mesh_slot.handle, anim_eval_context);
211
212 ASSERT_FALSE(result.is_empty());
213
214 for (const int vertex_index : IndexRange(vertex_count)) {
215 const std::string rna_path = "vertices[" + std::to_string(vertex_index) + "].co";
216 const std::optional<ParsedRNAPath<>> parsed_rna_path = ParsedRNAPath<>::from_string(rna_path);
217 ASSERT_TRUE(parsed_rna_path.has_value());
218
219 for (const int component : IndexRange(3)) {
220 const AnimatedProperty *property = result.lookup_ptr(
221 PropIdentifier(*parsed_rna_path, component));
222 ASSERT_NE(nullptr, property);
223 EXPECT_EQ(float(vertex_index), property->value);
224 }
225 }
226}
227
228TEST_F(AnimationEvaluationTest, strip_boundaries__single_strip)
229{
230 /* Single finite strip, check first, middle, and last frame. */
231 Strip &strip = layer->strip_add(*action, Strip::Type::Keyframe);
232 strip.resize(1.0f, 10.0f);
233
234 /* Set some keys. */
235 StripKeyframeData &strip_data = strip.data<StripKeyframeData>(*action);
236 strip_data.keyframe_insert(bmain, *slot, {"location", 0}, {1.0f, 47.0f}, settings);
237 strip_data.keyframe_insert(bmain, *slot, {"location", 0}, {5.0f, 327.0f}, settings);
238 strip_data.keyframe_insert(bmain, *slot, {"location", 0}, {10.0f, 48.0f}, settings);
239
240 /* Evaluate the layer to see how it handles the boundaries + something in between. */
241 EXPECT_TRUE(test_evaluate_layer("location", 0, {1.0f, 47.0f}));
242 EXPECT_TRUE(test_evaluate_layer("location", 0, {3.0f, 187.0f}));
243 EXPECT_TRUE(test_evaluate_layer("location", 0, {10.0f, 48.0f}));
244
245 EXPECT_TRUE(test_evaluate_layer_no_result("location", 0, 10.001f));
246}
247
248TEST_F(AnimationEvaluationTest, strip_boundaries__nonoverlapping)
249{
250 /* Two finite strips that are strictly distinct. */
251 Strip &strip1 = layer->strip_add(*action, Strip::Type::Keyframe);
252 Strip &strip2 = layer->strip_add(*action, Strip::Type::Keyframe);
253 strip1.resize(1.0f, 10.0f);
254 strip2.resize(11.0f, 20.0f);
255 strip2.frame_offset = 10;
256
257 /* Set some keys. */
258 {
259 StripKeyframeData &strip_data1 = strip1.data<StripKeyframeData>(*action);
260 strip_data1.keyframe_insert(bmain, *slot, {"location", 0}, {1.0f, 47.0f}, settings);
261 strip_data1.keyframe_insert(bmain, *slot, {"location", 0}, {5.0f, 327.0f}, settings);
262 strip_data1.keyframe_insert(bmain, *slot, {"location", 0}, {10.0f, 48.0f}, settings);
263 }
264 {
265 StripKeyframeData &strip_data2 = strip2.data<StripKeyframeData>(*action);
266 strip_data2.keyframe_insert(bmain, *slot, {"location", 0}, {1.0f, 47.0f}, settings);
267 strip_data2.keyframe_insert(bmain, *slot, {"location", 0}, {5.0f, 327.0f}, settings);
268 strip_data2.keyframe_insert(bmain, *slot, {"location", 0}, {10.0f, 48.0f}, settings);
269 }
270
271 /* Check Strip 1. */
272 EXPECT_TRUE(test_evaluate_layer("location", 0, {1.0f, 47.0f}));
273 EXPECT_TRUE(test_evaluate_layer("location", 0, {3.0f, 187.0f}));
274 EXPECT_TRUE(test_evaluate_layer("location", 0, {10.0f, 48.0f}));
275
276 /* Check Strip 2. */
277 EXPECT_TRUE(test_evaluate_layer("location", 0, {11.0f, 47.0f}));
278 EXPECT_TRUE(test_evaluate_layer("location", 0, {13.0f, 187.0f}));
279 EXPECT_TRUE(test_evaluate_layer("location", 0, {20.0f, 48.0f}));
280
281 /* Check outside the range of the strips. */
282 EXPECT_TRUE(test_evaluate_layer_no_result("location", 0, 0.999f));
283 EXPECT_TRUE(test_evaluate_layer_no_result("location", 0, 10.001f));
284 EXPECT_TRUE(test_evaluate_layer_no_result("location", 0, 10.999f));
285 EXPECT_TRUE(test_evaluate_layer_no_result("location", 0, 20.001f));
286}
287
288TEST_F(AnimationEvaluationTest, strip_boundaries__overlapping_edge)
289{
290 /* Two finite strips that are overlapping on their edge. */
291 Strip &strip1 = layer->strip_add(*action, Strip::Type::Keyframe);
292 Strip &strip2 = layer->strip_add(*action, Strip::Type::Keyframe);
293 strip1.resize(1.0f, 10.0f);
294 strip2.resize(10.0f, 19.0f);
295 strip2.frame_offset = 9;
296
297 /* Set some keys. */
298 {
299 StripKeyframeData &strip_data1 = strip1.data<StripKeyframeData>(*action);
300 strip_data1.keyframe_insert(bmain, *slot, {"location", 0}, {1.0f, 47.0f}, settings);
301 strip_data1.keyframe_insert(bmain, *slot, {"location", 0}, {5.0f, 327.0f}, settings);
302 strip_data1.keyframe_insert(bmain, *slot, {"location", 0}, {10.0f, 48.0f}, settings);
303 }
304 {
305 StripKeyframeData &strip_data2 = strip2.data<StripKeyframeData>(*action);
306 strip_data2.keyframe_insert(bmain, *slot, {"location", 0}, {1.0f, 47.0f}, settings);
307 strip_data2.keyframe_insert(bmain, *slot, {"location", 0}, {5.0f, 327.0f}, settings);
308 strip_data2.keyframe_insert(bmain, *slot, {"location", 0}, {10.0f, 48.0f}, settings);
309 }
310
311 /* Check Strip 1. */
312 EXPECT_TRUE(test_evaluate_layer("location", 0, {1.0f, 47.0f}));
313 EXPECT_TRUE(test_evaluate_layer("location", 0, {3.0f, 187.0f}));
314
315 /* Check overlapping frame. */
316 EXPECT_TRUE(test_evaluate_layer("location", 0, {10.0f, 47.0f}))
317 << "On the overlapping frame, only Strip 2 should be evaluated.";
318
319 /* Check Strip 2. */
320 EXPECT_TRUE(test_evaluate_layer("location", 0, {12.0f, 187.0f}));
321 EXPECT_TRUE(test_evaluate_layer("location", 0, {19.0f, 48.0f}));
322
323 /* Check outside the range of the strips. */
324 EXPECT_TRUE(test_evaluate_layer_no_result("location", 0, 0.999f));
325 EXPECT_TRUE(test_evaluate_layer_no_result("location", 0, 19.001f));
326}
327
329 public:
331 {
332 return result_;
333 }
334};
335
337
338TEST_F(AnimationEvaluationResultTest, prop_identifier_hashing)
339{
341
342 /* Test storing the same result twice, with different memory locations of the RNA paths. This
343 * tests that the mapping uses the actual string, and not just pointer comparison. */
344 const char *rna_path_1 = "pose.bones[\"Root\"].location";
345 const std::string rna_path_2(rna_path_1);
346 ASSERT_NE(rna_path_1, rna_path_2.c_str())
347 << "This test requires different addresses for the RNA path strings";
348
349 std::optional<ParsedRNAPath<>> parsed_rna_path_1 = ParsedRNAPath<>::from_string(rna_path_1);
350 std::optional<ParsedRNAPath<>> parsed_rna_path_2 = ParsedRNAPath<>::from_string(rna_path_2);
351
352 PathResolvedRNA fake_resolved_rna;
353 result.store(*parsed_rna_path_1, 0, 1.0f, fake_resolved_rna);
354 result.store(*parsed_rna_path_2, 0, 2.0f, fake_resolved_rna);
355 EXPECT_EQ(1, result.get_map().size())
356 << "Storing a result for the same property twice should just overwrite the previous value";
357
358 {
359 PropIdentifier key(*parsed_rna_path_1, 0);
360 AnimatedProperty *anim_prop = result.lookup_ptr(key);
361 EXPECT_EQ(2.0f, anim_prop->value) << "The last-stored result should survive.";
362 }
363 {
364 PropIdentifier key(*parsed_rna_path_2, 0);
365 AnimatedProperty *anim_prop = result.lookup_ptr(key);
366 EXPECT_EQ(2.0f, anim_prop->value) << "The last-stored result should survive.";
367 }
368}
369
370} // namespace blender::animrig::tests
Functions and classes to work with Actions.
Layered Action evaluation.
Blender kernel action and pose functionality.
General operations, lookup, etc. for blender objects.
Object is a sort of wrapper for general info.
constexpr IndexRange()=default
static std::optional< ParsedRNAPath > from_string(StringRefNull path)
Definition rna_path.cc:194
Layer & layer_add(std::optional< StringRefNull > name)
Map< PropIdentifier, AnimatedProperty > EvaluationMap
Strip & strip_add(Action &owning_action, Strip::Type strip_type)
SingleKeyingResult keyframe_insert(Main *bmain, const Slot &slot, const FCurveDescriptor &fcurve_descriptor, float2 time_value, const KeyframeSettings &settings, eInsertKeyFlags insert_key_flags=INSERTKEY_NOFLAGS, std::optional< float2 > cycle_range=std::nullopt)
void resize(float frame_start, float frame_end)
const T & data(const Action &owning_action) const
testing::AssertionResult test_evaluate_layer(const StringRefNull rna_path, const int array_index, const float2 eval_time__expect_value)
std::optional< float > evaluate_single_property(const StringRefNull rna_path, const int array_index, const float eval_time)
testing::AssertionResult test_evaluate_layer_no_result(const StringRefNull rna_path, const int array_index, const float eval_time)
#define layer
#define EXPECT_TRUE(result)
EvaluationResult evaluate_layer(PointerRNA &animated_id_ptr, Action &owning_action, Layer &layer, const slot_handle_t slot_handle, const AnimationEvalContext &anim_eval_context)
TEST_F(ActionIteratorsTest, iterate_all_fcurves_of_slot)
KeyframeSettings get_keyframe_settings(bool from_userprefs)
ActionSlotAssignmentResult assign_action_and_slot(Action *action, Slot *slot_to_assign, ID &animated_id)
void mesh_ensure_required_data_layers(Mesh &mesh)
unsigned int uint
Definition BKE_text.h:55
PointerRNA RNA_id_pointer_create(ID *id)
Main * BKE_main_new()
Definition main.cc:93
MINLINE uint ulp_diff_ff(float a, float b)
VecBase< float, 2 > float2
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
void BKE_main_free(Main *bmain)
Definition main.cc:202
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
Object * BKE_object_add_only_object(Main *bmain, ObjectType type, const char *name) ATTR_RETURNS_NONNULL
Definition object.cc:2265
Mesh * BKE_mesh_add(Main *bmain, const char *name)
void * BKE_id_new(Main *bmain, short type, const char *name)
Definition lib_id.cc:1612
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
eBezTriple_Interpolation interpolation