Blender V5.3
bone_collections_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_listbase.hh"
6#include "BLI_string.hh"
7
8#include "BLT_translation.hh"
9
10#include "BKE_armature.hh"
11#include "BKE_global.hh"
12#include "BKE_gtest_base.hh"
13#include "BKE_idtype.hh"
14#include "BKE_lib_id.hh"
15#include "BKE_main.hh"
16
17#include "MEM_guardedalloc.h"
18
21
22#include "testing/testing.h"
23
25
27
28TEST_F(AnimBoneCollectionTest, bonecoll_new_free)
29{
30 BoneCollection *bcoll = ANIM_bonecoll_new("some name");
31 EXPECT_NE(nullptr, bcoll);
32 EXPECT_EQ("some name", std::string(bcoll->name));
33 EXPECT_TRUE(bcoll->bones.is_empty());
36 bcoll->flags);
37 ANIM_bonecoll_free(bcoll);
38}
39
40TEST_F(AnimBoneCollectionTest, bonecoll_default_name)
41{
42 {
44 EXPECT_EQ(DATA_("Bones"), std::string(bcoll->name));
45 ANIM_bonecoll_free(bcoll);
46 }
47
48 {
49 BoneCollection *bcoll = ANIM_bonecoll_new(nullptr);
50 EXPECT_EQ(DATA_("Bones"), std::string(bcoll->name));
51 ANIM_bonecoll_free(bcoll);
52 }
53}
54
56 protected:
58 Bone bone1 = {}, bone2 = {}, bone3 = {};
60
61 void SetUp() override
62 {
64 G_MAIN = bmain;
65 STRNCPY(arm.id.name, "ARArmature");
66 STRNCPY(bone1.name, "bone1");
67 STRNCPY(bone2.name, "bone2");
68 STRNCPY(bone3.name, "bone3");
69
70 BLI_addtail(&arm.bonebase, &bone1); /* bone1 is root bone. */
71 BLI_addtail(&arm.bonebase, &bone2); /* bone2 is root bone. */
72 BLI_addtail(&bone2.childbase, &bone3); /* bone3 has bone2 as parent. */
73
74 arm.runtime = MEM_new<bke::bArmature_Runtime>(__func__);
76 }
77
78 void TearDown() override
79 {
80 /* Avoid freeing the bones, as they are part of this struct and not owned by
81 * the armature. */
82 arm.bonebase.clear_no_delete();
83
85
87 G_MAIN = nullptr;
88 }
89};
90
91TEST_F(ArmatureBoneCollections, armature_owned_collections)
92{
93 BoneCollection *bcoll1 = ANIM_armature_bonecoll_new(&arm, "collection");
94 BoneCollection *bcoll2 = ANIM_armature_bonecoll_new(&arm, "collection");
95
96 EXPECT_EQ(std::string("collection"), std::string(bcoll1->name));
97 EXPECT_EQ(std::string("collection.001"), std::string(bcoll2->name));
98
100 ANIM_armature_bonecoll_remove(&arm, bcoll2);
101}
102
103TEST_F(ArmatureBoneCollections, collection_hierarchy_creation)
104{
105 /* Implicit root: */
106 BoneCollection *bcoll_root_0 = ANIM_armature_bonecoll_new(&arm, "wortel");
107 /* Explicit root: */
108 BoneCollection *bcoll_root_1 = ANIM_armature_bonecoll_new(&arm, "wortel", -1);
109
110 ASSERT_EQ(0, armature_bonecoll_find_index(&arm, bcoll_root_0));
111 ASSERT_EQ(1, armature_bonecoll_find_index(&arm, bcoll_root_1));
112
113 /* Child of bcoll at index 0: */
114 BoneCollection *bcoll_child_of_0 = ANIM_armature_bonecoll_new(&arm, "koter", 0);
115 /* Child of bcoll at index 1: */
116 BoneCollection *bcoll_child_of_1 = ANIM_armature_bonecoll_new(&arm, "koter", 1);
117
118 ASSERT_EQ(4, arm.collection_array_num);
119 EXPECT_EQ(0, armature_bonecoll_find_index(&arm, bcoll_root_0));
120 EXPECT_EQ(1, armature_bonecoll_find_index(&arm, bcoll_root_1));
121 EXPECT_EQ(2, armature_bonecoll_find_index(&arm, bcoll_child_of_0));
122 EXPECT_EQ(3, armature_bonecoll_find_index(&arm, bcoll_child_of_1));
123
124 /* Add another child of bcoll_root_0, which should push bcoll_child_of_1 further down the array.
125 */
126 BoneCollection *bcoll_another_child_of_0 = ANIM_armature_bonecoll_new(&arm, "koter", 0);
127 ASSERT_EQ(5, arm.collection_array_num);
128 EXPECT_EQ(0, armature_bonecoll_find_index(&arm, bcoll_root_0));
129 EXPECT_EQ(1, armature_bonecoll_find_index(&arm, bcoll_root_1));
130 EXPECT_EQ(2, armature_bonecoll_find_index(&arm, bcoll_child_of_0));
131 EXPECT_EQ(3, armature_bonecoll_find_index(&arm, bcoll_another_child_of_0));
132 EXPECT_EQ(4, armature_bonecoll_find_index(&arm, bcoll_child_of_1));
133
134 /* Make sure the names remain unique within the entire Armature, and not just between siblings
135 * (i.e. a unique 'path' is not strong enough). */
136 EXPECT_EQ(std::string("wortel"), std::string(bcoll_root_0->name));
137 EXPECT_EQ(std::string("wortel.001"), std::string(bcoll_root_1->name));
138 EXPECT_EQ(std::string("koter"), std::string(bcoll_child_of_0->name));
139 EXPECT_EQ(std::string("koter.001"), std::string(bcoll_child_of_1->name));
140 EXPECT_EQ(std::string("koter.002"), std::string(bcoll_another_child_of_0->name));
141
142 /* Test the internal hierarchy bookkeeping. */
143 EXPECT_EQ(2, arm.collection_root_count);
144 EXPECT_EQ(2, bcoll_root_0->child_count);
145 EXPECT_EQ(1, bcoll_root_1->child_count);
146 EXPECT_EQ(0, bcoll_child_of_0->child_count);
147 EXPECT_EQ(0, bcoll_another_child_of_0->child_count);
148 EXPECT_EQ(0, bcoll_child_of_1->child_count);
149
150 EXPECT_EQ(2, bcoll_root_0->child_index);
151 EXPECT_EQ(4, bcoll_root_1->child_index);
152 EXPECT_EQ(0, bcoll_child_of_0->child_index);
153 EXPECT_EQ(0, bcoll_another_child_of_0->child_index);
154 EXPECT_EQ(0, bcoll_child_of_1->child_index);
155
156 /* TODO: test with deeper hierarchy. */
157}
158
159TEST_F(ArmatureBoneCollections, collection_hierarchy_removal)
160{
161 /* Set up a small hierarchy. */
162 BoneCollection *bcoll_root_0 = ANIM_armature_bonecoll_new(&arm, "root_0");
163 BoneCollection *bcoll_root_1 = ANIM_armature_bonecoll_new(&arm, "root_1");
164 BoneCollection *bcoll_r0_child0 = ANIM_armature_bonecoll_new(&arm, "r0_child0", 0);
165 BoneCollection *bcoll_r1_child0 = ANIM_armature_bonecoll_new(&arm, "r1_child0", 1);
166 BoneCollection *bcoll_r0_child1 = ANIM_armature_bonecoll_new(&arm, "r0_child1", 0);
167 BoneCollection *bcoll_r0_child2 = ANIM_armature_bonecoll_new(&arm, "r0_child2", 0);
168
169 ASSERT_EQ(2, arm.collection_root_count);
170 ASSERT_EQ(6, arm.collection_array_num);
171 ASSERT_STREQ(bcoll_root_0->name, arm.collection_array[0]->name);
172 ASSERT_STREQ(bcoll_root_1->name, arm.collection_array[1]->name);
173 ASSERT_STREQ(bcoll_r0_child0->name, arm.collection_array[2]->name);
174 ASSERT_STREQ(bcoll_r0_child1->name, arm.collection_array[3]->name);
175 ASSERT_STREQ(bcoll_r0_child2->name, arm.collection_array[4]->name);
176 ASSERT_STREQ(bcoll_r1_child0->name, arm.collection_array[5]->name);
177
178 ASSERT_EQ(2, arm.collection_array[0]->child_index);
179 ASSERT_EQ(5, arm.collection_array[1]->child_index);
180 ASSERT_EQ(0, arm.collection_array[2]->child_index);
181 ASSERT_EQ(0, arm.collection_array[3]->child_index);
182 ASSERT_EQ(0, arm.collection_array[4]->child_index);
183 ASSERT_EQ(0, arm.collection_array[5]->child_index);
184
185 ASSERT_EQ(3, arm.collection_array[0]->child_count);
186 ASSERT_EQ(1, arm.collection_array[1]->child_count);
187 ASSERT_EQ(0, arm.collection_array[2]->child_count);
188 ASSERT_EQ(0, arm.collection_array[3]->child_count);
189 ASSERT_EQ(0, arm.collection_array[4]->child_count);
190 ASSERT_EQ(0, arm.collection_array[5]->child_count);
191
192 /* Remove the middle child of root_0. */
193 ANIM_armature_bonecoll_remove(&arm, bcoll_r0_child1);
194
195 ASSERT_EQ(2, arm.collection_root_count);
196 ASSERT_EQ(5, arm.collection_array_num);
197 EXPECT_STREQ(bcoll_root_0->name, arm.collection_array[0]->name);
198 EXPECT_STREQ(bcoll_root_1->name, arm.collection_array[1]->name);
199 EXPECT_STREQ(bcoll_r0_child0->name, arm.collection_array[2]->name);
200 EXPECT_STREQ(bcoll_r0_child2->name, arm.collection_array[3]->name);
201 EXPECT_STREQ(bcoll_r1_child0->name, arm.collection_array[4]->name);
202
203 EXPECT_EQ(2, arm.collection_array[0]->child_index);
204 EXPECT_EQ(4, arm.collection_array[1]->child_index);
205 EXPECT_EQ(0, arm.collection_array[2]->child_index);
206 EXPECT_EQ(0, arm.collection_array[3]->child_index);
207 EXPECT_EQ(0, arm.collection_array[4]->child_index);
208
209 EXPECT_EQ(2, arm.collection_array[0]->child_count);
210 EXPECT_EQ(1, arm.collection_array[1]->child_count);
211 EXPECT_EQ(0, arm.collection_array[2]->child_count);
212 EXPECT_EQ(0, arm.collection_array[3]->child_count);
213 EXPECT_EQ(0, arm.collection_array[4]->child_count);
214
215 /* Remove the first child of root_0. */
216 ANIM_armature_bonecoll_remove(&arm, bcoll_r0_child0);
217
218 ASSERT_EQ(2, arm.collection_root_count);
219 ASSERT_EQ(4, arm.collection_array_num);
220 EXPECT_STREQ(bcoll_root_0->name, arm.collection_array[0]->name);
221 EXPECT_STREQ(bcoll_root_1->name, arm.collection_array[1]->name);
222 EXPECT_STREQ(bcoll_r0_child2->name, arm.collection_array[2]->name);
223 EXPECT_STREQ(bcoll_r1_child0->name, arm.collection_array[3]->name);
224
225 EXPECT_EQ(2, arm.collection_array[0]->child_index);
226 EXPECT_EQ(3, arm.collection_array[1]->child_index);
227 EXPECT_EQ(0, arm.collection_array[2]->child_index);
228 EXPECT_EQ(0, arm.collection_array[3]->child_index);
229
230 EXPECT_EQ(1, arm.collection_array[0]->child_count);
231 EXPECT_EQ(1, arm.collection_array[1]->child_count);
232 EXPECT_EQ(0, arm.collection_array[2]->child_count);
233 EXPECT_EQ(0, arm.collection_array[3]->child_count);
234
235 /* Remove root_1 itself, which should make its only child a new root. */
236 ANIM_armature_bonecoll_remove(&arm, bcoll_root_1);
237
238 ASSERT_EQ(2, arm.collection_root_count);
239 ASSERT_EQ(3, arm.collection_array_num);
240 EXPECT_STREQ(bcoll_root_0->name, arm.collection_array[0]->name);
241 EXPECT_STREQ(bcoll_r1_child0->name, arm.collection_array[1]->name);
242 EXPECT_STREQ(bcoll_r0_child2->name, arm.collection_array[2]->name);
243
244 EXPECT_EQ(2, arm.collection_array[0]->child_index);
245 EXPECT_EQ(0, arm.collection_array[1]->child_index);
246 EXPECT_EQ(0, arm.collection_array[2]->child_index);
247
248 EXPECT_EQ(1, arm.collection_array[0]->child_count);
249 EXPECT_EQ(0, arm.collection_array[1]->child_count);
250 EXPECT_EQ(0, arm.collection_array[2]->child_count);
251}
252
253TEST_F(ArmatureBoneCollections, collection_hierarchy_removal__more_complex_remove_inner_child)
254{
255 /* Set up a slightly bigger hierarchy. Contrary to the other tests these are
256 * actually declared in array order. */
257 BoneCollection *bcoll_root_0 = ANIM_armature_bonecoll_new(&arm, "root_0");
258 BoneCollection *bcoll_root_1 = ANIM_armature_bonecoll_new(&arm, "root_1");
259 BoneCollection *bcoll_r0_child0 = ANIM_armature_bonecoll_new(&arm, "r0_child0", 0);
260 BoneCollection *bcoll_r0_child1 = ANIM_armature_bonecoll_new(&arm, "r0_child1", 0);
261 BoneCollection *bcoll_r0_child2 = ANIM_armature_bonecoll_new(&arm, "r0_child2", 0);
262 BoneCollection *bcoll_r0c0_child0 = ANIM_armature_bonecoll_new(&arm, "r0c0_child0", 2);
263 BoneCollection *bcoll_r0c0_child1 = ANIM_armature_bonecoll_new(&arm, "r0c0_child1", 2);
264 BoneCollection *bcoll_r0c0_child2 = ANIM_armature_bonecoll_new(&arm, "r0c0_child2", 2);
265
266 ASSERT_EQ(2, arm.collection_root_count);
267 ASSERT_EQ(8, arm.collection_array_num);
268 ASSERT_STREQ(bcoll_root_0->name, arm.collection_array[0]->name);
269 ASSERT_STREQ(bcoll_root_1->name, arm.collection_array[1]->name);
270 ASSERT_STREQ(bcoll_r0_child0->name, arm.collection_array[2]->name); /* Children of root_0. */
271 ASSERT_STREQ(bcoll_r0_child1->name, arm.collection_array[3]->name);
272 ASSERT_STREQ(bcoll_r0_child2->name, arm.collection_array[4]->name);
273 ASSERT_STREQ(bcoll_r0c0_child0->name, arm.collection_array[5]->name); /* Kids of r0_child0. */
274 ASSERT_STREQ(bcoll_r0c0_child1->name, arm.collection_array[6]->name);
275 ASSERT_STREQ(bcoll_r0c0_child2->name, arm.collection_array[7]->name);
276
277 ASSERT_EQ(2, arm.collection_array[0]->child_index);
278 ASSERT_EQ(0, arm.collection_array[1]->child_index);
279 ASSERT_EQ(5, arm.collection_array[2]->child_index);
280 ASSERT_EQ(0, arm.collection_array[3]->child_index);
281 ASSERT_EQ(0, arm.collection_array[4]->child_index);
282 ASSERT_EQ(0, arm.collection_array[5]->child_index);
283 ASSERT_EQ(0, arm.collection_array[6]->child_index);
284 ASSERT_EQ(0, arm.collection_array[7]->child_index);
285
286 ASSERT_EQ(3, arm.collection_array[0]->child_count);
287 ASSERT_EQ(0, arm.collection_array[1]->child_count);
288 ASSERT_EQ(3, arm.collection_array[2]->child_count);
289 ASSERT_EQ(0, arm.collection_array[3]->child_count);
290 ASSERT_EQ(0, arm.collection_array[4]->child_count);
291 ASSERT_EQ(0, arm.collection_array[5]->child_count);
292 ASSERT_EQ(0, arm.collection_array[6]->child_count);
293 ASSERT_EQ(0, arm.collection_array[7]->child_count);
294
295 /* Remove bcoll_r0_child0, which should make all of its children a child of root_0. */
296 ANIM_armature_bonecoll_remove(&arm, bcoll_r0_child0);
297
298 ASSERT_EQ(2, arm.collection_root_count);
299 ASSERT_EQ(7, arm.collection_array_num);
300 EXPECT_STREQ(bcoll_root_0->name, arm.collection_array[0]->name);
301 EXPECT_STREQ(bcoll_root_1->name, arm.collection_array[1]->name);
302 EXPECT_STREQ(bcoll_r0c0_child0->name, arm.collection_array[2]->name); /* Children of root_0. */
303 EXPECT_STREQ(bcoll_r0c0_child1->name, arm.collection_array[3]->name);
304 EXPECT_STREQ(bcoll_r0c0_child2->name, arm.collection_array[4]->name);
305 EXPECT_STREQ(bcoll_r0_child1->name, arm.collection_array[5]->name);
306 EXPECT_STREQ(bcoll_r0_child2->name, arm.collection_array[6]->name);
307
308 EXPECT_EQ(2, arm.collection_array[0]->child_index);
309 EXPECT_EQ(0, arm.collection_array[1]->child_index);
310 EXPECT_EQ(0, arm.collection_array[2]->child_index);
311 EXPECT_EQ(0, arm.collection_array[3]->child_index);
312 EXPECT_EQ(0, arm.collection_array[4]->child_index);
313 EXPECT_EQ(0, arm.collection_array[5]->child_index);
314 EXPECT_EQ(0, arm.collection_array[6]->child_count);
315
316 EXPECT_EQ(5, arm.collection_array[0]->child_count);
317 EXPECT_EQ(0, arm.collection_array[1]->child_count);
318 EXPECT_EQ(0, arm.collection_array[2]->child_count);
319 EXPECT_EQ(0, arm.collection_array[3]->child_count);
320 EXPECT_EQ(0, arm.collection_array[4]->child_count);
321 EXPECT_EQ(0, arm.collection_array[5]->child_count);
322 EXPECT_EQ(0, arm.collection_array[6]->child_count);
323
324 /* Remove root_0, which should make all of its children new roots. */
325 ANIM_armature_bonecoll_remove(&arm, bcoll_root_0);
326
327 ASSERT_EQ(6, arm.collection_root_count);
328 ASSERT_EQ(6, arm.collection_array_num);
329 EXPECT_STREQ(bcoll_r0c0_child0->name, arm.collection_array[0]->name);
330 EXPECT_STREQ(bcoll_r0c0_child1->name, arm.collection_array[1]->name);
331 EXPECT_STREQ(bcoll_r0c0_child2->name, arm.collection_array[2]->name);
332 EXPECT_STREQ(bcoll_r0_child1->name, arm.collection_array[3]->name);
333 EXPECT_STREQ(bcoll_r0_child2->name, arm.collection_array[4]->name);
334 EXPECT_STREQ(bcoll_root_1->name, arm.collection_array[5]->name);
335
336 EXPECT_EQ(0, arm.collection_array[0]->child_index);
337 EXPECT_EQ(0, arm.collection_array[1]->child_index);
338 EXPECT_EQ(0, arm.collection_array[2]->child_index);
339 EXPECT_EQ(0, arm.collection_array[3]->child_index);
340 EXPECT_EQ(0, arm.collection_array[4]->child_index);
341 EXPECT_EQ(0, arm.collection_array[5]->child_index);
342
343 EXPECT_EQ(0, arm.collection_array[0]->child_count);
344 EXPECT_EQ(0, arm.collection_array[1]->child_count);
345 EXPECT_EQ(0, arm.collection_array[2]->child_count);
346 EXPECT_EQ(0, arm.collection_array[3]->child_count);
347 EXPECT_EQ(0, arm.collection_array[4]->child_count);
348 EXPECT_EQ(0, arm.collection_array[5]->child_count);
349}
350
351TEST_F(ArmatureBoneCollections, collection_hierarchy_removal__more_complex_remove_root)
352{
353 /* Set up a slightly bigger hierarchy. Contrary to the other tests these are
354 * actually declared in array order. */
355 BoneCollection *bcoll_root_0 = ANIM_armature_bonecoll_new(&arm, "root_0");
356 BoneCollection *bcoll_root_1 = ANIM_armature_bonecoll_new(&arm, "root_1");
357 BoneCollection *bcoll_r0_child0 = ANIM_armature_bonecoll_new(&arm, "r0_child0", 0);
358 BoneCollection *bcoll_r0_child1 = ANIM_armature_bonecoll_new(&arm, "r0_child1", 0);
359 BoneCollection *bcoll_r0_child2 = ANIM_armature_bonecoll_new(&arm, "r0_child2", 0);
360 BoneCollection *bcoll_r0c0_child0 = ANIM_armature_bonecoll_new(&arm, "r0c0_child0", 2);
361 BoneCollection *bcoll_r0c0_child1 = ANIM_armature_bonecoll_new(&arm, "r0c0_child1", 2);
362 BoneCollection *bcoll_r0c0_child2 = ANIM_armature_bonecoll_new(&arm, "r0c0_child2", 2);
363
364 ASSERT_EQ(2, arm.collection_root_count);
365 ASSERT_EQ(8, arm.collection_array_num);
366 ASSERT_STREQ(bcoll_root_0->name, arm.collection_array[0]->name);
367 ASSERT_STREQ(bcoll_root_1->name, arm.collection_array[1]->name);
368 ASSERT_STREQ(bcoll_r0_child0->name, arm.collection_array[2]->name); /* Children of root_0. */
369 ASSERT_STREQ(bcoll_r0_child1->name, arm.collection_array[3]->name);
370 ASSERT_STREQ(bcoll_r0_child2->name, arm.collection_array[4]->name);
371 ASSERT_STREQ(bcoll_r0c0_child0->name, arm.collection_array[5]->name); /* Kids of r0_child0. */
372 ASSERT_STREQ(bcoll_r0c0_child1->name, arm.collection_array[6]->name);
373 ASSERT_STREQ(bcoll_r0c0_child2->name, arm.collection_array[7]->name);
374
375 ASSERT_EQ(2, arm.collection_array[0]->child_index);
376 ASSERT_EQ(0, arm.collection_array[1]->child_index);
377 ASSERT_EQ(5, arm.collection_array[2]->child_index);
378 ASSERT_EQ(0, arm.collection_array[3]->child_index);
379 ASSERT_EQ(0, arm.collection_array[4]->child_index);
380 ASSERT_EQ(0, arm.collection_array[5]->child_index);
381 ASSERT_EQ(0, arm.collection_array[6]->child_index);
382 ASSERT_EQ(0, arm.collection_array[7]->child_index);
383
384 ASSERT_EQ(3, arm.collection_array[0]->child_count);
385 ASSERT_EQ(0, arm.collection_array[1]->child_count);
386 ASSERT_EQ(3, arm.collection_array[2]->child_count);
387 ASSERT_EQ(0, arm.collection_array[3]->child_count);
388 ASSERT_EQ(0, arm.collection_array[4]->child_count);
389 ASSERT_EQ(0, arm.collection_array[5]->child_count);
390 ASSERT_EQ(0, arm.collection_array[6]->child_count);
391 ASSERT_EQ(0, arm.collection_array[7]->child_count);
392
393 /* Remove root_0, which should make all of its children new roots. */
394 ANIM_armature_bonecoll_remove(&arm, bcoll_root_0);
395
396 ASSERT_EQ(4, arm.collection_root_count);
397 ASSERT_EQ(7, arm.collection_array_num);
398 ASSERT_STREQ(bcoll_r0_child0->name, arm.collection_array[0]->name);
399 ASSERT_STREQ(bcoll_r0_child1->name, arm.collection_array[1]->name);
400 ASSERT_STREQ(bcoll_r0_child2->name, arm.collection_array[2]->name);
401 ASSERT_STREQ(bcoll_root_1->name, arm.collection_array[3]->name);
402 ASSERT_STREQ(bcoll_r0c0_child0->name, arm.collection_array[4]->name); /* Kids of r0_child0. */
403 ASSERT_STREQ(bcoll_r0c0_child1->name, arm.collection_array[5]->name);
404 ASSERT_STREQ(bcoll_r0c0_child2->name, arm.collection_array[6]->name);
405
406 EXPECT_EQ(4, arm.collection_array[0]->child_index);
407 EXPECT_EQ(0, arm.collection_array[1]->child_index);
408 EXPECT_EQ(0, arm.collection_array[2]->child_index);
409 EXPECT_EQ(0, arm.collection_array[3]->child_index);
410 EXPECT_EQ(0, arm.collection_array[4]->child_index);
411 EXPECT_EQ(0, arm.collection_array[5]->child_index);
412 EXPECT_EQ(0, arm.collection_array[6]->child_count);
413
414 EXPECT_EQ(3, arm.collection_array[0]->child_count);
415 EXPECT_EQ(0, arm.collection_array[1]->child_count);
416 EXPECT_EQ(0, arm.collection_array[2]->child_count);
417 EXPECT_EQ(0, arm.collection_array[3]->child_count);
418 EXPECT_EQ(0, arm.collection_array[4]->child_count);
419 EXPECT_EQ(0, arm.collection_array[5]->child_count);
420 EXPECT_EQ(0, arm.collection_array[6]->child_count);
421}
422
424{
425 /* Set up a small hierarchy. */
426 BoneCollection *bcoll_root_0 = ANIM_armature_bonecoll_new(&arm, "root_0");
427 BoneCollection *bcoll_root_1 = ANIM_armature_bonecoll_new(&arm, "root_1");
428 BoneCollection *bcoll_r0_child0 = ANIM_armature_bonecoll_new(&arm, "r0_child0", 0);
429 BoneCollection *bcoll_r1_child0 = ANIM_armature_bonecoll_new(&arm, "r1_child0", 1);
430 BoneCollection *bcoll_r0_child1 = ANIM_armature_bonecoll_new(&arm, "r0_child1", 0);
431 BoneCollection *bcoll_r0c0_child0 = ANIM_armature_bonecoll_new(&arm, "r0c0_child0", 2);
432
433 ASSERT_EQ(2, arm.collection_root_count);
434 ASSERT_EQ(6, arm.collection_array_num);
435 ASSERT_STREQ(bcoll_root_0->name, arm.collection_array[0]->name);
436 ASSERT_STREQ(bcoll_root_1->name, arm.collection_array[1]->name);
437 ASSERT_STREQ(bcoll_r0_child0->name, arm.collection_array[2]->name);
438 ASSERT_STREQ(bcoll_r0_child1->name, arm.collection_array[3]->name);
439 ASSERT_STREQ(bcoll_r1_child0->name, arm.collection_array[4]->name);
440 ASSERT_STREQ(bcoll_r0c0_child0->name, arm.collection_array[5]->name);
441
442 ASSERT_EQ(2, arm.collection_array[0]->child_index);
443 ASSERT_EQ(4, arm.collection_array[1]->child_index);
444 ASSERT_EQ(5, arm.collection_array[2]->child_index);
445 ASSERT_EQ(0, arm.collection_array[3]->child_index);
446 ASSERT_EQ(0, arm.collection_array[4]->child_index);
447 ASSERT_EQ(0, arm.collection_array[5]->child_index);
448
449 ASSERT_EQ(2, arm.collection_array[0]->child_count);
450 ASSERT_EQ(1, arm.collection_array[1]->child_count);
451 ASSERT_EQ(1, arm.collection_array[2]->child_count);
452 ASSERT_EQ(0, arm.collection_array[3]->child_count);
453 ASSERT_EQ(0, arm.collection_array[4]->child_count);
454 ASSERT_EQ(0, arm.collection_array[5]->child_count);
455
458
465}
466
467TEST_F(ArmatureBoneCollections, collection_hierarchy_visibility)
468{
469 /* Set up a small hierarchy. */
470 BoneCollection *bcoll_root0 = ANIM_armature_bonecoll_new(&arm, "root0");
471 BoneCollection *bcoll_root1 = ANIM_armature_bonecoll_new(&arm, "root1");
472 const int root0_index = armature_bonecoll_find_index(&arm, bcoll_root0);
473 BoneCollection *bcoll_r0_child0 = ANIM_armature_bonecoll_new(&arm, "r0_child0", root0_index);
474 BoneCollection *bcoll_r0_child1 = ANIM_armature_bonecoll_new(&arm, "r0_child1", root0_index);
475 const int child0_index = armature_bonecoll_find_index(&arm, bcoll_r0_child0);
476 BoneCollection *bcoll_c0_child0 = ANIM_armature_bonecoll_new(&arm, "c0_child0", child0_index);
477
478 /* Initially, all bone collections should be marked as visible. */
481 EXPECT_TRUE(bcoll_r0_child0->flags & BONE_COLLECTION_VISIBLE);
482 EXPECT_TRUE(bcoll_r0_child1->flags & BONE_COLLECTION_VISIBLE);
483 EXPECT_TRUE(bcoll_c0_child0->flags & BONE_COLLECTION_VISIBLE);
484
485 /* Initially, all bone collections should have visible ancestors. */
491
492 /* Mark root_0 as invisible, this should also update its children. */
493 ANIM_bonecoll_hide(&arm, bcoll_root0);
494
497 EXPECT_TRUE(bcoll_r0_child0->flags & BONE_COLLECTION_VISIBLE);
498 EXPECT_TRUE(bcoll_r0_child1->flags & BONE_COLLECTION_VISIBLE);
499 EXPECT_TRUE(bcoll_c0_child0->flags & BONE_COLLECTION_VISIBLE);
500
506
507 /* Move r0_child0 to root1, that should change its BONE_COLLECTION_ANCESTORS_VISIBLE */
508 const int root1_index = armature_bonecoll_find_index(&arm, bcoll_root1);
509 armature_bonecoll_move_to_parent(&arm, child0_index, 0, root0_index, root1_index);
510
513 EXPECT_TRUE(bcoll_r0_child0->flags & BONE_COLLECTION_VISIBLE);
514 EXPECT_TRUE(bcoll_r0_child1->flags & BONE_COLLECTION_VISIBLE);
515 EXPECT_TRUE(bcoll_c0_child0->flags & BONE_COLLECTION_VISIBLE);
516
520 << "The child that was moved to a visible root should be affected";
522 << "The child that wasn't moved should not be affected.";
524 << "The grandchild that was indirectly moved to a visible root should be affected";
525
526 /* Add a new child to root0, it should have the right flags. */
527 BoneCollection *bcoll_r0_child2 = ANIM_armature_bonecoll_new(&arm, "r0_child2", root0_index);
528 EXPECT_TRUE(bcoll_r0_child2->flags & BONE_COLLECTION_VISIBLE);
530}
531
532TEST_F(ArmatureBoneCollections, bones_assign_unassign)
533{
534 BoneCollection *bcoll = ANIM_armature_bonecoll_new(&arm, "collection");
535
536 ANIM_armature_bonecoll_assign(bcoll, &bone1);
537 ANIM_armature_bonecoll_assign(bcoll, &bone2);
538
539 ASSERT_EQ(2, bcoll->bones.count()) << "expecting two bones in collection";
540 EXPECT_EQ(&bone1, static_cast<BoneCollectionMember *>(BLI_findlink(&bcoll->bones, 0))->bone);
541 EXPECT_EQ(&bone2, static_cast<BoneCollectionMember *>(BLI_findlink(&bcoll->bones, 1))->bone);
542
543 EXPECT_EQ(bcoll, bone1.runtime.collections.first()->bcoll)
544 << "expecting back-reference to collection in bone1 runtime data";
545 EXPECT_EQ(bcoll, bone2.runtime.collections.first()->bcoll)
546 << "expecting back-reference to collection in bone2 runtime data";
547
548 ANIM_armature_bonecoll_unassign(bcoll, &bone1);
549 ANIM_armature_bonecoll_unassign(bcoll, &bone2);
550
551 EXPECT_EQ(0, bone1.runtime.collections.count())
552 << "expecting back-references in bone1 runtime data to be cleared when unassigned";
553 EXPECT_EQ(0, bone2.runtime.collections.count())
554 << "expecting back-references in bone2 runtime data to be cleared when unassigned";
555
557}
558
559TEST_F(ArmatureBoneCollections, bones_assign_remove)
560{
561 BoneCollection *bcoll = ANIM_armature_bonecoll_new(&arm, "collection");
562
563 ANIM_armature_bonecoll_assign(bcoll, &bone1);
564 ANIM_armature_bonecoll_assign(bcoll, &bone2);
566
567 EXPECT_EQ(0, bone1.runtime.collections.count())
568 << "expecting back-references in bone1 runtime data to be cleared when the collection is "
569 "removed";
570 EXPECT_EQ(0, bone2.runtime.collections.count())
571 << "expecting back-references in bone2 runtime data to be cleared when the collection is "
572 "removed";
573}
574
575TEST_F(ArmatureBoneCollections, active_set_clear_by_pointer)
576{
577 BoneCollection *bcoll1 = ANIM_armature_bonecoll_new(&arm, "Bones 1");
578 BoneCollection *bcoll2 = ANIM_armature_bonecoll_new(&arm, "Bones 2");
579 BoneCollection *bcoll3 = ANIM_bonecoll_new("Alien Bones");
580
582 EXPECT_EQ(0, arm.runtime->active_collection_index);
583 EXPECT_EQ(bcoll1, arm.runtime->active_collection);
584 EXPECT_STREQ(bcoll1->name, arm.active_collection_name);
585
587 EXPECT_EQ(-1, arm.runtime->active_collection_index);
588 EXPECT_EQ(nullptr, arm.runtime->active_collection);
589 EXPECT_STREQ("", arm.active_collection_name);
590
592 EXPECT_EQ(1, arm.runtime->active_collection_index);
593 EXPECT_EQ(bcoll2, arm.runtime->active_collection);
594 EXPECT_STREQ(bcoll2->name, arm.active_collection_name);
595
597 EXPECT_EQ(-1, arm.runtime->active_collection_index);
598 EXPECT_EQ(nullptr, arm.runtime->active_collection);
599 EXPECT_STREQ("", arm.active_collection_name);
600
601 ANIM_bonecoll_free(bcoll3);
602}
603
604TEST_F(ArmatureBoneCollections, active_set_clear_by_index)
605{
606 BoneCollection *bcoll1 = ANIM_armature_bonecoll_new(&arm, "Bones 1");
607 BoneCollection *bcoll2 = ANIM_armature_bonecoll_new(&arm, "Bones 2");
608
610 EXPECT_EQ(0, arm.runtime->active_collection_index);
611 EXPECT_EQ(bcoll1, arm.runtime->active_collection);
612 EXPECT_STREQ(bcoll1->name, arm.active_collection_name);
613
615 EXPECT_EQ(-1, arm.runtime->active_collection_index);
616 EXPECT_EQ(nullptr, arm.runtime->active_collection);
617 EXPECT_STREQ("", arm.active_collection_name);
618
620 EXPECT_EQ(1, arm.runtime->active_collection_index);
621 EXPECT_EQ(bcoll2, arm.runtime->active_collection);
622 EXPECT_STREQ(bcoll2->name, arm.active_collection_name);
623
625 EXPECT_EQ(-1, arm.runtime->active_collection_index);
626 EXPECT_EQ(nullptr, arm.runtime->active_collection);
627 EXPECT_STREQ("", arm.active_collection_name);
628}
629
631{
632 BoneCollection *bcoll1 = ANIM_armature_bonecoll_new(&arm, "Bones 1");
633 BoneCollection *bcoll2 = ANIM_armature_bonecoll_new(&arm, "Bones 2");
634
637
639 << "Expecting local armature to be editable";
640
641 /* Fake that the armature is linked from another blend file. */
642 Library fake_lib = {};
643 arm.id.lib = &fake_lib;
645 << "Expecting local armature to not be editable";
646
647 /* Fake that the armature is an override, but linked from another blend file. */
648 IDOverrideLibrary fake_override = {};
649 bArmature fake_reference = {};
650 fake_override.reference = &fake_reference.id;
651 arm.id.override_library = &fake_override;
653 << "Expecting linked armature override to not be editable";
654
655 /* Fake that the armature is a local override. */
656 arm.id.lib = nullptr;
659 << "Expecting linked bone collection on local armature override to not be editable";
661 << "Expecting local bone collection on local armature override to be editable";
662}
663
664TEST_F(ArmatureBoneCollections, bcoll_move_to_index__roots)
665{
666 BoneCollection *bcoll1 = ANIM_armature_bonecoll_new(&arm, "collection");
667 BoneCollection *bcoll2 = ANIM_armature_bonecoll_new(&arm, "collection");
668 BoneCollection *bcoll3 = ANIM_armature_bonecoll_new(&arm, "collection");
669 BoneCollection *bcoll4 = ANIM_armature_bonecoll_new(&arm, "collection");
670
671 EXPECT_EQ(arm.collection_array[0], bcoll1);
672 EXPECT_EQ(arm.collection_array[1], bcoll2);
673 EXPECT_EQ(arm.collection_array[2], bcoll3);
674 EXPECT_EQ(arm.collection_array[3], bcoll4);
675
677
678 EXPECT_EQ(arm.collection_array[0], bcoll1);
679 EXPECT_EQ(arm.collection_array[1], bcoll3);
680 EXPECT_EQ(arm.collection_array[2], bcoll2);
681 EXPECT_EQ(arm.collection_array[3], bcoll4);
682
684
685 EXPECT_EQ(arm.collection_array[0], bcoll3);
686 EXPECT_EQ(arm.collection_array[1], bcoll2);
687 EXPECT_EQ(arm.collection_array[2], bcoll4);
688 EXPECT_EQ(arm.collection_array[3], bcoll1);
689
690 /* Out of bounds should not be accepted. */
692
693 EXPECT_EQ(arm.collection_array[0], bcoll3);
694 EXPECT_EQ(arm.collection_array[1], bcoll2);
695 EXPECT_EQ(arm.collection_array[2], bcoll4);
696 EXPECT_EQ(arm.collection_array[3], bcoll1);
697}
698
699TEST_F(ArmatureBoneCollections, bcoll_move_to_index__siblings)
700{
701 BoneCollection *root = ANIM_armature_bonecoll_new(&arm, "root");
702 BoneCollection *child0 = ANIM_armature_bonecoll_new(&arm, "child0", 0);
703 BoneCollection *child1 = ANIM_armature_bonecoll_new(&arm, "child1", 0);
704 BoneCollection *child2 = ANIM_armature_bonecoll_new(&arm, "child2", 0);
705 BoneCollection *child1_0 = ANIM_armature_bonecoll_new(&arm, "child1_0", 2);
706
707 ASSERT_STREQ(root->name, arm.collection_array[0]->name);
708 ASSERT_STREQ(child0->name, arm.collection_array[1]->name);
709 ASSERT_STREQ(child1->name, arm.collection_array[2]->name);
710 ASSERT_STREQ(child2->name, arm.collection_array[3]->name);
711 ASSERT_STREQ(child1_0->name, arm.collection_array[4]->name);
712
713 /* Move child2 to child0, i.e. a move 'to the left'. */
715
716 EXPECT_STREQ(root->name, arm.collection_array[0]->name);
717 EXPECT_STREQ(child2->name, arm.collection_array[1]->name);
718 EXPECT_STREQ(child0->name, arm.collection_array[2]->name);
719 EXPECT_STREQ(child1->name, arm.collection_array[3]->name);
720 EXPECT_STREQ(child1_0->name, arm.collection_array[4]->name);
721
722 /* Move child2 to child1, i.e. a move 'to the right'. */
724
725 EXPECT_STREQ(root->name, arm.collection_array[0]->name);
726 EXPECT_STREQ(child0->name, arm.collection_array[1]->name);
727 EXPECT_STREQ(child1->name, arm.collection_array[2]->name);
728 EXPECT_STREQ(child2->name, arm.collection_array[3]->name);
729 EXPECT_STREQ(child1_0->name, arm.collection_array[4]->name);
730
731 /* Move child2 to root, should not be allowed. */
733
734 EXPECT_STREQ(root->name, arm.collection_array[0]->name);
735 EXPECT_STREQ(child0->name, arm.collection_array[1]->name);
736 EXPECT_STREQ(child1->name, arm.collection_array[2]->name);
737 EXPECT_STREQ(child2->name, arm.collection_array[3]->name);
738 EXPECT_STREQ(child1_0->name, arm.collection_array[4]->name);
739
740 /* Move child1_0 to child_2, should not be allowed. */
742
743 EXPECT_STREQ(root->name, arm.collection_array[0]->name);
744 EXPECT_STREQ(child0->name, arm.collection_array[1]->name);
745 EXPECT_STREQ(child1->name, arm.collection_array[2]->name);
746 EXPECT_STREQ(child2->name, arm.collection_array[3]->name);
747 EXPECT_STREQ(child1_0->name, arm.collection_array[4]->name);
748}
749
750TEST_F(ArmatureBoneCollections, bcoll_move_to_parent)
751{
752 /* Set up a small hierarchy. */
753 BoneCollection *bcoll_root_0 = ANIM_armature_bonecoll_new(&arm, "root_0");
754 BoneCollection *bcoll_root_1 = ANIM_armature_bonecoll_new(&arm, "root_1");
755 BoneCollection *bcoll_r0_child0 = ANIM_armature_bonecoll_new(&arm, "r0_child0", 0);
756 BoneCollection *bcoll_r1_child0 = ANIM_armature_bonecoll_new(&arm, "r1_child0", 1);
757 BoneCollection *bcoll_r0_child1 = ANIM_armature_bonecoll_new(&arm, "r0_child1", 0);
758 BoneCollection *bcoll_r0_child2 = ANIM_armature_bonecoll_new(&arm, "r0_child2", 0);
759
760 ASSERT_EQ(2, arm.collection_root_count);
761 ASSERT_EQ(6, arm.collection_array_num);
762 ASSERT_STREQ(bcoll_root_0->name, arm.collection_array[0]->name);
763 ASSERT_STREQ(bcoll_root_1->name, arm.collection_array[1]->name);
764 ASSERT_STREQ(bcoll_r0_child0->name, arm.collection_array[2]->name);
765 ASSERT_STREQ(bcoll_r0_child1->name, arm.collection_array[3]->name);
766 ASSERT_STREQ(bcoll_r0_child2->name, arm.collection_array[4]->name);
767 ASSERT_STREQ(bcoll_r1_child0->name, arm.collection_array[5]->name);
768
769 ASSERT_EQ(2, arm.collection_array[0]->child_index);
770 ASSERT_EQ(5, arm.collection_array[1]->child_index);
771 ASSERT_EQ(0, arm.collection_array[2]->child_index);
772 ASSERT_EQ(0, arm.collection_array[3]->child_index);
773 ASSERT_EQ(0, arm.collection_array[4]->child_index);
774 ASSERT_EQ(0, arm.collection_array[5]->child_index);
775
776 ASSERT_EQ(3, arm.collection_array[0]->child_count);
777 ASSERT_EQ(1, arm.collection_array[1]->child_count);
778 ASSERT_EQ(0, arm.collection_array[2]->child_count);
779 ASSERT_EQ(0, arm.collection_array[3]->child_count);
780 ASSERT_EQ(0, arm.collection_array[4]->child_count);
781 ASSERT_EQ(0, arm.collection_array[5]->child_count);
782
783 /* Move the middle child of root_0 to root_1. */
784 EXPECT_EQ(5, armature_bonecoll_move_to_parent(&arm, 3, bcoll_root_1->child_count, 0, 1));
785
786 ASSERT_EQ(2, arm.collection_root_count);
787 ASSERT_EQ(6, arm.collection_array_num);
788 EXPECT_STREQ(bcoll_root_0->name, arm.collection_array[0]->name);
789 EXPECT_STREQ(bcoll_root_1->name, arm.collection_array[1]->name);
790 EXPECT_STREQ(bcoll_r0_child0->name, arm.collection_array[2]->name);
791 EXPECT_STREQ(bcoll_r0_child2->name, arm.collection_array[3]->name);
792 EXPECT_STREQ(bcoll_r1_child0->name, arm.collection_array[4]->name);
793 EXPECT_STREQ(bcoll_r0_child1->name, arm.collection_array[5]->name);
794
795 EXPECT_EQ(2, arm.collection_array[0]->child_index);
796 EXPECT_EQ(4, arm.collection_array[1]->child_index);
797 EXPECT_EQ(0, arm.collection_array[2]->child_index);
798 EXPECT_EQ(0, arm.collection_array[3]->child_index);
799 EXPECT_EQ(0, arm.collection_array[4]->child_index);
800 EXPECT_EQ(0, arm.collection_array[5]->child_index);
801
802 EXPECT_EQ(2, arm.collection_array[0]->child_count);
803 EXPECT_EQ(2, arm.collection_array[1]->child_count);
804 EXPECT_EQ(0, arm.collection_array[2]->child_count);
805 EXPECT_EQ(0, arm.collection_array[3]->child_count);
806 EXPECT_EQ(0, arm.collection_array[4]->child_count);
807 EXPECT_EQ(0, arm.collection_array[5]->child_count);
808
809 /* Move the first child of root_1 to root_0. This shouldn't change its index. */
810 EXPECT_EQ(4, armature_bonecoll_move_to_parent(&arm, 4, bcoll_root_0->child_count, 1, 0));
811
812 ASSERT_EQ(2, arm.collection_root_count);
813 ASSERT_EQ(6, arm.collection_array_num);
814 EXPECT_STREQ(bcoll_root_0->name, arm.collection_array[0]->name);
815 EXPECT_STREQ(bcoll_root_1->name, arm.collection_array[1]->name);
816 EXPECT_STREQ(bcoll_r0_child0->name, arm.collection_array[2]->name);
817 EXPECT_STREQ(bcoll_r0_child2->name, arm.collection_array[3]->name);
818 EXPECT_STREQ(bcoll_r1_child0->name, arm.collection_array[4]->name);
819 EXPECT_STREQ(bcoll_r0_child1->name, arm.collection_array[5]->name);
820
821 EXPECT_EQ(2, arm.collection_array[0]->child_index);
822 EXPECT_EQ(5, arm.collection_array[1]->child_index);
823 EXPECT_EQ(0, arm.collection_array[2]->child_index);
824 EXPECT_EQ(0, arm.collection_array[3]->child_index);
825 EXPECT_EQ(0, arm.collection_array[4]->child_index);
826 EXPECT_EQ(0, arm.collection_array[5]->child_index);
827
828 EXPECT_EQ(3, arm.collection_array[0]->child_count);
829 EXPECT_EQ(1, arm.collection_array[1]->child_count);
830 EXPECT_EQ(0, arm.collection_array[2]->child_count);
831 EXPECT_EQ(0, arm.collection_array[3]->child_count);
832 EXPECT_EQ(0, arm.collection_array[4]->child_count);
833 EXPECT_EQ(0, arm.collection_array[5]->child_count);
834
835 /* Move the final child of root_1 to root_0. This shouldn't change its index
836 * again, but leave root_1 without children. */
837 EXPECT_EQ(5, armature_bonecoll_move_to_parent(&arm, 5, bcoll_root_0->child_count, 1, 0));
838
839 ASSERT_EQ(2, arm.collection_root_count);
840 ASSERT_EQ(6, arm.collection_array_num);
841 EXPECT_STREQ(bcoll_root_0->name, arm.collection_array[0]->name);
842 EXPECT_STREQ(bcoll_root_1->name, arm.collection_array[1]->name);
843 EXPECT_STREQ(bcoll_r0_child0->name, arm.collection_array[2]->name);
844 EXPECT_STREQ(bcoll_r0_child2->name, arm.collection_array[3]->name);
845 EXPECT_STREQ(bcoll_r1_child0->name, arm.collection_array[4]->name);
846 EXPECT_STREQ(bcoll_r0_child1->name, arm.collection_array[5]->name);
847
848 EXPECT_EQ(2, arm.collection_array[0]->child_index);
849 EXPECT_EQ(0, arm.collection_array[1]->child_index);
850 EXPECT_EQ(0, arm.collection_array[2]->child_index);
851 EXPECT_EQ(0, arm.collection_array[3]->child_index);
852 EXPECT_EQ(0, arm.collection_array[4]->child_index);
853 EXPECT_EQ(0, arm.collection_array[5]->child_index);
854
855 EXPECT_EQ(4, arm.collection_array[0]->child_count);
856 EXPECT_EQ(0, arm.collection_array[1]->child_count);
857 EXPECT_EQ(0, arm.collection_array[2]->child_count);
858 EXPECT_EQ(0, arm.collection_array[3]->child_count);
859 EXPECT_EQ(0, arm.collection_array[4]->child_count);
860 EXPECT_EQ(0, arm.collection_array[5]->child_count);
861
862 /* Move the first child of root_0 (bcoll_r0_child0) to bcoll_r0_child2. */
863 EXPECT_EQ(5, armature_bonecoll_move_to_parent(&arm, 2, bcoll_r0_child2->child_count, 0, 3));
864
865 ASSERT_EQ(2, arm.collection_root_count);
866 ASSERT_EQ(6, arm.collection_array_num);
867 EXPECT_STREQ(bcoll_root_0->name, arm.collection_array[0]->name);
868 EXPECT_STREQ(bcoll_root_1->name, arm.collection_array[1]->name);
869 EXPECT_STREQ(bcoll_r0_child2->name, arm.collection_array[2]->name);
870 EXPECT_STREQ(bcoll_r1_child0->name, arm.collection_array[3]->name);
871 EXPECT_STREQ(bcoll_r0_child1->name, arm.collection_array[4]->name);
872 EXPECT_STREQ(bcoll_r0_child0->name, arm.collection_array[5]->name);
873
874 EXPECT_EQ(2, arm.collection_array[0]->child_index);
875 EXPECT_EQ(0, arm.collection_array[1]->child_index);
876 EXPECT_EQ(5, arm.collection_array[2]->child_index);
877 EXPECT_EQ(0, arm.collection_array[3]->child_index);
878 EXPECT_EQ(0, arm.collection_array[4]->child_index);
879 EXPECT_EQ(0, arm.collection_array[5]->child_index);
880
881 EXPECT_EQ(3, arm.collection_array[0]->child_count);
882 EXPECT_EQ(0, arm.collection_array[1]->child_count);
883 EXPECT_EQ(1, arm.collection_array[2]->child_count);
884 EXPECT_EQ(0, arm.collection_array[3]->child_count);
885 EXPECT_EQ(0, arm.collection_array[4]->child_count);
886 EXPECT_EQ(0, arm.collection_array[5]->child_count);
887}
888
889TEST_F(ArmatureBoneCollections, bcoll_move_to_parent__root_unroot)
890{
891 /* Set up a small hierarchy. */
892 BoneCollection *bcoll_root_0 = ANIM_armature_bonecoll_new(&arm, "root_0");
893 BoneCollection *bcoll_root_1 = ANIM_armature_bonecoll_new(&arm, "root_1");
894 BoneCollection *bcoll_r0_child0 = ANIM_armature_bonecoll_new(&arm, "r0_child0", 0);
895 BoneCollection *bcoll_r1_child0 = ANIM_armature_bonecoll_new(&arm, "r1_child0", 1);
896 BoneCollection *bcoll_r0_child1 = ANIM_armature_bonecoll_new(&arm, "r0_child1", 0);
897 BoneCollection *bcoll_r0_child2 = ANIM_armature_bonecoll_new(&arm, "r0_child2", 0);
898
899 ASSERT_EQ(2, arm.collection_root_count);
900 ASSERT_EQ(6, arm.collection_array_num);
901 ASSERT_STREQ(bcoll_root_0->name, arm.collection_array[0]->name);
902 ASSERT_STREQ(bcoll_root_1->name, arm.collection_array[1]->name);
903 ASSERT_STREQ(bcoll_r0_child0->name, arm.collection_array[2]->name);
904 ASSERT_STREQ(bcoll_r0_child1->name, arm.collection_array[3]->name);
905 ASSERT_STREQ(bcoll_r0_child2->name, arm.collection_array[4]->name);
906 ASSERT_STREQ(bcoll_r1_child0->name, arm.collection_array[5]->name);
907
908 ASSERT_EQ(2, arm.collection_array[0]->child_index);
909 ASSERT_EQ(5, arm.collection_array[1]->child_index);
910 ASSERT_EQ(0, arm.collection_array[2]->child_index);
911 ASSERT_EQ(0, arm.collection_array[3]->child_index);
912 ASSERT_EQ(0, arm.collection_array[4]->child_index);
913 ASSERT_EQ(0, arm.collection_array[5]->child_index);
914
915 ASSERT_EQ(3, arm.collection_array[0]->child_count);
916 ASSERT_EQ(1, arm.collection_array[1]->child_count);
917 ASSERT_EQ(0, arm.collection_array[2]->child_count);
918 ASSERT_EQ(0, arm.collection_array[3]->child_count);
919 ASSERT_EQ(0, arm.collection_array[4]->child_count);
920 ASSERT_EQ(0, arm.collection_array[5]->child_count);
921
922 /* Make a leaf node (bcoll_r0_child1) a root. */
923 EXPECT_EQ(2, armature_bonecoll_move_to_parent(&arm, 3, arm.collection_root_count, 0, -1));
924
925 ASSERT_EQ(3, arm.collection_root_count);
926 ASSERT_EQ(6, arm.collection_array_num);
927 EXPECT_STREQ(bcoll_root_0->name, arm.collection_array[0]->name);
928 EXPECT_STREQ(bcoll_root_1->name, arm.collection_array[1]->name);
929 EXPECT_STREQ(bcoll_r0_child1->name, arm.collection_array[2]->name); /* Became a root. */
930 EXPECT_STREQ(bcoll_r0_child0->name, arm.collection_array[3]->name);
931 EXPECT_STREQ(bcoll_r0_child2->name, arm.collection_array[4]->name);
932 EXPECT_STREQ(bcoll_r1_child0->name, arm.collection_array[5]->name);
933
934 EXPECT_EQ(3, arm.collection_array[0]->child_index);
935 EXPECT_EQ(5, arm.collection_array[1]->child_index);
936 EXPECT_EQ(0, arm.collection_array[2]->child_index);
937 EXPECT_EQ(0, arm.collection_array[3]->child_index);
938 EXPECT_EQ(0, arm.collection_array[4]->child_index);
939 EXPECT_EQ(0, arm.collection_array[5]->child_index);
940
941 EXPECT_EQ(2, arm.collection_array[0]->child_count);
942 EXPECT_EQ(1, arm.collection_array[1]->child_count);
943 EXPECT_EQ(0, arm.collection_array[2]->child_count);
944 EXPECT_EQ(0, arm.collection_array[3]->child_count);
945 EXPECT_EQ(0, arm.collection_array[4]->child_count);
946 EXPECT_EQ(0, arm.collection_array[5]->child_count);
947
948 /* Make a root node (root_1) a child of root_0. */
949 EXPECT_EQ(4, armature_bonecoll_move_to_parent(&arm, 1, bcoll_root_0->child_count, -1, 0));
950
951 ASSERT_EQ(2, arm.collection_root_count);
952 ASSERT_EQ(6, arm.collection_array_num);
953 EXPECT_STREQ(bcoll_root_0->name, arm.collection_array[0]->name);
954 EXPECT_STREQ(bcoll_r0_child1->name, arm.collection_array[1]->name); /* Actually a root. */
955 EXPECT_STREQ(bcoll_r0_child0->name, arm.collection_array[2]->name);
956 EXPECT_STREQ(bcoll_r0_child2->name, arm.collection_array[3]->name);
957 EXPECT_STREQ(bcoll_root_1->name, arm.collection_array[4]->name); /* Became a child. */
958 EXPECT_STREQ(bcoll_r1_child0->name, arm.collection_array[5]->name);
959
960 EXPECT_EQ(2, arm.collection_array[0]->child_index);
961 EXPECT_EQ(0, arm.collection_array[1]->child_index);
962 EXPECT_EQ(0, arm.collection_array[2]->child_index);
963 EXPECT_EQ(0, arm.collection_array[3]->child_index);
964 EXPECT_EQ(5, arm.collection_array[4]->child_index);
965 EXPECT_EQ(0, arm.collection_array[5]->child_index);
966
967 EXPECT_EQ(3, arm.collection_array[0]->child_count);
968 EXPECT_EQ(0, arm.collection_array[1]->child_count);
969 EXPECT_EQ(0, arm.collection_array[2]->child_count);
970 EXPECT_EQ(0, arm.collection_array[3]->child_count);
971 EXPECT_EQ(1, arm.collection_array[4]->child_count);
972 EXPECT_EQ(0, arm.collection_array[5]->child_count);
973
974 /* TODO: test with circular parenthood. */
975}
976
977TEST_F(ArmatureBoneCollections, bcoll_move_to_parent__within_siblings)
978{
979 /* Set up a small hierarchy. */
980 auto *bcoll_root_0 = ANIM_armature_bonecoll_new(&arm, "root_0");
981 auto *bcoll_root_1 = ANIM_armature_bonecoll_new(&arm, "root_1");
982 auto *bcoll_r1_child0 = ANIM_armature_bonecoll_new(&arm, "r1_child0", 1);
983 auto *bcoll_r0_child0 = ANIM_armature_bonecoll_new(&arm, "r0_child0", 0);
984 auto *bcoll_r0_child1 = ANIM_armature_bonecoll_new(&arm, "r0_child1", 0);
985 auto *bcoll_r0_child2 = ANIM_armature_bonecoll_new(&arm, "r0_child2", 0);
986 auto *bcoll_r0_child3 = ANIM_armature_bonecoll_new(&arm, "r0_child3", 0);
987
988 ASSERT_EQ(2, arm.collection_root_count);
989 ASSERT_EQ(7, arm.collection_array_num);
990 ASSERT_STREQ(bcoll_root_0->name, arm.collection_array[0]->name);
991 ASSERT_STREQ(bcoll_root_1->name, arm.collection_array[1]->name);
992 ASSERT_STREQ(bcoll_r1_child0->name, arm.collection_array[2]->name); /* Children root_1. */
993 ASSERT_STREQ(bcoll_r0_child0->name, arm.collection_array[3]->name); /* Children root_0. */
994 ASSERT_STREQ(bcoll_r0_child1->name, arm.collection_array[4]->name);
995 ASSERT_STREQ(bcoll_r0_child2->name, arm.collection_array[5]->name);
996 ASSERT_STREQ(bcoll_r0_child3->name, arm.collection_array[6]->name);
997
998 ASSERT_EQ(3, arm.collection_array[0]->child_index);
999 ASSERT_EQ(2, arm.collection_array[1]->child_index);
1000 ASSERT_EQ(0, arm.collection_array[2]->child_index);
1001 ASSERT_EQ(0, arm.collection_array[3]->child_index);
1002 ASSERT_EQ(0, arm.collection_array[4]->child_index);
1003 ASSERT_EQ(0, arm.collection_array[5]->child_index);
1004 ASSERT_EQ(0, arm.collection_array[6]->child_index);
1005
1006 ASSERT_EQ(4, arm.collection_array[0]->child_count);
1007 ASSERT_EQ(1, arm.collection_array[1]->child_count);
1008 ASSERT_EQ(0, arm.collection_array[2]->child_count);
1009 ASSERT_EQ(0, arm.collection_array[3]->child_count);
1010 ASSERT_EQ(0, arm.collection_array[4]->child_count);
1011 ASSERT_EQ(0, arm.collection_array[5]->child_count);
1012 ASSERT_EQ(0, arm.collection_array[6]->child_count);
1013
1014 /* First half of the test, move 3 children from root_1 to root_0. */
1015
1016 /* Move r0_child0 to become 1st child of root_1, before r1_child0. */
1017 EXPECT_EQ(2,
1019 3, /* From index. */
1020 0, /* To child number. */
1021 0, /* From parent. */
1022 1 /* To parent. */
1023 ));
1024
1025 ASSERT_EQ(2, arm.collection_root_count);
1026 ASSERT_EQ(7, arm.collection_array_num);
1027 EXPECT_STREQ(bcoll_root_0->name, arm.collection_array[0]->name);
1028 EXPECT_STREQ(bcoll_root_1->name, arm.collection_array[1]->name);
1029 EXPECT_STREQ(bcoll_r0_child0->name, arm.collection_array[2]->name); /* Children root_1. */
1030 EXPECT_STREQ(bcoll_r1_child0->name, arm.collection_array[3]->name);
1031 EXPECT_STREQ(bcoll_r0_child1->name, arm.collection_array[4]->name); /* Children root_0. */
1032 EXPECT_STREQ(bcoll_r0_child2->name, arm.collection_array[5]->name);
1033 EXPECT_STREQ(bcoll_r0_child3->name, arm.collection_array[6]->name);
1034
1035 EXPECT_EQ(4, arm.collection_array[0]->child_index);
1036 EXPECT_EQ(2, arm.collection_array[1]->child_index);
1037 EXPECT_EQ(0, arm.collection_array[2]->child_index);
1038 EXPECT_EQ(0, arm.collection_array[3]->child_index);
1039 EXPECT_EQ(0, arm.collection_array[4]->child_index);
1040 EXPECT_EQ(0, arm.collection_array[5]->child_index);
1041 EXPECT_EQ(0, arm.collection_array[6]->child_index);
1042
1043 EXPECT_EQ(3, arm.collection_array[0]->child_count);
1044 EXPECT_EQ(2, arm.collection_array[1]->child_count);
1045 EXPECT_EQ(0, arm.collection_array[2]->child_count);
1046 EXPECT_EQ(0, arm.collection_array[3]->child_count);
1047 EXPECT_EQ(0, arm.collection_array[4]->child_count);
1048 EXPECT_EQ(0, arm.collection_array[5]->child_count);
1049 EXPECT_EQ(0, arm.collection_array[6]->child_count);
1050
1051 /* Move r0_child1 to become the 2nd child of root_1. */
1052 EXPECT_EQ(3,
1054 4, /* From index. */
1055 1, /* To child number. */
1056 0, /* From parent. */
1057 1 /* To parent. */
1058 ));
1059
1060 ASSERT_EQ(2, arm.collection_root_count);
1061 ASSERT_EQ(7, arm.collection_array_num);
1062 EXPECT_STREQ(bcoll_root_0->name, arm.collection_array[0]->name);
1063 EXPECT_STREQ(bcoll_root_1->name, arm.collection_array[1]->name);
1064 EXPECT_STREQ(bcoll_r0_child0->name, arm.collection_array[2]->name); /* Children root_1. */
1065 EXPECT_STREQ(bcoll_r0_child1->name, arm.collection_array[3]->name);
1066 EXPECT_STREQ(bcoll_r1_child0->name, arm.collection_array[4]->name);
1067 EXPECT_STREQ(bcoll_r0_child2->name, arm.collection_array[5]->name); /* Children root_0. */
1068 EXPECT_STREQ(bcoll_r0_child3->name, arm.collection_array[6]->name);
1069
1070 EXPECT_EQ(5, arm.collection_array[0]->child_index);
1071 EXPECT_EQ(2, arm.collection_array[1]->child_index);
1072 EXPECT_EQ(0, arm.collection_array[2]->child_index);
1073 EXPECT_EQ(0, arm.collection_array[3]->child_index);
1074 EXPECT_EQ(0, arm.collection_array[4]->child_index);
1075 EXPECT_EQ(0, arm.collection_array[5]->child_index);
1076 EXPECT_EQ(0, arm.collection_array[6]->child_index);
1077
1078 EXPECT_EQ(2, arm.collection_array[0]->child_count);
1079 EXPECT_EQ(3, arm.collection_array[1]->child_count);
1080 EXPECT_EQ(0, arm.collection_array[2]->child_count);
1081 EXPECT_EQ(0, arm.collection_array[3]->child_count);
1082 EXPECT_EQ(0, arm.collection_array[4]->child_count);
1083 EXPECT_EQ(0, arm.collection_array[5]->child_count);
1084 EXPECT_EQ(0, arm.collection_array[6]->child_count);
1085
1086 /* Move r0_child3 to become the last child of root_1. */
1087 EXPECT_EQ(5,
1089 6, /* From index. */
1090 3, /* To child number. */
1091 0, /* From parent. */
1092 1 /* To parent. */
1093 ));
1094
1095 ASSERT_EQ(2, arm.collection_root_count);
1096 ASSERT_EQ(7, arm.collection_array_num);
1097 EXPECT_STREQ(bcoll_root_0->name, arm.collection_array[0]->name);
1098 EXPECT_STREQ(bcoll_root_1->name, arm.collection_array[1]->name);
1099 EXPECT_STREQ(bcoll_r0_child0->name, arm.collection_array[2]->name); /* Children root_1. */
1100 EXPECT_STREQ(bcoll_r0_child1->name, arm.collection_array[3]->name);
1101 EXPECT_STREQ(bcoll_r1_child0->name, arm.collection_array[4]->name);
1102 EXPECT_STREQ(bcoll_r0_child3->name, arm.collection_array[5]->name);
1103 EXPECT_STREQ(bcoll_r0_child2->name, arm.collection_array[6]->name); /* Children root_0. */
1104
1105 EXPECT_EQ(6, arm.collection_array[0]->child_index);
1106 EXPECT_EQ(2, arm.collection_array[1]->child_index);
1107 EXPECT_EQ(0, arm.collection_array[2]->child_index);
1108 EXPECT_EQ(0, arm.collection_array[3]->child_index);
1109 EXPECT_EQ(0, arm.collection_array[4]->child_index);
1110 EXPECT_EQ(0, arm.collection_array[5]->child_index);
1111 EXPECT_EQ(0, arm.collection_array[6]->child_index);
1112
1113 EXPECT_EQ(1, arm.collection_array[0]->child_count);
1114 EXPECT_EQ(4, arm.collection_array[1]->child_count);
1115 EXPECT_EQ(0, arm.collection_array[2]->child_count);
1116 EXPECT_EQ(0, arm.collection_array[3]->child_count);
1117 EXPECT_EQ(0, arm.collection_array[4]->child_count);
1118 EXPECT_EQ(0, arm.collection_array[5]->child_count);
1119 EXPECT_EQ(0, arm.collection_array[6]->child_count);
1120
1121 /* 2nd half of the test: move the children back to root_0 to test moving in
1122 * the other direction. */
1123
1124 /* Move r0_child3 to become the first child of root_0. */
1125 EXPECT_EQ(5,
1127 5, /* From index. */
1128 0, /* To child number. */
1129 1, /* From parent. */
1130 0 /* To parent. */
1131 ));
1132
1133 ASSERT_EQ(2, arm.collection_root_count);
1134 ASSERT_EQ(7, arm.collection_array_num);
1135 EXPECT_STREQ(bcoll_root_0->name, arm.collection_array[0]->name);
1136 EXPECT_STREQ(bcoll_root_1->name, arm.collection_array[1]->name);
1137 EXPECT_STREQ(bcoll_r0_child0->name, arm.collection_array[2]->name); /* Children root_1. */
1138 EXPECT_STREQ(bcoll_r0_child1->name, arm.collection_array[3]->name);
1139 EXPECT_STREQ(bcoll_r1_child0->name, arm.collection_array[4]->name);
1140 EXPECT_STREQ(bcoll_r0_child3->name, arm.collection_array[5]->name); /* Children root_0. */
1141 EXPECT_STREQ(bcoll_r0_child2->name, arm.collection_array[6]->name);
1142
1143 EXPECT_EQ(5, arm.collection_array[0]->child_index);
1144 EXPECT_EQ(2, arm.collection_array[1]->child_index);
1145 EXPECT_EQ(0, arm.collection_array[2]->child_index);
1146 EXPECT_EQ(0, arm.collection_array[3]->child_index);
1147 EXPECT_EQ(0, arm.collection_array[4]->child_index);
1148 EXPECT_EQ(0, arm.collection_array[5]->child_index);
1149 EXPECT_EQ(0, arm.collection_array[6]->child_index);
1150
1151 EXPECT_EQ(2, arm.collection_array[0]->child_count);
1152 EXPECT_EQ(3, arm.collection_array[1]->child_count);
1153 EXPECT_EQ(0, arm.collection_array[2]->child_count);
1154 EXPECT_EQ(0, arm.collection_array[3]->child_count);
1155 EXPECT_EQ(0, arm.collection_array[4]->child_count);
1156 EXPECT_EQ(0, arm.collection_array[5]->child_count);
1157 EXPECT_EQ(0, arm.collection_array[6]->child_count);
1158
1159 /* Move r0_child0 to become the last child of root_0. */
1160 EXPECT_EQ(6,
1162 2, /* From index. */
1163 2, /* To child number. */
1164 1, /* From parent. */
1165 0 /* To parent. */
1166 ));
1167
1168 ASSERT_EQ(2, arm.collection_root_count);
1169 ASSERT_EQ(7, arm.collection_array_num);
1170 EXPECT_STREQ(bcoll_root_0->name, arm.collection_array[0]->name);
1171 EXPECT_STREQ(bcoll_root_1->name, arm.collection_array[1]->name);
1172 EXPECT_STREQ(bcoll_r0_child1->name, arm.collection_array[2]->name); /* Children root_1. */
1173 EXPECT_STREQ(bcoll_r1_child0->name, arm.collection_array[3]->name);
1174 EXPECT_STREQ(bcoll_r0_child3->name, arm.collection_array[4]->name); /* Children root_0. */
1175 EXPECT_STREQ(bcoll_r0_child2->name, arm.collection_array[5]->name);
1176 EXPECT_STREQ(bcoll_r0_child0->name, arm.collection_array[6]->name);
1177
1178 EXPECT_EQ(4, arm.collection_array[0]->child_index);
1179 EXPECT_EQ(2, arm.collection_array[1]->child_index);
1180 EXPECT_EQ(0, arm.collection_array[2]->child_index);
1181 EXPECT_EQ(0, arm.collection_array[3]->child_index);
1182 EXPECT_EQ(0, arm.collection_array[4]->child_index);
1183 EXPECT_EQ(0, arm.collection_array[5]->child_index);
1184 EXPECT_EQ(0, arm.collection_array[6]->child_index);
1185
1186 EXPECT_EQ(3, arm.collection_array[0]->child_count);
1187 EXPECT_EQ(2, arm.collection_array[1]->child_count);
1188 EXPECT_EQ(0, arm.collection_array[2]->child_count);
1189 EXPECT_EQ(0, arm.collection_array[3]->child_count);
1190 EXPECT_EQ(0, arm.collection_array[4]->child_count);
1191 EXPECT_EQ(0, arm.collection_array[5]->child_count);
1192 EXPECT_EQ(0, arm.collection_array[6]->child_count);
1193
1194 /* Move r0_child1 to become the 3rd child of root_0. */
1195 EXPECT_EQ(5,
1197 2, /* From index. */
1198 2, /* To child number. */
1199 1, /* From parent. */
1200 0 /* To parent. */
1201 ));
1202
1203 ASSERT_EQ(2, arm.collection_root_count);
1204 ASSERT_EQ(7, arm.collection_array_num);
1205 EXPECT_STREQ(bcoll_root_0->name, arm.collection_array[0]->name);
1206 EXPECT_STREQ(bcoll_root_1->name, arm.collection_array[1]->name);
1207 EXPECT_STREQ(bcoll_r1_child0->name, arm.collection_array[2]->name); /* Children root_1. */
1208 EXPECT_STREQ(bcoll_r0_child3->name, arm.collection_array[3]->name); /* Children root_0. */
1209 EXPECT_STREQ(bcoll_r0_child2->name, arm.collection_array[4]->name);
1210 EXPECT_STREQ(bcoll_r0_child1->name, arm.collection_array[5]->name);
1211 EXPECT_STREQ(bcoll_r0_child0->name, arm.collection_array[6]->name);
1212
1213 EXPECT_EQ(3, arm.collection_array[0]->child_index);
1214 EXPECT_EQ(2, arm.collection_array[1]->child_index);
1215 EXPECT_EQ(0, arm.collection_array[2]->child_index);
1216 EXPECT_EQ(0, arm.collection_array[3]->child_index);
1217 EXPECT_EQ(0, arm.collection_array[4]->child_index);
1218 EXPECT_EQ(0, arm.collection_array[5]->child_index);
1219 EXPECT_EQ(0, arm.collection_array[6]->child_index);
1220
1221 EXPECT_EQ(4, arm.collection_array[0]->child_count);
1222 EXPECT_EQ(1, arm.collection_array[1]->child_count);
1223 EXPECT_EQ(0, arm.collection_array[2]->child_count);
1224 EXPECT_EQ(0, arm.collection_array[3]->child_count);
1225 EXPECT_EQ(0, arm.collection_array[4]->child_count);
1226 EXPECT_EQ(0, arm.collection_array[5]->child_count);
1227 EXPECT_EQ(0, arm.collection_array[6]->child_count);
1228}
1229
1230TEST_F(ArmatureBoneCollections, internal__bonecolls_rotate_block)
1231{
1232 /* Set up a small hierarchy. */
1233 BoneCollection *bcoll_root_0 = ANIM_armature_bonecoll_new(&arm, "root_0");
1234 BoneCollection *bcoll_root_1 = ANIM_armature_bonecoll_new(&arm, "root_1");
1235 BoneCollection *bcoll_r0_child0 = ANIM_armature_bonecoll_new(&arm, "r0_child0", 0);
1236 BoneCollection *bcoll_r1_child0 = ANIM_armature_bonecoll_new(&arm, "r1_child0", 1);
1237 BoneCollection *bcoll_r0_child1 = ANIM_armature_bonecoll_new(&arm, "r0_child1", 0);
1238 BoneCollection *bcoll_r0_child2 = ANIM_armature_bonecoll_new(&arm, "r0_child2", 0);
1239
1240 /* The tests below compare the collection names, instead of their pointers, so
1241 * that we get human-readable messages on failure. */
1242
1243 /* Unnecessary assertions, just to make it easier to understand in which order
1244 * the array starts out. */
1245 ASSERT_EQ(6, arm.collection_array_num);
1246 ASSERT_STREQ(bcoll_root_0->name, arm.collection_array[0]->name);
1247 ASSERT_STREQ(bcoll_root_1->name, arm.collection_array[1]->name);
1248 ASSERT_STREQ(bcoll_r0_child0->name, arm.collection_array[2]->name);
1249 ASSERT_STREQ(bcoll_r0_child1->name, arm.collection_array[3]->name);
1250 ASSERT_STREQ(bcoll_r0_child2->name, arm.collection_array[4]->name);
1251 ASSERT_STREQ(bcoll_r1_child0->name, arm.collection_array[5]->name);
1252
1253 ASSERT_EQ(2, arm.collection_array[0]->child_index);
1254 ASSERT_EQ(5, arm.collection_array[1]->child_index);
1255 ASSERT_EQ(0, arm.collection_array[2]->child_index);
1256 ASSERT_EQ(0, arm.collection_array[3]->child_index);
1257 ASSERT_EQ(0, arm.collection_array[4]->child_index);
1258 ASSERT_EQ(0, arm.collection_array[5]->child_index);
1259
1260 /* Move [0,1,2] to [1,2,3]. */
1261 internal::bonecolls_rotate_block(&arm, 0, 3, 1);
1262 ASSERT_EQ(6, arm.collection_array_num) << "array size should not change";
1263 EXPECT_STREQ(bcoll_r0_child1->name, arm.collection_array[0]->name);
1264 EXPECT_STREQ(bcoll_root_0->name, arm.collection_array[1]->name);
1265 EXPECT_STREQ(bcoll_root_1->name, arm.collection_array[2]->name);
1266 EXPECT_STREQ(bcoll_r0_child0->name, arm.collection_array[3]->name);
1267 EXPECT_STREQ(bcoll_r0_child2->name, arm.collection_array[4]->name);
1268 EXPECT_STREQ(bcoll_r1_child0->name, arm.collection_array[5]->name);
1269
1270 EXPECT_EQ(0, arm.collection_array[0]->child_index);
1271 EXPECT_EQ(3, arm.collection_array[1]->child_index);
1272 EXPECT_EQ(5, arm.collection_array[2]->child_index);
1273 EXPECT_EQ(0, arm.collection_array[3]->child_index);
1274 EXPECT_EQ(0, arm.collection_array[4]->child_index);
1275 EXPECT_EQ(0, arm.collection_array[5]->child_index);
1276
1277 /* Move [4,5] to [3,4]. */
1278 internal::bonecolls_rotate_block(&arm, 4, 2, -1);
1279 ASSERT_EQ(6, arm.collection_array_num) << "array size should not change";
1280 EXPECT_STREQ(bcoll_r0_child1->name, arm.collection_array[0]->name);
1281 EXPECT_STREQ(bcoll_root_0->name, arm.collection_array[1]->name);
1282 EXPECT_STREQ(bcoll_root_1->name, arm.collection_array[2]->name);
1283 EXPECT_STREQ(bcoll_r0_child2->name, arm.collection_array[3]->name);
1284 EXPECT_STREQ(bcoll_r1_child0->name, arm.collection_array[4]->name);
1285 EXPECT_STREQ(bcoll_r0_child0->name, arm.collection_array[5]->name);
1286
1287 EXPECT_EQ(0, arm.collection_array[0]->child_index);
1288 EXPECT_EQ(3, arm.collection_array[1]->child_index);
1289 EXPECT_EQ(4, arm.collection_array[2]->child_index);
1290 EXPECT_EQ(0, arm.collection_array[3]->child_index);
1291 EXPECT_EQ(0, arm.collection_array[4]->child_index);
1292 EXPECT_EQ(0, arm.collection_array[5]->child_index);
1293}
1294
1296 protected:
1298
1304
1305 void SetUp() override
1306 {
1307 STRNCPY(arm.id.name, "ARArmature");
1308 arm.runtime = MEM_new<bke::bArmature_Runtime>(__func__);
1309
1311 child0 = ANIM_armature_bonecoll_new(&arm, "child0", 0);
1312 child1 = ANIM_armature_bonecoll_new(&arm, "child1", 0);
1313 child2 = ANIM_armature_bonecoll_new(&arm, "child2", 0);
1314 child1_0 = ANIM_armature_bonecoll_new(&arm, "child1_0", 2);
1315
1316 ASSERT_STREQ(root->name, arm.collection_array[0]->name);
1317 ASSERT_STREQ(child0->name, arm.collection_array[1]->name);
1318 ASSERT_STREQ(child1->name, arm.collection_array[2]->name);
1319 ASSERT_STREQ(child2->name, arm.collection_array[3]->name);
1320 ASSERT_STREQ(child1_0->name, arm.collection_array[4]->name);
1321 }
1322
1323 void TearDown() override
1324 {
1326 }
1327
1328 testing::AssertionResult expect_bcolls(std::vector<std::string> expect_names)
1329 {
1330 std::vector<std::string> actual_names;
1331 for (const BoneCollection *bcoll : arm.collections_span()) {
1332 actual_names.emplace_back(bcoll->name);
1333 }
1334
1335 if (expect_names == actual_names) {
1336 return testing::AssertionSuccess();
1337 }
1338
1339 testing::AssertionResult failure = testing::AssertionFailure();
1340
1341 failure << "Expected bone collections differ from actual ones:" << std::endl;
1342
1343 /* This is what you get when C++ doesn't even have a standard library
1344 * function to do something like `expect_names.join(", ")`. */
1345 failure << "Expected collections: [";
1346 for (int i = 0; i < expect_names.size() - 1; i++) {
1347 failure << expect_names[i] << ", ";
1348 }
1349 failure << expect_names.back() << "]" << std::endl;
1350
1351 failure << "Actual collections : [";
1352 for (int i = 0; i < actual_names.size() - 1; i++) {
1353 failure << actual_names[i] << ", ";
1354 }
1355 failure << actual_names.back() << "]" << std::endl;
1356
1358
1359 return failure;
1360 }
1361};
1362
1363TEST_F(ArmatureBoneCollectionsTestList, move_before_after_index__before_first_sibling)
1364{
1365 /* Set the active index to be one of the affected bone collections. */
1367 ASSERT_EQ(3, arm.runtime->active_collection_index);
1368
1370 EXPECT_TRUE(expect_bcolls({"root", "child2", "child0", "child1", "child1_0"}));
1372
1373 /* The three indicators of the active collection should still be in sync. */
1374 EXPECT_EQ(1, arm.runtime->active_collection_index);
1375 EXPECT_EQ(child2, arm.runtime->active_collection);
1376 EXPECT_STREQ("child2", arm.active_collection_name);
1377}
1378
1379TEST_F(ArmatureBoneCollectionsTestList, move_before_after_index__after_first_sibling)
1380{
1382 EXPECT_TRUE(expect_bcolls({"root", "child0", "child2", "child1", "child1_0"}));
1384}
1385
1386TEST_F(ArmatureBoneCollectionsTestList, move_before_after_index__before_last_sibling)
1387{
1388 /* Set the active index to be one of the affected bone collections. */
1390 ASSERT_EQ(2, arm.runtime->active_collection_index);
1391
1393 EXPECT_TRUE(expect_bcolls({"root", "child1", "child0", "child2", "child1_0"}));
1395
1396 /* The three indicators of the active collection should still be in sync. */
1397 EXPECT_EQ(1, arm.runtime->active_collection_index);
1398 EXPECT_EQ(child1, arm.runtime->active_collection);
1399 EXPECT_STREQ("child1", arm.active_collection_name);
1400}
1401
1402TEST_F(ArmatureBoneCollectionsTestList, move_before_after_index__after_last_sibling)
1403{
1405 EXPECT_TRUE(expect_bcolls({"root", "child1", "child2", "child0", "child1_0"}));
1407}
1408
1409TEST_F(ArmatureBoneCollectionsTestList, move_before_after_index__other_parent_before__move_left)
1410{
1412 EXPECT_TRUE(expect_bcolls({"root", "child1_0", "child0", "child1", "child2"}));
1414}
1415
1416TEST_F(ArmatureBoneCollectionsTestList, move_before_after_index__other_parent_after__move_left)
1417{
1419 EXPECT_TRUE(expect_bcolls({"root", "child0", "child1_0", "child1", "child2"}));
1421}
1422
1423TEST_F(ArmatureBoneCollectionsTestList, move_before_after_index__other_parent_before__move_right)
1424{
1426 EXPECT_TRUE(expect_bcolls({"root", "child1", "child2", "child0", "child1_0"}));
1428}
1429
1430TEST_F(ArmatureBoneCollectionsTestList, move_before_after_index__other_parent_after__move_right)
1431{
1433 EXPECT_TRUE(expect_bcolls({"root", "child1", "child2", "child1_0", "child0"}));
1435}
1436
1437TEST_F(ArmatureBoneCollectionsTestList, move_before_after_index__to_root__before)
1438{
1440 EXPECT_TRUE(expect_bcolls({"child1_0", "root", "child0", "child1", "child2"}));
1442}
1443
1444TEST_F(ArmatureBoneCollectionsTestList, move_before_after_index__to_root__after)
1445{
1447 EXPECT_TRUE(expect_bcolls({"root", "child1_0", "child0", "child1", "child2"}));
1449}
1450
1452{
1453 /* Test with only one root. */
1455 EXPECT_TRUE(expect_bcolls({"root", "child0", "child1", "child2", "child1_0"}));
1456
1457 /* Move to "after the last child", which is the one root itself. */
1459 EXPECT_TRUE(expect_bcolls({"root", "child0", "child1", "child2", "child1_0"}));
1461 EXPECT_TRUE(expect_bcolls({"root", "child0", "child1", "child2", "child1_0"}));
1462
1463 /* Going beyond the number of children is not allowed. */
1465 EXPECT_TRUE(expect_bcolls({"root", "child0", "child1", "child2", "child1_0"}));
1466
1467 /* Add two roots to be able to play. */
1468 ANIM_armature_bonecoll_new(&arm, "root1");
1469 ANIM_armature_bonecoll_new(&arm, "root2");
1470 EXPECT_TRUE(expect_bcolls({"root", "root1", "root2", "child0", "child1", "child2", "child1_0"}));
1471
1472 /* Move the old root in between the two new ones. */
1474 EXPECT_TRUE(expect_bcolls({"root1", "root", "root2", "child0", "child1", "child2", "child1_0"}));
1475
1476 /* And to the last one. */
1478 EXPECT_TRUE(expect_bcolls({"root1", "root2", "root", "child0", "child1", "child2", "child1_0"}));
1479}
1480
1481TEST_F(ArmatureBoneCollectionsTestList, child_number_set__siblings)
1482{
1483 /* Move child0 to itself. */
1484 EXPECT_EQ(1, armature_bonecoll_child_number_set(&arm, child0, 0));
1485 EXPECT_TRUE(expect_bcolls({"root", "child0", "child1", "child2", "child1_0"}));
1486
1487 /* Move child2 to itself. */
1488 EXPECT_EQ(3, armature_bonecoll_child_number_set(&arm, child2, -1));
1489 EXPECT_TRUE(expect_bcolls({"root", "child0", "child1", "child2", "child1_0"}));
1490
1491 /* Going beyond the number of children is not allowed. */
1492 EXPECT_EQ(-1, armature_bonecoll_child_number_set(&arm, child0, 3));
1493 EXPECT_TRUE(expect_bcolls({"root", "child0", "child1", "child2", "child1_0"}));
1494
1495 /* Move child0 to in between child1 and child2. */
1496 EXPECT_EQ(2, armature_bonecoll_child_number_set(&arm, child0, 1));
1497 EXPECT_TRUE(expect_bcolls({"root", "child1", "child0", "child2", "child1_0"}));
1498
1499 /* Move child0 to the last spot. */
1500 EXPECT_EQ(3, armature_bonecoll_child_number_set(&arm, child0, 2));
1501 EXPECT_TRUE(expect_bcolls({"root", "child1", "child2", "child0", "child1_0"}));
1502}
1503
1505{
1506 EXPECT_FALSE(arm.flag & ARM_BCOLL_SOLO_ACTIVE) << "By default no solo'ing should be active";
1507
1508 /* Enable solo. */
1509 EXPECT_FALSE(child1->flags & BONE_COLLECTION_SOLO);
1510 ANIM_armature_bonecoll_solo_set(&arm, child1, true);
1511 EXPECT_TRUE(child1->flags & BONE_COLLECTION_SOLO);
1513
1514 /* Enable solo on another bone collection. */
1515 EXPECT_FALSE(child1_0->flags & BONE_COLLECTION_SOLO);
1516 ANIM_armature_bonecoll_solo_set(&arm, child1_0, true);
1517 EXPECT_TRUE(child1_0->flags & BONE_COLLECTION_SOLO);
1519
1520 /* Disable the first solo flag. */
1521 EXPECT_TRUE(child1->flags & BONE_COLLECTION_SOLO);
1522 ANIM_armature_bonecoll_solo_set(&arm, child1, false);
1523 EXPECT_FALSE(child1->flags & BONE_COLLECTION_SOLO);
1525
1526 /* Disable the second solo flag. This should also disable the ARM_BCOLL_SOLO_ACTIVE flag. */
1527 EXPECT_TRUE(child1_0->flags & BONE_COLLECTION_SOLO);
1528 ANIM_armature_bonecoll_solo_set(&arm, child1_0, false);
1529 EXPECT_FALSE(child1_0->flags & BONE_COLLECTION_SOLO);
1531}
1532
1534 protected:
1536
1542
1543 void SetUp() override
1544 {
1546
1547 /* TODO: make this clone `arm` into `dst_arm`, instead of assuming the below
1548 * code is still in sync with the super-class. */
1549 STRNCPY(dst_arm.id.name, "ARArmatureDST");
1550 dst_arm.runtime = MEM_new<bke::bArmature_Runtime>(__func__);
1551
1557
1558 ASSERT_STREQ(dst_root->name, dst_arm.collection_array[0]->name);
1559 ASSERT_STREQ(dst_child0->name, dst_arm.collection_array[1]->name);
1560 ASSERT_STREQ(dst_child1->name, dst_arm.collection_array[2]->name);
1561 ASSERT_STREQ(dst_child2->name, dst_arm.collection_array[3]->name);
1562 ASSERT_STREQ(dst_child1_0->name, dst_arm.collection_array[4]->name);
1563
1566 }
1567
1573};
1574
1576{
1577 /* Mimic that a new root, two children, and two grandchildren were added via library overrides.
1578 * These were saved in `arm`, and now need to be copied into `dst_arm`. */
1579 BoneCollection *src_root = ANIM_armature_bonecoll_new(&arm, "new_root");
1580 const int root_index = armature_bonecoll_find_index(&arm, src_root);
1581 BoneCollection *src_child1 = ANIM_armature_bonecoll_new(&arm, "new_child1", root_index);
1582 ANIM_armature_bonecoll_new(&arm, "new_child2", root_index);
1583 const int child1_index = armature_bonecoll_find_index(&arm, src_child1);
1584 ANIM_armature_bonecoll_new(&arm, "new_gchild1", child1_index);
1585 ANIM_armature_bonecoll_new(&arm, "new_gchild2", child1_index);
1586
1587 /* Copy the root. This should be the only change that's recorded by a library override operation.
1588 * It should also copy the entire subtree of that root. */
1589 const BoneCollection *anchor = dst_arm.collection_array[0];
1590 ASSERT_STREQ("root", anchor->name);
1592 &dst_arm, &arm, anchor, src_root);
1593
1594 /* Check the array order. */
1595 EXPECT_TRUE(expect_bcolls({"root",
1596 "new_root",
1597 "child0",
1598 "child1",
1599 "child2",
1600 "child1_0",
1601 "new_child1",
1602 "new_child2",
1603 "new_gchild1",
1604 "new_gchild2"}));
1605
1606 /* Check that the copied root is actually stored in the destination armature array. */
1607 const int new_root_index = armature_bonecoll_find_index(&dst_arm, copy_root);
1608 EXPECT_EQ(1, new_root_index);
1609
1610 /* Check the hierarchy. */
1611 const int new_child1_index = ANIM_armature_bonecoll_get_index_by_name(&dst_arm, "new_child1");
1612 EXPECT_TRUE(armature_bonecoll_is_root(&dst_arm, new_root_index));
1613 EXPECT_TRUE(armature_bonecoll_is_child_of(&dst_arm, new_root_index, new_child1_index));
1615 &dst_arm, new_root_index, ANIM_armature_bonecoll_get_index_by_name(&dst_arm, "new_child2")));
1617 &dst_arm,
1618 new_child1_index,
1619 ANIM_armature_bonecoll_get_index_by_name(&dst_arm, "new_gchild1")));
1621 &dst_arm,
1622 new_child1_index,
1623 ANIM_armature_bonecoll_get_index_by_name(&dst_arm, "new_gchild2")));
1624
1625 if (HasFailure()) {
1627 }
1628}
1629
1630} // namespace blender::animrig::tests
C++ functions to deal with Armature collections (i.e. the successor of bone layers).
#define G_MAIN
#define DATA_(msgid)
Read Blender memory allocation and freeing.
Internal C++ functions to deal with bone collections. These are mostly here for internal use in bone_...
testing::AssertionResult expect_bcolls(std::vector< std::string > expect_names)
#define EXPECT_FALSE(result)
#define EXPECT_NE(result, expect)
#define EXPECT_TRUE(result)
void bonecolls_rotate_block(bArmature *armature, const int start_index, const int count, const int direction)
void bonecolls_debug_list(const bArmature *armature)
TEST_F(ActionIteratorsTest, iterate_all_fcurves_of_slot)
int armature_bonecoll_find_index(const bArmature *armature, const BoneCollection *bcoll)
bool armature_bonecoll_is_root(const bArmature *armature, int bcoll_index)
int armature_bonecoll_child_number_set(bArmature *armature, BoneCollection *bcoll, int new_child_number)
int armature_bonecoll_find_parent_index(const bArmature *armature, int bcoll_index)
int armature_bonecoll_move_to_parent(bArmature *armature, int from_bcoll_index, int to_child_num, int from_parent_index, int to_parent_index)
bool armature_bonecoll_is_child_of(const bArmature *armature, int potential_parent_index, int potential_child_index)
bool ANIM_armature_bonecoll_assign(BoneCollection *bcoll, Bone *bone)
char * STRNCPY(char(&dst)[N], const char *src)
void ANIM_armature_bonecoll_active_index_set(bArmature *armature, int bone_collection_index)
BoneCollection * ANIM_armature_bonecoll_new(bArmature *armature, const char *name, int parent_index=-1)
Main * BKE_main_new()
Definition main.cc:93
void ANIM_armature_bonecoll_remove(bArmature *armature, BoneCollection *bcoll)
BoneCollection * ANIM_bonecoll_new(const char *name) ATTR_WARN_UNUSED_RESULT
void ANIM_bonecoll_free(BoneCollection *bcoll, bool do_id_user_count=true)
BoneCollection * ANIM_armature_bonecoll_insert_copy_after(bArmature *armature_dst, const bArmature *armature_src, const BoneCollection *anchor_in_dst, const BoneCollection *bcoll_to_copy)
void * BLI_findlink(const ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:541
void ANIM_bonecoll_hide(bArmature *armature, BoneCollection *bcoll)
void ANIM_armature_bonecoll_active_name_set(bArmature *armature, const char *name)
void BKE_main_free(Main *bmain)
Definition main.cc:202
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
int ANIM_armature_bonecoll_get_index_by_name(bArmature *armature, const char *name) ATTR_WARN_UNUSED_RESULT
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
void BKE_armature_bone_hash_make(bArmature *arm)
void ANIM_armature_bonecoll_active_set(bArmature *armature, BoneCollection *bcoll)
@ BONE_COLLECTION_OVERRIDE_LIBRARY_LOCAL
@ BONE_COLLECTION_ANCESTORS_VISIBLE
void BKE_libblock_free_datablock(ID *id, int flag) ATTR_NONNULL()
bool ANIM_armature_bonecoll_unassign(BoneCollection *bcoll, Bone *bone)
bool ANIM_armature_bonecoll_move_to_index(bArmature *armature, int from_index, int to_index)
int ANIM_armature_bonecoll_move_before_after_index(bArmature *armature, int from_index, int to_index, MoveLocation before_after)
void ANIM_armature_bonecoll_solo_set(bArmature *armature, BoneCollection *bcoll, bool is_solo)
bool ANIM_armature_bonecoll_is_editable(const bArmature *armature, const BoneCollection *bcoll)
eBoneCollection_Flag flags
ListBaseT< BoneCollectionMember > bones
ListBaseT< Bone > childbase
char name[258]
Definition DNA_ID.h:433
struct Library * lib
Definition DNA_ID.h:421
IDOverrideLibrary * override_library
Definition DNA_ID.h:495
bke::bArmature_Runtime * runtime
ListBaseT< Bone > bonebase
struct BoneCollection ** collection_array