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