- Action Modifier; paths now support Speed curve.
[blender.git] / source / blender / blenkernel / intern / lattice.c
1 /**
2  * lattice.c
3  *
4  *
5  * $Id$
6  *
7  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version. The Blender
13  * Foundation also sells licenses for use in proprietary software under
14  * the Blender License.  See http://www.blender.org/BL/ for information
15  * about this.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software Foundation,
24  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  *
26  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
27  * All rights reserved.
28  *
29  * The Original Code is: all of this file.
30  *
31  * Contributor(s): none yet.
32  *
33  * ***** END GPL/BL DUAL LICENSE BLOCK *****
34  */
35
36 #include <stdio.h>
37 #include <string.h>
38 #include <math.h>
39 #include <stdlib.h>
40
41 #include "MEM_guardedalloc.h"
42
43 #include "BLI_blenlib.h"
44 #include "BLI_arithb.h"
45
46 #include "DNA_armature_types.h"
47 #include "DNA_ipo_types.h"
48 #include "DNA_mesh_types.h"
49 #include "DNA_meshdata_types.h"
50 #include "DNA_modifier_types.h"
51 #include "DNA_object_types.h"
52 #include "DNA_scene_types.h"
53 #include "DNA_lattice_types.h"
54 #include "DNA_curve_types.h"
55 #include "DNA_key_types.h"
56
57 #include "BKE_anim.h"
58 #include "BKE_armature.h"
59 #include "BKE_curve.h"
60 #include "BKE_cdderivedmesh.h"
61 #include "BKE_DerivedMesh.h"
62 #include "BKE_deform.h"
63 #include "BKE_displist.h"
64 #include "BKE_global.h"
65 #include "BKE_ipo.h"
66 #include "BKE_key.h"
67 #include "BKE_lattice.h"
68 #include "BKE_library.h"
69 #include "BKE_main.h"
70 #include "BKE_mesh.h"
71 #include "BKE_modifier.h"
72 #include "BKE_object.h"
73 #include "BKE_screen.h"
74 #include "BKE_utildefines.h"
75
76 #include "BIF_editdeform.h"
77
78 #ifdef HAVE_CONFIG_H
79 #include <config.h>
80 #endif
81
82 #include "blendef.h"
83
84 Lattice *editLatt=0;
85 static Lattice *deformLatt=0;
86
87 static float *latticedata=0, latmat[4][4];
88
89 void calc_lat_fudu(int flag, int res, float *fu, float *du)
90 {
91         if(res==1) {
92                 *fu= 0.0;
93                 *du= 0.0;
94         }
95         else if(flag & LT_GRID) {
96                 *fu= -0.5f*(res-1);
97                 *du= 1.0f;
98         }
99         else {
100                 *fu= -1.0f;
101                 *du= 2.0f/(res-1);
102         }
103 }
104
105 void resizelattice(Lattice *lt, int uNew, int vNew, int wNew, Object *ltOb)
106 {
107         BPoint *bp;
108         int i, u, v, w;
109         float fu, fv, fw, uc, vc, wc, du=0.0, dv=0.0, dw=0.0;
110         float *co, (*vertexCos)[3] = NULL;
111         
112         /* vertex weight groups are just freed all for now */
113         if(lt->dvert) {
114                 free_dverts(lt->dvert, lt->pntsu*lt->pntsv*lt->pntsw);
115                 lt->dvert= NULL;
116         }
117         
118         while(uNew*vNew*wNew > 32000) {
119                 if( uNew>=vNew && uNew>=wNew) uNew--;
120                 else if( vNew>=uNew && vNew>=wNew) vNew--;
121                 else wNew--;
122         }
123
124         vertexCos = MEM_mallocN(sizeof(*vertexCos)*uNew*vNew*wNew, "tmp_vcos");
125
126         calc_lat_fudu(lt->flag, uNew, &fu, &du);
127         calc_lat_fudu(lt->flag, vNew, &fv, &dv);
128         calc_lat_fudu(lt->flag, wNew, &fw, &dw);
129
130                 /* If old size is different then resolution changed in interface,
131                  * try to do clever reinit of points. Pretty simply idea, we just
132                  * deform new verts by old lattice, but scaling them to match old
133                  * size first.
134                  */
135         if (ltOb) {
136                 if (uNew!=1 && lt->pntsu!=1) {
137                         fu = lt->fu;
138                         du = (lt->pntsu-1)*lt->du/(uNew-1);
139                 }
140
141                 if (vNew!=1 && lt->pntsv!=1) {
142                         fv = lt->fv;
143                         dv = (lt->pntsv-1)*lt->dv/(vNew-1);
144                 }
145
146                 if (wNew!=1 && lt->pntsw!=1) {
147                         fw = lt->fw;
148                         dw = (lt->pntsw-1)*lt->dw/(wNew-1);
149                 }
150         }
151
152         co = vertexCos[0];
153         for(w=0,wc=fw; w<wNew; w++,wc+=dw) {
154                 for(v=0,vc=fv; v<vNew; v++,vc+=dv) {
155                         for(u=0,uc=fu; u<uNew; u++,co+=3,uc+=du) {
156                                 co[0] = uc;
157                                 co[1] = vc;
158                                 co[2] = wc;
159                         }
160                 }
161         }
162         
163         if (ltOb) {
164                 float mat[4][4];
165                 int typeu = lt->typeu, typev = lt->typev, typew = lt->typew;
166
167                         /* works best if we force to linear type (endpoints match) */
168                 lt->typeu = lt->typev = lt->typew = KEY_LINEAR;
169
170                         /* prevent using deformed locations */
171                 freedisplist(&ltOb->disp);
172
173                 Mat4CpyMat4(mat, ltOb->obmat);
174                 Mat4One(ltOb->obmat);
175                 lattice_deform_verts(ltOb, NULL, NULL, vertexCos, uNew*vNew*wNew, NULL);
176                 Mat4CpyMat4(ltOb->obmat, mat);
177
178                 lt->typeu = typeu;
179                 lt->typev = typev;
180                 lt->typew = typew;
181         }
182
183         lt->fu = fu;
184         lt->fv = fv;
185         lt->fw = fw;
186         lt->du = du;
187         lt->dv = dv;
188         lt->dw = dw;
189
190         lt->pntsu = uNew;
191         lt->pntsv = vNew;
192         lt->pntsw = wNew;
193
194         MEM_freeN(lt->def);
195         lt->def= MEM_callocN(lt->pntsu*lt->pntsv*lt->pntsw*sizeof(BPoint), "lattice bp");
196         
197         bp= lt->def;
198         
199         for (i=0; i<lt->pntsu*lt->pntsv*lt->pntsw; i++,bp++) {
200                 VECCOPY(bp->vec, vertexCos[i]);
201         }
202
203         MEM_freeN(vertexCos);
204 }
205
206 Lattice *add_lattice()
207 {
208         Lattice *lt;
209         
210         lt= alloc_libblock(&G.main->latt, ID_LT, "Lattice");
211         
212         lt->flag= LT_GRID;
213         
214         lt->typeu= lt->typev= lt->typew= KEY_BSPLINE;
215         
216         lt->def= MEM_callocN(sizeof(BPoint), "lattvert"); /* temporary */
217         resizelattice(lt, 2, 2, 2, NULL);       /* creates a uniform lattice */
218                 
219         return lt;
220 }
221
222 Lattice *copy_lattice(Lattice *lt)
223 {
224         Lattice *ltn;
225
226         ltn= copy_libblock(lt);
227         ltn->def= MEM_dupallocN(lt->def);
228                 
229         id_us_plus((ID *)ltn->ipo);
230
231         ltn->key= copy_key(ltn->key);
232         if(ltn->key) ltn->key->from= (ID *)ltn;
233         
234         if(lt->dvert) {
235                 int tot= lt->pntsu*lt->pntsv*lt->pntsw;
236                 ltn->dvert = MEM_mallocN (sizeof (MDeformVert)*tot, "Lattice MDeformVert");
237                 copy_dverts(ltn->dvert, lt->dvert, tot);
238         }
239         
240         return ltn;
241 }
242
243 void free_lattice(Lattice *lt)
244 {
245         if(lt->def) MEM_freeN(lt->def);
246         if(lt->dvert) free_dverts(lt->dvert, lt->pntsu*lt->pntsv*lt->pntsw);
247 }
248
249
250 void make_local_lattice(Lattice *lt)
251 {
252         Object *ob;
253         Lattice *ltn;
254         int local=0, lib=0;
255
256         /* - only lib users: do nothing
257          * - only local users: set flag
258          * - mixed: make copy
259          */
260         
261         if(lt->id.lib==0) return;
262         if(lt->id.us==1) {
263                 lt->id.lib= 0;
264                 lt->id.flag= LIB_LOCAL;
265                 new_id(0, (ID *)lt, 0);
266                 return;
267         }
268         
269         ob= G.main->object.first;
270         while(ob) {
271                 if(ob->data==lt) {
272                         if(ob->id.lib) lib= 1;
273                         else local= 1;
274                 }
275                 ob= ob->id.next;
276         }
277         
278         if(local && lib==0) {
279                 lt->id.lib= 0;
280                 lt->id.flag= LIB_LOCAL;
281                 new_id(0, (ID *)lt, 0);
282         }
283         else if(local && lib) {
284                 ltn= copy_lattice(lt);
285                 ltn->id.us= 0;
286                 
287                 ob= G.main->object.first;
288                 while(ob) {
289                         if(ob->data==lt) {
290                                 
291                                 if(ob->id.lib==0) {
292                                         ob->data= ltn;
293                                         ltn->id.us++;
294                                         lt->id.us--;
295                                 }
296                         }
297                         ob= ob->id.next;
298                 }
299         }
300 }
301
302 void init_latt_deform(Object *oblatt, Object *ob)
303 {
304                 /* we make an array with all differences */
305         Lattice *lt = deformLatt = (oblatt==G.obedit)?editLatt:oblatt->data;
306         BPoint *bp = lt->def;
307         DispList *dl = find_displist(&oblatt->disp, DL_VERTS);
308         float *co = dl?dl->verts:NULL;
309         float *fp, imat[4][4];
310         float fu, fv, fw;
311         int u, v, w;
312
313         fp= latticedata= MEM_mallocN(sizeof(float)*3*deformLatt->pntsu*deformLatt->pntsv*deformLatt->pntsw, "latticedata");
314         
315                 /* for example with a particle system: ob==0 */
316         if(ob==0) {
317                 /* in deformspace, calc matrix  */
318                 Mat4Invert(latmat, oblatt->obmat);
319         
320                 /* back: put in deform array */
321                 Mat4Invert(imat, latmat);
322         }
323         else {
324                 /* in deformspace, calc matrix */
325                 Mat4Invert(imat, oblatt->obmat);
326                 Mat4MulMat4(latmat, ob->obmat, imat);
327         
328                 /* back: put in deform array */
329                 Mat4Invert(imat, latmat);
330         }
331         
332         for(w=0,fw=lt->fw; w<lt->pntsw; w++,fw+=lt->dw) {
333                 for(v=0,fv=lt->fv; v<lt->pntsv; v++, fv+=lt->dv) {
334                         for(u=0,fu=lt->fu; u<lt->pntsu; u++, bp++, co+=3, fp+=3, fu+=lt->du) {
335                                 if (dl) {
336                                         fp[0] = co[0] - fu;
337                                         fp[1] = co[1] - fv;
338                                         fp[2] = co[2] - fw;
339                                 } else {
340                                         fp[0] = bp->vec[0] - fu;
341                                         fp[1] = bp->vec[1] - fv;
342                                         fp[2] = bp->vec[2] - fw;
343                                 }
344
345                                 Mat4Mul3Vecfl(imat, fp);
346                         }
347                 }
348         }
349 }
350
351 void calc_latt_deform(float *co, float weight)
352 {
353         Lattice *lt;
354         float u, v, w, tu[4], tv[4], tw[4];
355         float *fpw, *fpv, *fpu, vec[3];
356         int ui, vi, wi, uu, vv, ww;
357         
358         if(latticedata==0) return;
359         
360         lt= deformLatt; /* just for shorter notation! */
361         
362         /* co is in local coords, treat with latmat */
363         
364         VECCOPY(vec, co);
365         Mat4MulVecfl(latmat, vec);
366         
367         /* u v w coords */
368         
369         if(lt->pntsu>1) {
370                 u= (vec[0]-lt->fu)/lt->du;
371                 ui= (int)floor(u);
372                 u -= ui;
373                 set_four_ipo(u, tu, lt->typeu);
374         }
375         else {
376                 tu[0]= tu[2]= tu[3]= 0.0; tu[1]= 1.0;
377                 ui= 0;
378         }
379         
380         if(lt->pntsv>1) {
381                 v= (vec[1]-lt->fv)/lt->dv;
382                 vi= (int)floor(v);
383                 v -= vi;
384                 set_four_ipo(v, tv, lt->typev);
385         }
386         else {
387                 tv[0]= tv[2]= tv[3]= 0.0; tv[1]= 1.0;
388                 vi= 0;
389         }
390         
391         if(lt->pntsw>1) {
392                 w= (vec[2]-lt->fw)/lt->dw;
393                 wi= (int)floor(w);
394                 w -= wi;
395                 set_four_ipo(w, tw, lt->typew);
396         }
397         else {
398                 tw[0]= tw[2]= tw[3]= 0.0; tw[1]= 1.0;
399                 wi= 0;
400         }
401         
402         for(ww= wi-1; ww<=wi+2; ww++) {
403                 w= tw[ww-wi+1];
404                 
405                 if(w!=0.0) {
406                         if(ww>0) {
407                                 if(ww<lt->pntsw) fpw= latticedata + 3*ww*lt->pntsu*lt->pntsv;
408                                 else fpw= latticedata + 3*(lt->pntsw-1)*lt->pntsu*lt->pntsv;
409                         }
410                         else fpw= latticedata;
411                         
412                         for(vv= vi-1; vv<=vi+2; vv++) {
413                                 v= w*tv[vv-vi+1];
414                                 
415                                 if(v!=0.0) {
416                                         if(vv>0) {
417                                                 if(vv<lt->pntsv) fpv= fpw + 3*vv*lt->pntsu;
418                                                 else fpv= fpw + 3*(lt->pntsv-1)*lt->pntsu;
419                                         }
420                                         else fpv= fpw;
421                                         
422                                         for(uu= ui-1; uu<=ui+2; uu++) {
423                                                 u= weight*v*tu[uu-ui+1];
424                                                 
425                                                 if(u!=0.0) {
426                                                         if(uu>0) {
427                                                                 if(uu<lt->pntsu) fpu= fpv + 3*uu;
428                                                                 else fpu= fpv + 3*(lt->pntsu-1);
429                                                         }
430                                                         else fpu= fpv;
431                                                         
432                                                         co[0]+= u*fpu[0];
433                                                         co[1]+= u*fpu[1];
434                                                         co[2]+= u*fpu[2];
435                                                 }
436                                         }
437                                 }
438                         }
439                 }
440         }
441 }
442
443 void end_latt_deform()
444 {
445
446         MEM_freeN(latticedata);
447         latticedata= 0;
448 }
449
450         /* calculations is in local space of deformed object
451            so we store in latmat transform from path coord inside object 
452          */
453 typedef struct {
454         float dmin[3], dmax[3], dsize, dloc[3];
455         float curvespace[4][4], objectspace[4][4], objectspace3[3][3];
456         int no_rot_axis;
457 } CurveDeform;
458
459 static void init_curve_deform(Object *par, Object *ob, CurveDeform *cd, int dloc)
460 {
461         Mat4Invert(ob->imat, ob->obmat);
462         Mat4MulMat4(cd->objectspace, par->obmat, ob->imat);
463         Mat4Invert(cd->curvespace, cd->objectspace);
464         Mat3CpyMat4(cd->objectspace3, cd->objectspace);
465         
466         // offset vector for 'no smear'
467         if(dloc) {
468                 Mat4Invert(par->imat, par->obmat);
469                 VecMat4MulVecfl(cd->dloc, par->imat, ob->obmat[3]);
470         }
471         else cd->dloc[0]=cd->dloc[1]=cd->dloc[2]= 0.0f;
472         
473         cd->no_rot_axis= 0;
474 }
475
476 /* this makes sure we can extend for non-cyclic. *vec needs 4 items! */
477 static int where_on_path_deform(Object *ob, float ctime, float *vec, float *dir)        /* returns OK */
478 {
479         Curve *cu= ob->data;
480         BevList *bl;
481         float ctime1;
482         int cycl=0;
483         
484         /* test for cyclic */
485         bl= cu->bev.first;
486         if(bl && bl->poly> -1) cycl= 1;
487
488         if(cycl==0) {
489                 ctime1= CLAMPIS(ctime, 0.0, 1.0);
490         }
491         else ctime1= ctime;
492         
493         /* vec needs 4 items */
494         if(where_on_path(ob, ctime1, vec, dir)) {
495                 
496                 if(cycl==0) {
497                         Path *path= cu->path;
498                         float dvec[3];
499                         
500                         if(ctime < 0.0) {
501                                 VecSubf(dvec, path->data+4, path->data);
502                                 VecMulf(dvec, ctime*(float)path->len);
503                                 VECADD(vec, vec, dvec);
504                         }
505                         else if(ctime > 1.0) {
506                                 VecSubf(dvec, path->data+4*path->len-4, path->data+4*path->len-8);
507                                 VecMulf(dvec, (ctime-1.0)*(float)path->len);
508                                 VECADD(vec, vec, dvec);
509                         }
510                 }
511                 return 1;
512         }
513         return 0;
514 }
515
516         /* for each point, rotate & translate to curve */
517         /* use path, since it has constant distances */
518         /* co: local coord, result local too */
519         /* returns quaternion for rotation, using cd->no_rot_axis */
520         /* axis is using another define!!! */
521 static float *calc_curve_deform(Object *par, float *co, short axis, CurveDeform *cd)
522 {
523         Curve *cu= par->data;
524         float fac, loc[4], dir[3], cent[3];
525         short upflag, index;
526         
527         if(axis==MOD_CURVE_POSX || axis==MOD_CURVE_NEGX) {
528                 upflag= OB_POSZ;
529                 cent[0]= 0.0;
530                 cent[1]= co[1];
531                 cent[2]= co[2];
532                 index= 0;
533         }
534         else if(axis==MOD_CURVE_POSY || axis==MOD_CURVE_NEGY) {
535                 upflag= OB_POSZ;
536                 cent[0]= co[0];
537                 cent[1]= 0.0;
538                 cent[2]= co[2];
539                 index= 1;
540         }
541         else {
542                 upflag= OB_POSY;
543                 cent[0]= co[0];
544                 cent[1]= co[1];
545                 cent[2]= 0.0;
546                 index= 2;
547         }
548         /* to be sure, mostly after file load */
549         if(cu->path==NULL) {
550                 makeDispListCurveTypes(par, 0);
551                 if(cu->path==NULL) return NULL; // happens on append...
552         }
553         
554         /* options */
555         if(ELEM3(axis, OB_NEGX, OB_NEGY, OB_NEGZ)) {
556                 if(cu->flag & CU_STRETCH)
557                         fac= (-co[index]-cd->dmax[index])/(cd->dmax[index] - cd->dmin[index]);
558                 else
559                         fac= (cd->dloc[index])/(cu->path->totdist) - (co[index]-cd->dmax[index])/(cu->path->totdist);
560         }
561         else {
562                 if(cu->flag & CU_STRETCH)
563                         fac= (co[index]-cd->dmin[index])/(cd->dmax[index] - cd->dmin[index]);
564                 else
565                         fac= (cd->dloc[index])/(cu->path->totdist) + (co[index]-cd->dmin[index])/(cu->path->totdist);
566         }
567         
568         /* we want the ipo to work on the default 100 frame range, because there's no  
569            actual time involved in path position */
570         if(cu->ipo) {
571                 fac*= 100.0f;
572                 if(calc_ipo_spec(cu->ipo, CU_SPEED, &fac)==0)
573                         fac/= 100.0;
574         }
575         
576         if( where_on_path_deform(par, fac, loc, dir)) { /* returns OK */
577                 float q[4], mat[3][3];
578                 float *quat;
579                 
580                 if(cd->no_rot_axis)     /* set by caller */
581                         dir[cd->no_rot_axis-1]= 0.0f;
582                 
583                 /* -1 for compatibility with old track defines */
584                 quat= vectoquat(dir, axis-1, upflag);   /* gives static quat */
585                 
586                 /* the tilt */
587                 if(loc[3]!=0.0) {
588                         Normalise(dir);
589                         q[0]= (float)cos(0.5*loc[3]);
590                         fac= (float)sin(0.5*loc[3]);
591                         q[1]= -fac*dir[0];
592                         q[2]= -fac*dir[1];
593                         q[3]= -fac*dir[2];
594                         QuatMul(quat, q, quat);
595                 }               
596                 QuatToMat3(quat, mat);
597         
598                 /* local rotation */
599                 Mat3MulVecfl(mat, cent);
600                 
601                 /* translation */
602                 VECADD(co, cent, loc);
603                 
604                 return quat;
605         }
606         return NULL;
607 }
608
609 void curve_deform_verts(Object *cuOb, Object *target, DerivedMesh *dm, float (*vertexCos)[3], int numVerts, char *vgroup, short defaxis)
610 {
611         Curve *cu = cuOb->data;
612         int a, flag = cu->flag;
613         CurveDeform cd;
614         int use_vgroups;
615         
616         cu->flag |= (CU_PATH|CU_FOLLOW); // needed for path & bevlist
617
618         init_curve_deform(cuOb, target, &cd, (cu->flag & CU_STRETCH)==0);
619                 
620         /* check whether to use vertex groups (only possible if target is a Mesh)
621          * we want either a Mesh with no derived data, or derived data with
622          * deformverts
623          */
624         if(target && target->type==OB_MESH) {
625                 /* if there's derived data without deformverts, don't use vgroups */
626                 if(dm && !dm->getVertData(dm, 0, LAYERTYPE_MDEFORMVERT))
627                         use_vgroups = 0;
628                 else
629                         use_vgroups = 1;
630         } else
631                 use_vgroups = 0;
632         
633         if(vgroup && vgroup[0] && use_vgroups) {
634                 bDeformGroup *curdef;
635                 Mesh *me= target->data;
636                 int index;
637                 
638                 /* find the group (weak loop-in-loop) */
639                 for(index = 0, curdef = target->defbase.first; curdef;
640                     curdef = curdef->next, index++)
641                         if (!strcmp(curdef->name, vgroup))
642                                 break;
643
644                 if(curdef && (me->dvert || dm)) {
645                         MDeformVert *dvert = me->dvert;
646                         float vec[3];
647                         int j;
648
649                         INIT_MINMAX(cd.dmin, cd.dmax);
650
651                         for(a = 0; a < numVerts; a++, dvert++) {
652                                 if(dm) dvert = dm->getVertData(dm, a, LAYERTYPE_MDEFORMVERT);
653
654                                 for(j = 0; j < dvert->totweight; j++) {
655                                         if(dvert->dw[j].def_nr == index) {
656                                                 Mat4MulVecfl(cd.curvespace, vertexCos[a]);
657                                                 DO_MINMAX(vertexCos[a], cd.dmin, cd.dmax);
658                                                 break;
659                                         }
660                                 }
661                         }
662
663                         dvert = me->dvert;
664                         for(a = 0; a < numVerts; a++, dvert++) {
665                                 if(dm) dvert = dm->getVertData(dm, a, LAYERTYPE_MDEFORMVERT);
666
667                                 for(j = 0; j < dvert->totweight; j++) {
668                                         if(dvert->dw[j].def_nr == index) {
669                                                 VECCOPY(vec, vertexCos[a]);
670                                                 calc_curve_deform(cuOb, vec, defaxis, &cd);
671                                                 VecLerpf(vertexCos[a], vertexCos[a], vec,
672                                                          dvert->dw[j].weight);
673                                                 Mat4MulVecfl(cd.objectspace, vertexCos[a]);
674                                                 break;
675                                         }
676                                 }
677                         }
678                 }
679         } else {
680                 INIT_MINMAX(cd.dmin, cd.dmax);
681                         
682                 for(a = 0; a < numVerts; a++) {
683                         Mat4MulVecfl(cd.curvespace, vertexCos[a]);
684                         DO_MINMAX(vertexCos[a], cd.dmin, cd.dmax);
685                 }
686
687                 for(a = 0; a < numVerts; a++) {
688                         calc_curve_deform(cuOb, vertexCos[a], defaxis, &cd);
689                         Mat4MulVecfl(cd.objectspace, vertexCos[a]);
690                 }
691         }
692         cu->flag = flag;
693 }
694
695 /* input vec and orco = local coord in armature space */
696 /* orco is original not-animated or deformed reference point */
697 /* result written in vec and mat */
698 void curve_deform_vector(Object *cuOb, Object *target, float *orco, float *vec, float mat[][3], int no_rot_axis)
699 {
700         CurveDeform cd;
701         float *quat;
702         
703         init_curve_deform(cuOb, target, &cd, 0);        /* 0 no dloc */
704         cd.no_rot_axis= no_rot_axis;                            /* option to only rotate for XY, for example */
705         
706         VECCOPY(cd.dmin, orco);
707         VECCOPY(cd.dmax, orco);
708
709         Mat4MulVecfl(cd.curvespace, vec);
710         
711         quat= calc_curve_deform(cuOb, vec, target->trackflag+1, &cd);
712         if(quat) {
713                 float qmat[3][3];
714                 
715                 QuatToMat3(quat, qmat);
716                 Mat3MulMat3(mat, qmat, cd.objectspace3);
717         }
718         else
719                 Mat3One(mat);
720         
721         Mat4MulVecfl(cd.objectspace, vec);
722
723 }
724
725 void lattice_deform_verts(Object *laOb, Object *target, DerivedMesh *dm,
726                           float (*vertexCos)[3], int numVerts, char *vgroup)
727 {
728         int a;
729         int use_vgroups;
730
731         init_latt_deform(laOb, target);
732
733         /* check whether to use vertex groups (only possible if target is a Mesh)
734          * we want either a Mesh with no derived data, or derived data with
735          * deformverts
736          */
737         if(target && target->type==OB_MESH) {
738                 /* if there's derived data without deformverts, don't use vgroups */
739                 if(dm && !dm->getVertData(dm, 0, LAYERTYPE_MDEFORMVERT))
740                         use_vgroups = 0;
741                 else
742                         use_vgroups = 1;
743         } else
744                 use_vgroups = 0;
745         
746         if(vgroup && vgroup[0] && use_vgroups) {
747                 bDeformGroup *curdef;
748                 Mesh *me = target->data;
749                 int index = 0;
750                 
751                 /* find the group (weak loop-in-loop) */
752                 for(curdef = target->defbase.first; curdef;
753                     curdef = curdef->next, index++)
754                         if(!strcmp(curdef->name, vgroup)) break;
755
756                 if(curdef && (me->dvert || dm)) {
757                         MDeformVert *dvert = me->dvert;
758                         int j;
759                         
760                         for(a = 0; a < numVerts; a++, dvert++) {
761                                 if(dm) dvert = dm->getVertData(dm, a, LAYERTYPE_MDEFORMVERT);
762                                 for(j = 0; j < dvert->totweight; j++) {
763                                         if (dvert->dw[j].def_nr == index) {
764                                                 calc_latt_deform(vertexCos[a], dvert->dw[j].weight);
765                                         }
766                                 }
767                         }
768                 }
769         } else {
770                 for(a = 0; a < numVerts; a++) {
771                         calc_latt_deform(vertexCos[a], 1.0f);
772                 }
773         }
774         end_latt_deform();
775 }
776
777 int object_deform_mball(Object *ob)
778 {
779         if(ob->parent && ob->parent->type==OB_LATTICE && ob->partype==PARSKEL) {
780                 DispList *dl;
781
782                 for (dl=ob->disp.first; dl; dl=dl->next) {
783                         lattice_deform_verts(ob->parent, ob, NULL,
784                                              (float(*)[3]) dl->verts, dl->nr, NULL);
785                 }
786
787                 return 1;
788         } else {
789                 return 0;
790         }
791 }
792
793 static BPoint *latt_bp(Lattice *lt, int u, int v, int w)
794 {
795         return lt->def+ u + v*lt->pntsu + w*lt->pntsu*lt->pntsv;
796 }
797
798 void outside_lattice(Lattice *lt)
799 {
800         BPoint *bp, *bp1, *bp2;
801         int u, v, w;
802         float fac1, du=0.0, dv=0.0, dw=0.0;
803
804         bp= lt->def;
805
806         if(lt->pntsu>1) du= 1.0f/((float)lt->pntsu-1);
807         if(lt->pntsv>1) dv= 1.0f/((float)lt->pntsv-1);
808         if(lt->pntsw>1) dw= 1.0f/((float)lt->pntsw-1);
809                 
810         for(w=0; w<lt->pntsw; w++) {
811                 
812                 for(v=0; v<lt->pntsv; v++) {
813                 
814                         for(u=0; u<lt->pntsu; u++, bp++) {
815                                 if(u==0 || v==0 || w==0 || u==lt->pntsu-1 || v==lt->pntsv-1 || w==lt->pntsw-1);
816                                 else {
817                                 
818                                         bp->hide= 1;
819                                         bp->f1 &= ~SELECT;
820                                         
821                                         /* u extrema */
822                                         bp1= latt_bp(lt, 0, v, w);
823                                         bp2= latt_bp(lt, lt->pntsu-1, v, w);
824                                         
825                                         fac1= du*u;
826                                         bp->vec[0]= (1.0f-fac1)*bp1->vec[0] + fac1*bp2->vec[0];
827                                         bp->vec[1]= (1.0f-fac1)*bp1->vec[1] + fac1*bp2->vec[1];
828                                         bp->vec[2]= (1.0f-fac1)*bp1->vec[2] + fac1*bp2->vec[2];
829                                         
830                                         /* v extrema */
831                                         bp1= latt_bp(lt, u, 0, w);
832                                         bp2= latt_bp(lt, u, lt->pntsv-1, w);
833                                         
834                                         fac1= dv*v;
835                                         bp->vec[0]+= (1.0f-fac1)*bp1->vec[0] + fac1*bp2->vec[0];
836                                         bp->vec[1]+= (1.0f-fac1)*bp1->vec[1] + fac1*bp2->vec[1];
837                                         bp->vec[2]+= (1.0f-fac1)*bp1->vec[2] + fac1*bp2->vec[2];
838                                         
839                                         /* w extrema */
840                                         bp1= latt_bp(lt, u, v, 0);
841                                         bp2= latt_bp(lt, u, v, lt->pntsw-1);
842                                         
843                                         fac1= dw*w;
844                                         bp->vec[0]+= (1.0f-fac1)*bp1->vec[0] + fac1*bp2->vec[0];
845                                         bp->vec[1]+= (1.0f-fac1)*bp1->vec[1] + fac1*bp2->vec[1];
846                                         bp->vec[2]+= (1.0f-fac1)*bp1->vec[2] + fac1*bp2->vec[2];
847                                         
848                                         VecMulf(bp->vec, 0.3333333f);
849                                         
850                                 }
851                         }
852                         
853                 }
854                 
855         }
856         
857 }
858
859 float (*lattice_getVertexCos(struct Object *ob, int *numVerts_r))[3]
860 {
861         Lattice *lt = (G.obedit==ob)?editLatt:ob->data;
862         int i, numVerts = *numVerts_r = lt->pntsu*lt->pntsv*lt->pntsw;
863         float (*vertexCos)[3] = MEM_mallocN(sizeof(*vertexCos)*numVerts,"lt_vcos");
864
865         for (i=0; i<numVerts; i++) {
866                 VECCOPY(vertexCos[i], lt->def[i].vec);
867         }
868
869         return vertexCos;
870 }
871
872 void lattice_applyVertexCos(struct Object *ob, float (*vertexCos)[3])
873 {
874         Lattice *lt = ob->data;
875         int i, numVerts = lt->pntsu*lt->pntsv*lt->pntsw;
876
877         for (i=0; i<numVerts; i++) {
878                 VECCOPY(lt->def[i].vec, vertexCos[i]);
879         }
880 }
881
882 void lattice_calc_modifiers(Object *ob)
883 {
884         float (*vertexCos)[3] = NULL;
885         ModifierData *md = modifiers_getVirtualModifierList(ob);
886         int numVerts, editmode = G.obedit==ob;
887
888         freedisplist(&ob->disp);
889
890         if (!editmode) {
891                 do_ob_key(ob);
892         }
893
894         for (; md; md=md->next) {
895                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
896
897                 if (!(md->mode&eModifierMode_Realtime)) continue;
898                 if (editmode && !(md->mode&eModifierMode_Editmode)) continue;
899                 if (mti->isDisabled && mti->isDisabled(md)) continue;
900                 if (mti->type!=eModifierTypeType_OnlyDeform) continue;
901
902                 if (!vertexCos) vertexCos = lattice_getVertexCos(ob, &numVerts);
903                 mti->deformVerts(md, ob, NULL, vertexCos, numVerts);
904         }
905
906         if (vertexCos) {
907                 DispList *dl = MEM_callocN(sizeof(*dl), "lt_dl");
908                 dl->type = DL_VERTS;
909                 dl->parts = 1;
910                 dl->nr = numVerts;
911                 dl->verts = (float*) vertexCos;
912                 
913                 BLI_addtail(&ob->disp, dl);
914         }
915 }