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