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