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