* corrective UV transformation - only works with UV edge slide right now. but should...
[blender.git] / source / blender / src / editmesh_tools.c
index 2b25253dc95d18fc3e27c347bc736e30433a5f86..c2fccb0e50f0d9b110a31a72b3d7d800a8f88bfa 100644 (file)
@@ -4648,6 +4648,12 @@ void bevel_menu_old()
 }
 
 /* *********** END BEVEL *********/
+typedef struct SlideUv {
+       float origuv[2];
+       float *uv_up, *uv_down;
+       //float *fuv[4];
+       LinkNode *fuv_list;
+} SlideUv;
 
 typedef struct SlideVert {
        EditEdge *up,*down;
@@ -4655,9 +4661,19 @@ typedef struct SlideVert {
 } SlideVert;
 
 int EdgeLoopDelete(void) {
+       
+       /* temporal flag setting so we keep UVs when deleting edge loops,
+        * this is a bit of a hack but it works how you would want in almost all cases */
+       short uvcalc_flag_orig = G.scene->toolsettings->uvcalc_flag; 
+       G.scene->toolsettings->uvcalc_flag |= UVCALC_TRANSFORM_CORRECT;
+       
        if(!EdgeSlide(1, 1)) {
                return 0;
        }
+       
+       /* restore uvcalc flag */
+       G.scene->toolsettings->uvcalc_flag = uvcalc_flag_orig;
+       
        EM_select_more();
        removedoublesflag(1,0, 0.001);
        EM_select_flush();
@@ -4674,11 +4690,21 @@ int EdgeSlide(short immediate, float imperc)
        EditVert *ev, *nearest;
        LinkNode *edgelist = NULL, *vertlist=NULL, *look;
        GHash *vertgh;
+
        SlideVert *tempsv;
        float perc = 0, percp = 0,vertdist, projectMat[4][4], viewMat[4][4];
        float shiftlabda= 0.0f,len = 0.0f;
        int i = 0,j, numsel, numadded=0, timesthrough = 0, vertsel=0, prop=1, cancel = 0,flip=0;
        int wasshift = 0;
+       
+       /* UV correction vars */
+       GHash **uvarray;
+       int  uvlay_tot= CustomData_number_of_layers(&G.editMesh->fdata, CD_MTFACE);
+       int uvlay_idx;
+       SlideUv *slideuvs, *suv, *suv_last;     
+       float uv_tmp[2];
+       LinkNode *fuv_link;
+       
        short event, draw=1;
        short mval[2], mvalo[2];
        char str[128]; 
@@ -4985,6 +5011,99 @@ int EdgeSlide(short immediate, float imperc)
                
                look = look->next;   
        }          
+       
+       
+       if (uvlay_tot && (G.scene->toolsettings->uvcalc_flag & UVCALC_TRANSFORM_CORRECT)) {
+               int maxnum = 0;
+               uvarray = MEM_callocN( uvlay_tot * sizeof(GHash *), "SlideUVs Array");
+               suv_last = slideuvs = MEM_callocN( uvlay_tot * (numadded+1) * sizeof(SlideUv), "SlideUVs"); /* uvLayers * verts */
+               suv = NULL;
+               
+               for (uvlay_idx=0; uvlay_idx<uvlay_tot; uvlay_idx++) {
+                       
+                       uvarray[uvlay_idx] = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp); 
+                       
+                       for(ev=em->verts.first;ev;ev=ev->next) {
+                               ev->tmp.l = 0;
+                       }
+                       look = vertlist;
+                       while(look) {
+                               float *uv_new;
+                               tempsv  = BLI_ghash_lookup(vertgh,(EditVert*)look->link);
+                               
+                               ev = look->link;
+                               suv = NULL;
+                               for(efa = em->faces.first;efa;efa=efa->next) {
+                                       if (ev->tmp.l != -1) { /* test for self, in this case its invalid */
+                                               int k=-1; /* face corner */
+                                       
+                                               /* Is this vert in the faces corner? */
+                                               if              (efa->v1==ev)                           k=0;
+                                               else if (efa->v2==ev)                           k=1;
+                                               else if (efa->v3==ev)                           k=2;
+                                               else if (efa->v4 && efa->v4==ev)        k=3;
+                                               
+                                               if (k != -1) {
+                                                       MTFace *tf = CustomData_em_get_n(&em->fdata, efa->data, CD_MTFACE, uvlay_idx);
+                                                       EditVert *ev_up, *ev_down;
+                                                       
+                                                       uv_new = tf->uv[k];
+                               
+                                                       if (ev->tmp.l) {
+                                                               if (fabs(suv->origuv[0]-uv_new[0]) > 0.0001 || fabs(suv->origuv[1]-uv_new[1])) {
+                                                                       ev->tmp.l = -1; /* Tag as invalid */
+                                                                       BLI_linklist_free(suv->fuv_list,NULL);
+                                                                       suv->fuv_list = NULL;
+                                                                       BLI_ghash_remove(uvarray[uvlay_idx],ev, NULL, NULL);
+                                                                       suv = NULL;
+                                                                       break;
+                                                               }
+                                                       } else {
+                                                               ev->tmp.l = 1;
+                                                               suv = suv_last;
+
+                                                               suv->fuv_list = NULL;
+                                                               suv->uv_up = suv->uv_down = NULL;
+                                                               suv->origuv[0] = uv_new[0];
+                                                               suv->origuv[1] = uv_new[1];
+                                                               
+                                                               BLI_linklist_prepend(&suv->fuv_list, uv_new);
+                                                               BLI_ghash_insert(uvarray[uvlay_idx],ev,suv);
+                                                               
+                                                               suv_last++; /* advance to next slide UV */
+                                                               maxnum++;
+                                                       }
+                               
+                                                       /* Now get the uvs along the up or down edge if we can */
+                                                       if (suv) {
+                                                               if (!suv->uv_up) {
+                                                                       ev_up = editedge_getOtherVert(tempsv->up,ev);
+                                                                       if              (efa->v1==ev_up)                                suv->uv_up = tf->uv[0];
+                                                                       else if (efa->v2==ev_up)                                suv->uv_up = tf->uv[1];
+                                                                       else if (efa->v3==ev_up)                                suv->uv_up = tf->uv[2];
+                                                                       else if (efa->v4 && efa->v4==ev_up)             suv->uv_up = tf->uv[3];
+                                                               }
+                                                               if (!suv->uv_down) { /* if the first face was apart of the up edge, it cant be apart of the down edge */
+                                                                       ev_down = editedge_getOtherVert(tempsv->down,ev);
+                                                                       if              (efa->v1==ev_down)                              suv->uv_down = tf->uv[0];
+                                                                       else if (efa->v2==ev_down)                              suv->uv_down = tf->uv[1];
+                                                                       else if (efa->v3==ev_down)                              suv->uv_down = tf->uv[2];
+                                                                       else if (efa->v4 && efa->v4==ev_down)   suv->uv_down = tf->uv[3];
+                                                               }
+                                       
+                                                               /* Copy the pointers to the face UV's */
+                                                               BLI_linklist_prepend(&suv->fuv_list, uv_new);
+                                                       }
+                                               }
+                                       }
+                               }
+                               look = look->next;
+                       }
+               } /* end uv layer loop */
+       } /* end uvlay_tot */
+       
+       
+       
        // we should have enough info now to slide
 
        len = 0.0f; 
