Revision: 38216
          
http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=38216
Author:   shuvro
Date:     2011-07-08 06:16:17 +0000 (Fri, 08 Jul 2011)
Log Message:
-----------
Initial implementation of the stretch calculation algorithm for recursive 
autoseam algorithm. This is not yet set as a stopping criteria for the 
recursive algorithm.

Modified Paths:
--------------
    branches/soc-2011-avocado/blender/source/blender/editors/include/ED_uvedit.h
    branches/soc-2011-avocado/blender/source/blender/editors/mesh/CMakeLists.txt
    
branches/soc-2011-avocado/blender/source/blender/editors/mesh/autoseam_tools.c
    
branches/soc-2011-avocado/blender/source/blender/editors/mesh/autoseam_tools.h

Modified: 
branches/soc-2011-avocado/blender/source/blender/editors/include/ED_uvedit.h
===================================================================
--- 
branches/soc-2011-avocado/blender/source/blender/editors/include/ED_uvedit.h    
    2011-07-08 04:43:53 UTC (rev 38215)
+++ 
branches/soc-2011-avocado/blender/source/blender/editors/include/ED_uvedit.h    
    2011-07-08 06:16:17 UTC (rev 38216)
@@ -89,6 +89,8 @@
 void minimize_stretch_iteration(bContext *C, wmOperator *op, int interactive);
 void minimize_stretch_exit(bContext *C, wmOperator *op, int cancel);
 
+
+
 /* uvedit_draw.c */
 void draw_uvedit_main(struct SpaceImage *sima, struct ARegion *ar, struct 
Scene *scene, struct Object *obedit);
 

Modified: 
branches/soc-2011-avocado/blender/source/blender/editors/mesh/CMakeLists.txt
===================================================================
--- 
branches/soc-2011-avocado/blender/source/blender/editors/mesh/CMakeLists.txt    
    2011-07-08 04:43:53 UTC (rev 38215)
+++ 
branches/soc-2011-avocado/blender/source/blender/editors/mesh/CMakeLists.txt    
    2011-07-08 06:16:17 UTC (rev 38216)
@@ -26,6 +26,7 @@
        ../../blenloader
        ../../blenlib
        ../../bmesh
+       ../../bmesh/intern/
        ../../imbuf
        ../../makesdna
        ../../makesrna

Modified: 
branches/soc-2011-avocado/blender/source/blender/editors/mesh/autoseam_tools.c
===================================================================
--- 
branches/soc-2011-avocado/blender/source/blender/editors/mesh/autoseam_tools.c  
    2011-07-08 04:43:53 UTC (rev 38215)
+++ 
branches/soc-2011-avocado/blender/source/blender/editors/mesh/autoseam_tools.c  
    2011-07-08 06:16:17 UTC (rev 38216)
@@ -27,9 +27,20 @@
  */
 
 #include "autoseam_tools.h"
+#include "BLI_array.h"
+#include "BLI_math.h"
+#include "bmesh_private.h"
 
 
+#define EDGE_NEW       1
+#define FACE_NEW       1
 
+#define ELE_NEW                1
+#define FACE_MARK      2
+#define EDGE_MARK      4
+
+
+
 /* ------------------------ Code from Andrea ------------------------ */
 static int find_index(int index, int* face_indices, int nindices)
 {
@@ -198,7 +209,7 @@
        *component_size = *component_size + 1;
 }
 
-int handle_separate_components(AUTOSEAM_Adjacency adj, int num_nodes, BMesh 
*bm, int recursion_depth)
+int handle_separate_components(AUTOSEAM_Adjacency adj, int num_nodes, BMesh 
*bm, int recursion_depth, bContext *C)
 {
        int i, j, k;
        int remaining_nodes = num_nodes;
@@ -251,7 +262,7 @@
                        }
                        
                        /* now call the recursive function for a single 
component*/
-                       generate_seam_recursive(bm, connected_adjacency, adj, 
recursion_depth);
+                       generate_seam_recursive(bm, connected_adjacency, adj, 
recursion_depth, C);
                        
                }
        
@@ -263,8 +274,170 @@
        autoseam_delete_adjacency(adj);
        return 0;
 }
