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