|
Blender
V2.59
|
00001 /* 00002 * lattice.c 00003 * 00004 * 00005 * $Id: lattice.c 36980 2011-05-28 13:11:24Z lmg $ 00006 * 00007 * ***** BEGIN GPL LICENSE BLOCK ***** 00008 * 00009 * This program is free software; you can redistribute it and/or 00010 * modify it under the terms of the GNU General Public License 00011 * as published by the Free Software Foundation; either version 2 00012 * of the License, or (at your option) any later version. 00013 * 00014 * This program is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 * GNU General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU General Public License 00020 * along with this program; if not, write to the Free Software Foundation, 00021 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00022 * 00023 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. 00024 * All rights reserved. 00025 * 00026 * The Original Code is: all of this file. 00027 * 00028 * Contributor(s): none yet. 00029 * 00030 * ***** END GPL LICENSE BLOCK ***** 00031 */ 00032 00039 #include <stdio.h> 00040 #include <string.h> 00041 #include <math.h> 00042 #include <stdlib.h> 00043 00044 #include "MEM_guardedalloc.h" 00045 00046 #include "BLI_blenlib.h" 00047 #include "BLI_math.h" 00048 #include "BLI_utildefines.h" 00049 00050 #include "DNA_mesh_types.h" 00051 #include "DNA_meshdata_types.h" 00052 #include "DNA_scene_types.h" 00053 #include "DNA_object_types.h" 00054 #include "DNA_lattice_types.h" 00055 #include "DNA_curve_types.h" 00056 #include "DNA_key_types.h" 00057 00058 #include "BKE_animsys.h" 00059 #include "BKE_anim.h" 00060 #include "BKE_cdderivedmesh.h" 00061 #include "BKE_displist.h" 00062 #include "BKE_global.h" 00063 #include "BKE_key.h" 00064 #include "BKE_lattice.h" 00065 #include "BKE_library.h" 00066 #include "BKE_main.h" 00067 #include "BKE_mesh.h" 00068 #include "BKE_modifier.h" 00069 00070 #include "BKE_deform.h" 00071 00072 //XXX #include "BIF_editdeform.h" 00073 00074 void calc_lat_fudu(int flag, int res, float *fu, float *du) 00075 { 00076 if(res==1) { 00077 *fu= 0.0; 00078 *du= 0.0; 00079 } 00080 else if(flag & LT_GRID) { 00081 *fu= -0.5f*(res-1); 00082 *du= 1.0f; 00083 } 00084 else { 00085 *fu= -1.0f; 00086 *du= 2.0f/(res-1); 00087 } 00088 } 00089 00090 void resizelattice(Lattice *lt, int uNew, int vNew, int wNew, Object *ltOb) 00091 { 00092 BPoint *bp; 00093 int i, u, v, w; 00094 float fu, fv, fw, uc, vc, wc, du=0.0, dv=0.0, dw=0.0; 00095 float *co, (*vertexCos)[3] = NULL; 00096 00097 /* vertex weight groups are just freed all for now */ 00098 if(lt->dvert) { 00099 free_dverts(lt->dvert, lt->pntsu*lt->pntsv*lt->pntsw); 00100 lt->dvert= NULL; 00101 } 00102 00103 while(uNew*vNew*wNew > 32000) { 00104 if( uNew>=vNew && uNew>=wNew) uNew--; 00105 else if( vNew>=uNew && vNew>=wNew) vNew--; 00106 else wNew--; 00107 } 00108 00109 vertexCos = MEM_mallocN(sizeof(*vertexCos)*uNew*vNew*wNew, "tmp_vcos"); 00110 00111 calc_lat_fudu(lt->flag, uNew, &fu, &du); 00112 calc_lat_fudu(lt->flag, vNew, &fv, &dv); 00113 calc_lat_fudu(lt->flag, wNew, &fw, &dw); 00114 00115 /* If old size is different then resolution changed in interface, 00116 * try to do clever reinit of points. Pretty simply idea, we just 00117 * deform new verts by old lattice, but scaling them to match old 00118 * size first. 00119 */ 00120 if (ltOb) { 00121 if (uNew!=1 && lt->pntsu!=1) { 00122 fu = lt->fu; 00123 du = (lt->pntsu-1)*lt->du/(uNew-1); 00124 } 00125 00126 if (vNew!=1 && lt->pntsv!=1) { 00127 fv = lt->fv; 00128 dv = (lt->pntsv-1)*lt->dv/(vNew-1); 00129 } 00130 00131 if (wNew!=1 && lt->pntsw!=1) { 00132 fw = lt->fw; 00133 dw = (lt->pntsw-1)*lt->dw/(wNew-1); 00134 } 00135 } 00136 00137 co = vertexCos[0]; 00138 for(w=0,wc=fw; w<wNew; w++,wc+=dw) { 00139 for(v=0,vc=fv; v<vNew; v++,vc+=dv) { 00140 for(u=0,uc=fu; u<uNew; u++,co+=3,uc+=du) { 00141 co[0] = uc; 00142 co[1] = vc; 00143 co[2] = wc; 00144 } 00145 } 00146 } 00147 00148 if (ltOb) { 00149 float mat[4][4]; 00150 int typeu = lt->typeu, typev = lt->typev, typew = lt->typew; 00151 00152 /* works best if we force to linear type (endpoints match) */ 00153 lt->typeu = lt->typev = lt->typew = KEY_LINEAR; 00154 00155 /* prevent using deformed locations */ 00156 freedisplist(<Ob->disp); 00157 00158 copy_m4_m4(mat, ltOb->obmat); 00159 unit_m4(ltOb->obmat); 00160 lattice_deform_verts(ltOb, NULL, NULL, vertexCos, uNew*vNew*wNew, NULL); 00161 copy_m4_m4(ltOb->obmat, mat); 00162 00163 lt->typeu = typeu; 00164 lt->typev = typev; 00165 lt->typew = typew; 00166 } 00167 00168 lt->fu = fu; 00169 lt->fv = fv; 00170 lt->fw = fw; 00171 lt->du = du; 00172 lt->dv = dv; 00173 lt->dw = dw; 00174 00175 lt->pntsu = uNew; 00176 lt->pntsv = vNew; 00177 lt->pntsw = wNew; 00178 00179 MEM_freeN(lt->def); 00180 lt->def= MEM_callocN(lt->pntsu*lt->pntsv*lt->pntsw*sizeof(BPoint), "lattice bp"); 00181 00182 bp= lt->def; 00183 00184 for (i=0; i<lt->pntsu*lt->pntsv*lt->pntsw; i++,bp++) { 00185 copy_v3_v3(bp->vec, vertexCos[i]); 00186 } 00187 00188 MEM_freeN(vertexCos); 00189 } 00190 00191 Lattice *add_lattice(const char *name) 00192 { 00193 Lattice *lt; 00194 00195 lt= alloc_libblock(&G.main->latt, ID_LT, name); 00196 00197 lt->flag= LT_GRID; 00198 00199 lt->typeu= lt->typev= lt->typew= KEY_BSPLINE; 00200 00201 lt->def= MEM_callocN(sizeof(BPoint), "lattvert"); /* temporary */ 00202 resizelattice(lt, 2, 2, 2, NULL); /* creates a uniform lattice */ 00203 00204 return lt; 00205 } 00206 00207 Lattice *copy_lattice(Lattice *lt) 00208 { 00209 Lattice *ltn; 00210 00211 ltn= copy_libblock(lt); 00212 ltn->def= MEM_dupallocN(lt->def); 00213 00214 ltn->key= copy_key(ltn->key); 00215 if(ltn->key) ltn->key->from= (ID *)ltn; 00216 00217 if(lt->dvert) { 00218 int tot= lt->pntsu*lt->pntsv*lt->pntsw; 00219 ltn->dvert = MEM_mallocN (sizeof (MDeformVert)*tot, "Lattice MDeformVert"); 00220 copy_dverts(ltn->dvert, lt->dvert, tot); 00221 } 00222 00223 ltn->editlatt= NULL; 00224 00225 return ltn; 00226 } 00227 00228 void free_lattice(Lattice *lt) 00229 { 00230 if(lt->def) MEM_freeN(lt->def); 00231 if(lt->dvert) free_dverts(lt->dvert, lt->pntsu*lt->pntsv*lt->pntsw); 00232 if(lt->editlatt) { 00233 Lattice *editlt= lt->editlatt->latt; 00234 00235 if(editlt->def) MEM_freeN(editlt->def); 00236 if(editlt->dvert) free_dverts(editlt->dvert, lt->pntsu*lt->pntsv*lt->pntsw); 00237 00238 MEM_freeN(editlt); 00239 MEM_freeN(lt->editlatt); 00240 } 00241 00242 /* free animation data */ 00243 if (lt->adt) { 00244 BKE_free_animdata(<->id); 00245 lt->adt= NULL; 00246 } 00247 } 00248 00249 00250 void make_local_lattice(Lattice *lt) 00251 { 00252 Main *bmain= G.main; 00253 Object *ob; 00254 int local=0, lib=0; 00255 00256 /* - only lib users: do nothing 00257 * - only local users: set flag 00258 * - mixed: make copy 00259 */ 00260 00261 if(lt->id.lib==NULL) return; 00262 if(lt->id.us==1) { 00263 lt->id.lib= NULL; 00264 lt->id.flag= LIB_LOCAL; 00265 new_id(&bmain->latt, (ID *)lt, NULL); 00266 return; 00267 } 00268 00269 for(ob= bmain->object.first; ob && ELEM(0, lib, local); ob= ob->id.next) { 00270 if(ob->data==lt) { 00271 if(ob->id.lib) lib= 1; 00272 else local= 1; 00273 } 00274 } 00275 00276 if(local && lib==0) { 00277 lt->id.lib= NULL; 00278 lt->id.flag= LIB_LOCAL; 00279 new_id(&bmain->latt, (ID *)lt, NULL); 00280 } 00281 else if(local && lib) { 00282 Lattice *ltn= copy_lattice(lt); 00283 ltn->id.us= 0; 00284 00285 for(ob= bmain->object.first; ob; ob= ob->id.next) { 00286 if(ob->data==lt) { 00287 if(ob->id.lib==NULL) { 00288 ob->data= ltn; 00289 ltn->id.us++; 00290 lt->id.us--; 00291 } 00292 } 00293 } 00294 } 00295 } 00296 00297 void init_latt_deform(Object *oblatt, Object *ob) 00298 { 00299 /* we make an array with all differences */ 00300 Lattice *lt= oblatt->data; 00301 BPoint *bp; 00302 DispList *dl = find_displist(&oblatt->disp, DL_VERTS); 00303 float *co = dl?dl->verts:NULL; 00304 float *fp, imat[4][4]; 00305 float fu, fv, fw; 00306 int u, v, w; 00307 00308 if(lt->editlatt) lt= lt->editlatt->latt; 00309 bp = lt->def; 00310 00311 fp= lt->latticedata= MEM_mallocN(sizeof(float)*3*lt->pntsu*lt->pntsv*lt->pntsw, "latticedata"); 00312 00313 /* for example with a particle system: ob==0 */ 00314 if(ob==NULL) { 00315 /* in deformspace, calc matrix */ 00316 invert_m4_m4(lt->latmat, oblatt->obmat); 00317 00318 /* back: put in deform array */ 00319 invert_m4_m4(imat, lt->latmat); 00320 } 00321 else { 00322 /* in deformspace, calc matrix */ 00323 invert_m4_m4(imat, oblatt->obmat); 00324 mul_m4_m4m4(lt->latmat, ob->obmat, imat); 00325 00326 /* back: put in deform array */ 00327 invert_m4_m4(imat, lt->latmat); 00328 } 00329 00330 for(w=0,fw=lt->fw; w<lt->pntsw; w++,fw+=lt->dw) { 00331 for(v=0,fv=lt->fv; v<lt->pntsv; v++, fv+=lt->dv) { 00332 for(u=0,fu=lt->fu; u<lt->pntsu; u++, bp++, co+=3, fp+=3, fu+=lt->du) { 00333 if (dl) { 00334 fp[0] = co[0] - fu; 00335 fp[1] = co[1] - fv; 00336 fp[2] = co[2] - fw; 00337 } else { 00338 fp[0] = bp->vec[0] - fu; 00339 fp[1] = bp->vec[1] - fv; 00340 fp[2] = bp->vec[2] - fw; 00341 } 00342 00343 mul_mat3_m4_v3(imat, fp); 00344 } 00345 } 00346 } 00347 } 00348 00349 void calc_latt_deform(Object *ob, float *co, float weight) 00350 { 00351 Lattice *lt= ob->data; 00352 float u, v, w, tu[4], tv[4], tw[4]; 00353 float vec[3]; 00354 int idx_w, idx_v, idx_u; 00355 int ui, vi, wi, uu, vv, ww; 00356 00357 /* vgroup influence */ 00358 int defgroup_nr= -1; 00359 float co_prev[3], weight_blend= 0.0f; 00360 MDeformVert *dvert= lattice_get_deform_verts(ob); 00361 00362 00363 if(lt->editlatt) lt= lt->editlatt->latt; 00364 if(lt->latticedata==NULL) return; 00365 00366 if(lt->vgroup[0] && dvert) { 00367 defgroup_nr= defgroup_name_index(ob, lt->vgroup); 00368 copy_v3_v3(co_prev, co); 00369 } 00370 00371 /* co is in local coords, treat with latmat */ 00372 mul_v3_m4v3(vec, lt->latmat, co); 00373 00374 /* u v w coords */ 00375 00376 if(lt->pntsu>1) { 00377 u= (vec[0]-lt->fu)/lt->du; 00378 ui= (int)floor(u); 00379 u -= ui; 00380 key_curve_position_weights(u, tu, lt->typeu); 00381 } 00382 else { 00383 tu[0]= tu[2]= tu[3]= 0.0; tu[1]= 1.0; 00384 ui= 0; 00385 } 00386 00387 if(lt->pntsv>1) { 00388 v= (vec[1]-lt->fv)/lt->dv; 00389 vi= (int)floor(v); 00390 v -= vi; 00391 key_curve_position_weights(v, tv, lt->typev); 00392 } 00393 else { 00394 tv[0]= tv[2]= tv[3]= 0.0; tv[1]= 1.0; 00395 vi= 0; 00396 } 00397 00398 if(lt->pntsw>1) { 00399 w= (vec[2]-lt->fw)/lt->dw; 00400 wi= (int)floor(w); 00401 w -= wi; 00402 key_curve_position_weights(w, tw, lt->typew); 00403 } 00404 else { 00405 tw[0]= tw[2]= tw[3]= 0.0; tw[1]= 1.0; 00406 wi= 0; 00407 } 00408 00409 for(ww= wi-1; ww<=wi+2; ww++) { 00410 w= tw[ww-wi+1]; 00411 00412 if(w != 0.0f) { 00413 if(ww>0) { 00414 if(ww<lt->pntsw) idx_w= ww*lt->pntsu*lt->pntsv; 00415 else idx_w= (lt->pntsw-1)*lt->pntsu*lt->pntsv; 00416 } 00417 else idx_w= 0; 00418 00419 for(vv= vi-1; vv<=vi+2; vv++) { 00420 v= w*tv[vv-vi+1]; 00421 00422 if(v != 0.0f) { 00423 if(vv>0) { 00424 if(vv<lt->pntsv) idx_v= idx_w + vv*lt->pntsu; 00425 else idx_v= idx_w + (lt->pntsv-1)*lt->pntsu; 00426 } 00427 else idx_v= idx_w; 00428 00429 for(uu= ui-1; uu<=ui+2; uu++) { 00430 u= weight*v*tu[uu-ui+1]; 00431 00432 if(u != 0.0f) { 00433 if(uu>0) { 00434 if(uu<lt->pntsu) idx_u= idx_v + uu; 00435 else idx_u= idx_v + (lt->pntsu-1); 00436 } 00437 else idx_u= idx_v; 00438 00439 madd_v3_v3fl(co, <->latticedata[idx_u * 3], u); 00440 00441 if(defgroup_nr != -1) 00442 weight_blend += (u * defvert_find_weight(dvert + idx_u, defgroup_nr)); 00443 } 00444 } 00445 } 00446 } 00447 } 00448 } 00449 00450 if(defgroup_nr != -1) 00451 interp_v3_v3v3(co, co_prev, co, weight_blend); 00452 00453 } 00454 00455 void end_latt_deform(Object *ob) 00456 { 00457 Lattice *lt= ob->data; 00458 00459 if(lt->editlatt) lt= lt->editlatt->latt; 00460 00461 if(lt->latticedata) 00462 MEM_freeN(lt->latticedata); 00463 lt->latticedata= NULL; 00464 } 00465 00466 /* calculations is in local space of deformed object 00467 so we store in latmat transform from path coord inside object 00468 */ 00469 typedef struct { 00470 float dmin[3], dmax[3], dsize, dloc[3]; 00471 float curvespace[4][4], objectspace[4][4], objectspace3[3][3]; 00472 int no_rot_axis; 00473 } CurveDeform; 00474 00475 static void init_curve_deform(Object *par, Object *ob, CurveDeform *cd, int dloc) 00476 { 00477 invert_m4_m4(ob->imat, ob->obmat); 00478 mul_m4_m4m4(cd->objectspace, par->obmat, ob->imat); 00479 invert_m4_m4(cd->curvespace, cd->objectspace); 00480 copy_m3_m4(cd->objectspace3, cd->objectspace); 00481 00482 // offset vector for 'no smear' 00483 if(dloc) { 00484 invert_m4_m4(par->imat, par->obmat); 00485 mul_v3_m4v3(cd->dloc, par->imat, ob->obmat[3]); 00486 } 00487 else { 00488 cd->dloc[0]=cd->dloc[1]=cd->dloc[2]= 0.0f; 00489 } 00490 00491 cd->no_rot_axis= 0; 00492 } 00493 00494 /* this makes sure we can extend for non-cyclic. *vec needs 4 items! */ 00495 static int where_on_path_deform(Object *ob, float ctime, float *vec, float *dir, float *quat, float *radius) /* returns OK */ 00496 { 00497 Curve *cu= ob->data; 00498 BevList *bl; 00499 float ctime1; 00500 int cycl=0; 00501 00502 /* test for cyclic */ 00503 bl= cu->bev.first; 00504 if (!bl->nr) return 0; 00505 if(bl && bl->poly> -1) cycl= 1; 00506 00507 if(cycl==0) { 00508 ctime1= CLAMPIS(ctime, 0.0f, 1.0f); 00509 } 00510 else ctime1= ctime; 00511 00512 /* vec needs 4 items */ 00513 if(where_on_path(ob, ctime1, vec, dir, quat, radius, NULL)) { 00514 00515 if(cycl==0) { 00516 Path *path= cu->path; 00517 float dvec[3]; 00518 00519 if(ctime < 0.0f) { 00520 sub_v3_v3v3(dvec, path->data[1].vec, path->data[0].vec); 00521 mul_v3_fl(dvec, ctime*(float)path->len); 00522 add_v3_v3(vec, dvec); 00523 if(quat) copy_qt_qt(quat, path->data[0].quat); 00524 if(radius) *radius= path->data[0].radius; 00525 } 00526 else if(ctime > 1.0f) { 00527 sub_v3_v3v3(dvec, path->data[path->len-1].vec, path->data[path->len-2].vec); 00528 mul_v3_fl(dvec, (ctime-1.0f)*(float)path->len); 00529 add_v3_v3(vec, dvec); 00530 if(quat) copy_qt_qt(quat, path->data[path->len-1].quat); 00531 if(radius) *radius= path->data[path->len-1].radius; 00532 /* weight - not used but could be added */ 00533 } 00534 } 00535 return 1; 00536 } 00537 return 0; 00538 } 00539 00540 /* for each point, rotate & translate to curve */ 00541 /* use path, since it has constant distances */ 00542 /* co: local coord, result local too */ 00543 /* returns quaternion for rotation, using cd->no_rot_axis */ 00544 /* axis is using another define!!! */ 00545 static int calc_curve_deform(Scene *scene, Object *par, float *co, short axis, CurveDeform *cd, float *quatp) 00546 { 00547 Curve *cu= par->data; 00548 float fac, loc[4], dir[3], new_quat[4], radius; 00549 short /*upflag, */ index; 00550 00551 index= axis-1; 00552 if(index>2) 00553 index -= 3; /* negative */ 00554 00555 /* to be sure, mostly after file load */ 00556 if(cu->path==NULL) { 00557 makeDispListCurveTypes(scene, par, 0); 00558 if(cu->path==NULL) return 0; // happens on append... 00559 } 00560 00561 /* options */ 00562 if(ELEM3(axis, OB_NEGX+1, OB_NEGY+1, OB_NEGZ+1)) { /* OB_NEG# 0-5, MOD_CURVE_POS# 1-6 */ 00563 if(cu->flag & CU_STRETCH) 00564 fac= (-co[index]-cd->dmax[index])/(cd->dmax[index] - cd->dmin[index]); 00565 else 00566 fac= (cd->dloc[index])/(cu->path->totdist) - (co[index]-cd->dmax[index])/(cu->path->totdist); 00567 } 00568 else { 00569 if(cu->flag & CU_STRETCH) 00570 fac= (co[index]-cd->dmin[index])/(cd->dmax[index] - cd->dmin[index]); 00571 else 00572 fac= (cd->dloc[index])/(cu->path->totdist) + (co[index]-cd->dmin[index])/(cu->path->totdist); 00573 } 00574 00575 #if 0 // XXX old animation system 00576 /* we want the ipo to work on the default 100 frame range, because there's no 00577 actual time involved in path position */ 00578 // huh? by WHY!!!!???? - Aligorith 00579 if(cu->ipo) { 00580 fac*= 100.0f; 00581 if(calc_ipo_spec(cu->ipo, CU_SPEED, &fac)==0) 00582 fac/= 100.0; 00583 } 00584 #endif // XXX old animation system 00585 00586 if( where_on_path_deform(par, fac, loc, dir, new_quat, &radius)) { /* returns OK */ 00587 float quat[4], cent[3]; 00588 00589 #if 0 // XXX - 2.4x Z-Up, Now use bevel tilt. 00590 if(cd->no_rot_axis) /* set by caller */ 00591 dir[cd->no_rot_axis-1]= 0.0f; 00592 00593 /* -1 for compatibility with old track defines */ 00594 vec_to_quat( quat,dir, axis-1, upflag); 00595 00596 /* the tilt */ 00597 if(loc[3]!=0.0) { 00598 normalize_v3(dir); 00599 q[0]= (float)cos(0.5*loc[3]); 00600 fac= (float)sin(0.5*loc[3]); 00601 q[1]= -fac*dir[0]; 00602 q[2]= -fac*dir[1]; 00603 q[3]= -fac*dir[2]; 00604 mul_qt_qtqt(quat, q, quat); 00605 } 00606 #endif 00607 00608 if(cd->no_rot_axis) { /* set by caller */ 00609 00610 /* this is not exactly the same as 2.4x, since the axis is having rotation removed rather than 00611 * changing the axis before calculating the tilt but serves much the same purpose */ 00612 float dir_flat[3]={0,0,0}, q[4]; 00613 copy_v3_v3(dir_flat, dir); 00614 dir_flat[cd->no_rot_axis-1]= 0.0f; 00615 00616 normalize_v3(dir); 00617 normalize_v3(dir_flat); 00618 00619 rotation_between_vecs_to_quat(q, dir, dir_flat); /* Could this be done faster? */ 00620 00621 mul_qt_qtqt(new_quat, q, new_quat); 00622 } 00623 00624 00625 /* Logic for 'cent' orientation * 00626 * 00627 * The way 'co' is copied to 'cent' may seem to have no meaning, but it does. 00628 * 00629 * Use a curve modifier to stretch a cube out, color each side RGB, positive side light, negative dark. 00630 * view with X up (default), from the angle that you can see 3 faces RGB colors (light), anti-clockwise 00631 * Notice X,Y,Z Up all have light colors and each ordered CCW. 00632 * 00633 * Now for Neg Up XYZ, the colors are all dark, and ordered clockwise - Campbell 00634 * 00635 * note: moved functions into quat_apply_track/vec_apply_track 00636 * */ 00637 copy_qt_qt(quat, new_quat); 00638 copy_v3_v3(cent, co); 00639 00640 /* zero the axis which is not used, 00641 * the big block of text above now applies to these 3 lines */ 00642 quat_apply_track(quat, axis-1, (axis==1 || axis==3) ? 1:0); /* up flag is a dummy, set so no rotation is done */ 00643 vec_apply_track(cent, axis-1); 00644 cent[axis < 4 ? axis-1 : axis-4]= 0.0f; 00645 00646 00647 /* scale if enabled */ 00648 if(cu->flag & CU_PATH_RADIUS) 00649 mul_v3_fl(cent, radius); 00650 00651 /* local rotation */ 00652 normalize_qt(quat); 00653 mul_qt_v3(quat, cent); 00654 00655 /* translation */ 00656 add_v3_v3v3(co, cent, loc); 00657 00658 if(quatp) 00659 copy_qt_qt(quatp, quat); 00660 00661 return 1; 00662 } 00663 return 0; 00664 } 00665 00666 void curve_deform_verts(Scene *scene, Object *cuOb, Object *target, DerivedMesh *dm, float (*vertexCos)[3], int numVerts, char *vgroup, short defaxis) 00667 { 00668 Curve *cu; 00669 int a, flag; 00670 CurveDeform cd; 00671 int use_vgroups; 00672 00673 if(cuOb->type != OB_CURVE) 00674 return; 00675 00676 cu = cuOb->data; 00677 flag = cu->flag; 00678 cu->flag |= (CU_PATH|CU_FOLLOW); // needed for path & bevlist 00679 00680 init_curve_deform(cuOb, target, &cd, (cu->flag & CU_STRETCH)==0); 00681 00682 /* dummy bounds, keep if CU_DEFORM_BOUNDS_OFF is set */ 00683 if(defaxis < 3) { 00684 cd.dmin[0]= cd.dmin[1]= cd.dmin[2]= 0.0f; 00685 cd.dmax[0]= cd.dmax[1]= cd.dmax[2]= 1.0f; 00686 } 00687 else { 00688 /* negative, these bounds give a good rest position */ 00689 cd.dmin[0]= cd.dmin[1]= cd.dmin[2]= -1.0f; 00690 cd.dmax[0]= cd.dmax[1]= cd.dmax[2]= 0.0f; 00691 } 00692 00693 /* check whether to use vertex groups (only possible if target is a Mesh) 00694 * we want either a Mesh with no derived data, or derived data with 00695 * deformverts 00696 */ 00697 if(target && target->type==OB_MESH) { 00698 /* if there's derived data without deformverts, don't use vgroups */ 00699 if(dm && !dm->getVertData(dm, 0, CD_MDEFORMVERT)) 00700 use_vgroups = 0; 00701 else 00702 use_vgroups = 1; 00703 } else 00704 use_vgroups = 0; 00705 00706 if(vgroup && vgroup[0] && use_vgroups) { 00707 Mesh *me= target->data; 00708 int index= defgroup_name_index(target, vgroup); 00709 00710 if(index != -1 && (me->dvert || dm)) { 00711 MDeformVert *dvert = me->dvert; 00712 float vec[3]; 00713 float weight; 00714 00715 00716 if(cu->flag & CU_DEFORM_BOUNDS_OFF) { 00717 /* dummy bounds */ 00718 cd.dmin[0]= cd.dmin[1]= cd.dmin[2]= 0.0f; 00719 cd.dmax[0]= cd.dmax[1]= cd.dmax[2]= 1.0f; 00720 00721 dvert = me->dvert; 00722 for(a = 0; a < numVerts; a++, dvert++) { 00723 if(dm) dvert = dm->getVertData(dm, a, CD_MDEFORMVERT); 00724 weight= defvert_find_weight(dvert, index); 00725 00726 if(weight > 0.0f) { 00727 mul_m4_v3(cd.curvespace, vertexCos[a]); 00728 copy_v3_v3(vec, vertexCos[a]); 00729 calc_curve_deform(scene, cuOb, vec, defaxis, &cd, NULL); 00730 interp_v3_v3v3(vertexCos[a], vertexCos[a], vec, weight); 00731 mul_m4_v3(cd.objectspace, vertexCos[a]); 00732 } 00733 } 00734 } 00735 else { 00736 /* set mesh min/max bounds */ 00737 INIT_MINMAX(cd.dmin, cd.dmax); 00738 00739 for(a = 0; a < numVerts; a++, dvert++) { 00740 if(dm) dvert = dm->getVertData(dm, a, CD_MDEFORMVERT); 00741 00742 if(defvert_find_weight(dvert, index) > 0.0f) { 00743 mul_m4_v3(cd.curvespace, vertexCos[a]); 00744 DO_MINMAX(vertexCos[a], cd.dmin, cd.dmax); 00745 } 00746 } 00747 00748 dvert = me->dvert; 00749 for(a = 0; a < numVerts; a++, dvert++) { 00750 if(dm) dvert = dm->getVertData(dm, a, CD_MDEFORMVERT); 00751 00752 weight= defvert_find_weight(dvert, index); 00753 00754 if(weight > 0.0f) { 00755 copy_v3_v3(vec, vertexCos[a]); 00756 calc_curve_deform(scene, cuOb, vec, defaxis, &cd, NULL); 00757 interp_v3_v3v3(vertexCos[a], vertexCos[a], vec, weight); 00758 mul_m4_v3(cd.objectspace, vertexCos[a]); 00759 } 00760 } 00761 } 00762 } 00763 } 00764 else { 00765 if(cu->flag & CU_DEFORM_BOUNDS_OFF) { 00766 for(a = 0; a < numVerts; a++) { 00767 mul_m4_v3(cd.curvespace, vertexCos[a]); 00768 calc_curve_deform(scene, cuOb, vertexCos[a], defaxis, &cd, NULL); 00769 mul_m4_v3(cd.objectspace, vertexCos[a]); 00770 } 00771 } 00772 else { 00773 /* set mesh min max bounds */ 00774 INIT_MINMAX(cd.dmin, cd.dmax); 00775 00776 for(a = 0; a < numVerts; a++) { 00777 mul_m4_v3(cd.curvespace, vertexCos[a]); 00778 DO_MINMAX(vertexCos[a], cd.dmin, cd.dmax); 00779 } 00780 00781 for(a = 0; a < numVerts; a++) { 00782 calc_curve_deform(scene, cuOb, vertexCos[a], defaxis, &cd, NULL); 00783 mul_m4_v3(cd.objectspace, vertexCos[a]); 00784 } 00785 } 00786 } 00787 cu->flag = flag; 00788 } 00789 00790 /* input vec and orco = local coord in armature space */ 00791 /* orco is original not-animated or deformed reference point */ 00792 /* result written in vec and mat */ 00793 void curve_deform_vector(Scene *scene, Object *cuOb, Object *target, float *orco, float *vec, float mat[][3], int no_rot_axis) 00794 { 00795 CurveDeform cd; 00796 float quat[4]; 00797 00798 if(cuOb->type != OB_CURVE) { 00799 unit_m3(mat); 00800 return; 00801 } 00802 00803 init_curve_deform(cuOb, target, &cd, 0); /* 0 no dloc */ 00804 cd.no_rot_axis= no_rot_axis; /* option to only rotate for XY, for example */ 00805 00806 copy_v3_v3(cd.dmin, orco); 00807 copy_v3_v3(cd.dmax, orco); 00808 00809 mul_m4_v3(cd.curvespace, vec); 00810 00811 if(calc_curve_deform(scene, cuOb, vec, target->trackflag+1, &cd, quat)) { 00812 float qmat[3][3]; 00813 00814 quat_to_mat3( qmat,quat); 00815 mul_m3_m3m3(mat, qmat, cd.objectspace3); 00816 } 00817 else 00818 unit_m3(mat); 00819 00820 mul_m4_v3(cd.objectspace, vec); 00821 00822 } 00823 00824 void lattice_deform_verts(Object *laOb, Object *target, DerivedMesh *dm, 00825 float (*vertexCos)[3], int numVerts, char *vgroup) 00826 { 00827 int a; 00828 int use_vgroups; 00829 00830 if(laOb->type != OB_LATTICE) 00831 return; 00832 00833 init_latt_deform(laOb, target); 00834 00835 /* check whether to use vertex groups (only possible if target is a Mesh) 00836 * we want either a Mesh with no derived data, or derived data with 00837 * deformverts 00838 */ 00839 if(target && target->type==OB_MESH) { 00840 /* if there's derived data without deformverts, don't use vgroups */ 00841 if(dm && !dm->getVertData(dm, 0, CD_MDEFORMVERT)) 00842 use_vgroups = 0; 00843 else 00844 use_vgroups = 1; 00845 } else 00846 use_vgroups = 0; 00847 00848 if(vgroup && vgroup[0] && use_vgroups) { 00849 Mesh *me = target->data; 00850 int index = defgroup_name_index(target, vgroup); 00851 float weight; 00852 00853 if(index >= 0 && (me->dvert || dm)) { 00854 MDeformVert *dvert = me->dvert; 00855 00856 for(a = 0; a < numVerts; a++, dvert++) { 00857 if(dm) dvert = dm->getVertData(dm, a, CD_MDEFORMVERT); 00858 00859 weight= defvert_find_weight(dvert, index); 00860 00861 if(weight > 0.0f) 00862 calc_latt_deform(laOb, vertexCos[a], weight); 00863 } 00864 } 00865 } else { 00866 for(a = 0; a < numVerts; a++) { 00867 calc_latt_deform(laOb, vertexCos[a], 1.0f); 00868 } 00869 } 00870 end_latt_deform(laOb); 00871 } 00872 00873 int object_deform_mball(Object *ob, ListBase *dispbase) 00874 { 00875 if(ob->parent && ob->parent->type==OB_LATTICE && ob->partype==PARSKEL) { 00876 DispList *dl; 00877 00878 for (dl=dispbase->first; dl; dl=dl->next) { 00879 lattice_deform_verts(ob->parent, ob, NULL, 00880 (float(*)[3]) dl->verts, dl->nr, NULL); 00881 } 00882 00883 return 1; 00884 } else { 00885 return 0; 00886 } 00887 } 00888 00889 static BPoint *latt_bp(Lattice *lt, int u, int v, int w) 00890 { 00891 return lt->def+ u + v*lt->pntsu + w*lt->pntsu*lt->pntsv; 00892 } 00893 00894 void outside_lattice(Lattice *lt) 00895 { 00896 BPoint *bp, *bp1, *bp2; 00897 int u, v, w; 00898 float fac1, du=0.0, dv=0.0, dw=0.0; 00899 00900 if(lt->flag & LT_OUTSIDE) { 00901 bp= lt->def; 00902 00903 if(lt->pntsu>1) du= 1.0f/((float)lt->pntsu-1); 00904 if(lt->pntsv>1) dv= 1.0f/((float)lt->pntsv-1); 00905 if(lt->pntsw>1) dw= 1.0f/((float)lt->pntsw-1); 00906 00907 for(w=0; w<lt->pntsw; w++) { 00908 00909 for(v=0; v<lt->pntsv; v++) { 00910 00911 for(u=0; u<lt->pntsu; u++, bp++) { 00912 if(u==0 || v==0 || w==0 || u==lt->pntsu-1 || v==lt->pntsv-1 || w==lt->pntsw-1); 00913 else { 00914 00915 bp->hide= 1; 00916 bp->f1 &= ~SELECT; 00917 00918 /* u extrema */ 00919 bp1= latt_bp(lt, 0, v, w); 00920 bp2= latt_bp(lt, lt->pntsu-1, v, w); 00921 00922 fac1= du*u; 00923 bp->vec[0]= (1.0f-fac1)*bp1->vec[0] + fac1*bp2->vec[0]; 00924 bp->vec[1]= (1.0f-fac1)*bp1->vec[1] + fac1*bp2->vec[1]; 00925 bp->vec[2]= (1.0f-fac1)*bp1->vec[2] + fac1*bp2->vec[2]; 00926 00927 /* v extrema */ 00928 bp1= latt_bp(lt, u, 0, w); 00929 bp2= latt_bp(lt, u, lt->pntsv-1, w); 00930 00931 fac1= dv*v; 00932 bp->vec[0]+= (1.0f-fac1)*bp1->vec[0] + fac1*bp2->vec[0]; 00933 bp->vec[1]+= (1.0f-fac1)*bp1->vec[1] + fac1*bp2->vec[1]; 00934 bp->vec[2]+= (1.0f-fac1)*bp1->vec[2] + fac1*bp2->vec[2]; 00935 00936 /* w extrema */ 00937 bp1= latt_bp(lt, u, v, 0); 00938 bp2= latt_bp(lt, u, v, lt->pntsw-1); 00939 00940 fac1= dw*w; 00941 bp->vec[0]+= (1.0f-fac1)*bp1->vec[0] + fac1*bp2->vec[0]; 00942 bp->vec[1]+= (1.0f-fac1)*bp1->vec[1] + fac1*bp2->vec[1]; 00943 bp->vec[2]+= (1.0f-fac1)*bp1->vec[2] + fac1*bp2->vec[2]; 00944 00945 mul_v3_fl(bp->vec, 0.3333333f); 00946 00947 } 00948 } 00949 00950 } 00951 00952 } 00953 } 00954 else { 00955 bp= lt->def; 00956 00957 for(w=0; w<lt->pntsw; w++) 00958 for(v=0; v<lt->pntsv; v++) 00959 for(u=0; u<lt->pntsu; u++, bp++) 00960 bp->hide= 0; 00961 } 00962 } 00963 00964 float (*lattice_getVertexCos(struct Object *ob, int *numVerts_r))[3] 00965 { 00966 Lattice *lt = ob->data; 00967 int i, numVerts; 00968 float (*vertexCos)[3]; 00969 00970 if(lt->editlatt) lt= lt->editlatt->latt; 00971 numVerts = *numVerts_r = lt->pntsu*lt->pntsv*lt->pntsw; 00972 00973 vertexCos = MEM_mallocN(sizeof(*vertexCos)*numVerts,"lt_vcos"); 00974 00975 for (i=0; i<numVerts; i++) { 00976 copy_v3_v3(vertexCos[i], lt->def[i].vec); 00977 } 00978 00979 return vertexCos; 00980 } 00981 00982 void lattice_applyVertexCos(struct Object *ob, float (*vertexCos)[3]) 00983 { 00984 Lattice *lt = ob->data; 00985 int i, numVerts = lt->pntsu*lt->pntsv*lt->pntsw; 00986 00987 for (i=0; i<numVerts; i++) { 00988 copy_v3_v3(lt->def[i].vec, vertexCos[i]); 00989 } 00990 } 00991 00992 void lattice_calc_modifiers(Scene *scene, Object *ob) 00993 { 00994 Lattice *lt= ob->data; 00995 ModifierData *md = modifiers_getVirtualModifierList(ob); 00996 float (*vertexCos)[3] = NULL; 00997 int numVerts, editmode = (lt->editlatt!=NULL); 00998 00999 freedisplist(&ob->disp); 01000 01001 for (; md; md=md->next) { 01002 ModifierTypeInfo *mti = modifierType_getInfo(md->type); 01003 01004 md->scene= scene; 01005 01006 if (!(md->mode&eModifierMode_Realtime)) continue; 01007 if (editmode && !(md->mode&eModifierMode_Editmode)) continue; 01008 if (mti->isDisabled && mti->isDisabled(md, 0)) continue; 01009 if (mti->type!=eModifierTypeType_OnlyDeform) continue; 01010 01011 if (!vertexCos) vertexCos = lattice_getVertexCos(ob, &numVerts); 01012 mti->deformVerts(md, ob, NULL, vertexCos, numVerts, 0, 0); 01013 } 01014 01015 /* always displist to make this work like derivedmesh */ 01016 if (!vertexCos) vertexCos = lattice_getVertexCos(ob, &numVerts); 01017 01018 { 01019 DispList *dl = MEM_callocN(sizeof(*dl), "lt_dl"); 01020 dl->type = DL_VERTS; 01021 dl->parts = 1; 01022 dl->nr = numVerts; 01023 dl->verts = (float*) vertexCos; 01024 01025 BLI_addtail(&ob->disp, dl); 01026 } 01027 } 01028 01029 struct MDeformVert* lattice_get_deform_verts(struct Object *oblatt) 01030 { 01031 Lattice *lt = (Lattice*)oblatt->data; 01032 BLI_assert(oblatt->type == OB_LATTICE); 01033 if(lt->editlatt) lt= lt->editlatt->latt; 01034 return lt->dvert; 01035 }