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