Blender V5.3
armature_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2020 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BKE_action.hh"
6#include "BKE_armature.hh"
7#include "BKE_gtest_base.hh"
8#include "BKE_lib_id.hh"
9#include "BKE_main.hh"
10#include "BKE_object.hh"
11#include "BKE_object_types.hh"
12
13#include "BLI_listbase.hh"
14#include "BLI_math_matrix.hh"
15#include "BLI_math_matrix_c.hh"
17#include "BLI_math_vector_c.hh"
18#include "BLI_string.hh"
19
20#include "DNA_ID.h"
21#include "DNA_action_types.h"
22#include "DNA_armature_types.h"
23#include "DNA_object_types.h"
24
25#include "MEM_guardedalloc.h"
26
27#include "testing/testing.h"
28
29namespace blender::bke::tests {
30
31static const float FLOAT_EPSILON = 1.2e-7;
32
33static const float SCALE_EPSILON = 3.71e-5;
34static const float ORTHO_EPSILON = 5e-5;
35
37static double EXPECT_M3_ORTHOGONAL(const float mat[3][3],
38 double epsilon_scale,
39 double epsilon_ortho)
40{
41 /* Do the checks in double precision to avoid precision issues in the checks themselves. */
42 double3x3 dmat;
43 copy_m3d_m3(dmat.ptr(), mat);
44
45 /* Check individual axis scaling. */
46 EXPECT_NEAR(math::length(dmat[0]), 1.0, epsilon_scale);
47 EXPECT_NEAR(math::length(dmat[1]), 1.0, epsilon_scale);
48 EXPECT_NEAR(math::length(dmat[2]), 1.0, epsilon_scale);
49
50 /* Check orthogonality. */
51 EXPECT_NEAR(dot_v3v3_db(dmat[0], dmat[1]), 0.0, epsilon_ortho);
52 EXPECT_NEAR(dot_v3v3_db(dmat[0], dmat[2]), 0.0, epsilon_ortho);
53 EXPECT_NEAR(dot_v3v3_db(dmat[1], dmat[2]), 0.0, epsilon_ortho);
54
55 /* Check determinant to detect flipping and as a secondary volume change check. */
56 double determinant = math::determinant(dmat);
57
58 EXPECT_NEAR(determinant, 1.0, epsilon_ortho);
59
60 return determinant;
61}
62
64{
65 float unit_matrix[3][3];
66 float roll;
67
68 unit_m3(unit_matrix);
69
70 /* Any vector with a unit matrix should return zero roll. */
71 mat3_vec_to_roll(unit_matrix, unit_matrix[0], &roll);
72 EXPECT_FLOAT_EQ(0.0f, roll);
73
74 mat3_vec_to_roll(unit_matrix, unit_matrix[1], &roll);
75 EXPECT_FLOAT_EQ(0.0f, roll);
76
77 mat3_vec_to_roll(unit_matrix, unit_matrix[2], &roll);
78 EXPECT_FLOAT_EQ(0.0f, roll);
79
80 {
81 /* Non-unit vector. */
82 float vector[3] = {1.0f, 1.0f, 1.0f};
83 mat3_vec_to_roll(unit_matrix, vector, &roll);
84 EXPECT_NEAR(0.0f, roll, FLOAT_EPSILON);
85
86 /* Normalized version of the above vector. */
87 normalize_v3(vector);
88 mat3_vec_to_roll(unit_matrix, vector, &roll);
89 EXPECT_NEAR(0.0f, roll, FLOAT_EPSILON);
90 }
91}
92
93TEST(mat3_vec_to_roll, Rotationmatrix)
94{
95 float rotation_matrix[3][3];
96 float roll;
97
98 const float rot_around_x[3] = {1.234f, 0.0f, 0.0f};
99 eul_to_mat3(rotation_matrix, rot_around_x);
100
101 {
102 const float unit_axis_x[3] = {1.0f, 0.0f, 0.0f};
103 mat3_vec_to_roll(rotation_matrix, unit_axis_x, &roll);
104 EXPECT_NEAR(1.234f, roll, FLOAT_EPSILON);
105 }
106
107 {
108 const float unit_axis_y[3] = {0.0f, 1.0f, 0.0f};
109 mat3_vec_to_roll(rotation_matrix, unit_axis_y, &roll);
110 EXPECT_NEAR(0, roll, FLOAT_EPSILON);
111 }
112
113 {
114 const float unit_axis_z[3] = {0.0f, 0.0f, 1.0f};
115 mat3_vec_to_roll(rotation_matrix, unit_axis_z, &roll);
116 EXPECT_NEAR(0, roll, FLOAT_EPSILON);
117 }
118
119 {
120 const float between_x_and_y[3] = {1.0f, 1.0f, 0.0f};
121 mat3_vec_to_roll(rotation_matrix, between_x_and_y, &roll);
122 EXPECT_NEAR(0.57158958f, roll, FLOAT_EPSILON);
123 }
124}
125
127static double test_vec_roll_to_mat3_normalized(const float input[3],
128 float roll,
129 const float expected_roll_mat[3][3],
130 bool normalize = true)
131{
132 float input_normalized[3];
133 float roll_mat[3][3];
134
135 if (normalize) {
136 /* The vector is re-normalized to replicate the actual usage. */
137 normalize_v3_v3(input_normalized, input);
138 }
139 else {
140 copy_v3_v3(input_normalized, input);
141 }
142
143 vec_roll_to_mat3_normalized(input_normalized, roll, roll_mat);
144
145 EXPECT_V3_NEAR(roll_mat[1], input_normalized, FLT_EPSILON);
146
147 if (expected_roll_mat) {
148 EXPECT_M3_NEAR(roll_mat, expected_roll_mat, FLT_EPSILON);
149 }
150
152}
153
155static double find_flip_boundary(double x, double z)
156{
157 /* Irrational scale factor to ensure values aren't 'nice', have a lot of rounding errors,
158 * and can't accidentally produce the exact result returned by the special case. */
159 const double scale = M_1_PI / 10;
160 double theta = x * x + z * z;
161 double minv = 0, maxv = 1e-2;
162
163 while (maxv - minv > FLT_EPSILON * 1e-3) {
164 double mid = (minv + maxv) / 2;
165
166 float roll_mat[3][3];
167 float input[3] = {float(x * mid * scale),
168 -float(sqrt(1 - theta * mid * mid) * scale),
169 float(z * mid * scale)};
170
171 normalize_v3(input);
172 vec_roll_to_mat3_normalized(input, 0, roll_mat);
173
174 /* The special case assigns exact constants rather than computing. */
175 if (roll_mat[0][0] == -1 && roll_mat[0][1] == 0 && roll_mat[2][1] == 0) {
176 minv = mid;
177 }
178 else {
179 maxv = mid;
180 }
181 }
182
183 return sqrt(theta) * (minv + maxv) * 0.5;
184}
185
187{
188 EXPECT_NEAR(find_flip_boundary(0, 1), 2.50e-4, 0.01e-4);
189}
190
192{
193 EXPECT_NEAR(find_flip_boundary(1, 1), 2.50e-4, 0.01e-4);
194}
195
196/* Test cases close to the -Y axis. */
198{
199 /* If normalized_vector is -Y, simple symmetry by Z axis. */
200 const float input[3] = {0.0f, -1.0f, 0.0f};
201 const float expected_roll_mat[3][3] = {
202 {-1.0f, 0.0f, 0.0f}, {0.0f, -1.0f, 0.0f}, {0.0f, 0.0f, 1.0f}};
203 test_vec_roll_to_mat3_normalized(input, 0.0f, expected_roll_mat, false);
204}
205
207{
208 /* If normalized_vector is close to -Y and
209 * it has X and Z values below a threshold,
210 * simple symmetry by Z axis. */
211 const float input[3] = {1e-24, -0.999999881, 0};
212 const float expected_roll_mat[3][3] = {
213 {-1.0f, 0.0f, 0.0f}, {0.0f, -1.0f, 0.0f}, {0.0f, 0.0f, 1.0f}};
214 test_vec_roll_to_mat3_normalized(input, 0.0f, expected_roll_mat, false);
215}
216
218{
219 /* If normalized_vector is in a critical range close to -Y, apply the special case. */
220 const float input[3] = {2.5e-4f, -0.999999881f, 2.5e-4f}; /* Corner Case. */
221 const float expected_roll_mat[3][3] = {{0.000000f, -2.5e-4f, -1.000000f},
222 {2.5e-4f, -0.999999881f, 2.5e-4f},
223 {-1.000000f, -2.5e-4f, 0.000000f}};
224 test_vec_roll_to_mat3_normalized(input, 0.0f, expected_roll_mat, false);
225}
226
227/* Test 90 degree rotations. */
229{
230 /* Rotate 90 around Z. */
231 const float input[3] = {1, 0, 0};
232 const float expected_roll_mat[3][3] = {{0, -1, 0}, {1, 0, 0}, {0, 0, 1}};
233 test_vec_roll_to_mat3_normalized(input, 0.0f, expected_roll_mat);
234}
235
237{
238 /* Rotate 90 around Z. */
239 const float input[3] = {-1, 0, 0};
240 const float expected_roll_mat[3][3] = {{0, 1, 0}, {-1, 0, 0}, {0, 0, 1}};
241 test_vec_roll_to_mat3_normalized(input, 0.0f, expected_roll_mat);
242}
243
245{
246 /* Rotate 90 around X. */
247 const float input[3] = {0, 0, -1};
248 const float expected_roll_mat[3][3] = {{1, 0, 0}, {0, 0, -1}, {0, 1, 0}};
249 test_vec_roll_to_mat3_normalized(input, 0.0f, expected_roll_mat);
250}
251
253{
254 /* Rotate 90 around X. */
255 const float input[3] = {0, 0, 1};
256 const float expected_roll_mat[3][3] = {{1, 0, 0}, {0, 0, 1}, {0, -1, 0}};
257 test_vec_roll_to_mat3_normalized(input, 0.0f, expected_roll_mat);
258}
259
260/* Test the general case when the vector is far enough from -Y. */
262{
263 const float input[3] = {1.0f, 1.0f, 1.0f}; /* Arbitrary Value. */
264 const float expected_roll_mat[3][3] = {{0.788675129f, -0.577350259f, -0.211324856f},
265 {0.577350259f, 0.577350259f, 0.577350259f},
266 {-0.211324856f, -0.577350259f, 0.788675129f}};
267 test_vec_roll_to_mat3_normalized(input, 0.0f, expected_roll_mat);
268}
269
271{
272 const float input[3] = {1.0f, -1.0f, 1.0f}; /* Arbitrary Value. */
273 const float expected_roll_mat[3][3] = {{0.211324856f, -0.577350259f, -0.788675129f},
274 {0.577350259f, -0.577350259f, 0.577350259f},
275 {-0.788675129f, -0.577350259f, 0.211324856f}};
276 test_vec_roll_to_mat3_normalized(input, 0.0f, expected_roll_mat);
277}
278
280{
281 const float input[3] = {-1.0f, -1.0f, 1.0f}; /* Arbitrary Value. */
282 const float expected_roll_mat[3][3] = {{0.211324856f, 0.577350259f, 0.788675129f},
283 {-0.577350259f, -0.577350259f, 0.577350259f},
284 {0.788675129f, -0.577350259f, 0.211324856f}};
285 test_vec_roll_to_mat3_normalized(input, 0.0f, expected_roll_mat);
286}
287
289{
290 const float input[3] = {-1.0f, -1.0f, -1.0f}; /* Arbitrary Value. */
291 const float expected_roll_mat[3][3] = {{0.211324856f, 0.577350259f, -0.788675129f},
292 {-0.577350259f, -0.577350259f, -0.577350259f},
293 {-0.788675129f, 0.577350259f, 0.211324856f}};
294 test_vec_roll_to_mat3_normalized(input, 0.0f, expected_roll_mat);
295}
296
297/* Test roll. */
299{
300 const float input[3] = {1.0f, 1.0f, 1.0f}; /* Arbitrary Value. */
301 const float expected_roll_mat[3][3] = {{0.211324856f, 0.577350259f, -0.788675129f},
302 {0.577350259f, 0.577350259f, 0.577350259f},
303 {0.788675129f, -0.577350259f, -0.211324856f}};
304 test_vec_roll_to_mat3_normalized(input, float(M_PI_2), expected_roll_mat);
305}
306
308static double test_vec_roll_to_mat3_orthogonal(double s, double x, double z)
309{
310 const float input[3] = {float(x), float(s * sqrt(1 - x * x - z * z)), float(z)};
311
312 return test_vec_roll_to_mat3_normalized(input, 0.0f, nullptr);
313}
314
316static void test_vec_roll_to_mat3_orthogonal(double s, double x1, double x2, double y1, double y2)
317{
318 const int count = 5000;
319 double delta = 0;
320 double tmax = 0;
321
322 for (int i = 0; i <= count; i++) {
323 double t = double(i) / count;
324 double det = test_vec_roll_to_mat3_orthogonal(s, interpd(x2, x1, t), interpd(y2, y1, t));
325
326 /* Find and report maximum error in the matrix determinant. */
327 double curdelta = abs(det - 1);
328 if (curdelta > delta) {
329 delta = curdelta;
330 tmax = t;
331 }
332 }
333
334 printf(" Max determinant deviation %.10f at %f.\n", delta, tmax);
335}
336
337#define TEST_VEC_ROLL_TO_MAT3_ORTHOGONAL(name, s, x1, x2, y1, y2) \
338 TEST(vec_roll_to_mat3_normalized, name) \
339 { \
340 test_vec_roll_to_mat3_orthogonal(s, x1, x2, y1, y2); \
341 }
342
343/* Moving from -Y towards X. */
344TEST_VEC_ROLL_TO_MAT3_ORTHOGONAL(OrthoN_000_005, -1, 0, 0, 3e-4, 0.005)
345TEST_VEC_ROLL_TO_MAT3_ORTHOGONAL(OrthoN_000_010, -1, 0, 0, 0.005, 0.010)
346TEST_VEC_ROLL_TO_MAT3_ORTHOGONAL(OrthoN_000_050, -1, 0, 0, 0.010, 0.050)
347TEST_VEC_ROLL_TO_MAT3_ORTHOGONAL(OrthoN_000_100, -1, 0, 0, 0.050, 0.100)
348TEST_VEC_ROLL_TO_MAT3_ORTHOGONAL(OrthoN_000_200, -1, 0, 0, 0.100, 0.200)
349TEST_VEC_ROLL_TO_MAT3_ORTHOGONAL(OrthoN_000_300, -1, 0, 0, 0.200, 0.300)
350
351/* Moving from -Y towards X and Y. */
352TEST_VEC_ROLL_TO_MAT3_ORTHOGONAL(OrthoN_005_005, -1, 3e-4, 0.005, 3e-4, 0.005)
353TEST_VEC_ROLL_TO_MAT3_ORTHOGONAL(OrthoN_010_010, -1, 0.005, 0.010, 0.005, 0.010)
354TEST_VEC_ROLL_TO_MAT3_ORTHOGONAL(OrthoN_050_050, -1, 0.010, 0.050, 0.010, 0.050)
355TEST_VEC_ROLL_TO_MAT3_ORTHOGONAL(OrthoN_100_100, -1, 0.050, 0.100, 0.050, 0.100)
356TEST_VEC_ROLL_TO_MAT3_ORTHOGONAL(OrthoN_200_200, -1, 0.100, 0.200, 0.100, 0.200)
357
358/* Moving from +Y towards X. */
359TEST_VEC_ROLL_TO_MAT3_ORTHOGONAL(OrthoP_000_005, 1, 0, 0, 0, 0.005)
360TEST_VEC_ROLL_TO_MAT3_ORTHOGONAL(OrthoP_000_100, 1, 0, 0, 0.005, 0.100)
361
362/* Moving from +Y towards X and Y. */
363TEST_VEC_ROLL_TO_MAT3_ORTHOGONAL(OrthoP_005_005, 1, 0, 0.005, 0, 0.005)
364TEST_VEC_ROLL_TO_MAT3_ORTHOGONAL(OrthoP_100_100, 1, 0.005, 0.100, 0.005, 0.100)
365
366class BKE_armature_find_selected_bones_test : public testing::Test {
367 protected:
369 Bone bone1 = {}, bone2 = {}, bone3 = {};
370
371 void SetUp() override
372 {
373 STRNCPY(bone1.name, "bone1");
374 STRNCPY(bone2.name, "bone2");
375 STRNCPY(bone3.name, "bone3");
376
377 BLI_addtail(&arm.bonebase, &bone1); /* bone1 is root bone. */
378 BLI_addtail(&arm.bonebase, &bone2); /* bone2 is root bone. */
379 BLI_addtail(&bone2.childbase, &bone3); /* bone3 has bone2 as parent. */
380 }
381};
382
384{
385 bone1.flag = BONE_SELECTED;
386 bone2.flag = eBone_Flag{};
387 bone3.flag = BONE_SELECTED;
388
389 std::vector<Bone *> seen_bones;
390 auto callback = [&](Bone *bone) { seen_bones.push_back(bone); };
391
393
394 ASSERT_EQ(seen_bones.size(), 2) << "Expected 2 selected bones, got " << seen_bones.size();
395 EXPECT_EQ(seen_bones[0], &bone1);
396 EXPECT_EQ(seen_bones[1], &bone3);
397
398 EXPECT_FALSE(result.all_bones_selected); /* Bone 2 was not selected. */
399 EXPECT_FALSE(result.no_bones_selected); /* Bones 1 and 3 were selected. */
400}
401
403{
404 bone1.flag = bone2.flag = bone3.flag = eBone_Flag{};
405
406 std::vector<Bone *> seen_bones;
407 auto callback = [&](Bone *bone) { seen_bones.push_back(bone); };
408
410
411 EXPECT_TRUE(seen_bones.empty()) << "Expected no selected bones, got " << seen_bones.size();
412 EXPECT_FALSE(result.all_bones_selected);
413 EXPECT_TRUE(result.no_bones_selected);
414}
415
417{
418 bone1.flag = bone2.flag = bone3.flag = BONE_SELECTED;
419
420 std::vector<Bone *> seen_bones;
421 auto callback = [&](Bone *bone) { seen_bones.push_back(bone); };
422
424
425 ASSERT_EQ(seen_bones.size(), 3) << "Expected 3 selected bones, got " << seen_bones.size();
426 EXPECT_EQ(seen_bones[0], &bone1);
427 EXPECT_EQ(seen_bones[1], &bone2);
428 EXPECT_EQ(seen_bones[2], &bone3);
429
430 EXPECT_TRUE(result.all_bones_selected);
431 EXPECT_FALSE(result.no_bones_selected);
432}
433
435 public:
436 Main *bmain = nullptr;
437 bArmature *armature = nullptr;
438 Bone *bone_root = nullptr;
439 Bone *bone_child1 = nullptr;
440 Bone *bone_child2 = nullptr;
441
443 {
444 armature = BKE_armature_add(bmain, "Test Armature");
445
446 bone_root = MEM_new<Bone>(__func__);
447 bone_child1 = MEM_new<Bone>(__func__);
448 bone_child2 = MEM_new<Bone>(__func__);
449
452
453 STRNCPY(bone_root->name, "bone_root");
454 STRNCPY(bone_child1->name, "bone_child1");
455 STRNCPY(bone_child2->name, "bone_child2");
456
460 }
461};
462
464 public:
465 void SetUp() override
466 {
468 }
469
470 void TearDown() override
471 {
473 }
474};
475
476TEST_F(ArmatureBoneIndexTest, armature_init_data)
477{
478 create_armature();
479
480 EXPECT_EQ(0, armature->runtime->bones.size())
481 << "After low-level bone adding, the runtime array should be empty.";
482 ASSERT_NE(0, armature->id.session_uid);
483 EXPECT_EQ(uint64_t(armature->id.session_uid) << 32, armature->runtime->bones_generation_count);
484
485 EXPECT_EQ(bone_root, armature->bone_get_indexed(0));
486 EXPECT_EQ(3, armature->runtime->bones.size())
487 << "After calling bone_get_index(), the runtime array should be filled.";
488
489 EXPECT_EQ(bone_child1, armature->bone_get_indexed(1));
490
491 armature->runtime->bones_tag_rebuild();
492 EXPECT_EQ(bone_child2, armature->bone_get_indexed(2));
493 EXPECT_EQ(3, armature->runtime->bones.size())
494 << "After clearing the bone array, calling bone_get_index() should rebuild it.";
495}
496
497TEST_F(ArmatureBoneIndexTest, armature_copy_data)
498{
499 create_armature();
500 EXPECT_EQ(bone_root, armature->bone_get_indexed(0)); /* Ensure the bone array is created. */
501
502 /* Regular copy, should get its own generation counter. */
503 bArmature *copy = id_cast<bArmature *>(BKE_id_copy(this->bmain, &armature->id));
504 ASSERT_NE(0, armature->id.session_uid);
505 EXPECT_NE(armature->runtime->bones_generation_count, copy->runtime->bones_generation_count);
506 EXPECT_EQ(3, copy->runtime->bones.size());
507
508 /* Getting the indexed bone should return a copied bone, and not the original. */
509 Bone *copybone_root = copy->bonebase.first();
510 EXPECT_EQ(copybone_root, copy->bone_get_indexed(0));
511
512 /* CoW-copy, should reuse the generation counter. */
513 bArmature *cow_copy = id_cast<bArmature *>(
514 BKE_id_copy_in_lib(this->bmain,
515 std::nullopt,
516 &armature->id,
517 std::nullopt,
518 nullptr,
520 ASSERT_NE(0, armature->id.session_uid);
521 EXPECT_EQ(armature->runtime->bones_generation_count, cow_copy->runtime->bones_generation_count);
522 EXPECT_EQ(3, cow_copy->runtime->bones.size());
523}
524
526 public:
527 Object *object = nullptr;
528
529 void SetUp() override
530 {
532 }
533
534 void TearDown() override
535 {
537 }
538
540 {
542 object = BKE_object_add_only_object(bmain, OB_ARMATURE, "PoseObject");
543 object->data = &armature->id;
545 BKE_pose_rebuild(bmain, object, armature, /*do_id_user=*/true);
546 }
547};
548
550{
551 create_object();
552
553 bPoseChannel *pchan_root = BKE_pose_channel_find_name(object->pose, bone_root->name);
554 bPoseChannel *pchan_child1 = BKE_pose_channel_find_name(object->pose, bone_child1->name);
555 bPoseChannel *pchan_child2 = BKE_pose_channel_find_name(object->pose, bone_child2->name);
556 EXPECT_EQ(bone_root, pchan_root->bone_get(*object));
557 EXPECT_EQ(bone_child1, pchan_child1->bone_get(*object));
558 EXPECT_EQ(bone_child2, pchan_child2->bone_get(*object));
559}
560
561TEST_F(PoseBoneIndexTest, reassign_armature_copy)
562{
563 create_object();
564
565 bPoseChannel *pchan_root = BKE_pose_channel_find_name(object->pose, bone_root->name);
566 bPoseChannel *pchan_child1 = BKE_pose_channel_find_name(object->pose, bone_child1->name);
567 bPoseChannel *pchan_child2 = BKE_pose_channel_find_name(object->pose, bone_child2->name);
568
569 /* Create a copy of the armature. */
570 bArmature *copy = id_cast<bArmature *>(BKE_id_copy(this->bmain, &armature->id));
571 Bone *copybone_root = BKE_armature_find_bone_name(copy, bone_root->name);
572 Bone *copybone_child1 = BKE_armature_find_bone_name(copy, bone_child1->name);
573 Bone *copybone_child2 = BKE_armature_find_bone_name(copy, bone_child2->name);
574
575 /* Check that the copy actually copied all the bones. Since this test trusts these pointers,
576 * better to be safe than sorry. */
577 ASSERT_NE(bone_root, copybone_root);
578 ASSERT_NE(bone_child1, copybone_child1);
579 ASSERT_NE(bone_child2, copybone_child2);
580
581 /* Check that the copy did indeed not change the order of the bones. */
582 Array<Bone *> expected_bones = {copybone_root, copybone_child1, copybone_child2};
583 ASSERT_EQ(expected_bones, copy->runtime->bones);
584
585 /* Assign the armature copy. After this, getting bone pointers should just work without
586 * explicitly rebuilding the pose. */
587 id_us_min(&armature->id);
588 object->data = &copy->id;
589 id_us_plus(&copy->id);
590
591 EXPECT_EQ(copybone_root, pchan_root->bone_get(*object));
592 EXPECT_EQ(copybone_child1, pchan_child1->bone_get(*object));
593 EXPECT_EQ(copybone_child2, pchan_child2->bone_get(*object));
594
595 /* Getting from the original armature should also work, because bone indices haven't changed. */
596 EXPECT_EQ(bone_root, pchan_root->bone_get(*armature));
597 EXPECT_EQ(bone_child1, pchan_child1->bone_get(*armature));
598 EXPECT_EQ(bone_child2, pchan_child2->bone_get(*armature));
599}
600
601TEST_F(PoseBoneIndexTest, pose_rebuild_test)
602{
603 /* After exiting Armature Edit mode, all Bones get recreated from EditBones. This clears the
604 * bones array, which triggers a rebuild of that array on first access. But, when this access
605 * happens via pchan->bone_get(), the pchan's bone index may need rebuilding as well. */
606
607 create_object(); /* Create armature + pose with 3 bones. */
608
609 bPoseChannel *pchan_root = BKE_pose_channel_find_name(object->pose, bone_root->name);
610 bPoseChannel *pchan_child1 = BKE_pose_channel_find_name(object->pose, bone_child1->name);
611
612 /* Mimic going into Armature Edit mode and deleting bone 'child2'. */
613 BLI_remlink(&bone_root->childbase, bone_child2);
614 MEM_delete(bone_child2);
615 bone_child2 = nullptr;
616 armature->runtime->bones_tag_rebuild();
617
618 EXPECT_EQ(bone_root, pchan_root->bone_get(*object));
619 EXPECT_EQ(bone_child1, pchan_child1->bone_get(*object));
620 /* This will fail, because bone_get() asserts the bone index is valid. */
621 // EXPECT_EQ(nullptr, pchan_child2->bone_get(*object));
622
623 const uint64_t armature_generation_count = armature->runtime->bones_generation_count;
624 const uint64_t object_generation_count = object->runtime->pose_bones_generation_count;
625 EXPECT_EQ(armature_generation_count, object_generation_count);
626}
627
628} // namespace blender::bke::tests
Blender kernel action and pose functionality.
General operations, lookup, etc. for blender objects.
#define M_1_PI
#define M_PI_2
ID and Library types, which are fundamental for SDNA.
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define TEST_VEC_ROLL_TO_MAT3_ORTHOGONAL(name, s, x1, x2, y1, y2)
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
#define FLT_EPSILON
b[1] b &&[1] b &&[1] b &&[1] b &&[1] b[3] b &&[1] b[3] float determinant(MatBase< C, R >) RET
VecBase< float, D > normalize(VecOp< float, D >) RET
#define abs
float3x3 scale(float3x3 mat, float2 scale)
#define EXPECT_FALSE(result)
#define EXPECT_NE(result, expect)
#define EXPECT_TRUE(result)
#define EXPECT_NEAR(result, expect, threshold)
static double find_flip_boundary(double x, double z)
TEST(mat3_vec_to_roll, UnitMatrix)
static const float SCALE_EPSILON
static double test_vec_roll_to_mat3_orthogonal(double s, double x, double z)
static double EXPECT_M3_ORTHOGONAL(const float mat[3][3], double epsilon_scale, double epsilon_ortho)
static const float ORTHO_EPSILON
static const float FLOAT_EPSILON
static double test_vec_roll_to_mat3_normalized(const float input[3], float roll, const float expected_roll_mat[3][3], bool normalize=true)
TEST_F(AnimDataTest, BKE_fcurves_id_cb_test)
SelectedBonesResult BKE_armature_find_selected_bones(const bArmature *armature, SelectedBoneCallback callback)
T length(const VecBase< T, Size > &a)
T determinant(const MatBase< T, Size, Size > &mat)
void mat3_vec_to_roll(const float mat[3][3], const float vec[3], float *r_roll)
unsigned __int64 uint64_t
Definition BKE_text.h:91
void eul_to_mat3(float mat[3][3], const float eul[3])
bArmature * BKE_armature_add(Main *bmain, const char *name)
char * STRNCPY(char(&dst)[N], const char *src)
void BKE_pose_rebuild(Main *bmain, Object *ob, bArmature *arm, bool do_id_user)
MINLINE double dot_v3v3_db(const double a[3], const double b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void copy_v3_v3(float r[3], const float a[3])
void id_us_plus(ID *id)
Definition lib_id.cc:433
MINLINE double interpd(double target, double origin, double t)
Main * BKE_main_new()
Definition main.cc:93
MatBase< double, 3, 3 > double3x3
@ LIB_ID_COPY_SET_COPIED_ON_WRITE
@ LIB_ID_COPY_DEFAULT
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
sqrt(x)+1/max(0
void copy_m3d_m3(double m1[3][3], const float m2[3][3])
MINLINE float normalize_v3_v3(float r[3], const float a[3])
void BKE_main_free(Main *bmain)
Definition main.cc:202
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
MINLINE float normalize_v3(float n[3])
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
void unit_m3(float m[3][3])
void vec_roll_to_mat3_normalized(const float nor[3], float roll, float r_mat[3][3])
Object * BKE_object_add_only_object(Main *bmain, ObjectType type, const char *name) ATTR_RETURNS_NONNULL
Definition object.cc:2265
ID * BKE_id_copy(Main *bmain, const ID *id)
Definition lib_id.cc:856
void id_us_min(ID *id)
Definition lib_id.cc:441
Bone * BKE_armature_find_bone_name(bArmature *arm, const char *name)
bPoseChannel * BKE_pose_channel_find_name(const bPose *pose, const char *name)
struct ID * BKE_id_copy_in_lib(Main *bmain, std::optional< Library * > owner_library, const ID *id, std::optional< const ID * > new_owner_id, ID **new_id_p, int flag)
Definition lib_id.cc:749
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
struct Bone * parent
ListBaseT< Bone > childbase
const c_style_mat & ptr() const
bke::bArmature_Runtime * runtime
ListBaseT< Bone > bonebase
bke::motionpath::Runtime * runtime