Blender V5.3
deform_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BKE_deform.hh"
6#include "BKE_gtest_base.hh"
7
9
10#include "testing/testing.h"
11
12namespace blender::bke::tests {
13
15
17{
18 /* Just making sure we don't crash on a vertex with no weights. */
19
20 /* Some compilers don't like zero-length arrays, so we just do nullptr here as
21 * a stand-in. */
22 MDeformWeight *weights = nullptr;
23
24 MDeformVert vert = {weights, 0, 0};
25
26 BKE_defvert_normalize_ex(vert, {}, {}, {});
27}
28
30{
31 MDeformWeight weights[1];
32 weights[0].def_nr = 0;
33 MDeformVert vert = {weights, 1, 0};
34
35 /* Excluded from normalized set: shouldn't be touched. */
36 weights[0].weight = 0.5;
37 BKE_defvert_normalize_ex(vert, {false}, {false}, {false});
38 EXPECT_FLOAT_EQ(0.5, weights[0].weight);
39
40 /* Locked: shouldn't be touched. */
41 weights[0].weight = 0.5;
42 BKE_defvert_normalize_ex(vert, {true}, {true}, {false});
43 EXPECT_FLOAT_EQ(0.5, weights[0].weight);
44
45 /* Unlocked: should get normalized to 1.0. */
46 weights[0].weight = 0.5;
47 BKE_defvert_normalize_ex(vert, {true}, {false}, {false});
48 EXPECT_FLOAT_EQ(1.0, weights[0].weight);
49
50 /* Unlocked and soft-locked: should get normalized to 1.0. */
51 weights[0].weight = 0.5;
52 BKE_defvert_normalize_ex(vert, {true}, {false}, {true});
53 EXPECT_FLOAT_EQ(1.0, weights[0].weight);
54
55 /* Locked and soft-locked: shouldn't be touched (locked takes precedent). */
56 weights[0].weight = 0.5;
57 BKE_defvert_normalize_ex(vert, {true}, {true}, {true});
58 EXPECT_FLOAT_EQ(0.5, weights[0].weight);
59
60 /* An empty "subset" flag list should be equivalent to everything being included. */
61 weights[0].weight = 0.5;
62 BKE_defvert_normalize_ex(vert, {}, {false}, {false});
63 EXPECT_FLOAT_EQ(1.0, weights[0].weight);
64
65 /* An empty "locked" flag list should be equivalent to everything being unlocked. */
66 weights[0].weight = 0.5;
67 BKE_defvert_normalize_ex(vert, {true}, {}, {false});
68 EXPECT_FLOAT_EQ(1.0, weights[0].weight);
69
70 /* Zero weight: single-group vertices are special cased for some reason to be
71 * set to 1.0. */
72 weights[0].weight = 0.0;
73 BKE_defvert_normalize_ex(vert, {}, {}, {});
74 EXPECT_FLOAT_EQ(1.0, weights[0].weight);
75
76 /* Zero weight locked: shouldn't be touched. */
77 weights[0].weight = 0.0;
78 BKE_defvert_normalize_ex(vert, {}, {true}, {});
79 EXPECT_FLOAT_EQ(0.0, weights[0].weight);
80}
81
83{
84 MDeformWeight weights[2];
85 weights[0].def_nr = 0;
86 weights[1].def_nr = 1;
87 MDeformVert vert = {weights, 2, 0};
88
89 /* Both excluded from normalized set: shouldn't be touched. */
90 weights[0].weight = 0.25;
91 weights[1].weight = 0.25;
92 BKE_defvert_normalize_ex(vert, {false, false}, {false, false}, {false, false});
93 EXPECT_FLOAT_EQ(0.25, weights[0].weight);
94 EXPECT_FLOAT_EQ(0.25, weights[1].weight);
95
96 /* One included: included one should be set to 1.0. */
97 weights[0].weight = 0.25;
98 weights[1].weight = 0.25;
99 BKE_defvert_normalize_ex(vert, {false, true}, {false, false}, {false, false});
100 EXPECT_FLOAT_EQ(0.25, weights[0].weight);
101 EXPECT_FLOAT_EQ(1.0, weights[1].weight);
102
103 /* Both included: should be normalized together. */
104 weights[0].weight = 0.25;
105 weights[1].weight = 0.25;
106 BKE_defvert_normalize_ex(vert, {true, true}, {false, false}, {false, false});
107 EXPECT_FLOAT_EQ(0.5, weights[0].weight);
108 EXPECT_FLOAT_EQ(0.5, weights[1].weight);
109
110 /* All flag arrays being empty should mean: included, unlocked, and not "just
111 * set". So this should behave as a simple normalization across both groups. */
112 weights[0].weight = 0.25;
113 weights[1].weight = 0.25;
114 BKE_defvert_normalize_ex(vert, {}, {}, {});
115 EXPECT_FLOAT_EQ(0.5, weights[0].weight);
116 EXPECT_FLOAT_EQ(0.5, weights[1].weight);
117
118 /* Both included but locked: shouldn't be touched. */
119 weights[0].weight = 0.25;
120 weights[1].weight = 0.25;
121 BKE_defvert_normalize_ex(vert, {}, {true, true}, {false, false});
122 EXPECT_FLOAT_EQ(0.25, weights[0].weight);
123 EXPECT_FLOAT_EQ(0.25, weights[1].weight);
124
125 /* Only one locked: locked shouldn't be touched, unlocked should pick up the
126 * slack for normalization. */
127 weights[0].weight = 0.25;
128 weights[1].weight = 0.25;
129 BKE_defvert_normalize_ex(vert, {}, {true, false}, {false, false});
130 EXPECT_FLOAT_EQ(0.25, weights[0].weight);
131 EXPECT_FLOAT_EQ(0.75, weights[1].weight);
132
133 /* Only one marked as soft-locked: soft-locked shouldn't be touched, the other
134 * should pick up the slack for normalization. */
135 weights[0].weight = 0.25;
136 weights[1].weight = 0.25;
137 BKE_defvert_normalize_ex(vert, {}, {false, false}, {true, false});
138 EXPECT_FLOAT_EQ(0.25, weights[0].weight);
139 EXPECT_FLOAT_EQ(0.75, weights[1].weight);
140
141 /* One locked, the other marked as soft-locked: soft-locked should pick up the
142 * slack for normalization. */
143 weights[0].weight = 0.25;
144 weights[1].weight = 0.25;
145 BKE_defvert_normalize_ex(vert, {}, {true, false}, {false, true});
146 EXPECT_FLOAT_EQ(0.25, weights[0].weight);
147 EXPECT_FLOAT_EQ(0.75, weights[1].weight);
148
149 /* Zero weight: shouldn't be touched. */
150 weights[0].weight = 0.0;
151 weights[1].weight = 0.0;
152 BKE_defvert_normalize_ex(vert, {}, {false, false}, {false, false});
153 EXPECT_FLOAT_EQ(0.0, weights[0].weight);
154 EXPECT_FLOAT_EQ(0.0, weights[1].weight);
155
156 /* Zero weight with one group soft-locked: soft-locked should pick up the slack. */
157 weights[0].weight = 0.0;
158 weights[1].weight = 0.0;
159 BKE_defvert_normalize_ex(vert, {}, {false, false}, {false, true});
160 EXPECT_FLOAT_EQ(0.0, weights[0].weight);
161 EXPECT_FLOAT_EQ(1.0, weights[1].weight);
162
163 /* Zero weight with both groups soft-locked: both should pick up the slack equally. */
164 weights[0].weight = 0.0;
165 weights[1].weight = 0.0;
166 BKE_defvert_normalize_ex(vert, {}, {false, false}, {true, true});
167 EXPECT_FLOAT_EQ(0.5, weights[0].weight);
168 EXPECT_FLOAT_EQ(0.5, weights[1].weight);
169}
170
172{
173 /* Note the out-of-order `def_nr`, which is part of this test. Further below,
174 * we write the weights ordered to line up with the boolean arrays to make
175 * things easier to follow. */
176 MDeformWeight weights[4];
177 weights[0].def_nr = 3;
178 weights[1].def_nr = 0;
179 weights[2].def_nr = 1;
180 weights[3].def_nr = 2;
181 MDeformVert vert = {weights, 4, 0};
182
183 /* One locked, one soft-locked: the remaining two should pick up the slack. */
184 weights[1].weight = 0.125;
185 weights[2].weight = 0.125;
186 weights[3].weight = 0.125;
187 weights[0].weight = 0.0625;
188 BKE_defvert_normalize_ex(vert, {}, {true, false, false, false}, {false, false, true, false});
189 EXPECT_FLOAT_EQ(0.125, weights[1].weight);
190 EXPECT_FLOAT_EQ(0.75 * 2.0 / 3.0, weights[2].weight);
191 EXPECT_FLOAT_EQ(0.125, weights[3].weight);
192 EXPECT_FLOAT_EQ(0.75 / 3.0, weights[0].weight);
193
194 /* One locked, two soft-locked: the remaining one should pick up the slack. */
195 weights[1].weight = 0.125;
196 weights[2].weight = 0.125;
197 weights[3].weight = 0.125;
198 weights[0].weight = 0.125;
199 BKE_defvert_normalize_ex(vert, {}, {true, false, false, false}, {false, true, true, false});
200 EXPECT_FLOAT_EQ(0.125, weights[1].weight);
201 EXPECT_FLOAT_EQ(0.125, weights[2].weight);
202 EXPECT_FLOAT_EQ(0.125, weights[3].weight);
203 EXPECT_FLOAT_EQ(0.625, weights[0].weight);
204
205 /* One locked, one soft-locked, and the rest zero-weight: the soft-locked one
206 * should pick up the slack. */
207 weights[1].weight = 0.125;
208 weights[2].weight = 0.0;
209 weights[3].weight = 0.125;
210 weights[0].weight = 0.0;
211 BKE_defvert_normalize_ex(vert, {}, {true, false, false, false}, {false, false, true, false});
212 EXPECT_FLOAT_EQ(0.125, weights[1].weight);
213 EXPECT_FLOAT_EQ(0.0, weights[2].weight);
214 EXPECT_FLOAT_EQ(0.875, weights[3].weight);
215 EXPECT_FLOAT_EQ(0.0, weights[0].weight);
216
217 /* One locked, two soft-locked, and the last zero-weight: the soft-locked ones
218 * should pick up the slack. */
219 weights[1].weight = 0.125;
220 weights[2].weight = 0.125;
221 weights[3].weight = 0.25;
222 weights[0].weight = 0.0;
223 BKE_defvert_normalize_ex(vert, {}, {true, false, false, false}, {false, true, true, false});
224 EXPECT_FLOAT_EQ(0.125, weights[1].weight);
225 EXPECT_FLOAT_EQ(0.875 / 3.0, weights[2].weight);
226 EXPECT_FLOAT_EQ(0.875 * 2.0 / 3.0, weights[3].weight);
227 EXPECT_FLOAT_EQ(0.0f, weights[0].weight);
228}
229
230} // namespace blender::bke::tests
support for deformation groups and hooks.
TEST_F(AnimDataTest, BKE_fcurves_id_cb_test)
void BKE_defvert_normalize_ex(MDeformVert &dvert, Span< bool > subset_flags, Span< bool > lock_flags, Span< bool > soft_lock_flags)
Definition deform.cc:252