svn merge ^/trunk/blender -r42617:42655
[blender.git] / source / blender / modifiers / intern / MOD_cast.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software  Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2005 by the Blender Foundation.
19  * All rights reserved.
20  *
21  * Contributor(s): Daniel Dunbar
22  *                 Ton Roosendaal,
23  *                 Ben Batt,
24  *                 Brecht Van Lommel,
25  *                 Campbell Barton
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  *
29  */
30
31 /** \file blender/modifiers/intern/MOD_cast.c
32  *  \ingroup modifiers
33  */
34
35
36 #include "DNA_meshdata_types.h"
37 #include "DNA_object_types.h"
38
39 #include "BLI_math.h"
40 #include "BLI_utildefines.h"
41 #include "BLI_string.h"
42
43
44 #include "BKE_deform.h"
45 #include "BKE_DerivedMesh.h"
46 #include "BKE_modifier.h"
47
48
49 #include "depsgraph_private.h"
50
51 #include "MOD_util.h"
52
53 static void initData(ModifierData *md)
54 {
55         CastModifierData *cmd = (CastModifierData*) md;
56
57         cmd->fac = 0.5f;
58         cmd->radius = 0.0f;
59         cmd->size = 0.0f;
60         cmd->flag = MOD_CAST_X | MOD_CAST_Y | MOD_CAST_Z
61                         | MOD_CAST_SIZE_FROM_RADIUS;
62         cmd->type = MOD_CAST_TYPE_SPHERE;
63         cmd->defgrp_name[0] = '\0';
64         cmd->object = NULL;
65 }
66
67
68 static void copyData(ModifierData *md, ModifierData *target)
69 {
70         CastModifierData *cmd = (CastModifierData*) md;
71         CastModifierData *tcmd = (CastModifierData*) target;
72
73         tcmd->fac = cmd->fac;
74         tcmd->radius = cmd->radius;
75         tcmd->size = cmd->size;
76         tcmd->flag = cmd->flag;
77         tcmd->type = cmd->type;
78         tcmd->object = cmd->object;
79         BLI_strncpy(tcmd->defgrp_name, cmd->defgrp_name, 32);
80 }
81
82 static int isDisabled(ModifierData *md, int UNUSED(useRenderParams))
83 {
84         CastModifierData *cmd = (CastModifierData*) md;
85         short flag;
86         
87         flag = cmd->flag & (MOD_CAST_X|MOD_CAST_Y|MOD_CAST_Z);
88
89         if((cmd->fac == 0.0f) || flag == 0) return 1;
90
91         return 0;
92 }
93
94 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
95 {
96         CastModifierData *cmd = (CastModifierData *)md;
97         CustomDataMask dataMask = 0;
98
99         /* ask for vertexgroups if we need them */
100         if(cmd->defgrp_name[0]) dataMask |= CD_MASK_MDEFORMVERT;
101
102         return dataMask;
103 }
104
105 static void foreachObjectLink(
106                                            ModifierData *md, Object *ob,
107         void (*walk)(void *userData, Object *ob, Object **obpoin),
108                    void *userData)
109 {
110         CastModifierData *cmd = (CastModifierData*) md;
111
112         walk (userData, ob, &cmd->object);
113 }
114
115 static void updateDepgraph(ModifierData *md, DagForest *forest,
116                                                 struct Scene *UNUSED(scene),
117                                                 Object *UNUSED(ob),
118                                                 DagNode *obNode)
119 {
120         CastModifierData *cmd = (CastModifierData*) md;
121
122         if (cmd->object) {
123                 DagNode *curNode = dag_get_node(forest, cmd->object);
124
125                 dag_add_relation(forest, curNode, obNode, DAG_RL_OB_DATA,
126                         "Cast Modifier");
127         }
128 }
129
130 static void sphere_do(
131                                    CastModifierData *cmd, Object *ob, DerivedMesh *dm,
132            float (*vertexCos)[3], int numVerts)
133 {
134         MDeformVert *dvert = NULL;
135
136         Object *ctrl_ob = NULL;
137
138         int i, defgrp_index;
139         int has_radius = 0;
140         short flag, type;
141         float fac, facm, len = 0.0f;
142         float vec[3], center[3] = {0.0f, 0.0f, 0.0f};
143         float mat[4][4], imat[4][4];
144
145         fac = cmd->fac;
146         facm = 1.0f - fac;
147
148         flag = cmd->flag;
149         type = cmd->type; /* projection type: sphere or cylinder */
150
151         if (type == MOD_CAST_TYPE_CYLINDER) 
152                 flag &= ~MOD_CAST_Z;
153
154         ctrl_ob = cmd->object;
155
156         /* spherify's center is {0, 0, 0} (the ob's own center in its local
157         * space), by default, but if the user defined a control object,
158         * we use its location, transformed to ob's local space */
159         if (ctrl_ob) {
160                 if(flag & MOD_CAST_USE_OB_TRANSFORM) {
161                         invert_m4_m4(ctrl_ob->imat, ctrl_ob->obmat);
162                         mul_m4_m4m4(mat, ob->obmat, ctrl_ob->imat);
163                         invert_m4_m4(imat, mat);
164                 }
165
166                 invert_m4_m4(ob->imat, ob->obmat);
167                 mul_v3_m4v3(center, ob->imat, ctrl_ob->obmat[3]);
168         }
169
170         /* now we check which options the user wants */
171
172         /* 1) (flag was checked in the "if (ctrl_ob)" block above) */
173         /* 2) cmd->radius > 0.0f: only the vertices within this radius from
174         * the center of the effect should be deformed */
175         if (cmd->radius > FLT_EPSILON) has_radius = 1;
176
177         /* 3) if we were given a vertex group name,
178         * only those vertices should be affected */
179         modifier_get_vgroup(ob, dm, cmd->defgrp_name, &dvert, &defgrp_index);
180
181         if(flag & MOD_CAST_SIZE_FROM_RADIUS) {
182                 len = cmd->radius;
183         }
184         else {
185                 len = cmd->size;
186         }
187
188         if(len <= 0) {
189                 for (i = 0; i < numVerts; i++) {
190                         len += len_v3v3(center, vertexCos[i]);
191                 }
192                 len /= numVerts;
193
194                 if (len == 0.0f) len = 10.0f;
195         }
196
197         /* ready to apply the effect, one vertex at a time;
198         * tiny optimization: the code is separated (with parts repeated)
199          * in two possible cases:
200         * with or w/o a vgroup. With lots of if's in the code below,
201         * further optimizations are possible, if needed */
202         if (dvert) { /* with a vgroup */
203                 MDeformVert *dv= dvert;
204                 float fac_orig = fac;
205                 for (i = 0; i < numVerts; i++, dv++) {
206                         float tmp_co[3];
207                         float weight;
208
209                         copy_v3_v3(tmp_co, vertexCos[i]);
210                         if(ctrl_ob) {
211                                 if(flag & MOD_CAST_USE_OB_TRANSFORM) {
212                                         mul_m4_v3(mat, tmp_co);
213                                 } else {
214                                         sub_v3_v3(tmp_co, center);
215                                 }
216                         }
217
218                         copy_v3_v3(vec, tmp_co);
219
220                         if (type == MOD_CAST_TYPE_CYLINDER)
221                                 vec[2] = 0.0f;
222
223                         if (has_radius) {
224                                 if (len_v3(vec) > cmd->radius) continue;
225                         }
226
227                         weight= defvert_find_weight(dv, defgrp_index);
228                         if (weight <= 0.0f) continue;
229
230                         fac = fac_orig * weight;
231                         facm = 1.0f - fac;
232
233                         normalize_v3(vec);
234
235                         if (flag & MOD_CAST_X)
236                                 tmp_co[0] = fac*vec[0]*len + facm*tmp_co[0];
237                         if (flag & MOD_CAST_Y)
238                                 tmp_co[1] = fac*vec[1]*len + facm*tmp_co[1];
239                         if (flag & MOD_CAST_Z)
240                                 tmp_co[2] = fac*vec[2]*len + facm*tmp_co[2];
241
242                         if(ctrl_ob) {
243                                 if(flag & MOD_CAST_USE_OB_TRANSFORM) {
244                                         mul_m4_v3(imat, tmp_co);
245                                 } else {
246                                         add_v3_v3(tmp_co, center);
247                                 }
248                         }
249
250                         copy_v3_v3(vertexCos[i], tmp_co);
251                 }
252                 return;
253         }
254
255         /* no vgroup */
256         for (i = 0; i < numVerts; i++) {
257                 float tmp_co[3];
258
259                 copy_v3_v3(tmp_co, vertexCos[i]);
260                 if(ctrl_ob) {
261                         if(flag & MOD_CAST_USE_OB_TRANSFORM) {
262                                 mul_m4_v3(mat, tmp_co);
263                         } else {
264                                 sub_v3_v3(tmp_co, center);
265                         }
266                 }
267
268                 copy_v3_v3(vec, tmp_co);
269
270                 if (type == MOD_CAST_TYPE_CYLINDER)
271                         vec[2] = 0.0f;
272
273                 if (has_radius) {
274                         if (len_v3(vec) > cmd->radius) continue;
275                 }
276
277                 normalize_v3(vec);
278
279                 if (flag & MOD_CAST_X)
280                         tmp_co[0] = fac*vec[0]*len + facm*tmp_co[0];
281                 if (flag & MOD_CAST_Y)
282                         tmp_co[1] = fac*vec[1]*len + facm*tmp_co[1];
283                 if (flag & MOD_CAST_Z)
284                         tmp_co[2] = fac*vec[2]*len + facm*tmp_co[2];
285
286                 if(ctrl_ob) {
287                         if(flag & MOD_CAST_USE_OB_TRANSFORM) {
288                                 mul_m4_v3(imat, tmp_co);
289                         } else {
290                                 add_v3_v3(tmp_co, center);
291                         }
292                 }
293
294                 copy_v3_v3(vertexCos[i], tmp_co);
295         }
296 }
297
298 static void cuboid_do(
299                                    CastModifierData *cmd, Object *ob, DerivedMesh *dm,
300            float (*vertexCos)[3], int numVerts)
301 {
302         MDeformVert *dvert = NULL;
303         Object *ctrl_ob = NULL;
304
305         int i, defgrp_index;
306         int has_radius = 0;
307         short flag;
308         float fac, facm;
309         float min[3], max[3], bb[8][3];
310         float center[3] = {0.0f, 0.0f, 0.0f};
311         float mat[4][4], imat[4][4];
312
313         fac = cmd->fac;
314         facm = 1.0f - fac;
315
316         flag = cmd->flag;
317
318         ctrl_ob = cmd->object;
319
320         /* now we check which options the user wants */
321
322         /* 1) (flag was checked in the "if (ctrl_ob)" block above) */
323         /* 2) cmd->radius > 0.0f: only the vertices within this radius from
324         * the center of the effect should be deformed */
325         if (cmd->radius > FLT_EPSILON) has_radius = 1;
326
327         /* 3) if we were given a vertex group name,
328         * only those vertices should be affected */
329         modifier_get_vgroup(ob, dm, cmd->defgrp_name, &dvert, &defgrp_index);
330
331         if (ctrl_ob) {
332                 if(flag & MOD_CAST_USE_OB_TRANSFORM) {
333                         invert_m4_m4(ctrl_ob->imat, ctrl_ob->obmat);
334                         mul_m4_m4m4(mat, ob->obmat, ctrl_ob->imat);
335                         invert_m4_m4(imat, mat);
336                 }
337
338                 invert_m4_m4(ob->imat, ob->obmat);
339                 mul_v3_m4v3(center, ob->imat, ctrl_ob->obmat[3]);
340         }
341
342         if((flag & MOD_CAST_SIZE_FROM_RADIUS) && has_radius) {
343                 for(i = 0; i < 3; i++) {
344                         min[i] = -cmd->radius;
345                         max[i] = cmd->radius;
346                 }
347         } else if(!(flag & MOD_CAST_SIZE_FROM_RADIUS) && cmd->size > 0) {
348                 for(i = 0; i < 3; i++) {
349                         min[i] = -cmd->size;
350                         max[i] = cmd->size;
351                 }
352         } else {
353                 /* get bound box */
354                 /* We can't use the object's bound box because other modifiers
355                 * may have changed the vertex data. */
356                 INIT_MINMAX(min, max);
357
358                 /* Cast's center is the ob's own center in its local space,
359                 * by default, but if the user defined a control object, we use
360                 * its location, transformed to ob's local space. */
361                 if (ctrl_ob) {
362                         float vec[3];
363
364                         /* let the center of the ctrl_ob be part of the bound box: */
365                         DO_MINMAX(center, min, max);
366
367                         for (i = 0; i < numVerts; i++) {
368                                 sub_v3_v3v3(vec, vertexCos[i], center);
369                                 DO_MINMAX(vec, min, max);
370                         }
371                 }
372                 else {
373                         for (i = 0; i < numVerts; i++) {
374                                 DO_MINMAX(vertexCos[i], min, max);
375                         }
376                 }
377
378                 /* we want a symmetric bound box around the origin */
379                 if (fabs(min[0]) > fabs(max[0])) max[0] = fabs(min[0]); 
380                 if (fabs(min[1]) > fabs(max[1])) max[1] = fabs(min[1]); 
381                 if (fabs(min[2]) > fabs(max[2])) max[2] = fabs(min[2]);
382                 min[0] = -max[0];
383                 min[1] = -max[1];
384                 min[2] = -max[2];
385         }
386
387         /* building our custom bounding box */
388         bb[0][0] = bb[2][0] = bb[4][0] = bb[6][0] = min[0];
389         bb[1][0] = bb[3][0] = bb[5][0] = bb[7][0] = max[0];
390         bb[0][1] = bb[1][1] = bb[4][1] = bb[5][1] = min[1];
391         bb[2][1] = bb[3][1] = bb[6][1] = bb[7][1] = max[1];
392         bb[0][2] = bb[1][2] = bb[2][2] = bb[3][2] = min[2];
393         bb[4][2] = bb[5][2] = bb[6][2] = bb[7][2] = max[2];
394
395         /* ready to apply the effect, one vertex at a time;
396         * tiny optimization: the code is separated (with parts repeated)
397          * in two possible cases:
398         * with or w/o a vgroup. With lots of if's in the code below,
399         * further optimizations are possible, if needed */
400         if (dvert) { /* with a vgroup */
401                 float fac_orig = fac;
402                 for (i = 0; i < numVerts; i++) {
403                         MDeformWeight *dw = NULL;
404                         int j, octant, coord;
405                         float d[3], dmax, apex[3], fbb;
406                         float tmp_co[3];
407
408                         copy_v3_v3(tmp_co, vertexCos[i]);
409                         if(ctrl_ob) {
410                                 if(flag & MOD_CAST_USE_OB_TRANSFORM) {
411                                         mul_m4_v3(mat, tmp_co);
412                                 } else {
413                                         sub_v3_v3(tmp_co, center);
414                                 }
415                         }
416
417                         if (has_radius) {
418                                 if (fabsf(tmp_co[0]) > cmd->radius ||
419                                                                 fabsf(tmp_co[1]) > cmd->radius ||
420                                                                 fabsf(tmp_co[2]) > cmd->radius) continue;
421                         }
422
423                         for (j = 0; j < dvert[i].totweight; ++j) {
424                                 if(dvert[i].dw[j].def_nr == defgrp_index) {
425                                         dw = &dvert[i].dw[j];
426                                         break;
427                                 }
428                         }
429                         if (!dw) continue;
430
431                         fac = fac_orig * dw->weight;
432                         facm = 1.0f - fac;
433
434                         /* The algo used to project the vertices to their
435                          * bounding box (bb) is pretty simple:
436                          * for each vertex v:
437                         * 1) find in which octant v is in;
438                         * 2) find which outer "wall" of that octant is closer to v;
439                         * 3) calculate factor (var fbb) to project v to that wall;
440                         * 4) project. */
441
442                         /* find in which octant this vertex is in */
443                         octant = 0;
444                         if (tmp_co[0] > 0.0f) octant += 1;
445                         if (tmp_co[1] > 0.0f) octant += 2;
446                         if (tmp_co[2] > 0.0f) octant += 4;
447
448                         /* apex is the bb's vertex at the chosen octant */
449                         copy_v3_v3(apex, bb[octant]);
450
451                         /* find which bb plane is closest to this vertex ... */
452                         d[0] = tmp_co[0] / apex[0];
453                         d[1] = tmp_co[1] / apex[1];
454                         d[2] = tmp_co[2] / apex[2];
455
456                         /* ... (the closest has the higher (closer to 1) d value) */
457                         dmax = d[0];
458                         coord = 0;
459                         if (d[1] > dmax) {
460                                 dmax = d[1];
461                                 coord = 1;
462                         }
463                         if (d[2] > dmax) {
464                                 /* dmax = d[2]; */ /* commented, we don't need it */
465                                 coord = 2;
466                         }
467
468                         /* ok, now we know which coordinate of the vertex to use */
469
470                         if (fabsf(tmp_co[coord]) < FLT_EPSILON) /* avoid division by zero */
471                                 continue;
472
473                         /* finally, this is the factor we wanted, to project the vertex
474                         * to its bounding box (bb) */
475                         fbb = apex[coord] / tmp_co[coord];
476
477                         /* calculate the new vertex position */
478                         if (flag & MOD_CAST_X)
479                                 tmp_co[0] = facm * tmp_co[0] + fac * tmp_co[0] * fbb;
480                         if (flag & MOD_CAST_Y)
481                                 tmp_co[1] = facm * tmp_co[1] + fac * tmp_co[1] * fbb;
482                         if (flag & MOD_CAST_Z)
483                                 tmp_co[2] = facm * tmp_co[2] + fac * tmp_co[2] * fbb;
484
485                         if(ctrl_ob) {
486                                 if(flag & MOD_CAST_USE_OB_TRANSFORM) {
487                                         mul_m4_v3(imat, tmp_co);
488                                 } else {
489                                         add_v3_v3(tmp_co, center);
490                                 }
491                         }
492
493                         copy_v3_v3(vertexCos[i], tmp_co);
494                 }
495                 return;
496         }
497
498         /* no vgroup (check previous case for comments about the code) */
499         for (i = 0; i < numVerts; i++) {
500                 int octant, coord;
501                 float d[3], dmax, fbb, apex[3];
502                 float tmp_co[3];
503
504                 copy_v3_v3(tmp_co, vertexCos[i]);
505                 if(ctrl_ob) {
506                         if(flag & MOD_CAST_USE_OB_TRANSFORM) {
507                                 mul_m4_v3(mat, tmp_co);
508                         } else {
509                                 sub_v3_v3(tmp_co, center);
510                         }
511                 }
512
513                 if (has_radius) {
514                         if (fabsf(tmp_co[0]) > cmd->radius ||
515                                                  fabsf(tmp_co[1]) > cmd->radius ||
516                                                  fabsf(tmp_co[2]) > cmd->radius) continue;
517                 }
518
519                 octant = 0;
520                 if (tmp_co[0] > 0.0f) octant += 1;
521                 if (tmp_co[1] > 0.0f) octant += 2;
522                 if (tmp_co[2] > 0.0f) octant += 4;
523
524                 copy_v3_v3(apex, bb[octant]);
525
526                 d[0] = tmp_co[0] / apex[0];
527                 d[1] = tmp_co[1] / apex[1];
528                 d[2] = tmp_co[2] / apex[2];
529
530                 dmax = d[0];
531                 coord = 0;
532                 if (d[1] > dmax) {
533                         dmax = d[1];
534                         coord = 1;
535                 }
536                 if (d[2] > dmax) {
537                         /* dmax = d[2]; */ /* commented, we don't need it */
538                         coord = 2;
539                 }
540
541                 if (fabsf(tmp_co[coord]) < FLT_EPSILON)
542                         continue;
543
544                 fbb = apex[coord] / tmp_co[coord];
545
546                 if (flag & MOD_CAST_X)
547                         tmp_co[0] = facm * tmp_co[0] + fac * tmp_co[0] * fbb;
548                 if (flag & MOD_CAST_Y)
549                         tmp_co[1] = facm * tmp_co[1] + fac * tmp_co[1] * fbb;
550                 if (flag & MOD_CAST_Z)
551                         tmp_co[2] = facm * tmp_co[2] + fac * tmp_co[2] * fbb;
552
553                 if(ctrl_ob) {
554                         if(flag & MOD_CAST_USE_OB_TRANSFORM) {
555                                 mul_m4_v3(imat, tmp_co);
556                         } else {
557                                 add_v3_v3(tmp_co, center);
558                         }
559                 }
560
561                 copy_v3_v3(vertexCos[i], tmp_co);
562         }
563 }
564
565 static void deformVerts(ModifierData *md, Object *ob,
566                                                 DerivedMesh *derivedData,
567                                                 float (*vertexCos)[3],
568                                                 int numVerts,
569                                                 int UNUSED(useRenderParams),
570                                                 int UNUSED(isFinalCalc))
571 {
572         DerivedMesh *dm = NULL;
573         CastModifierData *cmd = (CastModifierData *)md;
574
575         dm = get_dm(ob, NULL, derivedData, NULL, 0);
576
577         if (cmd->type == MOD_CAST_TYPE_CUBOID) {
578                 cuboid_do(cmd, ob, dm, vertexCos, numVerts);
579         } else { /* MOD_CAST_TYPE_SPHERE or MOD_CAST_TYPE_CYLINDER */
580                 sphere_do(cmd, ob, dm, vertexCos, numVerts);
581         }
582
583         if(dm != derivedData)
584                 dm->release(dm);
585 }
586
587 static void deformVertsEM(
588                                            ModifierData *md, Object *ob, struct BMEditMesh *editData,
589            DerivedMesh *derivedData, float (*vertexCos)[3], int numVerts)
590 {
591         DerivedMesh *dm = get_dm(ob, editData, derivedData, NULL, 0);
592         CastModifierData *cmd = (CastModifierData *)md;
593
594         if (cmd->type == MOD_CAST_TYPE_CUBOID) {
595                 cuboid_do(cmd, ob, dm, vertexCos, numVerts);
596         } else { /* MOD_CAST_TYPE_SPHERE or MOD_CAST_TYPE_CYLINDER */
597                 sphere_do(cmd, ob, dm, vertexCos, numVerts);
598         }
599
600         if(dm != derivedData)
601                 dm->release(dm);
602 }
603
604
605 ModifierTypeInfo modifierType_Cast = {
606         /* name */              "Cast",
607         /* structName */        "CastModifierData",
608         /* structSize */        sizeof(CastModifierData),
609         /* type */              eModifierTypeType_OnlyDeform,
610         /* flags */             eModifierTypeFlag_AcceptsCVs
611                                                         | eModifierTypeFlag_SupportsEditmode,
612
613         /* copyData */          copyData,
614         /* deformVerts */       deformVerts,
615         /* deformMatrices */    NULL,
616         /* deformVertsEM */     deformVertsEM,
617         /* deformMatricesEM */  NULL,
618         /* applyModifier */     NULL,
619         /* applyModifierEM */   NULL,
620         /* initData */          initData,
621         /* requiredDataMask */  requiredDataMask,
622         /* freeData */          NULL,
623         /* isDisabled */        isDisabled,
624         /* updateDepgraph */    updateDepgraph,
625         /* dependsOnTime */     NULL,
626         /* dependsOnNormals */  NULL,
627         /* foreachObjectLink */ foreachObjectLink,
628         /* foreachIDLink */     NULL,
629         /* foreachTexLink */    NULL,
630 };