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