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