-int generate_seam_recursive(BMesh *bm, AUTOSEAM_Adjacency adj, 
AUTOSEAM_Adjacency adj_big, int recursion_depth)
+
+
+/* The function assumes that there this is a triangulated face*/
+float stretch_of_triangulated_face(BMesh *bm, BMFace *face, bContext *C)
 {
+    BMVert *vert[3];
+    BMEdge *edge[3];
+    int i = 0;
+    BMIter iter2;
+       //BMVert *v;
+    BMEdge *e;
+    float Ps[3], Pt[3];
+    float *uv[3];
+    float face_area = BM_face_area(face);
+    BMLoop *l;
+    MLoopUV *luv;
+    BMIter liter;
+    float a,c,T;
+    int j;
+    float w, tmp[3];
+//    Scene *scene= CTX_data_scene(C);
+//     Object *obedit= CTX_data_edit_object(C);
+    
+//    if(!ED_uvedit_ensure_uvs(C, scene, obedit)) {
+//        //invalid case
+//             return -1.0;
+//     }
+    
+    if (face_area <= 0.0f) 
+        return 1e10f;
+    
+    w= 1.0f/(2.0f*face_area);
+    
+    i = 0;
+    BM_ITER(e, &iter2, bm, BM_EDGES_OF_FACE, face) {
+        //if(i == 3) break;
+        edge[i++] = e;
+    }
+    
+    /* now get the texture co-ordinates */
+    i = 0;
+    BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, face) {
+        luv = CustomData_bmesh_get(&bm->ldata, l->head.data, CD_MLOOPUV);
+        uv[i] = luv->uv;
+        vert[i++] = l->v;
+        
+    }
+        
+    // compute derivatives
+    copy_v3_v3(Ps, vert[0]->co);
+    mul_v3_fl(Ps, (uv[1][1] - uv[2][1]));
+    
+    copy_v3_v3(tmp, vert[1]->co);
+    mul_v3_fl(tmp, (uv[2][1] - uv[0][1]));
+    add_v3_v3(Ps, tmp);
+    
+    copy_v3_v3(tmp, vert[2]->co);
+    mul_v3_fl(tmp, (uv[0][1] - uv[1][1]));
+    add_v3_v3(Ps, tmp);
+    
+    mul_v3_fl(Ps, w);
+    
+    copy_v3_v3(Pt, vert[0]->co);
+    mul_v3_fl(Pt, (uv[2][0] - uv[1][0]));
+    
+    copy_v3_v3(tmp, vert[1]->co);
+    mul_v3_fl(tmp, (uv[0][0] - uv[2][0]));
+    add_v3_v3(Pt, tmp);
+    
+    copy_v3_v3(tmp, vert[2]->co);
+    mul_v3_fl(tmp, (uv[1][0] - uv[0][0]));
+    add_v3_v3(Pt, tmp);
+    
+    mul_v3_fl(Pt, w);
+    
+    
+    //Sander Tensor
+    a= dot_v3v3(Ps, Ps);
+    c= dot_v3v3(Pt, Pt);
+    
+    T =  sqrt(0.5f*(a + c));
+//    if (face->flag & PFACE_FILLED)
+//        T *= 0.2f;
+    
+    return T;
+
+}
+
+float stretch_of_mesh(bContext *C)
+{
+
+    BMIter face_iter;
+    BMFace *face, **newfaces = NULL;
+    BLI_array_declare(newfaces);
+    float (*projectverts)[3] = NULL;
+    BLI_array_declare(projectverts);
+    int i, lastlen=0;
+    Scene *scene= CTX_data_scene(C);
+    Object *obedit= CTX_data_edit_object(C);
+    BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
+       BMesh *bm = em->bm;
+    
+    float total_stretch = 0.0;
+    
+    /*first of all, unwrap the mesh once again*/
+    
+    if(!ED_uvedit_ensure_uvs(C, scene, obedit)) {
+        //invalid case
+               return -1.0;
+       }
+       ED_unwrap_lscm(scene, obedit, TRUE);
+    
+    /*
+     Now calculate the stretch.
+     sum = 0;
+     for all the faces of the mesh,
+            triangulate each face,
+            for all the faces that is generated for the triangulation
+                sum += strecth_of_face
+     return sum;
+    */
+    
+       i = 0;
+       for(face = BMIter_New(&face_iter, bm, BM_FACES_OF_MESH, NULL); face; 
face= BMIter_Step(&face_iter))
+       {
+        BMFace *temp_face;
+        int count_faces = 0;
+        
+               if (lastlen < face->len) {
+            BLI_array_empty(projectverts);
+            BLI_array_empty(newfaces);
+            /* allocate necessary memories*/
+            for (lastlen=0; lastlen < face->len; lastlen++) {
+                BLI_array_growone(projectverts);
+                BLI_array_growone(projectverts);
+                BLI_array_growone(projectverts);
+                BLI_array_growone(newfaces);
+            }
+        }
+        
+        /* triangulate face */
+        BM_Triangulate_Face(bm, face, projectverts, EDGE_NEW, FACE_NEW, 
newfaces);
+        
+        for (i=0; newfaces[i]; i++) {
+           //calculate strecth for each face, update stretch 
+            total_stretch += stretch_of_triangulated_face(bm, newfaces[i], C);
+            count_faces++;
+        }
+        
+        /* join the triangulated faces. */
+        if(count_faces > 1) temp_face = BM_Join_TwoFaces(bm, newfaces[0], 
newfaces[1], NULL);
+        for(i = 2; newfaces[i]; i++ ){
+            temp_face = BM_Join_TwoFaces(bm, temp_face, newfaces[i], NULL);
+        }
+       }
+    
+    BLI_array_free(projectverts);
+    BLI_array_free(newfaces);
+    return total_stretch;
+}
+
+
+int generate_seam_recursive(BMesh *bm, AUTOSEAM_Adjacency adj, 
AUTOSEAM_Adjacency adj_big, int recursion_depth, bContext *C)
+{
        int s;
        int i, j;
        int num_faces;
@@ -274,6 +447,7 @@
        AUTOSEAM_Adjacency adj_plus;
        AUTOSEAM_Adjacency adj_minus;
        
+    
        if(!recursion_depth) {
                autoseam_delete_adjacency(adj);
                return 0;
@@ -296,7 +470,8 @@
        //autoseam_clear_seam(bm);
        autoseam_mark_seam(bm, fplus, nplus, fminus, nminus);
                
-       /* create adj_plus */
+       printf("Stretch value is: %f\n", stretch_of_mesh(C));
+    /* create adj_plus */
        adj_plus = autoseam_create_adjacency(nplus);
        autoseam_set_min_value(adj_plus, min_value);
        
@@ -337,10 +512,14 @@
        recursion_depth--;
        
        /* recursive calls for two parts of the mesh. */
-       if(nplus > 0)
-               generate_seam_recursive(bm, adj_plus, adj_big, recursion_depth);
-       if(nminus > 0)
-               generate_seam_recursive(bm, adj_minus, adj_big, 
recursion_depth);
+       if(nplus > 0){
+        //can we construct PChart for this part and calculate strectch for 
that part?
+               generate_seam_recursive(bm, adj_plus, adj_big, recursion_depth, 
C);
+    }
+       if(nminus > 0){
+        //same is applicable for this part.
+               generate_seam_recursive(bm, adj_minus, adj_big, 
recursion_depth, C);
+    }
        
        autoseam_delete_adjacency(adj);
                
@@ -387,7 +566,7 @@
        autoseam_set_map_default(adj);
        
        /* This function will call the recusive function for each of the 
components of bmesh*/
-       handle_separate_components(adj, num_faces, bm, maxdepth);
+       handle_separate_components(adj, num_faces, bm, maxdepth, C);
        
        /* add uvs if they don't exist yet */
        if(!ED_uvedit_ensure_uvs(C, scene, obedit)) {
@@ -407,12 +586,12 @@
     
     
     /* Now try to minimize the stretch of the mesh */
-    minimize_stretch_init(C, op);
-    
-       iterations= RNA_int_get(op->ptr, "iterations");
-       for(i=0; i<iterations; i++)
-               minimize_stretch_iteration(C, op, 0);
-       minimize_stretch_exit(C, op, 0);
+//    minimize_stretch_init(C, op);
+//    
+//     iterations= RNA_int_get(op->ptr, "iterations");
+//     for(i=0; i<iterations; i++)
+//             minimize_stretch_iteration(C, op, 0);
+//     minimize_stretch_exit(C, op, 0);
        
 
        

Modified: 
branches/soc-2011-avocado/blender/source/blender/editors/mesh/autoseam_tools.h
===================================================================
--- 
branches/soc-2011-avocado/blender/source/blender/editors/mesh/autoseam_tools.h  
    2011-07-08 04:43:53 UTC (rev 38215)
+++ 
branches/soc-2011-avocado/blender/source/blender/editors/mesh/autoseam_tools.h  
    2011-07-08 06:16:17 UTC (rev 38216)
@@ -70,7 +70,7 @@
 void calculate_eigen(float **dual_graph, int dimension, float **eigen_vectors, 
float *eigen_valuess);
 int get_sign(float number);
 static void autoseam_clear_seam(BMesh *bm);
-int generate_seam_recursive(BMesh *bm, AUTOSEAM_Adjacency adj, 
AUTOSEAM_Adjacency adj_big, int recursion_depth);
+int generate_seam_recursive(BMesh *bm, AUTOSEAM_Adjacency adj, 
AUTOSEAM_Adjacency adj_big, int recursion_depth, bContext *C);
 
 
 

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to