Curve and Lattice deform Modifiers now accept optional Vertex Group name
[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_mesh_types.h"
48 #include "DNA_meshdata_types.h"
49 #include "DNA_modifier_types.h"
50 #include "DNA_object_types.h"
51 #include "DNA_scene_types.h"
52 #include "DNA_lattice_types.h"
53 #include "DNA_curve_types.h"
54 #include "DNA_key_types.h"
55
56 #include "BKE_anim.h"
57 #include "BKE_armature.h"
58 #include "BKE_curve.h"
59 #include "BKE_deform.h"
60 #include "BKE_displist.h"
61 #include "BKE_global.h"
62 #include "BKE_key.h"
63 #include "BKE_lattice.h"
64 #include "BKE_library.h"
65 #include "BKE_main.h"
66 #include "BKE_modifier.h"
67 #include "BKE_object.h"
68 #include "BKE_screen.h"
69 #include "BKE_utildefines.h"
70
71 #include "BIF_editdeform.h"
72
73 #ifdef HAVE_CONFIG_H
74 #include <config.h>
75 #endif
76
77 #include "blendef.h"
78
79 Lattice *editLatt=0;
80 static Lattice *deformLatt=0;
81
82 static float *latticedata=0, latmat[4][4];
83
84 void calc_lat_fudu(int flag, int res, float *fu, float *du)
85 {
86         if(res==1) {
87                 *fu= 0.0;
88                 *du= 0.0;
89         }
90         else if(flag & LT_GRID) {
91                 *fu= -0.5f*(res-1);
92                 *du= 1.0f;
93         }
94         else {
95                 *fu= -1.0f;
96                 *du= 2.0f/(res-1);
97         }
98 }
99
100 void resizelattice(Lattice *lt, int uNew, int vNew, int wNew, Object *ltOb)
101 {
102         BPoint *bp;
103         int i, u, v, w;
104         float fu, fv, fw, uc, vc, wc, du=0.0, dv=0.0, dw=0.0;
105         float *co, (*vertexCos)[3] = NULL;
106         
107         while(uNew*vNew*wNew > 32000) {
108                 if( uNew>=vNew && uNew>=wNew) uNew--;
109                 else if( vNew>=uNew && vNew>=wNew) vNew--;
110                 else wNew--;
111         }
112
113         vertexCos = MEM_mallocN(sizeof(*vertexCos)*uNew*vNew*wNew, "tmp_vcos");
114
115         calc_lat_fudu(lt->flag, uNew, &fu, &du);
116         calc_lat_fudu(lt->flag, vNew, &fv, &dv);
117         calc_lat_fudu(lt->flag, wNew, &fw, &dw);
118
119                 /* If old size is different then resolution changed in interface,
120                  * try to do clever reinit of points. Pretty simply idea, we just
121                  * deform new verts by old lattice, but scaling them to match old
122                  * size first.
123                  */
124         if (ltOb) {
125                 if (uNew!=1 && lt->pntsu!=1) {
126                         fu = lt->fu;
127                         du = (lt->pntsu-1)*lt->du/(uNew-1);
128                 }
129
130                 if (vNew!=1 && lt->pntsv!=1) {
131                         fv = lt->fv;
132                         dv = (lt->pntsv-1)*lt->dv/(vNew-1);
133                 }
134
135                 if (wNew!=1 && lt->pntsw!=1) {
136                         fw = lt->fw;
137                         dw = (lt->pntsw-1)*lt->dw/(wNew-1);
138                 }
139         }
140
141         co = vertexCos[0];
142         for(w=0,wc=fw; w<wNew; w++,wc+=dw) {
143                 for(v=0,vc=fv; v<vNew; v++,vc+=dv) {
144                         for(u=0,uc=fu; u<uNew; u++,co+=3,uc+=du) {
145                                 co[0] = uc;
146                                 co[1] = vc;
147                                 co[2] = wc;
148                         }
149                 }
150         }
151         
152         if (ltOb) {
153                 float mat[4][4];
154                 int typeu = lt->typeu, typev = lt->typev, typew = lt->typew;
155
156                         /* works best if we force to linear type (endpoints match) */
157                 lt->typeu = lt->typev = lt->typew = KEY_LINEAR;
158
159                         /* prevent using deformed locations */
160                 freedisplist(&ltOb->disp);
161
162                 Mat4CpyMat4(mat, ltOb->obmat);
163                 Mat4One(ltOb->obmat);
164                 lattice_deform_verts(ltOb, NULL, vertexCos, uNew*vNew*wNew, NULL);
165                 Mat4CpyMat4(ltOb->obmat, mat);
166
167                 lt->typeu = typeu;
168                 lt->typev = typev;
169                 lt->typew = typew;
170         }
171
172         lt->fu = fu;
173         lt->fv = fv;
174         lt->fw = fw;
175         lt->du = du;
176         lt->dv = dv;
177         lt->dw = dw;
178
179         lt->pntsu = uNew;
180         lt->pntsv = vNew;
181         lt->pntsw = wNew;
182
183         MEM_freeN(lt->def);
184         lt->def= MEM_callocN(lt->pntsu*lt->pntsv*lt->pntsw*sizeof(BPoint), "lattice bp");
185         
186         bp= lt->def;
187         
188         for (i=0; i<lt->pntsu*lt->pntsv*lt->pntsw; i++,bp++) {
189                 VECCOPY(bp->vec, vertexCos[i]);
190         }
191
192         MEM_freeN(vertexCos);
193 }
194
195 Lattice *add_lattice()
196 {
197         Lattice *lt;
198         
199         lt= alloc_libblock(&G.main->latt, ID_LT, "Lattice");
200         
201         lt->flag= LT_GRID;
202         
203         lt->typeu= lt->typev= lt->typew= KEY_BSPLINE;
204         
205         lt->def= MEM_callocN(sizeof(BPoint), "lattvert"); /* temporary */
206         resizelattice(lt, 2, 2, 2, NULL);       /* creates a uniform lattice */
207                 
208         return lt;
209 }
210
211 Lattice *copy_lattice(Lattice *lt)
212 {
213         Lattice *ltn;
214
215         ltn= copy_libblock(lt);
216         ltn->def= MEM_dupallocN(lt->def);
217                 
218         id_us_plus((ID *)ltn->ipo);
219
220         ltn->key= copy_key(ltn->key);
221         if(ltn->key) ltn->key->from= (ID *)ltn;
222         
223         return ltn;
224 }
225
226 void free_lattice(Lattice *lt)
227 {
228         if(lt->def) MEM_freeN(lt->def);
229 }
230
231
232 void make_local_lattice(Lattice *lt)
233 {
234         Object *ob;
235         Lattice *ltn;
236         int local=0, lib=0;
237
238         /* - only lib users: do nothing
239          * - only local users: set flag
240          * - mixed: make copy
241          */
242         
243         if(lt->id.lib==0) return;
244         if(lt->id.us==1) {
245                 lt->id.lib= 0;
246                 lt->id.flag= LIB_LOCAL;
247                 new_id(0, (ID *)lt, 0);
248                 return;
249         }
250         
251         ob= G.main->object.first;
252         while(ob) {
253                 if(ob->data==lt) {
254                         if(ob->id.lib) lib= 1;
255                         else local= 1;
256                 }
257                 ob= ob->id.next;
258         }
259         
260         if(local && lib==0) {
261                 lt->id.lib= 0;
262                 lt->id.flag= LIB_LOCAL;
263                 new_id(0, (ID *)lt, 0);
264         }
265         else if(local && lib) {
266                 ltn= copy_lattice(lt);
267                 ltn->id.us= 0;
268                 
269                 ob= G.main->object.first;
270                 while(ob) {
271                         if(ob->data==lt) {
272                                 
273                                 if(ob->id.lib==0) {
274                                         ob->data= ltn;
275                                         ltn->id.us++;
276                                         lt->id.us--;
277                                 }
278                         }
279                         ob= ob->id.next;
280                 }
281         }
282 }
283
284 void init_latt_deform(Object *oblatt, Object *ob)
285 {
286                 /* we make an array with all differences */
287         Lattice *lt = deformLatt = (oblatt==G.obedit)?editLatt:oblatt->data;
288         BPoint *bp = lt->def;
289         DispList *dl = find_displist(&oblatt->disp, DL_VERTS);
290         float *co = dl?dl->verts:NULL;
291         float *fp, imat[4][4];
292         float fu, fv, fw;
293         int u, v, w;
294
295         fp= latticedata= MEM_mallocN(sizeof(float)*3*deformLatt->pntsu*deformLatt->pntsv*deformLatt->pntsw, "latticedata");
296         
297                 /* for example with a particle system: ob==0 */
298         if(ob==0) {
299                 /* in deformspace, calc matrix  */
300                 Mat4Invert(latmat, oblatt->obmat);
301         
302                 /* back: put in deform array */
303                 Mat4Invert(imat, latmat);
304         }
305         else {
306                 /* in deformspace, calc matrix */
307                 Mat4Invert(imat, oblatt->obmat);
308                 Mat4MulMat4(latmat, ob->obmat, imat);
309         
310                 /* back: put in deform array */
311                 Mat4Invert(imat, latmat);
312         }
313         
314         for(w=0,fw=lt->fw; w<lt->pntsw; w++,fw+=lt->dw) {
315                 for(v=0,fv=lt->fv; v<lt->pntsv; v++, fv+=lt->dv) {
316                         for(u=0,fu=lt->fu; u<lt->pntsu; u++, bp++, co+=3, fp+=3, fu+=lt->du) {
317                                 if (dl) {
318                                         fp[0] = co[0] - fu;
319                                         fp[1] = co[1] - fv;
320                                         fp[2] = co[2] - fw;
321                                 } else {
322                                         fp[0] = bp->vec[0] - fu;
323                                         fp[1] = bp->vec[1] - fv;
324                                         fp[2] = bp->vec[2] - fw;
325                                 }
326
327                                 Mat4Mul3Vecfl(imat, fp);
328                         }
329                 }
330         }
331 }
332
333 void calc_latt_deform(float *co, float weight)
334 {
335         Lattice *lt;
336         float u, v, w, tu[4], tv[4], tw[4];
337         float *fpw, *fpv, *fpu, vec[3];
338         int ui, vi, wi, uu, vv, ww;
339         
340         if(latticedata==0) return;
341         
342         lt= deformLatt; /* just for shorter notation! */
343         
344         /* co is in local coords, treat with latmat */
345         
346         VECCOPY(vec, co);
347         Mat4MulVecfl(latmat, vec);
348         
349         /* u v w coords */
350         
351         if(lt->pntsu>1) {
352                 u= (vec[0]-lt->fu)/lt->du;
353                 ui= (int)floor(u);
354                 u -= ui;
355                 set_four_ipo(u, tu, lt->typeu);
356         }
357         else {
358                 tu[0]= tu[2]= tu[3]= 0.0; tu[1]= 1.0;
359                 ui= 0;
360         }
361         
362         if(lt->pntsv>1) {
363                 v= (vec[1]-lt->fv)/lt->dv;
364                 vi= (int)floor(v);
365                 v -= vi;
366                 set_four_ipo(v, tv, lt->typev);
367         }
368         else {
369                 tv[0]= tv[2]= tv[3]= 0.0; tv[1]= 1.0;
370                 vi= 0;
371         }
372         
373         if(lt->pntsw>1) {
374                 w= (vec[2]-lt->fw)/lt->dw;
375                 wi= (int)floor(w);
376                 w -= wi;
377                 set_four_ipo(w, tw, lt->typew);
378         }
379         else {
380                 tw[0]= tw[2]= tw[3]= 0.0; tw[1]= 1.0;
381                 wi= 0;
382         }
383         
384         for(ww= wi-1; ww<=wi+2; ww++) {
385                 w= tw[ww-wi+1];
386                 
387                 if(w!=0.0) {
388                         if(ww>0) {
389                                 if(ww<lt->pntsw) fpw= latticedata + 3*ww*lt->pntsu*lt->pntsv;
390                                 else fpw= latticedata + 3*(lt->pntsw-1)*lt->pntsu*lt->pntsv;
391                         }
392                         else fpw= latticedata;
393                         
394                         for(vv= vi-1; vv<=vi+2; vv++) {
395                                 v= w*tv[vv-vi+1];
396                                 
397                                 if(v!=0.0) {
398                                         if(vv>0) {
399                                                 if(vv<lt->pntsv) fpv= fpw + 3*vv*lt->pntsu;
400                                                 else fpv= fpw + 3*(lt->pntsv-1)*lt->pntsu;
401                                         }
402                                         else fpv= fpw;
403                                         
404                                         for(uu= ui-1; uu<=ui+2; uu++) {
405                                                 u= weight*v*tu[uu-ui+1];
406                                                 
407                                                 if(u!=0.0) {
408                                                         if(uu>0) {
409                                                                 if(uu<lt->pntsu) fpu= fpv + 3*uu;
410                                                                 else fpu= fpv + 3*(lt->pntsu-1);
411                                                         }
412                                                         else fpu= fpv;
413                                                         
414                                                         co[0]+= u*fpu[0];
415                                                         co[1]+= u*fpu[1];
416                                                         co[2]+= u*fpu[2];
417                                                 }
418                                         }
419                                 }
420                         }
421                 }
422         }
423 }
424
425 void end_latt_deform()
426 {
427
428         MEM_freeN(latticedata);
429         latticedata= 0;
430 }
431
432         /* calculations is in local space of deformed object
433            so we store in latmat transform from path coord inside object 
434          */
435 typedef struct {
436         float dmin[3], dmax[3], dsize, dloc[3];
437         float curvespace[4][4], objectspace[4][4];
438 } CurveDeform;
439
440 static void init_curve_deform(Object *par, Object *ob, CurveDeform *cd)
441 {
442         Mat4Invert(ob->imat, ob->obmat);
443         Mat4MulMat4(cd->objectspace, par->obmat, ob->imat);
444         Mat4Invert(cd->curvespace, cd->objectspace);
445
446         // offset vector for 'no smear'
447         Mat4Invert(par->imat, par->obmat);
448         VecMat4MulVecfl(cd->dloc, par->imat, ob->obmat[3]);
449
450 }
451
452 /* this makes sure we can extend for non-cyclic. *vec needs 4 items! */
453 static int where_on_path_deform(Object *ob, float ctime, float *vec, float *dir)        /* returns OK */
454 {
455         Curve *cu= ob->data;
456         BevList *bl;
457         float ctime1;
458         int cycl=0;
459         
460         /* test for cyclic */
461         bl= cu->bev.first;
462         if(bl && bl->poly> -1) cycl= 1;
463
464         if(cycl==0) {
465                 ctime1= CLAMPIS(ctime, 0.0, 1.0);
466         }
467         else ctime1= ctime;
468         
469         /* vec needs 4 items */
470         if(where_on_path(ob, ctime1, vec, dir)) {
471                 
472                 if(cycl==0) {
473                         Path *path= cu->path;
474                         float dvec[3];
475                         
476                         if(ctime < 0.0) {
477                                 VecSubf(dvec, path->data+4, path->data);
478                                 VecMulf(dvec, ctime*(float)path->len);
479                                 VECADD(vec, vec, dvec);
480                         }
481                         else if(ctime > 1.0) {
482                                 VecSubf(dvec, path->data+4*path->len-4, path->data+4*path->len-8);
483                                 VecMulf(dvec, (ctime-1.0)*(float)path->len);
484                                 VECADD(vec, vec, dvec);
485                         }
486                 }
487                 return 1;
488         }
489         return 0;
490 }
491
492         /* for each point, rotate & translate to curve */
493         /* use path, since it has constant distances */
494         /* co: local coord, result local too */
495 static void calc_curve_deform(Object *par, float *co, short axis, CurveDeform *cd)
496 {
497         Curve *cu= par->data;
498         float fac, loc[4], dir[3], *quat, q[4], mat[3][3], cent[3];
499         short upflag, index;
500         
501         if(axis==OB_POSX || axis==OB_NEGX) {
502                 upflag= OB_POSZ;
503                 cent[0]= 0.0;
504                 cent[1]= co[1];
505                 cent[2]= co[2];
506                 index= 0;
507         }
508         else if(axis==OB_POSY || axis==OB_NEGY) {
509                 upflag= OB_POSZ;
510                 cent[0]= co[0];
511                 cent[1]= 0.0;
512                 cent[2]= co[2];
513                 index= 1;
514         }
515         else {
516                 upflag= OB_POSY;
517                 cent[0]= co[0];
518                 cent[1]= co[1];
519                 cent[2]= 0.0;
520                 index= 2;
521         }
522         /* to be sure */
523         if(cu->path==NULL) {
524                 calc_curvepath(par);
525                 if(cu->path==NULL) return;      // happens on append...
526         }
527         /* options */
528         if(cu->flag & CU_STRETCH)
529                 fac= (co[index]-cd->dmin[index])/(cd->dmax[index] - cd->dmin[index]);
530         else
531                 fac= (cd->dloc[index])/(cu->path->totdist) + (co[index]-cd->dmin[index])/(cu->path->totdist);
532         
533         if( where_on_path_deform(par, fac, loc, dir)) { /* returns OK */
534
535                 quat= vectoquat(dir, axis, upflag);
536                 
537                 /* the tilt */
538                 if(loc[3]!=0.0) {
539                         Normalise(dir);
540                         q[0]= (float)cos(0.5*loc[3]);
541                         fac= (float)sin(0.5*loc[3]);
542                         q[1]= -fac*dir[0];
543                         q[2]= -fac*dir[1];
544                         q[3]= -fac*dir[2];
545                         QuatMul(quat, q, quat);
546                 }               
547                 QuatToMat3(quat, mat);
548         
549                 /* local rotation */
550                 Mat3MulVecfl(mat, cent);
551                 
552                 /* translation */
553                 VECADD(co, cent, loc);
554                 
555         }
556
557 }
558
559 void curve_deform_verts(Object *cuOb, Object *target, float (*vertexCos)[3], int numVerts, char *vgroup)
560 {
561         Curve *cu = cuOb->data;
562         int a, flag = cu->flag;
563         CurveDeform cd;
564         
565         cu->flag |= (CU_PATH|CU_FOLLOW); // needed for path & bevlist
566
567         init_curve_deform(cuOb, target, &cd);
568                 
569         INIT_MINMAX(cd.dmin, cd.dmax);
570                 
571         for(a=0; a<numVerts; a++) {
572                 Mat4MulVecfl(cd.curvespace, vertexCos[a]);
573                 DO_MINMAX(vertexCos[a], cd.dmin, cd.dmax);
574         }
575         
576         if(vgroup && vgroup[0] && target->type==OB_MESH) {
577                 bDeformGroup *curdef;
578                 Mesh *me= target->data;
579                 int index= 0;
580                 
581                 /* find the group (weak loop-in-loop) */
582                 for (curdef = target->defbase.first; curdef; curdef=curdef->next, index++)
583                         if (!strcmp(curdef->name, vgroup))
584                                 break;
585                 /* check for numVerts because old files can have modifier over subsurf still */
586                 if(curdef && me->dvert && numVerts==me->totvert) {
587                         MDeformVert *dvert= me->dvert;
588                         float vec[3];
589                         int j;
590                         
591                         for(a=0; a<numVerts; a++, dvert++) {
592                                 for(j=0; j<dvert->totweight; j++) {
593                                         if (dvert->dw[j].def_nr == index) {
594                                                 VECCOPY(vec, vertexCos[a]);
595                                                 calc_curve_deform(cuOb, vec, target->trackflag, &cd);
596                                                 VecLerpf(vertexCos[a], vertexCos[a], vec, dvert->dw[j].weight);
597                                                 Mat4MulVecfl(cd.objectspace, vertexCos[a]);
598                                         }
599                                 }
600                         }
601                 }
602         }
603         else {
604                 for(a=0; a<numVerts; a++) {
605                         calc_curve_deform(cuOb, vertexCos[a], target->trackflag, &cd);
606                         Mat4MulVecfl(cd.objectspace, vertexCos[a]);
607                 }
608         }
609         cu->flag = flag;
610 }
611
612 void lattice_deform_verts(Object *laOb, Object *target, float (*vertexCos)[3], int numVerts, char *vgroup)
613 {
614         int a;
615
616         init_latt_deform(laOb, target);
617         
618         if(vgroup && vgroup[0] && target->type==OB_MESH) {
619                 bDeformGroup *curdef;
620                 Mesh *me= target->data;
621                 int index= 0;
622                 
623                 /* find the group (weak loop-in-loop) */
624                 for (curdef = target->defbase.first; curdef; curdef=curdef->next, index++)
625                         if (!strcmp(curdef->name, vgroup))
626                                 break;
627                 /* check for numVerts because old files can have modifier over subsurf still */
628                 if(curdef && me->dvert && numVerts==me->totvert) {
629                         MDeformVert *dvert= me->dvert;
630                         int j;
631                         
632                         for(a=0; a<numVerts; a++, dvert++) {
633                                 for(j=0; j<dvert->totweight; j++) {
634                                         if (dvert->dw[j].def_nr == index) {
635                                                 calc_latt_deform(vertexCos[a], dvert->dw[j].weight);
636                                         }
637                                 }
638                         }
639                 }
640         }
641         else {
642                 for(a=0; a<numVerts; a++) {
643                         calc_latt_deform(vertexCos[a], 1.0f);
644                 }
645         }
646         end_latt_deform();
647 }
648
649 int object_deform_mball(Object *ob)
650 {
651         if(ob->parent && ob->parent->type==OB_LATTICE && ob->partype==PARSKEL) {
652                 DispList *dl;
653
654                 for (dl=ob->disp.first; dl; dl=dl->next) {
655                         lattice_deform_verts(ob->parent, ob, (float(*)[3]) dl->verts, dl->nr, NULL);
656                 }
657
658                 return 1;
659         } else {
660                 return 0;
661         }
662 }
663
664 static BPoint *latt_bp(Lattice *lt, int u, int v, int w)
665 {
666         return lt->def+ u + v*lt->pntsu + w*lt->pntsu*lt->pntsv;
667 }
668
669 void outside_lattice(Lattice *lt)
670 {
671         BPoint *bp, *bp1, *bp2;
672         int u, v, w;
673         float fac1, du=0.0, dv=0.0, dw=0.0;
674
675         bp= lt->def;
676
677         if(lt->pntsu>1) du= 1.0f/((float)lt->pntsu-1);
678         if(lt->pntsv>1) dv= 1.0f/((float)lt->pntsv-1);
679         if(lt->pntsw>1) dw= 1.0f/((float)lt->pntsw-1);
680                 
681         for(w=0; w<lt->pntsw; w++) {
682                 
683                 for(v=0; v<lt->pntsv; v++) {
684                 
685                         for(u=0; u<lt->pntsu; u++, bp++) {
686                                 if(u==0 || v==0 || w==0 || u==lt->pntsu-1 || v==lt->pntsv-1 || w==lt->pntsw-1);
687                                 else {
688                                 
689                                         bp->hide= 1;
690                                         bp->f1 &= ~SELECT;
691                                         
692                                         /* u extrema */
693                                         bp1= latt_bp(lt, 0, v, w);
694                                         bp2= latt_bp(lt, lt->pntsu-1, v, w);
695                                         
696                                         fac1= du*u;
697                                         bp->vec[0]= (1.0f-fac1)*bp1->vec[0] + fac1*bp2->vec[0];
698                                         bp->vec[1]= (1.0f-fac1)*bp1->vec[1] + fac1*bp2->vec[1];
699                                         bp->vec[2]= (1.0f-fac1)*bp1->vec[2] + fac1*bp2->vec[2];
700                                         
701                                         /* v extrema */
702                                         bp1= latt_bp(lt, u, 0, w);
703                                         bp2= latt_bp(lt, u, lt->pntsv-1, w);
704                                         
705                                         fac1= dv*v;
706                                         bp->vec[0]+= (1.0f-fac1)*bp1->vec[0] + fac1*bp2->vec[0];
707                                         bp->vec[1]+= (1.0f-fac1)*bp1->vec[1] + fac1*bp2->vec[1];
708                                         bp->vec[2]+= (1.0f-fac1)*bp1->vec[2] + fac1*bp2->vec[2];
709                                         
710                                         /* w extrema */
711                                         bp1= latt_bp(lt, u, v, 0);
712                                         bp2= latt_bp(lt, u, v, lt->pntsw-1);
713                                         
714                                         fac1= dw*w;
715                                         bp->vec[0]+= (1.0f-fac1)*bp1->vec[0] + fac1*bp2->vec[0];
716                                         bp->vec[1]+= (1.0f-fac1)*bp1->vec[1] + fac1*bp2->vec[1];
717                                         bp->vec[2]+= (1.0f-fac1)*bp1->vec[2] + fac1*bp2->vec[2];
718                                         
719                                         VecMulf(bp->vec, 0.3333333f);
720                                         
721                                 }
722                         }
723                         
724                 }
725                 
726         }
727         
728 }
729
730 float (*lattice_getVertexCos(struct Object *ob, int *numVerts_r))[3]
731 {
732         Lattice *lt = (G.obedit==ob)?editLatt:ob->data;
733         int i, numVerts = *numVerts_r = lt->pntsu*lt->pntsv*lt->pntsw;
734         float (*vertexCos)[3] = MEM_mallocN(sizeof(*vertexCos)*numVerts,"lt_vcos");
735
736         for (i=0; i<numVerts; i++) {
737                 VECCOPY(vertexCos[i], lt->def[i].vec);
738         }
739
740         return vertexCos;
741 }
742
743 void lattice_applyVertexCos(struct Object *ob, float (*vertexCos)[3])
744 {
745         Lattice *lt = ob->data;
746         int i, numVerts = lt->pntsu*lt->pntsv*lt->pntsw;
747
748         for (i=0; i<numVerts; i++) {
749                 VECCOPY(lt->def[i].vec, vertexCos[i]);
750         }
751 }
752
753 void lattice_calc_modifiers(Object *ob)
754 {
755         float (*vertexCos)[3] = NULL;
756         ModifierData *md = modifiers_getVirtualModifierList(ob);
757         int numVerts, editmode = G.obedit==ob;
758
759         freedisplist(&ob->disp);
760
761         if (!editmode) {
762                 do_ob_key(ob);
763         }
764
765         for (; md; md=md->next) {
766                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
767
768                 if (!(md->mode&(1<<0))) continue;
769                 if (editmode && !(md->mode&eModifierMode_Editmode)) continue;
770                 if (mti->isDisabled && mti->isDisabled(md)) continue;
771                 if (mti->type!=eModifierTypeType_OnlyDeform) continue;
772
773                 if (!vertexCos) vertexCos = lattice_getVertexCos(ob, &numVerts);
774                 mti->deformVerts(md, ob, NULL, vertexCos, numVerts);
775         }
776
777         if (vertexCos) {
778                 DispList *dl = MEM_callocN(sizeof(*dl), "lt_dl");
779                 dl->type = DL_VERTS;
780                 dl->parts = 1;
781                 dl->nr = numVerts;
782                 dl->verts = (float*) vertexCos;
783                 
784                 BLI_addtail(&ob->disp, dl);
785         }
786 }