Minor cleanup to lattice.c while looking into [#19525] Curve modifier moves mesh...
[blender.git] / source / blender / blenkernel / intern / lattice.c
1 /**
2  * lattice.c
3  *
4  *
5  * $Id$
6  *
7  * ***** BEGIN GPL LICENSE BLOCK *****
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL LICENSE BLOCK *****
31  */
32
33
34 #include <stdio.h>
35 #include <string.h>
36 #include <math.h>
37 #include <stdlib.h>
38
39 #include "MEM_guardedalloc.h"
40
41 #include "BLI_blenlib.h"
42 #include "BLI_math.h"
43
44 #include "DNA_mesh_types.h"
45 #include "DNA_meshdata_types.h"
46 #include "DNA_scene_types.h"
47 #include "DNA_object_types.h"
48 #include "DNA_lattice_types.h"
49 #include "DNA_curve_types.h"
50 #include "DNA_key_types.h"
51
52 #include "BKE_anim.h"
53 #include "BKE_cdderivedmesh.h"
54 #include "BKE_displist.h"
55 #include "BKE_global.h"
56 #include "BKE_key.h"
57 #include "BKE_lattice.h"
58 #include "BKE_library.h"
59 #include "BKE_main.h"
60 #include "BKE_mesh.h"
61 #include "BKE_modifier.h"
62 #include "BKE_utildefines.h"
63 #include "BKE_deform.h"
64
65 //XXX #include "BIF_editdeform.h"
66
67 void calc_lat_fudu(int flag, int res, float *fu, float *du)
68 {
69         if(res==1) {
70                 *fu= 0.0;
71                 *du= 0.0;
72         }
73         else if(flag & LT_GRID) {
74                 *fu= -0.5f*(res-1);
75                 *du= 1.0f;
76         }
77         else {
78                 *fu= -1.0f;
79                 *du= 2.0f/(res-1);
80         }
81 }
82
83 void resizelattice(Lattice *lt, int uNew, int vNew, int wNew, Object *ltOb)
84 {
85         BPoint *bp;
86         int i, u, v, w;
87         float fu, fv, fw, uc, vc, wc, du=0.0, dv=0.0, dw=0.0;
88         float *co, (*vertexCos)[3] = NULL;
89         
90         /* vertex weight groups are just freed all for now */
91         if(lt->dvert) {
92                 free_dverts(lt->dvert, lt->pntsu*lt->pntsv*lt->pntsw);
93                 lt->dvert= NULL;
94         }
95         
96         while(uNew*vNew*wNew > 32000) {
97                 if( uNew>=vNew && uNew>=wNew) uNew--;
98                 else if( vNew>=uNew && vNew>=wNew) vNew--;
99                 else wNew--;
100         }
101
102         vertexCos = MEM_mallocN(sizeof(*vertexCos)*uNew*vNew*wNew, "tmp_vcos");
103
104         calc_lat_fudu(lt->flag, uNew, &fu, &du);
105         calc_lat_fudu(lt->flag, vNew, &fv, &dv);
106         calc_lat_fudu(lt->flag, wNew, &fw, &dw);
107
108                 /* If old size is different then resolution changed in interface,
109                  * try to do clever reinit of points. Pretty simply idea, we just
110                  * deform new verts by old lattice, but scaling them to match old
111                  * size first.
112                  */
113         if (ltOb) {
114                 if (uNew!=1 && lt->pntsu!=1) {
115                         fu = lt->fu;
116                         du = (lt->pntsu-1)*lt->du/(uNew-1);
117                 }
118
119                 if (vNew!=1 && lt->pntsv!=1) {
120                         fv = lt->fv;
121                         dv = (lt->pntsv-1)*lt->dv/(vNew-1);
122                 }
123
124                 if (wNew!=1 && lt->pntsw!=1) {
125                         fw = lt->fw;
126                         dw = (lt->pntsw-1)*lt->dw/(wNew-1);
127                 }
128         }
129
130         co = vertexCos[0];
131         for(w=0,wc=fw; w<wNew; w++,wc+=dw) {
132                 for(v=0,vc=fv; v<vNew; v++,vc+=dv) {
133                         for(u=0,uc=fu; u<uNew; u++,co+=3,uc+=du) {
134                                 co[0] = uc;
135                                 co[1] = vc;
136                                 co[2] = wc;
137                         }
138                 }
139         }
140         
141         if (ltOb) {
142                 float mat[4][4];
143                 int typeu = lt->typeu, typev = lt->typev, typew = lt->typew;
144
145                         /* works best if we force to linear type (endpoints match) */
146                 lt->typeu = lt->typev = lt->typew = KEY_LINEAR;
147
148                         /* prevent using deformed locations */
149                 freedisplist(&ltOb->disp);
150
151                 copy_m4_m4(mat, ltOb->obmat);
152                 unit_m4(ltOb->obmat);
153                 lattice_deform_verts(ltOb, NULL, NULL, vertexCos, uNew*vNew*wNew, NULL);
154                 copy_m4_m4(ltOb->obmat, mat);
155
156                 lt->typeu = typeu;
157                 lt->typev = typev;
158                 lt->typew = typew;
159         }
160
161         lt->fu = fu;
162         lt->fv = fv;
163         lt->fw = fw;
164         lt->du = du;
165         lt->dv = dv;
166         lt->dw = dw;
167
168         lt->pntsu = uNew;
169         lt->pntsv = vNew;
170         lt->pntsw = wNew;
171
172         MEM_freeN(lt->def);
173         lt->def= MEM_callocN(lt->pntsu*lt->pntsv*lt->pntsw*sizeof(BPoint), "lattice bp");
174         
175         bp= lt->def;
176         
177         for (i=0; i<lt->pntsu*lt->pntsv*lt->pntsw; i++,bp++) {
178                 copy_v3_v3(bp->vec, vertexCos[i]);
179         }
180
181         MEM_freeN(vertexCos);
182 }
183
184 Lattice *add_lattice(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 {
478                 cd->dloc[0]=cd->dloc[1]=cd->dloc[2]= 0.0f;
479         }
480         
481         cd->no_rot_axis= 0;
482 }
483
484 /* this makes sure we can extend for non-cyclic. *vec needs 4 items! */
485 static int where_on_path_deform(Object *ob, float ctime, float *vec, float *dir, float *quat, float *radius)    /* returns OK */
486 {
487         Curve *cu= ob->data;
488         BevList *bl;
489         float ctime1;
490         int cycl=0;
491         
492         /* test for cyclic */
493         bl= cu->bev.first;
494         if (!bl->nr) return 0;
495         if(bl && bl->poly> -1) cycl= 1;
496
497         if(cycl==0) {
498                 ctime1= CLAMPIS(ctime, 0.0, 1.0);
499         }
500         else ctime1= ctime;
501         
502         /* vec needs 4 items */
503         if(where_on_path(ob, ctime1, vec, dir, quat, radius, NULL)) {
504                 
505                 if(cycl==0) {
506                         Path *path= cu->path;
507                         float dvec[3];
508                         
509                         if(ctime < 0.0) {
510                                 sub_v3_v3v3(dvec, path->data[1].vec, path->data[0].vec);
511                                 mul_v3_fl(dvec, ctime*(float)path->len);
512                                 add_v3_v3(vec, dvec);
513                                 if(quat) copy_qt_qt(quat, path->data[0].quat);
514                                 if(radius) *radius= path->data[0].radius;
515                         }
516                         else if(ctime > 1.0) {
517                                 sub_v3_v3v3(dvec, path->data[path->len-1].vec, path->data[path->len-2].vec);
518                                 mul_v3_fl(dvec, (ctime-1.0)*(float)path->len);
519                                 add_v3_v3(vec, dvec);
520                                 if(quat) copy_qt_qt(quat, path->data[path->len-1].quat);
521                                 if(radius) *radius= path->data[path->len-1].radius;
522                                 /* weight - not used but could be added */
523                         }
524                 }
525                 return 1;
526         }
527         return 0;
528 }
529
530         /* for each point, rotate & translate to curve */
531         /* use path, since it has constant distances */
532         /* co: local coord, result local too */
533         /* returns quaternion for rotation, using cd->no_rot_axis */
534         /* axis is using another define!!! */
535 static int calc_curve_deform(Scene *scene, Object *par, float *co, short axis, CurveDeform *cd, float *quatp)
536 {
537         Curve *cu= par->data;
538         float fac, loc[4], dir[3], new_quat[4], radius;
539         short /*upflag, */ index;
540
541         index= axis-1;
542         if(index>2)
543                 index -= 3; /* negative  */
544
545         /* to be sure, mostly after file load */
546         if(cu->path==NULL) {
547                 makeDispListCurveTypes(scene, par, 0);
548                 if(cu->path==NULL) return 0;    // happens on append...
549         }
550         
551         /* options */
552         if(ELEM3(axis, OB_NEGX+1, OB_NEGY+1, OB_NEGZ+1)) { /* OB_NEG# 0-5, MOD_CURVE_POS# 1-6 */
553                 if(cu->flag & CU_STRETCH)
554                         fac= (-co[index]-cd->dmax[index])/(cd->dmax[index] - cd->dmin[index]);
555                 else
556                         fac= (cd->dloc[index])/(cu->path->totdist) - (co[index]-cd->dmax[index])/(cu->path->totdist);
557         }
558         else {
559                 if(cu->flag & CU_STRETCH)
560                         fac= (co[index]-cd->dmin[index])/(cd->dmax[index] - cd->dmin[index]);
561                 else
562                         fac= (cd->dloc[index])/(cu->path->totdist) + (co[index]-cd->dmin[index])/(cu->path->totdist);
563         }
564         
565 #if 0 // XXX old animation system
566         /* we want the ipo to work on the default 100 frame range, because there's no  
567            actual time involved in path position */
568         // huh? by WHY!!!!???? - Aligorith
569         if(cu->ipo) {
570                 fac*= 100.0f;
571                 if(calc_ipo_spec(cu->ipo, CU_SPEED, &fac)==0)
572                         fac/= 100.0;
573         }
574 #endif // XXX old animation system
575         
576         if( where_on_path_deform(par, fac, loc, dir, new_quat, &radius)) {      /* returns OK */
577                 float quat[4], cent[3];
578
579 #if 0   // XXX - 2.4x Z-Up, Now use bevel tilt.
580                 if(cd->no_rot_axis)     /* set by caller */
581                         dir[cd->no_rot_axis-1]= 0.0f;
582                 
583                 /* -1 for compatibility with old track defines */
584                 vec_to_quat( quat,dir, axis-1, upflag);
585                 
586                 /* the tilt */
587                 if(loc[3]!=0.0) {
588                         normalize_v3(dir);
589                         q[0]= (float)cos(0.5*loc[3]);
590                         fac= (float)sin(0.5*loc[3]);
591                         q[1]= -fac*dir[0];
592                         q[2]= -fac*dir[1];
593                         q[3]= -fac*dir[2];
594                         mul_qt_qtqt(quat, q, quat);
595                 }
596 #endif
597
598
599                 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));
600                 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));
601                 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));
602
603                 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));
604                 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));
605                 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));
606
607
608                 if(cd->no_rot_axis) {   /* set by caller */
609
610                         /* this is not exactly the same as 2.4x, since the axis is having rotation removed rather then
611                          * changing the axis before calculating the tilt but serves much the same purpose */
612                         float dir_flat[3]={0,0,0}, q[4];
613                         copy_v3_v3(dir_flat, dir);
614                         dir_flat[cd->no_rot_axis-1]= 0.0f;
615
616                         normalize_v3(dir);
617                         normalize_v3(dir_flat);
618
619                         rotation_between_vecs_to_quat(q, dir, dir_flat); /* Could this be done faster? */
620
621                         mul_qt_qtqt(new_quat, q, new_quat);
622                 }
623
624
625                 /* Logic for 'cent' orientation *
626                  *
627                  * The way 'co' is copied to 'cent' may seem to have no meaning, but it does.
628                  *
629                  * Use a curve modifier to stretch a cube out, color each side RGB, positive side light, negative dark.
630                  * view with X up (default), from the angle that you can see 3 faces RGB colors (light), anti-clockwise
631                  * Notice X,Y,Z Up all have light colors and each ordered CCW.
632                  *
633                  * Now for Neg Up XYZ, the colors are all dark, and ordered clockwise - Campbell
634                  * */
635
636                 switch(axis) {
637                 case MOD_CURVE_POSX:
638                         mul_qt_qtqt(quat, new_quat, q_y90d);
639
640                         cent[0]=  0.0;
641                         cent[1]=  co[2];
642                         cent[2]=  co[1];
643                         break;
644                 case MOD_CURVE_NEGX:
645                         mul_qt_qtqt(quat, new_quat, q_ny90d);
646
647                         cent[0]=  0.0;
648                         cent[1]= -co[1];
649                         cent[2]=  co[2];
650
651                         break;
652                 case MOD_CURVE_POSY:
653                         mul_qt_qtqt(quat, new_quat, q_x90d);
654
655                         cent[0]=  co[2];
656                         cent[1]=  0.0;
657                         cent[2]= -co[0];
658                         break;
659                 case MOD_CURVE_NEGY:
660                         mul_qt_qtqt(quat, new_quat, q_nx90d);
661
662                         cent[0]= -co[0];
663                         cent[1]=  0.0;
664                         cent[2]= -co[2];
665                         break;
666                 case MOD_CURVE_POSZ:
667                         mul_qt_qtqt(quat, new_quat, q_z90d);
668
669                         cent[0]=  co[1];
670                         cent[1]= -co[0];
671                         cent[2]=  0.0;
672                         break;
673                 case MOD_CURVE_NEGZ:
674                         mul_qt_qtqt(quat, new_quat, q_nz90d);
675
676                         cent[0]=  co[0];
677                         cent[1]= -co[1];
678                         cent[2]=  0.0;
679                         break;
680                 }
681
682                 /* scale if enabled */
683                 if(cu->flag & CU_PATH_RADIUS)
684                         mul_v3_fl(cent, radius);
685                 
686                 /* local rotation */
687                 normalize_qt(quat);
688                 mul_qt_v3(quat, cent);
689
690                 /* translation */
691                 add_v3_v3v3(co, cent, loc);
692
693                 if(quatp)
694                         copy_qt_qt(quatp, quat);
695
696                 return 1;
697         }
698         return 0;
699 }
700
701 void curve_deform_verts(Scene *scene, Object *cuOb, Object *target, DerivedMesh *dm, float (*vertexCos)[3], int numVerts, char *vgroup, short defaxis)
702 {
703         Curve *cu;
704         int a, flag;
705         CurveDeform cd;
706         int use_vgroups;
707
708         if(cuOb->type != OB_CURVE)
709                 return;
710
711         cu = cuOb->data;
712         flag = cu->flag;
713         cu->flag |= (CU_PATH|CU_FOLLOW); // needed for path & bevlist
714
715         init_curve_deform(cuOb, target, &cd, (cu->flag & CU_STRETCH)==0);
716                 
717         /* check whether to use vertex groups (only possible if target is a Mesh)
718          * we want either a Mesh with no derived data, or derived data with
719          * deformverts
720          */
721         if(target && target->type==OB_MESH) {
722                 /* if there's derived data without deformverts, don't use vgroups */
723                 if(dm && !dm->getVertData(dm, 0, CD_MDEFORMVERT))
724                         use_vgroups = 0;
725                 else
726                         use_vgroups = 1;
727         } else
728                 use_vgroups = 0;
729         
730         if(vgroup && vgroup[0] && use_vgroups) {
731                 Mesh *me= target->data;
732                 int index= defgroup_name_index(target, vgroup);
733
734                 if(index != -1 && (me->dvert || dm)) {
735                         MDeformVert *dvert = me->dvert;
736                         float vec[3];
737                         float weight;
738
739                         INIT_MINMAX(cd.dmin, cd.dmax);
740
741                         for(a = 0; a < numVerts; a++, dvert++) {
742                                 if(dm) dvert = dm->getVertData(dm, a, CD_MDEFORMVERT);
743                                 
744                                 if(defvert_find_weight(dvert, index) > 0.0f) {
745                                         mul_m4_v3(cd.curvespace, vertexCos[a]);
746                                         DO_MINMAX(vertexCos[a], cd.dmin, cd.dmax);
747                                 }
748                         }
749
750                         dvert = me->dvert;
751                         for(a = 0; a < numVerts; a++, dvert++) {
752                                 if(dm) dvert = dm->getVertData(dm, a, CD_MDEFORMVERT);
753                                 
754                                 weight= defvert_find_weight(dvert, index);
755
756                                 if(weight > 0.0f) {
757                                         copy_v3_v3(vec, vertexCos[a]);
758                                         calc_curve_deform(scene, cuOb, vec, defaxis, &cd, NULL);
759                                         interp_v3_v3v3(vertexCos[a], vertexCos[a], vec, weight);
760                                         mul_m4_v3(cd.objectspace, vertexCos[a]);
761                                 }
762                         }
763                 }
764         } else {
765                 INIT_MINMAX(cd.dmin, cd.dmax);
766                         
767                 for(a = 0; a < numVerts; a++) {
768                         mul_m4_v3(cd.curvespace, vertexCos[a]);
769                         DO_MINMAX(vertexCos[a], cd.dmin, cd.dmax);
770                 }
771
772                 for(a = 0; a < numVerts; a++) {
773                         calc_curve_deform(scene, cuOb, vertexCos[a], defaxis, &cd, NULL);
774                         mul_m4_v3(cd.objectspace, vertexCos[a]);
775                 }
776         }
777         cu->flag = flag;
778 }
779
780 /* input vec and orco = local coord in armature space */
781 /* orco is original not-animated or deformed reference point */
782 /* result written in vec and mat */
783 void curve_deform_vector(Scene *scene, Object *cuOb, Object *target, float *orco, float *vec, float mat[][3], int no_rot_axis)
784 {
785         CurveDeform cd;
786         float quat[4];
787         
788         if(cuOb->type != OB_CURVE) {
789                 unit_m3(mat);
790                 return;
791         }
792
793         init_curve_deform(cuOb, target, &cd, 0);        /* 0 no dloc */
794         cd.no_rot_axis= no_rot_axis;                            /* option to only rotate for XY, for example */
795         
796         copy_v3_v3(cd.dmin, orco);
797         copy_v3_v3(cd.dmax, orco);
798
799         mul_m4_v3(cd.curvespace, vec);
800         
801         if(calc_curve_deform(scene, cuOb, vec, target->trackflag+1, &cd, quat)) {
802                 float qmat[3][3];
803                 
804                 quat_to_mat3( qmat,quat);
805                 mul_m3_m3m3(mat, qmat, cd.objectspace3);
806         }
807         else
808                 unit_m3(mat);
809         
810         mul_m4_v3(cd.objectspace, vec);
811
812 }
813
814 void lattice_deform_verts(Object *laOb, Object *target, DerivedMesh *dm,
815                                                   float (*vertexCos)[3], int numVerts, char *vgroup)
816 {
817         int a;
818         int use_vgroups;
819
820         if(laOb->type != OB_LATTICE)
821                 return;
822
823         init_latt_deform(laOb, target);
824
825         /* check whether to use vertex groups (only possible if target is a Mesh)
826          * we want either a Mesh with no derived data, or derived data with
827          * deformverts
828          */
829         if(target && target->type==OB_MESH) {
830                 /* if there's derived data without deformverts, don't use vgroups */
831                 if(dm && !dm->getVertData(dm, 0, CD_MDEFORMVERT))
832                         use_vgroups = 0;
833                 else
834                         use_vgroups = 1;
835         } else
836                 use_vgroups = 0;
837         
838         if(vgroup && vgroup[0] && use_vgroups) {
839                 Mesh *me = target->data;
840                 int index = defgroup_name_index(target, vgroup);
841                 float weight;
842
843                 if(index >= 0 && (me->dvert || dm)) {
844                         MDeformVert *dvert = me->dvert;
845                         
846                         for(a = 0; a < numVerts; a++, dvert++) {
847                                 if(dm) dvert = dm->getVertData(dm, a, CD_MDEFORMVERT);
848
849                                 weight= defvert_find_weight(dvert, index);
850
851                                 if(weight > 0.0f)
852                                         calc_latt_deform(laOb, vertexCos[a], weight);
853                         }
854                 }
855         } else {
856                 for(a = 0; a < numVerts; a++) {
857                         calc_latt_deform(laOb, vertexCos[a], 1.0f);
858                 }
859         }
860         end_latt_deform(laOb);
861 }
862
863 int object_deform_mball(Object *ob, ListBase *dispbase)
864 {
865         if(ob->parent && ob->parent->type==OB_LATTICE && ob->partype==PARSKEL) {
866                 DispList *dl;
867
868                 for (dl=dispbase->first; dl; dl=dl->next) {
869                         lattice_deform_verts(ob->parent, ob, NULL,
870                                                                  (float(*)[3]) dl->verts, dl->nr, NULL);
871                 }
872
873                 return 1;
874         } else {
875                 return 0;
876         }
877 }
878
879 static BPoint *latt_bp(Lattice *lt, int u, int v, int w)
880 {
881         return lt->def+ u + v*lt->pntsu + w*lt->pntsu*lt->pntsv;
882 }
883
884 void outside_lattice(Lattice *lt)
885 {
886         BPoint *bp, *bp1, *bp2;
887         int u, v, w;
888         float fac1, du=0.0, dv=0.0, dw=0.0;
889
890         if(lt->flag & LT_OUTSIDE) {
891                 bp= lt->def;
892
893                 if(lt->pntsu>1) du= 1.0f/((float)lt->pntsu-1);
894                 if(lt->pntsv>1) dv= 1.0f/((float)lt->pntsv-1);
895                 if(lt->pntsw>1) dw= 1.0f/((float)lt->pntsw-1);
896                         
897                 for(w=0; w<lt->pntsw; w++) {
898                         
899                         for(v=0; v<lt->pntsv; v++) {
900                         
901                                 for(u=0; u<lt->pntsu; u++, bp++) {
902                                         if(u==0 || v==0 || w==0 || u==lt->pntsu-1 || v==lt->pntsv-1 || w==lt->pntsw-1);
903                                         else {
904                                         
905                                                 bp->hide= 1;
906                                                 bp->f1 &= ~SELECT;
907                                                 
908                                                 /* u extrema */
909                                                 bp1= latt_bp(lt, 0, v, w);
910                                                 bp2= latt_bp(lt, lt->pntsu-1, v, w);
911                                                 
912                                                 fac1= du*u;
913                                                 bp->vec[0]= (1.0f-fac1)*bp1->vec[0] + fac1*bp2->vec[0];
914                                                 bp->vec[1]= (1.0f-fac1)*bp1->vec[1] + fac1*bp2->vec[1];
915                                                 bp->vec[2]= (1.0f-fac1)*bp1->vec[2] + fac1*bp2->vec[2];
916                                                 
917                                                 /* v extrema */
918                                                 bp1= latt_bp(lt, u, 0, w);
919                                                 bp2= latt_bp(lt, u, lt->pntsv-1, w);
920                                                 
921                                                 fac1= dv*v;
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                                                 /* w extrema */
927                                                 bp1= latt_bp(lt, u, v, 0);
928                                                 bp2= latt_bp(lt, u, v, lt->pntsw-1);
929                                                 
930                                                 fac1= dw*w;
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                                                 mul_v3_fl(bp->vec, 0.3333333f);
936                                                 
937                                         }
938                                 }
939                                 
940                         }
941                         
942                 }
943         }
944         else {
945                 bp= lt->def;
946
947                 for(w=0; w<lt->pntsw; w++)
948                         for(v=0; v<lt->pntsv; v++)
949                                 for(u=0; u<lt->pntsu; u++, bp++)
950                                         bp->hide= 0;
951         }
952 }
953
954 float (*lattice_getVertexCos(struct Object *ob, int *numVerts_r))[3]
955 {
956         Lattice *lt = ob->data;
957         int i, numVerts;
958         float (*vertexCos)[3];
959
960         if(lt->editlatt) lt= lt->editlatt;
961         numVerts = *numVerts_r = lt->pntsu*lt->pntsv*lt->pntsw;
962         
963         vertexCos = MEM_mallocN(sizeof(*vertexCos)*numVerts,"lt_vcos");
964         
965         for (i=0; i<numVerts; i++) {
966                 copy_v3_v3(vertexCos[i], lt->def[i].vec);
967         }
968
969         return vertexCos;
970 }
971
972 void lattice_applyVertexCos(struct Object *ob, float (*vertexCos)[3])
973 {
974         Lattice *lt = ob->data;
975         int i, numVerts = lt->pntsu*lt->pntsv*lt->pntsw;
976
977         for (i=0; i<numVerts; i++) {
978                 copy_v3_v3(lt->def[i].vec, vertexCos[i]);
979         }
980 }
981
982 void lattice_calc_modifiers(Scene *scene, Object *ob)
983 {
984         Lattice *lt= ob->data;
985         ModifierData *md = modifiers_getVirtualModifierList(ob);
986         float (*vertexCos)[3] = NULL;
987         int numVerts, editmode = (lt->editlatt!=NULL);
988
989         freedisplist(&ob->disp);
990
991         for (; md; md=md->next) {
992                 ModifierTypeInfo *mti = modifierType_getInfo(md->type);
993
994                 md->scene= scene;
995                 
996                 if (!(md->mode&eModifierMode_Realtime)) continue;
997                 if (editmode && !(md->mode&eModifierMode_Editmode)) continue;
998                 if (mti->isDisabled && mti->isDisabled(md, 0)) continue;
999                 if (mti->type!=eModifierTypeType_OnlyDeform) continue;
1000
1001                 if (!vertexCos) vertexCos = lattice_getVertexCos(ob, &numVerts);
1002                 mti->deformVerts(md, ob, NULL, vertexCos, numVerts, 0, 0);
1003         }
1004
1005         /* always displist to make this work like derivedmesh */
1006         if (!vertexCos) vertexCos = lattice_getVertexCos(ob, &numVerts);
1007         
1008         {
1009                 DispList *dl = MEM_callocN(sizeof(*dl), "lt_dl");
1010                 dl->type = DL_VERTS;
1011                 dl->parts = 1;
1012                 dl->nr = numVerts;
1013                 dl->verts = (float*) vertexCos;
1014                 
1015                 BLI_addtail(&ob->disp, dl);
1016         }
1017 }
1018
1019 struct MDeformVert* lattice_get_deform_verts(struct Object *oblatt)
1020 {
1021         if(oblatt->type == OB_LATTICE)
1022         {
1023                 Lattice *lt = (Lattice*)oblatt->data;
1024                 if(lt->editlatt) lt= lt->editlatt;
1025                 return lt->dvert;
1026         }
1027
1028         return NULL;    
1029 }
1030