Patch: [#30652] Influence slider for Lattice Modifier
[blender-staging.git] / source / blender / blenkernel / intern / lattice.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) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenkernel/intern/lattice.c
29  *  \ingroup bke
30  */
31
32
33
34 #include <stdio.h>
35 #include <string.h>
36 #include <math.h>
37 #include <stdlib.h>
38
39 #include "MEM_guardedalloc.h"
40
41 #include "BLI_blenlib.h"
42 #include "BLI_bpath.h"
43 #include "BLI_math.h"
44 #include "BLI_utildefines.h"
45
46 #include "DNA_mesh_types.h"
47 #include "DNA_meshdata_types.h"
48 #include "DNA_scene_types.h"
49 #include "DNA_object_types.h"
50 #include "DNA_lattice_types.h"
51 #include "DNA_curve_types.h"
52 #include "DNA_key_types.h"
53
54 #include "BKE_animsys.h"
55 #include "BKE_anim.h"
56 #include "BKE_cdderivedmesh.h"
57 #include "BKE_displist.h"
58 #include "BKE_global.h"
59 #include "BKE_key.h"
60 #include "BKE_lattice.h"
61 #include "BKE_library.h"
62 #include "BKE_main.h"
63 #include "BKE_mesh.h"
64 #include "BKE_modifier.h"
65
66 #include "BKE_deform.h"
67
68 //XXX #include "BIF_editdeform.h"
69
70 void calc_lat_fudu(int flag, int res, float *fu, float *du)
71 {
72         if (res==1) {
73                 *fu= 0.0;
74                 *du= 0.0;
75         }
76         else if (flag & LT_GRID) {
77                 *fu= -0.5f*(res-1);
78                 *du= 1.0f;
79         }
80         else {
81                 *fu= -1.0f;
82                 *du= 2.0f/(res-1);
83         }
84 }
85
86 void resizelattice(Lattice *lt, int uNew, int vNew, int wNew, Object *ltOb)
87 {
88         BPoint *bp;
89         int i, u, v, w;
90         float fu, fv, fw, uc, vc, wc, du=0.0, dv=0.0, dw=0.0;
91         float *co, (*vertexCos)[3] = NULL;
92         
93         /* vertex weight groups are just freed all for now */
94         if (lt->dvert) {
95                 free_dverts(lt->dvert, lt->pntsu*lt->pntsv*lt->pntsw);
96                 lt->dvert= NULL;
97         }
98         
99         while (uNew*vNew*wNew > 32000) {
100                 if ( uNew>=vNew && uNew>=wNew) uNew--;
101                 else if ( vNew>=uNew && vNew>=wNew) vNew--;
102                 else wNew--;
103         }
104
105         vertexCos = MEM_mallocN(sizeof(*vertexCos)*uNew*vNew*wNew, "tmp_vcos");
106
107         calc_lat_fudu(lt->flag, uNew, &fu, &du);
108         calc_lat_fudu(lt->flag, vNew, &fv, &dv);
109         calc_lat_fudu(lt->flag, wNew, &fw, &dw);
110
111                 /* If old size is different then resolution changed in interface,
112                  * try to do clever reinit of points. Pretty simply idea, we just
113                  * deform new verts by old lattice, but scaling them to match old
114                  * size first.
115                  */
116         if (ltOb) {
117                 if (uNew!=1 && lt->pntsu!=1) {
118                         fu = lt->fu;
119                         du = (lt->pntsu-1)*lt->du/(uNew-1);
120                 }
121
122                 if (vNew!=1 && lt->pntsv!=1) {
123                         fv = lt->fv;
124                         dv = (lt->pntsv-1)*lt->dv/(vNew-1);
125                 }
126
127                 if (wNew!=1 && lt->pntsw!=1) {
128                         fw = lt->fw;
129                         dw = (lt->pntsw-1)*lt->dw/(wNew-1);
130                 }
131         }
132
133         co = vertexCos[0];
134         for (w=0,wc=fw; w<wNew; w++,wc+=dw) {
135                 for (v=0,vc=fv; v<vNew; v++,vc+=dv) {
136                         for (u=0,uc=fu; u<uNew; u++,co+=3,uc+=du) {
137                                 co[0] = uc;
138                                 co[1] = vc;
139                                 co[2] = wc;
140                         }
141                 }
142         }
143         
144         if (ltOb) {
145                 float mat[4][4];
146                 int typeu = lt->typeu, typev = lt->typev, typew = lt->typew;
147
148                         /* works best if we force to linear type (endpoints match) */
149                 lt->typeu = lt->typev = lt->typew = KEY_LINEAR;
150
151                         /* prevent using deformed locations */
152                 freedisplist(&ltOb->disp);
153
154                 copy_m4_m4(mat, ltOb->obmat);
155                 unit_m4(ltOb->obmat);
156                 lattice_deform_verts(ltOb, NULL, NULL, vertexCos, uNew*vNew*wNew, NULL, 1.0f);
157                 copy_m4_m4(ltOb->obmat, mat);
158
159                 lt->typeu = typeu;
160                 lt->typev = typev;
161                 lt->typew = typew;
162         }
163
164         lt->fu = fu;
165         lt->fv = fv;
166         lt->fw = fw;
167         lt->du = du;
168         lt->dv = dv;
169         lt->dw = dw;
170
171         lt->pntsu = uNew;
172         lt->pntsv = vNew;
173         lt->pntsw = wNew;
174
175         MEM_freeN(lt->def);
176         lt->def= MEM_callocN(lt->pntsu*lt->pntsv*lt->pntsw*sizeof(BPoint), "lattice bp");
177         
178         bp= lt->def;
179         
180         for (i=0; i<lt->pntsu*lt->pntsv*lt->pntsw; i++,bp++) {
181                 copy_v3_v3(bp->vec, vertexCos[i]);
182         }
183
184         MEM_freeN(vertexCos);
185 }
186
187 Lattice *add_lattice(const char *name)
188 {
189         Lattice *lt;
190         
191         lt= alloc_libblock(&G.main->latt, ID_LT, name);
192         
193         lt->flag= LT_GRID;
194         
195         lt->typeu= lt->typev= lt->typew= KEY_BSPLINE;
196         
197         lt->def= MEM_callocN(sizeof(BPoint), "lattvert"); /* temporary */
198         resizelattice(lt, 2, 2, 2, NULL);       /* creates a uniform lattice */
199                 
200         return lt;
201 }
202
203 Lattice *copy_lattice(Lattice *lt)
204 {
205         Lattice *ltn;
206
207         ltn= copy_libblock(&lt->id);
208         ltn->def= MEM_dupallocN(lt->def);
209
210         ltn->key= copy_key(ltn->key);
211         if (ltn->key) ltn->key->from= (ID *)ltn;
212         
213         if (lt->dvert) {
214                 int tot= lt->pntsu*lt->pntsv*lt->pntsw;
215                 ltn->dvert = MEM_mallocN (sizeof (MDeformVert)*tot, "Lattice MDeformVert");
216                 copy_dverts(ltn->dvert, lt->dvert, tot);
217         }
218
219         ltn->editlatt= NULL;
220
221         return ltn;
222 }
223
224 void free_lattice(Lattice *lt)
225 {
226         if (lt->def) MEM_freeN(lt->def);
227         if (lt->dvert) free_dverts(lt->dvert, lt->pntsu*lt->pntsv*lt->pntsw);
228         if (lt->editlatt) {
229                 Lattice *editlt= lt->editlatt->latt;
230
231                 if (editlt->def) MEM_freeN(editlt->def);
232                 if (editlt->dvert) free_dverts(editlt->dvert, lt->pntsu*lt->pntsv*lt->pntsw);
233
234                 MEM_freeN(editlt);
235                 MEM_freeN(lt->editlatt);
236         }
237         
238         /* free animation data */
239         if (lt->adt) {
240                 BKE_free_animdata(&lt->id);
241                 lt->adt= NULL;
242         }
243 }
244
245
246 void make_local_lattice(Lattice *lt)
247 {
248         Main *bmain= G.main;
249         Object *ob;
250         int is_local= FALSE, is_lib= FALSE;
251
252         /* - only lib users: do nothing
253          * - only local users: set flag
254          * - mixed: make copy
255          */
256         
257         if (lt->id.lib==NULL) return;
258         if (lt->id.us==1) {
259                 id_clear_lib_data(bmain, &lt->id);
260                 return;
261         }
262         
263         for (ob= bmain->object.first; ob && ELEM(FALSE, is_lib, is_local); ob= ob->id.next) {
264                 if (ob->data==lt) {
265                         if (ob->id.lib) is_lib= TRUE;
266                         else is_local= TRUE;
267                 }
268         }
269         
270         if (is_local && is_lib==FALSE) {
271                 id_clear_lib_data(bmain, &lt->id);
272         }
273         else if (is_local && is_lib) {
274                 Lattice *lt_new= copy_lattice(lt);
275                 lt_new->id.us= 0;
276
277                 /* Remap paths of new ID using old library as base. */
278                 BKE_id_lib_local_paths(bmain, lt->id.lib, &lt_new->id);
279
280                 for (ob= bmain->object.first; ob; ob= ob->id.next) {
281                         if (ob->data==lt) {
282                                 if (ob->id.lib==NULL) {
283                                         ob->data= lt_new;
284                                         lt_new->id.us++;
285                                         lt->id.us--;
286                                 }
287                         }
288                 }
289         }
290 }
291
292 void init_latt_deform(Object *oblatt, Object *ob)
293 {
294                 /* we make an array with all differences */
295         Lattice *lt= oblatt->data;
296         BPoint *bp;
297         DispList *dl = find_displist(&oblatt->disp, DL_VERTS);
298         float *co = dl?dl->verts:NULL;
299         float *fp, imat[4][4];
300         float fu, fv, fw;
301         int u, v, w;
302
303         if (lt->editlatt) lt= lt->editlatt->latt;
304         bp = lt->def;
305         
306         fp= lt->latticedata= MEM_mallocN(sizeof(float)*3*lt->pntsu*lt->pntsv*lt->pntsw, "latticedata");
307         
308                 /* for example with a particle system: ob==0 */
309         if (ob==NULL) {
310                 /* in deformspace, calc matrix  */
311                 invert_m4_m4(lt->latmat, oblatt->obmat);
312         
313                 /* back: put in deform array */
314                 invert_m4_m4(imat, lt->latmat);
315         }
316         else {
317                 /* in deformspace, calc matrix */
318                 invert_m4_m4(imat, oblatt->obmat);
319                 mult_m4_m4m4(lt->latmat, imat, ob->obmat);
320         
321                 /* back: put in deform array */
322                 invert_m4_m4(imat, lt->latmat);
323         }
324         
325         for (w=0,fw=lt->fw; w<lt->pntsw; w++,fw+=lt->dw) {
326                 for (v=0,fv=lt->fv; v<lt->pntsv; v++, fv+=lt->dv) {
327                         for (u=0,fu=lt->fu; u<lt->pntsu; u++, bp++, co+=3, fp+=3, fu+=lt->du) {
328                                 if (dl) {
329                                         fp[0] = co[0] - fu;
330                                         fp[1] = co[1] - fv;
331                                         fp[2] = co[2] - fw;
332                                 }
333                                 else {
334                                         fp[0] = bp->vec[0] - fu;
335                                         fp[1] = bp->vec[1] - fv;
336                                         fp[2] = bp->vec[2] - fw;
337                                 }
338
339                                 mul_mat3_m4_v3(imat, fp);
340                         }
341                 }
342         }
343 }
344
345 void calc_latt_deform(Object *ob, float *co, float weight)
346 {
347         Lattice *lt= ob->data;
348         float u, v, w, tu[4], tv[4], tw[4];
349         float vec[3];
350         int idx_w, idx_v, idx_u;
351         int ui, vi, wi, uu, vv, ww;
352
353         /* vgroup influence */
354         int defgroup_nr= -1;
355         float co_prev[3], weight_blend= 0.0f;
356         MDeformVert *dvert= lattice_get_deform_verts(ob);
357
358
359         if (lt->editlatt) lt= lt->editlatt->latt;
360         if (lt->latticedata==NULL) return;
361
362         if (lt->vgroup[0] && dvert) {
363                 defgroup_nr= defgroup_name_index(ob, lt->vgroup);
364                 copy_v3_v3(co_prev, co);
365         }
366
367         /* co is in local coords, treat with latmat */
368         mul_v3_m4v3(vec, lt->latmat, co);
369
370         /* u v w coords */
371
372         if (lt->pntsu>1) {
373                 u= (vec[0]-lt->fu)/lt->du;
374                 ui= (int)floor(u);
375                 u -= ui;
376                 key_curve_position_weights(u, tu, lt->typeu);
377         }
378         else {
379                 tu[0]= tu[2]= tu[3]= 0.0; tu[1]= 1.0;
380                 ui= 0;
381         }
382
383         if (lt->pntsv>1) {
384                 v= (vec[1]-lt->fv)/lt->dv;
385                 vi= (int)floor(v);
386                 v -= vi;
387                 key_curve_position_weights(v, tv, lt->typev);
388         }
389         else {
390                 tv[0]= tv[2]= tv[3]= 0.0; tv[1]= 1.0;
391                 vi= 0;
392         }
393
394         if (lt->pntsw>1) {
395                 w= (vec[2]-lt->fw)/lt->dw;
396                 wi= (int)floor(w);
397                 w -= wi;
398                 key_curve_position_weights(w, tw, lt->typew);
399         }
400         else {
401                 tw[0]= tw[2]= tw[3]= 0.0; tw[1]= 1.0;
402                 wi= 0;
403         }
404
405         for (ww= wi-1; ww<=wi+2; ww++) {
406                 w= tw[ww-wi+1];
407
408                 if (w != 0.0f) {
409                         if (ww>0) {
410                                 if (ww<lt->pntsw) idx_w= ww*lt->pntsu*lt->pntsv;
411                                 else idx_w= (lt->pntsw-1)*lt->pntsu*lt->pntsv;
412                         }
413                         else idx_w= 0;
414
415                         for (vv= vi-1; vv<=vi+2; vv++) {
416                                 v= w*tv[vv-vi+1];
417
418                                 if (v != 0.0f) {
419                                         if (vv>0) {
420                                                 if (vv<lt->pntsv) idx_v= idx_w + vv*lt->pntsu;
421                                                 else idx_v= idx_w + (lt->pntsv-1)*lt->pntsu;
422                                         }
423                                         else idx_v= idx_w;
424
425                                         for (uu= ui-1; uu<=ui+2; uu++) {
426                                                 u= weight*v*tu[uu-ui+1];
427
428                                                 if (u != 0.0f) {
429                                                         if (uu>0) {
430                                                                 if (uu<lt->pntsu) idx_u= idx_v + uu;
431                                                                 else idx_u= idx_v + (lt->pntsu-1);
432                                                         }
433                                                         else idx_u= idx_v;
434
435                                                         madd_v3_v3fl(co, &lt->latticedata[idx_u * 3], u);
436
437                                                         if (defgroup_nr != -1)
438                                                                 weight_blend += (u * defvert_find_weight(dvert + idx_u, defgroup_nr));
439                                                 }
440                                         }
441                                 }
442                         }
443                 }
444         }
445
446         if (defgroup_nr != -1)
447                 interp_v3_v3v3(co, co_prev, co, weight_blend);
448
449 }
450
451 void end_latt_deform(Object *ob)
452 {
453         Lattice *lt= ob->data;
454         
455         if (lt->editlatt) lt= lt->editlatt->latt;
456         
457         if (lt->latticedata)
458                 MEM_freeN(lt->latticedata);
459         lt->latticedata= NULL;
460 }
461
462         /* calculations is in local space of deformed object
463          * so we store in latmat transform from path coord inside object 
464          */
465 typedef struct {
466         float dmin[3], dmax[3];
467         float curvespace[4][4], objectspace[4][4], objectspace3[3][3];
468         int no_rot_axis;
469 } CurveDeform;
470
471 static void init_curve_deform(Object *par, Object *ob, CurveDeform *cd)
472 {
473         invert_m4_m4(ob->imat, ob->obmat);
474         mult_m4_m4m4(cd->objectspace, ob->imat, par->obmat);
475         invert_m4_m4(cd->curvespace, cd->objectspace);
476         copy_m3_m4(cd->objectspace3, cd->objectspace);
477         cd->no_rot_axis= 0;
478 }
479
480 /* this makes sure we can extend for non-cyclic.
481  *
482  * returns OK: 1/0
483  */
484 static int where_on_path_deform(Object *ob, float ctime, float vec[4], float dir[3], float quat[4], float *radius)
485 {
486         Curve *cu= ob->data;
487         BevList *bl;
488         float ctime1;
489         int cycl=0;
490         
491         /* test for cyclic */
492         bl= cu->bev.first;
493         if (!bl->nr) return 0;
494         if (bl && bl->poly> -1) cycl= 1;
495
496         if (cycl==0) {
497                 ctime1= CLAMPIS(ctime, 0.0f, 1.0f);
498         }
499         else ctime1= ctime;
500         
501         /* vec needs 4 items */
502         if (where_on_path(ob, ctime1, vec, dir, quat, radius, NULL)) {
503                 
504                 if (cycl==0) {
505                         Path *path= cu->path;
506                         float dvec[3];
507                         
508                         if (ctime < 0.0f) {
509                                 sub_v3_v3v3(dvec, path->data[1].vec, path->data[0].vec);
510                                 mul_v3_fl(dvec, ctime*(float)path->len);
511                                 add_v3_v3(vec, dvec);
512                                 if (quat) copy_qt_qt(quat, path->data[0].quat);
513                                 if (radius) *radius= path->data[0].radius;
514                         }
515                         else if (ctime > 1.0f) {
516                                 sub_v3_v3v3(dvec, path->data[path->len-1].vec, path->data[path->len-2].vec);
517                                 mul_v3_fl(dvec, (ctime-1.0f)*(float)path->len);
518                                 add_v3_v3(vec, dvec);
519                                 if (quat) copy_qt_qt(quat, path->data[path->len-1].quat);
520                                 if (radius) *radius= path->data[path->len-1].radius;
521                                 /* weight - not used but could be added */
522                         }
523                 }
524                 return 1;
525         }
526         return 0;
527 }
528
529 /* for each point, rotate & translate to curve */
530 /* use path, since it has constant distances */
531 /* co: local coord, result local too */
532 /* returns quaternion for rotation, using cd->no_rot_axis */
533 /* axis is using another define!!! */
534 static int calc_curve_deform(Scene *scene, Object *par, float co[3],
535                              const short axis, CurveDeform *cd, float quat_r[4])
536 {
537         Curve *cu= par->data;
538         float fac, loc[4], dir[3], new_quat[4], radius;
539         short index;
540         const int is_neg_axis = (axis > 2);
541
542         /* to be sure, mostly after file load */
543         if (cu->path==NULL) {
544                 makeDispListCurveTypes(scene, par, 0);
545                 if (cu->path==NULL) return 0;   // happens on append...
546         }
547         
548         /* options */
549         if (is_neg_axis) {
550                 index = axis - 3;
551                 if (cu->flag & CU_STRETCH)
552                         fac= (-co[index]-cd->dmax[index])/(cd->dmax[index] - cd->dmin[index]);
553                 else
554                         fac= - (co[index]-cd->dmax[index])/(cu->path->totdist);
555         }
556         else {
557                 index = axis;
558                 if (cu->flag & CU_STRETCH)
559                         fac= (co[index]-cd->dmin[index])/(cd->dmax[index] - cd->dmin[index]);
560                 else
561                         fac= + (co[index]-cd->dmin[index])/(cu->path->totdist);
562         }
563         
564         if ( where_on_path_deform(par, fac, loc, dir, new_quat, &radius)) {     /* returns OK */
565                 float quat[4], cent[3];
566
567                 if (cd->no_rot_axis) {  /* set by caller */
568
569                         /* this is not exactly the same as 2.4x, since the axis is having rotation removed rather than
570                          * changing the axis before calculating the tilt but serves much the same purpose */
571                         float dir_flat[3]={0,0,0}, q[4];
572                         copy_v3_v3(dir_flat, dir);
573                         dir_flat[cd->no_rot_axis-1]= 0.0f;
574
575                         normalize_v3(dir);
576                         normalize_v3(dir_flat);
577
578                         rotation_between_vecs_to_quat(q, dir, dir_flat); /* Could this be done faster? */
579
580                         mul_qt_qtqt(new_quat, q, new_quat);
581                 }
582
583
584                 /* Logic for 'cent' orientation *
585                  *
586                  * The way 'co' is copied to 'cent' may seem to have no meaning, but it does.
587                  *
588                  * Use a curve modifier to stretch a cube out, color each side RGB, positive side light, negative dark.
589                  * view with X up (default), from the angle that you can see 3 faces RGB colors (light), anti-clockwise
590                  * Notice X,Y,Z Up all have light colors and each ordered CCW.
591                  *
592                  * Now for Neg Up XYZ, the colors are all dark, and ordered clockwise - Campbell
593                  *
594                  * note: moved functions into quat_apply_track/vec_apply_track
595                  * */
596                 copy_qt_qt(quat, new_quat);
597                 copy_v3_v3(cent, co);
598
599                 /* zero the axis which is not used,
600                  * the big block of text above now applies to these 3 lines */
601                 quat_apply_track(quat, axis, (axis == 0 || axis == 2) ? 1:0); /* up flag is a dummy, set so no rotation is done */
602                 vec_apply_track(cent, axis);
603                 cent[index]= 0.0f;
604
605
606                 /* scale if enabled */
607                 if (cu->flag & CU_PATH_RADIUS)
608                         mul_v3_fl(cent, radius);
609                 
610                 /* local rotation */
611                 normalize_qt(quat);
612                 mul_qt_v3(quat, cent);
613
614                 /* translation */
615                 add_v3_v3v3(co, cent, loc);
616
617                 if (quat_r)
618                         copy_qt_qt(quat_r, quat);
619
620                 return 1;
621         }
622         return 0;
623 }
624
625 void curve_deform_verts(Scene *scene, Object *cuOb, Object *target,
626                         DerivedMesh *dm, float (*vertexCos)[3],
627                         int numVerts, const char *vgroup, short defaxis)
628 {
629         Curve *cu;
630         int a, flag;
631         CurveDeform cd;
632         int use_vgroups;
633         const int is_neg_axis = (defaxis > 2);
634
635         if (cuOb->type != OB_CURVE)
636                 return;
637
638         cu = cuOb->data;
639         flag = cu->flag;
640         cu->flag |= (CU_PATH|CU_FOLLOW); // needed for path & bevlist
641
642         init_curve_deform(cuOb, target, &cd);
643
644         /* dummy bounds, keep if CU_DEFORM_BOUNDS_OFF is set */
645         if (is_neg_axis == FALSE) {
646                 cd.dmin[0]= cd.dmin[1]= cd.dmin[2]= 0.0f;
647                 cd.dmax[0]= cd.dmax[1]= cd.dmax[2]= 1.0f;
648         }
649         else {
650                 /* negative, these bounds give a good rest position */
651                 cd.dmin[0]= cd.dmin[1]= cd.dmin[2]= -1.0f;
652                 cd.dmax[0]= cd.dmax[1]= cd.dmax[2]=  0.0f;
653         }
654         
655         /* check whether to use vertex groups (only possible if target is a Mesh)
656          * we want either a Mesh with no derived data, or derived data with
657          * deformverts
658          */
659         if (target && target->type==OB_MESH) {
660                 /* if there's derived data without deformverts, don't use vgroups */
661                 if (dm && !dm->getVertData(dm, 0, CD_MDEFORMVERT))
662                         use_vgroups = 0;
663                 else
664                         use_vgroups = 1;
665         }
666         else {
667                 use_vgroups = 0;
668         }
669         
670         if (vgroup && vgroup[0] && use_vgroups) {
671                 Mesh *me= target->data;
672                 int index= defgroup_name_index(target, vgroup);
673
674                 if (index != -1 && (me->dvert || dm)) {
675                         MDeformVert *dvert = me->dvert;
676                         float vec[3];
677                         float weight;
678         
679
680                         if (cu->flag & CU_DEFORM_BOUNDS_OFF) {
681                                 dvert = me->dvert;
682                                 for (a = 0; a < numVerts; a++, dvert++) {
683                                         if (dm) dvert = dm->getVertData(dm, a, CD_MDEFORMVERT);
684                                         weight= defvert_find_weight(dvert, index);
685         
686                                         if (weight > 0.0f) {
687                                                 mul_m4_v3(cd.curvespace, vertexCos[a]);
688                                                 copy_v3_v3(vec, vertexCos[a]);
689                                                 calc_curve_deform(scene, cuOb, vec, defaxis, &cd, NULL);
690                                                 interp_v3_v3v3(vertexCos[a], vertexCos[a], vec, weight);
691                                                 mul_m4_v3(cd.objectspace, vertexCos[a]);
692                                         }
693                                 }
694                         }
695                         else {
696                                 /* set mesh min/max bounds */
697                                 INIT_MINMAX(cd.dmin, cd.dmax);
698         
699                                 for (a = 0; a < numVerts; a++, dvert++) {
700                                         if (dm) dvert = dm->getVertData(dm, a, CD_MDEFORMVERT);
701                                         
702                                         if (defvert_find_weight(dvert, index) > 0.0f) {
703                                                 mul_m4_v3(cd.curvespace, vertexCos[a]);
704                                                 DO_MINMAX(vertexCos[a], cd.dmin, cd.dmax);
705                                         }
706                                 }
707         
708                                 dvert = me->dvert;
709                                 for (a = 0; a < numVerts; a++, dvert++) {
710                                         if (dm) dvert = dm->getVertData(dm, a, CD_MDEFORMVERT);
711                                         
712                                         weight= defvert_find_weight(dvert, index);
713         
714                                         if (weight > 0.0f) {
715                                                 /* already in 'cd.curvespace', prev for loop */
716                                                 copy_v3_v3(vec, vertexCos[a]);
717                                                 calc_curve_deform(scene, cuOb, vec, defaxis, &cd, NULL);
718                                                 interp_v3_v3v3(vertexCos[a], vertexCos[a], vec, weight);
719                                                 mul_m4_v3(cd.objectspace, vertexCos[a]);
720                                         }
721                                 }
722                         }
723                 }
724         }
725         else {
726                 if (cu->flag & CU_DEFORM_BOUNDS_OFF) {
727                         for (a = 0; a < numVerts; a++) {
728                                 mul_m4_v3(cd.curvespace, vertexCos[a]);
729                                 calc_curve_deform(scene, cuOb, vertexCos[a], defaxis, &cd, NULL);
730                                 mul_m4_v3(cd.objectspace, vertexCos[a]);
731                         }
732                 }
733                 else {
734                         /* set mesh min max bounds */
735                         INIT_MINMAX(cd.dmin, cd.dmax);
736                                 
737                         for (a = 0; a < numVerts; a++) {
738                                 mul_m4_v3(cd.curvespace, vertexCos[a]);
739                                 DO_MINMAX(vertexCos[a], cd.dmin, cd.dmax);
740                         }
741         
742                         for (a = 0; a < numVerts; a++) {
743                                 /* already in 'cd.curvespace', prev for loop */
744                                 calc_curve_deform(scene, cuOb, vertexCos[a], defaxis, &cd, NULL);
745                                 mul_m4_v3(cd.objectspace, vertexCos[a]);
746                         }
747                 }
748         }
749         cu->flag = flag;
750 }
751
752 /* input vec and orco = local coord in armature space */
753 /* orco is original not-animated or deformed reference point */
754 /* result written in vec and mat */
755 void curve_deform_vector(Scene *scene, Object *cuOb, Object *target,
756                          float orco[3], float vec[3], float mat[][3], int no_rot_axis)
757 {
758         CurveDeform cd;
759         float quat[4];
760         
761         if (cuOb->type != OB_CURVE) {
762                 unit_m3(mat);
763                 return;
764         }
765
766         init_curve_deform(cuOb, target, &cd);
767         cd.no_rot_axis= no_rot_axis;                            /* option to only rotate for XY, for example */
768         
769         copy_v3_v3(cd.dmin, orco);
770         copy_v3_v3(cd.dmax, orco);
771
772         mul_m4_v3(cd.curvespace, vec);
773         
774         if (calc_curve_deform(scene, cuOb, vec, target->trackflag, &cd, quat)) {
775                 float qmat[3][3];
776                 
777                 quat_to_mat3( qmat,quat);
778                 mul_m3_m3m3(mat, qmat, cd.objectspace3);
779         }
780         else
781                 unit_m3(mat);
782         
783         mul_m4_v3(cd.objectspace, vec);
784
785 }
786
787 void lattice_deform_verts(Object *laOb, Object *target, DerivedMesh *dm,
788                           float (*vertexCos)[3], int numVerts, const char *vgroup, float influence)
789 {
790         int a;
791         int use_vgroups;
792
793         if (laOb->type != OB_LATTICE)
794                 return;
795
796         init_latt_deform(laOb, target);
797
798         /* check whether to use vertex groups (only possible if target is a Mesh)
799          * we want either a Mesh with no derived data, or derived data with
800          * deformverts
801          */
802         if (target && target->type==OB_MESH) {
803                 /* if there's derived data without deformverts, don't use vgroups */
804                 if (dm && !dm->getVertData(dm, 0, CD_MDEFORMVERT))
805                         use_vgroups = 0;
806                 else
807                         use_vgroups = 1;
808         }
809         else {
810                 use_vgroups = 0;
811         }
812         
813         if (vgroup && vgroup[0] && use_vgroups) {
814                 Mesh *me = target->data;
815                 int index = defgroup_name_index(target, vgroup);
816                 float weight;
817
818                 if (index >= 0 && (me->dvert || dm)) {
819                         MDeformVert *dvert = me->dvert;
820                         
821                         for (a = 0; a < numVerts; a++, dvert++) {
822                                 if (dm) dvert = dm->getVertData(dm, a, CD_MDEFORMVERT);
823
824                                 weight= defvert_find_weight(dvert, index);
825
826                                 if (weight > 0.0f)
827                                         calc_latt_deform(laOb, vertexCos[a], weight*influence);
828                         }
829                 }
830         }
831         else {
832                 for (a = 0; a < numVerts; a++) {
833                         calc_latt_deform(laOb, vertexCos[a], influence);
834                 }
835         }
836         end_latt_deform(laOb);
837 }
838
839 int object_deform_mball(Object *ob, ListBase *dispbase)
840 {
841         if (ob->parent && ob->parent->type==OB_LATTICE && ob->partype==PARSKEL) {
842                 DispList *dl;
843
844                 for (dl=dispbase->first; dl; dl=dl->next) {
845                         lattice_deform_verts(ob->parent, ob, NULL,
846                                                                  (float(*)[3]) dl->verts, dl->nr, NULL, 1.0f);
847                 }
848
849                 return 1;
850         }
851         else {
852                 return 0;
853         }
854 }
855
856 static BPoint *latt_bp(Lattice *lt, int u, int v, int w)
857 {
858         return &lt->def[LT_INDEX(lt, u, v, w)];
859 }
860
861 void outside_lattice(Lattice *lt)
862 {
863         BPoint *bp, *bp1, *bp2;
864         int u, v, w;
865         float fac1, du=0.0, dv=0.0, dw=0.0;
866
867         if (lt->flag & LT_OUTSIDE) {
868                 bp= lt->def;
869
870                 if (lt->pntsu>1) du= 1.0f/((float)lt->pntsu-1);
871                 if (lt->pntsv>1) dv= 1.0f/((float)lt->pntsv-1);
872                 if (lt->pntsw>1) dw= 1.0f/((float)lt->pntsw-1);
873                         
874                 for (w=0; w<lt->pntsw; w++) {
875                         
876                         for (v=0; v<lt->pntsv; v++) {
877                         
878                                 for (u=0; u<lt->pntsu; u++, bp++) {
879                                         if (u==0 || v==0 || w==0 || u==lt->pntsu-1 || v==lt->pntsv-1 || w==lt->pntsw-1);
880                                         else {
881                                         
882                                                 bp->hide= 1;
883                                                 bp->f1 &= ~SELECT;
884                                                 
885                                                 /* u extrema */
886                                                 bp1= latt_bp(lt, 0, v, w);
887                                                 bp2= latt_bp(lt, lt->pntsu-1, v, w);
888                                                 
889                                                 fac1= du*u;
890                                                 bp->vec[0]= (1.0f-fac1)*bp1->vec[0] + fac1*bp2->vec[0];
891                                                 bp->vec[1]= (1.0f-fac1)*bp1->vec[1] + fac1*bp2->vec[1];
892                                                 bp->vec[2]= (1.0f-fac1)*bp1->vec[2] + fac1*bp2->vec[2];
893                                                 
894                                                 /* v extrema */
895                                                 bp1= latt_bp(lt, u, 0, w);
896                                                 bp2= latt_bp(lt, u, lt->pntsv-1, w);
897                                                 
898                                                 fac1= dv*v;
899                                                 bp->vec[0]+= (1.0f-fac1)*bp1->vec[0] + fac1*bp2->vec[0];
900                                                 bp->vec[1]+= (1.0f-fac1)*bp1->vec[1] + fac1*bp2->vec[1];
901                                                 bp->vec[2]+= (1.0f-fac1)*bp1->vec[2] + fac1*bp2->vec[2];
902                                                 
903                                                 /* w extrema */
904                                                 bp1= latt_bp(lt, u, v, 0);
905                                                 bp2= latt_bp(lt, u, v, lt->pntsw-1);
906                                                 
907                                                 fac1= dw*w;
908                                                 bp->vec[0]+= (1.0f-fac1)*bp1->vec[0] + fac1*bp2->vec[0];
909                                                 bp->vec[1]+= (1.0f-fac1)*bp1->vec[1] + fac1*bp2->vec[1];
910                                                 bp->vec[2]+= (1.0f-fac1)*bp1->vec[2] + fac1*bp2->vec[2];
911                                                 
912                                                 mul_v3_fl(bp->vec, 0.3333333f);
913                                                 
914                                         }
915                                 }
916                                 
917                         }
918                         
919                 }
920         }
921         else {
922                 bp= lt->def;
923
924                 for (w=0; w<lt->pntsw; w++)
925                         for (v=0; v<lt->pntsv; v++)
926                                 for (u=0; u<lt->pntsu; u++, bp++)
927                                         bp->hide= 0;
928         }
929 }
930
931 float (*lattice_getVertexCos(struct Object *ob, int *numVerts_r))[3]
932 {
933         Lattice *lt = ob->data;
934         int i, numVerts;
935         float (*vertexCos)[3];
936
937         if (lt->editlatt) lt= lt->editlatt->latt;
938         numVerts = *numVerts_r = lt->pntsu*lt->pntsv*lt->pntsw;
939         
940         vertexCos = MEM_mallocN(sizeof(*vertexCos)*numVerts,"lt_vcos");
941         
942         for (i=0; i<numVerts; i++) {
943                 copy_v3_v3(vertexCos[i], lt->def[i].vec);
944         }
945
946         return vertexCos;
947 }
948
949 void lattice_applyVertexCos(struct Object *ob, float (*vertexCos)[3])
950 {
951         Lattice *lt = ob->data;
952         int i, numVerts = lt->pntsu*lt->pntsv*lt->pntsw;
953
954         for (i=0; i<numVerts; i++) {
955                 copy_v3_v3(lt->def[i].vec, vertexCos[i]);
956         }
957 }
958
959 void lattice_calc_modifiers(Scene *scene, Object *ob)
960 {
961         Lattice *lt= ob->data;
962         ModifierData *md = modifiers_getVirtualModifierList(ob);
963         float (*vertexCos)[3] = NULL;
964         int numVerts, editmode = (lt->editlatt!=NULL);
965
966         freedisplist(&ob->disp);
967
968         for (; md; md=md->next) {
969                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
970
971                 md->scene= scene;
972                 
973                 if (!(md->mode&eModifierMode_Realtime)) continue;
974                 if (editmode && !(md->mode&eModifierMode_Editmode)) continue;
975                 if (mti->isDisabled && mti->isDisabled(md, 0)) continue;
976                 if (mti->type!=eModifierTypeType_OnlyDeform) continue;
977
978                 if (!vertexCos) vertexCos = lattice_getVertexCos(ob, &numVerts);
979                 mti->deformVerts(md, ob, NULL, vertexCos, numVerts, 0, 0);
980         }
981
982         /* always displist to make this work like derivedmesh */
983         if (!vertexCos) vertexCos = lattice_getVertexCos(ob, &numVerts);
984         
985         {
986                 DispList *dl = MEM_callocN(sizeof(*dl), "lt_dl");
987                 dl->type = DL_VERTS;
988                 dl->parts = 1;
989                 dl->nr = numVerts;
990                 dl->verts = (float*) vertexCos;
991                 
992                 BLI_addtail(&ob->disp, dl);
993         }
994 }
995
996 struct MDeformVert* lattice_get_deform_verts(struct Object *oblatt)
997 {
998         Lattice *lt = (Lattice*)oblatt->data;
999         BLI_assert(oblatt->type == OB_LATTICE);
1000         if (lt->editlatt) lt= lt->editlatt->latt;
1001         return lt->dvert;
1002 }