Patch to change license to GPL only, from GSR.
[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 float *calc_curve_deform(Object *par, float *co, short axis, CurveDeform *cd)
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 NULL; // 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];
574                 float *quat;
575                 
576                 if(cd->no_rot_axis)     /* set by caller */
577                         dir[cd->no_rot_axis-1]= 0.0f;
578                 
579                 /* -1 for compatibility with old track defines */
580                 quat= vectoquat(dir, axis-1, upflag);   /* gives static quat */
581                 
582                 /* the tilt */
583                 if(loc[3]!=0.0) {
584                         Normalize(dir);
585                         q[0]= (float)cos(0.5*loc[3]);
586                         fac= (float)sin(0.5*loc[3]);
587                         q[1]= -fac*dir[0];
588                         q[2]= -fac*dir[1];
589                         q[3]= -fac*dir[2];
590                         QuatMul(quat, q, quat);
591                 }               
592                 QuatToMat3(quat, mat);
593         
594                 /* local rotation */
595                 Mat3MulVecfl(mat, cent);
596                 
597                 /* translation */
598                 VECADD(co, cent, loc);
599                 
600                 return quat;
601         }
602         return NULL;
603 }
604
605 void curve_deform_verts(Object *cuOb, Object *target, DerivedMesh *dm, float (*vertexCos)[3], int numVerts, char *vgroup, short defaxis)
606 {
607         Curve *cu = cuOb->data;
608         int a, flag = cu->flag;
609         CurveDeform cd;
610         int use_vgroups;
611         
612         cu->flag |= (CU_PATH|CU_FOLLOW); // needed for path & bevlist
613
614         init_curve_deform(cuOb, target, &cd, (cu->flag & CU_STRETCH)==0);
615                 
616         /* check whether to use vertex groups (only possible if target is a Mesh)
617          * we want either a Mesh with no derived data, or derived data with
618          * deformverts
619          */
620         if(target && target->type==OB_MESH) {
621                 /* if there's derived data without deformverts, don't use vgroups */
622                 if(dm && !dm->getVertData(dm, 0, CD_MDEFORMVERT))
623                         use_vgroups = 0;
624                 else
625                         use_vgroups = 1;
626         } else
627                 use_vgroups = 0;
628         
629         if(vgroup && vgroup[0] && use_vgroups) {
630                 bDeformGroup *curdef;
631                 Mesh *me= target->data;
632                 int index;
633                 
634                 /* find the group (weak loop-in-loop) */
635                 for(index = 0, curdef = target->defbase.first; curdef;
636                     curdef = curdef->next, index++)
637                         if (!strcmp(curdef->name, vgroup))
638                                 break;
639
640                 if(curdef && (me->dvert || dm)) {
641                         MDeformVert *dvert = me->dvert;
642                         float vec[3];
643                         int j;
644
645                         INIT_MINMAX(cd.dmin, cd.dmax);
646
647                         for(a = 0; a < numVerts; a++, dvert++) {
648                                 if(dm) dvert = dm->getVertData(dm, a, CD_MDEFORMVERT);
649
650                                 for(j = 0; j < dvert->totweight; j++) {
651                                         if(dvert->dw[j].def_nr == index) {
652                                                 Mat4MulVecfl(cd.curvespace, vertexCos[a]);
653                                                 DO_MINMAX(vertexCos[a], cd.dmin, cd.dmax);
654                                                 break;
655                                         }
656                                 }
657                         }
658
659                         dvert = me->dvert;
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                                                 VECCOPY(vec, vertexCos[a]);
666                                                 calc_curve_deform(cuOb, vec, defaxis, &cd);
667                                                 VecLerpf(vertexCos[a], vertexCos[a], vec,
668                                                          dvert->dw[j].weight);
669                                                 Mat4MulVecfl(cd.objectspace, vertexCos[a]);
670                                                 break;
671                                         }
672                                 }
673                         }
674                 }
675         } else {
676                 INIT_MINMAX(cd.dmin, cd.dmax);
677                         
678                 for(a = 0; a < numVerts; a++) {
679                         Mat4MulVecfl(cd.curvespace, vertexCos[a]);
680                         DO_MINMAX(vertexCos[a], cd.dmin, cd.dmax);
681                 }
682
683                 for(a = 0; a < numVerts; a++) {
684                         calc_curve_deform(cuOb, vertexCos[a], defaxis, &cd);
685                         Mat4MulVecfl(cd.objectspace, vertexCos[a]);
686                 }
687         }
688         cu->flag = flag;
689 }
690
691 /* input vec and orco = local coord in armature space */
692 /* orco is original not-animated or deformed reference point */
693 /* result written in vec and mat */
694 void curve_deform_vector(Object *cuOb, Object *target, float *orco, float *vec, float mat[][3], int no_rot_axis)
695 {
696         CurveDeform cd;
697         float *quat;
698         
699         init_curve_deform(cuOb, target, &cd, 0);        /* 0 no dloc */
700         cd.no_rot_axis= no_rot_axis;                            /* option to only rotate for XY, for example */
701         
702         VECCOPY(cd.dmin, orco);
703         VECCOPY(cd.dmax, orco);
704
705         Mat4MulVecfl(cd.curvespace, vec);
706         
707         quat= calc_curve_deform(cuOb, vec, target->trackflag+1, &cd);
708         if(quat) {
709                 float qmat[3][3];
710                 
711                 QuatToMat3(quat, qmat);
712                 Mat3MulMat3(mat, qmat, cd.objectspace3);
713         }
714         else
715                 Mat3One(mat);
716         
717         Mat4MulVecfl(cd.objectspace, vec);
718
719 }
720
721 void lattice_deform_verts(Object *laOb, Object *target, DerivedMesh *dm,
722                           float (*vertexCos)[3], int numVerts, char *vgroup)
723 {
724         int a;
725         int use_vgroups;
726
727         init_latt_deform(laOb, target);
728
729         /* check whether to use vertex groups (only possible if target is a Mesh)
730          * we want either a Mesh with no derived data, or derived data with
731          * deformverts
732          */
733         if(target && target->type==OB_MESH) {
734                 /* if there's derived data without deformverts, don't use vgroups */
735                 if(dm && !dm->getVertData(dm, 0, CD_MDEFORMVERT))
736                         use_vgroups = 0;
737                 else
738                         use_vgroups = 1;
739         } else
740                 use_vgroups = 0;
741         
742         if(vgroup && vgroup[0] && use_vgroups) {
743                 bDeformGroup *curdef;
744                 Mesh *me = target->data;
745                 int index = 0;
746                 
747                 /* find the group (weak loop-in-loop) */
748                 for(curdef = target->defbase.first; curdef;
749                     curdef = curdef->next, index++)
750                         if(!strcmp(curdef->name, vgroup)) break;
751
752                 if(curdef && (me->dvert || dm)) {
753                         MDeformVert *dvert = me->dvert;
754                         int j;
755                         
756                         for(a = 0; a < numVerts; a++, dvert++) {
757                                 if(dm) dvert = dm->getVertData(dm, a, CD_MDEFORMVERT);
758                                 for(j = 0; j < dvert->totweight; j++) {
759                                         if (dvert->dw[j].def_nr == index) {
760                                                 calc_latt_deform(vertexCos[a], dvert->dw[j].weight);
761                                         }
762                                 }
763                         }
764                 }
765         } else {
766                 for(a = 0; a < numVerts; a++) {
767                         calc_latt_deform(vertexCos[a], 1.0f);
768                 }
769         }
770         end_latt_deform();
771 }
772
773 int object_deform_mball(Object *ob)
774 {
775         if(ob->parent && ob->parent->type==OB_LATTICE && ob->partype==PARSKEL) {
776                 DispList *dl;
777
778                 for (dl=ob->disp.first; dl; dl=dl->next) {
779                         lattice_deform_verts(ob->parent, ob, NULL,
780                                              (float(*)[3]) dl->verts, dl->nr, NULL);
781                 }
782
783                 return 1;
784         } else {
785                 return 0;
786         }
787 }
788
789 static BPoint *latt_bp(Lattice *lt, int u, int v, int w)
790 {
791         return lt->def+ u + v*lt->pntsu + w*lt->pntsu*lt->pntsv;
792 }
793
794 void outside_lattice(Lattice *lt)
795 {
796         BPoint *bp, *bp1, *bp2;
797         int u, v, w;
798         float fac1, du=0.0, dv=0.0, dw=0.0;
799
800         bp= lt->def;
801
802         if(lt->pntsu>1) du= 1.0f/((float)lt->pntsu-1);
803         if(lt->pntsv>1) dv= 1.0f/((float)lt->pntsv-1);
804         if(lt->pntsw>1) dw= 1.0f/((float)lt->pntsw-1);
805                 
806         for(w=0; w<lt->pntsw; w++) {
807                 
808                 for(v=0; v<lt->pntsv; v++) {
809                 
810                         for(u=0; u<lt->pntsu; u++, bp++) {
811                                 if(u==0 || v==0 || w==0 || u==lt->pntsu-1 || v==lt->pntsv-1 || w==lt->pntsw-1);
812                                 else {
813                                 
814                                         bp->hide= 1;
815                                         bp->f1 &= ~SELECT;
816                                         
817                                         /* u extrema */
818                                         bp1= latt_bp(lt, 0, v, w);
819                                         bp2= latt_bp(lt, lt->pntsu-1, v, w);
820                                         
821                                         fac1= du*u;
822                                         bp->vec[0]= (1.0f-fac1)*bp1->vec[0] + fac1*bp2->vec[0];
823                                         bp->vec[1]= (1.0f-fac1)*bp1->vec[1] + fac1*bp2->vec[1];
824                                         bp->vec[2]= (1.0f-fac1)*bp1->vec[2] + fac1*bp2->vec[2];
825                                         
826                                         /* v extrema */
827                                         bp1= latt_bp(lt, u, 0, w);
828                                         bp2= latt_bp(lt, u, lt->pntsv-1, w);
829                                         
830                                         fac1= dv*v;
831                                         bp->vec[0]+= (1.0f-fac1)*bp1->vec[0] + fac1*bp2->vec[0];
832                                         bp->vec[1]+= (1.0f-fac1)*bp1->vec[1] + fac1*bp2->vec[1];
833                                         bp->vec[2]+= (1.0f-fac1)*bp1->vec[2] + fac1*bp2->vec[2];
834                                         
835                                         /* w extrema */
836                                         bp1= latt_bp(lt, u, v, 0);
837                                         bp2= latt_bp(lt, u, v, lt->pntsw-1);
838                                         
839                                         fac1= dw*w;
840                                         bp->vec[0]+= (1.0f-fac1)*bp1->vec[0] + fac1*bp2->vec[0];
841                                         bp->vec[1]+= (1.0f-fac1)*bp1->vec[1] + fac1*bp2->vec[1];
842                                         bp->vec[2]+= (1.0f-fac1)*bp1->vec[2] + fac1*bp2->vec[2];
843                                         
844                                         VecMulf(bp->vec, 0.3333333f);
845                                         
846                                 }
847                         }
848                         
849                 }
850                 
851         }
852         
853 }
854
855 float (*lattice_getVertexCos(struct Object *ob, int *numVerts_r))[3]
856 {
857         Lattice *lt = (G.obedit==ob)?editLatt:ob->data;
858         int i, numVerts = *numVerts_r = lt->pntsu*lt->pntsv*lt->pntsw;
859         float (*vertexCos)[3] = MEM_mallocN(sizeof(*vertexCos)*numVerts,"lt_vcos");
860
861         for (i=0; i<numVerts; i++) {
862                 VECCOPY(vertexCos[i], lt->def[i].vec);
863         }
864
865         return vertexCos;
866 }
867
868 void lattice_applyVertexCos(struct Object *ob, float (*vertexCos)[3])
869 {
870         Lattice *lt = ob->data;
871         int i, numVerts = lt->pntsu*lt->pntsv*lt->pntsw;
872
873         for (i=0; i<numVerts; i++) {
874                 VECCOPY(lt->def[i].vec, vertexCos[i]);
875         }
876 }
877
878 void lattice_calc_modifiers(Object *ob)
879 {
880         float (*vertexCos)[3] = NULL;
881         ModifierData *md = modifiers_getVirtualModifierList(ob);
882         int numVerts, editmode = G.obedit==ob;
883
884         freedisplist(&ob->disp);
885
886         if (!editmode) {
887                 do_ob_key(ob);
888         }
889
890         for (; md; md=md->next) {
891                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
892
893                 if (!(md->mode&eModifierMode_Realtime)) continue;
894                 if (editmode && !(md->mode&eModifierMode_Editmode)) continue;
895                 if (mti->isDisabled && mti->isDisabled(md)) continue;
896                 if (mti->type!=eModifierTypeType_OnlyDeform) continue;
897
898                 if (!vertexCos) vertexCos = lattice_getVertexCos(ob, &numVerts);
899                 mti->deformVerts(md, ob, NULL, vertexCos, numVerts);
900         }
901
902         if (vertexCos) {
903                 DispList *dl = MEM_callocN(sizeof(*dl), "lt_dl");
904                 dl->type = DL_VERTS;
905                 dl->parts = 1;
906                 dl->nr = numVerts;
907                 dl->verts = (float*) vertexCos;
908                 
909                 BLI_addtail(&ob->disp, dl);
910         }
911 }