merge with trunk at r31523
[blender.git] / source / blender / editors / mesh / meshtools.c
index 09bc513a568fb400cc285612ba00a329ed09b04c..35e1a0975e51ce03d1e7662660322e2ba62c37f3 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * $Id
+ * $Id$
  *
  * ***** BEGIN GPL LICENSE BLOCK *****
  *
@@ -15,7 +15,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  *
  * The Original Code is Copyright (C) 2004 by Blender Foundation
  * All rights reserved.
@@ -31,6 +31,7 @@
        meshtools.c: no editmode (violated already :), tools operating on meshes
 */
 
+#include <stddef.h>
 #include <stdlib.h>
 #include <string.h>
 #include <math.h>
 
 #include "MEM_guardedalloc.h"
 
-#include "DNA_image_types.h"
 #include "DNA_key_types.h"
 #include "DNA_material_types.h"
 #include "DNA_meshdata_types.h"
-#include "DNA_mesh_types.h"
 #include "DNA_object_types.h"
 #include "DNA_scene_types.h"
-#include "DNA_screen_types.h"
-#include "DNA_space_types.h"
-#include "DNA_view3d_types.h"
-#include "DNA_windowmanager_types.h"
-#include "DNA_world_types.h"
 
 #include "BLI_math.h"
 #include "BLI_blenlib.h"
 #include "BLI_threads.h"
 
 
-#include "BKE_blender.h"
 #include "BKE_context.h"
 #include "BKE_depsgraph.h"
-#include "BKE_customdata.h"
-#include "BKE_global.h"
-#include "BKE_image.h"
+#include "BKE_deform.h"
+#include "BKE_DerivedMesh.h"
 #include "BKE_key.h"
 #include "BKE_library.h"
 #include "BKE_main.h"
 #include "BKE_mesh.h"
 #include "BKE_material.h"
-#include "BKE_object.h"
-#include "BKE_utildefines.h"
 #include "BKE_report.h"
 #include "BKE_tessmesh.h"
 
-#include "RE_pipeline.h"
-#include "RE_shader_ext.h"
-
-#include "PIL_time.h"
-
-#include "IMB_imbuf_types.h"
-#include "IMB_imbuf.h"
-
-#include "GPU_draw.h"
-
 #include "BLO_sys_types.h" // for intptr_t support
 
 #include "ED_mesh.h"
 /* own include */
 #include "mesh_intern.h"
 
-/* XXX */
-static void waitcursor(int val) {}
-static void error() {}
-static int pupmenu() {return 0;}
-/* XXX */
-
 
 /* * ********************** no editmode!!! *********** */
 
@@ -113,6 +87,7 @@ return 0 if no join is made (error) and 1 of the join is done */
 
 int join_mesh_exec(bContext *C, wmOperator *op)
 {
+       Main *bmain= CTX_data_main(C);
        Scene *scene= CTX_data_scene(C);
        Object *ob= CTX_data_active_object(C);
        Material **matar, *ma;
@@ -215,12 +190,7 @@ int join_mesh_exec(bContext *C, wmOperator *op)
                        /* Join this object's vertex groups to the base one's */
                        for(dg=base->object->defbase.first; dg; dg=dg->next) {
                                /* See if this group exists in the object (if it doesn't, add it to the end) */
-                               for(odg=ob->defbase.first; odg; odg=odg->next) {
-                                       if(!strcmp(odg->name, dg->name)) {
-                                               break;
-                                       }
-                               }
-                               if(!odg) {
+                               if(!defgroup_find_name(ob, dg->name)) {
                                        odg = MEM_callocN(sizeof(bDeformGroup), "join deformGroup");
                                        memcpy(odg, dg, sizeof(bDeformGroup));
                                        BLI_addtail(&ob->defbase, odg);
@@ -512,7 +482,7 @@ int join_mesh_exec(bContext *C, wmOperator *op)
                        
                        /* free base, now that data is merged */
                        if(base->object != ob)
-                               ED_base_object_free_and_unlink(scene, base);
+                               ED_base_object_free_and_unlink(bmain, scene, base);
                }
        }
        CTX_DATA_END;
@@ -578,17 +548,17 @@ int join_mesh_exec(bContext *C, wmOperator *op)
                /* free it's ipo too - both are not actually freed from memory yet as ID-blocks */
                if(nkey->ipo) {
                        free_ipo(nkey->ipo);
-                       BLI_remlink(&G.main->ipo, nkey->ipo);
+                       BLI_remlink(&bmain->ipo, nkey->ipo);
                        MEM_freeN(nkey->ipo);
                }
 #endif
                
                free_key(nkey);
-               BLI_remlink(&G.main->key, nkey);
+               BLI_remlink(&bmain->key, nkey);
                MEM_freeN(nkey);
        }
        
-       DAG_scene_sort(scene);  // removed objects, need to rebuild dag before editmode call
+       DAG_scene_sort(bmain, scene);   // removed objects, need to rebuild dag before editmode call
        
        ED_object_enter_editmode(C, EM_WAITCURSOR);
        ED_object_exit_editmode(C, EM_FREEDATA|EM_WAITCURSOR|EM_DO_UNDO);
@@ -598,160 +568,87 @@ int join_mesh_exec(bContext *C, wmOperator *op)
        return OPERATOR_FINISHED;
 }
 
