0faff6d33543bc2139271dcd12cc9fc42a14102d
[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 #include "BLI_cellalloc.h"
44
45 #include "DNA_armature_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_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 void calc_lat_fudu(int flag, int res, float *fu, float *du)
76 {
77         if(res==1) {
78                 *fu= 0.0;
79                 *du= 0.0;
80         }
81         else if(flag & LT_GRID) {
82                 *fu= -0.5f*(res-1);
83                 *du= 1.0f;
84         }
85         else {
86                 *fu= -1.0f;
87                 *du= 2.0f/(res-1);
88         }
89 }
90
91 void resizelattice(Lattice *lt, int uNew, int vNew, int wNew, Object *ltOb)
92 {
93         BPoint *bp;
94         int i, u, v, w;
95         float fu, fv, fw, uc, vc, wc, du=0.0, dv=0.0, dw=0.0;
96         float *co, (*vertexCos)[3] = NULL;
97         
98         /* vertex weight groups are just freed all for now */
99         if(lt->dvert) {
100                 free_dverts(lt->dvert, lt->pntsu*lt->pntsv*lt->pntsw);
101                 lt->dvert= NULL;
102         }
103         
104         while(uNew*vNew*wNew > 32000) {
105                 if( uNew>=vNew && uNew>=wNew) uNew--;
106                 else if( vNew>=uNew && vNew>=wNew) vNew--;
107                 else wNew--;
108         }
109
110         vertexCos = MEM_mallocN(sizeof(*vertexCos)*uNew*vNew*wNew, "tmp_vcos");
111
112         calc_lat_fudu(lt->flag, uNew, &fu, &du);
113         calc_lat_fudu(lt->flag, vNew, &fv, &dv);
114         calc_lat_fudu(lt->flag, wNew, &fw, &dw);
115
116                 /* If old size is different then resolution changed in interface,
117                  * try to do clever reinit of points. Pretty simply idea, we just
118                  * deform new verts by old lattice, but scaling them to match old
119                  * size first.
120                  */
121         if (ltOb) {
122                 if (uNew!=1 && lt->pntsu!=1) {
123                         fu = lt->fu;
124                         du = (lt->pntsu-1)*lt->du/(uNew-1);
125                 }
126
127                 if (vNew!=1 && lt->pntsv!=1) {
128                         fv = lt->fv;
129                         dv = (lt->pntsv-1)*lt->dv/(vNew-1);
130                 }
131
132                 if (wNew!=1 && lt->pntsw!=1) {
133                         fw = lt->fw;
134                         dw = (lt->pntsw-1)*lt->dw/(wNew-1);
135                 }
136         }
137
138         co = vertexCos[0];
139         for(w=0,wc=fw; w<wNew; w++,wc+=dw) {
140                 for(v=0,vc=fv; v<vNew; v++,vc+=dv) {
141                         for(u=0,uc=fu; u<uNew; u++,co+=3,uc+=du) {
142                                 co[0] = uc;
143                                 co[1] = vc;
144                                 co[2] = wc;
145                         }
146                 }
147         }
148         
149         if (ltOb) {
150                 float mat[4][4];
151                 int typeu = lt->typeu, typev = lt->typev, typew = lt->typew;
152
153                         /* works best if we force to linear type (endpoints match) */
154                 lt->typeu = lt->typev = lt->typew = KEY_LINEAR;
155
156                         /* prevent using deformed locations */
157                 freedisplist(&ltOb->disp);
158
159                 Mat4CpyMat4(mat, ltOb->obmat);
160                 Mat4One(ltOb->obmat);
161                 lattice_deform_verts(ltOb, NULL, NULL, vertexCos, uNew*vNew*wNew, NULL);
162                 Mat4CpyMat4(ltOb->obmat, mat);
163
164                 lt->typeu = typeu;
165                 lt->typev = typev;
166                 lt->typew = typew;
167         }
168
169         lt->fu = fu;
170         lt->fv = fv;
171         lt->fw = fw;
172         lt->du = du;
173         lt->dv = dv;
174         lt->dw = dw;
175
176         lt->pntsu = uNew;
177         lt->pntsv = vNew;
178         lt->pntsw = wNew;
179
180         MEM_freeN(lt->def);
181         lt->def= MEM_callocN(lt->pntsu*lt->pntsv*lt->pntsw*sizeof(BPoint), "lattice bp");
182         
183         bp= lt->def;
184         
185         for (i=0; i<lt->pntsu*lt->pntsv*lt->pntsw; i++,bp++) {
186                 VECCOPY(bp->vec, vertexCos[i]);
187         }
188
189         MEM_freeN(vertexCos);
190 }
191
192 Lattice *add_lattice(char *name)
193 {
194         Lattice *lt;
195         
196         lt= alloc_libblock(&G.main->latt, ID_LT, name);
197         
198         lt->flag= LT_GRID;
199         
200         lt->typeu= lt->typev= lt->typew= KEY_BSPLINE;
201         
202         lt->def= MEM_callocN(sizeof(BPoint), "lattvert"); /* temporary */
203         resizelattice(lt, 2, 2, 2, NULL);       /* creates a uniform lattice */
204                 
205         return lt;
206 }
207
208 Lattice *copy_lattice(Lattice *lt)
209 {
210         Lattice *ltn;
211
212         ltn= copy_libblock(lt);
213         ltn->def= MEM_dupallocN(lt->def);
214                 
215 #if 0 // XXX old animation system
216         id_us_plus((ID *)ltn->ipo);
217 #endif // XXX old animation system
218
219         ltn->key= copy_key(ltn->key);
220         if(ltn->key) ltn->key->from= (ID *)ltn;
221         
222         if(lt->dvert) {
223                 int tot= lt->pntsu*lt->pntsv*lt->pntsw;
224                 ltn->dvert = MEM_mallocN (sizeof (MDeformVert)*tot, "Lattice MDeformVert");
225                 copy_dverts(ltn->dvert, lt->dvert, tot);
226         }
227         
228         return ltn;
229 }
230
231 void free_lattice(Lattice *lt)
232 {
233         if(lt->def) MEM_freeN(lt->def);
234         if(lt->dvert) free_dverts(lt->dvert, lt->pntsu*lt->pntsv*lt->pntsw);
235         if(lt->editlatt) {
236                 if(lt->editlatt->def) MEM_freeN(lt->editlatt->def);
237                 if(lt->editlatt->dvert) free_dverts(lt->editlatt->dvert, lt->pntsu*lt->pntsv*lt->pntsw);
238                 MEM_freeN(lt->editlatt);
239         }
240 }
241
242
243 void make_local_lattice(Lattice *lt)
244 {
245         Object *ob;
246         Lattice *ltn;
247         int local=0, lib=0;
248
249         /* - only lib users: do nothing
250          * - only local users: set flag
251          * - mixed: make copy
252          */
253         
254         if(lt->id.lib==0) return;
255         if(lt->id.us==1) {
256                 lt->id.lib= 0;
257                 lt->id.flag= LIB_LOCAL;
258                 new_id(0, (ID *)lt, 0);
259                 return;
260         }
261         
262         ob= G.main->object.first;
263         while(ob) {
264                 if(ob->data==lt) {
265                         if(ob->id.lib) lib= 1;
266                         else local= 1;
267                 }
268                 ob= ob->id.next;
269         }
270         
271         if(local && lib==0) {
272                 lt->id.lib= 0;
273                 lt->id.flag= LIB_LOCAL;
274                 new_id(0, (ID *)lt, 0);
275         }
276         else if(local && lib) {
277                 ltn= copy_lattice(lt);
278                 ltn->id.us= 0;
279                 
280                 ob= G.main->object.first;
281                 while(ob) {
282                         if(ob->data==lt) {
283                                 
284                                 if(ob->id.lib==0) {
285                                         ob->data= ltn;
286                                         ltn->id.us++;
287                                         lt->id.us--;
288                                 }
289                         }
290                         ob= ob->id.next;
291                 }
292         }
293 }
294
295 void init_latt_deform(Object *oblatt, Object *ob)
296 {
297                 /* we make an array with all differences */
298         Lattice *lt= oblatt->data;
299         BPoint *bp;
300         DispList *dl = find_displist(&oblatt->disp, DL_VERTS);
301         float *co = dl?dl->verts:NULL;
302         float *fp, imat[4][4];
303         float fu, fv, fw;
304         int u, v, w;
305
306         if(lt->editlatt) lt= lt->editlatt;
307         bp = lt->def;
308         
309         fp= lt->latticedata= MEM_mallocN(sizeof(float)*3*lt->pntsu*lt->pntsv*lt->pntsw, "latticedata");
310         
311                 /* for example with a particle system: ob==0 */
312         if(ob==NULL) {
313                 /* in deformspace, calc matrix  */
314                 Mat4Invert(lt->latmat, oblatt->obmat);
315         
316                 /* back: put in deform array */
317                 Mat4Invert(imat, lt->latmat);
318         }
319         else {
320                 /* in deformspace, calc matrix */
321                 Mat4Invert(imat, oblatt->obmat);
322                 Mat4MulMat4(lt->latmat, ob->obmat, imat);
323         
324                 /* back: put in deform array */
325                 Mat4Invert(imat, lt->latmat);
326         }
327         
328         for(w=0,fw=lt->fw; w<lt->pntsw; w++,fw+=lt->dw) {
329                 for(v=0,fv=lt->fv; v<lt->pntsv; v++, fv+=lt->dv) {
330                         for(u=0,fu=lt->fu; u<lt->pntsu; u++, bp++, co+=3, fp+=3, fu+=lt->du) {
331                                 if (dl) {
332                                         fp[0] = co[0] - fu;
333                                         fp[1] = co[1] - fv;
334                                         fp[2] = co[2] - fw;
335                                 } else {
336                                         fp[0] = bp->vec[0] - fu;
337                                         fp[1] = bp->vec[1] - fv;
338                                         fp[2] = bp->vec[2] - fw;
339                                 }
340
341                                 Mat4Mul3Vecfl(imat, fp);
342                         }
343                 }
344         }
345 }
346
347 void calc_latt_deform(Object *ob, float *co, float weight)
348 {
349         Lattice *lt= ob->data;
350         float u, v, w, tu[4], tv[4], tw[4];
351         float *fpw, *fpv, *fpu, vec[3];
352         int ui, vi, wi, uu, vv, ww;
353         
354         if(lt->editlatt) lt= lt->editlatt;
355         if(lt->latticedata==NULL) return;
356         
357         /* co is in local coords, treat with latmat */
358         
359         VECCOPY(vec, co);
360         Mat4MulVecfl(lt->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                 key_curve_position_weights(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                 key_curve_position_weights(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                 key_curve_position_weights(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= lt->latticedata + 3*ww*lt->pntsu*lt->pntsv;
403                                 else fpw= lt->latticedata + 3*(lt->pntsw-1)*lt->pntsu*lt->pntsv;
404                         }
405                         else fpw= lt->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(Object *ob)
439 {
440         Lattice *lt= ob->data;
441         
442         if(lt->editlatt) lt= lt->editlatt;
443         
444         if(lt->latticedata)
445                 MEM_freeN(lt->latticedata);
446         lt->latticedata= NULL;
447 }
448
449         /* calculations is in local space of deformed object
450            so we store in latmat transform from path coord inside object 
451          */
452 typedef struct {
453         float dmin[3], dmax[3], dsize, dloc[3];
454         float curvespace[4][4], objectspace[4][4], objectspace3[3][3];
455         int no_rot_axis;
456 } CurveDeform;
457
458 static void init_curve_deform(Object *par, Object *ob, CurveDeform *cd, int dloc)
459 {
460         Mat4Invert(ob->imat, ob->obmat);
461         Mat4MulMat4(cd->objectspace, par->obmat, ob->imat);
462         Mat4Invert(cd->curvespace, cd->objectspace);
463         Mat3CpyMat4(cd->objectspace3, cd->objectspace);
464         
465         // offset vector for 'no smear'
466         if(dloc) {
467                 Mat4Invert(par->imat, par->obmat);
468                 VecMat4MulVecfl(cd->dloc, par->imat, ob->obmat[3]);
469         }
470         else cd->dloc[0]=cd->dloc[1]=cd->dloc[2]= 0.0f;
471         
472         cd->no_rot_axis= 0;
473 }
474
475 /* this makes sure we can extend for non-cyclic. *vec needs 4 items! */
476 static int where_on_path_deform(Object *ob, float ctime, float *vec, float *dir, float *quat, float *radius)    /* returns OK */
477 {
478         Curve *cu= ob->data;
479         BevList *bl;
480         float ctime1;
481         int cycl=0;
482         
483         /* test for cyclic */
484         bl= cu->bev.first;
485         if (!bl->nr) return 0;
486         if(bl && bl->poly> -1) cycl= 1;
487
488         if(cycl==0) {
489                 ctime1= CLAMPIS(ctime, 0.0, 1.0);
490         }
491         else ctime1= ctime;
492         
493         /* vec needs 4 items */
494         if(where_on_path(ob, ctime1, vec, dir, quat, radius)) {
495                 
496                 if(cycl==0) {
497                         Path *path= cu->path;
498                         float dvec[3];
499                         
500                         if(ctime < 0.0) {
501                                 VecSubf(dvec, path->data[1].vec, path->data[0].vec);
502                                 VecMulf(dvec, ctime*(float)path->len);
503                                 VECADD(vec, vec, dvec);
504                                 if(quat) QUATCOPY(quat, path->data[0].quat);
505                                 if(radius) *radius= path->data[0].radius;
506                         }
507                         else if(ctime > 1.0) {
508                                 VecSubf(dvec, path->data[path->len-1].vec, path->data[path->len-2].vec);
509                                 VecMulf(dvec, (ctime-1.0)*(float)path->len);
510                                 VECADD(vec, vec, dvec);
511                                 if(quat) QUATCOPY(quat, path->data[path->len-1].quat);
512                                 if(radius) *radius= path->data[path->len-1].radius;
513                         }
514                 }
515                 return 1;
516         }
517         return 0;
518 }
519
520         /* for each point, rotate & translate to curve */
521         /* use path, since it has constant distances */
522         /* co: local coord, result local too */
523         /* returns quaternion for rotation, using cd->no_rot_axis */
524         /* axis is using another define!!! */
525 static int calc_curve_deform(Scene *scene, Object *par, float *co, short axis, CurveDeform *cd, float *quatp)
526 {
527         Curve *cu= par->data;
528         float fac, loc[4], dir[3], new_quat[4], radius;
529         short /*upflag, */ index;
530
531         index= axis-1;
532         if(index>2)
533                 index -= 3; /* negative  */
534
535         /* to be sure, mostly after file load */
536         if(cu->path==NULL) {
537                 makeDispListCurveTypes(scene, par, 0);
538                 if(cu->path==NULL) return 0;    // happens on append...
539         }
540         
541         /* options */
542         if(ELEM3(axis, OB_NEGX+1, OB_NEGY+1, OB_NEGZ+1)) { /* OB_NEG# 0-5, MOD_CURVE_POS# 1-6 */
543                 if(cu->flag & CU_STRETCH)
544                         fac= (-co[index]-cd->dmax[index])/(cd->dmax[index] - cd->dmin[index]);
545                 else
546                         fac= (cd->dloc[index])/(cu->path->totdist) - (co[index]-cd->dmax[index])/(cu->path->totdist);
547         }
548         else {
549                 if(cu->flag & CU_STRETCH)
550                         fac= (co[index]-cd->dmin[index])/(cd->dmax[index] - cd->dmin[index]);
551                 else
552                         fac= (cd->dloc[index])/(cu->path->totdist) + (co[index]-cd->dmin[index])/(cu->path->totdist);
553         }
554         
555 #if 0 // XXX old animation system
556         /* we want the ipo to work on the default 100 frame range, because there's no  
557            actual time involved in path position */
558         // huh? by WHY!!!!???? - Aligorith
559         if(cu->ipo) {
560                 fac*= 100.0f;
561                 if(calc_ipo_spec(cu->ipo, CU_SPEED, &fac)==0)
562                         fac/= 100.0;
563         }
564 #endif // XXX old animation system
565         
566         if( where_on_path_deform(par, fac, loc, dir, new_quat, &radius)) {      /* returns OK */
567                 float quat[4], cent[3];
568
569 #if 0   // XXX - 2.4x Z-Up, Now use bevel tilt.
570                 if(cd->no_rot_axis)     /* set by caller */
571                         dir[cd->no_rot_axis-1]= 0.0f;
572                 
573                 /* -1 for compatibility with old track defines */
574                 vectoquat(dir, axis-1, upflag, quat);
575                 
576                 /* the tilt */
577                 if(loc[3]!=0.0) {
578                         Normalize(dir);
579                         q[0]= (float)cos(0.5*loc[3]);
580                         fac= (float)sin(0.5*loc[3]);
581                         q[1]= -fac*dir[0];
582                         q[2]= -fac*dir[1];
583                         q[3]= -fac*dir[2];
584                         QuatMul(quat, q, quat);
585                 }
586 #endif
587
588
589                 static float q_x90d[4] = {0.70710676908493, 0.70710676908493, 0.0, 0.0};        // float rot_axis[3]= {1,0,0}; AxisAngleToQuat(q, rot_axis, 90 * (M_PI / 180));
590                 static float q_y90d[4] = {0.70710676908493, 0.0, 0.70710676908493, 0.0};        // float rot_axis[3]= {0,1,0}; AxisAngleToQuat(q, rot_axis, 90 * (M_PI / 180));
591                 static float q_z90d[4] = {0.70710676908493, 0.0, 0.0, 0.70710676908493};        // float rot_axis[3]= {0,0,2}; AxisAngleToQuat(q, rot_axis, 90 * (M_PI / 180));
592
593                 static float q_nx90d[4] = {0.70710676908493, -0.70710676908493, 0.0, 0.0};      // float rot_axis[3]= {1,0,0}; AxisAngleToQuat(q, rot_axis, -90 * (M_PI / 180));
594                 static float q_ny90d[4] = {0.70710676908493, 0.0, -0.70710676908493, 0.0};      // float rot_axis[3]= {0,1,0}; AxisAngleToQuat(q, rot_axis, -90 * (M_PI / 180));
595                 static float q_nz90d[4] = {0.70710676908493, 0.0, 0.0, -0.70710676908493};      // float rot_axis[3]= {0,0,2}; AxisAngleToQuat(q, rot_axis, -90 * (M_PI / 180));
596
597
598                 if(cd->no_rot_axis) {   /* set by caller */
599
600                         /* this is not exactly the same as 2.4x, since the axis is having rotation removed rather then
601                          * changing the axis before calculating the tilt but serves much the same purpose */
602                         float dir_flat[3]={0,0,0}, q[4];
603                         VECCOPY(dir_flat, dir);
604                         dir_flat[cd->no_rot_axis-1]= 0.0f;
605
606                         Normalize(dir);
607                         Normalize(dir_flat);
608
609                         RotationBetweenVectorsToQuat(q, dir, dir_flat); /* Could this be done faster? */
610
611                         QuatMul(new_quat, q, new_quat);
612                 }
613
614
615                 /* Logic for 'cent' orientation *
616                  *
617                  * The way 'co' is copied to 'cent' may seem to have no meaning, but it does.
618                  *
619                  * Use a curve modifier to stretch a cube out, color each side RGB, positive side light, negative dark.
620                  * view with X up (default), from the angle that you can see 3 faces RGB colors (light), anti-clockwise
621                  * Notice X,Y,Z Up all have light colors and each ordered CCW.
622                  *
623                  * Now for Neg Up XYZ, the colors are all dark, and ordered clockwise - Campbell
624                  * */
625
626                 switch(axis) {
627                 case MOD_CURVE_POSX:
628                         QuatMul(quat, new_quat, q_y90d);
629
630                         cent[0]=  0.0;
631                         cent[1]=  co[2];
632                         cent[2]=  co[1];
633                         break;
634                 case MOD_CURVE_NEGX:
635                         QuatMul(quat, new_quat, q_ny90d);
636
637                         cent[0]=  0.0;
638                         cent[1]= -co[1];
639                         cent[2]=  co[2];
640
641                         break;
642                 case MOD_CURVE_POSY:
643                         QuatMul(quat, new_quat, q_x90d);
644
645                         cent[0]=  co[2];
646                         cent[1]=  0.0;
647                         cent[2]= -co[0];
648                         break;
649                 case MOD_CURVE_NEGY:
650                         QuatMul(quat, new_quat, q_nx90d);
651
652                         cent[0]= -co[0];
653                         cent[1]=  0.0;
654                         cent[2]= -co[2];
655                         break;
656                 case MOD_CURVE_POSZ:
657                         QuatMul(quat, new_quat, q_z90d);
658
659                         cent[0]=  co[1];
660                         cent[1]= -co[0];
661                         cent[2]=  0.0;
662                         break;
663                 case MOD_CURVE_NEGZ:
664                         QuatMul(quat, new_quat, q_nz90d);
665
666                         cent[0]=  co[0];
667                         cent[1]= -co[1];
668                         cent[2]=  0.0;
669                         break;
670                 }
671
672                 /* scale if enabled */
673                 if(cu->flag & CU_PATH_RADIUS)
674                         VecMulf(cent, radius);
675                 
676                 /* local rotation */
677                 NormalQuat(quat);
678                 QuatMulVecf(quat, cent);
679
680                 /* translation */
681                 VECADD(co, cent, loc);
682
683                 if(quatp)
684                         QUATCOPY(quatp, quat);
685                 
686                 return 1;
687         }
688         return 0;
689 }
690
691 void curve_deform_verts(Scene *scene, Object *cuOb, Object *target, DerivedMesh *dm, float (*vertexCos)[3], int numVerts, char *vgroup, short defaxis)
692 {
693         Curve *cu;
694         int a, flag;
695         CurveDeform cd;
696         int use_vgroups;
697
698         if(cuOb->type != OB_CURVE)
699                 return;
700
701         cu = cuOb->data;
702         flag = cu->flag;
703         cu->flag |= (CU_PATH|CU_FOLLOW); // needed for path & bevlist
704
705         init_curve_deform(cuOb, target, &cd, (cu->flag & CU_STRETCH)==0);
706                 
707         /* check whether to use vertex groups (only possible if target is a Mesh)
708          * we want either a Mesh with no derived data, or derived data with
709          * deformverts
710          */
711         if(target && target->type==OB_MESH) {
712                 /* if there's derived data without deformverts, don't use vgroups */
713                 if(dm && !dm->getVertData(dm, 0, CD_MDEFORMVERT))
714                         use_vgroups = 0;
715                 else
716                         use_vgroups = 1;
717         } else
718                 use_vgroups = 0;
719         
720         if(vgroup && vgroup[0] && use_vgroups) {
721                 bDeformGroup *curdef;
722                 Mesh *me= target->data;
723                 int index;
724                 
725                 /* find the group (weak loop-in-loop) */
726                 for(index = 0, curdef = target->defbase.first; curdef;
727                     curdef = curdef->next, index++)
728                         if (!strcmp(curdef->name, vgroup))
729                                 break;
730
731                 if(curdef && (me->dvert || dm)) {
732                         MDeformVert *dvert = me->dvert;
733                         float vec[3];
734                         int j;
735
736                         INIT_MINMAX(cd.dmin, cd.dmax);
737
738                         for(a = 0; a < numVerts; a++, dvert++) {
739                                 if(dm) dvert = dm->getVertData(dm, a, CD_MDEFORMVERT);
740
741                                 for(j = 0; j < dvert->totweight; j++) {
742                                         if(dvert->dw[j].def_nr == index) {
743                                                 Mat4MulVecfl(cd.curvespace, vertexCos[a]);
744                                                 DO_MINMAX(vertexCos[a], cd.dmin, cd.dmax);
745                                                 break;
746                                         }
747                                 }
748                         }
749
750                         dvert = me->dvert;
751                         for(a = 0; a < numVerts; a++, dvert++) {
752                                 if(dm) dvert = dm->getVertData(dm, a, CD_MDEFORMVERT);
753
754                                 for(j = 0; j < dvert->totweight; j++) {
755                                         if(dvert->dw[j].def_nr == index) {
756                                                 VECCOPY(vec, vertexCos[a]);
757                                                 calc_curve_deform(scene, cuOb, vec, defaxis, &cd, NULL);
758                                                 VecLerpf(vertexCos[a], vertexCos[a], vec,
759                                                          dvert->dw[j].weight);
760                                                 Mat4MulVecfl(cd.objectspace, vertexCos[a]);
761                                                 break;
762                                         }
763                                 }
764                         }
765                 }
766         } else {
767                 INIT_MINMAX(cd.dmin, cd.dmax);
768                         
769                 for(a = 0; a < numVerts; a++) {
770                         Mat4MulVecfl(cd.curvespace, vertexCos[a]);
771                         DO_MINMAX(vertexCos[a], cd.dmin, cd.dmax);
772                 }
773
774                 for(a = 0; a < numVerts; a++) {
775                         calc_curve_deform(scene, cuOb, vertexCos[a], defaxis, &cd, NULL);
776                         Mat4MulVecfl(cd.objectspace, vertexCos[a]);
777                 }
778         }
779         cu->flag = flag;
780 }
781
782 /* input vec and orco = local coord in armature space */
783 /* orco is original not-animated or deformed reference point */
784 /* result written in vec and mat */
785 void curve_deform_vector(Scene *scene, Object *cuOb, Object *target, float *orco, float *vec, float mat[][3], int no_rot_axis)
786 {
787         CurveDeform cd;
788         float quat[4];
789         
790         if(cuOb->type != OB_CURVE) {
791                 Mat3One(mat);
792                 return;
793         }
794
795         init_curve_deform(cuOb, target, &cd, 0);        /* 0 no dloc */
796         cd.no_rot_axis= no_rot_axis;                            /* option to only rotate for XY, for example */
797         
798         VECCOPY(cd.dmin, orco);
799         VECCOPY(cd.dmax, orco);
800
801         Mat4MulVecfl(cd.curvespace, vec);
802         
803         if(calc_curve_deform(scene, cuOb, vec, target->trackflag+1, &cd, quat)) {
804                 float qmat[3][3];
805                 
806                 QuatToMat3(quat, qmat);
807                 Mat3MulMat3(mat, qmat, cd.objectspace3);
808         }
809         else
810                 Mat3One(mat);
811         
812         Mat4MulVecfl(cd.objectspace, vec);
813
814 }
815
816 void lattice_deform_verts(Object *laOb, Object *target, DerivedMesh *dm,
817                           float (*vertexCos)[3], int numVerts, char *vgroup)
818 {
819         int a;
820         int use_vgroups;
821
822         if(laOb->type != OB_LATTICE)
823                 return;
824
825         init_latt_deform(laOb, target);
826
827         /* check whether to use vertex groups (only possible if target is a Mesh)
828          * we want either a Mesh with no derived data, or derived data with
829          * deformverts
830          */
831         if(target && target->type==OB_MESH) {
832                 /* if there's derived data without deformverts, don't use vgroups */
833                 if(dm && !dm->getVertData(dm, 0, CD_MDEFORMVERT))
834                         use_vgroups = 0;
835                 else
836                         use_vgroups = 1;
837         } else
838                 use_vgroups = 0;
839         
840         if(vgroup && vgroup[0] && use_vgroups) {
841                 bDeformGroup *curdef;
842                 Mesh *me = target->data;
843                 int index = 0;
844                 
845                 /* find the group (weak loop-in-loop) */
846                 for(curdef = target->defbase.first; curdef;
847                     curdef = curdef->next, index++)
848                         if(!strcmp(curdef->name, vgroup)) break;
849
850                 if(curdef && (me->dvert || dm)) {
851                         MDeformVert *dvert = me->dvert;
852                         int j;
853                         
854                         for(a = 0; a < numVerts; a++, dvert++) {
855                                 if(dm) dvert = dm->getVertData(dm, a, CD_MDEFORMVERT);
856                                 for(j = 0; j < dvert->totweight; j++) {
857                                         if (dvert->dw[j].def_nr == index) {
858                                                 calc_latt_deform(laOb, vertexCos[a], dvert->dw[j].weight);
859                                         }
860                                 }
861                         }
862                 }
863         } else {
864                 for(a = 0; a < numVerts; a++) {
865                         calc_latt_deform(laOb, vertexCos[a], 1.0f);
866                 }
867         }
868         end_latt_deform(laOb);
869 }
870
871 int object_deform_mball(Object *ob)
872 {
873         if(ob->parent && ob->parent->type==OB_LATTICE && ob->partype==PARSKEL) {
874                 DispList *dl;
875
876                 for (dl=ob->disp.first; dl; dl=dl->next) {
877                         lattice_deform_verts(ob->parent, ob, NULL,
878                                              (float(*)[3]) dl->verts, dl->nr, NULL);
879                 }
880
881                 return 1;
882         } else {
883                 return 0;
884         }
885 }
886
887 static BPoint *latt_bp(Lattice *lt, int u, int v, int w)
888 {
889         return lt->def+ u + v*lt->pntsu + w*lt->pntsu*lt->pntsv;
890 }
891
892 void outside_lattice(Lattice *lt)
893 {
894         BPoint *bp, *bp1, *bp2;
895         int u, v, w;
896         float fac1, du=0.0, dv=0.0, dw=0.0;
897
898         if(lt->flag & LT_OUTSIDE) {
899                 bp= lt->def;
900
901                 if(lt->pntsu>1) du= 1.0f/((float)lt->pntsu-1);
902                 if(lt->pntsv>1) dv= 1.0f/((float)lt->pntsv-1);
903                 if(lt->pntsw>1) dw= 1.0f/((float)lt->pntsw-1);
904                         
905                 for(w=0; w<lt->pntsw; w++) {
906                         
907                         for(v=0; v<lt->pntsv; v++) {
908                         
909                                 for(u=0; u<lt->pntsu; u++, bp++) {
910                                         if(u==0 || v==0 || w==0 || u==lt->pntsu-1 || v==lt->pntsv-1 || w==lt->pntsw-1);
911                                         else {
912                                         
913                                                 bp->hide= 1;
914                                                 bp->f1 &= ~SELECT;
915                                                 
916                                                 /* u extrema */
917                                                 bp1= latt_bp(lt, 0, v, w);
918                                                 bp2= latt_bp(lt, lt->pntsu-1, v, w);
919                                                 
920                                                 fac1= du*u;
921                                                 bp->vec[0]= (1.0f-fac1)*bp1->vec[0] + fac1*bp2->vec[0];
922                                                 bp->vec[1]= (1.0f-fac1)*bp1->vec[1] + fac1*bp2->vec[1];
923                                                 bp->vec[2]= (1.0f-fac1)*bp1->vec[2] + fac1*bp2->vec[2];
924                                                 
925                                                 /* v extrema */
926                                                 bp1= latt_bp(lt, u, 0, w);
927                                                 bp2= latt_bp(lt, u, lt->pntsv-1, w);
928                                                 
929                                                 fac1= dv*v;
930                                                 bp->vec[0]+= (1.0f-fac1)*bp1->vec[0] + fac1*bp2->vec[0];
931                                                 bp->vec[1]+= (1.0f-fac1)*bp1->vec[1] + fac1*bp2->vec[1];
932                                                 bp->vec[2]+= (1.0f-fac1)*bp1->vec[2] + fac1*bp2->vec[2];
933                                                 
934                                                 /* w extrema */
935                                                 bp1= latt_bp(lt, u, v, 0);
936                                                 bp2= latt_bp(lt, u, v, lt->pntsw-1);
937                                                 
938                                                 fac1= dw*w;
939                                                 bp->vec[0]+= (1.0f-fac1)*bp1->vec[0] + fac1*bp2->vec[0];
940                                                 bp->vec[1]+= (1.0f-fac1)*bp1->vec[1] + fac1*bp2->vec[1];
941                                                 bp->vec[2]+= (1.0f-fac1)*bp1->vec[2] + fac1*bp2->vec[2];
942                                                 
943                                                 VecMulf(bp->vec, 0.3333333f);
944                                                 
945                                         }
946                                 }
947                                 
948                         }
949                         
950                 }
951         }
952         else {
953                 bp= lt->def;
954
955                 for(w=0; w<lt->pntsw; w++)
956                         for(v=0; v<lt->pntsv; v++)
957                                 for(u=0; u<lt->pntsu; u++, bp++)
958                                         bp->hide= 0;
959         }
960 }
961
962 float (*lattice_getVertexCos(struct Object *ob, int *numVerts_r))[3]
963 {
964         Lattice *lt = ob->data;
965         int i, numVerts;
966         float (*vertexCos)[3];
967
968         if(lt->editlatt) lt= lt->editlatt;
969         numVerts = *numVerts_r = lt->pntsu*lt->pntsv*lt->pntsw;
970         
971         vertexCos = MEM_mallocN(sizeof(*vertexCos)*numVerts,"lt_vcos");
972         
973         for (i=0; i<numVerts; i++) {
974                 VECCOPY(vertexCos[i], lt->def[i].vec);
975         }
976
977         return vertexCos;
978 }
979
980 void lattice_applyVertexCos(struct Object *ob, float (*vertexCos)[3])
981 {
982         Lattice *lt = ob->data;
983         int i, numVerts = lt->pntsu*lt->pntsv*lt->pntsw;
984
985         for (i=0; i<numVerts; i++) {
986                 VECCOPY(lt->def[i].vec, vertexCos[i]);
987         }
988 }
989
990 void lattice_calc_modifiers(Scene *scene, Object *ob)
991 {
992         Lattice *lt= ob->data;
993         ModifierData *md = modifiers_getVirtualModifierList(ob);
994         float (*vertexCos)[3] = NULL;
995         int numVerts, editmode = (lt->editlatt!=NULL);
996
997         freedisplist(&ob->disp);
998
999         if (!editmode) {
1000                 do_ob_key(scene, ob);
1001         }
1002
1003         for (; md; md=md->next) {
1004                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1005
1006                 md->scene= scene;
1007                 
1008                 if (!(md->mode&eModifierMode_Realtime)) continue;
1009                 if (editmode && !(md->mode&eModifierMode_Editmode)) continue;
1010                 if (mti->isDisabled && mti->isDisabled(md)) continue;
1011                 if (mti->type!=eModifierTypeType_OnlyDeform) continue;
1012
1013                 if (!vertexCos) vertexCos = lattice_getVertexCos(ob, &numVerts);
1014                 mti->deformVerts(md, ob, NULL, vertexCos, numVerts, 0, 0);
1015         }
1016
1017         /* always displist to make this work like derivedmesh */
1018         if (!vertexCos) vertexCos = lattice_getVertexCos(ob, &numVerts);
1019         
1020         {
1021                 DispList *dl = MEM_callocN(sizeof(*dl), "lt_dl");
1022                 dl->type = DL_VERTS;
1023                 dl->parts = 1;
1024                 dl->nr = numVerts;
1025                 dl->verts = (float*) vertexCos;
1026                 
1027                 BLI_addtail(&ob->disp, dl);
1028         }
1029 }
1030
1031 struct MDeformVert* lattice_get_deform_verts(struct Object *oblatt)
1032 {
1033         if(oblatt->type == OB_LATTICE)
1034         {
1035                 Lattice *lt = (Lattice*)oblatt->data;
1036                 if(lt->editlatt) lt= lt->editlatt;
1037                 return lt->dvert;
1038         }
1039
1040         return NULL;    
1041 }
1042