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