1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
|
/*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2005 by the Blender Foundation.
* All rights reserved.
*
* Contributor(s): Daniel Dunbar
* Ton Roosendaal,
* Ben Batt,
* Brecht Van Lommel,
* Campbell Barton
*
* ***** END GPL LICENSE BLOCK *****
*
*/
/** \file blender/modifiers/intern/MOD_simpledeform.c
* \ingroup modifiers
*/
#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_object_types.h"
#include "BLI_math.h"
#include "BLI_utildefines.h"
#include "BKE_cdderivedmesh.h"
#include "BKE_editmesh.h"
#include "BKE_mesh.h"
#include "BKE_library_query.h"
#include "BKE_modifier.h"
#include "BKE_deform.h"
#include "MOD_util.h"
#include "bmesh.h"
#define BEND_EPS 0.000001f
/* Re-maps the indicies for X Y Z by shifting them up and wrapping, such that
* X = Y, Y = Z, Z = X (for X axis), and X = Z, Y = X, Z = Y (for Y axis). This
* exists because the deformations (excluding bend) are based on the Z axis.
* Having this helps avoid long, drawn out switches. */
static const uint axis_map_table[3][3] = {
{1, 2, 0},
{2, 0, 1},
{0, 1, 2},
};
BLI_INLINE void copy_v3_v3_map(float a[3], const float b[3], const uint map[3])
{
a[0] = b[map[0]];
a[1] = b[map[1]];
a[2] = b[map[2]];
}
BLI_INLINE void copy_v3_v3_unmap(float a[3], const float b[3], const uint map[3])
{
a[map[0]] = b[0];
a[map[1]] = b[1];
a[map[2]] = b[2];
}
/* Clamps/Limits the given coordinate to: limits[0] <= co[axis] <= limits[1]
* The amount of clamp is saved on dcut */
static void axis_limit(const int axis, const float limits[2], float co[3], float dcut[3])
{
float val = co[axis];
if (limits[0] > val) val = limits[0];
if (limits[1] < val) val = limits[1];
dcut[axis] = co[axis] - val;
co[axis] = val;
}
static void simpleDeform_taper(const float factor, const int UNUSED(axis), const float dcut[3], float r_co[3])
{
float x = r_co[0], y = r_co[1], z = r_co[2];
float scale = z * factor;
r_co[0] = x + x * scale;
r_co[1] = y + y * scale;
r_co[2] = z;
add_v3_v3(r_co, dcut);
}
static void simpleDeform_stretch(const float factor, const int UNUSED(axis), const float dcut[3], float r_co[3])
{
float x = r_co[0], y = r_co[1], z = r_co[2];
float scale;
scale = (z * z * factor - factor + 1.0f);
r_co[0] = x * scale;
r_co[1] = y * scale;
r_co[2] = z * (1.0f + factor);
add_v3_v3(r_co, dcut);
}
static void simpleDeform_twist(const float factor, const int UNUSED(axis), const float *dcut, float r_co[3])
{
float x = r_co[0], y = r_co[1], z = r_co[2];
float theta, sint, cost;
theta = z * factor;
sint = sinf(theta);
cost = cosf(theta);
r_co[0] = x * cost - y * sint;
r_co[1] = x * sint + y * cost;
r_co[2] = z;
add_v3_v3(r_co, dcut);
}
static void simpleDeform_bend(const float factor, const int axis, const float dcut[3], float r_co[3])
{
float x = r_co[0], y = r_co[1], z = r_co[2];
float theta, sint, cost;
BLI_assert(!(fabsf(factor) < BEND_EPS));
switch (axis) {
case 0:
ATTR_FALLTHROUGH;
case 1:
theta = z * factor;
break;
default:
theta = x * factor;
}
sint = sinf(theta);
cost = cosf(theta);
switch (axis) {
case 0:
r_co[0] = x;
r_co[1] = (y - 1.0f / factor) * cost + 1.0f / factor;
r_co[2] = -(y - 1.0f / factor) * sint;
{
r_co[0] += dcut[0];
r_co[1] += sint * dcut[2];
r_co[2] += cost * dcut[2];
}
break;
case 1:
r_co[0] = (x - 1.0f / factor) * cost + 1.0f / factor;
r_co[1] = y;
r_co[2] = -(x - 1.0f / factor) * sint;
{
r_co[0] += sint * dcut[2];
r_co[1] += dcut[1];
r_co[2] += cost * dcut[2];
}
break;
default:
r_co[0] = -(y - 1.0f / factor) * sint;
r_co[1] = (y - 1.0f / factor) * cost + 1.0f / factor;
r_co[2] = z;
{
r_co[0] += cost * dcut[0];
r_co[1] += sint * dcut[0];
r_co[2] += dcut[2];
}
}
}
/* simple deform modifier */
static void SimpleDeformModifier_do(SimpleDeformModifierData *smd, struct Object *ob, struct Mesh *mesh,
float (*vertexCos)[3], int numVerts)
{
const float base_limit[2] = {0.0f, 0.0f};
int i;
float smd_limit[2], smd_factor;
SpaceTransform *transf = NULL, tmp_transf;
void (*simpleDeform_callback)(const float factor, const int axis, const float dcut[3], float co[3]) = NULL; /* Mode callback */
int vgroup;
MDeformVert *dvert;
/* This is historically the lock axis, _not_ the deform axis as the name would imply */
const int deform_axis = smd->deform_axis;
int lock_axis = smd->axis;
if (smd->mode == MOD_SIMPLEDEFORM_MODE_BEND) { /* Bend mode shouln't have any lock axis */
lock_axis = 0;
}
else {
/* Don't lock axis if it is the chosen deform axis, as this flattens
* the geometry */
if (deform_axis == 0) {
lock_axis &= ~MOD_SIMPLEDEFORM_LOCK_AXIS_X;
}
if (deform_axis == 1) {
lock_axis &= ~MOD_SIMPLEDEFORM_LOCK_AXIS_Y;
}
if (deform_axis == 2) {
lock_axis &= ~MOD_SIMPLEDEFORM_LOCK_AXIS_Z;
}
}
/* Safe-check */
if (smd->origin == ob) smd->origin = NULL; /* No self references */
if (smd->limit[0] < 0.0f) smd->limit[0] = 0.0f;
if (smd->limit[0] > 1.0f) smd->limit[0] = 1.0f;
smd->limit[0] = min_ff(smd->limit[0], smd->limit[1]); /* Upper limit >= than lower limit */
/* Calculate matrixs do convert between coordinate spaces */
if (smd->origin) {
transf = &tmp_transf;
BLI_SPACE_TRANSFORM_SETUP(transf, ob, smd->origin);
}
/* Update limits if needed */
int limit_axis = deform_axis;
if (smd->mode == MOD_SIMPLEDEFORM_MODE_BEND) {
/* Bend is a special case. */
switch (deform_axis) {
case 0:
ATTR_FALLTHROUGH;
case 1:
limit_axis = 2;
break;
default:
limit_axis = 0;
}
}
{
float lower = FLT_MAX;
float upper = -FLT_MAX;
for (i = 0; i < numVerts; i++) {
float tmp[3];
copy_v3_v3(tmp, vertexCos[i]);
if (transf) {
BLI_space_transform_apply(transf, tmp);
}
lower = min_ff(lower, tmp[limit_axis]);
upper = max_ff(upper, tmp[limit_axis]);
}
/* SMD values are normalized to the BV, calculate the absolut values */
smd_limit[1] = lower + (upper - lower) * smd->limit[1];
smd_limit[0] = lower + (upper - lower) * smd->limit[0];
smd_factor = smd->factor / max_ff(FLT_EPSILON, smd_limit[1] - smd_limit[0]);
}
switch (smd->mode) {
case MOD_SIMPLEDEFORM_MODE_TWIST: simpleDeform_callback = simpleDeform_twist; break;
case MOD_SIMPLEDEFORM_MODE_BEND: simpleDeform_callback = simpleDeform_bend; break;
case MOD_SIMPLEDEFORM_MODE_TAPER: simpleDeform_callback = simpleDeform_taper; break;
case MOD_SIMPLEDEFORM_MODE_STRETCH: simpleDeform_callback = simpleDeform_stretch; break;
default:
return; /* No simpledeform mode? */
}
if (smd->mode == MOD_SIMPLEDEFORM_MODE_BEND) {
if (fabsf(smd_factor) < BEND_EPS) {
return;
}
}
modifier_get_vgroup_mesh(ob, mesh, smd->vgroup_name, &dvert, &vgroup);
const bool invert_vgroup = (smd->flag & MOD_SIMPLEDEFORM_FLAG_INVERT_VGROUP) != 0;
const uint *axis_map = axis_map_table[(smd->mode != MOD_SIMPLEDEFORM_MODE_BEND) ? deform_axis : 2];
for (i = 0; i < numVerts; i++) {
float weight = defvert_array_find_weight_safe(dvert, i, vgroup);
if (invert_vgroup) {
weight = 1.0f - weight;
}
if (weight != 0.0f) {
float co[3], dcut[3] = {0.0f, 0.0f, 0.0f};
if (transf) {
BLI_space_transform_apply(transf, vertexCos[i]);
}
copy_v3_v3(co, vertexCos[i]);
/* Apply axis limits, and axis mappings */
if (lock_axis & MOD_SIMPLEDEFORM_LOCK_AXIS_X) {
axis_limit(0, base_limit, co, dcut);
}
if (lock_axis & MOD_SIMPLEDEFORM_LOCK_AXIS_Y) {
axis_limit(1, base_limit, co, dcut);
}
if (lock_axis & MOD_SIMPLEDEFORM_LOCK_AXIS_Z) {
axis_limit(2, base_limit, co, dcut);
}
axis_limit(limit_axis, smd_limit, co, dcut);
/* apply the deform to a mapped copy of the vertex, and then re-map it back. */
float co_remap[3];
float dcut_remap[3];
copy_v3_v3_map(co_remap, co, axis_map);
copy_v3_v3_map(dcut_remap, dcut, axis_map);
simpleDeform_callback(smd_factor, deform_axis, dcut_remap, co_remap); /* apply deform */
copy_v3_v3_unmap(co, co_remap, axis_map);
interp_v3_v3v3(vertexCos[i], vertexCos[i], co, weight); /* Use vertex weight has coef of linear interpolation */
if (transf) {
BLI_space_transform_invert(transf, vertexCos[i]);
}
}
}
}
/* SimpleDeform */
static void initData(ModifierData *md)
{
SimpleDeformModifierData *smd = (SimpleDeformModifierData *) md;
smd->mode = MOD_SIMPLEDEFORM_MODE_TWIST;
smd->axis = 0;
smd->deform_axis = 0;
smd->origin = NULL;
smd->factor = DEG2RADF(45.0f);
smd->limit[0] = 0.0f;
smd->limit[1] = 1.0f;
}
static void copyData(ModifierData *md, ModifierData *target)
{
#if 0
SimpleDeformModifierData *smd = (SimpleDeformModifierData *)md;
SimpleDeformModifierData *tsmd = (SimpleDeformModifierData *)target;
#endif
modifier_copyData_generic(md, target);
}
static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
{
SimpleDeformModifierData *smd = (SimpleDeformModifierData *)md;
CustomDataMask dataMask = 0;
/* ask for vertexgroups if we need them */
if (smd->vgroup_name[0])
dataMask |= CD_MASK_MDEFORMVERT;
return dataMask;
}
static void foreachObjectLink(
ModifierData *md, Object *ob,
ObjectWalkFunc walk, void *userData)
{
SimpleDeformModifierData *smd = (SimpleDeformModifierData *)md;
walk(userData, ob, &smd->origin, IDWALK_CB_NOP);
}
static void updateDepsgraph(ModifierData *md, const ModifierUpdateDepsgraphContext *ctx)
{
SimpleDeformModifierData *smd = (SimpleDeformModifierData *)md;
if (smd->origin != NULL) {
DEG_add_object_relation(ctx->node, smd->origin, DEG_OB_COMP_TRANSFORM, "SimpleDeform Modifier");
}
}
static void deformVerts(ModifierData *md, const ModifierEvalContext *ctx,
struct Mesh *mesh,
float (*vertexCos)[3],
int numVerts)
{
Mesh *mesh_src = mesh ? mesh : ctx->object->data;
SimpleDeformModifier_do((SimpleDeformModifierData *)md, ctx->object, mesh_src, vertexCos, numVerts);
}
static void deformVertsEM(ModifierData *md, const ModifierEvalContext *ctx,
struct BMEditMesh *editData,
struct Mesh *mesh,
float (*vertexCos)[3],
int numVerts)
{
Mesh *mesh_src = mesh;
if (!mesh) {
mesh_src = BKE_bmesh_to_mesh_nomain(editData->bm, &(struct BMeshToMeshParams){0});
}
SimpleDeformModifier_do((SimpleDeformModifierData *)md, ctx->object, mesh_src, vertexCos, numVerts);
}
ModifierTypeInfo modifierType_SimpleDeform = {
/* name */ "SimpleDeform",
/* structName */ "SimpleDeformModifierData",
/* structSize */ sizeof(SimpleDeformModifierData),
/* type */ eModifierTypeType_OnlyDeform,
/* flags */ eModifierTypeFlag_AcceptsMesh |
eModifierTypeFlag_AcceptsCVs |
eModifierTypeFlag_AcceptsLattice |
eModifierTypeFlag_SupportsEditmode |
eModifierTypeFlag_EnableInEditmode,
/* copyData */ copyData,
/* deformVerts_DM */ NULL,
/* deformMatrices_DM */ NULL,
/* deformVertsEM_DM */ NULL,
/* deformMatricesEM_DM*/NULL,
/* applyModifier_DM */ NULL,
/* applyModifierEM_DM */NULL,
/* deformVerts */ deformVerts,
/* deformMatrices */ NULL,
/* deformVertsEM */ deformVertsEM,
/* deformMatricesEM */ NULL,
/* applyModifier */ NULL,
/* applyModifierEM */ NULL,
/* initData */ initData,
/* requiredDataMask */ requiredDataMask,
/* freeData */ NULL,
/* isDisabled */ NULL,
/* updateDepsgraph */ updateDepsgraph,
/* dependsOnTime */ NULL,
/* dependsOnNormals */ NULL,
/* foreachObjectLink */ foreachObjectLink,
/* foreachIDLink */ NULL,
/* foreachTexLink */ NULL,
};
|