-/* ********************** SORT FACES ******************* */
-
-static void permutate(void *list, int num, int size, int *index)
-{
-       void *buf;
-       int len;
-       int i;
+/*********************** JOIN AS SHAPES ***************************/
 
-       len = num * size;
+/* Append selected meshes vertex locations as shapes of the active mesh, 
+  return 0 if no join is made (error) and 1 of the join is done */
 
-       buf = MEM_mallocN(len, "permutate");
-       memcpy(buf, list, len);
+int join_mesh_shapes_exec(bContext *C, wmOperator *op)
+{
+       Scene *scene= CTX_data_scene(C);
+       Object *ob= CTX_data_active_object(C);
+       Mesh *me= (Mesh *)ob->data;
+       Mesh *selme=NULL;
+       DerivedMesh *dm=NULL;
+       Key *key=me->key;
+       KeyBlock *kb;
+       int ok=0, nonequal_verts=0;
        
-       for (i = 0; i < num; i++) {
-               memcpy((char *)list + (i * size), (char *)buf + (index[i] * size), size);
+       CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
+               if (base->object == ob) continue;
+               
+               if (base->object->type==OB_MESH) {
+                       selme = (Mesh *)base->object->data;
+                       
+                       if (selme->totvert==me->totvert)
+                               ok++;
+                       else
+                               nonequal_verts=1;
+               }
        }
-       MEM_freeN(buf);
-}
-
-/* sort faces on view axis */
-static float *face_sort_floats;
-static int float_sort(const void *v1, const void *v2)
-{
-       float x1, x2;
+       CTX_DATA_END;
        
-       x1 = face_sort_floats[((int *) v1)[0]];
-       x2 = face_sort_floats[((int *) v2)[0]];
+       if (!ok) {
+               if (nonequal_verts)
+                       BKE_report(op->reports, RPT_ERROR, "Selected meshes must have equal numbers of vertices.");
+               else
+                       BKE_report(op->reports, RPT_ERROR, "No additional selected meshes with equal vertex count to join.");
+               return OPERATOR_CANCELLED;
+       }
        
-       if( x1 > x2 ) return 1;
-       else if( x1 < x2 ) return -1;
-       return 0;
-}
-
+       if(key == NULL) {
+               key= me->key= add_key((ID *)me);
+               key->type= KEY_RELATIVE;
 
-void sort_faces(Scene *scene, View3D *v3d)
-{
-       RegionView3D *rv3d= NULL; // get from context 
-       Object *ob= OBACT;
-       Mesh *me;
-       CustomDataLayer *layer;
-       int i, *index;
-       short event;
-       float reverse = 1;
-       int ctrl= 0;    // XXX
-       
-       if(!ob) return;
-       if(scene->obedit) return;
-       if(ob->type!=OB_MESH) return;
-       if (!v3d) return;
-       
-       me= ob->data;
-       if(me->totface==0) return;
-       
-       event = pupmenu(
-       "Sort Faces (Ctrl to reverse)%t|"
-       "View Axis%x1|"
-       "Cursor Distance%x2|"
-       "Material%x3|"
-       "Selection%x4|"
-       "Randomize%x5");
-       
-       if (event==-1) return;
-       
-       if(ctrl)
-               reverse = -1;
-       
-/*     create index list */
-       index = (int *) MEM_mallocN(sizeof(int) * me->totface, "sort faces");
-       for (i = 0; i < me->totface; i++) {
-               index[i] = i;
+               /* first key added, so it was the basis. initialise it with the existing mesh */
+               kb= add_keyblock(key, NULL);
+               mesh_to_key(me, kb);
        }
        
-       face_sort_floats = (float *) MEM_mallocN(sizeof(float) * me->totface, "sort faces float");
-       
-/* sort index list instead of faces itself 
-   and apply this permutation to all face layers */
-   
-       if (event == 5) {
-               /* Random */
-               for(i=0; i<me->totface; i++) {
-                       face_sort_floats[i] = BLI_frand();
-               }
-               qsort(index, me->totface, sizeof(int), float_sort);             
-       } else {
-               MFace *mf;
-               float vec[3];
-               float mat[4][4];
-               float cur[3];
-               
-               if (event == 1)
-                       mul_m4_m4m4(mat, OBACT->obmat, rv3d->viewmat); /* apply the view matrix to the object matrix */
-               else if (event == 2) { /* sort from cursor */
-                       if( v3d && v3d->localvd ) {
-                               VECCOPY(cur, v3d->cursor);
-                       } else {
-                               VECCOPY(cur, scene->cursor);
-                       }
-                       invert_m4_m4(mat, OBACT->obmat);
-                       mul_m4_v3(mat, cur);
-               }
+       /* now ready to add new keys from selected meshes */
+       CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
+               if (base->object == ob) continue;
                
-               mf= me->mface;
-               for(i=0; i<me->totface; i++, mf++) {
+               if(base->object->type==OB_MESH) {
+                       selme = (Mesh *)base->object->data;
                        
-                       if (event==3) {
-                               face_sort_floats[i] = ((float)mf->mat_nr)*reverse;
-                       } else if (event==4) {
-                               /*selected first*/
-                               if (mf->flag & ME_FACE_SEL)     face_sort_floats[i] = 0.0;
-                               else                                            face_sort_floats[i] = reverse;
-                       } else {
-                               /* find the faces center */
-                               add_v3_v3v3(vec, (me->mvert+mf->v1)->co, (me->mvert+mf->v2)->co);
-                               if (mf->v4) {
-                                       add_v3_v3v3(vec, vec, (me->mvert+mf->v3)->co);
-                                       add_v3_v3v3(vec, vec, (me->mvert+mf->v4)->co);
-                                       mul_v3_fl(vec, 0.25f);
-                               } else {
-                                       add_v3_v3v3(vec, vec, (me->mvert+mf->v3)->co);
-                                       mul_v3_fl(vec, 1.0f/3.0f);
-                               } /* done */
+                       if (selme->totvert==me->totvert) {
+                               dm = mesh_get_derived_deform(scene, base->object, CD_MASK_BAREMESH);
                                
-                               if (event == 1) { /* sort on view axis */
-                                       mul_m4_v3(mat, vec);
-                                       face_sort_floats[i] = vec[2] * reverse;
-                               } else { /* distance from cursor*/
-                                       face_sort_floats[i] = len_v3v3(cur, vec) * reverse; /* back to front */
-                               }
+                               if (!dm) continue;
+                                       
+                               kb= add_keyblock(key, base->object->id.name+2);
+                               
+                               DM_to_meshkey(dm, me, kb);
+                               
+                               dm->release(dm);
                        }
                }
-               qsort(index, me->totface, sizeof(int), float_sort);
        }
+       CTX_DATA_END;
        
-       MEM_freeN(face_sort_floats);
+       WM_event_add_notifier(C, NC_SCENE|ND_OB_ACTIVE, scene);
        
-       for(i = 0; i < me->fdata.totlayer; i++) {
-               layer = &me->fdata.layers[i];
-               permutate(layer->data, me->totface, CustomData_sizeof(layer->type), index);
-       }
-
-       MEM_freeN(index);
-
-       DAG_id_flush_update(ob->data, OB_RECALC_DATA);
+       return OPERATOR_FINISHED;
 }
 
-
-
 /* ********************* MESH VERTEX OCTREE LOOKUP ************* */
 
 /* important note; this is unfinished, needs better API for editmode, and custom threshold */
 
 #define MOC_RES                        8
 #define MOC_NODE_RES   8
-#define MOC_THRESH             0.0002f
+#define MOC_THRESH             0.00002f
 
 typedef struct MocNode {
        struct MocNode *next;
@@ -984,7 +881,204 @@ intptr_t mesh_octree_table(Object *ob, BMEditMesh *em, float *co, char mode)
        return 0;
 }
 
-int mesh_get_x_mirror_vert(Object *ob, int index)
+
+/* ********************* MESH VERTEX MIRR TOPO LOOKUP *************** */
+
+#define MIRRHASH_TYPE int
+
+typedef struct MirrTopoPair {
+       long hash;
+       int vIndex;
+} MirrTopoPair;
+
+static int MirrTopo_long_sort(const void *l1, const void *l2)
+{
+       if(                     (MIRRHASH_TYPE)(intptr_t)l1 > (MIRRHASH_TYPE)(intptr_t)l2 ) return  1;
+       else if(        (MIRRHASH_TYPE)(intptr_t)l1 < (MIRRHASH_TYPE)(intptr_t)l2 ) return -1;
+       return 0;
+}
+
+static int MirrTopo_item_sort(const void *v1, const void *v2)
+{
+       if(                     ((MirrTopoPair *)v1)->hash > ((MirrTopoPair *)v2)->hash ) return  1;
+       else if(        ((MirrTopoPair *)v1)->hash < ((MirrTopoPair *)v2)->hash ) return -1;
+       return 0;
+}
+
+static long *mesh_topo_lookup = NULL;
+static int  mesh_topo_lookup_tot = -1;
+static int  mesh_topo_lookup_mode = -1;
+
+/* mode is 's' start, or 'e' end, or 'u' use */
+/* if end, ob can be NULL */
+long mesh_mirrtopo_table(Object *ob, char mode)
+{
+#if 0
+       if(mode=='u') {         /* use table */
+               Mesh *me= ob->data;
+               if(     (mesh_topo_lookup==NULL) ||
+                       (mesh_topo_lookup_mode != ob->mode) ||
+                       (me->edit_mesh && me->edit_mesh->totvert != mesh_topo_lookup_tot) ||
+                       (me->edit_mesh==NULL && me->totvert != mesh_topo_lookup_tot)
+               ) {
+                       mesh_mirrtopo_table(ob, 's');
+               }
+       } else if(mode=='s') { /* start table */
+               Mesh *me= ob->data;
+               MEdge *medge;
+               EditMesh *em= me->edit_mesh;
+
+
+               /* editmode*/
+               EditEdge *eed;
+
+               int a, last, totvert;
+               int totUnique= -1, totUniqueOld= -1;
+
+               MIRRHASH_TYPE *MirrTopoHash = NULL;
+               MIRRHASH_TYPE *MirrTopoHash_Prev = NULL;
+               MirrTopoPair *MirrTopoPairs;
+               mesh_topo_lookup_mode= ob->mode;
+
+               /* reallocate if needed */
+               if (mesh_topo_lookup) {
+                       MEM_freeN(mesh_topo_lookup);
+                       mesh_topo_lookup = NULL;
+               }
+
+               if(em) {
+                       EditVert *eve;
+                       totvert= 0;
+                       for(eve= em->verts.first; eve; eve= eve->next) {
+                               eve->hash = totvert++;
+                       }
+               } else {
+                       totvert = me->totvert;
+               }
+
+               MirrTopoHash = MEM_callocN( totvert * sizeof(MIRRHASH_TYPE), "TopoMirr" );
+
+               /* Initialize the vert-edge-user counts used to detect unique topology */
+               if(em) {
+                       for(eed=em->edges.first; eed; eed= eed->next) {
+                               MirrTopoHash[eed->v1->hash]++;
+                               MirrTopoHash[eed->v2->hash]++;
+                       }
+               } else {
+                       for(a=0, medge=me->medge; a<me->totedge; a++, medge++) {
+                               MirrTopoHash[medge->v1]++;
+                               MirrTopoHash[medge->v2]++;
+                       }
+               }
+
+               MirrTopoHash_Prev = MEM_dupallocN( MirrTopoHash );
+
+               totUniqueOld = -1;
+               while(1) {
+                       /* use the number of edges per vert to give verts unique topology IDs */
+
+                       if(em) {
+                               for(eed=em->edges.first; eed; eed= eed->next) {
+                                       MirrTopoHash[eed->v1->hash] += MirrTopoHash_Prev[eed->v2->hash];
+                                       MirrTopoHash[eed->v2->hash] += MirrTopoHash_Prev[eed->v1->hash];
+                               }
+                       } else {
+                               for(a=0, medge=me->medge; a<me->totedge; a++, medge++) {
+                                       /* This can make realy big numbers, wrapping around here is fine */
+                                       MirrTopoHash[medge->v1] += MirrTopoHash_Prev[medge->v2];
+                                       MirrTopoHash[medge->v2] += MirrTopoHash_Prev[medge->v1];
+                               }
+                       }
+                       memcpy(MirrTopoHash_Prev, MirrTopoHash, sizeof(MIRRHASH_TYPE) * totvert);
+
+                       /* sort so we can count unique values */
+                       qsort(MirrTopoHash_Prev, totvert, sizeof(MIRRHASH_TYPE), MirrTopo_long_sort);
+
+                       totUnique = 1; /* account for skiping the first value */
+                       for(a=1; a<totvert; a++) {
+                               if (MirrTopoHash_Prev[a-1] != MirrTopoHash_Prev[a]) {
+                                       totUnique++;
+                               }
+                       }
+
+                       if (totUnique <= totUniqueOld) {
+                               /* Finish searching for unique valus when 1 loop dosnt give a
+                                * higher number of unique values compared to the previous loop */
+                               break;
+                       } else {
+                               totUniqueOld = totUnique;
+                       }
+                       /* Copy the hash calculated this iter, so we can use them next time */
+                       memcpy(MirrTopoHash_Prev, MirrTopoHash, sizeof(MIRRHASH_TYPE) * totvert);
+               }
+
+               /* Hash/Index pairs are needed for sorting to find index pairs */
+               MirrTopoPairs= MEM_callocN( sizeof(MirrTopoPair) * totvert, "MirrTopoPairs");
+
+               /* since we are looping through verts, initialize these values here too */
+               mesh_topo_lookup = MEM_mallocN( totvert * sizeof(long), "mesh_topo_lookup" );
+
+               if(em) {
+                       EM_init_index_arrays(em,1,0,0);
+               }
+
+
+               for(a=0; a<totvert; a++) {
+                       MirrTopoPairs[a].hash= MirrTopoHash[a];
+                       MirrTopoPairs[a].vIndex = a;
+
+                       /* initialize lookup */
+                       mesh_topo_lookup[a] = -1;
+               }
+
+               qsort(MirrTopoPairs, totvert, sizeof(MirrTopoPair), MirrTopo_item_sort);
+
+               /* Since the loop starts at 2, we must define the last index where the hash's differ */
+               last = ((totvert >= 2) && (MirrTopoPairs[0].hash == MirrTopoPairs[1].hash)) ? 0 : 1;
+
+               /* Get the pairs out of the sorted hashes, note, totvert+1 means we can use the previous 2,
+                * but you cant ever access the last 'a' index of MirrTopoPairs */
+               for(a=2; a < totvert+1; a++) {
+                       /* printf("I %d %ld %d\n", (a-last), MirrTopoPairs[a  ].hash, MirrTopoPairs[a  ].vIndex ); */
+                       if ((a==totvert) || (MirrTopoPairs[a-1].hash != MirrTopoPairs[a].hash)) {
+                               if (a-last==2) {
+                                       if(em) {
+                                               mesh_topo_lookup[MirrTopoPairs[a-1].vIndex] =   (long)EM_get_vert_for_index(MirrTopoPairs[a-2].vIndex);
+                                               mesh_topo_lookup[MirrTopoPairs[a-2].vIndex] =   (long)EM_get_vert_for_index(MirrTopoPairs[a-1].vIndex);
+                                       } else {
+                                               mesh_topo_lookup[MirrTopoPairs[a-1].vIndex] =   MirrTopoPairs[a-2].vIndex;
+                                               mesh_topo_lookup[MirrTopoPairs[a-2].vIndex] =   MirrTopoPairs[a-1].vIndex;
+                                       }
+                               }
+                               last= a;
+                       }
+               }
+               if(em) {
+                       EM_free_index_arrays();
+               }
+
+               MEM_freeN( MirrTopoPairs );
+               MirrTopoPairs = NULL;
+
+               MEM_freeN( MirrTopoHash );
+               MEM_freeN( MirrTopoHash_Prev );
+
+               mesh_topo_lookup_tot = totvert;
+
+       } else if(mode=='e') { /* end table */
+               if (mesh_topo_lookup) {
+                       MEM_freeN(mesh_topo_lookup);
+               }
+               mesh_topo_lookup = NULL;
+               mesh_topo_lookup_tot= -1;
+       }
+       return 0;
+#endif
+
+       return 0;
+}
+
+int mesh_get_x_mirror_vert_spacial(Object *ob, int index)
 {
        Mesh *me= ob->data;
        MVert *mvert;
@@ -998,7 +1092,33 @@ int mesh_get_x_mirror_vert(Object *ob, int index)
        return mesh_octree_table(ob, NULL, vec, 'u');
 }
 
+/*BMESH_TODO: hook into editbmesh_bvh.h code, though that'll require caching the
+              bvh structure most likely, not too difficult*/
 BMVert *editmesh_get_x_mirror_vert(Object *ob, BMEditMesh *em, float *co)
+{
+#if 0
+       if (mesh_mirrtopo_table(ob, 'u')==-1)
+               return -1;
+
+       return mesh_topo_lookup[index];
+#endif
+       return NULL;
+
+}
+
+int mesh_get_x_mirror_vert(Object *ob, int index)
+{
+#if 0 //BMESH_TODO
+       if (((Mesh *)ob->data)->editflag & ME_EDIT_MIRROR_TOPO) {
+               return mesh_get_x_mirror_vert_topo(ob, index);
+       } else {
+               return mesh_get_x_mirror_vert_spacial(ob, index);
+       }
+#endif
+       return 0;
+}
+
+static EditVert *editmesh_get_x_mirror_vert_spacial(Object *ob, EditMesh *em, float *co)
 {
        float vec[3];
        intptr_t poinval;
@@ -1020,6 +1140,89 @@ BMVert *editmesh_get_x_mirror_vert(Object *ob, BMEditMesh *em, float *co)
        return NULL;
 }
 
+static EditVert *editmesh_get_x_mirror_vert_topo(Object *ob, struct EditMesh *em, EditVert *eve, int index)
+{
+       long poinval;
+       if (mesh_mirrtopo_table(ob, 'u')==-1)
+               return NULL;
+
+       if (index!=-1) {
+               index = BLI_findindex(&em->verts, eve);
+       }
+
+       if (index==-1)
+               return NULL;
+
+       poinval= mesh_topo_lookup[ index ];
+
+       if(poinval != -1)
+               return (EditVert *)(poinval);
+       return NULL;
+}
+
+#if 0
+EditVert *editmesh_get_x_mirror_vert(Object *ob, struct EditMesh *em, EditVert *eve, float *co, int index)
+{
+       //BMESH_TODO use this flag, ME_EDIT_MIRROR_TOPO, at appropriate places
+       if (((Mesh *)ob->data)->editflag & ME_EDIT_MIRROR_TOPO) {
+               return editmesh_get_x_mirror_vert_topo(ob, em, eve, index);
+       } else {
+               return editmesh_get_x_mirror_vert_spacial(ob, em, eve->co);
+       }
+}
+#endif
+
+
+#if 0
+float *editmesh_get_mirror_uv(int axis, float *uv, float *mirrCent, float *face_cent)
+{
+       float vec[2];
+       float cent_vec[2];
+       float cent[2];
+
+       /* ignore nan verts */
+       if (isnan(uv[0]) || !finite(uv[0]) ||
+               isnan(uv[1]) || !finite(uv[1])
+          )
+               return NULL;
+
+       if (axis) {
+               vec[0]= uv[0];
+               vec[1]= -((uv[1])-mirrCent[1]) + mirrCent[1];
+
+               cent_vec[0] = face_cent[0];
+               cent_vec[1]= -((face_cent[1])-mirrCent[1]) + mirrCent[1];
+       } else {
+               vec[0]= -((uv[0])-mirrCent[0]) + mirrCent[0];
+               vec[1]= uv[1];
+
+               cent_vec[0]= -((face_cent[0])-mirrCent[0]) + mirrCent[0];
+               cent_vec[1] = face_cent[1];
+       }
+
+       /* TODO - Optimize */
+       {
+               EditFace *efa;
+               int i, len;
+               for(efa=em->faces.first; efa; efa=efa->next) {
+                       MTFace *tf= (MTFace *)CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
+                       uv_center(tf->uv, cent, (void *)efa->v4);
+
+                       if ( (fabs(cent[0] - cent_vec[0]) < 0.001) && (fabs(cent[1] - cent_vec[1]) < 0.001) ) {
+                               len = efa->v4 ? 4 : 3;
+                               for (i=0; i<len; i++) {
+                                       if ( (fabs(tf->uv[i][0] - vec[0]) < 0.001) && (fabs(tf->uv[i][1] - vec[1]) < 0.001) ) {
+                                               return tf->uv[i];
+                                       }
+                               }
+                       }
+               }
+       }
+
+       return NULL;
+}
+#endif
+
 static unsigned int mirror_facehash(void *ptr)
 {
        MFace *mf= ptr;
@@ -1085,7 +1288,7 @@ int *mesh_get_x_mirror_faces(Object *ob, BMEditMesh *em)
 
        mesh_octree_table(ob, em, NULL, 'e');
 
-       fhash= BLI_ghash_new(mirror_facehash, mirror_facecmp);
+       fhash= BLI_ghash_new(mirror_facehash, mirror_facecmp, "mirror_facehash gh");
        for(a=0, mf=mface; a<me->totface; a++, mf++)
                BLI_ghash_insert(fhash, mf, mf);
 
@@ -1115,191 +1318,3 @@ int *mesh_get_x_mirror_faces(Object *ob, BMEditMesh *em)
        
        return mirrorfaces;
 }
-
-/* ****************** render BAKING ********************** */
-
-/* threaded break test */
-static int thread_break(void *unused)
-{
-       return G.afbreek;
-}
-
-static ScrArea *biggest_image_area(bScreen *screen)
-{
-       ScrArea *sa, *big= NULL;
-       int size, maxsize= 0;
-       
-       for(sa= screen->areabase.first; sa; sa= sa->next) {
-               if(sa->spacetype==SPACE_IMAGE) {
-                       size= sa->winx*sa->winy;
-                       if(sa->winx > 10 && sa->winy > 10 && size > maxsize) {
-                               maxsize= size;
-                               big= sa;
-                       }
-               }
-       }
-       return big;
-}
-
-
-typedef struct BakeRender {
-       Render *re;
-       struct Object *actob;
-       int event, tot, ready;
-} BakeRender;
-
-static void *do_bake_render(void *bake_v)
-{
-       BakeRender *bkr= bake_v;
-       
-       bkr->tot= RE_bake_shade_all_selected(bkr->re, bkr->event, bkr->actob);
-       bkr->ready= 1;
-       
-       return NULL;
-}
-
-
-void objects_bake_render(Scene *scene, short event, char **error_msg)
-{
-       Object *actob= OBACT;
-       int active= scene->r.bake_flag & R_BAKE_TO_ACTIVE;
-       short prev_r_raytrace= 0, prev_wo_amb_occ= 0;
-       
-       if(event==0) event= scene->r.bake_mode;
-       
-       if(scene->r.renderer!=R_INTERN) {        
-               *error_msg = "Bake only supported for Internal Renderer";
-               return;
-       }        
-       
-       if(active && !actob) {
-               *error_msg = "No active object";
-               return;
-       }
-       
-       if(event>0) {
-               bScreen *screen= NULL; // XXX CTX
-               Render *re= RE_NewRender("_Bake View_");
-               ScrArea *area= biggest_image_area(screen);
-               ListBase threads;
-               BakeRender bkr;
-               int timer=0, tot; // XXX, sculptmode= G.f & G_SCULPTMODE;
-
-// XXX         if(sculptmode) set_sculptmode();
-               
-               if(event==1) event= RE_BAKE_ALL;
-               else if(event==2) event= RE_BAKE_AO;
-               else if(event==3) event= RE_BAKE_NORMALS;
-               else if(event==4) event= RE_BAKE_TEXTURE;
-               else if(event==5) event= RE_BAKE_DISPLACEMENT;
-               else event= RE_BAKE_SHADOW;
-
-               if(event==RE_BAKE_AO) {
-                       if(scene->world==NULL) {
-                               *error_msg = "No world set up";
-                               return;
-                       }
-
-                       /* If raytracing or AO is disabled, switch it on temporarily for baking. */
-                       prev_wo_amb_occ = (scene->world->mode & WO_AMB_OCC) != 0;
-                       scene->world->mode |= WO_AMB_OCC;
-               }
-               if(event==RE_BAKE_AO || active) {
-                       prev_r_raytrace = (scene->r.mode & R_RAYTRACE) != 0;
-                       scene->r.mode |= R_RAYTRACE;
-               }
-               
-               waitcursor(1);
-               RE_test_break_cb(re, NULL, thread_break);
-               G.afbreek= 0;   /* blender_test_break uses this global */
-               
-               RE_Database_Baking(re, scene, event, (active)? actob: NULL);
-               
-               /* baking itself is threaded, cannot use test_break in threads. we also update optional imagewindow */
-       
-               BLI_init_threads(&threads, do_bake_render, 1);
-               bkr.re= re;
-               bkr.event= event;
-               bkr.ready= 0;
-               bkr.actob= (active)? actob: NULL;
-               BLI_insert_thread(&threads, &bkr);
-               
-               while(bkr.ready==0) {
-                       PIL_sleep_ms(50);
-                       if(bkr.ready)
-                               break;
-                       
-                       if (!G.background) {
-                               blender_test_break();
-                               
-                               timer++;
-                               if(area && timer==20) {
-                                       Image *ima= RE_bake_shade_get_image();
-                                       if(ima) ((SpaceImage *)area->spacedata.first)->image= ima;
-// XX                                  scrarea_do_windraw(area);
-//                                     myswapbuffers();        
-                                       timer= 0;
-                               }
-                       }
-               }
-               BLI_end_threads(&threads);
-               tot= bkr.tot;
-               
-               RE_Database_Free(re);
-               waitcursor(0);
-               
-               if(tot==0) *error_msg = "No Images found to bake to";
-               else {
-                       Image *ima;
-                       /* force OpenGL reload and mipmap recalc */
-                       for(ima= G.main->image.first; ima; ima= ima->id.next) {
-                               if(ima->ok==IMA_OK_LOADED) {
-                                       ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
-                                       if(ibuf && (ibuf->userflags & IB_BITMAPDIRTY)) {
-                                               GPU_free_image(ima); 
-                                               imb_freemipmapImBuf(ibuf);
-                                       }
-                               }
-                       }
-               }
-               
-               /* restore raytrace and AO */
-               if(event==RE_BAKE_AO)
-                       if(prev_wo_amb_occ == 0)
-                               scene->world->mode &= ~WO_AMB_OCC;
-
-               if(event==RE_BAKE_AO || active)
-                       if(prev_r_raytrace == 0)
-                               scene->r.mode &= ~R_RAYTRACE;
-               
-// XXX         if(sculptmode) set_sculptmode();
-               
-       }
-}
-
-/* all selected meshes with UV maps are rendered for current scene visibility */
-static void objects_bake_render_ui(Scene *scene, short event)
-{
-       char *error_msg = NULL;
-//     int is_editmode = (obedit!=NULL);
-       
-       /* Deal with editmode, this is a bit clunky but since UV's are in editmode, users are likely to bake from their */
-// XXX if (is_editmode) exit_editmode(0);
-       
-       objects_bake_render(scene, event, &error_msg);
-       
-// XXX if (is_editmode) enter_editmode(0);
-       
-       if (error_msg)
-               error(error_msg);
-}
-
-void objects_bake_render_menu(Scene *scene)
-{
-       short event;
-       
-       event= pupmenu("Bake Selected Meshes %t|Full Render %x1|Ambient Occlusion %x2|Normals %x3|Texture Only %x4|Displacement %x5|Shadow %x6");
-       if (event < 1) return;
-       objects_bake_render_ui(scene, event);
-}
-