46e7022c7182f13132de2e76fdea24440ee1c2dd
[blender-staging.git] / source / blender / blenkernel / intern / lattice.c
1 /**
2  * lattice.c
3  *
4  *
5  * $Id$
6  *
7  * ***** BEGIN GPL LICENSE BLOCK *****
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL LICENSE BLOCK *****
31  */
32
33
34 #include <stdio.h>
35 #include <string.h>
36 #include <math.h>
37 #include <stdlib.h>
38
39 #include "MEM_guardedalloc.h"
40
41 #include "BLI_blenlib.h"
42 #include "BLI_arithb.h"
43
44 #include "DNA_armature_types.h"
45 #include "DNA_ipo_types.h"
46 #include "DNA_mesh_types.h"
47 #include "DNA_meshdata_types.h"
48 #include "DNA_modifier_types.h"
49 #include "DNA_object_types.h"
50 #include "DNA_scene_types.h"
51 #include "DNA_lattice_types.h"
52 #include "DNA_curve_types.h"
53 #include "DNA_key_types.h"
54
55 #include "BKE_anim.h"
56 #include "BKE_armature.h"
57 #include "BKE_curve.h"
58 #include "BKE_cdderivedmesh.h"
59 #include "BKE_DerivedMesh.h"
60 #include "BKE_deform.h"
61 #include "BKE_displist.h"
62 #include "BKE_global.h"
63 #include "BKE_ipo.h"
64 #include "BKE_key.h"
65 #include "BKE_lattice.h"
66 #include "BKE_library.h"
67 #include "BKE_main.h"
68 #include "BKE_mesh.h"
69 #include "BKE_modifier.h"
70 #include "BKE_object.h"
71 #include "BKE_screen.h"
72 #include "BKE_utildefines.h"
73
74 //XXX #include "BIF_editdeform.h"
75
76 Lattice *editLatt=0;
77
78 void calc_lat_fudu(int flag, int res, float *fu, float *du)
79 {
80         if(res==1) {
81                 *fu= 0.0;
82                 *du= 0.0;
83         }
84         else if(flag & LT_GRID) {
85                 *fu= -0.5f*(res-1);
86                 *du= 1.0f;
87         }
88         else {
89                 *fu= -1.0f;
90                 *du= 2.0f/(res-1);
91         }
92 }
93
94 void resizelattice(Lattice *lt, int uNew, int vNew, int wNew, Object *ltOb)
95 {
96         BPoint *bp;
97         int i, u, v, w;
98         float fu, fv, fw, uc, vc, wc, du=0.0, dv=0.0, dw=0.0;
99         float *co, (*vertexCos)[3] = NULL;
100         
101         /* vertex weight groups are just freed all for now */
102         if(lt->dvert) {
103                 free_dverts(lt->dvert, lt->pntsu*lt->pntsv*lt->pntsw);
104                 lt->dvert= NULL;
105         }
106         
107         while(uNew*vNew*wNew > 32000) {
108                 if( uNew>=vNew && uNew>=wNew) uNew--;
109                 else if( vNew>=uNew && vNew>=wNew) vNew--;
110                 else wNew--;
111         }
112
113         vertexCos = MEM_mallocN(sizeof(*vertexCos)*uNew*vNew*wNew, "tmp_vcos");
114
115         calc_lat_fudu(lt->flag, uNew, &fu, &du);
116         calc_lat_fudu(lt->flag, vNew, &fv, &dv);
117         calc_lat_fudu(lt->flag, wNew, &fw, &dw);
118
119                 /* If old size is different then resolution changed in interface,
120                  * try to do clever reinit of points. Pretty simply idea, we just
121                  * deform new verts by old lattice, but scaling them to match old
122                  * size first.
123                  */
124         if (ltOb) {
125                 if (uNew!=1 && lt->pntsu!=1) {
126                         fu = lt->fu;
127                         du = (lt->pntsu-1)*lt->du/(uNew-1);
128                 }
129
130                 if (vNew!=1 && lt->pntsv!=1) {
131                         fv = lt->fv;
132                         dv = (lt->pntsv-1)*lt->dv/(vNew-1);
133                 }
134
135                 if (wNew!=1 && lt->pntsw!=1) {
136                         fw = lt->fw;
137                         dw = (lt->pntsw-1)*lt->dw/(wNew-1);
138                 }
139         }
140
141         co = vertexCos[0];
142         for(w=0,wc=fw; w<wNew; w++,wc+=dw) {
143                 for(v=0,vc=fv; v<vNew; v++,vc+=dv) {
144                         for(u=0,uc=fu; u<uNew; u++,co+=3,uc+=du) {
145                                 co[0] = uc;
146                                 co[1] = vc;
147                                 co[2] = wc;
148                         }
149                 }
150         }
151         
152         if (ltOb) {
153                 float mat[4][4];
154                 int typeu = lt->typeu, typev = lt->typev, typew = lt->typew;
155
156                         /* works best if we force to linear type (endpoints match) */
157                 lt->typeu = lt->typev = lt->typew = KEY_LINEAR;
158
159                         /* prevent using deformed locations */
160                 freedisplist(&ltOb->disp);
161
162                 Mat4CpyMat4(mat, ltOb->obmat);
163                 Mat4One(ltOb->obmat);
164                 lattice_deform_verts(ltOb, NULL, NULL, vertexCos, uNew*vNew*wNew, NULL);
165                 Mat4CpyMat4(ltOb->obmat, mat);
166
167                 lt->typeu = typeu;
168                 lt->typev = typev;
169                 lt->typew = typew;
170         }
171
172         lt->fu = fu;
173         lt->fv = fv;
174         lt->fw = fw;
175         lt->du = du;
176         lt->dv = dv;
177         lt->dw = dw;
178
179         lt->pntsu = uNew;
180         lt->pntsv = vNew;
181         lt->pntsw = wNew;
182
183         MEM_freeN(lt->def);
184         lt->def= MEM_callocN(lt->pntsu*lt->pntsv*lt->pntsw*sizeof(BPoint), "lattice bp");
185         
186         bp= lt->def;
187         
188         for (i=0; i<lt->pntsu*lt->pntsv*lt->pntsw; i++,bp++) {
189                 VECCOPY(bp->vec, vertexCos[i]);
190         }
191
192         MEM_freeN(vertexCos);
193 }
194
195 Lattice *add_lattice(char *name)
196 {
197         Lattice *lt;
198         
199         lt= alloc_libblock(&G.main->latt, ID_LT, name);
200         
201         lt->flag= LT_GRID;
202         
203         lt->typeu= lt->typev= lt->typew= KEY_BSPLINE;
204         
205         lt->def= MEM_callocN(sizeof(BPoint), "lattvert"); /* temporary */
206         resizelattice(lt, 2, 2, 2, NULL);       /* creates a uniform lattice */
207                 
208         return lt;
209 }
210
211 Lattice *copy_lattice(Lattice *lt)
212 {
213         Lattice *ltn;
214
215         ltn= copy_libblock(lt);
216         ltn->def= MEM_dupallocN(lt->def);
217                 
218         id_us_plus((ID *)ltn->ipo);
219
220         ltn->key= copy_key(ltn->key);
221         if(ltn->key) ltn->key->from= (ID *)ltn;
222         
223         if(lt->dvert) {
224                 int tot= lt->pntsu*lt->pntsv*lt->pntsw;
225                 ltn->dvert = MEM_mallocN (sizeof (MDeformVert)*tot, "Lattice MDeformVert");
226                 copy_dverts(ltn->dvert, lt->dvert, tot);
227         }
228         
229         return ltn;
230 }
231
232 void free_lattice(Lattice *lt)
233 {
234         if(lt->def) MEM_freeN(lt->def);
235         if(lt->dvert) free_dverts(lt->dvert, lt->pntsu*lt->pntsv*lt->pntsw);
236 }
237
238
239 void make_local_lattice(Lattice *lt)
240 {
241         Object *ob;
242         Lattice *ltn;
243         int local=0, lib=0;
244
245         /* - only lib users: do nothing
246          * - only local users: set flag
247          * - mixed: make copy
248          */
249         
250         if(lt->id.lib==0) return;
251         if(lt->id.us==1) {
252                 lt->id.lib= 0;
253                 lt->id.flag= LIB_LOCAL;
254                 new_id(0, (ID *)lt, 0);
255                 return;
256         }
257         
258         ob= G.main->object.first;
259         while(ob) {
260                 if(ob->data==lt) {
261                         if(ob->id.lib) lib= 1;
262                         else local= 1;
263                 }
264                 ob= ob->id.next;
265         }
266         
267         if(local && lib==0) {
268                 lt->id.lib= 0;
269                 lt->id.flag= LIB_LOCAL;
270                 new_id(0, (ID *)lt, 0);
271         }
272         else if(local && lib) {
273                 ltn= copy_lattice(lt);
274                 ltn->id.us= 0;
275                 
276                 ob= G.main->object.first;
277                 while(ob) {
278                         if(ob->data==lt) {
279                                 
280                                 if(ob->id.lib==0) {
281                                         ob->data= ltn;
282                                         ltn->id.us++;
283                                         lt->id.us--;
284                                 }
285                         }
286                         ob= ob->id.next;
287                 }
288         }
289 }
290
291 void init_latt_deform(Object *oblatt, Object *ob)
292 {
293                 /* we make an array with all differences */
294         Lattice *lt= oblatt->data;
295         BPoint *bp;
296         DispList *dl = find_displist(&oblatt->disp, DL_VERTS);
297         float *co = dl?dl->verts:NULL;
298         float *fp, imat[4][4];
299         float fu, fv, fw;
300         int u, v, w;
301
302         if(lt->editlatt) lt= lt->editlatt;
303         bp = lt->def;
304         
305         fp= lt->latticedata= MEM_mallocN(sizeof(float)*3*lt->pntsu*lt->pntsv*lt->pntsw, "latticedata");
306         
307                 /* for example with a particle system: ob==0 */
308         if(ob==NULL) {
309                 /* in deformspace, calc matrix  */
310                 Mat4Invert(lt->latmat, oblatt->obmat);
311         
312                 /* back: put in deform array */
313                 Mat4Invert(imat, lt->latmat);
314         }
315         else {
316                 /* in deformspace, calc matrix */
317                 Mat4Invert(imat, oblatt->obmat);
318                 Mat4MulMat4(lt->latmat, ob->obmat, imat);
319         
320                 /* back: put in deform array */
321                 Mat4Invert(imat, lt->latmat);
322         }
323         
324         for(w=0,fw=lt->fw; w<lt->pntsw; w++,fw+=lt->dw) {
325                 for(v=0,fv=lt->fv; v<lt->pntsv; v++, fv+=lt->dv) {
326                         for(u=0,fu=lt->fu; u<lt->pntsu; u++, bp++, co+=3, fp+=3, fu+=lt->du) {
327                                 if (dl) {
328                                         fp[0] = co[0] - fu;
329                                         fp[1] = co[1] - fv;
330                                         fp[2] = co[2] - fw;
331                                 } else {
332                                         fp[0] = bp->vec[0] - fu;
333                                         fp[1] = bp->vec[1] - fv;
334                                         fp[2] = bp->vec[2] - fw;
335                                 }
336
337                                 Mat4Mul3Vecfl(imat, fp);
338                         }
339                 }
340         }
341 }
342
343 void calc_latt_deform(Object *ob, float *co, float weight)
344 {
345         Lattice *lt= ob->data;
346         float u, v, w, tu[4], tv[4], tw[4];
347         float *fpw, *fpv, *fpu, vec[3];
348         int ui, vi, wi, uu, vv, ww;
349         
350         if(lt->editlatt) lt= lt->editlatt;
351         if(lt->latticedata==NULL) return;
352         
353         /* co is in local coords, treat with latmat */
354         
355         VECCOPY(vec, co);
356         Mat4MulVecfl(lt->latmat, vec);
357         
358         /* u v w coords */
359         
360         if(lt->pntsu>1) {
361                 u= (vec[0]-lt->fu)/lt->du;
362                 ui= (int)floor(u);
363                 u -= ui;
364                 set_four_ipo(u, tu, lt->typeu);
365         }
366         else {
367                 tu[0]= tu[2]= tu[3]= 0.0; tu[1]= 1.0;
368                 ui= 0;
369         }
370         
371         if(lt->pntsv>1) {
372                 v= (vec[1]-lt->fv)/lt->dv;
373                 vi= (int)floor(v);
374                 v -= vi;
375                 set_four_ipo(v, tv, lt->typev);
376         }
377         else {
378                 tv[0]= tv[2]= tv[3]= 0.0; tv[1]= 1.0;
379                 vi= 0;
380         }
381         
382         if(lt->pntsw>1) {
383                 w= (vec[2]-lt->fw)/lt->dw;
384                 wi= (int)floor(w);
385                 w -= wi;
386                 set_four_ipo(w, tw, lt->typew);
387         }
388         else {
389                 tw[0]= tw[2]= tw[3]= 0.0; tw[1]= 1.0;
390                 wi= 0;
391         }
392         
393         for(ww= wi-1; ww<=wi+2; ww++) {
394                 w= tw[ww-wi+1];
395                 
396                 if(w!=0.0) {
397                         if(ww>0) {
398                                 if(ww<lt->pntsw) fpw= lt->latticedata + 3*ww*lt->pntsu*lt->pntsv;
399                                 else fpw= lt->latticedata + 3*(lt->pntsw-1)*lt->pntsu*lt->pntsv;
400                         }
401                         else fpw= lt->latticedata;
402                         
403                         for(vv= vi-1; vv<=vi+2; vv++) {
404                                 v= w*tv[vv-vi+1];
405                                 
406                                 if(v!=0.0) {
407                                         if(vv>0) {
408                                                 if(vv<lt->pntsv) fpv= fpw + 3*vv*lt->pntsu;
409                                                 else fpv= fpw + 3*(lt->pntsv-1)*lt->pntsu;
410                                         }
411                                         else fpv= fpw;
412                                         
413                                         for(uu= ui-1; uu<=ui+2; uu++) {
414                                                 u= weight*v*tu[uu-ui+1];
415                                                 
416                                                 if(u!=0.0) {
417                                                         if(uu>0) {
418                                                                 if(uu<lt->pntsu) fpu= fpv + 3*uu;
419                                                                 else fpu= fpv + 3*(lt->pntsu-1);
420                                                         }
421                                                         else fpu= fpv;
422                                                         
423                                                         co[0]+= u*fpu[0];
424                                                         co[1]+= u*fpu[1];
425                                                         co[2]+= u*fpu[2];
426                                                 }
427                                         }
428                                 }
429                         }
430                 }
431         }
432 }
433
434 void end_latt_deform(Object *ob)
435 {
436         Lattice *lt= ob->data;
437         
438         if(lt->editlatt) lt= lt->editlatt;
439         
440         if(lt->latticedata)
441                 MEM_freeN(lt->latticedata);
442         lt->latticedata= NULL;
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(Scene *scene, 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(scene, 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(Scene *scene, 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(scene, 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(scene, 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(Scene *scene, 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(scene, 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(laOb, vertexCos[a], dvert->dw[j].weight);
775                                         }
776                                 }
777                         }
778                 }
779         } else {
780                 for(a = 0; a < numVerts; a++) {
781                         calc_latt_deform(laOb, vertexCos[a], 1.0f);
782                 }
783         }
784         end_latt_deform(laOb);
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 = ob->data;
872         int i, numVerts;
873         float (*vertexCos)[3];
874
875         if(lt->editlatt) lt= lt->editlatt;
876         numVerts = *numVerts_r = lt->pntsu*lt->pntsv*lt->pntsw;
877         
878         vertexCos = MEM_mallocN(sizeof(*vertexCos)*numVerts,"lt_vcos");
879         
880         for (i=0; i<numVerts; i++) {
881                 VECCOPY(vertexCos[i], lt->def[i].vec);
882         }
883
884         return vertexCos;
885 }
886
887 void lattice_applyVertexCos(struct Object *ob, float (*vertexCos)[3])
888 {
889         Lattice *lt = ob->data;
890         int i, numVerts = lt->pntsu*lt->pntsv*lt->pntsw;
891
892         for (i=0; i<numVerts; i++) {
893                 VECCOPY(lt->def[i].vec, vertexCos[i]);
894         }
895 }
896
897 void lattice_calc_modifiers(Scene *scene, Object *ob)
898 {
899         Lattice *lt= ob->data;
900         ModifierData *md = modifiers_getVirtualModifierList(ob);
901         float (*vertexCos)[3] = NULL;
902         int numVerts, editmode = (lt->editlatt!=NULL);
903
904         freedisplist(&ob->disp);
905
906         if (!editmode) {
907                 do_ob_key(scene, ob);
908         }
909
910         for (; md; md=md->next) {
911                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
912
913                 md->scene= scene;
914                 
915                 if (!(md->mode&eModifierMode_Realtime)) continue;
916                 if (editmode && !(md->mode&eModifierMode_Editmode)) continue;
917                 if (mti->isDisabled && mti->isDisabled(md)) continue;
918                 if (mti->type!=eModifierTypeType_OnlyDeform) continue;
919
920                 if (!vertexCos) vertexCos = lattice_getVertexCos(ob, &numVerts);
921                 mti->deformVerts(md, ob, NULL, vertexCos, numVerts);
922         }
923
924         /* always displist to make this work like derivedmesh */
925         if (!vertexCos) vertexCos = lattice_getVertexCos(ob, &numVerts);
926         
927         {
928                 DispList *dl = MEM_callocN(sizeof(*dl), "lt_dl");
929                 dl->type = DL_VERTS;
930                 dl->parts = 1;
931                 dl->nr = numVerts;
932                 dl->verts = (float*) vertexCos;
933                 
934                 BLI_addtail(&ob->disp, dl);
935         }
936 }
937
938 struct MDeformVert* lattice_get_deform_verts(struct Object *oblatt)
939 {
940         if(oblatt->type == OB_LATTICE)
941         {
942                 Lattice *lt = (Lattice*)oblatt->data;
943                 if(lt->editlatt) lt= lt->editlatt;
944                 return lt->dvert;
945         }
946
947         return NULL;    
948 }
949