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