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