4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): none yet.
27 * ***** END GPL LICENSE BLOCK *****
28 * meshlaplacian.c: Algorithms using the mesh laplacian.
34 #include "MEM_guardedalloc.h"
36 #include "DNA_listBase.h"
37 #include "DNA_object_types.h"
38 #include "DNA_mesh_types.h"
39 #include "DNA_meshdata_types.h"
40 #include "DNA_modifier_types.h"
41 #include "DNA_scene_types.h"
44 #include "BLI_edgehash.h"
45 #include "BLI_memarena.h"
47 #include "BKE_DerivedMesh.h"
48 #include "BKE_utildefines.h"
51 #include "BLI_editVert.h"
52 #include "BLI_polardecomp.h"
55 #include "RE_raytrace.h"
57 #include "ONL_opennl.h"
59 #include "BLO_sys_types.h" // for intptr_t support
61 #include "ED_armature.h"
64 #include "meshlaplacian.h"
67 /* ************* XXX *************** */
68 static void waitcursor(int val) {}
69 static void progress_bar() {}
70 static void start_progress_bar() {}
71 static void end_progress_bar() {}
72 static void error(char *str) { printf("error: %s\n", str); }
73 /* ************* XXX *************** */
76 /************************** Laplacian System *****************************/
78 struct LaplacianSystem {
79 NLContext context; /* opennl context */
83 float **verts; /* vertex coordinates */
84 float *varea; /* vertex weights for laplacian computation */
85 char *vpinned; /* vertex pinning */
86 int (*faces)[3]; /* face vertex indices */
87 float (*fweights)[3]; /* cotangent weights per face */
89 int areaweights; /* use area in cotangent weights? */
90 int storeweights; /* store cotangent weights in fweights */
91 int nlbegun; /* nlBegin(NL_SYSTEM/NL_MATRIX) done */
93 EdgeHash *edgehash; /* edge hash for construction */
95 struct HeatWeighting {
99 float (*verts)[3]; /* vertex coordinates */
100 float (*vnors)[3]; /* vertex normals */
102 float (*root)[3]; /* bone root */
103 float (*tip)[3]; /* bone tip */
104 float (*source)[3]; /* vertex source */
107 float *H; /* diagonal H matrix */
108 float *p; /* values from all p vectors */
109 float *mindist; /* minimum distance to a bone for all vertices */
111 RayObject *raytree; /* ray tracing acceleration structure */
112 RayFace *faces; /* faces to add to the ray tracing struture */
113 MFace **vface; /* a face that the vertex belongs to */
117 struct RigidDeformation {
128 /* Laplacian matrix construction */
130 /* Computation of these weights for the laplacian is based on:
131 "Discrete Differential-Geometry Operators for Triangulated 2-Manifolds",
132 Meyer et al, 2002. Section 3.5, formula (8).
134 We do it a bit different by going over faces instead of going over each
135 vertex and adjacent faces, since we don't store this adjacency. Also, the
136 formulas are tweaked a bit to work for non-manifold meshes. */
138 static void laplacian_increase_edge_count(EdgeHash *edgehash, int v1, int v2)
140 void **p = BLI_edgehash_lookup_p(edgehash, v1, v2);
143 *p = (void*)((intptr_t)*p + (intptr_t)1);
145 BLI_edgehash_insert(edgehash, v1, v2, (void*)(intptr_t)1);
148 static int laplacian_edge_count(EdgeHash *edgehash, int v1, int v2)
150 return (int)(intptr_t)BLI_edgehash_lookup(edgehash, v1, v2);
153 static float cotan_weight(float *v1, float *v2, float *v3)
155 float a[3], b[3], c[3], clen;
157 sub_v3_v3v3(a, v2, v1);
158 sub_v3_v3v3(b, v3, v1);
159 cross_v3_v3v3(c, a, b);
166 return dot_v3v3(a, b)/clen;
169 static void laplacian_triangle_area(LaplacianSystem *sys, int i1, int i2, int i3)
171 float t1, t2, t3, len1, len2, len3, area;
172 float *varea= sys->varea, *v1, *v2, *v3;
179 t1= cotan_weight(v1, v2, v3);
180 t2= cotan_weight(v2, v3, v1);
181 t3= cotan_weight(v3, v1, v2);
183 if(RAD2DEG(angle_v3v3v3(v2, v1, v3)) > 90) obtuse= 1;
184 else if(RAD2DEG(angle_v3v3v3(v1, v2, v3)) > 90) obtuse= 2;
185 else if(RAD2DEG(angle_v3v3v3(v1, v3, v2)) > 90) obtuse= 3;
188 area= area_tri_v3(v1, v2, v3);
190 varea[i1] += (obtuse == 1)? area: area*0.5;
191 varea[i2] += (obtuse == 2)? area: area*0.5;
192 varea[i3] += (obtuse == 3)? area: area*0.5;
195 len1= len_v3v3(v2, v3);
196 len2= len_v3v3(v1, v3);
197 len3= len_v3v3(v1, v2);
203 varea[i1] += (t2 + t3)*0.25f;
204 varea[i2] += (t1 + t3)*0.25f;
205 varea[i3] += (t1 + t2)*0.25f;
209 static void laplacian_triangle_weights(LaplacianSystem *sys, int f, int i1, int i2, int i3)
212 float *varea= sys->varea, *v1, *v2, *v3;
218 /* instead of *0.5 we divided by the number of faces of the edge, it still
219 needs to be verified that this is indeed the correct thing to do! */
220 t1= cotan_weight(v1, v2, v3)/laplacian_edge_count(sys->edgehash, i2, i3);
221 t2= cotan_weight(v2, v3, v1)/laplacian_edge_count(sys->edgehash, i3, i1);
222 t3= cotan_weight(v3, v1, v2)/laplacian_edge_count(sys->edgehash, i1, i2);
224 nlMatrixAdd(i1, i1, (t2+t3)*varea[i1]);
225 nlMatrixAdd(i2, i2, (t1+t3)*varea[i2]);
226 nlMatrixAdd(i3, i3, (t1+t2)*varea[i3]);
228 nlMatrixAdd(i1, i2, -t3*varea[i1]);
229 nlMatrixAdd(i2, i1, -t3*varea[i2]);
231 nlMatrixAdd(i2, i3, -t1*varea[i2]);
232 nlMatrixAdd(i3, i2, -t1*varea[i3]);
234 nlMatrixAdd(i3, i1, -t2*varea[i3]);
235 nlMatrixAdd(i1, i3, -t2*varea[i1]);
237 if(sys->storeweights) {
238 sys->fweights[f][0]= t1*varea[i1];
239 sys->fweights[f][1]= t2*varea[i2];
240 sys->fweights[f][2]= t3*varea[i3];
244 LaplacianSystem *laplacian_system_construct_begin(int totvert, int totface, int lsq)
246 LaplacianSystem *sys;
248 sys= MEM_callocN(sizeof(LaplacianSystem), "LaplacianSystem");
250 sys->verts= MEM_callocN(sizeof(float*)*totvert, "LaplacianSystemVerts");
251 sys->vpinned= MEM_callocN(sizeof(char)*totvert, "LaplacianSystemVpinned");
252 sys->faces= MEM_callocN(sizeof(int)*3*totface, "LaplacianSystemFaces");
258 sys->storeweights= 0;
260 /* create opennl context */
262 nlSolverParameteri(NL_NB_VARIABLES, totvert);
264 nlSolverParameteri(NL_LEAST_SQUARES, NL_TRUE);
266 sys->context= nlGetCurrent();
271 void laplacian_add_vertex(LaplacianSystem *sys, float *co, int pinned)
273 sys->verts[sys->totvert]= co;
274 sys->vpinned[sys->totvert]= pinned;
278 void laplacian_add_triangle(LaplacianSystem *sys, int v1, int v2, int v3)
280 sys->faces[sys->totface][0]= v1;
281 sys->faces[sys->totface][1]= v2;
282 sys->faces[sys->totface][2]= v3;
286 void laplacian_system_construct_end(LaplacianSystem *sys)
289 int a, totvert=sys->totvert, totface=sys->totface;
291 laplacian_begin_solve(sys, 0);
293 sys->varea= MEM_callocN(sizeof(float)*totvert, "LaplacianSystemVarea");
295 sys->edgehash= BLI_edgehash_new();
296 for(a=0, face=sys->faces; a<sys->totface; a++, face++) {
297 laplacian_increase_edge_count(sys->edgehash, (*face)[0], (*face)[1]);
298 laplacian_increase_edge_count(sys->edgehash, (*face)[1], (*face)[2]);
299 laplacian_increase_edge_count(sys->edgehash, (*face)[2], (*face)[0]);
303 for(a=0, face=sys->faces; a<sys->totface; a++, face++)
304 laplacian_triangle_area(sys, (*face)[0], (*face)[1], (*face)[2]);
306 for(a=0; a<totvert; a++) {
307 if(sys->areaweights) {
308 if(sys->varea[a] != 0.0f)
309 sys->varea[a]= 0.5f/sys->varea[a];
314 /* for heat weighting */
316 nlMatrixAdd(a, a, sys->heat.H[a]);
319 if(sys->storeweights)
320 sys->fweights= MEM_callocN(sizeof(float)*3*totface, "LaplacianFWeight");
322 for(a=0, face=sys->faces; a<totface; a++, face++)
323 laplacian_triangle_weights(sys, a, (*face)[0], (*face)[1], (*face)[2]);
325 MEM_freeN(sys->faces);
329 MEM_freeN(sys->varea);
333 BLI_edgehash_free(sys->edgehash, NULL);
337 void laplacian_system_delete(LaplacianSystem *sys)
339 if(sys->verts) MEM_freeN(sys->verts);
340 if(sys->varea) MEM_freeN(sys->varea);
341 if(sys->vpinned) MEM_freeN(sys->vpinned);
342 if(sys->faces) MEM_freeN(sys->faces);
343 if(sys->fweights) MEM_freeN(sys->fweights);
345 nlDeleteContext(sys->context);
349 void laplacian_begin_solve(LaplacianSystem *sys, int index)
357 for(a=0; a<sys->totvert; a++) {
358 if(sys->vpinned[a]) {
359 nlSetVariable(0, a, sys->verts[a][index]);
370 void laplacian_add_right_hand_side(LaplacianSystem *sys, int v, float value)
372 nlRightHandSideAdd(0, v, value);
375 int laplacian_system_solve(LaplacianSystem *sys)
383 return nlSolveAdvanced(NULL, NL_TRUE);
386 float laplacian_system_get_solution(int v)
388 return nlGetVariable(0, v);
391 /************************* Heat Bone Weighting ******************************/
392 /* From "Automatic Rigging and Animation of 3D Characters"
393 Ilya Baran and Jovan Popovic, SIGGRAPH 2007 */
395 #define C_WEIGHT 1.0f
396 #define WEIGHT_LIMIT_START 0.05f
397 #define WEIGHT_LIMIT_END 0.025f
398 #define DISTANCE_EPSILON 1e-4f
400 /* Raytracing for vertex to bone/vertex visibility */
401 static void heat_ray_tree_create(LaplacianSystem *sys)
403 MFace *mface = sys->heat.mface;
404 int totface = sys->heat.totface;
405 int totvert = sys->heat.totvert;
408 sys->heat.raytree = RE_rayobject_vbvh_create(totface);
409 sys->heat.faces = MEM_callocN(sizeof(RayFace)*totface, "Heat RayFaces");
410 sys->heat.vface = MEM_callocN(sizeof(MFace*)*totvert, "HeatVFaces");
412 for(a=0; a<totface; a++) {
415 RayFace *rayface = sys->heat.faces+a;
417 RayObject *obj = RE_rayface_from_coords(
418 rayface, &sys->heat, mf,
419 sys->heat.verts[mf->v1], sys->heat.verts[mf->v2],
420 sys->heat.verts[mf->v3], mf->v4 ? sys->heat.verts[mf->v4] : 0
422 RE_rayobject_add(sys->heat.raytree, obj);
424 //Setup inverse pointers to use on isect.orig
425 sys->heat.vface[mf->v1]= mf;
426 sys->heat.vface[mf->v2]= mf;
427 sys->heat.vface[mf->v3]= mf;
428 if(mf->v4) sys->heat.vface[mf->v4]= mf;
430 RE_rayobject_done(sys->heat.raytree);
433 static int heat_ray_source_visible(LaplacianSystem *sys, int vertex, int source)
440 mface= sys->heat.vface[vertex];
445 memset(&isec, 0, sizeof(isec));
446 isec.mode= RE_RAY_SHADOW;
448 isec.orig.ob = &sys->heat;
449 isec.orig.face = mface;
450 isec.skip = RE_SKIP_CULLFACE;
452 copy_v3_v3(isec.start, sys->heat.verts[vertex]);
454 if(sys->heat.root) /* bone */
455 closest_to_line_segment_v3(end, isec.start,
456 sys->heat.root[source], sys->heat.tip[source]);
458 copy_v3_v3(end, sys->heat.source[source]);
460 sub_v3_v3v3(isec.vec, end, isec.start);
461 isec.labda = 1.0f - 1e-5;
462 madd_v3_v3v3fl(isec.start, isec.start, isec.vec, 1e-5);
464 visible= !RE_rayobject_raycast(sys->heat.raytree, &isec);
469 static float heat_source_distance(LaplacianSystem *sys, int vertex, int source)
471 float closest[3], d[3], dist, cosine;
473 /* compute euclidian distance */
474 if(sys->heat.root) /* bone */
475 closest_to_line_segment_v3(closest, sys->heat.verts[vertex],
476 sys->heat.root[source], sys->heat.tip[source]);
478 copy_v3_v3(closest, sys->heat.source[source]);
480 sub_v3_v3v3(d, sys->heat.verts[vertex], closest);
481 dist= normalize_v3(d);
483 /* if the vertex normal does not point along the bone, increase distance */
484 cosine= INPR(d, sys->heat.vnors[vertex]);
486 return dist/(0.5f*(cosine + 1.001f));
489 static int heat_source_closest(LaplacianSystem *sys, int vertex, int source)
493 dist= heat_source_distance(sys, vertex, source);
495 if(dist <= sys->heat.mindist[vertex]*(1.0f + DISTANCE_EPSILON))
496 if(heat_ray_source_visible(sys, vertex, source))
502 static void heat_set_H(LaplacianSystem *sys, int vertex)
504 float dist, mindist, h;
505 int j, numclosest = 0;
509 /* compute minimum distance */
510 for(j=0; j<sys->heat.numsource; j++) {
511 dist= heat_source_distance(sys, vertex, j);
517 sys->heat.mindist[vertex]= mindist;
519 /* count number of sources with approximately this minimum distance */
520 for(j=0; j<sys->heat.numsource; j++)
521 if(heat_source_closest(sys, vertex, j))
524 sys->heat.p[vertex]= (numclosest > 0)? 1.0f/numclosest: 0.0f;
526 /* compute H entry */
529 h= numclosest*C_WEIGHT/(mindist*mindist);
536 sys->heat.H[vertex]= h;
539 void heat_calc_vnormals(LaplacianSystem *sys)
542 int a, v1, v2, v3, (*face)[3];
544 sys->heat.vnors= MEM_callocN(sizeof(float)*3*sys->totvert, "HeatVNors");
546 for(a=0, face=sys->faces; a<sys->totface; a++, face++) {
551 normal_tri_v3( fnor,sys->verts[v1], sys->verts[v2], sys->verts[v3]);
553 add_v3_v3v3(sys->heat.vnors[v1], sys->heat.vnors[v1], fnor);
554 add_v3_v3v3(sys->heat.vnors[v2], sys->heat.vnors[v2], fnor);
555 add_v3_v3v3(sys->heat.vnors[v3], sys->heat.vnors[v3], fnor);
558 for(a=0; a<sys->totvert; a++)
559 normalize_v3(sys->heat.vnors[a]);
562 static void heat_laplacian_create(LaplacianSystem *sys)
564 MFace *mface = sys->heat.mface, *mf;
565 int totface= sys->heat.totface;
566 int totvert= sys->heat.totvert;
569 /* heat specific definitions */
570 sys->heat.mindist= MEM_callocN(sizeof(float)*totvert, "HeatMinDist");
571 sys->heat.H= MEM_callocN(sizeof(float)*totvert, "HeatH");
572 sys->heat.p= MEM_callocN(sizeof(float)*totvert, "HeatP");
574 /* add verts and faces to laplacian */
575 for(a=0; a<totvert; a++)
576 laplacian_add_vertex(sys, sys->heat.verts[a], 0);
578 for(a=0, mf=mface; a<totface; a++, mf++) {
579 laplacian_add_triangle(sys, mf->v1, mf->v2, mf->v3);
581 laplacian_add_triangle(sys, mf->v1, mf->v3, mf->v4);
584 /* for distance computation in set_H */
585 heat_calc_vnormals(sys);
587 for(a=0; a<totvert; a++)
591 static void heat_system_free(LaplacianSystem *sys)
593 RE_rayobject_free(sys->heat.raytree);
594 MEM_freeN(sys->heat.vface);
595 MEM_freeN(sys->heat.faces);
597 MEM_freeN(sys->heat.mindist);
598 MEM_freeN(sys->heat.H);
599 MEM_freeN(sys->heat.p);
600 MEM_freeN(sys->heat.vnors);
603 static float heat_limit_weight(float weight)
607 if(weight < WEIGHT_LIMIT_END) {
610 else if(weight < WEIGHT_LIMIT_START) {
611 t= (weight - WEIGHT_LIMIT_END)/(WEIGHT_LIMIT_START - WEIGHT_LIMIT_END);
612 return t*WEIGHT_LIMIT_START;
618 void heat_bone_weighting(Object *ob, Mesh *me, float (*verts)[3], int numsource, bDeformGroup **dgrouplist, bDeformGroup **dgroupflip, float (*root)[3], float (*tip)[3], int *selected)
620 LaplacianSystem *sys;
622 float solution, weight;
623 int *vertsflipped = NULL;
624 int a, totface, j, bbone, firstsegment, lastsegment, thrownerror = 0;
626 /* count triangles */
627 for(totface=0, a=0, mface=me->mface; a<me->totface; a++, mface++) {
629 if(mface->v4) totface++;
632 /* create laplacian */
633 sys = laplacian_system_construct_begin(me->totvert, totface, 1);
635 sys->heat.mface= me->mface;
636 sys->heat.totface= me->totface;
637 sys->heat.totvert= me->totvert;
638 sys->heat.verts= verts;
639 sys->heat.root= root;
641 sys->heat.numsource= numsource;
643 heat_ray_tree_create(sys);
644 heat_laplacian_create(sys);
646 laplacian_system_construct_end(sys);
649 vertsflipped = MEM_callocN(sizeof(int)*me->totvert, "vertsflipped");
650 for(a=0; a<me->totvert; a++)
651 vertsflipped[a] = mesh_get_x_mirror_vert(ob, a);
654 /* compute weights per bone */
655 for(j=0; j<numsource; j++) {
659 firstsegment= (j == 0 || dgrouplist[j-1] != dgrouplist[j]);
660 lastsegment= (j == numsource-1 || dgrouplist[j] != dgrouplist[j+1]);
661 bbone= !(firstsegment && lastsegment);
664 if(bbone && firstsegment) {
665 for(a=0; a<me->totvert; a++) {
666 ED_vgroup_vert_remove(ob, dgrouplist[j], a);
667 if(vertsflipped && dgroupflip[j] && vertsflipped[a] >= 0)
668 ED_vgroup_vert_remove(ob, dgroupflip[j], vertsflipped[a]);
672 /* fill right hand side */
673 laplacian_begin_solve(sys, -1);
675 for(a=0; a<me->totvert; a++)
676 if(heat_source_closest(sys, a, j))
677 laplacian_add_right_hand_side(sys, a,
678 sys->heat.H[a]*sys->heat.p[a]);
681 if(laplacian_system_solve(sys)) {
682 /* load solution into vertex groups */
683 for(a=0; a<me->totvert; a++) {
684 solution= laplacian_system_get_solution(a);
688 ED_vgroup_vert_add(ob, dgrouplist[j], a, solution,
692 weight= heat_limit_weight(solution);
694 ED_vgroup_vert_add(ob, dgrouplist[j], a, weight,
697 ED_vgroup_vert_remove(ob, dgrouplist[j], a);
700 /* do same for mirror */
701 if(vertsflipped && dgroupflip[j] && vertsflipped[a] >= 0) {
704 ED_vgroup_vert_add(ob, dgroupflip[j], vertsflipped[a],
705 solution, WEIGHT_ADD);
708 weight= heat_limit_weight(solution);
710 ED_vgroup_vert_add(ob, dgroupflip[j], vertsflipped[a],
711 weight, WEIGHT_REPLACE);
713 ED_vgroup_vert_remove(ob, dgroupflip[j], vertsflipped[a]);
718 else if(!thrownerror) {
719 error("Bone Heat Weighting:"
720 " failed to find solution for one or more bones");
725 /* remove too small vertex weights */
726 if(bbone && lastsegment) {
727 for(a=0; a<me->totvert; a++) {
728 weight= ED_vgroup_vert_weight(ob, dgrouplist[j], a);
729 weight= heat_limit_weight(weight);
731 ED_vgroup_vert_remove(ob, dgrouplist[j], a);
733 if(vertsflipped && dgroupflip[j] && vertsflipped[a] >= 0) {
734 weight= ED_vgroup_vert_weight(ob, dgroupflip[j], vertsflipped[a]);
735 weight= heat_limit_weight(weight);
737 ED_vgroup_vert_remove(ob, dgroupflip[j], vertsflipped[a]);
744 if(vertsflipped) MEM_freeN(vertsflipped);
746 heat_system_free(sys);
748 laplacian_system_delete(sys);
752 /********************** As-Rigid-As-Possible Deformation ******************/
753 /* From "As-Rigid-As-Possible Surface Modeling",
754 Olga Sorkine and Marc Alexa, ESGP 2007. */
757 - transpose R in orthogonal
758 - flipped normals and per face adding
759 - move cancelling to transform, make origco pointer
762 static LaplacianSystem *RigidDeformSystem = NULL;
764 static void rigid_add_half_edge_to_R(LaplacianSystem *sys, EditVert *v1, EditVert *v2, float w)
769 sub_v3_v3v3(e, sys->rigid.origco[v1->tmp.l], sys->rigid.origco[v2->tmp.l]);
770 sub_v3_v3v3(e_, v1->co, v2->co);
773 for (i=0; i<3; i++) {
774 sys->rigid.R[v1->tmp.l][i][0] += w*e[0]*e_[i];
775 sys->rigid.R[v1->tmp.l][i][1] += w*e[1]*e_[i];
776 sys->rigid.R[v1->tmp.l][i][2] += w*e[2]*e_[i];
780 static void rigid_add_edge_to_R(LaplacianSystem *sys, EditVert *v1, EditVert *v2, float w)
782 rigid_add_half_edge_to_R(sys, v1, v2, w);
783 rigid_add_half_edge_to_R(sys, v2, v1, w);
786 static void rigid_orthogonalize_R(float R[][3])
791 polar_decomp(M, Q, S);
795 static void rigid_add_half_edge_to_rhs(LaplacianSystem *sys, EditVert *v1, EditVert *v2, float w)
798 float Rsum[3][3], rhs[3];
800 if (sys->vpinned[v1->tmp.l])
803 add_m3_m3m3(Rsum, sys->rigid.R[v1->tmp.l], sys->rigid.R[v2->tmp.l]);
806 sub_v3_v3v3(rhs, sys->rigid.origco[v1->tmp.l], sys->rigid.origco[v2->tmp.l]);
807 mul_m3_v3(Rsum, rhs);
808 mul_v3_fl(rhs, 0.5f);
811 add_v3_v3v3(sys->rigid.rhs[v1->tmp.l], sys->rigid.rhs[v1->tmp.l], rhs);
814 static void rigid_add_edge_to_rhs(LaplacianSystem *sys, EditVert *v1, EditVert *v2, float w)
816 rigid_add_half_edge_to_rhs(sys, v1, v2, w);
817 rigid_add_half_edge_to_rhs(sys, v2, v1, w);
820 void rigid_deform_iteration()
822 LaplacianSystem *sys= RigidDeformSystem;
831 nlMakeCurrent(sys->context);
835 memset(sys->rigid.R, 0, sizeof(float)*3*3*sys->totvert);
836 memset(sys->rigid.rhs, 0, sizeof(float)*3*sys->totvert);
838 for(a=0, efa=em->faces.first; efa; efa=efa->next, a++) {
839 rigid_add_edge_to_R(sys, efa->v1, efa->v2, sys->fweights[a][2]);
840 rigid_add_edge_to_R(sys, efa->v2, efa->v3, sys->fweights[a][0]);
841 rigid_add_edge_to_R(sys, efa->v3, efa->v1, sys->fweights[a][1]);
845 rigid_add_edge_to_R(sys, efa->v1, efa->v3, sys->fweights[a][2]);
846 rigid_add_edge_to_R(sys, efa->v3, efa->v4, sys->fweights[a][0]);
847 rigid_add_edge_to_R(sys, efa->v4, efa->v1, sys->fweights[a][1]);
851 for(a=0, eve=em->verts.first; eve; eve=eve->next, a++) {
852 rigid_orthogonalize_R(sys->rigid.R[a]);
856 /* compute right hand sides for solving */
857 for(a=0, efa=em->faces.first; efa; efa=efa->next, a++) {
858 rigid_add_edge_to_rhs(sys, efa->v1, efa->v2, sys->fweights[a][2]);
859 rigid_add_edge_to_rhs(sys, efa->v2, efa->v3, sys->fweights[a][0]);
860 rigid_add_edge_to_rhs(sys, efa->v3, efa->v1, sys->fweights[a][1]);
864 rigid_add_edge_to_rhs(sys, efa->v1, efa->v3, sys->fweights[a][2]);
865 rigid_add_edge_to_rhs(sys, efa->v3, efa->v4, sys->fweights[a][0]);
866 rigid_add_edge_to_rhs(sys, efa->v4, efa->v1, sys->fweights[a][1]);
870 /* solve for positions, for X,Y and Z separately */
872 laplacian_begin_solve(sys, i);
874 for(a=0; a<sys->totvert; a++)
876 laplacian_add_right_hand_side(sys, a, sys->rigid.rhs[a][i]);
878 if(laplacian_system_solve(sys)) {
879 for(a=0, eve=em->verts.first; eve; eve=eve->next, a++)
880 eve->co[i]= laplacian_system_get_solution(a);
883 if(!sys->rigid.thrownerror) {
884 error("RigidDeform: failed to find solution.");
885 sys->rigid.thrownerror= 1;
892 static void rigid_laplacian_create(LaplacianSystem *sys)
894 EditMesh *em = sys->rigid.mesh;
899 /* add verts and faces to laplacian */
900 for(a=0, eve=em->verts.first; eve; eve=eve->next, a++) {
901 laplacian_add_vertex(sys, eve->co, eve->pinned);
905 for(efa=em->faces.first; efa; efa=efa->next) {
906 laplacian_add_triangle(sys,
907 efa->v1->tmp.l, efa->v2->tmp.l, efa->v3->tmp.l);
909 laplacian_add_triangle(sys,
910 efa->v1->tmp.l, efa->v3->tmp.l, efa->v4->tmp.l);
914 void rigid_deform_begin(EditMesh *em)
916 LaplacianSystem *sys;
919 int a, totvert, totface;
921 /* count vertices, triangles */
922 for(totvert=0, eve=em->verts.first; eve; eve=eve->next)
925 for(totface=0, efa=em->faces.first; efa; efa=efa->next) {
927 if(efa->v4) totface++;
930 /* create laplacian */
931 sys = laplacian_system_construct_begin(totvert, totface, 0);
934 sys->rigid.R = MEM_callocN(sizeof(float)*3*3*totvert, "RigidDeformR");
935 sys->rigid.rhs = MEM_callocN(sizeof(float)*3*totvert, "RigidDeformRHS");
936 sys->rigid.origco = MEM_callocN(sizeof(float)*3*totvert, "RigidDeformCo");
938 for(a=0, eve=em->verts.first; eve; eve=eve->next, a++)
939 copy_v3_v3(sys->rigid.origco[a], eve->co);
942 sys->storeweights= 1;
944 rigid_laplacian_create(sys);
946 laplacian_system_construct_end(sys);
948 RigidDeformSystem = sys;
951 void rigid_deform_end(int cancel)
953 LaplacianSystem *sys = RigidDeformSystem;
956 EditMesh *em = sys->rigid.mesh;
961 for(a=0, eve=em->verts.first; eve; eve=eve->next, a++)
963 copy_v3_v3(eve->co, sys->rigid.origco[a]);
965 if(sys->rigid.R) MEM_freeN(sys->rigid.R);
966 if(sys->rigid.rhs) MEM_freeN(sys->rigid.rhs);
967 if(sys->rigid.origco) MEM_freeN(sys->rigid.origco);
970 laplacian_system_delete(sys);
973 RigidDeformSystem = NULL;
977 /************************** Harmonic Coordinates ****************************/
978 /* From "Harmonic Coordinates for Character Articulation",
979 Pushkar Joshi, Mark Meyer, Tony DeRose, Brian Green and Tom Sanocki,
982 #define EPSILON 0.0001f
984 #define MESHDEFORM_TAG_UNTYPED 0
985 #define MESHDEFORM_TAG_BOUNDARY 1
986 #define MESHDEFORM_TAG_INTERIOR 2
987 #define MESHDEFORM_TAG_EXTERIOR 3
989 #define MESHDEFORM_LEN_THRESHOLD 1e-6
991 #define MESHDEFORM_MIN_INFLUENCE 0.0005
993 static int MESHDEFORM_OFFSET[7][3] =
994 {{0,0,0}, {1,0,0}, {-1,0,0}, {0,1,0}, {0,-1,0}, {0,0,1}, {0,0,-1}};
996 typedef struct MDefBoundIsect {
998 int nvert, v[4], facing;
1002 typedef struct MDefBindInfluence {
1003 struct MDefBindInfluence *next;
1006 } MDefBindInfluence;
1008 typedef struct MeshDeformBind {
1009 /* grid dimensions */
1010 float min[3], max[3];
1011 float width[3], halfwidth[3];
1015 DerivedMesh *cagedm;
1016 float (*cagecos)[3];
1017 float (*vertexcos)[3];
1018 int totvert, totcagevert;
1022 MDefBoundIsect *(*boundisect)[6];
1025 float *phi, *totalphi;
1030 MDefBindInfluence **dyngrid;
1031 float cagemat[4][4];
1040 /* ray intersection */
1042 /* our own triangle intersection, so we can fully control the epsilons and
1043 * prevent corner case from going wrong*/
1044 static int meshdeform_tri_intersect(float orig[3], float end[3], float vert0[3],
1045 float vert1[3], float vert2[3], float *isectco, float *uvw)
1047 float edge1[3], edge2[3], tvec[3], pvec[3], qvec[3];
1048 float det,inv_det, u, v, dir[3], isectdir[3];
1050 sub_v3_v3v3(dir, end, orig);
1052 /* find vectors for two edges sharing vert0 */
1053 sub_v3_v3v3(edge1, vert1, vert0);
1054 sub_v3_v3v3(edge2, vert2, vert0);
1056 /* begin calculating determinant - also used to calculate U parameter */
1057 cross_v3_v3v3(pvec, dir, edge2);
1059 /* if determinant is near zero, ray lies in plane of triangle */
1060 det = INPR(edge1, pvec);
1064 inv_det = 1.0f / det;
1066 /* calculate distance from vert0 to ray origin */
1067 sub_v3_v3v3(tvec, orig, vert0);
1069 /* calculate U parameter and test bounds */
1070 u = INPR(tvec, pvec) * inv_det;
1071 if (u < -EPSILON || u > 1.0f+EPSILON)
1074 /* prepare to test V parameter */
1075 cross_v3_v3v3(qvec, tvec, edge1);
1077 /* calculate V parameter and test bounds */
1078 v = INPR(dir, qvec) * inv_det;
1079 if (v < -EPSILON || u + v > 1.0f+EPSILON)
1082 isectco[0]= (1.0f - u - v)*vert0[0] + u*vert1[0] + v*vert2[0];
1083 isectco[1]= (1.0f - u - v)*vert0[1] + u*vert1[1] + v*vert2[1];
1084 isectco[2]= (1.0f - u - v)*vert0[2] + u*vert1[2] + v*vert2[2];
1086 uvw[0]= 1.0 - u - v;
1090 /* check if it is within the length of the line segment */
1091 sub_v3_v3v3(isectdir, isectco, orig);
1093 if(INPR(dir, isectdir) < -EPSILON)
1096 if(INPR(dir, dir) + EPSILON < INPR(isectdir, isectdir))
1102 /* blender's raytracer is not use now, even though it is much faster. it can
1103 * give problems with rays falling through, so we use our own intersection
1104 * function above with tweaked epsilons */
1107 static MeshDeformBind *MESHDEFORM_BIND = NULL;
1109 static void meshdeform_ray_coords_func(RayFace *face, float **v1, float **v2, float **v3, float **v4)
1111 MFace *mface= (MFace*)face;
1112 float (*cagecos)[3]= MESHDEFORM_BIND->cagecos;
1114 *v1= cagecos[mface->v1];
1115 *v2= cagecos[mface->v2];
1116 *v3= cagecos[mface->v3];
1117 *v4= (mface->v4)? cagecos[mface->v4]: NULL;
1120 static int meshdeform_ray_check_func(Isect *is, RayFace *face)
1125 static void meshdeform_ray_tree_create(MeshDeformBind *mdb)
1128 float min[3], max[3];
1131 /* create a raytrace tree from the mesh */
1132 INIT_MINMAX(min, max);
1134 for(a=0; a<mdb->totcagevert; a++)
1135 DO_MINMAX(mdb->cagecos[a], min, max)
1137 MESHDEFORM_BIND= mdb;
1139 mface= mdb->cagedm->getFaceArray(mdb->cagedm);
1140 totface= mdb->cagedm->getNumFaces(mdb->cagedm);
1142 mdb->raytree= RE_ray_tree_create(64, totface, min, max,
1143 meshdeform_ray_coords_func, meshdeform_ray_check_func);
1145 for(a=0; a<totface; a++, mface++)
1146 RE_ray_tree_add_face(mdb->raytree, mface);
1148 RE_ray_tree_done(mdb->raytree);
1151 static void meshdeform_ray_tree_free(MeshDeformBind *mdb)
1153 MESHDEFORM_BIND= NULL;
1154 RE_ray_tree_free(mdb->raytree);
1158 static int meshdeform_intersect(MeshDeformBind *mdb, Isect *isec)
1161 float face[4][3], co[3], uvw[3], len, nor[3], end[3];
1162 int f, hit, is= 0, totface;
1166 mface= mdb->cagedm->getFaceArray(mdb->cagedm);
1167 totface= mdb->cagedm->getNumFaces(mdb->cagedm);
1169 VECADDFAC( end, isec->start, isec->vec, isec->labda );
1171 for(f=0; f<totface; f++, mface++) {
1172 copy_v3_v3(face[0], mdb->cagecos[mface->v1]);
1173 copy_v3_v3(face[1], mdb->cagecos[mface->v2]);
1174 copy_v3_v3(face[2], mdb->cagecos[mface->v3]);
1177 copy_v3_v3(face[3], mdb->cagecos[mface->v4]);
1178 hit = meshdeform_tri_intersect(isec->start, end, face[0], face[1], face[2], co, uvw);
1181 normal_tri_v3( nor,face[0], face[1], face[2]);
1184 hit= meshdeform_tri_intersect(isec->start, end, face[0], face[2], face[3], co, uvw);
1185 normal_tri_v3( nor,face[0], face[2], face[3]);
1189 hit= meshdeform_tri_intersect(isec->start, end, face[0], face[1], face[2], co, uvw);
1190 normal_tri_v3( nor,face[0], face[1], face[2]);
1194 len= len_v3v3(isec->start, co)/len_v3v3(isec->start, end);
1195 if(len < isec->labda) {
1197 isec->hit.face = mface;
1198 isec->isect= (INPR(isec->vec, nor) <= 0.0f);
1207 static MDefBoundIsect *meshdeform_ray_tree_intersect(MeshDeformBind *mdb, float *co1, float *co2)
1209 MDefBoundIsect *isect;
1211 float (*cagecos)[3];
1213 float vert[4][3], len, end[3];
1214 static float epsilon[3]= {0, 0, 0}; //1e-4, 1e-4, 1e-4};
1217 memset(&isec, 0, sizeof(isec));
1218 isec.mode= RE_RAY_MIRROR; /* we want the closest intersection */
1222 VECADD(isec.start, co1, epsilon);
1223 VECADD(end, co2, epsilon);
1224 sub_v3_v3v3(isec.vec, end, isec.start);
1227 /*if(RE_ray_tree_intersect(mdb->raytree, &isec)) {*/
1230 if(meshdeform_intersect(mdb, &isec)) {
1232 mface=(MFace*)isec.hit.face;
1234 /* create MDefBoundIsect */
1235 isect= BLI_memarena_alloc(mdb->memarena, sizeof(*isect));
1237 /* compute intersection coordinate */
1238 isect->co[0]= co1[0] + isec.vec[0]*len;
1239 isect->co[1]= co1[1] + isec.vec[1]*len;
1240 isect->co[2]= co1[2] + isec.vec[2]*len;
1242 isect->len= len_v3v3(co1, isect->co);
1243 if(isect->len < MESHDEFORM_LEN_THRESHOLD)
1244 isect->len= MESHDEFORM_LEN_THRESHOLD;
1246 isect->v[0]= mface->v1;
1247 isect->v[1]= mface->v2;
1248 isect->v[2]= mface->v3;
1249 isect->v[3]= mface->v4;
1250 isect->nvert= (mface->v4)? 4: 3;
1252 isect->facing= isec.isect;
1254 /* compute mean value coordinates for interpolation */
1255 cagecos= mdb->cagecos;
1256 copy_v3_v3(vert[0], cagecos[mface->v1]);
1257 copy_v3_v3(vert[1], cagecos[mface->v2]);
1258 copy_v3_v3(vert[2], cagecos[mface->v3]);
1259 if(mface->v4) copy_v3_v3(vert[3], cagecos[mface->v4]);
1260 interp_weights_poly_v3( isect->uvw,vert, isect->nvert, isect->co);
1268 static int meshdeform_inside_cage(MeshDeformBind *mdb, float *co)
1270 MDefBoundIsect *isect;
1271 float outside[3], start[3], dir[3];
1274 for(i=1; i<=6; i++) {
1277 outside[0] = co[0] + (mdb->max[0] - mdb->min[0] + 1.0f)*MESHDEFORM_OFFSET[i][0];
1278 outside[1] = co[1] + (mdb->max[1] - mdb->min[1] + 1.0f)*MESHDEFORM_OFFSET[i][1];
1279 outside[2] = co[2] + (mdb->max[2] - mdb->min[2] + 1.0f)*MESHDEFORM_OFFSET[i][2];
1281 copy_v3_v3(start, co);
1282 sub_v3_v3v3(dir, outside, start);
1285 isect = meshdeform_ray_tree_intersect(mdb, start, outside);
1286 if(isect && !isect->facing)
1295 static int meshdeform_index(MeshDeformBind *mdb, int x, int y, int z, int n)
1297 int size= mdb->size;
1299 x += MESHDEFORM_OFFSET[n][0];
1300 y += MESHDEFORM_OFFSET[n][1];
1301 z += MESHDEFORM_OFFSET[n][2];
1303 if(x < 0 || x >= mdb->size)
1305 if(y < 0 || y >= mdb->size)
1307 if(z < 0 || z >= mdb->size)
1310 return x + y*size + z*size*size;
1313 static void meshdeform_cell_center(MeshDeformBind *mdb, int x, int y, int z, int n, float *center)
1315 x += MESHDEFORM_OFFSET[n][0];
1316 y += MESHDEFORM_OFFSET[n][1];
1317 z += MESHDEFORM_OFFSET[n][2];
1319 center[0]= mdb->min[0] + x*mdb->width[0] + mdb->halfwidth[0];
1320 center[1]= mdb->min[1] + y*mdb->width[1] + mdb->halfwidth[1];
1321 center[2]= mdb->min[2] + z*mdb->width[2] + mdb->halfwidth[2];
1324 static void meshdeform_add_intersections(MeshDeformBind *mdb, int x, int y, int z)
1326 MDefBoundIsect *isect;
1327 float center[3], ncenter[3];
1330 a= meshdeform_index(mdb, x, y, z, 0);
1331 meshdeform_cell_center(mdb, x, y, z, 0, center);
1333 /* check each outgoing edge for intersection */
1334 for(i=1; i<=6; i++) {
1335 if(meshdeform_index(mdb, x, y, z, i) == -1)
1338 meshdeform_cell_center(mdb, x, y, z, i, ncenter);
1340 isect= meshdeform_ray_tree_intersect(mdb, center, ncenter);
1342 mdb->boundisect[a][i-1]= isect;
1343 mdb->tag[a]= MESHDEFORM_TAG_BOUNDARY;
1348 static void meshdeform_bind_floodfill(MeshDeformBind *mdb)
1350 int *stack, *tag= mdb->tag;
1351 int a, b, i, xyz[3], stacksize, size= mdb->size;
1353 stack= MEM_callocN(sizeof(int)*mdb->size3, "MeshDeformBindStack");
1355 /* we know lower left corner is EXTERIOR because of padding */
1356 tag[0]= MESHDEFORM_TAG_EXTERIOR;
1360 /* floodfill exterior tag */
1361 while(stacksize > 0) {
1362 a= stack[--stacksize];
1364 xyz[2]= a/(size*size);
1365 xyz[1]= (a - xyz[2]*size*size)/size;
1366 xyz[0]= a - xyz[1]*size - xyz[2]*size*size;
1368 for(i=1; i<=6; i++) {
1369 b= meshdeform_index(mdb, xyz[0], xyz[1], xyz[2], i);
1372 if(tag[b] == MESHDEFORM_TAG_UNTYPED ||
1373 (tag[b] == MESHDEFORM_TAG_BOUNDARY && !mdb->boundisect[a][i-1])) {
1374 tag[b]= MESHDEFORM_TAG_EXTERIOR;
1375 stack[stacksize++]= b;
1381 /* other cells are interior */
1382 for(a=0; a<size*size*size; a++)
1383 if(tag[a]==MESHDEFORM_TAG_UNTYPED)
1384 tag[a]= MESHDEFORM_TAG_INTERIOR;
1390 for(a=0; a<size*size*size; a++)
1391 if(tag[a]==MESHDEFORM_TAG_BOUNDARY)
1393 else if(tag[a]==MESHDEFORM_TAG_INTERIOR)
1395 else if(tag[a]==MESHDEFORM_TAG_EXTERIOR) {
1398 if(mdb->semibound[a])
1402 printf("interior %d exterior %d boundary %d semi-boundary %d\n", ti, te, tb, ts);
1409 static float meshdeform_boundary_phi(MeshDeformBind *mdb, MDefBoundIsect *isect, int cagevert)
1413 for(a=0; a<isect->nvert; a++)
1414 if(isect->v[a] == cagevert)
1415 return isect->uvw[a];
1420 static float meshdeform_interp_w(MeshDeformBind *mdb, float *gridvec, float *vec, int cagevert)
1422 float dvec[3], ivec[3], wx, wy, wz, result=0.0f;
1423 float weight, totweight= 0.0f;
1426 for(i=0; i<3; i++) {
1427 ivec[i]= (int)gridvec[i];
1428 dvec[i]= gridvec[i] - ivec[i];
1431 for(i=0; i<8; i++) {
1432 if(i & 1) { x= ivec[0]+1; wx= dvec[0]; }
1433 else { x= ivec[0]; wx= 1.0f-dvec[0]; }
1435 if(i & 2) { y= ivec[1]+1; wy= dvec[1]; }
1436 else { y= ivec[1]; wy= 1.0f-dvec[1]; }
1438 if(i & 4) { z= ivec[2]+1; wz= dvec[2]; }
1439 else { z= ivec[2]; wz= 1.0f-dvec[2]; }
1441 CLAMP(x, 0, mdb->size-1);
1442 CLAMP(y, 0, mdb->size-1);
1443 CLAMP(z, 0, mdb->size-1);
1445 a= meshdeform_index(mdb, x, y, z, 0);
1447 result += weight*mdb->phi[a];
1448 totweight += weight;
1451 if(totweight > 0.0f)
1452 result /= totweight;
1457 static void meshdeform_check_semibound(MeshDeformBind *mdb, int x, int y, int z)
1461 a= meshdeform_index(mdb, x, y, z, 0);
1462 if(mdb->tag[a] != MESHDEFORM_TAG_EXTERIOR)
1466 if(mdb->boundisect[a][i-1])
1467 mdb->semibound[a]= 1;
1470 static float meshdeform_boundary_total_weight(MeshDeformBind *mdb, int x, int y, int z)
1472 float weight, totweight= 0.0f;
1475 a= meshdeform_index(mdb, x, y, z, 0);
1477 /* count weight for neighbour cells */
1478 for(i=1; i<=6; i++) {
1479 if(meshdeform_index(mdb, x, y, z, i) == -1)
1482 if(mdb->boundisect[a][i-1])
1483 weight= 1.0f/mdb->boundisect[a][i-1]->len;
1484 else if(!mdb->semibound[a])
1485 weight= 1.0f/mdb->width[0];
1489 totweight += weight;
1495 static void meshdeform_matrix_add_cell(MeshDeformBind *mdb, int x, int y, int z)
1497 MDefBoundIsect *isect;
1498 float weight, totweight;
1501 acenter= meshdeform_index(mdb, x, y, z, 0);
1502 if(mdb->tag[acenter] == MESHDEFORM_TAG_EXTERIOR)
1505 nlMatrixAdd(mdb->varidx[acenter], mdb->varidx[acenter], 1.0f);
1507 totweight= meshdeform_boundary_total_weight(mdb, x, y, z);
1508 for(i=1; i<=6; i++) {
1509 a= meshdeform_index(mdb, x, y, z, i);
1510 if(a == -1 || mdb->tag[a] == MESHDEFORM_TAG_EXTERIOR)
1513 isect= mdb->boundisect[acenter][i-1];
1515 weight= (1.0f/mdb->width[0])/totweight;
1516 nlMatrixAdd(mdb->varidx[acenter], mdb->varidx[a], -weight);
1521 static void meshdeform_matrix_add_rhs(MeshDeformBind *mdb, int x, int y, int z, int cagevert)
1523 MDefBoundIsect *isect;
1524 float rhs, weight, totweight;
1527 acenter= meshdeform_index(mdb, x, y, z, 0);
1528 if(mdb->tag[acenter] == MESHDEFORM_TAG_EXTERIOR)
1531 totweight= meshdeform_boundary_total_weight(mdb, x, y, z);
1532 for(i=1; i<=6; i++) {
1533 a= meshdeform_index(mdb, x, y, z, i);
1537 isect= mdb->boundisect[acenter][i-1];
1540 weight= (1.0f/isect->len)/totweight;
1541 rhs= weight*meshdeform_boundary_phi(mdb, isect, cagevert);
1542 nlRightHandSideAdd(0, mdb->varidx[acenter], rhs);
1547 static void meshdeform_matrix_add_semibound_phi(MeshDeformBind *mdb, int x, int y, int z, int cagevert)
1549 MDefBoundIsect *isect;
1550 float rhs, weight, totweight;
1553 a= meshdeform_index(mdb, x, y, z, 0);
1554 if(!mdb->semibound[a])
1559 totweight= meshdeform_boundary_total_weight(mdb, x, y, z);
1560 for(i=1; i<=6; i++) {
1561 isect= mdb->boundisect[a][i-1];
1564 weight= (1.0f/isect->len)/totweight;
1565 rhs= weight*meshdeform_boundary_phi(mdb, isect, cagevert);
1571 static void meshdeform_matrix_add_exterior_phi(MeshDeformBind *mdb, int x, int y, int z, int cagevert)
1573 float phi, totweight;
1576 acenter= meshdeform_index(mdb, x, y, z, 0);
1577 if(mdb->tag[acenter] != MESHDEFORM_TAG_EXTERIOR || mdb->semibound[acenter])
1582 for(i=1; i<=6; i++) {
1583 a= meshdeform_index(mdb, x, y, z, i);
1585 if(a != -1 && mdb->semibound[a]) {
1591 if(totweight != 0.0f)
1592 mdb->phi[acenter]= phi/totweight;
1595 static void meshdeform_matrix_solve(MeshDeformBind *mdb)
1598 float vec[3], gridvec[3];
1599 int a, b, x, y, z, totvar;
1602 /* setup variable indices */
1603 mdb->varidx= MEM_callocN(sizeof(int)*mdb->size3, "MeshDeformDSvaridx");
1604 for(a=0, totvar=0; a<mdb->size3; a++)
1605 mdb->varidx[a]= (mdb->tag[a] == MESHDEFORM_TAG_EXTERIOR)? -1: totvar++;
1608 MEM_freeN(mdb->varidx);
1612 progress_bar(0, "Starting mesh deform solve");
1614 /* setup opennl solver */
1616 context= nlGetCurrent();
1618 nlSolverParameteri(NL_NB_VARIABLES, totvar);
1619 nlSolverParameteri(NL_NB_ROWS, totvar);
1620 nlSolverParameteri(NL_NB_RIGHT_HAND_SIDES, 1);
1626 for(z=0; z<mdb->size; z++)
1627 for(y=0; y<mdb->size; y++)
1628 for(x=0; x<mdb->size; x++)
1629 meshdeform_matrix_add_cell(mdb, x, y, z);
1631 /* solve for each cage vert */
1632 for(a=0; a<mdb->totcagevert; a++) {
1638 /* fill in right hand side and solve */
1639 for(z=0; z<mdb->size; z++)
1640 for(y=0; y<mdb->size; y++)
1641 for(x=0; x<mdb->size; x++)
1642 meshdeform_matrix_add_rhs(mdb, x, y, z, a);
1651 if(nlSolveAdvanced(NULL, NL_TRUE)) {
1652 for(z=0; z<mdb->size; z++)
1653 for(y=0; y<mdb->size; y++)
1654 for(x=0; x<mdb->size; x++)
1655 meshdeform_matrix_add_semibound_phi(mdb, x, y, z, a);
1657 for(z=0; z<mdb->size; z++)
1658 for(y=0; y<mdb->size; y++)
1659 for(x=0; x<mdb->size; x++)
1660 meshdeform_matrix_add_exterior_phi(mdb, x, y, z, a);
1662 for(b=0; b<mdb->size3; b++) {
1663 if(mdb->tag[b] != MESHDEFORM_TAG_EXTERIOR)
1664 mdb->phi[b]= nlGetVariable(0, mdb->varidx[b]);
1665 mdb->totalphi[b] += mdb->phi[b];
1669 /* static bind : compute weights for each vertex */
1670 for(b=0; b<mdb->totvert; b++) {
1671 if(mdb->inside[b]) {
1672 copy_v3_v3(vec, mdb->vertexcos[b]);
1673 mul_m4_v3(mdb->cagemat, vec);
1674 gridvec[0]= (vec[0] - mdb->min[0] - mdb->halfwidth[0])/mdb->width[0];
1675 gridvec[1]= (vec[1] - mdb->min[1] - mdb->halfwidth[1])/mdb->width[1];
1676 gridvec[2]= (vec[2] - mdb->min[2] - mdb->halfwidth[2])/mdb->width[2];
1678 mdb->weights[b*mdb->totcagevert + a]= meshdeform_interp_w(mdb, gridvec, vec, a);
1683 MDefBindInfluence *inf;
1686 for(b=0; b<mdb->size3; b++) {
1687 if(mdb->phi[b] >= MESHDEFORM_MIN_INFLUENCE) {
1688 inf= BLI_memarena_alloc(mdb->memarena, sizeof(*inf));
1690 inf->weight= mdb->phi[b];
1691 inf->next= mdb->dyngrid[b];
1692 mdb->dyngrid[b]= inf;
1698 error("Mesh Deform: failed to find solution.");
1702 sprintf(message, "Mesh deform solve %d / %d |||", a+1, mdb->totcagevert);
1703 progress_bar((float)(a+1)/(float)(mdb->totcagevert), message);
1708 for(b=0; b<mdb->size3; b++)
1709 if(mdb->tag[b] != MESHDEFORM_TAG_EXTERIOR)
1710 if(fabs(mdb->totalphi[b] - 1.0f) > 1e-4)
1711 printf("totalphi deficiency [%s|%d] %d: %.10f\n",
1712 (mdb->tag[b] == MESHDEFORM_TAG_INTERIOR)? "interior": "boundary", mdb->semibound[b], mdb->varidx[b], mdb->totalphi[b]);
1716 MEM_freeN(mdb->varidx);
1718 nlDeleteContext(context);
1721 static void harmonic_coordinates_bind(Scene *scene, MeshDeformModifierData *mmd, MeshDeformBind *mdb)
1723 MDefBindInfluence *inf;
1724 MDefInfluence *mdinf;
1726 float center[3], vec[3], maxwidth, totweight;
1727 int a, b, x, y, z, totinside, offset;
1729 /* compute bounding box of the cage mesh */
1730 INIT_MINMAX(mdb->min, mdb->max);
1732 for(a=0; a<mdb->totcagevert; a++)
1733 DO_MINMAX(mdb->cagecos[a], mdb->min, mdb->max);
1735 /* allocate memory */
1736 mdb->size= (2<<(mmd->gridsize-1)) + 2;
1737 mdb->size3= mdb->size*mdb->size*mdb->size;
1738 mdb->tag= MEM_callocN(sizeof(int)*mdb->size3, "MeshDeformBindTag");
1739 mdb->phi= MEM_callocN(sizeof(float)*mdb->size3, "MeshDeformBindPhi");
1740 mdb->totalphi= MEM_callocN(sizeof(float)*mdb->size3, "MeshDeformBindTotalPhi");
1741 mdb->boundisect= MEM_callocN(sizeof(*mdb->boundisect)*mdb->size3, "MDefBoundIsect");
1742 mdb->semibound= MEM_callocN(sizeof(int)*mdb->size3, "MDefSemiBound");
1744 mdb->inside= MEM_callocN(sizeof(int)*mdb->totvert, "MDefInside");
1746 if(mmd->flag & MOD_MDEF_DYNAMIC_BIND)
1747 mdb->dyngrid= MEM_callocN(sizeof(MDefBindInfluence*)*mdb->size3, "MDefDynGrid");
1749 mdb->weights= MEM_callocN(sizeof(float)*mdb->totvert*mdb->totcagevert, "MDefWeights");
1751 mdb->memarena= BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE);
1752 BLI_memarena_use_calloc(mdb->memarena);
1754 /* make bounding box equal size in all directions, add padding, and compute
1755 * width of the cells */
1758 if(mdb->max[a]-mdb->min[a] > maxwidth)
1759 maxwidth= mdb->max[a]-mdb->min[a];
1761 for(a=0; a<3; a++) {
1762 center[a]= (mdb->min[a]+mdb->max[a])*0.5f;
1763 mdb->min[a]= center[a] - maxwidth*0.5f;
1764 mdb->max[a]= center[a] + maxwidth*0.5f;
1766 mdb->width[a]= (mdb->max[a]-mdb->min[a])/(mdb->size-4);
1767 mdb->min[a] -= 2.1f*mdb->width[a];
1768 mdb->max[a] += 2.1f*mdb->width[a];
1770 mdb->width[a]= (mdb->max[a]-mdb->min[a])/mdb->size;
1771 mdb->halfwidth[a]= mdb->width[a]*0.5f;
1774 progress_bar(0, "Setting up mesh deform system");
1777 /* create ray tree */
1778 meshdeform_ray_tree_create(mdb);
1782 for(a=0; a<mdb->totvert; a++) {
1783 copy_v3_v3(vec, mdb->vertexcos[a]);
1784 mul_m4_v3(mdb->cagemat, vec);
1785 mdb->inside[a]= meshdeform_inside_cage(mdb, vec);
1790 /* free temporary MDefBoundIsects */
1791 BLI_memarena_free(mdb->memarena);
1792 mdb->memarena= BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE);
1794 /* start with all cells untyped */
1795 for(a=0; a<mdb->size3; a++)
1796 mdb->tag[a]= MESHDEFORM_TAG_UNTYPED;
1798 /* detect intersections and tag boundary cells */
1799 for(z=0; z<mdb->size; z++)
1800 for(y=0; y<mdb->size; y++)
1801 for(x=0; x<mdb->size; x++)
1802 meshdeform_add_intersections(mdb, x, y, z);
1806 meshdeform_ray_tree_free(mdb);
1809 /* compute exterior and interior tags */
1810 meshdeform_bind_floodfill(mdb);
1812 for(z=0; z<mdb->size; z++)
1813 for(y=0; y<mdb->size; y++)
1814 for(x=0; x<mdb->size; x++)
1815 meshdeform_check_semibound(mdb, x, y, z);
1818 meshdeform_matrix_solve(mdb);
1820 /* assign results */
1821 if(mmd->flag & MOD_MDEF_DYNAMIC_BIND) {
1822 mmd->totinfluence= 0;
1823 for(a=0; a<mdb->size3; a++)
1824 for(inf=mdb->dyngrid[a]; inf; inf=inf->next)
1825 mmd->totinfluence++;
1827 /* convert MDefBindInfluences to smaller MDefInfluences */
1828 mmd->dyngrid= MEM_callocN(sizeof(MDefCell)*mdb->size3, "MDefDynGrid");
1829 mmd->dyninfluences= MEM_callocN(sizeof(MDefInfluence)*mmd->totinfluence, "MDefInfluence");
1831 for(a=0; a<mdb->size3; a++) {
1832 cell= &mmd->dyngrid[a];
1833 cell->offset= offset;
1836 mdinf= mmd->dyninfluences + cell->offset;
1837 for(inf=mdb->dyngrid[a]; inf; inf=inf->next, mdinf++) {
1838 mdinf->weight= inf->weight;
1839 mdinf->vertex= inf->vertex;
1840 totweight += mdinf->weight;
1841 cell->totinfluence++;
1844 if(totweight > 0.0f) {
1845 mdinf= mmd->dyninfluences + cell->offset;
1846 for(b=0; b<cell->totinfluence; b++, mdinf++)
1847 mdinf->weight /= totweight;
1850 offset += cell->totinfluence;
1853 mmd->dynverts= mdb->inside;
1854 mmd->dyngridsize= mdb->size;
1855 copy_v3_v3(mmd->dyncellmin, mdb->min);
1856 mmd->dyncellwidth= mdb->width[0];
1857 MEM_freeN(mdb->dyngrid);
1860 mmd->bindweights= mdb->weights;
1861 MEM_freeN(mdb->inside);
1864 MEM_freeN(mdb->tag);
1865 MEM_freeN(mdb->phi);
1866 MEM_freeN(mdb->totalphi);
1867 MEM_freeN(mdb->boundisect);
1868 MEM_freeN(mdb->semibound);
1869 BLI_memarena_free(mdb->memarena);
1872 static void heat_weighting_bind(Scene *scene, DerivedMesh *dm, MeshDeformModifierData *mmd, MeshDeformBind *mdb)
1874 LaplacianSystem *sys;
1875 MFace *mface= dm->getFaceArray(dm), *mf;
1876 int totvert= dm->getNumVerts(dm);
1877 int totface= dm->getNumFaces(dm);
1878 float solution, weight;
1879 int a, tottri, j, thrownerror = 0;
1881 mdb->weights= MEM_callocN(sizeof(float)*mdb->totvert*mdb->totcagevert, "MDefWeights");
1883 /* count triangles */
1884 for(tottri=0, a=0, mf=mface; a<totface; a++, mf++) {
1886 if(mf->v4) tottri++;
1889 /* create laplacian */
1890 sys = laplacian_system_construct_begin(totvert, tottri, 1);
1892 sys->heat.mface= mface;
1893 sys->heat.totface= totface;
1894 sys->heat.totvert= totvert;
1895 sys->heat.verts= mdb->vertexcos;
1896 sys->heat.source = mdb->cagecos;
1897 sys->heat.numsource= mdb->totcagevert;
1899 heat_ray_tree_create(sys);
1900 heat_laplacian_create(sys);
1902 laplacian_system_construct_end(sys);
1904 /* compute weights per bone */
1905 for(j=0; j<mdb->totcagevert; j++) {
1906 /* fill right hand side */
1907 laplacian_begin_solve(sys, -1);
1909 for(a=0; a<totvert; a++)
1910 if(heat_source_closest(sys, a, j))
1911 laplacian_add_right_hand_side(sys, a,
1912 sys->heat.H[a]*sys->heat.p[a]);
1915 if(laplacian_system_solve(sys)) {
1916 /* load solution into vertex groups */
1917 for(a=0; a<totvert; a++) {
1918 solution= laplacian_system_get_solution(a);
1920 weight= heat_limit_weight(solution);
1922 mdb->weights[a*mdb->totcagevert + j] = weight;
1925 else if(!thrownerror) {
1926 error("Mesh Deform Heat Weighting:"
1927 " failed to find solution for one or more vertices");
1934 heat_system_free(sys);
1935 laplacian_system_delete(sys);
1937 mmd->bindweights= mdb->weights;
1940 void mesh_deform_bind(Scene *scene, DerivedMesh *dm, MeshDeformModifierData *mmd, float *vertexcos, int totvert, float cagemat[][4])
1947 start_progress_bar();
1949 memset(&mdb, 0, sizeof(MeshDeformBind));
1951 /* get mesh and cage mesh */
1952 mdb.vertexcos= MEM_callocN(sizeof(float)*3*totvert, "MeshDeformCos");
1953 mdb.totvert= totvert;
1955 mdb.cagedm= mesh_create_derived_no_deform(scene, mmd->object, NULL, CD_MASK_BAREMESH);
1956 mdb.totcagevert= mdb.cagedm->getNumVerts(mdb.cagedm);
1957 mdb.cagecos= MEM_callocN(sizeof(*mdb.cagecos)*mdb.totcagevert, "MeshDeformBindCos");
1958 copy_m4_m4(mdb.cagemat, cagemat);
1960 mvert= mdb.cagedm->getVertArray(mdb.cagedm);
1961 for(a=0; a<mdb.totcagevert; a++)
1962 copy_v3_v3(mdb.cagecos[a], mvert[a].co);
1963 for(a=0; a<mdb.totvert; a++)
1964 mul_v3_m4v3(mdb.vertexcos[a], mdb.cagemat, vertexcos + a*3);
1967 if(mmd->mode == MOD_MDEF_VOLUME)
1968 harmonic_coordinates_bind(scene, mmd, &mdb);
1970 heat_weighting_bind(scene, dm, mmd, &mdb);
1972 /* assign bind variables */
1973 mmd->bindcos= (float*)mdb.cagecos;
1974 mmd->totvert= mdb.totvert;
1975 mmd->totcagevert= mdb.totcagevert;
1976 copy_m4_m4(mmd->bindmat, mmd->object->obmat);
1978 /* transform bindcos to world space */
1979 for(a=0; a<mdb.totcagevert; a++)
1980 mul_m4_v3(mmd->object->obmat, mmd->bindcos+a*3);
1983 mdb.cagedm->release(mdb.cagedm);
1984 MEM_freeN(mdb.vertexcos);