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