style cleanup / comment formatting for bli/bke/bmesh
[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_bpath.h"
43 #include "BLI_math.h"
44 #include "BLI_utildefines.h"
45
46 #include "DNA_mesh_types.h"
47 #include "DNA_meshdata_types.h"
48 #include "DNA_scene_types.h"
49 #include "DNA_object_types.h"
50 #include "DNA_lattice_types.h"
51 #include "DNA_curve_types.h"
52 #include "DNA_key_types.h"
53
54 #include "BKE_animsys.h"
55 #include "BKE_anim.h"
56 #include "BKE_cdderivedmesh.h"
57 #include "BKE_displist.h"
58 #include "BKE_global.h"
59 #include "BKE_key.h"
60 #include "BKE_lattice.h"
61 #include "BKE_library.h"
62 #include "BKE_main.h"
63 #include "BKE_mesh.h"
64 #include "BKE_modifier.h"
65
66 #include "BKE_deform.h"
67
68 //XXX #include "BIF_editdeform.h"
69
70 void calc_lat_fudu(int flag, int res, float *fu, float *du)
71 {
72         if (res==1) {
73                 *fu= 0.0;
74                 *du= 0.0;
75         }
76         else if (flag & LT_GRID) {
77                 *fu= -0.5f*(res-1);
78                 *du= 1.0f;
79         }
80         else {
81                 *fu= -1.0f;
82                 *du= 2.0f/(res-1);
83         }
84 }
85
86 void resizelattice(Lattice *lt, int uNew, int vNew, int wNew, Object *ltOb)
87 {
88         BPoint *bp;
89         int i, u, v, w;
90         float fu, fv, fw, uc, vc, wc, du=0.0, dv=0.0, dw=0.0;
91         float *co, (*vertexCos)[3] = NULL;
92         
93         /* vertex weight groups are just freed all for now */
94         if (lt->dvert) {
95                 free_dverts(lt->dvert, lt->pntsu*lt->pntsv*lt->pntsw);
96                 lt->dvert= NULL;
97         }
98         
99         while(uNew*vNew*wNew > 32000) {
100                 if ( uNew>=vNew && uNew>=wNew) uNew--;
101                 else if ( vNew>=uNew && vNew>=wNew) vNew--;
102                 else wNew--;
103         }
104
105         vertexCos = MEM_mallocN(sizeof(*vertexCos)*uNew*vNew*wNew, "tmp_vcos");
106
107         calc_lat_fudu(lt->flag, uNew, &fu, &du);
108         calc_lat_fudu(lt->flag, vNew, &fv, &dv);
109         calc_lat_fudu(lt->flag, wNew, &fw, &dw);
110
111                 /* If old size is different then resolution changed in interface,
112                  * try to do clever reinit of points. Pretty simply idea, we just
113                  * deform new verts by old lattice, but scaling them to match old
114                  * size first.
115                  */
116         if (ltOb) {
117                 if (uNew!=1 && lt->pntsu!=1) {
118                         fu = lt->fu;
119                         du = (lt->pntsu-1)*lt->du/(uNew-1);
120                 }
121
122                 if (vNew!=1 && lt->pntsv!=1) {
123                         fv = lt->fv;
124                         dv = (lt->pntsv-1)*lt->dv/(vNew-1);
125                 }
126
127                 if (wNew!=1 && lt->pntsw!=1) {
128                         fw = lt->fw;
129                         dw = (lt->pntsw-1)*lt->dw/(wNew-1);
130                 }
131         }
132
133         co = vertexCos[0];
134         for (w=0,wc=fw; w<wNew; w++,wc+=dw) {
135                 for (v=0,vc=fv; v<vNew; v++,vc+=dv) {
136                         for (u=0,uc=fu; u<uNew; u++,co+=3,uc+=du) {
137                                 co[0] = uc;
138                                 co[1] = vc;
139                                 co[2] = wc;
140                         }
141                 }
142         }
143         
144         if (ltOb) {
145                 float mat[4][4];
146                 int typeu = lt->typeu, typev = lt->typev, typew = lt->typew;
147
148                         /* works best if we force to linear type (endpoints match) */
149                 lt->typeu = lt->typev = lt->typew = KEY_LINEAR;
150
151                         /* prevent using deformed locations */
152                 freedisplist(&ltOb->disp);
153
154                 copy_m4_m4(mat, ltOb->obmat);
155                 unit_m4(ltOb->obmat);
156                 lattice_deform_verts(ltOb, NULL, NULL, vertexCos, uNew*vNew*wNew, NULL);
157                 copy_m4_m4(ltOb->obmat, mat);
158
159                 lt->typeu = typeu;
160                 lt->typev = typev;
161                 lt->typew = typew;
162         }
163
164         lt->fu = fu;
165         lt->fv = fv;
166         lt->fw = fw;
167         lt->du = du;
168         lt->dv = dv;
169         lt->dw = dw;
170
171         lt->pntsu = uNew;
172         lt->pntsv = vNew;
173         lt->pntsw = wNew;
174
175         MEM_freeN(lt->def);
176         lt->def= MEM_callocN(lt->pntsu*lt->pntsv*lt->pntsw*sizeof(BPoint), "lattice bp");
177         
178         bp= lt->def;
179         
180         for (i=0; i<lt->pntsu*lt->pntsv*lt->pntsw; i++,bp++) {
181                 copy_v3_v3(bp->vec, vertexCos[i]);
182         }
183
184         MEM_freeN(vertexCos);
185 }
186
187 Lattice *add_lattice(const char *name)
188 {
189         Lattice *lt;
190         
191         lt= alloc_libblock(&G.main->latt, ID_LT, name);
192         
193         lt->flag= LT_GRID;
194         
195         lt->typeu= lt->typev= lt->typew= KEY_BSPLINE;
196         
197         lt->def= MEM_callocN(sizeof(BPoint), "lattvert"); /* temporary */
198         resizelattice(lt, 2, 2, 2, NULL);       /* creates a uniform lattice */
199                 
200         return lt;
201 }
202
203 Lattice *copy_lattice(Lattice *lt)
204 {
205         Lattice *ltn;
206
207         ltn= copy_libblock(&lt->id);
208         ltn->def= MEM_dupallocN(lt->def);
209
210         ltn->key= copy_key(ltn->key);
211         if (ltn->key) ltn->key->from= (ID *)ltn;
212         
213         if (lt->dvert) {
214                 int tot= lt->pntsu*lt->pntsv*lt->pntsw;
215                 ltn->dvert = MEM_mallocN (sizeof (MDeformVert)*tot, "Lattice MDeformVert");
216                 copy_dverts(ltn->dvert, lt->dvert, tot);
217         }
218
219         ltn->editlatt= NULL;
220
221         return ltn;
222 }
223
224 void free_lattice(Lattice *lt)
225 {
226         if (lt->def) MEM_freeN(lt->def);
227         if (lt->dvert) free_dverts(lt->dvert, lt->pntsu*lt->pntsv*lt->pntsw);
228         if (lt->editlatt) {
229                 Lattice *editlt= lt->editlatt->latt;
230
231                 if (editlt->def) MEM_freeN(editlt->def);
232                 if (editlt->dvert) free_dverts(editlt->dvert, lt->pntsu*lt->pntsv*lt->pntsw);
233
234                 MEM_freeN(editlt);
235                 MEM_freeN(lt->editlatt);
236         }
237         
238         /* free animation data */
239         if (lt->adt) {
240                 BKE_free_animdata(&lt->id);
241                 lt->adt= NULL;
242         }
243 }
244
245
246 void make_local_lattice(Lattice *lt)
247 {
248         Main *bmain= G.main;
249         Object *ob;
250         int is_local= FALSE, is_lib= FALSE;
251
252         /* - only lib users: do nothing
253          * - only local users: set flag
254          * - mixed: make copy
255          */
256         
257         if (lt->id.lib==NULL) return;
258         if (lt->id.us==1) {
259                 id_clear_lib_data(bmain, &lt->id);
260                 return;
261         }
262         
263         for (ob= bmain->object.first; ob && ELEM(FALSE, is_lib, is_local); ob= ob->id.next) {
264                 if (ob->data==lt) {
265                         if (ob->id.lib) is_lib= TRUE;
266                         else is_local= TRUE;
267                 }
268         }
269         
270         if (is_local && is_lib==FALSE) {
271                 id_clear_lib_data(bmain, &lt->id);
272         }
273         else if (is_local && is_lib) {
274                 Lattice *lt_new= copy_lattice(lt);
275                 lt_new->id.us= 0;
276
277                 /* Remap paths of new ID using old library as base. */
278                 BKE_id_lib_local_paths(bmain, lt->id.lib, &lt_new->id);
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= lt_new;
284                                         lt_new->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                 mult_m4_m4m4(lt->latmat, imat, ob->obmat);
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];
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)
471 {
472         invert_m4_m4(ob->imat, ob->obmat);
473         mult_m4_m4m4(cd->objectspace, ob->imat, par->obmat);
474         invert_m4_m4(cd->curvespace, cd->objectspace);
475         copy_m3_m4(cd->objectspace3, cd->objectspace);
476         cd->no_rot_axis= 0;
477 }
478
479 /* this makes sure we can extend for non-cyclic.
480  *
481  * returns OK: 1/0
482  */
483 static int where_on_path_deform(Object *ob, float ctime, float vec[4], float dir[3], float quat[4], float *radius)
484 {
485         Curve *cu= ob->data;
486         BevList *bl;
487         float ctime1;
488         int cycl=0;
489         
490         /* test for cyclic */
491         bl= cu->bev.first;
492         if (!bl->nr) return 0;
493         if (bl && bl->poly> -1) cycl= 1;
494
495         if (cycl==0) {
496                 ctime1= CLAMPIS(ctime, 0.0f, 1.0f);
497         }
498         else ctime1= ctime;
499         
500         /* vec needs 4 items */
501         if (where_on_path(ob, ctime1, vec, dir, quat, radius, NULL)) {
502                 
503                 if (cycl==0) {
504                         Path *path= cu->path;
505                         float dvec[3];
506                         
507                         if (ctime < 0.0f) {
508                                 sub_v3_v3v3(dvec, path->data[1].vec, path->data[0].vec);
509                                 mul_v3_fl(dvec, ctime*(float)path->len);
510                                 add_v3_v3(vec, dvec);
511                                 if (quat) copy_qt_qt(quat, path->data[0].quat);
512                                 if (radius) *radius= path->data[0].radius;
513                         }
514                         else if (ctime > 1.0f) {
515                                 sub_v3_v3v3(dvec, path->data[path->len-1].vec, path->data[path->len-2].vec);
516                                 mul_v3_fl(dvec, (ctime-1.0f)*(float)path->len);
517                                 add_v3_v3(vec, dvec);
518                                 if (quat) copy_qt_qt(quat, path->data[path->len-1].quat);
519                                 if (radius) *radius= path->data[path->len-1].radius;
520                                 /* weight - not used but could be added */
521                         }
522                 }
523                 return 1;
524         }
525         return 0;
526 }
527
528 /* for each point, rotate & translate to curve */
529 /* use path, since it has constant distances */
530 /* co: local coord, result local too */
531 /* returns quaternion for rotation, using cd->no_rot_axis */
532 /* axis is using another define!!! */
533 static int calc_curve_deform(Scene *scene, Object *par, float co[3],
534                              const short axis, CurveDeform *cd, float quat_r[4])
535 {
536         Curve *cu= par->data;
537         float fac, loc[4], dir[3], new_quat[4], radius;
538         short index;
539         const int is_neg_axis = (axis > 2);
540
541         /* to be sure, mostly after file load */
542         if (cu->path==NULL) {
543                 makeDispListCurveTypes(scene, par, 0);
544                 if (cu->path==NULL) return 0;   // happens on append...
545         }
546         
547         /* options */
548         if (is_neg_axis) {
549                 index = axis - 3;
550                 if (cu->flag & CU_STRETCH)
551                         fac= (-co[index]-cd->dmax[index])/(cd->dmax[index] - cd->dmin[index]);
552                 else
553                         fac= - (co[index]-cd->dmax[index])/(cu->path->totdist);
554         }
555         else {
556                 index = axis;
557                 if (cu->flag & CU_STRETCH)
558                         fac= (co[index]-cd->dmin[index])/(cd->dmax[index] - cd->dmin[index]);
559                 else
560                         fac= + (co[index]-cd->dmin[index])/(cu->path->totdist);
561         }
562         
563         if ( where_on_path_deform(par, fac, loc, dir, new_quat, &radius)) {     /* returns OK */
564                 float quat[4], cent[3];
565
566                 if (cd->no_rot_axis) {  /* set by caller */
567
568                         /* this is not exactly the same as 2.4x, since the axis is having rotation removed rather than
569                          * changing the axis before calculating the tilt but serves much the same purpose */
570                         float dir_flat[3]={0,0,0}, q[4];
571                         copy_v3_v3(dir_flat, dir);
572                         dir_flat[cd->no_rot_axis-1]= 0.0f;
573
574                         normalize_v3(dir);
575                         normalize_v3(dir_flat);
576
577                         rotation_between_vecs_to_quat(q, dir, dir_flat); /* Could this be done faster? */
578
579                         mul_qt_qtqt(new_quat, q, new_quat);
580                 }
581
582
583                 /* Logic for 'cent' orientation *
584                  *
585                  * The way 'co' is copied to 'cent' may seem to have no meaning, but it does.
586                  *
587                  * Use a curve modifier to stretch a cube out, color each side RGB, positive side light, negative dark.
588                  * view with X up (default), from the angle that you can see 3 faces RGB colors (light), anti-clockwise
589                  * Notice X,Y,Z Up all have light colors and each ordered CCW.
590                  *
591                  * Now for Neg Up XYZ, the colors are all dark, and ordered clockwise - Campbell
592                  *
593                  * note: moved functions into quat_apply_track/vec_apply_track
594                  * */
595                 copy_qt_qt(quat, new_quat);
596                 copy_v3_v3(cent, co);
597
598                 /* zero the axis which is not used,
599                  * the big block of text above now applies to these 3 lines */
600                 quat_apply_track(quat, axis, (axis == 0 || axis == 2) ? 1:0); /* up flag is a dummy, set so no rotation is done */
601                 vec_apply_track(cent, axis);
602                 cent[index]= 0.0f;
603
604
605                 /* scale if enabled */
606                 if (cu->flag & CU_PATH_RADIUS)
607                         mul_v3_fl(cent, radius);
608                 
609                 /* local rotation */
610                 normalize_qt(quat);
611                 mul_qt_v3(quat, cent);
612
613                 /* translation */
614                 add_v3_v3v3(co, cent, loc);
615
616                 if (quat_r)
617                         copy_qt_qt(quat_r, quat);
618
619                 return 1;
620         }
621         return 0;
622 }
623
624 void curve_deform_verts(Scene *scene, Object *cuOb, Object *target,
625                         DerivedMesh *dm, float (*vertexCos)[3],
626                         int numVerts, const char *vgroup, short defaxis)
627 {
628         Curve *cu;
629         int a, flag;
630         CurveDeform cd;
631         int use_vgroups;
632         const int is_neg_axis = (defaxis > 2);
633
634         if (cuOb->type != OB_CURVE)
635                 return;
636
637         cu = cuOb->data;
638         flag = cu->flag;
639         cu->flag |= (CU_PATH|CU_FOLLOW); // needed for path & bevlist
640
641         init_curve_deform(cuOb, target, &cd);
642
643         /* dummy bounds, keep if CU_DEFORM_BOUNDS_OFF is set */
644         if (is_neg_axis == FALSE) {
645                 cd.dmin[0]= cd.dmin[1]= cd.dmin[2]= 0.0f;
646                 cd.dmax[0]= cd.dmax[1]= cd.dmax[2]= 1.0f;
647         }
648         else {
649                 /* negative, these bounds give a good rest position */
650                 cd.dmin[0]= cd.dmin[1]= cd.dmin[2]= -1.0f;
651                 cd.dmax[0]= cd.dmax[1]= cd.dmax[2]=  0.0f;
652         }
653         
654         /* check whether to use vertex groups (only possible if target is a Mesh)
655          * we want either a Mesh with no derived data, or derived data with
656          * deformverts
657          */
658         if (target && target->type==OB_MESH) {
659                 /* if there's derived data without deformverts, don't use vgroups */
660                 if (dm && !dm->getVertData(dm, 0, CD_MDEFORMVERT))
661                         use_vgroups = 0;
662                 else
663                         use_vgroups = 1;
664         } else
665                 use_vgroups = 0;
666         
667         if (vgroup && vgroup[0] && use_vgroups) {
668                 Mesh *me= target->data;
669                 int index= defgroup_name_index(target, vgroup);
670
671                 if (index != -1 && (me->dvert || dm)) {
672                         MDeformVert *dvert = me->dvert;
673                         float vec[3];
674                         float weight;
675         
676
677                         if (cu->flag & CU_DEFORM_BOUNDS_OFF) {
678                                 dvert = me->dvert;
679                                 for (a = 0; a < numVerts; a++, dvert++) {
680                                         if (dm) dvert = dm->getVertData(dm, a, CD_MDEFORMVERT);
681                                         weight= defvert_find_weight(dvert, index);
682         
683                                         if (weight > 0.0f) {
684                                                 mul_m4_v3(cd.curvespace, vertexCos[a]);
685                                                 copy_v3_v3(vec, vertexCos[a]);
686                                                 calc_curve_deform(scene, cuOb, vec, defaxis, &cd, NULL);
687                                                 interp_v3_v3v3(vertexCos[a], vertexCos[a], vec, weight);
688                                                 mul_m4_v3(cd.objectspace, vertexCos[a]);
689                                         }
690                                 }
691                         }
692                         else {
693                                 /* set mesh min/max bounds */
694                                 INIT_MINMAX(cd.dmin, cd.dmax);
695         
696                                 for (a = 0; a < numVerts; a++, dvert++) {
697                                         if (dm) dvert = dm->getVertData(dm, a, CD_MDEFORMVERT);
698                                         
699                                         if (defvert_find_weight(dvert, index) > 0.0f) {
700                                                 mul_m4_v3(cd.curvespace, vertexCos[a]);
701                                                 DO_MINMAX(vertexCos[a], cd.dmin, cd.dmax);
702                                         }
703                                 }
704         
705                                 dvert = me->dvert;
706                                 for (a = 0; a < numVerts; a++, dvert++) {
707                                         if (dm) dvert = dm->getVertData(dm, a, CD_MDEFORMVERT);
708                                         
709                                         weight= defvert_find_weight(dvert, index);
710         
711                                         if (weight > 0.0f) {
712                                                 /* already in 'cd.curvespace', prev for loop */
713                                                 copy_v3_v3(vec, vertexCos[a]);
714                                                 calc_curve_deform(scene, cuOb, vec, defaxis, &cd, NULL);
715                                                 interp_v3_v3v3(vertexCos[a], vertexCos[a], vec, weight);
716                                                 mul_m4_v3(cd.objectspace, vertexCos[a]);
717                                         }
718                                 }
719                         }
720                 }
721         }
722         else {
723                 if (cu->flag & CU_DEFORM_BOUNDS_OFF) {
724                         for (a = 0; a < numVerts; a++) {
725                                 mul_m4_v3(cd.curvespace, vertexCos[a]);
726                                 calc_curve_deform(scene, cuOb, vertexCos[a], defaxis, &cd, NULL);
727                                 mul_m4_v3(cd.objectspace, vertexCos[a]);
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++) {
735                                 mul_m4_v3(cd.curvespace, vertexCos[a]);
736                                 DO_MINMAX(vertexCos[a], cd.dmin, cd.dmax);
737                         }
738         
739                         for (a = 0; a < numVerts; a++) {
740                                 /* already in 'cd.curvespace', prev for loop */
741                                 calc_curve_deform(scene, cuOb, vertexCos[a], defaxis, &cd, NULL);
742                                 mul_m4_v3(cd.objectspace, vertexCos[a]);
743                         }
744                 }
745         }
746         cu->flag = flag;
747 }
748
749 /* input vec and orco = local coord in armature space */
750 /* orco is original not-animated or deformed reference point */
751 /* result written in vec and mat */
752 void curve_deform_vector(Scene *scene, Object *cuOb, Object *target,
753                          float orco[3], float vec[3], float mat[][3], int no_rot_axis)
754 {
755         CurveDeform cd;
756         float quat[4];
757         
758         if (cuOb->type != OB_CURVE) {
759                 unit_m3(mat);
760                 return;
761         }
762
763         init_curve_deform(cuOb, target, &cd);
764         cd.no_rot_axis= no_rot_axis;                            /* option to only rotate for XY, for example */
765         
766         copy_v3_v3(cd.dmin, orco);
767         copy_v3_v3(cd.dmax, orco);
768
769         mul_m4_v3(cd.curvespace, vec);
770         
771         if (calc_curve_deform(scene, cuOb, vec, target->trackflag, &cd, quat)) {
772                 float qmat[3][3];
773                 
774                 quat_to_mat3( qmat,quat);
775                 mul_m3_m3m3(mat, qmat, cd.objectspace3);
776         }
777         else
778                 unit_m3(mat);
779         
780         mul_m4_v3(cd.objectspace, vec);
781
782 }
783
784 void lattice_deform_verts(Object *laOb, Object *target, DerivedMesh *dm,
785                           float (*vertexCos)[3], int numVerts, const char *vgroup)
786 {
787         int a;
788         int use_vgroups;
789
790         if (laOb->type != OB_LATTICE)
791                 return;
792
793         init_latt_deform(laOb, target);
794
795         /* check whether to use vertex groups (only possible if target is a Mesh)
796          * we want either a Mesh with no derived data, or derived data with
797          * deformverts
798          */
799         if (target && target->type==OB_MESH) {
800                 /* if there's derived data without deformverts, don't use vgroups */
801                 if (dm && !dm->getVertData(dm, 0, CD_MDEFORMVERT))
802                         use_vgroups = 0;
803                 else
804                         use_vgroups = 1;
805         } else
806                 use_vgroups = 0;
807         
808         if (vgroup && vgroup[0] && use_vgroups) {
809                 Mesh *me = target->data;
810                 int index = defgroup_name_index(target, vgroup);
811                 float weight;
812
813                 if (index >= 0 && (me->dvert || dm)) {
814                         MDeformVert *dvert = me->dvert;
815                         
816                         for (a = 0; a < numVerts; a++, dvert++) {
817                                 if (dm) dvert = dm->getVertData(dm, a, CD_MDEFORMVERT);
818
819                                 weight= defvert_find_weight(dvert, index);
820
821                                 if (weight > 0.0f)
822                                         calc_latt_deform(laOb, vertexCos[a], weight);
823                         }
824                 }
825         } else {
826                 for (a = 0; a < numVerts; a++) {
827                         calc_latt_deform(laOb, vertexCos[a], 1.0f);
828                 }
829         }
830         end_latt_deform(laOb);
831 }
832
833 int object_deform_mball(Object *ob, ListBase *dispbase)
834 {
835         if (ob->parent && ob->parent->type==OB_LATTICE && ob->partype==PARSKEL) {
836                 DispList *dl;
837
838                 for (dl=dispbase->first; dl; dl=dl->next) {
839                         lattice_deform_verts(ob->parent, ob, NULL,
840                                                                  (float(*)[3]) dl->verts, dl->nr, NULL);
841                 }
842
843                 return 1;
844         } else {
845                 return 0;
846         }
847 }
848
849 static BPoint *latt_bp(Lattice *lt, int u, int v, int w)
850 {
851         return &lt->def[LT_INDEX(lt, u, v, w)];
852 }
853
854 void outside_lattice(Lattice *lt)
855 {
856         BPoint *bp, *bp1, *bp2;
857         int u, v, w;
858         float fac1, du=0.0, dv=0.0, dw=0.0;
859
860         if (lt->flag & LT_OUTSIDE) {
861                 bp= lt->def;
862
863                 if (lt->pntsu>1) du= 1.0f/((float)lt->pntsu-1);
864                 if (lt->pntsv>1) dv= 1.0f/((float)lt->pntsv-1);
865                 if (lt->pntsw>1) dw= 1.0f/((float)lt->pntsw-1);
866                         
867                 for (w=0; w<lt->pntsw; w++) {
868                         
869                         for (v=0; v<lt->pntsv; v++) {
870                         
871                                 for (u=0; u<lt->pntsu; u++, bp++) {
872                                         if (u==0 || v==0 || w==0 || u==lt->pntsu-1 || v==lt->pntsv-1 || w==lt->pntsw-1);
873                                         else {
874                                         
875                                                 bp->hide= 1;
876                                                 bp->f1 &= ~SELECT;
877                                                 
878                                                 /* u extrema */
879                                                 bp1= latt_bp(lt, 0, v, w);
880                                                 bp2= latt_bp(lt, lt->pntsu-1, v, w);
881                                                 
882                                                 fac1= du*u;
883                                                 bp->vec[0]= (1.0f-fac1)*bp1->vec[0] + fac1*bp2->vec[0];
884                                                 bp->vec[1]= (1.0f-fac1)*bp1->vec[1] + fac1*bp2->vec[1];
885                                                 bp->vec[2]= (1.0f-fac1)*bp1->vec[2] + fac1*bp2->vec[2];
886                                                 
887                                                 /* v extrema */
888                                                 bp1= latt_bp(lt, u, 0, w);
889                                                 bp2= latt_bp(lt, u, lt->pntsv-1, w);
890                                                 
891                                                 fac1= dv*v;
892                                                 bp->vec[0]+= (1.0f-fac1)*bp1->vec[0] + fac1*bp2->vec[0];
893                                                 bp->vec[1]+= (1.0f-fac1)*bp1->vec[1] + fac1*bp2->vec[1];
894                                                 bp->vec[2]+= (1.0f-fac1)*bp1->vec[2] + fac1*bp2->vec[2];
895                                                 
896                                                 /* w extrema */
897                                                 bp1= latt_bp(lt, u, v, 0);
898                                                 bp2= latt_bp(lt, u, v, lt->pntsw-1);
899                                                 
900                                                 fac1= dw*w;
901                                                 bp->vec[0]+= (1.0f-fac1)*bp1->vec[0] + fac1*bp2->vec[0];
902                                                 bp->vec[1]+= (1.0f-fac1)*bp1->vec[1] + fac1*bp2->vec[1];
903                                                 bp->vec[2]+= (1.0f-fac1)*bp1->vec[2] + fac1*bp2->vec[2];
904                                                 
905                                                 mul_v3_fl(bp->vec, 0.3333333f);
906                                                 
907                                         }
908                                 }
909                                 
910                         }
911                         
912                 }
913         }
914         else {
915                 bp= lt->def;
916
917                 for (w=0; w<lt->pntsw; w++)
918                         for (v=0; v<lt->pntsv; v++)
919                                 for (u=0; u<lt->pntsu; u++, bp++)
920                                         bp->hide= 0;
921         }
922 }
923
924 float (*lattice_getVertexCos(struct Object *ob, int *numVerts_r))[3]
925 {
926         Lattice *lt = ob->data;
927         int i, numVerts;
928         float (*vertexCos)[3];
929
930         if (lt->editlatt) lt= lt->editlatt->latt;
931         numVerts = *numVerts_r = lt->pntsu*lt->pntsv*lt->pntsw;
932         
933         vertexCos = MEM_mallocN(sizeof(*vertexCos)*numVerts,"lt_vcos");
934         
935         for (i=0; i<numVerts; i++) {
936                 copy_v3_v3(vertexCos[i], lt->def[i].vec);
937         }
938
939         return vertexCos;
940 }
941
942 void lattice_applyVertexCos(struct Object *ob, float (*vertexCos)[3])
943 {
944         Lattice *lt = ob->data;
945         int i, numVerts = lt->pntsu*lt->pntsv*lt->pntsw;
946
947         for (i=0; i<numVerts; i++) {
948                 copy_v3_v3(lt->def[i].vec, vertexCos[i]);
949         }
950 }
951
952 void lattice_calc_modifiers(Scene *scene, Object *ob)
953 {
954         Lattice *lt= ob->data;
955         ModifierData *md = modifiers_getVirtualModifierList(ob);
956         float (*vertexCos)[3] = NULL;
957         int numVerts, editmode = (lt->editlatt!=NULL);
958
959         freedisplist(&ob->disp);
960
961         for (; md; md=md->next) {
962                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
963
964                 md->scene= scene;
965                 
966                 if (!(md->mode&eModifierMode_Realtime)) continue;
967                 if (editmode && !(md->mode&eModifierMode_Editmode)) continue;
968                 if (mti->isDisabled && mti->isDisabled(md, 0)) continue;
969                 if (mti->type!=eModifierTypeType_OnlyDeform) continue;
970
971                 if (!vertexCos) vertexCos = lattice_getVertexCos(ob, &numVerts);
972                 mti->deformVerts(md, ob, NULL, vertexCos, numVerts, 0, 0);
973         }
974
975         /* always displist to make this work like derivedmesh */
976         if (!vertexCos) vertexCos = lattice_getVertexCos(ob, &numVerts);
977         
978         {
979                 DispList *dl = MEM_callocN(sizeof(*dl), "lt_dl");
980                 dl->type = DL_VERTS;
981                 dl->parts = 1;
982                 dl->nr = numVerts;
983                 dl->verts = (float*) vertexCos;
984                 
985                 BLI_addtail(&ob->disp, dl);
986         }
987 }
988
989 struct MDeformVert* lattice_get_deform_verts(struct Object *oblatt)
990 {
991         Lattice *lt = (Lattice*)oblatt->data;
992         BLI_assert(oblatt->type == OB_LATTICE);
993         if (lt->editlatt) lt= lt->editlatt->latt;
994         return lt->dvert;
995 }