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