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