I made multitude of fixes based on the comments provided online:
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 /** \file blender/blenkernel/intern/lattice.c
34  *  \ingroup bke
35  */
36
37
38
39 #include <stdio.h>
40 #include <string.h>
41 #include <math.h>
42 #include <stdlib.h>
43
44 #include "MEM_guardedalloc.h"
45
46 #include "BLI_blenlib.h"
47 #include "BLI_math.h"
48 #include "BLI_utildefines.h"
49
50 #include "DNA_mesh_types.h"
51 #include "DNA_meshdata_types.h"
52 #include "DNA_scene_types.h"
53 #include "DNA_object_types.h"
54 #include "DNA_lattice_types.h"
55 #include "DNA_curve_types.h"
56 #include "DNA_key_types.h"
57
58 #include "BKE_animsys.h"
59 #include "BKE_anim.h"
60 #include "BKE_cdderivedmesh.h"
61 #include "BKE_displist.h"
62 #include "BKE_global.h"
63 #include "BKE_key.h"
64 #include "BKE_lattice.h"
65 #include "BKE_library.h"
66 #include "BKE_main.h"
67 #include "BKE_mesh.h"
68 #include "BKE_modifier.h"
69
70 #include "BKE_deform.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                 copy_m4_m4(mat, ltOb->obmat);
159                 unit_m4(ltOb->obmat);
160                 lattice_deform_verts(ltOb, NULL, NULL, vertexCos, uNew*vNew*wNew, NULL);
161                 copy_m4_m4(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                 copy_v3_v3(bp->vec, vertexCos[i]);
186         }
187
188         MEM_freeN(vertexCos);
189 }
190
191 Lattice *add_lattice(const 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         ltn->key= copy_key(ltn->key);
215         if(ltn->key) ltn->key->from= (ID *)ltn;
216         
217         if(lt->dvert) {
218                 int tot= lt->pntsu*lt->pntsv*lt->pntsw;
219                 ltn->dvert = MEM_mallocN (sizeof (MDeformVert)*tot, "Lattice MDeformVert");
220                 copy_dverts(ltn->dvert, lt->dvert, tot);
221         }
222
223         ltn->editlatt= NULL;
224
225         return ltn;
226 }
227
228 void free_lattice(Lattice *lt)
229 {
230         if(lt->def) MEM_freeN(lt->def);
231         if(lt->dvert) free_dverts(lt->dvert, lt->pntsu*lt->pntsv*lt->pntsw);
232         if(lt->editlatt) {
233                 Lattice *editlt= lt->editlatt->latt;
234
235                 if(editlt->def) MEM_freeN(editlt->def);
236                 if(editlt->dvert) free_dverts(editlt->dvert, lt->pntsu*lt->pntsv*lt->pntsw);
237
238                 MEM_freeN(editlt);
239                 MEM_freeN(lt->editlatt);
240         }
241         
242         /* free animation data */
243         if (lt->adt) {
244                 BKE_free_animdata(&lt->id);
245                 lt->adt= NULL;
246         }
247 }
248
249
250 void make_local_lattice(Lattice *lt)
251 {
252         Main *bmain= G.main;
253         Object *ob;
254         int local=0, lib=0;
255
256         /* - only lib users: do nothing
257          * - only local users: set flag
258          * - mixed: make copy
259          */
260         
261         if(lt->id.lib==NULL) return;
262         if(lt->id.us==1) {
263                 lt->id.lib= NULL;
264                 lt->id.flag= LIB_LOCAL;
265                 new_id(&bmain->latt, (ID *)lt, NULL);
266                 return;
267         }
268         
269         for(ob= bmain->object.first; ob && ELEM(0, lib, local); ob= ob->id.next) {
270                 if(ob->data==lt) {
271                         if(ob->id.lib) lib= 1;
272                         else local= 1;
273                 }
274         }
275         
276         if(local && lib==0) {
277                 lt->id.lib= NULL;
278                 lt->id.flag= LIB_LOCAL;
279                 new_id(&bmain->latt, (ID *)lt, NULL);
280         }
281         else if(local && lib) {
282                 Lattice *ltn= copy_lattice(lt);
283                 ltn->id.us= 0;
284
285                 for(ob= bmain->object.first; ob; ob= ob->id.next) {
286                         if(ob->data==lt) {
287                                 if(ob->id.lib==NULL) {
288                                         ob->data= ltn;
289                                         ltn->id.us++;
290                                         lt->id.us--;
291                                 }
292                         }
293                 }
294         }
295 }
296
297 void init_latt_deform(Object *oblatt, Object *ob)
298 {
299                 /* we make an array with all differences */
300         Lattice *lt= oblatt->data;
301         BPoint *bp;
302         DispList *dl = find_displist(&oblatt->disp, DL_VERTS);
303         float *co = dl?dl->verts:NULL;
304         float *fp, imat[4][4];
305         float fu, fv, fw;
306         int u, v, w;
307
308         if(lt->editlatt) lt= lt->editlatt->latt;
309         bp = lt->def;
310         
311         fp= lt->latticedata= MEM_mallocN(sizeof(float)*3*lt->pntsu*lt->pntsv*lt->pntsw, "latticedata");
312         
313                 /* for example with a particle system: ob==0 */
314         if(ob==NULL) {
315                 /* in deformspace, calc matrix  */
316                 invert_m4_m4(lt->latmat, oblatt->obmat);
317         
318                 /* back: put in deform array */
319                 invert_m4_m4(imat, lt->latmat);
320         }
321         else {
322                 /* in deformspace, calc matrix */
323                 invert_m4_m4(imat, oblatt->obmat);
324                 mul_m4_m4m4(lt->latmat, ob->obmat, imat);
325         
326                 /* back: put in deform array */
327                 invert_m4_m4(imat, lt->latmat);
328         }
329         
330         for(w=0,fw=lt->fw; w<lt->pntsw; w++,fw+=lt->dw) {
331                 for(v=0,fv=lt->fv; v<lt->pntsv; v++, fv+=lt->dv) {
332                         for(u=0,fu=lt->fu; u<lt->pntsu; u++, bp++, co+=3, fp+=3, fu+=lt->du) {
333                                 if (dl) {
334                                         fp[0] = co[0] - fu;
335                                         fp[1] = co[1] - fv;
336                                         fp[2] = co[2] - fw;
337                                 } else {
338                                         fp[0] = bp->vec[0] - fu;
339                                         fp[1] = bp->vec[1] - fv;
340                                         fp[2] = bp->vec[2] - fw;
341                                 }
342
343                                 mul_mat3_m4_v3(imat, fp);
344                         }
345                 }
346         }
347 }
348
349 void calc_latt_deform(Object *ob, float *co, float weight)
350 {
351         Lattice *lt= ob->data;
352         float u, v, w, tu[4], tv[4], tw[4];
353         float vec[3];
354         int idx_w, idx_v, idx_u;
355         int ui, vi, wi, uu, vv, ww;
356
357         /* vgroup influence */
358         int defgroup_nr= -1;
359         float co_prev[3], weight_blend= 0.0f;
360         MDeformVert *dvert= lattice_get_deform_verts(ob);
361
362
363         if(lt->editlatt) lt= lt->editlatt->latt;
364         if(lt->latticedata==NULL) return;
365
366         if(lt->vgroup[0] && dvert) {
367                 defgroup_nr= defgroup_name_index(ob, lt->vgroup);
368                 copy_v3_v3(co_prev, co);
369         }
370
371         /* co is in local coords, treat with latmat */
372         mul_v3_m4v3(vec, lt->latmat, co);
373
374         /* u v w coords */
375
376         if(lt->pntsu>1) {
377                 u= (vec[0]-lt->fu)/lt->du;
378                 ui= (int)floor(u);
379                 u -= ui;
380                 key_curve_position_weights(u, tu, lt->typeu);
381         }
382         else {
383                 tu[0]= tu[2]= tu[3]= 0.0; tu[1]= 1.0;
384                 ui= 0;
385         }
386
387         if(lt->pntsv>1) {
388                 v= (vec[1]-lt->fv)/lt->dv;
389                 vi= (int)floor(v);
390                 v -= vi;
391                 key_curve_position_weights(v, tv, lt->typev);
392         }
393         else {
394                 tv[0]= tv[2]= tv[3]= 0.0; tv[1]= 1.0;
395                 vi= 0;
396         }
397
398         if(lt->pntsw>1) {
399                 w= (vec[2]-lt->fw)/lt->dw;
400                 wi= (int)floor(w);
401                 w -= wi;
402                 key_curve_position_weights(w, tw, lt->typew);
403         }
404         else {
405                 tw[0]= tw[2]= tw[3]= 0.0; tw[1]= 1.0;
406                 wi= 0;
407         }
408
409         for(ww= wi-1; ww<=wi+2; ww++) {
410                 w= tw[ww-wi+1];
411
412                 if(w != 0.0f) {
413                         if(ww>0) {
414                                 if(ww<lt->pntsw) idx_w= ww*lt->pntsu*lt->pntsv;
415                                 else idx_w= (lt->pntsw-1)*lt->pntsu*lt->pntsv;
416                         }
417                         else idx_w= 0;
418
419                         for(vv= vi-1; vv<=vi+2; vv++) {
420                                 v= w*tv[vv-vi+1];
421
422                                 if(v != 0.0f) {
423                                         if(vv>0) {
424                                                 if(vv<lt->pntsv) idx_v= idx_w + vv*lt->pntsu;
425                                                 else idx_v= idx_w + (lt->pntsv-1)*lt->pntsu;
426                                         }
427                                         else idx_v= idx_w;
428
429                                         for(uu= ui-1; uu<=ui+2; uu++) {
430                                                 u= weight*v*tu[uu-ui+1];
431
432                                                 if(u != 0.0f) {
433                                                         if(uu>0) {
434                                                                 if(uu<lt->pntsu) idx_u= idx_v + uu;
435                                                                 else idx_u= idx_v + (lt->pntsu-1);
436                                                         }
437                                                         else idx_u= idx_v;
438
439                                                         madd_v3_v3fl(co, &lt->latticedata[idx_u * 3], u);
440
441                                                         if(defgroup_nr != -1)
442                                                                 weight_blend += (u * defvert_find_weight(dvert + idx_u, defgroup_nr));
443                                                 }
444                                         }
445                                 }
446                         }
447                 }
448         }
449
450         if(defgroup_nr != -1)
451                 interp_v3_v3v3(co, co_prev, co, weight_blend);
452
453 }
454
455 void end_latt_deform(Object *ob)
456 {
457         Lattice *lt= ob->data;
458         
459         if(lt->editlatt) lt= lt->editlatt->latt;
460         
461         if(lt->latticedata)
462                 MEM_freeN(lt->latticedata);
463         lt->latticedata= NULL;
464 }
465
466         /* calculations is in local space of deformed object
467            so we store in latmat transform from path coord inside object 
468          */
469 typedef struct {
470         float dmin[3], dmax[3], dsize, dloc[3];
471         float curvespace[4][4], objectspace[4][4], objectspace3[3][3];
472         int no_rot_axis;
473 } CurveDeform;
474
475 static void init_curve_deform(Object *par, Object *ob, CurveDeform *cd, int dloc)
476 {
477         invert_m4_m4(ob->imat, ob->obmat);
478         mul_m4_m4m4(cd->objectspace, par->obmat, ob->imat);
479         invert_m4_m4(cd->curvespace, cd->objectspace);
480         copy_m3_m4(cd->objectspace3, cd->objectspace);
481         
482         // offset vector for 'no smear'
483         if(dloc) {
484                 invert_m4_m4(par->imat, par->obmat);
485                 mul_v3_m4v3(cd->dloc, par->imat, ob->obmat[3]);
486         }
487         else {
488                 cd->dloc[0]=cd->dloc[1]=cd->dloc[2]= 0.0f;
489         }
490         
491         cd->no_rot_axis= 0;
492 }
493
494 /* this makes sure we can extend for non-cyclic. *vec needs 4 items! */
495 static int where_on_path_deform(Object *ob, float ctime, float *vec, float *dir, float *quat, float *radius)    /* returns OK */
496 {
497         Curve *cu= ob->data;
498         BevList *bl;
499         float ctime1;
500         int cycl=0;
501         
502         /* test for cyclic */
503         bl= cu->bev.first;
504         if (!bl->nr) return 0;
505         if(bl && bl->poly> -1) cycl= 1;
506
507         if(cycl==0) {
508                 ctime1= CLAMPIS(ctime, 0.0f, 1.0f);
509         }
510         else ctime1= ctime;
511         
512         /* vec needs 4 items */
513         if(where_on_path(ob, ctime1, vec, dir, quat, radius, NULL)) {
514                 
515                 if(cycl==0) {
516                         Path *path= cu->path;
517                         float dvec[3];
518                         
519                         if(ctime < 0.0f) {
520                                 sub_v3_v3v3(dvec, path->data[1].vec, path->data[0].vec);
521                                 mul_v3_fl(dvec, ctime*(float)path->len);
522                                 add_v3_v3(vec, dvec);
523                                 if(quat) copy_qt_qt(quat, path->data[0].quat);
524                                 if(radius) *radius= path->data[0].radius;
525                         }
526                         else if(ctime > 1.0f) {
527                                 sub_v3_v3v3(dvec, path->data[path->len-1].vec, path->data[path->len-2].vec);
528                                 mul_v3_fl(dvec, (ctime-1.0f)*(float)path->len);
529                                 add_v3_v3(vec, dvec);
530                                 if(quat) copy_qt_qt(quat, path->data[path->len-1].quat);
531                                 if(radius) *radius= path->data[path->len-1].radius;
532                                 /* weight - not used but could be added */
533                         }
534                 }
535                 return 1;
536         }
537         return 0;
538 }
539
540         /* for each point, rotate & translate to curve */
541         /* use path, since it has constant distances */
542         /* co: local coord, result local too */
543         /* returns quaternion for rotation, using cd->no_rot_axis */
544         /* axis is using another define!!! */
545 static int calc_curve_deform(Scene *scene, Object *par, float *co, short axis, CurveDeform *cd, float *quatp)
546 {
547         Curve *cu= par->data;
548         float fac, loc[4], dir[3], new_quat[4], radius;
549         short /*upflag, */ index;
550
551         index= axis-1;
552         if(index>2)
553                 index -= 3; /* negative  */
554
555         /* to be sure, mostly after file load */
556         if(cu->path==NULL) {
557                 makeDispListCurveTypes(scene, par, 0);
558                 if(cu->path==NULL) return 0;    // happens on append...
559         }
560         
561         /* options */
562         if(ELEM3(axis, OB_NEGX+1, OB_NEGY+1, OB_NEGZ+1)) { /* OB_NEG# 0-5, MOD_CURVE_POS# 1-6 */
563                 if(cu->flag & CU_STRETCH)
564                         fac= (-co[index]-cd->dmax[index])/(cd->dmax[index] - cd->dmin[index]);
565                 else
566                         fac= (cd->dloc[index])/(cu->path->totdist) - (co[index]-cd->dmax[index])/(cu->path->totdist);
567         }
568         else {
569                 if(cu->flag & CU_STRETCH)
570                         fac= (co[index]-cd->dmin[index])/(cd->dmax[index] - cd->dmin[index]);
571                 else
572                         fac= (cd->dloc[index])/(cu->path->totdist) + (co[index]-cd->dmin[index])/(cu->path->totdist);
573         }
574         
575 #if 0 // XXX old animation system
576         /* we want the ipo to work on the default 100 frame range, because there's no  
577            actual time involved in path position */
578         // huh? by WHY!!!!???? - Aligorith
579         if(cu->ipo) {
580                 fac*= 100.0f;
581                 if(calc_ipo_spec(cu->ipo, CU_SPEED, &fac)==0)
582                         fac/= 100.0;
583         }
584 #endif // XXX old animation system
585         
586         if( where_on_path_deform(par, fac, loc, dir, new_quat, &radius)) {      /* returns OK */
587                 float quat[4], cent[3];
588
589 #if 0   // XXX - 2.4x Z-Up, Now use bevel tilt.
590                 if(cd->no_rot_axis)     /* set by caller */
591                         dir[cd->no_rot_axis-1]= 0.0f;
592                 
593                 /* -1 for compatibility with old track defines */
594                 vec_to_quat( quat,dir, axis-1, upflag);
595                 
596                 /* the tilt */
597                 if(loc[3]!=0.0) {
598                         normalize_v3(dir);
599                         q[0]= (float)cos(0.5*loc[3]);
600                         fac= (float)sin(0.5*loc[3]);
601                         q[1]= -fac*dir[0];
602                         q[2]= -fac*dir[1];
603                         q[3]= -fac*dir[2];
604                         mul_qt_qtqt(quat, q, quat);
605                 }
606 #endif
607
608                 if(cd->no_rot_axis) {   /* set by caller */
609
610                         /* this is not exactly the same as 2.4x, since the axis is having rotation removed rather than
611                          * changing the axis before calculating the tilt but serves much the same purpose */
612                         float dir_flat[3]={0,0,0}, q[4];
613                         copy_v3_v3(dir_flat, dir);
614                         dir_flat[cd->no_rot_axis-1]= 0.0f;
615
616                         normalize_v3(dir);
617                         normalize_v3(dir_flat);
618
619                         rotation_between_vecs_to_quat(q, dir, dir_flat); /* Could this be done faster? */
620
621                         mul_qt_qtqt(new_quat, q, new_quat);
622                 }
623
624
625                 /* Logic for 'cent' orientation *
626                  *
627                  * The way 'co' is copied to 'cent' may seem to have no meaning, but it does.
628                  *
629                  * Use a curve modifier to stretch a cube out, color each side RGB, positive side light, negative dark.
630                  * view with X up (default), from the angle that you can see 3 faces RGB colors (light), anti-clockwise
631                  * Notice X,Y,Z Up all have light colors and each ordered CCW.
632                  *
633                  * Now for Neg Up XYZ, the colors are all dark, and ordered clockwise - Campbell
634                  *
635                  * note: moved functions into quat_apply_track/vec_apply_track
636                  * */
637                 copy_qt_qt(quat, new_quat);
638                 copy_v3_v3(cent, co);
639
640                 /* zero the axis which is not used,
641                  * the big block of text above now applies to these 3 lines */
642                 quat_apply_track(quat, axis-1, (axis==1 || axis==3) ? 1:0); /* up flag is a dummy, set so no rotation is done */
643                 vec_apply_track(cent, axis-1);
644                 cent[axis < 4 ? axis-1 : axis-4]= 0.0f;
645
646
647                 /* scale if enabled */
648                 if(cu->flag & CU_PATH_RADIUS)
649                         mul_v3_fl(cent, radius);
650                 
651                 /* local rotation */
652                 normalize_qt(quat);
653                 mul_qt_v3(quat, cent);
654
655                 /* translation */
656                 add_v3_v3v3(co, cent, loc);
657
658                 if(quatp)
659                         copy_qt_qt(quatp, quat);
660
661                 return 1;
662         }
663         return 0;
664 }
665
666 void curve_deform_verts(Scene *scene, Object *cuOb, Object *target, DerivedMesh *dm, float (*vertexCos)[3], int numVerts, char *vgroup, short defaxis)
667 {
668         Curve *cu;
669         int a, flag;
670         CurveDeform cd;
671         int use_vgroups;
672
673         if(cuOb->type != OB_CURVE)
674                 return;
675
676         cu = cuOb->data;
677         flag = cu->flag;
678         cu->flag |= (CU_PATH|CU_FOLLOW); // needed for path & bevlist
679
680         init_curve_deform(cuOb, target, &cd, (cu->flag & CU_STRETCH)==0);
681
682         /* dummy bounds, keep if CU_DEFORM_BOUNDS_OFF is set */
683         if(defaxis < 3) {
684                 cd.dmin[0]= cd.dmin[1]= cd.dmin[2]= 0.0f;
685                 cd.dmax[0]= cd.dmax[1]= cd.dmax[2]= 1.0f;
686         }
687         else {
688                 /* negative, these bounds give a good rest position */
689                 cd.dmin[0]= cd.dmin[1]= cd.dmin[2]= -1.0f;
690                 cd.dmax[0]= cd.dmax[1]= cd.dmax[2]=  0.0f;
691         }
692         
693         /* check whether to use vertex groups (only possible if target is a Mesh)
694          * we want either a Mesh with no derived data, or derived data with
695          * deformverts
696          */
697         if(target && target->type==OB_MESH) {
698                 /* if there's derived data without deformverts, don't use vgroups */
699                 if(dm && !dm->getVertData(dm, 0, CD_MDEFORMVERT))
700                         use_vgroups = 0;
701                 else
702                         use_vgroups = 1;
703         } else
704                 use_vgroups = 0;
705         
706         if(vgroup && vgroup[0] && use_vgroups) {
707                 Mesh *me= target->data;
708                 int index= defgroup_name_index(target, vgroup);
709
710                 if(index != -1 && (me->dvert || dm)) {
711                         MDeformVert *dvert = me->dvert;
712                         float vec[3];
713                         float weight;
714         
715
716                         if(cu->flag & CU_DEFORM_BOUNDS_OFF) {
717                                 /* dummy bounds */
718                                 cd.dmin[0]= cd.dmin[1]= cd.dmin[2]= 0.0f;
719                                 cd.dmax[0]= cd.dmax[1]= cd.dmax[2]= 1.0f;
720                                 
721                                 dvert = me->dvert;
722                                 for(a = 0; a < numVerts; a++, dvert++) {
723                                         if(dm) dvert = dm->getVertData(dm, a, CD_MDEFORMVERT);
724                                         weight= defvert_find_weight(dvert, index);
725         
726                                         if(weight > 0.0f) {
727                                                 mul_m4_v3(cd.curvespace, vertexCos[a]);
728                                                 copy_v3_v3(vec, vertexCos[a]);
729                                                 calc_curve_deform(scene, cuOb, vec, defaxis, &cd, NULL);
730                                                 interp_v3_v3v3(vertexCos[a], vertexCos[a], vec, weight);
731                                                 mul_m4_v3(cd.objectspace, vertexCos[a]);
732                                         }
733                                 }
734                         }
735                         else {
736                                 /* set mesh min/max bounds */
737                                 INIT_MINMAX(cd.dmin, cd.dmax);
738         
739                                 for(a = 0; a < numVerts; a++, dvert++) {
740                                         if(dm) dvert = dm->getVertData(dm, a, CD_MDEFORMVERT);
741                                         
742                                         if(defvert_find_weight(dvert, index) > 0.0f) {
743                                                 mul_m4_v3(cd.curvespace, vertexCos[a]);
744                                                 DO_MINMAX(vertexCos[a], cd.dmin, cd.dmax);
745                                         }
746                                 }
747         
748                                 dvert = me->dvert;
749                                 for(a = 0; a < numVerts; a++, dvert++) {
750                                         if(dm) dvert = dm->getVertData(dm, a, CD_MDEFORMVERT);
751                                         
752                                         weight= defvert_find_weight(dvert, index);
753         
754                                         if(weight > 0.0f) {
755                                                 copy_v3_v3(vec, vertexCos[a]);
756                                                 calc_curve_deform(scene, cuOb, vec, defaxis, &cd, NULL);
757                                                 interp_v3_v3v3(vertexCos[a], vertexCos[a], vec, weight);
758                                                 mul_m4_v3(cd.objectspace, vertexCos[a]);
759                                         }
760                                 }
761                         }
762                 }
763         }
764         else {
765                 if(cu->flag & CU_DEFORM_BOUNDS_OFF) {
766                         for(a = 0; a < numVerts; a++) {
767                                 mul_m4_v3(cd.curvespace, vertexCos[a]);
768                                 calc_curve_deform(scene, cuOb, vertexCos[a], defaxis, &cd, NULL);
769                                 mul_m4_v3(cd.objectspace, vertexCos[a]);
770                         }
771                 }
772                 else {
773                         /* set mesh min max bounds */
774                         INIT_MINMAX(cd.dmin, cd.dmax);
775                                 
776                         for(a = 0; a < numVerts; a++) {
777                                 mul_m4_v3(cd.curvespace, vertexCos[a]);
778                                 DO_MINMAX(vertexCos[a], cd.dmin, cd.dmax);
779                         }
780         
781                         for(a = 0; a < numVerts; a++) {
782                                 calc_curve_deform(scene, cuOb, vertexCos[a], defaxis, &cd, NULL);
783                                 mul_m4_v3(cd.objectspace, vertexCos[a]);
784                         }
785                 }
786         }
787         cu->flag = flag;
788 }
789
790 /* input vec and orco = local coord in armature space */
791 /* orco is original not-animated or deformed reference point */
792 /* result written in vec and mat */
793 void curve_deform_vector(Scene *scene, Object *cuOb, Object *target, float *orco, float *vec, float mat[][3], int no_rot_axis)
794 {
795         CurveDeform cd;
796         float quat[4];
797         
798         if(cuOb->type != OB_CURVE) {
799                 unit_m3(mat);
800                 return;
801         }
802
803         init_curve_deform(cuOb, target, &cd, 0);        /* 0 no dloc */
804         cd.no_rot_axis= no_rot_axis;                            /* option to only rotate for XY, for example */
805         
806         copy_v3_v3(cd.dmin, orco);
807         copy_v3_v3(cd.dmax, orco);
808
809         mul_m4_v3(cd.curvespace, vec);
810         
811         if(calc_curve_deform(scene, cuOb, vec, target->trackflag+1, &cd, quat)) {
812                 float qmat[3][3];
813                 
814                 quat_to_mat3( qmat,quat);
815                 mul_m3_m3m3(mat, qmat, cd.objectspace3);
816         }
817         else
818                 unit_m3(mat);
819         
820         mul_m4_v3(cd.objectspace, vec);
821
822 }
823
824 void lattice_deform_verts(Object *laOb, Object *target, DerivedMesh *dm,
825                                                   float (*vertexCos)[3], int numVerts, char *vgroup)
826 {
827         int a;
828         int use_vgroups;
829
830         if(laOb->type != OB_LATTICE)
831                 return;
832
833         init_latt_deform(laOb, target);
834
835         /* check whether to use vertex groups (only possible if target is a Mesh)
836          * we want either a Mesh with no derived data, or derived data with
837          * deformverts
838          */
839         if(target && target->type==OB_MESH) {
840                 /* if there's derived data without deformverts, don't use vgroups */
841                 if(dm && !dm->getVertData(dm, 0, CD_MDEFORMVERT))
842                         use_vgroups = 0;
843                 else
844                         use_vgroups = 1;
845         } else
846                 use_vgroups = 0;
847         
848         if(vgroup && vgroup[0] && use_vgroups) {
849                 Mesh *me = target->data;
850                 int index = defgroup_name_index(target, vgroup);
851                 float weight;
852
853                 if(index >= 0 && (me->dvert || dm)) {
854                         MDeformVert *dvert = me->dvert;
855                         
856                         for(a = 0; a < numVerts; a++, dvert++) {
857                                 if(dm) dvert = dm->getVertData(dm, a, CD_MDEFORMVERT);
858
859                                 weight= defvert_find_weight(dvert, index);
860
861                                 if(weight > 0.0f)
862                                         calc_latt_deform(laOb, vertexCos[a], weight);
863                         }
864                 }
865         } else {
866                 for(a = 0; a < numVerts; a++) {
867                         calc_latt_deform(laOb, vertexCos[a], 1.0f);
868                 }
869         }
870         end_latt_deform(laOb);
871 }
872
873 int object_deform_mball(Object *ob, ListBase *dispbase)
874 {
875         if(ob->parent && ob->parent->type==OB_LATTICE && ob->partype==PARSKEL) {
876                 DispList *dl;
877
878                 for (dl=dispbase->first; dl; dl=dl->next) {
879                         lattice_deform_verts(ob->parent, ob, NULL,
880                                                                  (float(*)[3]) dl->verts, dl->nr, NULL);
881                 }
882
883                 return 1;
884         } else {
885                 return 0;
886         }
887 }
888
889 static BPoint *latt_bp(Lattice *lt, int u, int v, int w)
890 {
891         return lt->def+ u + v*lt->pntsu + w*lt->pntsu*lt->pntsv;
892 }
893
894 void outside_lattice(Lattice *lt)
895 {
896         BPoint *bp, *bp1, *bp2;
897         int u, v, w;
898         float fac1, du=0.0, dv=0.0, dw=0.0;
899
900         if(lt->flag & LT_OUTSIDE) {
901                 bp= lt->def;
902
903                 if(lt->pntsu>1) du= 1.0f/((float)lt->pntsu-1);
904                 if(lt->pntsv>1) dv= 1.0f/((float)lt->pntsv-1);
905                 if(lt->pntsw>1) dw= 1.0f/((float)lt->pntsw-1);
906                         
907                 for(w=0; w<lt->pntsw; w++) {
908                         
909                         for(v=0; v<lt->pntsv; v++) {
910                         
911                                 for(u=0; u<lt->pntsu; u++, bp++) {
912                                         if(u==0 || v==0 || w==0 || u==lt->pntsu-1 || v==lt->pntsv-1 || w==lt->pntsw-1);
913                                         else {
914                                         
915                                                 bp->hide= 1;
916                                                 bp->f1 &= ~SELECT;
917                                                 
918                                                 /* u extrema */
919                                                 bp1= latt_bp(lt, 0, v, w);
920                                                 bp2= latt_bp(lt, lt->pntsu-1, v, w);
921                                                 
922                                                 fac1= du*u;
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                                                 /* v extrema */
928                                                 bp1= latt_bp(lt, u, 0, w);
929                                                 bp2= latt_bp(lt, u, lt->pntsv-1, w);
930                                                 
931                                                 fac1= dv*v;
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                                                 /* w extrema */
937                                                 bp1= latt_bp(lt, u, v, 0);
938                                                 bp2= latt_bp(lt, u, v, lt->pntsw-1);
939                                                 
940                                                 fac1= dw*w;
941                                                 bp->vec[0]+= (1.0f-fac1)*bp1->vec[0] + fac1*bp2->vec[0];
942                                                 bp->vec[1]+= (1.0f-fac1)*bp1->vec[1] + fac1*bp2->vec[1];
943                                                 bp->vec[2]+= (1.0f-fac1)*bp1->vec[2] + fac1*bp2->vec[2];
944                                                 
945                                                 mul_v3_fl(bp->vec, 0.3333333f);
946                                                 
947                                         }
948                                 }
949                                 
950                         }
951                         
952                 }
953         }
954         else {
955                 bp= lt->def;
956
957                 for(w=0; w<lt->pntsw; w++)
958                         for(v=0; v<lt->pntsv; v++)
959                                 for(u=0; u<lt->pntsu; u++, bp++)
960                                         bp->hide= 0;
961         }
962 }
963
964 float (*lattice_getVertexCos(struct Object *ob, int *numVerts_r))[3]
965 {
966         Lattice *lt = ob->data;
967         int i, numVerts;
968         float (*vertexCos)[3];
969
970         if(lt->editlatt) lt= lt->editlatt->latt;
971         numVerts = *numVerts_r = lt->pntsu*lt->pntsv*lt->pntsw;
972         
973         vertexCos = MEM_mallocN(sizeof(*vertexCos)*numVerts,"lt_vcos");
974         
975         for (i=0; i<numVerts; i++) {
976                 copy_v3_v3(vertexCos[i], lt->def[i].vec);
977         }
978
979         return vertexCos;
980 }
981
982 void lattice_applyVertexCos(struct Object *ob, float (*vertexCos)[3])
983 {
984         Lattice *lt = ob->data;
985         int i, numVerts = lt->pntsu*lt->pntsv*lt->pntsw;
986
987         for (i=0; i<numVerts; i++) {
988                 copy_v3_v3(lt->def[i].vec, vertexCos[i]);
989         }
990 }
991
992 void lattice_calc_modifiers(Scene *scene, Object *ob)
993 {
994         Lattice *lt= ob->data;
995         ModifierData *md = modifiers_getVirtualModifierList(ob);
996         float (*vertexCos)[3] = NULL;
997         int numVerts, editmode = (lt->editlatt!=NULL);
998
999         freedisplist(&ob->disp);
1000
1001         for (; md; md=md->next) {
1002                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1003
1004                 md->scene= scene;
1005                 
1006                 if (!(md->mode&eModifierMode_Realtime)) continue;
1007                 if (editmode && !(md->mode&eModifierMode_Editmode)) continue;
1008                 if (mti->isDisabled && mti->isDisabled(md, 0)) continue;
1009                 if (mti->type!=eModifierTypeType_OnlyDeform) continue;
1010
1011                 if (!vertexCos) vertexCos = lattice_getVertexCos(ob, &numVerts);
1012                 mti->deformVerts(md, ob, NULL, vertexCos, numVerts, 0, 0);
1013         }
1014
1015         /* always displist to make this work like derivedmesh */
1016         if (!vertexCos) vertexCos = lattice_getVertexCos(ob, &numVerts);
1017         
1018         {
1019                 DispList *dl = MEM_callocN(sizeof(*dl), "lt_dl");
1020                 dl->type = DL_VERTS;
1021                 dl->parts = 1;
1022                 dl->nr = numVerts;
1023                 dl->verts = (float*) vertexCos;
1024                 
1025                 BLI_addtail(&ob->disp, dl);
1026         }
1027 }
1028
1029 struct MDeformVert* lattice_get_deform_verts(struct Object *oblatt)
1030 {
1031         Lattice *lt = (Lattice*)oblatt->data;
1032         BLI_assert(oblatt->type == OB_LATTICE);
1033         if(lt->editlatt) lt= lt->editlatt->latt;
1034         return lt->dvert;
1035 }