@@ -5089,7 +5208,21 @@ int EdgeSlide(short immediate, float imperc)
                                        
                                        tempev = editedge_getOtherVert((perc>=0)?tempsv->up:tempsv->down, ev);
                                        VecLerpf(ev->co, tempsv->origvert.co, tempev->co, fabs(perc));
-                                                                       
+                                       
+                                       if (G.scene->toolsettings->uvcalc_flag & UVCALC_TRANSFORM_CORRECT) {
+                                               for (uvlay_idx=0; uvlay_idx<uvlay_tot; uvlay_idx++) {
+                                                       suv = BLI_ghash_lookup( uvarray[uvlay_idx], ev );
+                                                       if (suv && suv->fuv_list && suv->uv_up && suv->uv_down) {
+                                                               Vec2Lerpf(uv_tmp, suv->origuv,  (perc>=0)?suv->uv_up:suv->uv_down, fabs(perc));
+                                                               fuv_link = suv->fuv_list;
+                                                               while (fuv_link) {
+                                                                       VECCOPY2D(((float *)fuv_link->link), uv_tmp);
+                                                                       fuv_link = fuv_link->next;
+                                                               }
+                                                       }
+                                               }
+                                       }
+                                       
                                        look = look->next;       
                                }
                        }
@@ -5105,8 +5238,37 @@ int EdgeSlide(short immediate, float imperc)
                                        if(newlen < 0.0) {newlen = 0.0;}
                                        if(flip == 0) {
                                                VecLerpf(ev->co, editedge_getOtherVert(tempsv->down,ev)->co, editedge_getOtherVert(tempsv->up,ev)->co, fabs(newlen));                                                                   
+                                               if (G.scene->toolsettings->uvcalc_flag & UVCALC_TRANSFORM_CORRECT) {
+                                                       /* dont do anything if no UVs */
+                                                       for (uvlay_idx=0; uvlay_idx<uvlay_tot; uvlay_idx++) {
+                                                               suv = BLI_ghash_lookup( uvarray[uvlay_idx], ev );
+                                                               if (suv && suv->fuv_list && suv->uv_up && suv->uv_down) {
+                                                                       Vec2Lerpf(uv_tmp, suv->uv_down, suv->uv_up, fabs(newlen));
+                                                                       fuv_link = suv->fuv_list;
+                                                                       while (fuv_link) {
+                                                                               VECCOPY2D(((float *)fuv_link->link), uv_tmp);
+                                                                               fuv_link = fuv_link->next;
+                                                                       }
+                                                               }
+                                                       }
+                                               }
                                        } else{
                                                VecLerpf(ev->co, editedge_getOtherVert(tempsv->up,ev)->co, editedge_getOtherVert(tempsv->down,ev)->co, fabs(newlen));                           
+                                               
+                                               if (G.scene->toolsettings->uvcalc_flag & UVCALC_TRANSFORM_CORRECT) {
+                                                       /* dont do anything if no UVs */
+                                                       for (uvlay_idx=0; uvlay_idx<uvlay_tot; uvlay_idx++) {
+                                                               suv = BLI_ghash_lookup( uvarray[uvlay_idx], ev );
+                                                               if (suv && suv->fuv_list && suv->uv_up && suv->uv_down) {
+                                                                       Vec2Lerpf(uv_tmp, suv->uv_up, suv->uv_down, fabs(newlen));
+                                                                       fuv_link = suv->fuv_list;
+                                                                       while (fuv_link) {
+                                                                               VECCOPY2D(((float *)fuv_link->link), uv_tmp);
+                                                                               fuv_link = fuv_link->next;
+                                                                       }
+                                                               }
+                                                       }
+                                               }
                                        }
                                        look = look->next;       
                                }
@@ -5276,6 +5438,24 @@ int EdgeSlide(short immediate, float imperc)
        BLI_ghash_free(vertgh, NULL, (GHashValFreeFP)MEM_freeN);
        BLI_linklist_free(vertlist,NULL); 
        BLI_linklist_free(edgelist,NULL); 
+       
+       if (uvlay_tot && (G.scene->toolsettings->uvcalc_flag & UVCALC_TRANSFORM_CORRECT)) {
+               for (uvlay_idx=0; uvlay_idx<uvlay_tot; uvlay_idx++) {
+                       BLI_ghash_free(uvarray[uvlay_idx], NULL, NULL);
+               }
+               MEM_freeN(uvarray);
+               MEM_freeN(slideuvs);
+               
+               suv = suv_last-1;
+               while (suv >= slideuvs) {
+                       if (suv->fuv_list) {
+                               BLI_linklist_free(suv->fuv_list,NULL);
+                       }
+                       suv--;
+               }
+               
+               allqueue(REDRAWIMAGE, 0);
+       }
 
        if(cancel == 1) {
                return -1;