=bmesh= merge from trunk at r36529
authorJoseph Eagar <joeedh@gmail.com>
Sun, 8 May 2011 23:43:18 +0000 (23:43 +0000)
committerJoseph Eagar <joeedh@gmail.com>
Sun, 8 May 2011 23:43:18 +0000 (23:43 +0000)
162 files changed:
1  2 
intern/ghost/GHOST_C-api.h
intern/ghost/GHOST_ISystem.h
intern/ghost/intern/GHOST_C-api.cpp
intern/ghost/intern/GHOST_SystemCocoa.h
intern/ghost/intern/GHOST_SystemX11.cpp
intern/guardedalloc/intern/mallocn.c
intern/guardedalloc/intern/mmap_win.c
release/scripts/presets/interaction/maya.py
release/scripts/startup/bl_ui/properties_data_modifier.py
release/scripts/startup/bl_ui/space_info.py
release/scripts/startup/bl_ui/space_userpref.py
release/scripts/startup/bl_ui/space_view3d.py
source/blender/blenkernel/BKE_blender.h
source/blender/blenkernel/BKE_cdderivedmesh.h
source/blender/blenkernel/BKE_mesh.h
source/blender/blenkernel/CMakeLists.txt
source/blender/blenkernel/SConscript
source/blender/blenkernel/intern/DerivedMesh.c
source/blender/blenkernel/intern/armature.c
source/blender/blenkernel/intern/blender.c
source/blender/blenkernel/intern/cdderivedmesh.c
source/blender/blenkernel/intern/cloth.c
source/blender/blenkernel/intern/constraint.c
source/blender/blenkernel/intern/context.c
source/blender/blenkernel/intern/curve.c
source/blender/blenkernel/intern/deform.c
source/blender/blenkernel/intern/displist.c
source/blender/blenkernel/intern/font.c
source/blender/blenkernel/intern/lattice.c
source/blender/blenkernel/intern/library.c
source/blender/blenkernel/intern/mesh.c
source/blender/blenkernel/intern/mesh_validate.c
source/blender/blenkernel/intern/node.c
source/blender/blenkernel/intern/object.c
source/blender/blenkernel/intern/particle.c
source/blender/blenkernel/intern/particle_system.c
source/blender/blenkernel/intern/sca.c
source/blender/blenkernel/intern/seqcache.c
source/blender/blenkernel/intern/smoke.c
source/blender/blenkernel/intern/softbody.c
source/blender/blenkernel/intern/subsurf_ccg.c
source/blender/blenlib/BLI_math_geom.h
source/blender/blenlib/CMakeLists.txt
source/blender/blenlib/intern/BLI_ghash.c
source/blender/blenlib/intern/math_geom.c
source/blender/blenlib/intern/math_vector_inline.c
source/blender/blenlib/intern/pbvh.c
source/blender/blenloader/intern/readfile.c
source/blender/blenloader/intern/writefile.c
source/blender/bmesh/bmesh_iterators.h
source/blender/editors/armature/editarmature.c
source/blender/editors/armature/meshlaplacian.c
source/blender/editors/armature/poseobject.c
source/blender/editors/armature/reeb.c
source/blender/editors/curve/editcurve.c
source/blender/editors/curve/editfont.c
source/blender/editors/include/ED_mesh.h
source/blender/editors/include/ED_object.h
source/blender/editors/include/ED_uvedit.h
source/blender/editors/include/ED_view3d.h
source/blender/editors/interface/interface_handlers.c
source/blender/editors/mesh/bmesh_select.c
source/blender/editors/mesh/bmesh_tools.c
source/blender/editors/mesh/editface.c
source/blender/editors/mesh/mesh_data.c
source/blender/editors/mesh/mesh_ops.c
source/blender/editors/mesh/meshtools.c
source/blender/editors/metaball/mball_edit.c
source/blender/editors/object/object_add.c
source/blender/editors/object/object_constraint.c
source/blender/editors/object/object_intern.h
source/blender/editors/object/object_lattice.c
source/blender/editors/object/object_ops.c
source/blender/editors/object/object_relations.c
source/blender/editors/object/object_transform.c
source/blender/editors/object/object_vgroup.c
source/blender/editors/physics/particle_edit.c
source/blender/editors/render/render_shading.c
source/blender/editors/screen/screen_ops.c
source/blender/editors/sculpt_paint/paint_image.c
source/blender/editors/sculpt_paint/sculpt.c
source/blender/editors/sculpt_paint/sculpt_undo.c
source/blender/editors/space_image/CMakeLists.txt
source/blender/editors/space_image/image_buttons.c
source/blender/editors/space_image/image_draw.c
source/blender/editors/space_image/image_ops.c
source/blender/editors/space_image/space_image.c
source/blender/editors/space_outliner/outliner.c
source/blender/editors/space_view3d/CMakeLists.txt
source/blender/editors/space_view3d/SConscript
source/blender/editors/space_view3d/drawobject.c
source/blender/editors/space_view3d/space_view3d.c
source/blender/editors/space_view3d/view3d_edit.c
source/blender/editors/space_view3d/view3d_header.c
source/blender/editors/space_view3d/view3d_intern.h
source/blender/editors/space_view3d/view3d_ops.c
source/blender/editors/space_view3d/view3d_select.c
source/blender/editors/space_view3d/view3d_snap.c
source/blender/editors/space_view3d/view3d_view.c
source/blender/editors/transform/transform.c
source/blender/editors/transform/transform.h
source/blender/editors/transform/transform_constraints.c
source/blender/editors/transform/transform_conversions.c
source/blender/editors/transform/transform_generics.c
source/blender/editors/transform/transform_manipulator.c
source/blender/editors/transform/transform_snap.c
source/blender/editors/util/crazyspace.c
source/blender/editors/uvedit/CMakeLists.txt
source/blender/editors/uvedit/uvedit_buttons.c
source/blender/editors/uvedit/uvedit_draw.c
source/blender/editors/uvedit/uvedit_ops.c
source/blender/editors/uvedit/uvedit_parametrizer.c
source/blender/editors/uvedit/uvedit_unwrap_ops.c
source/blender/gpu/intern/gpu_draw.c
source/blender/gpu/intern/gpu_shader_material.glsl
source/blender/makesdna/DNA_mesh_types.h
source/blender/makesdna/DNA_modifier_types.h
source/blender/makesdna/DNA_userdef_types.h
source/blender/makesdna/intern/makesdna.c
source/blender/makesrna/intern/CMakeLists.txt
source/blender/makesrna/intern/rna_constraint.c
source/blender/makesrna/intern/rna_fcurve.c
source/blender/makesrna/intern/rna_image_api.c
source/blender/makesrna/intern/rna_modifier.c
source/blender/makesrna/intern/rna_nla.c
source/blender/makesrna/intern/rna_object.c
source/blender/makesrna/intern/rna_object_api.c
source/blender/makesrna/intern/rna_scene.c
source/blender/makesrna/intern/rna_userdef.c
source/blender/modifiers/CMakeLists.txt
source/blender/modifiers/MOD_modifiertypes.h
source/blender/modifiers/intern/MOD_build.c
source/blender/modifiers/intern/MOD_cast.c
source/blender/modifiers/intern/MOD_displace.c
source/blender/modifiers/intern/MOD_explode.c
source/blender/modifiers/intern/MOD_multires.c
source/blender/modifiers/intern/MOD_ngoninterp.c
source/blender/modifiers/intern/MOD_util.c
source/blender/modifiers/intern/MOD_util.h
source/blender/modifiers/intern/MOD_warp.c
source/blender/modifiers/intern/MOD_wave.c
source/blender/python/intern/bpy.c
source/blender/render/intern/raytrace/reorganize.h
source/blender/render/intern/source/convertblender.c
source/blender/render/intern/source/pipeline.c
source/blender/render/intern/source/pointdensity.c
source/blender/render/intern/source/shadeoutput.c
source/blender/render/intern/source/strand.c
source/blender/windowmanager/CMakeLists.txt
source/blender/windowmanager/SConscript
source/blender/windowmanager/intern/wm_event_system.c
source/blender/windowmanager/intern/wm_init_exit.c
source/blender/windowmanager/intern/wm_operators.c
source/blenderplayer/CMakeLists.txt
source/blenderplayer/bad_level_call_stubs/stubs.c
source/creator/CMakeLists.txt
source/creator/creator.c
source/gameengine/Converter/BL_BlenderDataConversion.cpp
source/gameengine/Ketsji/KXNetwork/SConscript
source/gameengine/Ketsji/SConscript
source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_OpenGLRasterizer.cpp
source/gameengine/Rasterizer/SConscript

index 18a806278fe7aa51de544b3aee73fa285e295125,75837239c4ac75fb30152750598a066aa98f4024..2f394c84e61f358a87d97801045ac5abb1b995e0
@@@ -846,12 -845,18 +846,24 @@@ extern GHOST_TUns8* GHOST_getClipboard(
  extern void GHOST_putClipboard(GHOST_TInt8 *buffer, int selection);
  
  
 +extern GHOST_TSuccess GHOST_RecordEvents(GHOST_SystemHandle systemhandle, FILE *file);
 +extern GHOST_TSuccess GHOST_StopRecording(GHOST_SystemHandle systemhandle);
 +extern GHOST_TSuccess GHOST_PlaybackEvents(GHOST_SystemHandle systemhandle, FILE *file);
 +extern int GHOST_PlayingEvents(GHOST_SystemHandle systemhandle);
 +extern int GHOST_RecordingEvents(GHOST_SystemHandle systemhandle);
 +
+ /**
+  * Toggles console
+  * @action    0 - Hides
+  *                    1 - Shows
+  *                    2 - Toggles
+  *                    3 - Hides if it runs not from  command line
+  *                    * - Does nothing
+  * @return current status (1 -visible, 0 - hidden)
+  */
+ extern int GHOST_toggleConsole(int action);
  #ifdef __cplusplus
  }
  #endif
Simple merge
Simple merge
Simple merge
Simple merge
index dd3a1005bae63cc247156faf97533aea95db1e19,1edec2b69d9bd706e09754c5ef43e847b4674f52..8491faf2ceb8515d1978afafe1a9a996f910a041
@@@ -58,14 -53,8 +58,11 @@@ int CDDM_Check(struct DerivedMesh *dm)
     data to not overwrite the original */
  struct DerivedMesh *CDDM_from_mesh(struct Mesh *mesh, struct Object *ob);
  
--/* creates a CDDerivedMesh from the given EditMesh */
--struct DerivedMesh *CDDM_from_editmesh(struct EditMesh *em, struct Mesh *me);
 +/* creates a CDDerivedMesh from the given BMEditMesh */
 +DerivedMesh *CDDM_from_BMEditMesh(struct BMEditMesh *em, struct Mesh *me, int use_mdisps);
 +
 +/* merge verts  */
 +DerivedMesh *CDDM_merge_verts(DerivedMesh *dm, int *vtargetmap);
  
  /* creates a CDDerivedMesh from the given curve object */
  struct DerivedMesh *CDDM_from_curve(struct Object *ob);
index 42c4cb47388e939849407d1cbb2908ed7cd80b40,08c150e30e3993a50bdc83012737f77e428ad56e..44d1f9f14278419f9c20d325cd2ff2da89c98020
@@@ -89,12 -66,6 +89,7 @@@ void free_mesh(struct Mesh *me, int unl
  struct Mesh *add_mesh(const char *name);
  struct Mesh *copy_mesh(struct Mesh *me);
  void mesh_update_customdata_pointers(struct Mesh *me);
- /*this ensures grouped customdata (e.g. mtexpoly and mloopuv and mtface, or
-   mloopcol and mcol) have the same relative active/render/clone/mask indices.*/
- void mesh_update_linked_customdata(struct Mesh *me);
- void make_local_tface(struct Mesh *me);
 +
  void make_local_mesh(struct Mesh *me);
  void boundbox_mesh(struct Mesh *me, float *loc, float *size);
  void tex_space_mesh(struct Mesh *me);
index a5a4f72523c9a976b5175c599ce7cececcbe2670,c6df694f61e1c5ac527cf6741a9a387dc90987eb..4040cc7d4f78867aedd160c89c4a5fa2c8710423
@@@ -87,8 -91,6 +92,7 @@@ set(SR
        intern/depsgraph.c
        intern/displist.c
        intern/effect.c
-       intern/exotic.c
 +      intern/editderivedbmesh.c
        intern/fcurve.c
        intern/fluidsim.c
        intern/fmodifier.c
Simple merge
index 421e8a89137f093e34b4b95c525e4ca047bd213b,d9c98bc0200af2a4e782ec1b32add26497d7370e..a8617773658a0c4c0855e04b2ce87a01b64ffe94
@@@ -1214,9 -1822,9 +1223,9 @@@ static void mesh_calc_modifiers(Scene *
  
                        /* if this is not the last modifier in the stack then recalculate the normals
                         * to avoid giving bogus normals to the next modifier see: [#23673] */
 -                      if(isPrevDeform &&  mti->dependsOnNormals && mti->dependsOnNormals(md)) {
 +                      if(dm && isPrevDeform &&  mti->dependsOnNormals && mti->dependsOnNormals(md)) {
                                /* XXX, this covers bug #23673, but we may need normal calc for other types */
-                               if(dm->type == DM_TYPE_CDDM) {
+                               if(dm && dm->type == DM_TYPE_CDDM) {
                                        CDDM_apply_vert_coords(dm, deformedVerts);
                                        CDDM_calc_normals(dm);
                                }
index e51aa4624b92d603f451416092e725965c08a924,45a60b842a7e633e29a9387b15a14f2bf14a96c5..8c9240bb0f8c35f73e9ca0809f03c0847253b46f
  #include "BKE_curve.h"
  /* -- */
  #include "BKE_object.h"
 +#include "BKE_tessmesh.h"
 +#include "BLI_edgehash.h"
 +
 +#include "BLI_blenlib.h"
 +#include "BLI_editVert.h"
 +#include "BLI_math.h"
 +#include "BLI_cellalloc.h"
 +#include "BLI_array.h"
 +#include "BLI_edgehash.h"
  
 +#include "bmesh.h"
 +
 +enum {
 +      MESHCMP_DVERT_WEIGHTMISMATCH = 1,
 +      MESHCMP_DVERT_GROUPMISMATCH,
 +      MESHCMP_DVERT_TOTGROUPMISMATCH,
 +      MESHCMP_LOOPCOLMISMATCH,
 +      MESHCMP_LOOPUVMISMATCH,
 +      MESHCMP_LOOPMISMATCH,
 +      MESHCMP_POLYVERTMISMATCH,
 +      MESHCMP_POLYMISMATCH,
 +      MESHCMP_EDGEUNKNOWN,
 +      MESHCMP_VERTCOMISMATCH,
 +      MESHCMP_CDLAYERS_MISMATCH,
 +};
  
 -EditMesh *BKE_mesh_get_editmesh(Mesh *me)
 +static char *cmpcode_to_str(int code)
  {
 -      return me->edit_mesh;
 +      switch (code) {
 +              case MESHCMP_DVERT_WEIGHTMISMATCH:
 +                      return "Vertex Weight Mismatch";
 +              case MESHCMP_DVERT_GROUPMISMATCH:
 +                                      return "Vertex Group Mismatch";
 +              case MESHCMP_DVERT_TOTGROUPMISMATCH:
 +                                      return "Vertex Doesn't Belong To Same Number Of Groups";
 +              case MESHCMP_LOOPCOLMISMATCH:
 +                                      return "Vertex Color Mismatch";
 +              case MESHCMP_LOOPUVMISMATCH:
 +                                      return "UV Mismatch";
 +              case MESHCMP_LOOPMISMATCH:
 +                                      return "Loop Mismatch";
 +              case MESHCMP_POLYVERTMISMATCH:
 +                                      return "Loop Vert Mismatch In Poly Test";
 +              case MESHCMP_POLYMISMATCH:
 +                                      return "Loop Vert Mismatch";
 +              case MESHCMP_EDGEUNKNOWN:
 +                                      return "Edge Mismatch";
 +              case MESHCMP_VERTCOMISMATCH:
 +                                      return "Vertex Coordinate Mismatch";
 +              case MESHCMP_CDLAYERS_MISMATCH:
 +                                      "CustomData Layer Count Mismatch";
 +              default:
 +                              return "Mesh Comparison Code Unknown";
 +              }
  }
  
 -void BKE_mesh_end_editmesh(Mesh *UNUSED(me), EditMesh *UNUSED(em))
 +/*thresh is threshold for comparing vertices, uvs, vertex colors,
 +  weights, etc.*/
 +int customdata_compare(CustomData *c1, CustomData *c2, Mesh *m1, Mesh *m2, float thresh)
  {
 +      CustomDataLayer *l1, *l2;
 +      int i, i1=0, i2=0, tot, j;
 +      
 +      for (i=0; i<c1->totlayer; i++) {
 +              if (ELEM7(c1->layers[i].type, CD_MVERT, CD_MEDGE, CD_MPOLY, 
 +                                CD_MLOOPUV, CD_MLOOPCOL, CD_MTEXPOLY, CD_MDEFORMVERT))                
 +                      i1++;
 +      }
 +      
 +      for (i=0; i<c2->totlayer; i++) {
 +              if (ELEM7(c2->layers[i].type, CD_MVERT, CD_MEDGE, CD_MPOLY, 
 +                                CD_MLOOPUV, CD_MLOOPCOL, CD_MTEXPOLY, CD_MDEFORMVERT))                
 +                      i2++;
 +      }
 +      
 +      if (i1 != i2)
 +              return MESHCMP_CDLAYERS_MISMATCH;
 +      
 +      l1 = c1->layers; l2 = c2->layers;
 +      tot = i1;
 +      i1 = 0; i2 = 0; 
 +      for (i=0; i < tot; i++) {
 +              while (i1 < c1->totlayer && !ELEM7(l1->type, CD_MVERT, CD_MEDGE, CD_MPOLY, 
 +                                CD_MLOOPUV, CD_MLOOPCOL, CD_MTEXPOLY, CD_MDEFORMVERT))
 +                      i1++, l1++;
 +
 +              while (i2 < c2->totlayer && !ELEM7(l2->type, CD_MVERT, CD_MEDGE, CD_MPOLY, 
 +                                CD_MLOOPUV, CD_MLOOPCOL, CD_MTEXPOLY, CD_MDEFORMVERT))
 +                      i2++, l2++;
 +              
 +              if (l1->type == CD_MVERT) {
 +                      MVert *v1 = l1->data;
 +                      MVert *v2 = l2->data;
 +                      int vtot = m1->totvert;
 +                      
 +                      for (j=0; j<vtot; j++, v1++, v2++) {
 +                              if (len_v3v3(v1->co, v2->co) > thresh)
 +                                      return MESHCMP_VERTCOMISMATCH;
 +                              /*I don't care about normals, let's just do coodinates*/
 +                      }
 +              }
 +              
 +              /*we're order-agnostic for edges here*/
 +              if (l1->type == CD_MEDGE) {
 +                      MEdge *e1 = l1->data;
 +                      MEdge *e2 = l2->data;
 +                      EdgeHash *eh = BLI_edgehash_new();
 +                      int etot = m1->totedge;
 +              
 +                      for (j=0; j<etot; j++, e1++) {
 +                              BLI_edgehash_insert(eh, e1->v1, e1->v2, e1);
 +                      }
 +                      
 +                      for (j=0; j<etot; j++, e2++) {
 +                              if (!BLI_edgehash_lookup(eh, e2->v1, e2->v2))
 +                                      return MESHCMP_EDGEUNKNOWN;
 +                      }
 +                      BLI_edgehash_free(eh, NULL);
 +              }
 +              
 +              if (l1->type == CD_MPOLY) {
 +                      MPoly *p1 = l1->data;
 +                      MPoly *p2 = l2->data;
 +                      int ptot = m1->totpoly;
 +              
 +                      for (j=0; j<ptot; j++, p1++, p2++) {
 +                              MLoop *lp1, *lp2;
 +                              int k;
 +                              
 +                              if (p1->totloop != p2->totloop)
 +                                      return MESHCMP_POLYMISMATCH;
 +                              
 +                              lp1 = m1->mloop + p1->loopstart;
 +                              lp2 = m2->mloop + p2->loopstart;
 +                              
 +                              for (k=0; k<p1->totloop; k++, lp1++, lp2++) {
 +                                      if (lp1->v != lp2->v)
 +                                              return MESHCMP_POLYVERTMISMATCH;
 +                              }
 +                      }
 +              }
 +              if (l1->type == CD_MLOOP) {
 +                      MLoop *lp1 = l1->data;
 +                      MLoop *lp2 = l2->data;
 +                      int ltot = m1->totloop;
 +              
 +                      for (j=0; j<ltot; j++, lp1++, lp2++) {
 +                              if (lp1->v != lp2->v)
 +                                      return MESHCMP_LOOPMISMATCH;
 +                      }
 +              }
 +              if (l1->type == CD_MLOOPUV) {
 +                      MLoopUV *lp1 = l1->data;
 +                      MLoopUV *lp2 = l2->data;
 +                      int ltot = m1->totloop;
 +              
 +                      for (j=0; j<ltot; j++, lp1++, lp2++) {
 +                              if (len_v2v2(lp1->uv, lp2->uv) > thresh)
 +                                      return MESHCMP_LOOPUVMISMATCH;
 +                      }
 +              }
 +              
 +              if (l1->type == CD_MLOOPCOL) {
 +                      MLoopCol *lp1 = l1->data;
 +                      MLoopCol *lp2 = l2->data;
 +                      int ltot = m1->totloop;
 +              
 +                      for (j=0; j<ltot; j++, lp1++, lp2++) {
 +                              if (ABS(lp1->r - lp2->r) > thresh || 
 +                                  ABS(lp1->g - lp2->g) > thresh || 
 +                                  ABS(lp1->b - lp2->b) > thresh || 
 +                                  ABS(lp1->a - lp2->a) > thresh)
 +                              {
 +                                      return MESHCMP_LOOPCOLMISMATCH;
 +                              }
 +                      }
 +              }
 +
 +              if (l1->type == CD_MDEFORMVERT) {
 +                      MDeformVert *dv1 = l1->data;
 +                      MDeformVert *dv2 = l2->data;
 +                      int dvtot = m1->totvert;
 +              
 +                      for (j=0; j<dvtot; j++, dv1++, dv2++) {
 +                              int k;
 +                              MDeformWeight *dw1 = dv1->dw, *dw2=dv2->dw;
 +                              
 +                              if (dv1->totweight != dv2->totweight)
 +                                      return MESHCMP_DVERT_TOTGROUPMISMATCH;
 +                              
 +                              for (k=0; k<dv1->totweight; k++, dw1++, dw2++) {
 +                                      if (dw1->def_nr != dw2->def_nr)
 +                                              return MESHCMP_DVERT_GROUPMISMATCH;
 +                                      if (ABS(dw1->weight - dw2->weight) > thresh)
 +                                              return MESHCMP_DVERT_WEIGHTMISMATCH;
 +                              }
 +                      }
 +              }
 +      }
 +      
 +      return 0;
  }
  
- void mesh_update_linked_customdata(Mesh *me)
 +/*used for testing.  returns an error string the two meshes don't match*/
 +char *mesh_cmp(Mesh *me1, Mesh *me2, float thresh)
 +{
 +      int c;
 +      
 +      if (!me1 || !me2)
 +              return "Requires two input meshes";
 +      
 +      if (me1->totvert != me2->totvert) 
 +              return "Number of verts don't match";
 +      
 +      if (me1->totedge != me2->totedge)
 +              return "Number of edges don't match";
 +      
 +      if (me1->totpoly != me2->totpoly)
 +              return "Number of faces don't match";
 +                              
 +      if (me1->totloop !=me2->totloop)
 +              return "Number of loops don't match";
 +      
 +      if ((c = customdata_compare(&me1->vdata, &me2->vdata, me1, me2, thresh)))
 +              return cmpcode_to_str(c);
 +
 +      if ((c = customdata_compare(&me1->edata, &me2->edata, me1, me2, thresh)))
 +              return cmpcode_to_str(c);
 +
 +      if ((c = customdata_compare(&me1->ldata, &me2->ldata, me1, me2, thresh)))
 +              return cmpcode_to_str(c);
 +
 +      if ((c = customdata_compare(&me1->pdata, &me2->pdata, me1, me2, thresh)))
 +              return cmpcode_to_str(c);
 +      
 +      return NULL;
 +}
 +
 +static void mesh_ensure_tesselation_customdata(Mesh *me)
 +{
 +      int tottex, totcol;
 +
 +      tottex = CustomData_number_of_layers(&me->fdata, CD_MTFACE);
 +      totcol = CustomData_number_of_layers(&me->fdata, CD_MCOL);
 +      
 +      if (tottex != CustomData_number_of_layers(&me->pdata, CD_MTEXPOLY) ||
 +          totcol != CustomData_number_of_layers(&me->ldata, CD_MLOOPCOL))
 +      {
 +              CustomData_free(&me->fdata, me->totface);
 +              
 +              me->mface = NULL;
 +              me->mtface = NULL;
 +              me->mcol = NULL;
 +              me->totface = 0;
 +
 +              memset(&me->fdata, 0, sizeof(&me->fdata));
 +
 +              CustomData_from_bmeshpoly(&me->fdata, &me->pdata, &me->ldata, me->totface);
 +              printf("Warning! Tesselation uvs or vcol data got out of sync, had to reset!\n");
 +      }
 +}
 +
 +/*this ensures grouped customdata (e.g. mtexpoly and mloopuv and mtface, or
 +  mloopcol and mcol) have the same relative active/render/clone/mask indices.*/
++static void mesh_update_linked_customdata(Mesh *me)
 +{
 +      int act;
 +
 +      if (me->edit_btmesh)
 +              BMEdit_UpdateLinkedCustomData(me->edit_btmesh);
 +
 +      mesh_ensure_tesselation_customdata(me);
 +
 +      if (CustomData_has_layer(&me->pdata, CD_MTEXPOLY)) {
 +              act = CustomData_get_active_layer(&me->pdata, CD_MTEXPOLY);
 +              CustomData_set_layer_active(&me->ldata, CD_MLOOPUV, act);
 +              CustomData_set_layer_active(&me->fdata, CD_MTFACE, act);
 +
 +              act = CustomData_get_render_layer(&me->pdata, CD_MTEXPOLY);
 +              CustomData_set_layer_render(&me->ldata, CD_MLOOPUV, act);
 +              CustomData_set_layer_render(&me->fdata, CD_MTFACE, act);
 +
 +              act = CustomData_get_clone_layer(&me->pdata, CD_MTEXPOLY);
 +              CustomData_set_layer_clone(&me->ldata, CD_MLOOPUV, act);
 +              CustomData_set_layer_clone(&me->fdata, CD_MTFACE, act);
 +
 +              act = CustomData_get_stencil_layer(&me->pdata, CD_MTEXPOLY);
 +              CustomData_set_layer_stencil(&me->ldata, CD_MLOOPUV, act);
 +              CustomData_set_layer_stencil(&me->fdata, CD_MTFACE, act);
 +      }
 +
 +      if (CustomData_has_layer(&me->ldata, CD_MLOOPCOL)) {
 +              act = CustomData_get_active_layer(&me->ldata, CD_MLOOPCOL);
 +              CustomData_set_layer_active(&me->fdata, CD_MCOL, act);
 +
 +              act = CustomData_get_render_layer(&me->ldata, CD_MLOOPCOL);
 +              CustomData_set_layer_render(&me->fdata, CD_MCOL, act);
 +
 +              act = CustomData_get_clone_layer(&me->ldata, CD_MLOOPCOL);
 +              CustomData_set_layer_clone(&me->fdata, CD_MCOL, act);
 +
 +              act = CustomData_get_stencil_layer(&me->ldata, CD_MLOOPCOL);
 +              CustomData_set_layer_stencil(&me->fdata, CD_MCOL, act);
 +      }
 +}
  
  void mesh_update_customdata_pointers(Mesh *me)
  {
@@@ -546,18 -234,10 +546,20 @@@ Mesh *copy_mesh(Mesh *me
                                        id_lib_extern((ID*)tface->tpage);
                }
        }
 -      
 +
 +      for(i=0; i<me->pdata.totlayer; i++) {
 +              if(me->pdata.layers[i].type == CD_MTEXPOLY) {
 +                      txface= (MTexPoly*)me->pdata.layers[i].data;
 +
 +                      for(a=0; a<me->totpoly; a++, txface++)
 +                              if(txface->tpage)
 +                                      id_lib_extern((ID*)txface->tpage);
 +              }
 +      }
 +
        men->mselect= NULL;
 -      men->edit_mesh= NULL;
++      men->edit_btmesh= NULL;
+       men->pv= NULL; /* looks like this is no-longer supported but NULL just incase */
  
        men->bb= MEM_dupallocN(men->bb);
        
        return men;
  }
  
- void make_local_tface(Mesh *me)
 +BMesh *BKE_mesh_to_bmesh(Mesh *me, Object *ob)
 +{
 +      BMesh *bm;
 +      int allocsize[4] = {512,512,2048,512};
 +
 +      bm = BM_Make_Mesh(ob, allocsize);
 +
 +      BMO_CallOpf(bm, "mesh_to_bmesh mesh=%p object=%p set_shapekey=%i", me, ob, 1);
 +
 +      return bm;
 +}
 +
+ static void make_local_tface(Main *bmain, Mesh *me)
  {
        MTFace *tface;
 +      MTexPoly *txface;
        Image *ima;
        int a, i;
        
                        }
                }
        }
 +
  }
  
+ static void expand_local_mesh(Main *bmain, Mesh *me)
+ {
+       id_lib_extern((ID *)me->texcomesh);
+       if(me->mtface) {
+               /* why is this an exception? - should not really make local when extern'ing - campbell */
+               make_local_tface(bmain, me);
+       }
+       if(me->mat) {
+               extern_local_matarar(me->mat, me->totcol);
+       }
+ }
  void make_local_mesh(Mesh *me)
  {
        Main *bmain= G.main;
index 42beb055140564c502e5d3f8b8f2eca6dfc2198f,00f88fb6202f773dd4d05b773e85da14f82aadd6..6f329433580ad927ead450838a247e323ef8029d
mode 100755,100644..100755
index 13748bf10ca2eb8b073a69b8a25a90499754b19d,5d44841df8b698b32e077717318bff0f786fdf08..2c0428b8597277269b2a120e6ec8078775b0b6a0
  
  #include "CCGSubSurf.h"
  
 -static int ccgDM_getVertMapIndex(CCGSubSurf *ss, CCGVert *v);
 -static int ccgDM_getEdgeMapIndex(CCGSubSurf *ss, CCGEdge *e);
 -static int ccgDM_getFaceMapIndex(CCGSubSurf *ss, CCGFace *f);
 +static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss,
 +                                         int drawInteriorEdges,
 +                                         int useSubsurfUv,
 +                                         DerivedMesh *dm);
+ static int ccgDM_use_grid_pbvh(CCGDerivedMesh *ccgdm);
  
  ///
  
Simple merge
Simple merge
index 85e98a5f6e62db68769e9c7debbe512361a24981,e1b13e0ee50b3afea4aa64ae9052a29466d2909c..a5cb20a13520922236f2cd330c41c24c6823cb85
@@@ -3438,27 -3416,19 +3454,26 @@@ static void lib_link_mesh(FileData *fd
  
  static void direct_link_dverts(FileData *fd, int count, MDeformVert *mdverts)
  {
-       int     i;
+       int i;
  
-       if (!mdverts)
+       if (mdverts == NULL) {
                return;
+       }
  
 -      for (i= count; i > 0; i--, mdverts++) {
 -              if(mdverts->dw) {
 -                      mdverts->dw= newdataadr(fd, mdverts->dw);
 -              }
 -              if (mdverts->dw == NULL) {
 -                      mdverts->totweight= 0;
 -              }
 +      for (i=0; i<count; i++) {
 +              void *tmp;
 +
 +              mdverts[i].dw=newdataadr(fd, mdverts[i].dw);
 +              
 +              /*convert to vgroup allocation system*/
 +              if (mdverts[i].dw) {
 +                      tmp = BLI_cellalloc_malloc(MEM_allocN_len(mdverts[i].dw), "vgroups from readfile.c");
 +                      memcpy(tmp, mdverts[i].dw, MEM_allocN_len(mdverts[i].dw));
 +
 +                      MEM_freeN(mdverts[i].dw);
 +                      mdverts[i].dw = tmp;
-               }
-               if (!mdverts[i].dw)
++              } else
 +                      mdverts[i].totweight=0;
        }
  }
  
index 1d35db8058e9fd2fc4fa175004de40cecbac6792,0000000000000000000000000000000000000000..a3af1938c8b154236d1637aa9f7fad7ef1e4e6ab
mode 100644,000000..100644
--- /dev/null
@@@ -1,91 -1,0 +1,90 @@@
 +/*
 + * BMESH ITERATORS
 + * 
 + * The functions and structures in this file 
 + * provide a unified method for iterating over 
 + * the elements of a mesh and answering simple
 + * adjacency queries. Tool authors should use
 + * the iterators provided in this file instead
 + * of inspecting the structure directly.
 + *
 +*/
 +
 +#ifndef BM_ITERATORS_H
 +#define BM_ITERATORS_H
 +
 +#include "BLI_mempool.h"
 +
 +/*Defines for passing to BMIter_New.
 + 
 + "OF" can be substituted for "around"
 +  so BM_VERTS_OF_FACE means "vertices
 +  around a face."
 + */
 +
 +/*these iterator over all elements of a specific
 +  type in the mesh.*/
 +#define BM_VERTS_OF_MESH                      1
 +#define BM_EDGES_OF_MESH                      2
 +#define BM_FACES_OF_MESH                      3
 +
 +/*these are topological iterators.*/
 +#define BM_EDGES_OF_VERT                      4
 +#define BM_FACES_OF_VERT                      5
 +#define BM_LOOPS_OF_VERT                      6
 +#define BM_FACES_OF_EDGE                      7
 +#define BM_VERTS_OF_FACE                      8
 +#define BM_EDGES_OF_FACE                      9
 +#define BM_LOOPS_OF_FACE                      10
 +/*returns elements from all boundaries, and returns 
 +the first element at the end to flag that we're entering 
 +a different face hole boundary*/
 +#define BM_ALL_LOOPS_OF_FACE          11
 +
 +/*iterate through loops around this loop, which are fetched
 +  from the other faces in the radial cycle surrounding the
 +  input loop's edge.*/
 +#define BM_LOOPS_OF_LOOP              12
 +#define BM_LOOPS_OF_EDGE              13
 +
 +#define BM_ITER(ele, iter, bm, type, data) \
 +      ele = BMIter_New(iter, bm, type, data); \
 +      for ( ; ele; ele=BMIter_Step(iter))
 +
 +#define BM_ITER_SELECT(ele, iter, bm, type, data)\
 +for (ele = BMIter_New(iter, bm, type, data); ele; ele=BMIter_Step(iter)) {\
 +      if (BM_TestHFlag(ele, BM_HIDDEN) || !BM_TestHFlag(ele, BM_SELECT)) continue;
 +
 +#define BM_ITER_VISIBLE(ele, iter, bm, type, data)\
 +for (ele = BMIter_New(iter, bm, type, data); ele; ele=BMIter_Step(iter)) {\
 +      if (BM_TestHFlag(ele, BM_HIDDEN)) continue;
 +              
 +#define BM_ITER_NOTSELECT(ele, iter, bm, type, data)\
 +for (ele = BMIter_New(iter, bm, type, data); ele; ele=BMIter_Step(iter)) {\
 +      if (BM_TestHFlag(ele, BM_HIDDEN) || BM_TestHFlag(ele, BM_SELECT)) continue;
 +
 +/*Iterator Structure*/
 +typedef struct BMIter {
 +      BLI_mempool_iter pooliter;
 +
 +      struct BMVert *firstvert, *nextvert, *vdata;
 +      struct BMEdge *firstedge, *nextedge, *edata;
 +      struct BMLoop *firstloop, *nextloop, *ldata, *l;
 +      struct BMFace *firstpoly, *nextpoly, *pdata;
 +      struct BMesh *bm;
 +      void (*begin)(struct BMIter *iter);
 +      void *(*step)(struct BMIter *iter);
 +      union{
 +              void            *p;
 +              int                     i;
 +              long            l;
 +              float           f;
 +      }filter;
 +      int type, count;
 +}BMIter;
 +
 +void *BMIter_New(struct BMIter *iter, struct BMesh *bm, int type, void *data);
 +void *BMIter_Step(struct BMIter *iter);
 +void *BMIter_AtIndex(struct BMesh *bm, int type, void *data, int index);
 +
 +#endif
index b55af8ec3881deae5894ed22f96509d8f4dd9dfe,c3c843154e5f35c4c2c9a903e21e64511b65fd98..adbaa4196842def1dff1b721e254410f10f9ca6e
@@@ -96,145 -84,110 +96,147 @@@ struct ToolSeetings
  #define B_JOINTRIA_VCOL               0X400
  #define B_JOINTRIA_SHARP      0X800
  #define B_JOINTRIA_MAT                0X1000
 -#define B_FRACTAL                     0x2000
 -#define B_SPHERE                      0x4000
 -
 -/* meshtools.c */
 +#define B_FRACTAL             0x2000
 +#define B_SPHERE              0x4000
  
 -intptr_t      mesh_octree_table(struct Object *ob, struct EditMesh *em, float *co, char mode);
 -long          mesh_mirrtopo_table(struct Object *ob, char mode);
 +float *bm_get_cd_float(struct CustomData *cdata, void *data, int type);
  
 -struct EditVert   *editmesh_get_x_mirror_vert(struct Object *ob, struct EditMesh *em, struct EditVert *eve, float *co, int index);
 -int                   mesh_get_x_mirror_vert(struct Object *ob, int index);
 -int                   *mesh_get_x_mirror_faces(struct Object *ob, struct EditMesh *em);
 +/* bmeshutils.c */
  
 -int                   join_mesh_exec(struct bContext *C, struct wmOperator *op);
 -int                   join_mesh_shapes_exec(struct bContext *C, struct wmOperator *op);
 +/*
 + [note: I've decided to use ideasman's code for non-editmode stuff, but since
 +  it has a big "not for editmode!" disclaimer, I'm going to keep what I have here
 +  - joeedh]
 +  
 + x-mirror editing api.  usage:
 +  
 +  EDBM_CacheMirrorVerts(em);
 +  ...
 +  ...
 +  BM_ITER(v, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
 +     mirrorv = EDBM_GetMirrorVert(em, v);
 +  }
 +  ...
 +  ...
 +  EDBM_EndMirrorCache(em);
 + 
 +  note: why do we only allow x axis mirror editing?
 +  */
 +void EDBM_CacheMirrorVerts(struct BMEditMesh *em);
 +
 +/*retrieves mirrored cache vert, or NULL if there isn't one.
 +  note: calling this without ensuring the mirror cache state
 +  is bad.*/
 +struct BMVert *EDBM_GetMirrorVert(struct BMEditMesh *em, struct BMVert *v);
 +void EDBM_EndMirrorCache(struct BMEditMesh *em);
 +
 +void EDBM_RecalcNormals(struct BMEditMesh *em);
 +
 +void EDBM_MakeEditBMesh(struct ToolSettings *ts, struct Scene *scene, struct Object *ob);
 +void EDBM_FreeEditBMesh(struct BMEditMesh *tm);
 +void EDBM_LoadEditBMesh(struct Scene *scene, struct Object *ob);
 +
 +void EDBM_init_index_arrays(struct BMEditMesh *em, int forvert, int foredge, int forface);
 +void EDBM_free_index_arrays(struct BMEditMesh *em);
 +struct BMVert *EDBM_get_vert_for_index(struct BMEditMesh *em, int index);
 +struct BMEdge *EDBM_get_edge_for_index(struct BMEditMesh *em, int index);
 +struct BMFace *EDBM_get_face_for_index(struct BMEditMesh *em, int index);
 +struct BMFace *EDBM_get_actFace(struct BMEditMesh *em, int sloppy);
 +
 +int EDBM_CallAndSelectOpf(struct BMEditMesh *em, struct wmOperator *op, 
 +                                                const char *selectslot, const char *fmt, ...);
 +
 +/*flushes based on the current select mode.  if in vertex select mode,
 +  verts select/deselect edges and faces, if in edge select mode,
 +  edges select/deselect faces and vertices, and in face select mode faces select/deselect
 +  edges and vertices.*/
 +void EDBM_selectmode_flush(struct BMEditMesh *em);
 +
 +int EDBM_get_actSelection(struct BMEditMesh *em, struct BMEditSelection *ese);
 +
 +/*exactly the same as EDBM_selectmode_flush, but you pass in the selectmode
 +  instead of using the current one*/
 +void EDBM_select_flush(struct BMEditMesh *em, int selectmode);
 +void EDBM_deselect_flush(struct BMEditMesh *em);
 +
 +void EDBM_selectmode_set(struct BMEditMesh *em);
 +void EDBM_convertsel(struct BMEditMesh *em, short oldmode, short selectmode);
 +void          undo_push_mesh(struct bContext *C, const char *name);
  
 -/* mesh_ops.c */
 -void          ED_operatortypes_mesh(void);
 -void          ED_operatormacros_mesh(void);
 -void          ED_keymap_mesh(struct wmKeyConfig *keyconf);
 +void EDBM_editselection_center(struct BMEditMesh *em, float *center, struct BMEditSelection *ese);
 +void EDBM_editselection_plane(struct BMEditMesh *em, float *plane, struct BMEditSelection *ese);
 +void EDBM_editselection_normal(float *normal, struct BMEditSelection *ese);
 +int EDBM_vertColorCheck(struct BMEditMesh *em);
 +void EDBM_validate_selections(struct BMEditMesh *em);
  
 +void EDBM_pin_mesh(struct BMEditMesh *em, int swap);
 +void EDBM_unpin_mesh(struct BMEditMesh *em, int swap);
  
 -/* editmesh.c */
 -void          make_editMesh(struct Scene *scene, struct Object *ob);
 -void          load_editMesh(struct Scene *scene, struct Object *ob);
 -void          remake_editMesh(struct Scene *scene, struct Object *ob);
 -void          free_editMesh(struct EditMesh *em);
 +void EDBM_hide_mesh(struct BMEditMesh *em, int swap);
 +void EDBM_reveal_mesh(struct BMEditMesh *em);
  
 -void          recalc_editnormals(struct EditMesh *em);
 +int                   EDBM_check_backbuf(unsigned int index);
 +int                   EDBM_mask_init_backbuf_border(struct ViewContext *vc, short mcords[][2], short tot, short xmin, short ymin, short xmax, short ymax);
 +void          EDBM_free_backbuf(void);
 +int                   EDBM_init_backbuf_border(struct ViewContext *vc, short xmin, short ymin, short xmax, short ymax);
 +int                   EDBM_init_backbuf_circle(struct ViewContext *vc, short xs, short ys, short rads);
  
 -void          EM_init_index_arrays(struct EditMesh *em, int forVert, int forEdge, int forFace);
 -void          EM_free_index_arrays(void);
 -struct EditVert       *EM_get_vert_for_index(int index);
 -struct EditEdge       *EM_get_edge_for_index(int index);
 -struct EditFace       *EM_get_face_for_index(int index);
 -int                   EM_texFaceCheck(struct EditMesh *em);
 -int                   EM_vertColorCheck(struct EditMesh *em);
 +void EDBM_select_swap(struct BMEditMesh *em); /* exported for UV */
 +void EDBM_set_actFace(struct BMEditMesh *em, struct BMFace *efa);
  
 -void          undo_push_mesh(struct bContext *C, const char *name);
 +int EDBM_texFaceCheck(struct BMEditMesh *em);
 +struct MTexPoly *EDBM_get_active_mtexpoly(struct BMEditMesh *em, struct BMFace **act_efa, int sloppy);
  
 +void EDBM_free_uv_vert_map(struct UvVertMap *vmap);
 +struct UvMapVert *EDBM_get_uv_map_vert(struct UvVertMap *vmap, unsigned int v);
 +struct UvVertMap *EDBM_make_uv_vert_map(struct BMEditMesh *em, int selected, int do_face_idx_array, float *limit);
 +void          EM_add_data_layer(struct BMEditMesh *em, struct CustomData *data, int type, const char *name);
 +void          EM_free_data_layer(struct BMEditMesh *em, struct CustomData *data, int type);
  
 -/* editmesh_lib.c */
 +void EDBM_toggle_select_all(struct BMEditMesh *em);
 +void EDBM_set_flag_all(struct BMEditMesh *em, int flag);
 +void EDBM_clear_flag_all(struct BMEditMesh *em, int flag);
 +void EDBM_automerge(struct Scene *scene, struct Object *ob, int update);
  
 -struct EditFace       *EM_get_actFace(struct EditMesh *em, int sloppy);
 -void             EM_set_actFace(struct EditMesh *em, struct EditFace *efa);
 -float            EM_face_area(struct EditFace *efa);
 +/* meshtools.c */
  
 -void          EM_select_edge(struct EditEdge *eed, int sel);
 -void          EM_select_face(struct EditFace *efa, int sel);
 -void          EM_select_face_fgon(struct EditMesh *em, struct EditFace *efa, int val);
 -void          EM_select_swap(struct EditMesh *em);
 -void          EM_toggle_select_all(struct EditMesh *em);
 -void          EM_select_all(struct EditMesh *em);
 -void          EM_deselect_all(struct EditMesh *em);
 -void          EM_selectmode_flush(struct EditMesh *em);
 -void          EM_deselect_flush(struct EditMesh *em);
 -void          EM_selectmode_set(struct EditMesh *em);
 -void          EM_select_flush(struct EditMesh *em);
 -void          EM_convertsel(struct EditMesh *em, short oldmode, short selectmode);
 -void          EM_validate_selections(struct EditMesh *em);
 -void          EM_selectmode_to_scene(struct Scene *scene, struct Object *obedit);
 +void EM_project_snap_verts(struct bContext *C, struct ARegion *ar, struct Object *obedit, struct BMEditMesh *em);
  
 -                      /* exported to transform */
 -int                   EM_get_actSelection(struct EditMesh *em, struct EditSelection *ese);
 -void          EM_editselection_normal(float *normal, struct EditSelection *ese);
 -void          EM_editselection_plane(float *plane, struct EditSelection *ese);
 -void          EM_editselection_center(float *center, struct EditSelection *ese);                      
 +/* editmesh_mods.c */
 +extern unsigned int bm_vertoffs, bm_solidoffs, bm_wireoffs;
  
 -struct UvVertMap *EM_make_uv_vert_map(struct EditMesh *em, int selected, int do_face_idx_array, float *limit);
 -struct UvMapVert *EM_get_uv_map_vert(struct UvVertMap *vmap, unsigned int v);
 -void              EM_free_uv_vert_map(struct UvVertMap *vmap);
 +intptr_t      mesh_octree_table(struct Object *ob, struct BMEditMesh *em, float *co, char mode);
 +long          mesh_mirrtopo_table(struct Object *ob, char mode);
  
 -void          EM_add_data_layer(struct EditMesh *em, struct CustomData *data, int type, const char *name);
 -void          EM_free_data_layer(struct EditMesh *em, struct CustomData *data, int type);
++//BMESH_TODO void             EM_cache_x_mirror_vert(struct Object *ob, struct BMEditMesh *em);
 -void          EM_make_hq_normals(struct EditMesh *em);
 -void          EM_solidify(struct EditMesh *em, float dist);
++int                   mouse_mesh(struct bContext *C, const short mval[2], short extend);
 -int                   EM_deselect_nth(struct EditMesh *em, int nth);
 +struct BMVert   *editbmesh_get_x_mirror_vert(struct Object *ob, struct BMEditMesh *em, struct BMVert *eve, float *co, int index);
 +int                   mesh_get_x_mirror_vert(struct Object *ob, int index);
 +int                   *mesh_get_x_mirror_faces(struct Object *ob, struct BMEditMesh *em);
  
 -void EM_project_snap_verts(struct bContext *C, struct ARegion *ar, struct Object *obedit, struct EditMesh *em);
 +int                   join_mesh_exec(struct bContext *C, struct wmOperator *op);
 +int                   join_mesh_shapes_exec(struct bContext *C, struct wmOperator *op);
  
 -/* editmesh_mods.c */
 -extern unsigned int em_vertoffs, em_solidoffs, em_wireoffs;
 +/* mesh_ops.c */
 +void          ED_operatortypes_mesh(void);
 +void          ED_operatormacros_mesh(void);
 +void          ED_keymap_mesh(struct wmKeyConfig *keyconf);
  
 -void          EM_cache_x_mirror_vert(struct Object *ob, struct EditMesh *em);
 -int                   mouse_mesh(struct bContext *C, const short mval[2], short extend);
 -int                   EM_check_backbuf(unsigned int index);
 -int                   EM_mask_init_backbuf_border(struct ViewContext *vc, short mcords[][2], short tot, short xmin, short ymin, short xmax, short ymax);
 -void          EM_free_backbuf(void);
 -int                   EM_init_backbuf_border(struct ViewContext *vc, short xmin, short ymin, short xmax, short ymax);
 -int                   EM_init_backbuf_circle(struct ViewContext *vc, short xs, short ys, short rads);
  
 -void          EM_hide_mesh(struct EditMesh *em, int swap);
 -void          EM_reveal_mesh(struct EditMesh *em);
 +/* editmesh.c */
  
 -void          EM_select_by_material(struct EditMesh *em, int index);
 -void          EM_deselect_by_material(struct EditMesh *em, int index); 
 +void          ED_spacetypes_init(void);
 +void          ED_keymap_mesh(struct wmKeyConfig *keyconf);
  
 -void          EM_automerge(struct Scene *scene, struct Object *obedit, int update);
 +/* bmesh_mods.c */
 +extern unsigned int bm_vertoffs, bm_solidoffs, bm_wireoffs;
  
- int mouse_mesh(struct bContext *C, short mval[2], short extend);
  /* editface.c */
  void paintface_flush_flags(struct Object *ob);
 -struct MTFace *EM_get_active_mtface(struct EditMesh *em, struct EditFace **act_efa, struct MCol **mcol, int sloppy);
 +struct MTexPoly       *EM_get_active_mtexpoly(struct BMEditMesh *em, struct BMFace **act_efa, struct MLoopCol **col, int sloppy);
- int paintface_mouse_select(struct bContext *C, struct Object *ob, short mval[2], int extend);
+ int paintface_mouse_select(struct bContext *C, struct Object *ob, const short mval[2], int extend);
  int do_paintface_box_select(struct ViewContext *vc, struct rcti *rect, int select, int extend);
  void paintface_deselect_all_visible(struct Object *ob, int action, short flush_flags);
  void paintface_select_linked(struct bContext *C, struct Object *ob, short mval[2], int mode);
index 37c146a810c4e8e79a58ca5e4a46b01a35a13f89,297bd234edd2d9ac806f0533fedf883212b68dcb..0ea9fc1a3f93a1f57a67a58688d4f9353cb97195
@@@ -116,9 -113,7 +116,8 @@@ int get_view3d_cliprange(struct View3D 
  int get_view3d_viewplane(struct View3D *v3d, struct RegionView3D *rv3d, int winxi, int winyi, struct rctf *viewplane, float *clipsta, float *clipend, float *pixsize);
  int get_view3d_ortho(struct View3D *v3d, struct RegionView3D *rv3d);
  void view3d_get_object_project_mat(struct RegionView3D *v3d, struct Object *ob, float pmat[4][4]);
- /* computes screen x/y in vec */
- void view3d_project_float(struct ARegion *a, float *vec, float *adr, float mat[4][4]);
+ void view3d_project_float(struct ARegion *a, const float vec[3], float adr[2], float mat[4][4]);
 +void view3d_project_float_v3(struct ARegion *a, float *vec, float *adr, float mat[4][4]);
  void view3d_calc_camera_border(struct Scene *scene, struct ARegion *ar, struct RegionView3D *rv3d, struct View3D *v3d, struct rctf *viewborder_r, short do_shift);
  
  /* drawobject.c iterators */
index 20474e2fba0b913f35ce1d47e06d0114c526c59d,0000000000000000000000000000000000000000..e0c0215bc2b6934a976fab6b425f69debed2366d
mode 100644,000000..100644
--- /dev/null
@@@ -1,2440 -1,0 +1,2440 @@@
- int mouse_mesh(bContext *C, short mval[2], short extend)
 +/**
 + * $Id: 
 + *
 + * ***** BEGIN GPL LICENSE BLOCK *****
 + *
 + * This program is free software; you can redistribute it and/or
 + * modify it under the terms of the GNU General Public License
 + * as published by the Free Software Foundation; either version 2
 + * of the License, or (at your option) any later version.
 + *
 + * This program is distributed in the hope that it will be useful,
 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 + * GNU General Public License for more details.
 + *
 + * 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.
 + *
 + * The Original Code is Copyright (C) 2004 Blender Foundation.
 + * All rights reserved.
 + *
 + * The Original Code is: all of this file.
 + *
 + * Contributor(s): none yet.
 + *
 + * ***** END GPL LICENSE BLOCK *****
 + */
 +
 +/*
 +
 +BMEditMesh_mods.c, UI level access, no geometry changes 
 +
 +*/
 +
 +#include <stdlib.h>
 +#include <string.h>
 +#include <math.h>
 +
 +#include "MEM_guardedalloc.h"
 +
 +#include "DNA_mesh_types.h"
 +#include "DNA_material_types.h"
 +#include "DNA_meshdata_types.h"
 +#include "DNA_modifier_types.h"
 +#include "DNA_object_types.h"
 +#include "DNA_texture_types.h"
 +#include "DNA_scene_types.h"
 +#include "DNA_screen_types.h"
 +#include "DNA_space_types.h"
 +#include "DNA_view3d_types.h"
 +
 +#include "BLI_blenlib.h"
 +#include "BLI_math.h"
 +#include "BLI_rand.h"
 +#include "BLI_array.h"
 +#include "BLI_smallhash.h"
 +
 +#include "BKE_context.h"
 +#include "BKE_displist.h"
 +#include "BKE_depsgraph.h"
 +#include "BKE_DerivedMesh.h"
 +#include "BKE_customdata.h"
 +#include "BKE_global.h"
 +#include "BKE_mesh.h"
 +#include "BKE_material.h"
 +#include "BKE_texture.h"
 +#include "BKE_utildefines.h"
 +#include "BKE_report.h"
 +#include "BKE_tessmesh.h"
 +#include "BKE_paint.h"
 +
 +#include "IMB_imbuf_types.h"
 +#include "IMB_imbuf.h"
 +
 +#include "RE_render_ext.h"  /* externtex */
 +
 +#include "WM_api.h"
 +#include "WM_types.h"
 +
 +#include "RNA_access.h"
 +#include "RNA_define.h"
 +
 +#include "ED_mesh.h"
 +#include "ED_screen.h"
 +#include "ED_view3d.h"
 +#include "bmesh.h"
 +
 +#include "BIF_gl.h"
 +#include "BIF_glutil.h"
 +
 +#include "UI_resources.h"
 +
 +#include "mesh_intern.h"
 +
 +#include "BLO_sys_types.h" // for intptr_t support
 +
 +/* ****************************** MIRROR **************** */
 +
 +void EDBM_select_mirrored(Object *obedit, BMEditMesh *em)
 +{
 +      if(em->selectmode & SCE_SELECT_VERTEX) {
 +              BMVert *eve, *v1;
 +              BMIter iter;
 +              int i;
 +              
 +              BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
 +                      if (BM_TestHFlag(eve, BM_SELECT) && !BM_TestHFlag(eve, BM_HIDDEN)) {
 +                              v1= editbmesh_get_x_mirror_vert(obedit, em, eve, eve->co, i);
 +                              if(v1) {
 +                                      BM_Select(em->bm, eve, 0);
 +                                      BM_Select(em->bm, v1, 1);
 +                              }
 +                      }
 +                      i++;
 +              }
 +      }
 +}
 +
 +void EDBM_automerge(Scene *scene, Object *obedit, int update)
 +{
 +      BMEditMesh *em;
 +      
 +      if ((scene->toolsettings->automerge) &&
 +          (obedit && obedit->type==OB_MESH) &&
 +          (((Mesh*)obedit->data)->mr==NULL))
 +      {
 +              em = ((Mesh*)obedit->data)->edit_btmesh;
 +              if (!em)
 +                      return;
 +
 +              BMO_CallOpf(em->bm, "automerge verts=%hv dist=%f", BM_SELECT, scene->toolsettings->doublimit);
 +              if (update) {
 +                      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +              }
 +      }
 +}
 +
 +/* ****************************** SELECTION ROUTINES **************** */
 +
 +unsigned int bm_solidoffs=0, bm_wireoffs=0, bm_vertoffs=0;    /* set in drawobject.c ... for colorindices */
 +
 +/* facilities for border select and circle select */
 +static char *selbuf= NULL;
 +
 +/* opengl doesn't support concave... */
 +static void draw_triangulated(short mcords[][2], short tot)
 +{
 +      ListBase lb={NULL, NULL};
 +      DispList *dl;
 +      float *fp;
 +      int a;
 +      
 +      /* make displist */
 +      dl= MEM_callocN(sizeof(DispList), "poly disp");
 +      dl->type= DL_POLY;
 +      dl->parts= 1;
 +      dl->nr= tot;
 +      dl->verts= fp=  MEM_callocN(tot*3*sizeof(float), "poly verts");
 +      BLI_addtail(&lb, dl);
 +      
 +      for(a=0; a<tot; a++, fp+=3) {
 +              fp[0]= (float)mcords[a][0];
 +              fp[1]= (float)mcords[a][1];
 +      }
 +      
 +      /* do the fill */
 +      filldisplist(&lb, &lb, 0);
 +
 +      /* do the draw */
 +      dl= lb.first;   /* filldisplist adds in head of list */
 +      if(dl->type==DL_INDEX3) {
 +              int *index;
 +              
 +              a= dl->parts;
 +              fp= dl->verts;
 +              index= dl->index;
 +              glBegin(GL_TRIANGLES);
 +              while(a--) {
 +                      glVertex3fv(fp+3*index[0]);
 +                      glVertex3fv(fp+3*index[1]);
 +                      glVertex3fv(fp+3*index[2]);
 +                      index+= 3;
 +              }
 +              glEnd();
 +      }
 +      
 +      freedisplist(&lb);
 +}
 +
 +
 +/* reads rect, and builds selection array for quick lookup */
 +/* returns if all is OK */
 +int EDBM_init_backbuf_border(ViewContext *vc, short xmin, short ymin, short xmax, short ymax)
 +{
 +      struct ImBuf *buf;
 +      unsigned int *dr;
 +      int a;
 +      
 +      if(vc->obedit==NULL || vc->v3d->drawtype<OB_SOLID || (vc->v3d->flag & V3D_ZBUF_SELECT)==0) return 0;
 +      
 +      buf= view3d_read_backbuf(vc, xmin, ymin, xmax, ymax);
 +      if(buf==NULL) return 0;
 +      if(bm_vertoffs==0) return 0;
 +
 +      dr = buf->rect;
 +      
 +      /* build selection lookup */
 +      selbuf= MEM_callocN(bm_vertoffs+1, "selbuf");
 +      
 +      a= (xmax-xmin+1)*(ymax-ymin+1);
 +      while(a--) {
 +              if(*dr>0 && *dr<=bm_vertoffs) 
 +                      selbuf[*dr]= 1;
 +              dr++;
 +      }
 +      IMB_freeImBuf(buf);
 +      return 1;
 +}
 +
 +int EDBM_check_backbuf(unsigned int index)
 +{
 +      if(selbuf==NULL) return 1;
 +      if(index>0 && index<=bm_vertoffs)
 +              return selbuf[index];
 +      return 0;
 +}
 +
 +void EDBM_free_backbuf(void)
 +{
 +      if(selbuf) MEM_freeN(selbuf);
 +      selbuf= NULL;
 +}
 +
 +/* mcords is a polygon mask
 +   - grab backbuffer,
 +   - draw with black in backbuffer, 
 +   - grab again and compare
 +   returns 'OK' 
 +*/
 +int EDBM_mask_init_backbuf_border(ViewContext *vc, short mcords[][2], short tot, short xmin, short ymin, short xmax, short ymax)
 +{
 +      unsigned int *dr, *drm;
 +      struct ImBuf *buf, *bufmask;
 +      int a;
 +      
 +      /* method in use for face selecting too */
 +      if(vc->obedit==NULL) {
 +              if(paint_facesel_test(vc->obact));
 +              else return 0;
 +      }
 +      else if(vc->v3d->drawtype<OB_SOLID || (vc->v3d->flag & V3D_ZBUF_SELECT)==0) return 0;
 +
 +      buf= view3d_read_backbuf(vc, xmin, ymin, xmax, ymax);
 +      if(buf==NULL) return 0;
 +      if(bm_vertoffs==0) return 0;
 +
 +      dr = buf->rect;
 +
 +      /* draw the mask */
 +      glDisable(GL_DEPTH_TEST);
 +      
 +      glColor3ub(0, 0, 0);
 +      
 +      /* yah, opengl doesn't do concave... tsk! */
 +      ED_region_pixelspace(vc->ar);
 +      draw_triangulated(mcords, tot); 
 +      
 +      glBegin(GL_LINE_LOOP);  /* for zero sized masks, lines */
 +      for(a=0; a<tot; a++) glVertex2s(mcords[a][0], mcords[a][1]);
 +      glEnd();
 +      
 +      glFinish();     /* to be sure readpixels sees mask */
 +      
 +      /* grab mask */
 +      bufmask= view3d_read_backbuf(vc, xmin, ymin, xmax, ymax);
 +      drm = bufmask->rect;
 +      if(bufmask==NULL) return 0; /* only when mem alloc fails, go crash somewhere else! */
 +      
 +      /* build selection lookup */
 +      selbuf= MEM_callocN(bm_vertoffs+1, "selbuf");
 +      
 +      a= (xmax-xmin+1)*(ymax-ymin+1);
 +      while(a--) {
 +              if(*dr>0 && *dr<=bm_vertoffs && *drm==0) selbuf[*dr]= 1;
 +              dr++; drm++;
 +      }
 +      IMB_freeImBuf(buf);
 +      IMB_freeImBuf(bufmask);
 +      return 1;
 +      
 +}
 +
 +/* circle shaped sample area */
 +int EDBM_init_backbuf_circle(ViewContext *vc, short xs, short ys, short rads)
 +{
 +      struct ImBuf *buf;
 +      unsigned int *dr;
 +      short xmin, ymin, xmax, ymax, xc, yc;
 +      int radsq;
 +      
 +      /* method in use for face selecting too */
 +      if(vc->obedit==NULL) {
 +              if(paint_facesel_test(vc->obact));
 +              else return 0;
 +      }
 +      else if(vc->v3d->drawtype<OB_SOLID || (vc->v3d->flag & V3D_ZBUF_SELECT)==0) return 0;
 +      
 +      xmin= xs-rads; xmax= xs+rads;
 +      ymin= ys-rads; ymax= ys+rads;
 +      buf= view3d_read_backbuf(vc, xmin, ymin, xmax, ymax);
 +      if(bm_vertoffs==0) return 0;
 +      if(buf==NULL) return 0;
 +
 +      dr = buf->rect;
 +      
 +      /* build selection lookup */
 +      selbuf= MEM_callocN(bm_vertoffs+1, "selbuf");
 +      radsq= rads*rads;
 +      for(yc= -rads; yc<=rads; yc++) {
 +              for(xc= -rads; xc<=rads; xc++, dr++) {
 +                      if(xc*xc + yc*yc < radsq) {
 +                              if(*dr>0 && *dr<=bm_vertoffs) selbuf[*dr]= 1;
 +                      }
 +              }
 +      }
 +
 +      IMB_freeImBuf(buf);
 +      return 1;
 +      
 +}
 +
 +static void findnearestvert__doClosest(void *userData, BMVert *eve, int x, int y, int index)
 +{
 +      struct { short mval[2], pass, select, strict; int dist, lastIndex, closestIndex; BMVert *closest; } *data = userData;
 +
 +      if (data->pass==0) {
 +              if (index<=data->lastIndex)
 +                      return;
 +      } else {
 +              if (index>data->lastIndex)
 +                      return;
 +      }
 +
 +      if (data->dist>3) {
 +              int temp = abs(data->mval[0] - x) + abs(data->mval[1]- y);
 +              if (BM_TestHFlag(eve, BM_SELECT) == data->select) {
 +                      if (data->strict == 1)
 +                              return;
 +                      else
 +                              temp += 5;
 +              }
 +
 +              if (temp<data->dist) {
 +                      data->dist = temp;
 +                      data->closest = eve;
 +                      data->closestIndex = index;
 +              }
 +      }
 +}
 +
 +
 +
 +
 +static unsigned int findnearestvert__backbufIndextest(void *handle, unsigned int index)
 +{
 +      BMEditMesh *em= (BMEditMesh *)handle;
 +      BMVert *eve = BMIter_AtIndex(em->bm, BM_VERTS_OF_MESH, NULL, index-1);
 +
 +      if(eve && BM_TestHFlag(eve, BM_SELECT)) return 0;
 +      return 1; 
 +}
 +/**
 + * findnearestvert
 + * 
 + * dist (in/out): minimal distance to the nearest and at the end, actual distance
 + * sel: selection bias
 + *            if SELECT, selected vertice are given a 5 pixel bias to make them farter than unselect verts
 + *            if 0, unselected vertice are given the bias
 + * strict: if 1, the vertice corresponding to the sel parameter are ignored and not just biased 
 + */
 +BMVert *EDBM_findnearestvert(ViewContext *vc, int *dist, short sel, short strict)
 +{
 +      if(vc->v3d->drawtype>OB_WIRE && (vc->v3d->flag & V3D_ZBUF_SELECT)){
 +              int distance;
 +              unsigned int index;
 +              BMVert *eve;
 +              
 +              if(strict) index = view3d_sample_backbuf_rect(vc, vc->mval, 50, bm_wireoffs, 0xFFFFFF, &distance, strict, vc->em, findnearestvert__backbufIndextest); 
 +              else index = view3d_sample_backbuf_rect(vc, vc->mval, 50, bm_wireoffs, 0xFFFFFF, &distance, 0, NULL, NULL); 
 +              
 +              eve = BMIter_AtIndex(vc->em->bm, BM_VERTS_OF_MESH, NULL, index-1);
 +              
 +              if(eve && distance < *dist) {
 +                      *dist = distance;
 +                      return eve;
 +              } else {
 +                      return NULL;
 +              }
 +                      
 +      }
 +      else {
 +              struct { short mval[2], pass, select, strict; int dist, lastIndex, closestIndex; BMVert *closest; } data;
 +              static int lastSelectedIndex=0;
 +              static BMVert *lastSelected=NULL;
 +              
 +              if (lastSelected && BMIter_AtIndex(vc->em->bm, BM_VERTS_OF_MESH, NULL, lastSelectedIndex)!=lastSelected) {
 +                      lastSelectedIndex = 0;
 +                      lastSelected = NULL;
 +              }
 +
 +              data.lastIndex = lastSelectedIndex;
 +              data.mval[0] = vc->mval[0];
 +              data.mval[1] = vc->mval[1];
 +              data.select = sel;
 +              data.dist = *dist;
 +              data.strict = strict;
 +              data.closest = NULL;
 +              data.closestIndex = 0;
 +
 +              data.pass = 0;
 +
 +              ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d);
 +
 +              mesh_foreachScreenVert(vc, findnearestvert__doClosest, &data, 1);
 +
 +              if (data.dist>3) {
 +                      data.pass = 1;
 +                      mesh_foreachScreenVert(vc, findnearestvert__doClosest, &data, 1);
 +              }
 +
 +              *dist = data.dist;
 +              lastSelected = data.closest;
 +              lastSelectedIndex = data.closestIndex;
 +
 +              return data.closest;
 +      }
 +}
 +
 +/* returns labda for closest distance v1 to line-piece v2-v3 */
 +float labda_PdistVL2Dfl( float *v1, float *v2, float *v3) 
 +{
 +      float rc[2], len;
 +      
 +      rc[0]= v3[0]-v2[0];
 +      rc[1]= v3[1]-v2[1];
 +      len= rc[0]*rc[0]+ rc[1]*rc[1];
 +      if(len==0.0f)
 +              return 0.0f;
 +      
 +      return ( rc[0]*(v1[0]-v2[0]) + rc[1]*(v1[1]-v2[1]) )/len;
 +}
 +
 +/* note; uses v3d, so needs active 3d window */
 +static void findnearestedge__doClosest(void *userData, BMEdge *eed, int x0, int y0, int x1, int y1, int UNUSED(index))
 +{
 +      struct { ViewContext vc; float mval[2]; int dist; BMEdge *closest; } *data = userData;
 +      float v1[2], v2[2];
 +      int distance;
 +              
 +      v1[0] = x0;
 +      v1[1] = y0;
 +      v2[0] = x1;
 +      v2[1] = y1;
 +              
 +      distance= dist_to_line_segment_v2(data->mval, v1, v2);
 +              
 +      if(BM_TestHFlag(eed, BM_SELECT)) distance+=5;
 +      if(distance < data->dist) {
 +              if(data->vc.rv3d->rflag & RV3D_CLIPPING) {
 +                      float labda= labda_PdistVL2Dfl(data->mval, v1, v2);
 +                      float vec[3];
 +
 +                      vec[0]= eed->v1->co[0] + labda*(eed->v2->co[0] - eed->v1->co[0]);
 +                      vec[1]= eed->v1->co[1] + labda*(eed->v2->co[1] - eed->v1->co[1]);
 +                      vec[2]= eed->v1->co[2] + labda*(eed->v2->co[2] - eed->v1->co[2]);
 +                      mul_m4_v3(data->vc.obedit->obmat, vec);
 +
 +                      if(view3d_test_clipping(data->vc.rv3d, vec, 1)==0) {
 +                              data->dist = distance;
 +                              data->closest = eed;
 +                      }
 +              }
 +              else {
 +                      data->dist = distance;
 +                      data->closest = eed;
 +              }
 +      }
 +}
 +BMEdge *EDBM_findnearestedge(ViewContext *vc, int *dist)
 +{
 +
 +      if(vc->v3d->drawtype>OB_WIRE && (vc->v3d->flag & V3D_ZBUF_SELECT)) {
 +              int distance;
 +              unsigned int index;
 +              BMEdge *eed;
 +              
 +              view3d_validate_backbuf(vc);
 +              
 +              index = view3d_sample_backbuf_rect(vc, vc->mval, 50, bm_solidoffs, bm_wireoffs, &distance,0, NULL, NULL);
 +              eed = BMIter_AtIndex(vc->em->bm, BM_EDGES_OF_MESH, NULL, index-1);
 +              
 +              if (eed && distance<*dist) {
 +                      *dist = distance;
 +                      return eed;
 +              } else {
 +                      return NULL;
 +              }
 +      }
 +      else {
 +              struct { ViewContext vc; float mval[2]; int dist; BMEdge *closest; } data;
 +
 +              data.vc= *vc;
 +              data.mval[0] = vc->mval[0];
 +              data.mval[1] = vc->mval[1];
 +              data.dist = *dist;
 +              data.closest = NULL;
 +              ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d);
 +
 +              mesh_foreachScreenEdge(vc, findnearestedge__doClosest, &data, 2);
 +
 +              *dist = data.dist;
 +              return data.closest;
 +      }
 +}
 +
 +static void findnearestface__getDistance(void *userData, BMFace *efa, int x, int y, int UNUSED(index))
 +{
 +      struct { short mval[2]; int dist; BMFace *toFace; } *data = userData;
 +
 +      if (efa==data->toFace) {
 +              int temp = abs(data->mval[0]-x) + abs(data->mval[1]-y);
 +
 +              if (temp<data->dist)
 +                      data->dist = temp;
 +      }
 +}
 +static void findnearestface__doClosest(void *userData, BMFace *efa, int x, int y, int index)
 +{
 +      struct { short mval[2], pass; int dist, lastIndex, closestIndex; BMFace *closest; } *data = userData;
 +
 +      if (data->pass==0) {
 +              if (index<=data->lastIndex)
 +                      return;
 +      } else {
 +              if (index>data->lastIndex)
 +                      return;
 +      }
 +
 +      if (data->dist>3) {
 +              int temp = abs(data->mval[0]-x) + abs(data->mval[1]-y);
 +
 +              if (temp<data->dist) {
 +                      data->dist = temp;
 +                      data->closest = efa;
 +                      data->closestIndex = index;
 +              }
 +      }
 +}
 +
 +BMFace *EDBM_findnearestface(ViewContext *vc, int *dist)
 +{
 +
 +      if(vc->v3d->drawtype>OB_WIRE && (vc->v3d->flag & V3D_ZBUF_SELECT)) {
 +              unsigned int index;
 +              BMFace *efa;
 +
 +              view3d_validate_backbuf(vc);
 +
 +              index = view3d_sample_backbuf(vc, vc->mval[0], vc->mval[1]);
 +              efa = BMIter_AtIndex(vc->em->bm, BM_FACES_OF_MESH, NULL, index-1);
 +              
 +              if (efa) {
 +                      struct { short mval[2]; int dist; BMFace *toFace; } data;
 +
 +                      data.mval[0] = vc->mval[0];
 +                      data.mval[1] = vc->mval[1];
 +                      data.dist = 0x7FFF;             /* largest short */
 +                      data.toFace = efa;
 +
 +                      mesh_foreachScreenFace(vc, findnearestface__getDistance, &data);
 +
 +                      if(vc->em->selectmode == SCE_SELECT_FACE || data.dist<*dist) {  /* only faces, no dist check */
 +                              *dist= data.dist;
 +                              return efa;
 +                      }
 +              }
 +              
 +              return NULL;
 +      }
 +      else {
 +              struct { short mval[2], pass; int dist, lastIndex, closestIndex; BMFace *closest; } data;
 +              static int lastSelectedIndex=0;
 +              static BMFace *lastSelected=NULL;
 +
 +              if (lastSelected && BMIter_AtIndex(vc->em->bm, BM_FACES_OF_MESH, NULL, lastSelectedIndex)!=lastSelected) {
 +                      lastSelectedIndex = 0;
 +                      lastSelected = NULL;
 +              }
 +
 +              data.lastIndex = lastSelectedIndex;
 +              data.mval[0] = vc->mval[0];
 +              data.mval[1] = vc->mval[1];
 +              data.dist = *dist;
 +              data.closest = NULL;
 +              data.closestIndex = 0;
 +              ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d);
 +
 +              data.pass = 0;
 +              mesh_foreachScreenFace(vc, findnearestface__doClosest, &data);
 +
 +              if (data.dist>3) {
 +                      data.pass = 1;
 +                      ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d);
 +                      mesh_foreachScreenFace(vc, findnearestface__doClosest, &data);
 +              }
 +
 +              *dist = data.dist;
 +              lastSelected = data.closest;
 +              lastSelectedIndex = data.closestIndex;
 +
 +              return data.closest;
 +      }
 +}
 +
 +/* best distance based on screen coords. 
 +   use em->selectmode to define how to use 
 +   selected vertices and edges get disadvantage
 +   return 1 if found one
 +*/
 +static int unified_findnearest(ViewContext *vc, BMVert **eve, BMEdge **eed, BMFace **efa) 
 +{
 +      BMEditMesh *em= vc->em;
 +      int dist= 75;
 +      
 +      *eve= NULL;
 +      *eed= NULL;
 +      *efa= NULL;
 +      
 +      /* no afterqueue (yet), so we check it now, otherwise the em_xxxofs indices are bad */
 +      view3d_validate_backbuf(vc);
 +      
 +      if(em->selectmode & SCE_SELECT_VERTEX)
 +              *eve= EDBM_findnearestvert(vc, &dist, BM_SELECT, 0);
 +      if(em->selectmode & SCE_SELECT_FACE)
 +              *efa= EDBM_findnearestface(vc, &dist);
 +
 +      dist-= 20;      /* since edges select lines, we give dots advantage of 20 pix */
 +      if(em->selectmode & SCE_SELECT_EDGE)
 +              *eed= EDBM_findnearestedge(vc, &dist);
 +
 +      /* return only one of 3 pointers, for frontbuffer redraws */
 +      if(*eed) {
 +              *efa= NULL; *eve= NULL;
 +      }
 +      else if(*efa) {
 +              *eve= NULL;
 +      }
 +      
 +      return (*eve || *eed || *efa);
 +}
 +
 +/* ****************  SIMILAR "group" SELECTS. FACE, EDGE AND VERTEX ************** */
 +
 +static EnumPropertyItem prop_similar_types[] = {
 +      {SIMVERT_NORMAL, "NORMAL", 0, "Normal", ""},
 +      {SIMVERT_FACE, "FACE", 0, "Amount of Adjacent Faces", ""},
 +      {SIMVERT_VGROUP, "VGROUP", 0, "Vertex Groups", ""},
 +
 +      {SIMEDGE_LENGTH, "LENGTH", 0, "Length", ""},
 +      {SIMEDGE_DIR, "DIR", 0, "Direction", ""},
 +      {SIMEDGE_FACE, "FACE", 0, "Amount of Faces Around an Edge", ""},
 +      {SIMEDGE_FACE_ANGLE, "FACE_ANGLE", 0, "Face Angles", ""},
 +      {SIMEDGE_CREASE, "CREASE", 0, "Crease", ""},
 +      {SIMEDGE_SEAM, "SEAM", 0, "Seam", ""},
 +      {SIMEDGE_SHARP, "SHARP", 0, "Sharpness", ""},
 +
 +      {SIMFACE_MATERIAL, "MATERIAL", 0, "Material", ""},
 +      {SIMFACE_IMAGE, "IMAGE", 0, "Image", ""},
 +      {SIMFACE_AREA, "AREA", 0, "Area", ""},
 +      {SIMFACE_PERIMETER, "PERIMETER", 0, "Perimeter", ""},
 +      {SIMFACE_NORMAL, "NORMAL", 0, "Normal", ""},
 +      {SIMFACE_COPLANAR, "COPLANAR", 0, "Co-planar", ""},
 +
 +      {0, NULL, 0, NULL, NULL}
 +};
 +
 +/* selects new faces/edges/verts based on the existing selection */
 +
 +static int similar_face_select_exec(bContext *C, wmOperator *op)
 +{
 +      Object *ob = CTX_data_edit_object(C);
 +      BMEditMesh *em = ((Mesh*)ob->data)->edit_btmesh;
 +      BMOperator bmop;
 +
 +      /* get the type from RNA */
 +      int type = RNA_enum_get(op->ptr, "type");
 +
 +      float thresh = CTX_data_tool_settings(C)->select_thresh;
 +
 +      /* initialize the bmop using EDBM api, which does various ui error reporting and other stuff */
 +      EDBM_InitOpf(em, &bmop, op, "similarfaces faces=%hf type=%d thresh=%f", BM_SELECT, type, thresh);
 +
 +      /* execute the operator */
 +      BMO_Exec_Op(em->bm, &bmop);
 +
 +      /* clear the existing selection */
 +      EDBM_clear_flag_all(em, BM_SELECT);
 +
 +      /* select the output */
 +      BMO_HeaderFlag_Buffer(em->bm, &bmop, "faceout", BM_SELECT, BM_ALL);
 +
 +      /* finish the operator */
 +      if( !EDBM_FinishOp(em, &bmop, op, 1) )
 +              return OPERATOR_CANCELLED;
 +
 +      /* dependencies graph and notification stuff */
 +      DAG_id_tag_update(ob->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_SELECT, ob->data);
 +
 +      /* we succeeded */
 +      return OPERATOR_FINISHED;
 +}     
 +
 +/* ***************************************************** */
 +
 +/* EDGE GROUP */
 +
 +/* wrap the above function but do selection flushing edge to face */
 +static int similar_edge_select_exec(bContext *C, wmOperator *op)
 +{
 +      Object *ob = CTX_data_edit_object(C);
 +      BMEditMesh *em = ((Mesh*)ob->data)->edit_btmesh;
 +      BMOperator bmop;
 +
 +      /* get the type from RNA */
 +      int type = RNA_enum_get(op->ptr, "type");
 +
 +      float thresh = CTX_data_tool_settings(C)->select_thresh;
 +
 +      /* initialize the bmop using EDBM api, which does various ui error reporting and other stuff */
 +      EDBM_InitOpf(em, &bmop, op, "similaredges edges=%he type=%d thresh=%f", BM_SELECT, type, thresh);
 +
 +      /* execute the operator */
 +      BMO_Exec_Op(em->bm, &bmop);
 +
 +      /* clear the existing selection */
 +      EDBM_clear_flag_all(em, BM_SELECT);
 +
 +      /* select the output */
 +      BMO_HeaderFlag_Buffer(em->bm, &bmop, "edgeout", BM_SELECT, BM_ALL);
 +      EDBM_selectmode_flush(em);
 +
 +      /* finish the operator */
 +      if( !EDBM_FinishOp(em, &bmop, op, 1) )
 +              return OPERATOR_CANCELLED;
 +
 +      /* dependencies graph and notification stuff */
 +      DAG_id_tag_update(ob->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_SELECT, ob->data);
 +
 +      /* we succeeded */
 +      return OPERATOR_FINISHED;
 +}
 +
 +/* ********************************* */
 +
 +/*
 +VERT GROUP
 + mode 1: same normal
 + mode 2: same number of face users
 + mode 3: same vertex groups
 +*/
 +
 +
 +static int similar_vert_select_exec(bContext *C, wmOperator *op)
 +{
 +      Object *ob = CTX_data_edit_object(C);
 +      BMEditMesh *em = ((Mesh*)ob->data)->edit_btmesh;
 +      BMOperator bmop;
 +      /* get the type from RNA */
 +      int type = RNA_enum_get(op->ptr, "type");
 +      float thresh = CTX_data_tool_settings(C)->select_thresh;
 +
 +      /* initialize the bmop using EDBM api, which does various ui error reporting and other stuff */
 +      EDBM_InitOpf(em, &bmop, op, "similarverts verts=%hv type=%d thresh=%f", BM_SELECT, type, thresh);
 +
 +      /* execute the operator */
 +      BMO_Exec_Op(em->bm, &bmop);
 +
 +      /* clear the existing selection */
 +      EDBM_clear_flag_all(em, BM_SELECT);
 +
 +      /* select the output */
 +      BMO_HeaderFlag_Buffer(em->bm, &bmop, "vertout", BM_SELECT, BM_ALL);
 +
 +      /* finish the operator */
 +      if( !EDBM_FinishOp(em, &bmop, op, 1) )
 +              return OPERATOR_CANCELLED;
 +
 +      EDBM_selectmode_flush(em);
 +
 +      /* dependencies graph and notification stuff */
 +      DAG_id_tag_update(ob->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_SELECT, ob->data);
 +
 +      /* we succeeded */
 +      return OPERATOR_FINISHED;
 +}
 +
 +static int select_similar_exec(bContext *C, wmOperator *op)
 +{
 +      int type= RNA_enum_get(op->ptr, "type");
 +
 +      if(type < 100)
 +              return similar_vert_select_exec(C, op);
 +      else if(type < 200)
 +              return similar_edge_select_exec(C, op);
 +      else
 +              return similar_face_select_exec(C, op);
 +}
 +
 +static EnumPropertyItem *select_similar_type_itemf(bContext *C, PointerRNA *UNUSED(ptr), int *free)
 +{
 +      Object *obedit = CTX_data_edit_object(C);
 +      EnumPropertyItem *item= NULL;
 +      int a, totitem= 0;
 +      
 +      if(obedit && obedit->type == OB_MESH) {
 +              BMEditMesh *em= ((Mesh*)obedit->data)->edit_btmesh; 
 +
 +              if(em->selectmode & SCE_SELECT_VERTEX) {
 +                      for (a=SIMVERT_NORMAL; a<SIMEDGE_LENGTH; a++) {
 +                              RNA_enum_items_add_value(&item, &totitem, prop_similar_types, a);
 +                      }
 +              } else if(em->selectmode & SCE_SELECT_EDGE) {
 +                      for (a=SIMEDGE_LENGTH; a<SIMFACE_MATERIAL; a++) {
 +                              RNA_enum_items_add_value(&item, &totitem, prop_similar_types, a);
 +                      }
 +              } else if(em->selectmode & SCE_SELECT_FACE) {
 +                      for (a=SIMFACE_MATERIAL; a<=SIMFACE_COPLANAR; a++) {
 +                              RNA_enum_items_add_value(&item, &totitem, prop_similar_types, a);
 +                      }
 +              }
 +              RNA_enum_item_end(&item, &totitem);
 +
 +              *free= 1;
 +
 +              return item;
 +      }
 +      
 +      return NULL;
 +}
 +
 +void MESH_OT_select_similar(wmOperatorType *ot)
 +{
 +      PropertyRNA *prop;
 +
 +      /* identifiers */
 +      ot->name= "Select Similar";
 +      ot->idname= "MESH_OT_select_similar";
 +      
 +      /* api callbacks */
 +      ot->invoke= WM_menu_invoke;
 +      ot->exec= select_similar_exec;
 +      ot->poll= ED_operator_editmesh;
 +      ot->description= "Select similar vertices, edges or faces by property types.";
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +      
 +      /* properties */
 +      prop= ot->prop= RNA_def_enum(ot->srna, "type", prop_similar_types, SIMVERT_NORMAL, "Type", "");
 +      RNA_def_enum_funcs(prop, select_similar_type_itemf);
 +}
 +
 +/* ***************************************************** */
 +
 +/* ****************  LOOP SELECTS *************** */
 +/*faceloop_select, edgeloop_select, and edgering_select, are left
 +  here for reference purposes temporarily, but have all been replaced
 +  by uses of walker_select.*/
 +
 +static void walker_select(BMEditMesh *em, int walkercode, void *start, int select)
 +{
 +      BMesh *bm = em->bm;
 +      BMHeader *h;
 +      BMWalker walker;
 +
 +      BMW_Init(&walker, bm, walkercode, 0, 0);
 +      h = BMW_Begin(&walker, start);
 +      for (; h; h=BMW_Step(&walker)) {
 +              BM_Select(bm, h, select);
 +      }
 +      BMW_End(&walker);
 +}
 +
 +static int loop_multiselect(bContext *UNUSED(C), wmOperator *UNUSED(op))
 +{
 +#if 0 //BMESH_TODO
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= EM_GetBMEditMesh(((Mesh *)obedit->data));
 +      BMEdge *eed;
 +      BMEdge **edarray;
 +      int edindex, edfirstcount;
 +      int looptype= RNA_boolean_get(op->ptr, "ring");
 +      
 +      /* sets em->totedgesel */
 +      EM_nedges_selected(em);
 +      
 +      edarray = MEM_mallocN(sizeof(BMEdge*)*em->totedgesel,"edge array");
 +      edindex = 0;
 +      edfirstcount = em->totedgesel;
 +      
 +      for(eed=em->edges.first; eed; eed=eed->next){
 +              if(eed->f&SELECT){
 +                      edarray[edindex] = eed;
 +                      edindex += 1;
 +              }
 +      }
 +      
 +      if(looptype){
 +              for(edindex = 0; edindex < edfirstcount; edindex +=1){
 +                      eed = edarray[edindex];
 +                      edgering_select(em, eed,SELECT);
 +              }
 +              EM_selectmode_flush(em);
 +      }
 +      else{
 +              for(edindex = 0; edindex < edfirstcount; edindex +=1){
 +                      eed = edarray[edindex];
 +                      edgeloop_select(em, eed,SELECT);
 +              }
 +              EM_selectmode_flush(em);
 +      }
 +      MEM_freeN(edarray);
 +//    if (EM_texFaceCheck())
 +      
 +      WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit);
 +
 +      EM_EndBMEditMesh(obedit->data, em);
 +#endif
 +      return OPERATOR_FINISHED;       
 +}
 +
 +void MESH_OT_loop_multi_select(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Multi Select Loops";
 +      ot->idname= "MESH_OT_loop_multi_select";
 +      
 +      /* api callbacks */
 +      ot->exec= loop_multiselect;
 +      ot->poll= ED_operator_editmesh;
 +      ot->description= "Select a loop of connected edges by connection type.";
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +      
 +      /* properties */
 +      RNA_def_boolean(ot->srna, "ring", 0, "Ring", "");
 +}
 +
 +              
 +/* ***************** MAIN MOUSE SELECTION ************** */
 +
 +
 +/* ***************** loop select (non modal) ************** */
 +
 +static void mouse_mesh_loop(bContext *C, short mval[2], short extend, short ring)
 +{
 +      ViewContext vc;
 +      BMEditMesh *em;
 +      BMEdge *eed;
 +      int select= 1;
 +      int dist= 50;
 +      
 +      em_setup_viewcontext(C, &vc);
 +      vc.mval[0]= mval[0];
 +      vc.mval[1]= mval[1];
 +      em= vc.em;
 +      
 +      /* no afterqueue (yet), so we check it now, otherwise the bm_xxxofs indices are bad */
 +      view3d_validate_backbuf(&vc);
 +
 +      eed= EDBM_findnearestedge(&vc, &dist);
 +      if(eed) {
 +              if(extend==0) EDBM_clear_flag_all(em, BM_SELECT);
 +      
 +              if(BM_TestHFlag(em, BM_SELECT)==0) select=1;
 +              else if(extend) select=0;
 +
 +              if(em->selectmode & SCE_SELECT_FACE) {
 +                      walker_select(em, BMW_FACELOOP, eed, select);
 +              }
 +              else if(em->selectmode & SCE_SELECT_EDGE) {
 +                      if(ring)
 +                              walker_select(em, BMW_EDGERING, eed, select);
 +                      else
 +                              walker_select(em, BMW_LOOP, eed, select);
 +              }
 +              else if(em->selectmode & SCE_SELECT_VERTEX) {
 +                      if(ring)
 +                              walker_select(em, BMW_EDGERING, eed, select);
 +                      else 
 +                              walker_select(em, BMW_LOOP, eed, select);
 +              }
 +
 +              EDBM_selectmode_flush(em);
 +//                    if (EM_texFaceCheck())
 +              
 +              /* sets as active, useful for other tools */
 +              if(select && em->selectmode & SCE_SELECT_EDGE) {
 +                      EDBM_store_selection(em, eed);
 +              }
 +
 +              WM_event_add_notifier(C, NC_GEOM|ND_SELECT, vc.obedit);
 +      }
 +}
 +
 +static int mesh_select_loop_invoke(bContext *C, wmOperator *op, wmEvent *event)
 +{
 +      
 +      view3d_operator_needs_opengl(C);
 +      
 +      mouse_mesh_loop(C, event->mval, RNA_boolean_get(op->ptr, "extend"),
 +                                      RNA_boolean_get(op->ptr, "ring"));
 +      
 +      /* cannot do tweaks for as long this keymap is after transform map */
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_loop_select(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Loop Select";
 +      ot->idname= "MESH_OT_loop_select";
 +      
 +      /* api callbacks */
 +      ot->invoke= mesh_select_loop_invoke;
 +      ot->poll= ED_operator_editmesh;
 +      ot->description= "Select a loop of connected edges.";
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +      
 +      /* properties */
 +      RNA_def_boolean(ot->srna, "extend", 0, "Extend Select", "");
 +      RNA_def_boolean(ot->srna, "ring", 0, "Select Ring", "");
 +}
 +
 +/* ******************* mesh shortest path select, uses prev-selected edge ****************** */
 +
 +/* since you want to create paths with multiple selects, it doesn't have extend option */
 +static void mouse_mesh_shortest_path(bContext *UNUSED(C), short UNUSED(mval[2]))
 +{
 +#if 0 //BMESH_TODO
 +      ViewContext vc;
 +      BMEditMesh *em;
 +      BMEdge *eed;
 +      int dist= 50;
 +      
 +      em_setup_viewcontext(C, &vc);
 +      vc.mval[0]= mval[0];
 +      vc.mval[1]= mval[1];
 +      em= vc.em;
 +      
 +      eed= findnearestedge(&vc, &dist);
 +      if(eed) {
 +              Mesh *me= vc.obedit->data;
 +              int path = 0;
 +              
 +              if (em->bm->selected.last) {
 +                      EditSelection *ese = em->bm->selected.last;
 +                      
 +                      if(ese && ese->type == BMEdge) {
 +                              BMEdge *eed_act;
 +                              eed_act = (BMEdge*)ese->data;
 +                              if (eed_act != eed) {
 +                                      if (edgetag_shortest_path(vc.scene, em, eed_act, eed)) {
 +                                              EM_remove_selection(em, eed_act, BMEdge);
 +                                              path = 1;
 +                                      }
 +                              }
 +                      }
 +              }
 +              if (path==0) {
 +                      int act = (edgetag_context_check(vc.scene, eed)==0);
 +                      edgetag_context_set(em, vc.scene, eed, act); /* switch the edge option */
 +              }
 +              
 +              EM_selectmode_flush(em);
 +
 +              /* even if this is selected it may not be in the selection list */
 +              if(edgetag_context_check(vc.scene, eed)==0)
 +                      EDBM_remove_selection(em, eed);
 +              else
 +                      EDBM_store_selection(em, eed);
 +      
 +              /* force drawmode for mesh */
 +              switch (CTX_data_tool_settings(C)->edge_mode) {
 +                      
 +                      case EDGE_MODE_TAG_SEAM:
 +                              me->drawflag |= ME_DRAWSEAMS;
 +                              break;
 +                      case EDGE_MODE_TAG_SHARP:
 +                              me->drawflag |= ME_DRAWSHARP;
 +                              break;
 +                      case EDGE_MODE_TAG_CREASE:      
 +                              me->drawflag |= ME_DRAWCREASES;
 +                              break;
 +                      case EDGE_MODE_TAG_BEVEL:
 +                              me->drawflag |= ME_DRAWBWEIGHTS;
 +                              break;
 +              }
 +              
 +              DAG_id_tag_update(ob->data, OB_RECALC_DATA);
 +              WM_event_add_notifier(C, NC_GEOM|ND_SELECT, ob->data);
 +      }
 +#endif
 +}
 +
 +
 +static int mesh_shortest_path_select_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *event)
 +{
 +      
 +      view3d_operator_needs_opengl(C);
 +
 +      mouse_mesh_shortest_path(C, event->mval);
 +      
 +      return OPERATOR_FINISHED;
 +}
 +      
 +void MESH_OT_select_shortest_path(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Shortest Path Select";
 +      ot->idname= "MESH_OT_select_shortest_path";
 +      
 +      /* api callbacks */
 +      ot->invoke= mesh_shortest_path_select_invoke;
 +      ot->poll= ED_operator_editmesh;
 +      ot->description= "Select shortest path between two selections.";
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +      
 +      /* properties */
 +      RNA_def_boolean(ot->srna, "extend", 0, "Extend Select", "");
 +}
 +
 +/* ************************************************** */
 +/* here actual select happens */
 +/* gets called via generic mouse select operator */
-       BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
-       int nth = RNA_int_get(op->ptr, "nth");
++int mouse_mesh(bContext *C, const short mval[2], short extend)
 +{
 +      ViewContext vc;
 +      BMVert *eve = NULL;
 +      BMEdge *eed = NULL;
 +      BMFace *efa = NULL;
 +      
 +      /* setup view context for argument to callbacks */
 +      em_setup_viewcontext(C, &vc);
 +      vc.mval[0]= mval[0];
 +      vc.mval[1]= mval[1];
 +      
 +      if(unified_findnearest(&vc, &eve, &eed, &efa)) {
 +              
 +              if(extend==0) EDBM_clear_flag_all(vc.em, BM_SELECT);
 +              
 +              if(efa) {
 +                      /* set the last selected face */
 +                      EDBM_set_actFace(vc.em, efa);
 +                      
 +                      if(!BM_TestHFlag(efa, BM_SELECT)) {
 +                              EDBM_store_selection(vc.em, efa);
 +                              BM_Select(vc.em->bm, efa, 1);
 +                      }
 +                      else if(extend) {
 +                              EDBM_remove_selection(vc.em, efa);
 +                              BM_Select(vc.em->bm, efa, 0);
 +                      }
 +              }
 +              else if(eed) {
 +                      if(!BM_TestHFlag(eed, BM_SELECT)) {
 +                              EDBM_store_selection(vc.em, eed);
 +                              BM_Select(vc.em->bm, eed, 1);
 +                      }
 +                      else if(extend) {
 +                              EDBM_remove_selection(vc.em, eed);
 +                              BM_Select(vc.em->bm, eed, 0);
 +                      }
 +              }
 +              else if(eve) {
 +                      if(!BM_TestHFlag(eve, BM_SELECT)) {
 +                              EDBM_store_selection(vc.em, eve);
 +                              BM_Select(vc.em->bm, eve, 1);
 +                      }
 +                      else if(extend){ 
 +                              EDBM_remove_selection(vc.em, eve);
 +                              BM_Select(vc.em->bm, eve, 0);
 +                      }
 +              }
 +              
 +              EDBM_selectmode_flush(vc.em);
 +                
 +//            if (EM_texFaceCheck()) {
 +
 +              if (efa && efa->mat_nr != vc.obedit->actcol-1) {
 +                      vc.obedit->actcol= efa->mat_nr+1;
 +                      vc.em->mat_nr= efa->mat_nr;
 +//                    BIF_preview_changed(ID_MA);
 +              }
 +
 +              WM_event_add_notifier(C, NC_GEOM|ND_SELECT, vc.obedit);
 +              return 1;
 +      }
 +
 +      return 0;
 +}
 +
 +static void EDBM_strip_selections(BMEditMesh *em)
 +{
 +      BMEditSelection *ese, *nextese;
 +
 +      if(!(em->selectmode & SCE_SELECT_VERTEX)){
 +              ese = em->bm->selected.first;
 +              while(ese){
 +                      nextese = ese->next; 
 +                      if(ese->type == BM_VERT) BLI_freelinkN(&(em->bm->selected),ese);
 +                      ese = nextese;
 +              }
 +      }
 +      if(!(em->selectmode & SCE_SELECT_EDGE)){
 +              ese=em->bm->selected.first;
 +              while(ese){
 +                      nextese = ese->next;
 +                      if(ese->type == BM_EDGE) BLI_freelinkN(&(em->bm->selected), ese);
 +                      ese = nextese;
 +              }
 +      }
 +      if(!(em->selectmode & SCE_SELECT_FACE)){
 +              ese=em->bm->selected.first;
 +              while(ese){
 +                      nextese = ese->next;
 +                      if(ese->type == BM_FACE) BLI_freelinkN(&(em->bm->selected), ese);
 +                      ese = nextese;
 +              }
 +      }
 +}
 +
 +/* when switching select mode, makes sure selection is consistant for editing */
 +/* also for paranoia checks to make sure edge or face mode works */
 +void EDBM_selectmode_set(BMEditMesh *em)
 +{
 +      BMVert *eve;
 +      BMEdge *eed;
 +      BMFace *efa;
 +      BMIter iter;
 +      
 +      em->bm->selectmode = em->selectmode;
 +
 +      EDBM_strip_selections(em); /*strip BMEditSelections from em->selected that are not relevant to new mode*/
 +      
 +      if(em->selectmode & SCE_SELECT_VERTEX) {
 +              /*BMIter iter;
 +              
 +              eed = BMIter_New(&iter, em->bm, BM_EDGES_OF_MESH, NULL);
 +              for ( ; eed; eed=BMIter_Step(&iter)) BM_Select(em->bm, eed, 0);
 +              
 +              efa = BMIter_New(&iter, em->bm, BM_FACES_OF_MESH, NULL);
 +              for ( ; efa; efa=BMIter_Step(&iter)) BM_Select(em->bm, efa, 0);*/
 +
 +              EDBM_selectmode_flush(em);
 +      }
 +      else if(em->selectmode & SCE_SELECT_EDGE) {
 +              /* deselect vertices, and select again based on edge select */
 +              eve = BMIter_New(&iter, em->bm, BM_VERTS_OF_MESH, NULL);
 +              for ( ; eve; eve=BMIter_Step(&iter)) BM_Select(em->bm, eve, 0);
 +              
 +              eed = BMIter_New(&iter, em->bm, BM_EDGES_OF_MESH, NULL);
 +              for ( ; eed; eed=BMIter_Step(&iter)) {
 +                      if (BM_TestHFlag(eed, BM_SELECT))
 +                              BM_Select(em->bm, eed, 1);
 +              }
 +              
 +              /* selects faces based on edge status */
 +              EDBM_selectmode_flush(em);
 +      }
 +      else if(em->selectmode & SCE_SELECT_FACE) {
 +              /* deselect eges, and select again based on face select */
 +              eed = BMIter_New(&iter, em->bm, BM_EDGES_OF_MESH, NULL);
 +              for ( ; eed; eed=BMIter_Step(&iter)) BM_Select(em->bm, eed, 0);
 +              
 +              efa = BMIter_New(&iter, em->bm, BM_FACES_OF_MESH, NULL);
 +              for ( ; efa; efa=BMIter_Step(&iter)) {
 +                      if (BM_TestHFlag(efa, BM_SELECT))
 +                              BM_Select(em->bm, efa, 1);
 +              }
 +      }
 +}
 +
 +void EDBM_convertsel(BMEditMesh *em, short oldmode, short selectmode)
 +{
 +      BMEdge *eed;
 +      BMFace *efa;
 +      BMIter iter;
 +
 +      /*have to find out what the selectionmode was previously*/
 +      if(oldmode == SCE_SELECT_VERTEX) {
 +              if(selectmode == SCE_SELECT_EDGE) {
 +                      /*select all edges associated with every selected vertex*/
 +                      eed = BMIter_New(&iter, em->bm, BM_EDGES_OF_MESH, NULL);
 +                      for ( ; eed; eed=BMIter_Step(&iter)) {
 +                              if(BM_TestHFlag(eed->v1, BM_SELECT)) BM_Select(em->bm, eed, 1);
 +                              else if(BM_TestHFlag(eed->v2, BM_SELECT)) BM_Select(em->bm, eed, 1);
 +                      }
 +              }               
 +              else if(selectmode == SCE_SELECT_FACE) {
 +                      BMIter liter;
 +                      BMLoop *l;
 +
 +                      /*select all faces associated with every selected vertex*/
 +                      efa = BMIter_New(&iter, em->bm, BM_FACES_OF_MESH, NULL);
 +                      for ( ; efa; efa=BMIter_Step(&iter)) {
 +                              l = BMIter_New(&liter, em->bm, BM_LOOPS_OF_FACE, efa);
 +                              for (; l; l=BMIter_Step(&liter)) {
 +                                      if (BM_TestHFlag(l->v, BM_SELECT)) {
 +                                              BM_Select(em->bm, efa, 1);
 +                                              break;
 +                                      }
 +                              }
 +                      }
 +              }
 +      }
 +      
 +      if(oldmode == SCE_SELECT_EDGE){
 +              if(selectmode == SCE_SELECT_FACE) {
 +                      BMIter liter;
 +                      BMLoop *l;
 +
 +                      /*select all faces associated with every selected vertex*/
 +                      efa = BMIter_New(&iter, em->bm, BM_FACES_OF_MESH, NULL);
 +                      for ( ; efa; efa=BMIter_Step(&iter)) {
 +                              l = BMIter_New(&liter, em->bm, BM_LOOPS_OF_FACE, efa);
 +                              for (; l; l=BMIter_Step(&liter)) {
 +                                      if (BM_TestHFlag(l->v, BM_SELECT)) {
 +                                              BM_Select(em->bm, efa, 1);
 +                                              break;
 +                                      }
 +                              }
 +                      }
 +              }
 +      }
 +}
 +
 +
 +void EDBM_select_swap(BMEditMesh *em) /* exported for UV */
 +{
 +      BMIter iter;
 +      BMVert *eve;
 +      BMEdge *eed;
 +      BMFace *efa;
 +      
 +      if(em->bm->selectmode & SCE_SELECT_VERTEX) {
 +              BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
 +                      if (BM_TestHFlag(eve, BM_HIDDEN))
 +                              continue;
 +                      BM_Select(em->bm, eve, !BM_TestHFlag(eve, BM_SELECT));
 +              }
 +      }
 +      else if(em->selectmode & SCE_SELECT_EDGE) {
 +              BM_ITER(eed, &iter, em->bm, BM_EDGES_OF_MESH, NULL) {
 +                      if (BM_TestHFlag(eed, BM_HIDDEN))
 +                              continue;
 +                      BM_Select(em->bm, eed, !BM_TestHFlag(eed, BM_SELECT));
 +              }
 +      }
 +      else {
 +              BM_ITER(efa, &iter, em->bm, BM_FACES_OF_MESH, NULL) {
 +                      if (BM_TestHFlag(efa, BM_HIDDEN))
 +                              continue;
 +                      BM_Select(em->bm, efa, !BM_TestHFlag(efa, BM_SELECT));
 +              }
 +
 +      }
 +//    if (EM_texFaceCheck())
 +}
 +
 +static int select_inverse_mesh_exec(bContext *C, wmOperator *UNUSED(op))
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +      
 +      EDBM_select_swap(em);
 +      
 +      WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit);
 +
 +      return OPERATOR_FINISHED;       
 +}
 +
 +void MESH_OT_select_inverse(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Select Inverse";
 +      ot->idname= "MESH_OT_select_inverse";
 +      ot->description= "Select inverse of (un)selected vertices, edges or faces.";
 +      
 +      /* api callbacks */
 +      ot->exec= select_inverse_mesh_exec;
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +}
 +
 +
 +static int select_linked_pick_invoke(bContext *C, wmOperator *op, wmEvent *event)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      ViewContext vc;
 +      BMWalker walker;
 +      BMEditMesh *em;
 +      BMVert *eve;
 +      BMEdge *e, *eed;
 +      BMFace *efa;
 +      int sel= !RNA_boolean_get(op->ptr, "deselect");
 +      
 +      /* unified_finednearest needs ogl */
 +      view3d_operator_needs_opengl(C);
 +      
 +      /* setup view context for argument to callbacks */
 +      em_setup_viewcontext(C, &vc);
 +      em = vc.em;
 +
 +      if(vc.em->bm->totedge==0)
 +              return OPERATOR_CANCELLED;
 +      
 +      vc.mval[0]= event->mval[0];
 +      vc.mval[1]= event->mval[1];
 +      
 +      /* return warning! */
 +
 +      /*if(limit) {
 +              int retval= select_linked_limited_invoke(&vc, 0, sel);
 +              WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit);
 +              return retval;
 +      }*/
 +      
 +      if( unified_findnearest(&vc, &eve, &eed, &efa)==0 ) {
 +              WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit);
 +      
 +              return OPERATOR_CANCELLED;
 +      }
 +      
 +      if (efa) {
 +              eed = bm_firstfaceloop(efa)->e;
 +      } else if (!eed) {
 +              if (!eve || !eve->e)
 +                      return OPERATOR_CANCELLED;
 +              
 +              eed = eve->e;
 +      }
 +
 +      BMW_Init(&walker, em->bm, BMW_SHELL, 0, 0);
 +      e = BMW_Begin(&walker, eed->v1);
 +      for (; e; e=BMW_Step(&walker)) {
 +                      BM_Select(em->bm, e->v1, sel);
 +                      BM_Select(em->bm, e->v2, sel);
 +      }
 +      BMW_End(&walker);
 +      EDBM_select_flush(em, SCE_SELECT_VERTEX);
 +
 +      WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit);
 +      return OPERATOR_FINISHED;       
 +}
 +
 +void MESH_OT_select_linked_pick(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Select Linked";
 +      ot->idname= "MESH_OT_select_linked_pick";
 +      
 +      /* api callbacks */
 +      ot->invoke= select_linked_pick_invoke;
 +      ot->poll= ED_operator_editmesh;
 +      ot->description= "select/deselect all vertices linked to the edge under the mouse cursor.";
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +      
 +      RNA_def_boolean(ot->srna, "deselect", 0, "Deselect", "");
 +      RNA_def_boolean(ot->srna, "limit", 0, "Limit by Seams", "");
 +}
 +
 +
 +static int select_linked_exec(bContext *C, wmOperator *UNUSED(op))
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh*)obedit->data)->edit_btmesh;
 +      BLI_array_declare(verts);
 +      BMVert **verts = NULL;
 +      BMIter iter;
 +      BMVert *v;
 +      BMEdge *e;
 +      BMWalker walker;
 +      int i, tot;
 +
 +      tot = 0;
 +              BM_ITER_SELECT(v, &iter, em->bm, BM_VERTS_OF_MESH, NULL)
 +              if (BM_TestHFlag(v, BM_SELECT)) {
 +                      BLI_array_growone(verts);
 +                      verts[tot++] = v;
 +              }
 +      }
 +
 +      BMW_Init(&walker, em->bm, BMW_SHELL, 0, 0);
 +      for (i=0; i<tot; i++) {
 +              e = BMW_Begin(&walker, verts[i]);
 +              for (; e; e=BMW_Step(&walker)) {
 +                      BM_Select(em->bm, e->v1, 1);
 +                      BM_Select(em->bm, e->v2, 1);
 +              }
 +      }
 +      BMW_End(&walker);
 +      EDBM_select_flush(em, SCE_SELECT_VERTEX);
 +
 +      BLI_array_free(verts);
 +      WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit);
 +
 +      return OPERATOR_FINISHED;       
 +}
 +
 +void MESH_OT_select_linked(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Select Linked All";
 +      ot->idname= "MESH_OT_select_linked";
 +      
 +      /* api callbacks */
 +      ot->exec= select_linked_exec;
 +      ot->poll= ED_operator_editmesh;
 +      ot->description= "Select all vertices linked to the active mesh.";
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +      
 +      RNA_def_boolean(ot->srna, "limit", 0, "Limit by Seams", "");
 +}
 +
 +/* ******************** **************** */
 +
 +static int select_more(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= (((Mesh *)obedit->data))->edit_btmesh;
 +      BMOperator bmop;
 +      int usefaces = em->selectmode > SCE_SELECT_EDGE;
 +
 +      EDBM_InitOpf(em, &bmop, op, "regionextend geom=%hvef constrict=%d usefaces=%d", 
 +                   BM_SELECT, 0, usefaces);
 +
 +      BMO_Exec_Op(em->bm, &bmop);
 +      BMO_HeaderFlag_Buffer(em->bm, &bmop, "geomout", BM_SELECT, BM_ALL);
 +
 +      EDBM_selectmode_flush(em);
 +
 +      if (!EDBM_FinishOp(em, &bmop, op, 1))
 +              return OPERATOR_CANCELLED;
 +
 +      WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit);
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_select_more(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Select More";
 +      ot->idname= "MESH_OT_select_more";
 +      ot->description= "Select more vertices, edges or faces connected to initial selection.";
 +
 +      /* api callbacks */
 +      ot->exec= select_more;
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +}
 +
 +static int select_less(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= (((Mesh *)obedit->data))->edit_btmesh;
 +      BMOperator bmop;
 +      int usefaces = em->selectmode > SCE_SELECT_EDGE;
 +
 +      EDBM_InitOpf(em, &bmop, op, "regionextend geom=%hvef constrict=%d usefaces=%d", 
 +                   BM_SELECT, 1, usefaces);
 +
 +      BMO_Exec_Op(em->bm, &bmop);
 +      BMO_UnHeaderFlag_Buffer(em->bm, &bmop, "geomout", BM_SELECT, BM_ALL);
 +
 +      EDBM_selectmode_flush(em);
 +
 +      if (!EDBM_FinishOp(em, &bmop, op, 1))
 +              return OPERATOR_CANCELLED;
 +
 +      WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit);
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_select_less(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Select Less";
 +      ot->idname= "MESH_OT_select_less";
 +      ot->description= "Deselect vertices, edges or faces at the boundary of each selection region.";
 +
 +      /* api callbacks */
 +      ot->exec= select_less;
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +}
 +
 +static int mesh_select_nth_exec(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
++//    BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
++//    int nth = RNA_int_get(op->ptr, "nth");
 +
 +#if 0 //BMESH_TODO
 +      if(EM_deselect_nth(em, nth) == 0) {
 +              BKE_report(op->reports, RPT_ERROR, "Mesh has no active vert/edge/face.");
 +              return OPERATOR_CANCELLED;
 +      }
 +#else
 +              BKE_report(op->reports, RPT_ERROR, "Unimplemented");
 +#endif
 +
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +
 +void MESH_OT_select_nth(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Select Nth";
 +      ot->description= "";
 +      ot->idname= "MESH_OT_select_nth";
 +
 +      /* api callbacks */
 +      ot->exec= mesh_select_nth_exec;
 +      ot->poll= ED_operator_editmesh;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      RNA_def_int(ot->srna, "nth", 2, 2, 100, "Nth Selection", "", 1, INT_MAX);
 +}
 +
 +#if 0 //BMESH_TODO, select nth tool
 +/* not that optimal!, should be nicer with bmesh */
 +static void tag_face_edges(EditFace *efa)
 +{
 +      if(efa->v4)
 +              efa->e1->tmp.l= efa->e2->tmp.l= efa->e3->tmp.l= efa->e4->tmp.l= 1;
 +      else
 +              efa->e1->tmp.l= efa->e2->tmp.l= efa->e3->tmp.l= 1;
 +}
 +static int tag_face_edges_test(EditFace *efa)
 +{
 +      if(efa->v4)
 +              return (efa->e1->tmp.l || efa->e2->tmp.l || efa->e3->tmp.l || efa->e4->tmp.l) ? 1:0;
 +      else
 +              return (efa->e1->tmp.l || efa->e2->tmp.l || efa->e3->tmp.l) ? 1:0;
 +}
 +
 +void em_deselect_nth_face(EditMesh *em, int nth, EditFace *efa_act)
 +{
 +      EditFace *efa;
 +      EditEdge *eed;
 +      int ok= 1;
 +
 +      if(efa_act==NULL) {
 +              return;
 +      }
 +
 +      /* to detect loose edges, we put f2 flag on 1 */
 +      for(eed= em->edges.first; eed; eed= eed->next) {
 +              eed->tmp.l= 0;
 +      }
 +
 +      for (efa= em->faces.first; efa; efa= efa->next) {
 +              efa->tmp.l = 0;
 +      }
 +
 +      efa_act->tmp.l = 1;
 +
 +      while(ok) {
 +              ok = 0;
 +
 +              for (efa= em->faces.first; efa; efa= efa->next) {
 +                      if(efa->tmp.l==1) { /* initialize */
 +                              tag_face_edges(efa);
 +                      }
 +
 +                      if(efa->tmp.l)
 +                              efa->tmp.l++;
 +              }
 +
 +              for (efa= em->faces.first; efa; efa= efa->next) {
 +                      if(efa->tmp.l==0 && tag_face_edges_test(efa)) {
 +                              efa->tmp.l= 1;
 +                              ok = 1; /* keep looping */
 +                      }
 +              }
 +      }
 +
 +      for (efa= em->faces.first; efa; efa= efa->next) {
 +              if(efa->tmp.l > 0 && efa->tmp.l % nth) {
 +                      EM_select_face(efa, 0);
 +              }
 +      }
 +      for (efa= em->faces.first; efa; efa= efa->next) {
 +              if(efa->f & SELECT) {
 +                      EM_select_face(efa, 1);
 +              }
 +      }
 +
 +      EM_nvertices_selected(em);
 +      EM_nedges_selected(em);
 +      EM_nfaces_selected(em);
 +}
 +
 +/* not that optimal!, should be nicer with bmesh */
 +static void tag_edge_verts(EditEdge *eed)
 +{
 +      eed->v1->tmp.l= eed->v2->tmp.l= 1;
 +}
 +static int tag_edge_verts_test(EditEdge *eed)
 +{
 +      return (eed->v1->tmp.l || eed->v2->tmp.l) ? 1:0;
 +}
 +
 +void em_deselect_nth_edge(EditMesh *em, int nth, EditEdge *eed_act)
 +{
 +      EditEdge *eed;
 +      EditVert *eve;
 +      int ok= 1;
 +
 +      if(eed_act==NULL) {
 +              return;
 +      }
 +
 +      for(eve= em->verts.first; eve; eve= eve->next) {
 +              eve->tmp.l= 0;
 +      }
 +
 +      for (eed= em->edges.first; eed; eed= eed->next) {
 +              eed->tmp.l = 0;
 +      }
 +
 +      eed_act->tmp.l = 1;
 +
 +      while(ok) {
 +              ok = 0;
 +
 +              for (eed= em->edges.first; eed; eed= eed->next) {
 +                      if(eed->tmp.l==1) { /* initialize */
 +                              tag_edge_verts(eed);
 +                      }
 +
 +                      if(eed->tmp.l)
 +                              eed->tmp.l++;
 +              }
 +
 +              for (eed= em->edges.first; eed; eed= eed->next) {
 +                      if(eed->tmp.l==0 && tag_edge_verts_test(eed)) {
 +                              eed->tmp.l= 1;
 +                              ok = 1; /* keep looping */
 +                      }
 +              }
 +      }
 +
 +      for (eed= em->edges.first; eed; eed= eed->next) {
 +              if(eed->tmp.l > 0 && eed->tmp.l % nth) {
 +                      EM_select_edge(eed, 0);
 +              }
 +      }
 +      for (eed= em->edges.first; eed; eed= eed->next) {
 +              if(eed->f & SELECT) {
 +                      EM_select_edge(eed, 1);
 +              }
 +      }
 +
 +      {
 +              /* grr, should be a function */
 +              EditFace *efa;
 +              for (efa= em->faces.first; efa; efa= efa->next) {
 +                      if(efa->v4) {
 +                              if(efa->e1->f & efa->e2->f & efa->e3->f & efa->e4->f & SELECT );
 +                              else efa->f &= ~SELECT;
 +                      }
 +                      else {
 +                              if(efa->e1->f & efa->e2->f & efa->e3->f & SELECT );
 +                              else efa->f &= ~SELECT;
 +                      }
 +              }
 +      }
 +
 +      EM_nvertices_selected(em);
 +      EM_nedges_selected(em);
 +      EM_nfaces_selected(em);
 +}
 +
 +void em_deselect_nth_vert(EditMesh *em, int nth, EditVert *eve_act)
 +{
 +      EditVert *eve;
 +      EditEdge *eed;
 +      int ok= 1;
 +
 +      if(eve_act==NULL) {
 +              return;
 +      }
 +
 +      for (eve= em->verts.first; eve; eve= eve->next) {
 +              eve->tmp.l = 0;
 +      }
 +
 +      eve_act->tmp.l = 1;
 +
 +      while(ok) {
 +              ok = 0;
 +
 +              for (eve= em->verts.first; eve; eve= eve->next) {
 +                      if(eve->tmp.l)
 +                              eve->tmp.l++;
 +              }
 +
 +              for (eed= em->edges.first; eed; eed= eed->next) {
 +                      if(eed->v1->tmp.l==2 && eed->v2->tmp.l==0) { /* initialize */
 +                              eed->v2->tmp.l= 1;
 +                              ok = 1; /* keep looping */
 +                      }
 +                      else if(eed->v2->tmp.l==2 && eed->v1->tmp.l==0) { /* initialize */
 +                              eed->v1->tmp.l= 1;
 +                              ok = 1; /* keep looping */
 +                      }
 +              }
 +      }
 +
 +      for (eve= em->verts.first; eve; eve= eve->next) {
 +              if(eve->tmp.l > 0 && eve->tmp.l % nth) {
 +                      eve->f &= ~SELECT;
 +              }
 +      }
 +
 +      EM_deselect_flush(em);
 +
 +      EM_nvertices_selected(em);
 +      // EM_nedges_selected(em); // flush does these
 +      // EM_nfaces_selected(em); // flush does these
 +}
 +#endif
 +
 +int EM_deselect_nth(BMEditMesh *em, int nth)
 +{
 +#if 0 //BMESH_TODO
 +      EditSelection *ese;
 +      ese = ((EditSelection*)em->selected.last);
 +      if(ese) {
 +              if(ese->type == EDITVERT) {
 +                      em_deselect_nth_vert(em, nth, (EditVert*)ese->data);
 +                      return 1;
 +              }
 +
 +              if(ese->type == EDITEDGE) {
 +                      em_deselect_nth_edge(em, nth, (EditEdge*)ese->data);
 +                      return 1;
 +              }
 +      }
 +      else {
 +              EditFace *efa_act = EM_get_actFace(em, 0);
 +              if(efa_act) {
 +                      em_deselect_nth_face(em, nth, efa_act);
 +                      return 1;
 +              }
 +      }
 +
 +      return 0;
 +#endif
 +      return 1;
 +}
 +
 +void em_setup_viewcontext(bContext *C, ViewContext *vc)
 +{
 +      view3d_set_viewcontext(C, vc);
 +      
 +      if(vc->obedit) {
 +              Mesh *me= vc->obedit->data;
 +              vc->em= me->edit_btmesh;
 +      }
 +}
 +
 +/* poll call for mesh operators requiring a view3d context */
 +int EM_view3d_poll(bContext *C)
 +{
 +      if(ED_operator_editmesh(C) && ED_operator_view3d_active(C))
 +              return 1;
 +      return 0;
 +}
 +
 +
 +static int select_sharp_edges_exec(bContext *C, wmOperator *op)
 +{
 +      /* Find edges that have exactly two neighboring faces,
 +      * check the angle between those faces, and if angle is
 +      * small enough, select the edge
 +      */
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +      BMIter iter;
 +      BMEdge *e;
 +      BMLoop *l1, *l2;
 +      float sharp = RNA_float_get(op->ptr, "sharpness"), angle;
 +
 +      sharp = (sharp * M_PI) / 180.0;
 +
 +      BM_ITER(e, &iter, em->bm, BM_EDGES_OF_MESH, NULL) {
 +              if (BM_TestHFlag(e, BM_HIDDEN) || !e->l)
 +                      continue;
 +
 +              l1 = e->l;
 +              l2 = l1->radial_next;
 +
 +              if (l1 == l2)
 +                      continue;
 +
 +              /* edge has exactly two neighboring faces, check angle */
 +              angle = saacos(l1->f->no[0]*l2->f->no[0]+l1->f->no[1]*l2->f->no[1]+l1->f->no[2]*l2->f->no[2]);
 +
 +              if (fabs(angle) < sharp) {
 +                      BM_Select(em->bm, e, 1);
 +              }
 +
 +      }
 +
 +      WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit->data);
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_edges_select_sharp(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Select Sharp Edges";
 +      ot->description= "Marked selected edges as sharp.";
 +      ot->idname= "MESH_OT_edges_select_sharp";
 +      
 +      /* api callbacks */
 +      ot->exec= select_sharp_edges_exec;
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +      
 +      /* props */
 +      RNA_def_float(ot->srna, "sharpness", 1.0f, 0.01f, FLT_MAX, "sharpness", "", 1.0f, 180.0f);
 +}
 +
 +static int select_linked_flat_faces_exec(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +      BMIter iter, liter, liter2;
 +      BMFace *f, **stack = NULL;
 +      BLI_array_declare(stack);
 +      BMLoop *l, *l2;
 +      float sharp = RNA_float_get(op->ptr, "sharpness");
 +      int i;
 +
 +      sharp = (sharp * M_PI) / 180.0;
 +
 +      BM_ITER(f, &iter, em->bm, BM_FACES_OF_MESH, NULL) {
 +              BMINDEX_SET(f, 0);
 +      }
 +
 +      BM_ITER(f, &iter, em->bm, BM_FACES_OF_MESH, NULL) {
 +              if (BM_TestHFlag(f, BM_HIDDEN) || !BM_TestHFlag(f, BM_SELECT) || BMINDEX_GET(f))
 +                      continue;
 +
 +              BLI_array_empty(stack);
 +              i = 1;
 +
 +              BLI_array_growone(stack);
 +              stack[i-1] = f;
 +
 +              while (i) {
 +                      f = stack[i-1];
 +                      i--;
 +
 +                      BM_Select(em->bm, f, 1);
 +
 +                      BMINDEX_SET(f, 1);
 +
 +                      BM_ITER(l, &liter, em->bm, BM_LOOPS_OF_FACE, f) {
 +                              BM_ITER(l2, &liter2, em->bm, BM_LOOPS_OF_LOOP, l) {
 +                                      float angle;
 +
 +                                      if (BMINDEX_GET(l2->f) || BM_TestHFlag(l2->f, BM_HIDDEN))
 +                                              continue;
 +
 +                                      /* edge has exactly two neighboring faces, check angle */
 +                                      angle = saacos(f->no[0]*l2->f->no[0]+f->no[1]*l2->f->no[1]+f->no[2]*l2->f->no[2]);
 +
 +                                      /* invalidate: edge too sharp */
 +                                      if (fabs(angle) < sharp) {
 +                                              BLI_array_growone(stack);
 +                                              stack[i] = l2->f;
 +                                              i++;
 +                                      }
 +                              }
 +                      }
 +              }
 +      }
 +
 +      BLI_array_free(stack);
 +
 +      WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit->data);
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_faces_select_linked_flat(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Select Linked Flat Faces";
 +      ot->description= "Select linked faces by angle.";
 +      ot->idname= "MESH_OT_faces_select_linked_flat";
 +      
 +      /* api callbacks */
 +      ot->exec= select_linked_flat_faces_exec;
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +      
 +      /* props */
 +      RNA_def_float(ot->srna, "sharpness", 1.0f, 0.01f, FLT_MAX, "sharpness", "", 1.0f, 180.0f);
 +}
 +
 +static int select_non_manifold_exec(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +      BMVert *v;
 +      BMEdge *e;
 +      BMIter iter;
 +
 +      /* Selects isolated verts, and edges that do not have 2 neighboring
 +       * faces
 +       */
 +      
 +      if(em->selectmode==SCE_SELECT_FACE) {
 +              BKE_report(op->reports, RPT_ERROR, "Doesn't work in face selection mode");
 +              return OPERATOR_CANCELLED;
 +      }
 +      
 +      BM_ITER(v, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
 +              if (!BM_TestHFlag(em->bm, BM_HIDDEN) && BM_Nonmanifold_Vert(em->bm, v))
 +                      BM_Select(em->bm, v, 1);
 +      }
 +      
 +      BM_ITER(e, &iter, em->bm, BM_EDGES_OF_MESH, NULL) {
 +              if (!BM_TestHFlag(em->bm, BM_HIDDEN) && BM_Nonmanifold_Edge(em->bm, e))
 +                      BM_Select(em->bm, e, 1);
 +      }
 +
 +      WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit->data);
 +
 +      return OPERATOR_FINISHED;       
 +}
 +
 +void MESH_OT_select_non_manifold(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Select Non Manifold";
 +      ot->description= "Select all non-manifold vertices or edges.";
 +      ot->idname= "MESH_OT_select_non_manifold";
 +      
 +      /* api callbacks */
 +      ot->exec= select_non_manifold_exec;
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +}
 +
 +static int mesh_select_random_exec(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +      BMVert *eve;
 +      BMEdge *eed;
 +      BMFace *efa;
 +      BMIter iter;
 +      float randfac =  RNA_float_get(op->ptr, "percent")/100.0f;
 +
 +      BLI_srand( BLI_rand() ); /* random seed */
 +      
 +      if(!RNA_boolean_get(op->ptr, "extend"))
 +              EDBM_clear_flag_all(em, BM_SELECT);
 +
 +      if(em->selectmode & SCE_SELECT_VERTEX) {
 +              BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
 +                      if (!BM_TestHFlag(eve, BM_HIDDEN) && BLI_frand() < randfac)
 +                              BM_Select(em->bm, eve, 1);
 +              }
 +              EDBM_selectmode_flush(em);
 +      }
 +      else if(em->selectmode & SCE_SELECT_EDGE) {
 +              BM_ITER(eed, &iter, em->bm, BM_EDGES_OF_MESH, NULL) {
 +                      if (!BM_TestHFlag(eed, BM_HIDDEN) && BLI_frand() < randfac)
 +                              BM_Select(em->bm, eed, 1);
 +              }
 +              EDBM_selectmode_flush(em);
 +      }
 +      else {
 +              BM_ITER(efa, &iter, em->bm, BM_FACES_OF_MESH, NULL) {
 +                      if (!BM_TestHFlag(efa, BM_HIDDEN) && BLI_frand() < randfac)
 +                              BM_Select(em->bm, efa, 1);
 +              }
 +              EDBM_selectmode_flush(em);
 +      }
 +      
 +      WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit->data);
 +      
 +      return OPERATOR_FINISHED;       
 +}
 +
 +void MESH_OT_select_random(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Select Random";
 +      ot->description= "Randomly select vertices.";
 +      ot->idname= "MESH_OT_select_random";
 +
 +      /* api callbacks */
 +      ot->exec= mesh_select_random_exec;
 +      ot->poll= ED_operator_editmesh;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +      
 +      /* props */
 +      RNA_def_float_percentage(ot->srna, "percent", 50.f, 0.0f, 100.0f, "Percent", "Percentage of elements to select randomly.", 0.f, 100.0f);
 +      RNA_def_boolean(ot->srna, "extend", 0, "Extend Selection", "Extend selection instead of deselecting everything first.");
 +}
 +
 +static int select_next_loop(bContext *C, wmOperator *UNUSED(op))
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= (((Mesh *)obedit->data))->edit_btmesh;
 +      BMFace *f;
 +      BMVert *v;
 +      BMIter iter;
 +      
 +      BM_ITER(v, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
 +              BMINDEX_SET(v, 0);
 +      }
 +      
 +      BM_ITER(f, &iter, em->bm, BM_FACES_OF_MESH, NULL) {
 +              BMLoop *l;
 +              BMIter liter;
 +              
 +              BM_ITER(l, &liter, em->bm, BM_LOOPS_OF_FACE, f) {
 +                      if (BM_TestHFlag(l->v, BM_SELECT) && !BM_TestHFlag(l->v, BM_HIDDEN)) {
 +                              BMINDEX_SET(l->next->v, 1);
 +                              BM_Select(em->bm, l->v, 0);
 +                      }
 +              }
 +      }
 +
 +      BM_ITER(v, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
 +              if (BMINDEX_GET(v)) {
 +                      BM_Select(em->bm, v, 1);
 +              }
 +      }
 +
 +      WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit);
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_select_next_loop(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Select Next Loop";
 +      ot->idname= "MESH_OT_select_next_loop";
 +      ot->description= "";
 +
 +      /* api callbacks */
 +      ot->exec= select_next_loop;
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +}
 +
 +
 +static int region_to_loop(bContext *C, wmOperator *UNUSED(op))
 +{
 +      Object *obedit = CTX_data_edit_object(C);
 +      BMEditMesh *em = ((Mesh*)obedit->data)->edit_btmesh;
 +      BMFace *f;
 +      BMEdge *e;
 +      BMIter iter;
 +      
 +      BM_ITER(e, &iter, em->bm, BM_EDGES_OF_MESH, NULL) {
 +              BMINDEX_SET(e, 0);
 +      }
 +
 +      BM_ITER(f, &iter, em->bm, BM_FACES_OF_MESH, NULL) {
 +              BMLoop *l1, *l2;
 +              BMIter liter1, liter2;
 +              
 +              BM_ITER(l1, &liter1, em->bm, BM_LOOPS_OF_FACE, f) {
 +                      int tot=0, totsel=0;
 +                      
 +                      BM_ITER(l2, &liter2, em->bm, BM_LOOPS_OF_EDGE, l1->e) {
 +                              tot++;
 +                              totsel += BM_TestHFlag(l2->f, BM_SELECT) != 0;
 +                      }
 +                      
 +                      if ((tot != totsel && totsel > 0) || (totsel == 1 && tot == 1))
 +                              BMINDEX_SET(l1->e, 1);
 +              }
 +      }
 +
 +      EDBM_clear_flag_all(em, BM_SELECT);
 +      
 +      BM_ITER(e, &iter, em->bm, BM_EDGES_OF_MESH, NULL) {
 +              if (BMINDEX_GET(e) && !BM_TestHFlag(e, BM_HIDDEN))
 +                      BM_Select_Edge(em->bm, e, 1);
 +      }
 +      
 +      WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit->data);
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_region_to_loop(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Select Boundary Loop";
 +      ot->idname= "MESH_OT_region_to_loop";
 +
 +      /* api callbacks */
 +      ot->exec= region_to_loop;
 +      ot->poll= ED_operator_editmesh;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +}
 +
 +static int loop_find_region(BMEditMesh *em, BMLoop *l, int flag, 
 +      SmallHash *fhash, BMFace ***region_out)
 +{
 +      BLI_array_declare(region);
 +      BLI_array_declare(stack);
 +      BMFace **region = NULL, *f;
 +      BMFace **stack = NULL;
 +      
 +      BLI_array_append(stack, l->f);
 +      BLI_smallhash_insert(fhash, (uintptr_t)l->f, NULL);
 +      
 +      while (BLI_array_count(stack) > 0) {
 +              BMIter liter1, liter2;
 +              BMLoop *l1, *l2;
 +              
 +              f = BLI_array_pop(stack);
 +              BLI_array_append(region, f);
 +              
 +              BM_ITER(l1, &liter1, em->bm, BM_LOOPS_OF_FACE, f) {
 +                      if (BM_TestHFlag(l1->e, flag))
 +                              continue;
 +                      
 +                      BM_ITER(l2, &liter2, em->bm, BM_LOOPS_OF_EDGE, l1->e) {
 +                              if (BLI_smallhash_haskey(fhash, (uintptr_t)l2->f))
 +                                      continue;
 +                              
 +                              BLI_array_append(stack, l2->f);
 +                              BLI_smallhash_insert(fhash, (uintptr_t)l2->f, NULL);
 +                      }
 +              }
 +      }
 +      
 +      BLI_array_free(stack);
 +      
 +      *region_out = region;
 +      return BLI_array_count(region);
 +}
 +
 +int verg_radial(const void *va, const void *vb)
 +{
 +      BMEdge *e1 = *((void**)va);
 +      BMEdge *e2 = *((void**)vb);
 +      int a, b;
 +      
 +      a = BM_Edge_FaceCount(e1);
 +      b = BM_Edge_FaceCount(e2);
 +      
 +      if (a > b) return -1;
 +      if (a == b) return 0;
 +      if (a < b) return 1;
 +      
 +      return -1;
 +}
 +
 +static int loop_find_regions(BMEditMesh *em, int selbigger)
 +{
 +      SmallHash visithash;
 +      BMIter iter;
 +      BMEdge *e, **edges=NULL;
 +      BLI_array_declare(edges);
 +      BMFace *f;
 +      int count = 0, i;
 +      
 +      BLI_smallhash_init(&visithash);
 +      
 +      BM_ITER(f, &iter, em->bm, BM_FACES_OF_MESH, NULL) {
 +              BMINDEX_SET(f, 0);
 +      }
 +
 +      BM_ITER(e, &iter, em->bm, BM_EDGES_OF_MESH, NULL) {
 +              if (BM_TestHFlag(e, BM_SELECT)) {
 +                      BLI_array_append(edges, e);
 +                      BMINDEX_SET(e, 1);
 +              } else {
 +                      BMINDEX_SET(e, 0);
 +              }
 +      }
 +      
 +      /*sort edges by radial cycle length*/
 +      qsort(edges,  BLI_array_count(edges), sizeof(void*), verg_radial);
 +      
 +      for (i=0; i<BLI_array_count(edges); i++) {
 +              BMIter liter;
 +              BMLoop *l;
 +              BMFace **region=NULL, **r;
 +              int c, tot=0;
 +              
 +              e = edges[i];
 +              
 +              if (!BMINDEX_GET(e))
 +                      continue;
 +              
 +              BM_ITER(l, &liter, em->bm, BM_LOOPS_OF_EDGE, e) {
 +                      if (BLI_smallhash_haskey(&visithash, (uintptr_t)l->f))
 +                              continue;
 +                                              
 +                      c = loop_find_region(em, l, BM_SELECT, &visithash, &r);
 +                      
 +                      if (!region || (selbigger ? c >= tot : c < tot)) {
 +                              tot = c;
 +                              if (region) 
 +                                      MEM_freeN(region);
 +                              region = r;
 +                      }
 +              }
 +              
 +              if (region) {
 +                      int j;
 +                      
 +                      for (j=0; j<tot; j++) {
 +                              BMINDEX_SET(region[j], 1);
 +                              BM_ITER(l, &liter, em->bm, BM_LOOPS_OF_FACE, region[j]) {
 +                                      BMINDEX_SET(l->e, 0);
 +                              }
 +                      }
 +                      
 +                      count += tot;
 +                      
 +                      MEM_freeN(region);
 +              }
 +      }
 +      
 +      BLI_array_free(edges);
 +      BLI_smallhash_release(&visithash);
 +      
 +      return count;
 +}
 +
 +static int loop_to_region(bContext *C, wmOperator *op)
 +{
 +      Object *obedit = CTX_data_edit_object(C);
 +      BMEditMesh *em = ((Mesh*)obedit->data)->edit_btmesh;
 +      BMIter iter;
 +      BMFace *f;
 +      int selbigger = RNA_boolean_get(op->ptr, "select_bigger");
 +      int a, b;
 +      
 +      /*find the set of regions with smallest number of total faces*/
 +      a = loop_find_regions(em, selbigger);
 +      b = loop_find_regions(em, !selbigger);
 +      
 +      if ((a <= b) ^ selbigger) {
 +              loop_find_regions(em, selbigger);
 +      }
 +      
 +      EDBM_clear_flag_all(em, BM_SELECT);
 +      
 +      BM_ITER(f, &iter, em->bm, BM_FACES_OF_MESH, NULL) {
 +              if (BMINDEX_GET(f) && !BM_TestHFlag(f, BM_HIDDEN)) {
 +                      BM_Select_Face(em->bm, f, 1);
 +              }
 +      }
 +      
 +      WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit->data);
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_loop_to_region(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Select Loop Inner-Region";
 +      ot->idname= "MESH_OT_loop_to_region";
 +
 +      /* api callbacks */
 +      ot->exec= loop_to_region;
 +      ot->poll= ED_operator_editmesh;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +      
 +      RNA_def_boolean(ot->srna, "select_bigger", 0, "Select Bigger", "Select bigger regions instead of smaller ones");
 +}
index ee829ac473c87d32719febf0a8048289ee473636,0000000000000000000000000000000000000000..ca6f61e24fd1be6d5ea4d78d70bb43adfa309893
mode 100644,000000..100644
--- /dev/null
@@@ -1,5118 -1,0 +1,5118 @@@
-               view3d_get_view_aligned_coordinate(&vc, min, event->mval);
 + /* $Id: bmesh_tools.c
 + *
 + * ***** BEGIN GPL LICENSE BLOCK *****
 + *
 + * This program is free software; you can redistribute it and/or
 + * modify it under the terms of the GNU General Public License
 + * as published by the Free Software Foundation; either version 2
 + * of the License, or (at your option) any later version.
 + *
 + * This program is distributed in the hope that it will be useful,
 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 + * GNU General Public License for more details.
 + *
 + * 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.
 + *
 + * The Original Code is Copyright (C) 2004 by Blender Foundation.
 + * All rights reserved.
 + *
 + * The Original Code is: all of this file.
 + *
 + * Contributor(s): Joseph Eagar
 + *
 + * ***** END GPL LICENSE BLOCK *****
 + */
 +#include <stdlib.h>
 +#include <stdarg.h>
 +#include <string.h>
 +#include <math.h>
 +#include <float.h>
 +
 +#include "MEM_guardedalloc.h"
 +#include "PIL_time.h"
 +
 +#include "BLO_sys_types.h" // for intptr_t support
 +
 +#include "DNA_mesh_types.h"
 +#include "DNA_material_types.h"
 +#include "DNA_meshdata_types.h"
 +#include "DNA_modifier_types.h"
 +#include "DNA_object_types.h"
 +#include "DNA_scene_types.h"
 +#include "DNA_screen_types.h"
 +#include "DNA_view3d_types.h"
 +#include "DNA_key_types.h"
 +#include "DNA_windowmanager_types.h"
 +
 +#include "RNA_types.h"
 +#include "RNA_define.h"
 +#include "RNA_access.h"
 +
 +#include "BLI_blenlib.h"
 +#include "BLI_math.h"
 +#include "BLI_editVert.h"
 +#include "BLI_rand.h"
 +#include "BLI_ghash.h"
 +#include "BLI_linklist.h"
 +#include "BLI_heap.h"
 +#include "BLI_array.h"
 +#include "BLI_smallhash.h"
 +
 +#include "BKE_material.h"
 +#include "BKE_context.h"
 +#include "BKE_customdata.h"
 +#include "BKE_DerivedMesh.h"
 +#include "BKE_cdderivedmesh.h"
 +#include "BKE_depsgraph.h"
 +#include "BKE_global.h"
 +#include "BKE_library.h"
 +#include "BKE_mesh.h"
 +#include "BKE_object.h"
 +#include "BKE_utildefines.h"
 +#include "BKE_bmesh.h"
 +#include "BKE_report.h"
 +#include "BKE_tessmesh.h"
 +#include "BKE_texture.h"
 +#include "BKE_main.h"
 +
 +#include "BIF_gl.h"
 +#include "BIF_glutil.h"
 +
 +#include "WM_api.h"
 +#include "WM_types.h"
 +
 +#include "ED_mesh.h"
 +#include "ED_view3d.h"
 +#include "ED_util.h"
 +#include "ED_screen.h"
 +#include "ED_transform.h"
 +#include "ED_object.h"
 +
 +#include "UI_interface.h"
 +
 +#include "RE_render_ext.h"
 +
 +#include "mesh_intern.h"
 +#include "bmesh.h"
 +
 +#include "editbmesh_bvh.h"
 +
 +static void add_normal_aligned(float *nor, float *add)
 +{
 +      if( INPR(nor, add) < -0.9999f)
 +              sub_v3_v3v3(nor, nor, add);
 +      else
 +              add_v3_v3v3(nor, nor, add);
 +}
 +
 +
 +static int subdivide_exec(bContext *C, wmOperator *op)
 +{
 +      ToolSettings *ts = CTX_data_tool_settings(C);
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +      int cuts= RNA_int_get(op->ptr,"number_cuts");
 +      float fractal= RNA_float_get(op->ptr, "fractal")/2.5;
 +      int flag= 0;
 +
 +      if(fractal != 0.0f)
 +              flag |= B_FRACTAL;
 +      
 +      if (RNA_boolean_get(op->ptr, "quadtri") && 
 +          RNA_enum_get(op->ptr, "quadcorner") == SUBD_STRAIGHT_CUT)
 +      {
 +              RNA_enum_set(op->ptr, "quadcorner", SUBD_INNERVERT);    
 +      }
 +      
 +      BM_esubdivideflag(obedit, em->bm, BM_SELECT, 
 +                        0.0f, fractal, 
 +                        ts->editbutflag|flag, 
 +                        cuts, 0, RNA_enum_get(op->ptr, "quadcorner"), 
 +                        RNA_boolean_get(op->ptr, "quadtri"),
 +                        1, RNA_int_get(op->ptr, "seed"));
 +
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +/* Note, these values must match delete_mesh() event values */
 +static EnumPropertyItem prop_mesh_cornervert_types[] = {
 +      {SUBD_INNERVERT,     "INNERVERT", 0,      "Inner Vert", ""},
 +      {SUBD_PATH,          "PATH", 0,           "Path", ""},
 +      {SUBD_STRAIGHT_CUT,  "STRAIGHT_CUT", 0,   "Straight Cut", ""},
 +      {SUBD_FAN,           "FAN", 0,            "Fan", ""},
 +      {0, NULL, 0, NULL, NULL}
 +};
 +
 +void MESH_OT_subdivide(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Subdivide";
 +      ot->description= "Subdivide selected edges.";
 +      ot->idname= "MESH_OT_subdivide";
 +
 +      /* api callbacks */
 +      ot->exec= subdivide_exec;
 +      ot->poll= ED_operator_editmesh;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      /* properties */
 +      RNA_def_int(ot->srna, "number_cuts", 1, 1, 50, "Number of Cuts", "", 1, INT_MAX);
 +
 +      RNA_def_boolean(ot->srna, "quadtri", 0, "Quad/Tri Mode", "Tries to prevent ngons");
 +      RNA_def_enum(ot->srna, "quadcorner", prop_mesh_cornervert_types, SUBD_STRAIGHT_CUT, "Quad Corner Type", "How to subdivide quad corners (anything other then Straight Cut will prevent ngons)");
 +
 +      RNA_def_float(ot->srna, "fractal", 0.0, 0.0f, FLT_MAX, "Fractal", "Fractal randomness factor.", 0.0f, 1000.0f);
 +      RNA_def_int(ot->srna, "seed", 0, 0, 10000, "Random Seed", "Seed for the random number generator", 0, 50);
 +}
 +
 +/* individual face extrude */
 +/* will use vertex normals for extrusion directions, so *nor is unaffected */
 +short EDBM_Extrude_face_indiv(BMEditMesh *em, wmOperator *op, short flag, float *UNUSED(nor)) 
 +{
 +      BMOIter siter;
 +      BMIter liter;
 +      BMFace *f;
 +      BMLoop *l;
 +      BMOperator bmop;
 +
 +      EDBM_InitOpf(em, &bmop, op, "extrude_face_indiv faces=%hf", flag);
 +
 +      /*deselect original verts*/
 +      EDBM_clear_flag_all(em, BM_SELECT);
 +
 +      BMO_Exec_Op(em->bm, &bmop);
 +      
 +      BMO_ITER(f, &siter, em->bm, &bmop, "faceout", BM_FACE) {
 +              BM_Select(em->bm, f, 1);
 +
 +              /*set face vertex normals to face normal*/
 +              BM_ITER(l, &liter, em->bm, BM_LOOPS_OF_FACE, f) {
 +                      VECCOPY(l->v->no, f->no);
 +              }
 +      }
 +
 +      if (!EDBM_FinishOp(em, &bmop, op, 1)) return 0;
 +
 +      return 's'; // s is shrink/fatten
 +}
 +
 +#if 0
 +short EDBM_Extrude_face_indiv(BMEditMesh *em, wmOperator *op, short flag, float *nor) 
 +      EditVert *eve, *v1, *v2, *v3, *v4;
 +      EditEdge *eed;
 +      EditFace *efa, *nextfa;
 +      
 +      if(em==NULL) return 0;
 +      
 +      /* selected edges with 1 or more selected face become faces */
 +      /* selected faces each makes new faces */
 +      /* always remove old faces, keeps volumes manifold */
 +      /* select the new extrusion, deselect old */
 +      
 +      /* step 1; init, count faces in edges */
 +      recalc_editnormals(em);
 +      
 +      for(eve= em->verts.first; eve; eve= eve->next) eve->f1= 0;      // new select flag
 +
 +      for(eed= em->edges.first; eed; eed= eed->next) {
 +              eed->f2= 0; // amount of unselected faces
 +      }
 +      for(efa= em->faces.first; efa; efa= efa->next) {
 +              if(efa->f & SELECT);
 +              else {
 +                      efa->e1->f2++;
 +                      efa->e2->f2++;
 +                      efa->e3->f2++;
 +                      if(efa->e4) efa->e4->f2++;
 +              }
 +      }
 +
 +      /* step 2: make new faces from faces */
 +      for(efa= em->faces.last; efa; efa= efa->prev) {
 +              if(efa->f & SELECT) {
 +                      v1= addvertlist(em, efa->v1->co, efa->v1);
 +                      v2= addvertlist(em, efa->v2->co, efa->v2);
 +                      v3= addvertlist(em, efa->v3->co, efa->v3);
 +                      
 +                      v1->f1= v2->f1= v3->f1= 1;
 +                      VECCOPY(v1->no, efa->n);
 +                      VECCOPY(v2->no, efa->n);
 +                      VECCOPY(v3->no, efa->n);
 +                      if(efa->v4) {
 +                              v4= addvertlist(em, efa->v4->co, efa->v4); 
 +                              v4->f1= 1;
 +                              VECCOPY(v4->no, efa->n);
 +                      }
 +                      else v4= NULL;
 +                      
 +                      /* side faces, clockwise */
 +                      addfacelist(em, efa->v2, v2, v1, efa->v1, efa, NULL);
 +                      addfacelist(em, efa->v3, v3, v2, efa->v2, efa, NULL);
 +                      if(efa->v4) {
 +                              addfacelist(em, efa->v4, v4, v3, efa->v3, efa, NULL);
 +                              addfacelist(em, efa->v1, v1, v4, efa->v4, efa, NULL);
 +                      }
 +                      else {
 +                              addfacelist(em, efa->v1, v1, v3, efa->v3, efa, NULL);
 +                      }
 +                      /* top face */
 +                      addfacelist(em, v1, v2, v3, v4, efa, NULL);
 +              }
 +      }
 +      
 +      /* step 3: remove old faces */
 +      efa= em->faces.first;
 +      while(efa) {
 +              nextfa= efa->next;
 +              if(efa->f & SELECT) {
 +                      BLI_remlink(&em->faces, efa);
 +                      free_editface(em, efa);
 +              }
 +              efa= nextfa;
 +      }
 +
 +      /* step 4: redo selection */
 +      EM_clear_flag_all(em, SELECT);
 +      
 +      for(eve= em->verts.first; eve; eve= eve->next) {
 +              if(eve->f1)  eve->f |= SELECT;
 +      }
 +      
 +      EM_select_flush(em);
 +      
 +      return 'n';
 +}
 +#endif
 +
 +/* extrudes individual edges */
 +short EDBM_Extrude_edges_indiv(BMEditMesh *em, wmOperator *op, short flag, float *UNUSED(nor)) 
 +{
 +      BMOperator bmop;
 +
 +      EDBM_InitOpf(em, &bmop, op, "extrude_edge_only edges=%he", flag);
 +
 +      /*deselect original verts*/
 +      EDBM_clear_flag_all(em, BM_SELECT);
 +
 +      BMO_Exec_Op(em->bm, &bmop);
 +      BMO_HeaderFlag_Buffer(em->bm, &bmop, "geomout", BM_SELECT, BM_VERT|BM_EDGE);
 +
 +      if (!EDBM_FinishOp(em, &bmop, op, 1)) return 0;
 +
 +      return 'n'; // n is normal grab
 +}
 +
 +#if 0
 +/* nor is filled with constraint vector */
 +short EDBM_Extrude_edges_indiv(BMEditMesh *em, short flag, float *nor) 
 +{
 +      EditVert *eve;
 +      EditEdge *eed;
 +      EditFace *efa;
 +      
 +      for(eve= em->verts.first; eve; eve= eve->next) eve->tmp.v = NULL;
 +      for(eed= em->edges.first; eed; eed= eed->next) {
 +              eed->tmp.f = NULL;
 +              eed->f2= ((eed->f & flag)!=0);
 +      }
 +      
 +      set_edge_directions_f2(em, 2);
 +
 +      /* sample for next loop */
 +      for(efa= em->faces.first; efa; efa= efa->next) {
 +              efa->e1->tmp.f = efa;
 +              efa->e2->tmp.f = efa;
 +              efa->e3->tmp.f = efa;
 +              if(efa->e4) efa->e4->tmp.f = efa;
 +      }
 +      /* make the faces */
 +      for(eed= em->edges.first; eed; eed= eed->next) {
 +              if(eed->f & flag) {
 +                      if(eed->v1->tmp.v == NULL)
 +                              eed->v1->tmp.v = addvertlist(em, eed->v1->co, eed->v1);
 +                      if(eed->v2->tmp.v == NULL)
 +                              eed->v2->tmp.v = addvertlist(em, eed->v2->co, eed->v2);
 +
 +                      if(eed->dir==1) 
 +                              addfacelist(em, eed->v1, eed->v2, 
 +                                                      eed->v2->tmp.v, eed->v1->tmp.v, 
 +                                                      eed->tmp.f, NULL);
 +                      else 
 +                              addfacelist(em, eed->v2, eed->v1, 
 +                                                      eed->v1->tmp.v, eed->v2->tmp.v, 
 +                                                      eed->tmp.f, NULL);
 +
 +                      /* for transform */
 +                      if(eed->tmp.f) {
 +                              efa = eed->tmp.f;
 +                              if (efa->f & SELECT) add_normal_aligned(nor, efa->n);
 +                      }
 +              }
 +      }
 +      normalize_v3(nor);
 +      
 +      /* set correct selection */
 +      EM_clear_flag_all(em, SELECT);
 +      for(eve= em->verts.last; eve; eve= eve->prev) {
 +              if(eve->tmp.v) {
 +                      eve->tmp.v->f |= flag;
 +              }
 +      }
 +
 +      for(eed= em->edges.first; eed; eed= eed->next) {
 +              if(eed->v1->f & eed->v2->f & flag) eed->f |= flag;
 +      }
 +      
 +      if(nor[0]==0.0 && nor[1]==0.0 && nor[2]==0.0) return 'g'; // g is grab
 +      return 'n';  // n is for normal constraint
 +}
 +#endif
 +
 +/* extrudes individual vertices */
 +short EDBM_Extrude_verts_indiv(BMEditMesh *em, wmOperator *op, short flag, float *UNUSED(nor)) 
 +{
 +      BMOperator bmop;
 +
 +      EDBM_InitOpf(em, &bmop, op, "extrude_vert_indiv verts=%hv", flag);
 +
 +      /*deselect original verts*/
 +      BMO_UnHeaderFlag_Buffer(em->bm, &bmop, "verts", BM_SELECT, BM_VERT);
 +
 +      BMO_Exec_Op(em->bm, &bmop);
 +      BMO_HeaderFlag_Buffer(em->bm, &bmop, "vertout", BM_SELECT, BM_VERT);
 +
 +      if (!EDBM_FinishOp(em, &bmop, op, 1)) return 0;
 +
 +      return 'g'; // g is grab
 +}
 +
 +short EDBM_Extrude_edge(Object *obedit, BMEditMesh *em, int flag, float *nor)
 +{
 +      BMesh *bm = em->bm;
 +      BMIter iter;
 +      BMOIter siter;
 +      BMOperator extop;
 +      BMVert *vert;
 +      BMEdge *edge;
 +      BMFace *f;
 +      ModifierData *md;
 +      BMHeader *el;
 +      
 +      BMO_Init_Op(&extop, "extrudefaceregion");
 +      BMO_HeaderFlag_To_Slot(bm, &extop, "edgefacein",
 +                             flag, BM_VERT|BM_EDGE|BM_FACE);
 +
 +      /* If a mirror modifier with clipping is on, we need to adjust some 
 +       * of the cases above to handle edges on the line of symmetry.
 +       */
 +      md = obedit->modifiers.first;
 +      for (; md; md=md->next) {
 +              if (md->type==eModifierType_Mirror) {
 +                      MirrorModifierData *mmd = (MirrorModifierData*) md;     
 +              
 +                      if(mmd->flag & MOD_MIR_CLIPPING) {
 +                              float mtx[4][4];
 +                              if (mmd->mirror_ob) {
 +                                      float imtx[4][4];
 +                                      invert_m4_m4(imtx, mmd->mirror_ob->obmat);
 +                                      mul_m4_m4m4(mtx, obedit->obmat, imtx);
 +                              }
 +
 +                              for (edge=BMIter_New(&iter,bm,BM_EDGES_OF_MESH,NULL);
 +                                   edge; edge=BMIter_Step(&iter))
 +                              {
 +                                      if(edge->head.flag & flag) {
 +                                              float co1[3], co2[3];
 +
 +                                              copy_v3_v3(co1, edge->v1->co);
 +                                              copy_v3_v3(co2, edge->v2->co);
 +
 +                                              if (mmd->mirror_ob) {
 +                                                      mul_v3_m4v3(co1, mtx, co1);
 +                                                      mul_v3_m4v3(co2, mtx, co2);
 +                                              }
 +
 +                                              if (mmd->flag & MOD_MIR_AXIS_X)
 +                                                      if ( (fabs(co1[0]) < mmd->tolerance) &&
 +                                                               (fabs(co2[0]) < mmd->tolerance) )
 +                                                              BMO_Insert_MapPointer(bm, &extop, "exclude", edge, NULL);
 +
 +                                              if (mmd->flag & MOD_MIR_AXIS_Y)
 +                                                      if ( (fabs(co1[1]) < mmd->tolerance) &&
 +                                                               (fabs(co2[1]) < mmd->tolerance) )
 +                                                              BMO_Insert_MapPointer(bm, &extop, "exclude", edge, NULL);
 +
 +                                              if (mmd->flag & MOD_MIR_AXIS_Z)
 +                                                      if ( (fabs(co1[2]) < mmd->tolerance) &&
 +                                                               (fabs(co2[2]) < mmd->tolerance) )
 +                                                              BMO_Insert_MapPointer(bm, &extop, "exclude", edge, NULL);
 +                                      }
 +                              }
 +                      }
 +              }
 +      }
 +
 +      BM_ITER(vert, &iter, bm, BM_VERTS_OF_MESH, NULL) {
 +              BM_Select(bm, vert, 0);
 +      }
 +
 +      BM_ITER(edge, &iter, bm, BM_EDGES_OF_MESH, NULL) {
 +              BM_Select(bm, edge, 0);
 +      }
 +
 +      BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
 +              BM_Select(bm, f, 0);
 +      }
 +
 +      BMO_Exec_Op(bm, &extop);
 +
 +      nor[0] = nor[1] = nor[2] = 0.0f;
 +      
 +      BMO_ITER(el, &siter, bm, &extop, "geomout", BM_ALL) {
 +              BM_Select(bm, el, 1);
 +
 +              if (el->type == BM_FACE) {
 +                      f = (BMFace*)el;
 +                      add_normal_aligned(nor, f->no);
 +              };
 +      }
 +
 +      normalize_v3(nor);
 +
 +      BMO_Finish_Op(bm, &extop);
 +
 +      if(nor[0]==0.0 && nor[1]==0.0 && nor[2]==0.0) return 'g'; // grab
 +      return 'n'; // normal constraint 
 +
 +}
 +short EDBM_Extrude_vert(Object *obedit, BMEditMesh *em, short flag, float *nor)
 +{
 +              BMIter iter;
 +              BMEdge *eed;
 +              
 +              /*ensure vert flags are consistent for edge selections*/
 +              eed = BMIter_New(&iter, em->bm, BM_EDGES_OF_MESH, NULL);
 +              for ( ; eed; eed=BMIter_Step(&iter)) {
 +                      if (BM_TestHFlag(eed, flag)) {
 +                              if (flag != BM_SELECT) {
 +                                      BM_SetHFlag(eed->v1, flag);
 +                                      BM_SetHFlag(eed->v2, flag);
 +                              } else {
 +                                      BM_Select(em->bm, eed->v1, 1);
 +                                      BM_Select(em->bm, eed->v2, 1);
 +                              }
 +                      } else {
 +                              if (BM_TestHFlag(eed->v1, flag) && BM_TestHFlag(eed->v2, flag)) {
 +                                      if (flag != BM_SELECT)
 +                                              BM_SetHFlag(eed, flag);
 +                                      else BM_Select(em->bm, eed, 1);
 +                              }
 +                      }
 +              }
 +
 +              return EDBM_Extrude_edge(obedit, em, flag, nor);
 +
 +}
 +
 +static int extrude_repeat_mesh(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em = ((Mesh *)obedit->data)->edit_btmesh;
 +      RegionView3D *rv3d = CTX_wm_region_view3d(C);           
 +              
 +      int steps = RNA_int_get(op->ptr,"steps");
 +      
 +      float offs = RNA_float_get(op->ptr,"offset");
 +      float dvec[3], tmat[3][3], bmat[3][3], nor[3]= {0.0, 0.0, 0.0};
 +      short a;
 +
 +      /* dvec */
 +      dvec[0]= rv3d->persinv[2][0];
 +      dvec[1]= rv3d->persinv[2][1];
 +      dvec[2]= rv3d->persinv[2][2];
 +      normalize_v3(dvec);
 +      dvec[0]*= offs;
 +      dvec[1]*= offs;
 +      dvec[2]*= offs;
 +
 +      /* base correction */
 +      copy_m3_m4(bmat, obedit->obmat);
 +      invert_m3_m3(tmat, bmat);
 +      mul_m3_v3(tmat, dvec);
 +
 +      for(a=0; a<steps; a++) {
 +              EDBM_Extrude_edge(obedit, em, BM_SELECT, nor);
 +              //BMO_CallOpf(em->bm, "extrudefaceregion edgefacein=%hef", BM_SELECT);
 +              BMO_CallOpf(em->bm, "translate vec=%v verts=%hv", (float*)dvec, BM_SELECT);
 +              //extrudeflag(obedit, em, SELECT, nor);
 +              //translateflag(em, SELECT, dvec);
 +      }
 +      
 +      EDBM_RecalcNormals(em);
 +
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_extrude_repeat(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Extrude Repeat Mesh";
 +      ot->description= "Extrude selected vertices, edges or faces repeatedly.";
 +      ot->idname= "MESH_OT_extrude_repeat";
 +      
 +      /* api callbacks */
 +      ot->exec= extrude_repeat_mesh;
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +      
 +      /* props */
 +      RNA_def_float(ot->srna, "offset", 2.0f, 0.0f, 100.0f, "Offset", "", 0.0f, FLT_MAX);
 +      RNA_def_int(ot->srna, "steps", 10, 0, 180, "Steps", "", 0, INT_MAX);
 +}
 +
 +/* generic extern called extruder */
 +int EDBM_Extrude_Mesh(Scene *scene, Object *obedit, BMEditMesh *em, wmOperator *op, float *norin)
 +{
 +      short nr, transmode= 0;
 +      float stacknor[3] = {0.0f, 0.0f, 0.0f};
 +      float *nor = norin ? norin : stacknor;
 +
 +      nor[0] = nor[1] = nor[2] = 0.0f;
 +
 +      if(em->selectmode & SCE_SELECT_VERTEX) {
 +              if(em->bm->totvertsel==0) nr= 0;
 +              else if(em->bm->totvertsel==1) nr= 4;
 +              else if(em->bm->totedgesel==0) nr= 4;
 +              else if(em->bm->totfacesel==0) 
 +                      nr= 3; // pupmenu("Extrude %t|Only Edges%x3|Only Vertices%x4");
 +              else if(em->bm->totfacesel==1)
 +                      nr= 1; // pupmenu("Extrude %t|Region %x1|Only Edges%x3|Only Vertices%x4");
 +              else 
 +                      nr= 1; // pupmenu("Extrude %t|Region %x1||Individual Faces %x2|Only Edges%x3|Only Vertices%x4");
 +      }
 +      else if(em->selectmode & SCE_SELECT_EDGE) {
 +              if (em->bm->totedgesel==0) nr = 0;
 +              
 +              nr = 1;
 +              /*else if (em->totedgesel==1) nr = 3;
 +              else if(em->totfacesel==0) nr = 3;
 +              else if(em->totfacesel==1)
 +                      nr= 1; // pupmenu("Extrude %t|Region %x1|Only Edges%x3");
 +              else
 +                      nr= 1; // pupmenu("Extrude %t|Region %x1||Individual Faces %x2|Only Edges%x3");
 +              */
 +      }
 +      else {
 +              if (em->bm->totfacesel == 0) nr = 0;
 +              else if (em->bm->totfacesel == 1) nr = 1;
 +              else
 +                      nr= 1; // pupmenu("Extrude %t|Region %x1||Individual Faces %x2");
 +      }
 +
 +      if(nr<1) return 'g';
 +
 +      if(nr==1 && em->selectmode & SCE_SELECT_VERTEX) 
 +              transmode= EDBM_Extrude_vert(obedit, em, SELECT, nor);
 +      else if (nr == 1) transmode= EDBM_Extrude_edge(obedit, em, SELECT, nor);
 +      else if(nr==4) transmode= EDBM_Extrude_verts_indiv(em, op, SELECT, nor);
 +      else if(nr==3) transmode= EDBM_Extrude_edges_indiv(em, op, SELECT, nor);
 +      else transmode= EDBM_Extrude_face_indiv(em, op, SELECT, nor);
 +      
 +      if(transmode==0) {
 +              BKE_report(op->reports, RPT_ERROR, "Not a valid selection for extrude");
 +      }
 +      else {
 +              
 +                      /* We need to force immediate calculation here because 
 +                      * transform may use derived objects (which are now stale).
 +                      *
 +                      * This shouldn't be necessary, derived queries should be
 +                      * automatically building this data if invalid. Or something.
 +                      */
 +//            DAG_object_flush_update(scene, obedit, OB_RECALC_DATA); 
 +              object_handle_update(scene, obedit);
 +
 +              /* individual faces? */
 +//            BIF_TransformSetUndo("Extrude");
 +              if(nr==2) {
 +//                    initTransform(TFM_SHRINKFATTEN, CTX_NO_PET|CTX_NO_MIRROR);
 +//                    Transform();
 +              }
 +              else {
 +//                    initTransform(TFM_TRANSLATION, CTX_NO_PET|CTX_NO_MIRROR);
 +                      if(transmode=='n') {
 +                              mul_m4_v3(obedit->obmat, nor);
 +                              sub_v3_v3v3(nor, nor, obedit->obmat[3]);
 +//                            BIF_setSingleAxisConstraint(nor, "along normal");
 +                      }
 +//                    Transform();
 +              }
 +      }
 +      
 +      return transmode;
 +}
 +
 +/* extrude without transform */
 +static int mesh_extrude_region_exec(bContext *C, wmOperator *op)
 +{
 +      Scene *scene = CTX_data_scene(C);
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh*)obedit->data)->edit_btmesh;
 +      
 +      EDBM_Extrude_Mesh(scene, obedit, em, op, NULL);
 +      
 +      WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit);
 +      
 +      return OPERATOR_FINISHED;       
 +}
 +
 +void MESH_OT_extrude_region(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Extrude Region";
 +      ot->idname= "MESH_OT_extrude_region";
 +      
 +      /* api callbacks */
 +      //ot->invoke= mesh_extrude_region_invoke;
 +      ot->exec= mesh_extrude_region_exec;
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      RNA_def_boolean(ot->srna, "mirror", 0, "Mirror Editing", "");
 +}
 +
 +static int mesh_extrude_verts_exec(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh*)obedit->data)->edit_btmesh;
 +      float nor[3];
 +
 +      EDBM_Extrude_verts_indiv(em, op, BM_SELECT, nor);
 +      
 +      WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit);
 +      
 +      return OPERATOR_FINISHED;       
 +}
 +
 +void MESH_OT_extrude_verts_indiv(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Extrude Only Vertices";
 +      ot->idname= "MESH_OT_extrude_verts_indiv";
 +      
 +      /* api callbacks */
 +      ot->exec= mesh_extrude_verts_exec;
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      /* to give to transform */
 +      RNA_def_boolean(ot->srna, "mirror", 0, "Mirror Editing", "");
 +}
 +
 +static int mesh_extrude_edges_exec(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh*)obedit->data)->edit_btmesh;
 +      float nor[3];
 +
 +      EDBM_Extrude_edges_indiv(em, op, BM_SELECT, nor);
 +      
 +      WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit);
 +      
 +      return OPERATOR_FINISHED;       
 +}
 +
 +void MESH_OT_extrude_edges_indiv(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Extrude Only Edges";
 +      ot->idname= "MESH_OT_extrude_edges_indiv";
 +      
 +      /* api callbacks */
 +      ot->exec= mesh_extrude_edges_exec;
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      /* to give to transform */
 +      RNA_def_boolean(ot->srna, "mirror", 0, "Mirror Editing", "");
 +}
 +
 +static int mesh_extrude_faces_exec(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh*)obedit->data)->edit_btmesh;
 +      float nor[3];
 +
 +      EDBM_Extrude_face_indiv(em, op, BM_SELECT, nor);
 +      
 +      WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit);
 +      
 +      return OPERATOR_FINISHED;       
 +}
 +
 +void MESH_OT_extrude_faces_indiv(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Extrude Individual Faces";
 +      ot->idname= "MESH_OT_extrude_faces_indiv";
 +      
 +      /* api callbacks */
 +      ot->exec= mesh_extrude_faces_exec;
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      RNA_def_boolean(ot->srna, "mirror", 0, "Mirror Editing", "");
 +}
 +
 +/* ******************** (de)select all operator **************** */
 +
 +void EDBM_toggle_select_all(BMEditMesh *em) /* exported for UV */
 +{
 +      if(em->bm->totvertsel || em->bm->totedgesel || em->bm->totfacesel)
 +              EDBM_clear_flag_all(em, SELECT);
 +      else 
 +              EDBM_set_flag_all(em, SELECT);
 +}
 +
 +static int toggle_select_all_exec(bContext *C, wmOperator *UNUSED(op))
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +      
 +      EDBM_toggle_select_all(em);
 +      
 +      WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit);
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_select_all(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Select/Deselect All";
 +      ot->idname= "MESH_OT_select_all";
 +      ot->description= "(de)select all vertices, edges or faces.";
 +      
 +      /* api callbacks */
 +      ot->exec= toggle_select_all_exec;
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +}
 +
 +/* *************** add-click-mesh (extrude) operator ************** */
 +
 +static int dupli_extrude_cursor(bContext *C, wmOperator *op, wmEvent *event)
 +{
 +      ViewContext vc;
 +      BMVert *v1;
 +      BMIter iter;
 +      float min[3], max[3];
 +      int done= 0;
 +      
 +      em_setup_viewcontext(C, &vc);
 +      
 +      INIT_MINMAX(min, max);
 +      
 +      BM_ITER_SELECT(v1, &iter, vc.em->bm, BM_VERTS_OF_MESH, NULL)
 +              DO_MINMAX(v1->co, min, max);
 +              done= 1;
 +      }
 +
 +      /* call extrude? */
 +      if(done) {
 +              BMEdge *eed;
 +              float vec[3], cent[3], mat[3][3];
 +              float nor[3]= {0.0, 0.0, 0.0};
 +              
 +              /* check for edges that are half selected, use for rotation */
 +              done= 0;
 +              BM_ITER(eed, &iter, vc.em->bm, BM_EDGES_OF_MESH, NULL) {
 +                      if (BM_TestHFlag(eed->v1, BM_SELECT) ^ BM_TestHFlag(eed->v2, BM_SELECT)) {
 +                              if(BM_TestHFlag(eed->v1, BM_SELECT)) 
 +                                      sub_v3_v3v3(vec, eed->v1->co, eed->v2->co);
 +                              else 
 +                                      sub_v3_v3v3(vec, eed->v2->co, eed->v1->co);
 +                              add_v3_v3v3(nor, nor, vec);
 +                              done= 1;
 +                      }
 +              }
 +              if(done) normalize_v3(nor);
 +              
 +              /* center */
 +              add_v3_v3v3(cent, min, max);
 +              mul_v3_fl(cent, 0.5f);
 +              VECCOPY(min, cent);
 +              
 +              mul_m4_v3(vc.obedit->obmat, min);       // view space
-               view3d_get_view_aligned_coordinate(&vc, min, event->mval);
++              view3d_get_view_aligned_coordinate(&vc, min, event->mval, 0);
 +              invert_m4_m4(vc.obedit->imat, vc.obedit->obmat); 
 +              mul_m4_v3(vc.obedit->imat, min); // back in object space
 +              
 +              sub_v3_v3v3(min, min, cent);
 +              
 +              /* calculate rotation */
 +              unit_m3(mat);
 +              if(done) {
 +                      float dot;
 +                      
 +                      VECCOPY(vec, min);
 +                      normalize_v3(vec);
 +                      dot= INPR(vec, nor);
 +
 +                      if( fabs(dot)<0.999) {
 +                              float cross[3], si, q1[4];
 +                              
 +                              cross_v3_v3v3(cross, nor, vec);
 +                              normalize_v3(cross);
 +                              dot= 0.5f*saacos(dot);
 +                              si= (float)sin(dot);
 +                              q1[0]= (float)cos(dot);
 +                              q1[1]= cross[0]*si;
 +                              q1[2]= cross[1]*si;
 +                              q1[3]= cross[2]*si;
 +                              
 +                              quat_to_mat3( mat,q1);
 +                      }
 +              }
 +              
 +
 +              EDBM_Extrude_edge(vc.obedit, vc.em, SELECT, nor);
 +              EDBM_CallOpf(vc.em, op, "rotate verts=%hv cent=%v mat=%m3",
 +                      BM_SELECT, cent, mat);
 +              EDBM_CallOpf(vc.em, op, "translate verts=%hv vec=%v",
 +                      BM_SELECT, min);
 +      }
 +      else {
 +              float *curs= give_cursor(vc.scene, vc.v3d);
 +              BMOperator bmop;
 +              BMOIter oiter;
 +              
 +              VECCOPY(min, curs);
 +
++              view3d_get_view_aligned_coordinate(&vc, min, event->mval, 0);
 +              invert_m4_m4(vc.obedit->imat, vc.obedit->obmat); 
 +              mul_m4_v3(vc.obedit->imat, min); // back in object space
 +              
 +              EDBM_InitOpf(vc.em, &bmop, op, "makevert co=%v", min);
 +              BMO_Exec_Op(vc.em->bm, &bmop);
 +
 +              BMO_ITER(v1, &oiter, vc.em->bm, &bmop, "newvertout", BM_VERT) {
 +                      BM_Select(vc.em->bm, v1, 1);
 +              }
 +
 +              if (!EDBM_FinishOp(vc.em, &bmop, op, 1))
 +                      return OPERATOR_CANCELLED;
 +      }
 +
 +      //retopo_do_all();
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, vc.obedit->data); 
 +      DAG_id_tag_update(vc.obedit->data, OB_RECALC_DATA);
 +      
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_dupli_extrude_cursor(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Duplicate or Extrude at 3D Cursor";
 +      ot->idname= "MESH_OT_dupli_extrude_cursor";
 +      
 +      /* api callbacks */
 +      ot->invoke= dupli_extrude_cursor;
 +      ot->description= "Duplicate and extrude selected vertices, edges or faces towards the mouse cursor.";
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +}
 +
 +static int delete_mesh(bContext *C, Object *obedit, wmOperator *op, int event, Scene *UNUSED(scene))
 +{
 +      BMEditMesh *bem = ((Mesh*)obedit->data)->edit_btmesh;
 +      
 +      if(event<1) return OPERATOR_CANCELLED;
 +
 +      if(event==10 ) {
 +              //"Erase Vertices";
 +
 +              if (!EDBM_CallOpf(bem, op, "del geom=%hv context=%i", BM_SELECT, DEL_VERTS))
 +                      return OPERATOR_CANCELLED;
 +      } 
 +      else if(event==11) {
 +              //"Edge Loop"
 +              if (!EDBM_CallOpf(bem, op, "dissolveedgeloop edges=%he", BM_SELECT))
 +                      return OPERATOR_CANCELLED;
 +      }
 +      else if(event==7) {
 +              //"Dissolve"
 +              if (bem->selectmode & SCE_SELECT_FACE) {
 +                      if (!EDBM_CallOpf(bem, op, "dissolvefaces faces=%hf",BM_SELECT))
 +                              return OPERATOR_CANCELLED;
 +              } else if (bem->selectmode & SCE_SELECT_EDGE) {
 +                      if (!EDBM_CallOpf(bem, op, "dissolveedges edges=%he",BM_SELECT))
 +                              return OPERATOR_CANCELLED;
 +              } else if (bem->selectmode & SCE_SELECT_VERTEX) {
 +                      if (!EDBM_CallOpf(bem, op, "dissolveverts verts=%hv",BM_SELECT))
 +                              return OPERATOR_CANCELLED;
 +              }
 +      }
 +      else if(event==4) {
 +              //Edges and Faces
 +              if (!EDBM_CallOpf(bem, op, "del geom=%hef context=%i", BM_SELECT, DEL_EDGESFACES))
 +                      return OPERATOR_CANCELLED;
 +      } 
 +      else if(event==1) {
 +              //"Erase Edges"
 +              if (!EDBM_CallOpf(bem, op, "del geom=%he context=%i", BM_SELECT, DEL_EDGES))
 +                      return OPERATOR_CANCELLED;
 +      }
 +      else if(event==2) {
 +              //"Erase Faces";
 +              if (!EDBM_CallOpf(bem, op, "del geom=%hf context=%i", BM_SELECT, DEL_FACES))
 +                      return OPERATOR_CANCELLED;
 +      }
 +      else if(event==5) {
 +              //"Erase Only Faces";
 +              if (!EDBM_CallOpf(bem, op, "del geom=%hf context=%d",
 +                                BM_SELECT, DEL_ONLYFACES))
 +                      return OPERATOR_CANCELLED;
 +      }
 +      
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +/* Note, these values must match delete_mesh() event values */
 +static EnumPropertyItem prop_mesh_delete_types[] = {
 +      {7, "DISSOLVE",         0, "Dissolve", ""},
 +      {12, "COLLAPSE", 0, "Collapse", ""},
 +      {10,"VERT",             0, "Vertices", ""},
 +      {1, "EDGE",             0, "Edges", ""},
 +      {2, "FACE",             0, "Faces", ""},
 +      {11, "EDGE_LOOP", 0, "Edge Loop", ""},
 +      {4, "EDGE_FACE", 0, "Edges & Faces", ""},
 +      {5, "ONLY_FACE", 0, "Only Faces", ""},
 +      {0, NULL, 0, NULL, NULL}
 +};
 +
 +static int delete_mesh_exec(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em = ((Mesh*)obedit->data)->edit_btmesh;
 +      Scene *scene = CTX_data_scene(C);
 +      int type = RNA_enum_get(op->ptr, "type");
 +      
 +      if (type != 12) {
 +              delete_mesh(C, obedit, op, type, scene);
 +      } else {
 +              if (!EDBM_CallOpf(em, op, "collapse edges=%he", BM_SELECT))
 +                      return OPERATOR_CANCELLED;
 +      }
 +              
 +      
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA|ND_SELECT, obedit);
 +      
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_delete(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Delete";
 +      ot->description= "Delete selected vertices, edges or faces.";
 +      ot->idname= "MESH_OT_delete";
 +      
 +      /* api callbacks */
 +      ot->invoke= WM_menu_invoke;
 +      ot->exec= delete_mesh_exec;
 +      
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +      
 +      /*props */
 +      ot->prop = RNA_def_enum(ot->srna, "type", prop_mesh_delete_types, 10, "Type", "Method used for deleting mesh data");
 +}
 +
 +
 +static int addedgeface_mesh_exec(bContext *C, wmOperator *op)
 +{
 +      BMOperator bmop;
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +      
 +      if (!EDBM_InitOpf(em, &bmop, op, "contextual_create geom=%hfev", BM_SELECT))
 +              return OPERATOR_CANCELLED;
 +      
 +      BMO_Exec_Op(em->bm, &bmop);
 +      BMO_HeaderFlag_Buffer(em->bm, &bmop, "faceout", BM_SELECT, BM_FACE);
 +
 +      if (!EDBM_FinishOp(em, &bmop, op, 1))
 +              return OPERATOR_CANCELLED;
 +
 +      WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit);
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_edge_face_add(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Make Edge/Face";
 +      ot->description= "Add an edge or face to selected.";
 +      ot->idname= "MESH_OT_edge_face_add";
 +      
 +      /* api callbacks */
 +      ot->exec= addedgeface_mesh_exec;
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +      
 +}
 +
 +static EnumPropertyItem prop_mesh_edit_types[] = {
 +      {1, "VERT", 0, "Vertices", ""},
 +      {2, "EDGE", 0, "Edges", ""},
 +      {3, "FACE", 0, "Faces", ""},
 +      {0, NULL, 0, NULL, NULL}
 +};
 +
 +static int mesh_selection_type_exec(bContext *C, wmOperator *op)
 +{             
 +      
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +      int type = RNA_enum_get(op->ptr,"type");
 +
 +      switch (type) {
 +              case 1:
 +                      em->selectmode = SCE_SELECT_VERTEX;
 +                      break;
 +              case 2:
 +                      em->selectmode = SCE_SELECT_EDGE;
 +                      break;
 +              case 3:
 +                      em->selectmode = SCE_SELECT_FACE;
 +                      break;
 +      }
 +
 +      EDBM_selectmode_set(em);
 +      CTX_data_tool_settings(C)->selectmode = em->selectmode;
 +
 +      WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit);
 +      
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_selection_type(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Selection Mode";
 +      ot->description= "Set the selection mode type.";
 +      ot->idname= "MESH_OT_selection_type";
 +      
 +      /* api callbacks */
 +      ot->invoke= WM_menu_invoke;
 +      ot->exec= mesh_selection_type_exec;
 +      
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +      
 +      /* props */
 +      RNA_def_enum(ot->srna, "type", prop_mesh_edit_types, 0, "Type", "Set the mesh selection type");
 +      RNA_def_boolean(ot->srna, "inclusive", 0, "Inclusive", "Selects geometry around selected geometry, occording to selection mode");       
 +}
 +
 +/* ************************* SEAMS AND EDGES **************** */
 +
 +static int editbmesh_mark_seam(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      Mesh *me= ((Mesh *)obedit->data);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +      BMesh *bm = em->bm;
 +      BMEdge *eed;
 +      BMIter iter;
 +      int clear = RNA_boolean_get(op->ptr, "clear");
 +      
 +      /* auto-enable seams drawing */
 +      if(clear==0) {
 +              me->drawflag |= ME_DRAWSEAMS;
 +      }
 +
 +      if(clear) {
 +              BM_ITER_SELECT(eed, &iter, bm, BM_EDGES_OF_MESH, NULL)
 +                      BM_ClearHFlag(eed, BM_SEAM);
 +              }
 +      }
 +      else {
 +              BM_ITER_SELECT(eed, &iter, bm, BM_EDGES_OF_MESH, NULL)
 +                      BM_SetHFlag(eed, BM_SEAM);
 +              }
 +      }
 +
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_mark_seam(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Mark Seam";
 +      ot->idname= "MESH_OT_mark_seam";
 +      ot->description= "(un)mark selected edges as a seam.";
 +      
 +      /* api callbacks */
 +      ot->exec= editbmesh_mark_seam;
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +      
 +      RNA_def_boolean(ot->srna, "clear", 0, "Clear", "");
 +}
 +
 +static int editbmesh_mark_sharp(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      Mesh *me= ((Mesh *)obedit->data);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +      BMesh *bm = em->bm;
 +      BMEdge *eed;
 +      BMIter iter;
 +      int clear = RNA_boolean_get(op->ptr, "clear");
 +
 +      /* auto-enable sharp edge drawing */
 +      if(clear == 0) {
 +              me->drawflag |= ME_DRAWSHARP;
 +      }
 +
 +      if(!clear) {
 +              BM_ITER_SELECT(eed, &iter, bm, BM_EDGES_OF_MESH, NULL)
 +                      BM_SetHFlag(eed, BM_SHARP);
 +              }
 +      } else {
 +              BM_ITER_SELECT(eed, &iter, bm, BM_EDGES_OF_MESH, NULL)
 +                      BM_ClearHFlag(eed, BM_SHARP);
 +              }
 +      }
 +
 +
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_mark_sharp(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Mark Sharp";
 +      ot->idname= "MESH_OT_mark_sharp";
 +      ot->description= "(un)mark selected edges as sharp.";
 +      
 +      /* api callbacks */
 +      ot->exec= editbmesh_mark_sharp;
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +      
 +      RNA_def_boolean(ot->srna, "clear", 0, "Clear", "");
 +}
 +
 +
 +static int editbmesh_vert_connect(bContext *C, wmOperator *UNUSED(op))
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +      BMesh *bm = em->bm;
 +      BMOperator bmop;
 +      int len = 0;
 +      
 +      BMO_InitOpf(bm, &bmop, "connectverts verts=%hv", BM_SELECT);
 +      BMO_Exec_Op(bm, &bmop);
 +      len = BMO_GetSlot(&bmop, "edgeout")->len;
 +      BMO_Finish_Op(bm, &bmop);
 +      
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return len ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
 +}
 +
 +void MESH_OT_vert_connect(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Vertex Connect";
 +      ot->idname= "MESH_OT_vert_connect";
 +      
 +      /* api callbacks */
 +      ot->exec= editbmesh_vert_connect;
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +}
 +
 +static int editbmesh_edge_split(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +      BMesh *bm = em->bm;
 +      BMOperator bmop;
 +      int len = 0;
 +      
 +      BMO_InitOpf(bm, &bmop, "edgesplit edges=%he numcuts=%d", BM_SELECT, RNA_int_get(op->ptr,"number_cuts"));
 +      BMO_Exec_Op(bm, &bmop);
 +      len = BMO_GetSlot(&bmop, "outsplit")->len;
 +      BMO_Finish_Op(bm, &bmop);
 +      
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return len ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
 +}
 +
 +void MESH_OT_edge_split(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Edge Split";
 +      ot->idname= "MESH_OT_edge_split";
 +      
 +      /* api callbacks */
 +      ot->exec= editbmesh_edge_split;
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      RNA_def_int(ot->srna, "number_cuts", 1, 1, 10, "Number of Cuts", "", 1, INT_MAX);
 +}
 +
 +/****************** add duplicate operator ***************/
 +
 +static int mesh_duplicate_exec(bContext *C, wmOperator *op)
 +{
 +      Object *ob= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh*)ob->data)->edit_btmesh;
 +      BMOperator bmop;
 +
 +      EDBM_InitOpf(em, &bmop, op, "dupe geom=%hvef", BM_SELECT);
 +      
 +      BMO_Exec_Op(em->bm, &bmop);
 +      EDBM_clear_flag_all(em, BM_SELECT);
 +
 +      BMO_HeaderFlag_Buffer(em->bm, &bmop, "newout", BM_SELECT, BM_ALL);
 +
 +      if (!EDBM_FinishOp(em, &bmop, op, 1))
 +              return OPERATOR_CANCELLED;
 +
 +      DAG_id_tag_update(ob->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, ob->data);
 +      
 +      return OPERATOR_FINISHED;
 +}
 +
 +static int mesh_duplicate_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
 +{
 +      WM_cursor_wait(1);
 +      mesh_duplicate_exec(C, op);
 +      WM_cursor_wait(0);
 +      
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_duplicate(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Duplicate";
 +      ot->description= "Duplicate selected vertices, edges or faces.";
 +      ot->idname= "MESH_OT_duplicate";
 +      
 +      /* api callbacks */
 +      ot->invoke= mesh_duplicate_invoke;
 +      ot->exec= mesh_duplicate_exec;
 +      
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* to give to transform */
 +      RNA_def_int(ot->srna, "mode", TFM_TRANSLATION, 0, INT_MAX, "Mode", "", 0, INT_MAX);
 +}
 +
 +static int flip_normals(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= (((Mesh *)obedit->data))->edit_btmesh;
 +      
 +      if (!EDBM_CallOpf(em, op, "reversefaces faces=%hf", BM_SELECT))
 +              return OPERATOR_CANCELLED;
 +      
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_flip_normals(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Flip Normals";
 +      ot->description= "Flip the direction of selected face's vertex and face normals";
 +      ot->idname= "MESH_OT_flip_normals";
 +      
 +      /* api callbacks */
 +      ot->exec= flip_normals;
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +}
 +
 +float *bm_get_cd_float(CustomData *cdata, void *data, int type)
 +{
 +      float *f = CustomData_bmesh_get(cdata, data, type);
 +
 +      return f;
 +}
 +
 +static const EnumPropertyItem direction_items[]= {
 +      {DIRECTION_CW, "CW", 0, "Clockwise", ""},
 +      {DIRECTION_CCW, "CCW", 0, "Counter Clockwise", ""},
 +      {0, NULL, 0, NULL, NULL}};
 +
 +/* only accepts 1 selected edge, or 2 selected faces */
 +static int edge_rotate_selected(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +      BMOperator bmop;
 +      BMEdge *eed;
 +      BMIter iter;
 +      int ccw = RNA_int_get(op->ptr, "direction") == 1; // direction == 2 when clockwise and ==1 for counter CW.
 +      
 +      if (!(em->bm->totfacesel == 2 || em->bm->totedgesel == 1)) {
 +              BKE_report(op->reports, RPT_ERROR, "Select one edge or two adjacent faces");
 +              return OPERATOR_CANCELLED;
 +      }
 +
 +      /*first see if we have two adjacent faces*/
 +      BM_ITER(eed, &iter, em->bm, BM_EDGES_OF_MESH, NULL) {
 +              if (BM_Edge_FaceCount(eed) == 2) {
 +                      if ((BM_TestHFlag(eed->l->f, BM_SELECT) && BM_TestHFlag(((BMLoop*)eed->l->radial_next)->f, BM_SELECT))
 +                               && !(BM_TestHFlag(eed->l->f, BM_HIDDEN) || BM_TestHFlag(((BMLoop*)eed->l->radial_next)->f, BM_HIDDEN)))
 +                      {
 +                              break;
 +                      }
 +              }
 +      }
 +      
 +      /*ok, we don't have two adjacent faces, but we do have two selected ones.
 +        that's an error condition.*/
 +      if (!eed && em->bm->totfacesel == 2) {
 +              BKE_report(op->reports, RPT_ERROR, "Select one edge or two adjacent faces");
 +              return OPERATOR_CANCELLED;
 +      }
 +
 +      if (!eed) {
 +              BM_ITER_SELECT(eed, &iter, em->bm, BM_EDGES_OF_MESH, NULL)
 +                      if (BM_TestHFlag(eed, BM_SELECT))
 +                              break;
 +              }
 +      }
 +
 +      /*this should never happen*/
 +      if (!eed)
 +              return OPERATOR_CANCELLED;
 +      
 +      EDBM_InitOpf(em, &bmop, op, "edgerotate edges=%e ccw=%d", eed, ccw);
 +      BMO_Exec_Op(em->bm, &bmop);
 +
 +      BMO_HeaderFlag_Buffer(em->bm, &bmop, "edgeout", BM_SELECT, BM_EDGE);
 +
 +      if (!EDBM_FinishOp(em, &bmop, op, 1))
 +              return OPERATOR_CANCELLED;
 +
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_edge_rotate(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Rotate Selected Edge";
 +      ot->description= "Rotate selected edge or adjoining faces.";
 +      ot->idname= "MESH_OT_edge_rotate";
 +
 +      /* api callbacks */
 +      ot->exec= edge_rotate_selected;
 +      ot->poll= ED_operator_editmesh;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      /* props */
 +      RNA_def_enum(ot->srna, "direction", direction_items, DIRECTION_CW, "direction", "direction to rotate edge around.");
 +}
 +
 +/* pinning code */
 +
 +/* swap is 0 or 1, if 1 it pins not selected */
 +void EDBM_pin_mesh(BMEditMesh *em, int swap)
 +{
 +      BMIter iter;
 +      BMHeader *h;
 +      int itermode;
 +
 +      if(em==NULL) return;
 +      
 +      if (em->selectmode & SCE_SELECT_VERTEX)
 +              itermode = BM_VERTS_OF_MESH;
 +      else if (em->selectmode & SCE_SELECT_EDGE)
 +              itermode = BM_EDGES_OF_MESH;
 +      else
 +              itermode = BM_FACES_OF_MESH;
 +
 +      BM_ITER(h, &iter, em->bm, itermode, NULL) {
 +              if (BM_TestHFlag(h, BM_SELECT) ^ swap)
 +                      BM_Pin(em->bm, h, 1);
 +      }
 +
 +      EDBM_selectmode_flush(em);
 +}
 +
 +static int pin_mesh_exec(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= (((Mesh *)obedit->data))->edit_btmesh;
 +      Mesh *me= ((Mesh *)obedit->data);
 +
 +      me->drawflag |= ME_DRAW_PINS;
 +      
 +      EDBM_pin_mesh(em, RNA_boolean_get(op->ptr, "unselected"));
 +              
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;       
 +}
 +
 +void MESH_OT_pin(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Pin Selection";
 +      ot->idname= "MESH_OT_pin";
 +      
 +      /* api callbacks */
 +      ot->exec= pin_mesh_exec;
 +      ot->poll= ED_operator_editmesh;
 +      ot->description= "Pin (un)selected vertices, edges or faces.";
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +      
 +      /* props */
 +      RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Pin unselected rather than selected.");
 +}
 +
 +/* swap is 0 or 1, if 1 it unhides not selected */
 +void EDBM_unpin_mesh(BMEditMesh *em, int swap)
 +{
 +      BMIter iter;
 +      BMHeader *ele;
 +      int itermode;
 +      
 +      if(em==NULL) return;
 +      
 +      if (em->selectmode & SCE_SELECT_VERTEX)
 +              itermode = BM_VERTS_OF_MESH;
 +      else if (em->selectmode & SCE_SELECT_EDGE)
 +              itermode = BM_EDGES_OF_MESH;
 +      else
 +              itermode = BM_FACES_OF_MESH;
 +
 +      BM_ITER(ele, &iter, em->bm, itermode, NULL) {
 +              if (BM_TestHFlag(ele, BM_SELECT) ^ swap)
 +                      BM_Pin(em->bm, ele, 0);
 +      }
 +
 +      EDBM_selectmode_flush(em);
 +}
 +
 +static int unpin_mesh_exec(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= (((Mesh *)obedit->data))->edit_btmesh;
 +      
 +      EDBM_unpin_mesh(em, RNA_boolean_get(op->ptr, "unselected"));
 +
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;       
 +}
 +
 +void MESH_OT_unpin(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Unpin Selection";
 +      ot->idname= "MESH_OT_unpin";
 +      ot->description= "Unpin (un)selected vertices, edges or faces.";
 +      
 +      /* api callbacks */
 +      ot->exec= unpin_mesh_exec;
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      /* props */
 +      RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Unpin unselected rather than selected.");
 +}
 +
 +
 +/* swap is 0 or 1, if 1 it hides not selected */
 +void EDBM_hide_mesh(BMEditMesh *em, int swap)
 +{
 +      BMIter iter;
 +      BMHeader *h;
 +      int itermode;
 +
 +      if(em==NULL) return;
 +      
 +      if (em->selectmode & SCE_SELECT_VERTEX)
 +              itermode = BM_VERTS_OF_MESH;
 +      else if (em->selectmode & SCE_SELECT_EDGE)
 +              itermode = BM_EDGES_OF_MESH;
 +      else
 +              itermode = BM_FACES_OF_MESH;
 +
 +      BM_ITER(h, &iter, em->bm, itermode, NULL) {
 +              if (BM_TestHFlag(h, BM_SELECT) ^ swap)
 +                      BM_Hide(em->bm, h, 1);
 +      }
 +
 +      /*original hide flushing comment (OUTDATED): 
 +        hide happens on least dominant select mode, and flushes up, not down! (helps preventing errors in subsurf) */
 +      /*  - vertex hidden, always means edge is hidden too
 +              - edge hidden, always means face is hidden too
 +              - face hidden, only set face hide
 +              - then only flush back down what's absolute hidden
 +      */
 +
 +}
 +
 +static int hide_mesh_exec(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= (((Mesh *)obedit->data))->edit_btmesh;
 +      
 +      EDBM_hide_mesh(em, RNA_boolean_get(op->ptr, "unselected"));
 +              
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;       
 +}
 +
 +void MESH_OT_hide(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Hide Selection";
 +      ot->idname= "MESH_OT_hide";
 +      
 +      /* api callbacks */
 +      ot->exec= hide_mesh_exec;
 +      ot->poll= ED_operator_editmesh;
 +       ot->description= "Hide (un)selected vertices, edges or faces.";
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +      
 +      /* props */
 +      RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected.");
 +}
 +
 +
 +void EDBM_reveal_mesh(BMEditMesh *em)
 +{
 +      BMIter iter;
 +      BMHeader *ele;
 +      int i, types[3] = {BM_VERTS_OF_MESH, BM_EDGES_OF_MESH, BM_FACES_OF_MESH};
 +      int sels[3] = {1, !(em->selectmode & SCE_SELECT_VERTEX), !(em->selectmode & (SCE_SELECT_VERTEX | SCE_SELECT_EDGE))};
 +
 +      for (i=0; i<3; i++) {
 +              BM_ITER(ele, &iter, em->bm, types[i], NULL) {
 +                      if (BM_TestHFlag(ele, BM_HIDDEN)) {
 +                              BM_Hide(em->bm, ele, 0);
 +
 +                              if (sels[i])
 +                                      BM_Select(em->bm, ele, 1);
 +                      }
 +              }
 +      }
 +
 +      EDBM_selectmode_flush(em);
 +}
 +
 +static int reveal_mesh_exec(bContext *C, wmOperator *UNUSED(op))
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= (((Mesh *)obedit->data))->edit_btmesh;
 +      
 +      EDBM_reveal_mesh(em);
 +
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;       
 +}
 +
 +void MESH_OT_reveal(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Reveal Hidden";
 +      ot->idname= "MESH_OT_reveal";
 +      ot->description= "Reveal all hidden vertices, edges and faces.";
 +      
 +      /* api callbacks */
 +      ot->exec= reveal_mesh_exec;
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +}
 +
 +static int normals_make_consistent_exec(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +      
 +      /*doflip has to do with bmesh_rationalize_normals, it's an internal
 +      thing*/
 +      if (!EDBM_CallOpf(em, op, "righthandfaces faces=%hf doflip=%d", BM_SELECT, 1))
 +              return OPERATOR_CANCELLED;
 +      
 +      if (RNA_boolean_get(op->ptr, "inside"))
 +              EDBM_CallOpf(em, op, "reversefaces faces=%hf doflip=%d", BM_SELECT, 1);
 +
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;       
 +}
 +
 +void MESH_OT_normals_make_consistent(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Make Normals Consistent";
 +      ot->description= "Make face and vertex normals point either outside or inside the mesh";
 +      ot->idname= "MESH_OT_normals_make_consistent";
 +      
 +      /* api callbacks */
 +      ot->exec= normals_make_consistent_exec;
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +      
 +      RNA_def_boolean(ot->srna, "inside", 0, "Inside", "");
 +}
 +
 +
 +
 +static int do_smooth_vertex(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +      ModifierData *md;
 +      int mirrx=0, mirry=0, mirrz=0;
 +      int i, repeat;
 +
 +      /* if there is a mirror modifier with clipping, flag the verts that
 +       * are within tolerance of the plane(s) of reflection 
 +       */
 +      for(md=obedit->modifiers.first; md; md=md->next) {
 +              if(md->type==eModifierType_Mirror) {
 +                      MirrorModifierData *mmd = (MirrorModifierData*) md;     
 +              
 +                      if(mmd->flag & MOD_MIR_CLIPPING) {
 +                              if (mmd->flag & MOD_MIR_AXIS_X)
 +                                      mirrx = 1;
 +                              if (mmd->flag & MOD_MIR_AXIS_Y)
 +                                      mirry = 1;
 +                              if (mmd->flag & MOD_MIR_AXIS_Z)
 +                                      mirrz = 1;
 +                      }
 +              }
 +      }
 +
 +      repeat = RNA_int_get(op->ptr,"repeat");
 +      if (!repeat)
 +              repeat = 1;
 +      
 +      for (i=0; i<repeat; i++) {
 +              if (!EDBM_CallOpf(em, op, "vertexsmooth verts=%hv mirror_clip_x=%d mirror_clip_y=%d mirror_clip_z=%d",
 +                                BM_SELECT, mirrx, mirry, mirrz))
 +              {
 +                      return OPERATOR_CANCELLED;
 +              }
 +      }
 +
 +      //BMESH_TODO: need to handle the x-axis editing option here properly.
 +      //should probably make a helper function for that? I dunno.
 +
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;
 +}     
 +      
 +void MESH_OT_vertices_smooth(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Smooth Vertex";
 +      ot->description= "Flatten angles of selected vertices.";
 +      ot->idname= "MESH_OT_vertices_smooth";
 +      
 +      /* api callbacks */
 +      ot->exec= do_smooth_vertex;
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      RNA_def_int(ot->srna, "repeat", 1, 1, 100, "Number of times to smooth the mesh", "", 1, INT_MAX);
 +}
 +
 +
 +static int bm_test_exec(bContext *C, wmOperator *UNUSED(op))
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      ARegion *ar = CTX_wm_region(C);
 +      View3D *v3d = CTX_wm_view3d(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +      BMBVHTree *tree = BMBVH_NewBVH(em);
 +      BMIter iter;
 +      BMEdge *e;
 +
 +      /*hide all back edges*/
 +      BM_ITER(e, &iter, em->bm, BM_EDGES_OF_MESH, NULL) {
 +              if (!BM_TestHFlag(e, BM_SELECT))
 +                      continue;
 +
 +              if (!BMBVH_EdgeVisible(tree, e, ar, v3d, obedit))
 +                      BM_Select(em->bm, e, 0);
 +      }
 +
 +      BMBVH_FreeBVH(tree);
 +      
 +#if 0 //uv island walker test
 +      BMIter iter, liter;
 +      BMFace *f;
 +      BMLoop *l, *l2;
 +      MLoopUV *luv;
 +      BMWalker walker;
 +      int i=0;
 +
 +      BM_ITER(f, &iter, em->bm, BM_FACES_OF_MESH, NULL) {
 +              BM_ITER(l, &liter, em->bm, BM_LOOPS_OF_FACE, f) {
 +                      luv = CustomData_bmesh_get(&em->bm->ldata, l->head.data, CD_MLOOPUV);
 +              }
 +      }
 +
 +      BMW_Init(&walker, em->bm, BMW_UVISLAND, 0);
 +
 +      BM_ITER(f, &iter, em->bm, BM_FACES_OF_MESH, NULL) {
 +              BM_ITER(l, &liter, em->bm, BM_LOOPS_OF_FACE, f) {
 +                      luv = CustomData_bmesh_get(&em->bm->ldata, l->head.data, CD_MLOOPUV);
 +                      if (luv->flag & MLOOPUV_VERTSEL) {
 +                              l2 = BMW_Begin(&walker, l);
 +                              for (; l2; l2=BMW_Step(&walker)) {
 +                                      luv = CustomData_bmesh_get(&em->bm->ldata, l2->head.data, CD_MLOOPUV);
 +                                      luv->flag |= MLOOPUV_VERTSEL;
 +                              }                               
 +                      }
 +              }
 +      }
 +
 +      BMW_End(&walker);
 +#endif
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;
 +}     
 +      
 +void MESH_OT_bm_test(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "BMesh Test Operator";
 +      ot->idname= "MESH_OT_bm_test";
 +      
 +      /* api callbacks */
 +      ot->exec= bm_test_exec;
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      //RNA_def_int(ot->srna, "repeat", 1, 1, 100, "Number of times to smooth the mesh", "", 1, INT_MAX);
 +}
 +
 +/********************** Smooth/Solid Operators *************************/
 +
 +void mesh_set_smooth_faces(BMEditMesh *em, short smooth)
 +{
 +      BMIter iter;
 +      BMFace *efa;
 +
 +      if(em==NULL) return;
 +      
 +      BM_ITER(efa, &iter, em->bm, BM_FACES_OF_MESH, NULL) {
 +              if (BM_TestHFlag(efa, BM_SELECT)) {
 +                      if (smooth)
 +                              BM_SetHFlag(efa, BM_SMOOTH);
 +                      else
 +                              BM_ClearHFlag(efa, BM_SMOOTH);
 +              }
 +      }
 +}
 +
 +static int mesh_faces_shade_smooth_exec(bContext *C, wmOperator *UNUSED(op))
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +
 +      mesh_set_smooth_faces(em, 1);
 +
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_faces_shade_smooth(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Shade Smooth";
 +       ot->description= "Display faces smooth (using vertex normals).";
 +      ot->idname= "MESH_OT_faces_shade_smooth";
 +
 +      /* api callbacks */
 +      ot->exec= mesh_faces_shade_smooth_exec;
 +      ot->poll= ED_operator_editmesh;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +}
 +
 +static int mesh_faces_shade_flat_exec(bContext *C, wmOperator *UNUSED(op))
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +
 +      mesh_set_smooth_faces(em, 0);
 +
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_faces_shade_flat(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Shade Flat";
 +      ot->description= "Display faces flat.";
 +      ot->idname= "MESH_OT_faces_shade_flat";
 +
 +      /* api callbacks */
 +      ot->exec= mesh_faces_shade_flat_exec;
 +      ot->poll= ED_operator_editmesh;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +}
 +
 +
 +/********************** UV/Color Operators *************************/
 +
 +
 +static const EnumPropertyItem axis_items[]= {
 +      {OPUVC_AXIS_X, "X", 0, "X", ""},
 +      {OPUVC_AXIS_Y, "Y", 0, "Y", ""},
 +      {0, NULL, 0, NULL, NULL}};
 +
 +static int mesh_rotate_uvs(bContext *C, wmOperator *op)
 +{
 +      Object *ob = CTX_data_edit_object(C);
 +      BMEditMesh *em = ((Mesh*)ob->data)->edit_btmesh;
 +      BMOperator bmop;
 +
 +      /* get the direction from RNA */
 +      int dir = RNA_enum_get(op->ptr, "direction");
 +
 +      /* initialize the bmop using EDBM api, which does various ui error reporting and other stuff */
 +      EDBM_InitOpf(em, &bmop, op, "meshrotateuvs faces=%hf dir=%d", BM_SELECT, dir);
 +
 +      /* execute the operator */
 +      BMO_Exec_Op(em->bm, &bmop);
 +
 +      /* finish the operator */
 +      if( !EDBM_FinishOp(em, &bmop, op, 1) )
 +              return OPERATOR_CANCELLED;
 +
 +
 +      /* dependencies graph and notification stuff */
 +      DAG_id_tag_update(ob->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, ob->data);
 +/*    DAG_id_tag_update(ob->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, ob->data);
 +*/
 +      /* we succeeded */
 +      return OPERATOR_FINISHED;
 +}
 +
 +static int mesh_reverse_uvs(bContext *C, wmOperator *op)
 +{
 +      Object *ob = CTX_data_edit_object(C);
 +      BMEditMesh *em = ((Mesh*)ob->data)->edit_btmesh;
 +      BMOperator bmop;
 +
 +      /* initialize the bmop using EDBM api, which does various ui error reporting and other stuff */
 +      EDBM_InitOpf(em, &bmop, op, "meshreverseuvs faces=%hf", BM_SELECT);
 +
 +      /* execute the operator */
 +      BMO_Exec_Op(em->bm, &bmop);
 +
 +      /* finish the operator */
 +      if( !EDBM_FinishOp(em, &bmop, op, 1) )
 +              return OPERATOR_CANCELLED;
 +
 +      /* dependencies graph and notification stuff */
 +      DAG_id_tag_update(ob->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, ob->data);
 +/*    DAG_id_tag_update(ob->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, ob->data);
 +*/
 +      /* we succeeded */
 +      return OPERATOR_FINISHED;
 +}
 +
 +static int mesh_rotate_colors(bContext *C, wmOperator *op)
 +{
 +      Object *ob = CTX_data_edit_object(C);
 +      BMEditMesh *em = ((Mesh*)ob->data)->edit_btmesh;
 +      BMOperator bmop;
 +
 +      /* get the direction from RNA */
 +      int dir = RNA_enum_get(op->ptr, "direction");
 +
 +      /* initialize the bmop using EDBM api, which does various ui error reporting and other stuff */
 +      EDBM_InitOpf(em, &bmop, op, "meshrotatecolors faces=%hf dir=%d", BM_SELECT, dir);
 +
 +      /* execute the operator */
 +      BMO_Exec_Op(em->bm, &bmop);
 +
 +      /* finish the operator */
 +      if( !EDBM_FinishOp(em, &bmop, op, 1) )
 +              return OPERATOR_CANCELLED;
 +
 +
 +      /* dependencies graph and notification stuff */
 +      DAG_id_tag_update(ob->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, ob->data);
 +/*    DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_OBJECT | ND_GEOM_SELECT, ob);
 +*/
 +      /* we succeeded */
 +      return OPERATOR_FINISHED;
 +}
 +
 +
 +static int mesh_reverse_colors(bContext *C, wmOperator *op)
 +{
 +      Object *ob = CTX_data_edit_object(C);
 +      BMEditMesh *em = ((Mesh*)ob->data)->edit_btmesh;
 +      BMOperator bmop;
 +
 +      /* initialize the bmop using EDBM api, which does various ui error reporting and other stuff */
 +      EDBM_InitOpf(em, &bmop, op, "meshreversecolors faces=%hf", BM_SELECT);
 +
 +      /* execute the operator */
 +      BMO_Exec_Op(em->bm, &bmop);
 +
 +      /* finish the operator */
 +      if( !EDBM_FinishOp(em, &bmop, op, 1) )
 +              return OPERATOR_CANCELLED;
 +
 +      DAG_id_tag_update(ob->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, ob->data);
 +
 +      /* we succeeded */
 +      return OPERATOR_FINISHED;
 +#if 0
 +      Scene *scene= CTX_data_scene(C);
 +      Object *obedit= CTX_data_edit_object(C);
 +      EditMesh *em= BKE_mesh_get_editmesh((Mesh *)obedit->data);
 +
 +      EditFace *efa;
 +      short change = 0;
 +      MCol tmpcol, *mcol;
 +      int axis= RNA_enum_get(op->ptr, "axis");
 +
 +      if (!EM_vertColorCheck(em)) {
 +              BKE_report(op->reports, RPT_ERROR, "Mesh has no color layers");
 +              BKE_mesh_end_editmesh(obedit->data, em);
 +              return OPERATOR_CANCELLED;
 +      }
 +
 +      for(efa=em->faces.first; efa; efa=efa->next) {
 +              if (efa->f & SELECT) {
 +                      mcol = CustomData_em_get(&em->fdata, efa->data, CD_MCOL);
 +                      if (axis == AXIS_Y) {
 +                              tmpcol= mcol[1];
 +                              mcol[1]= mcol[2];
 +                              mcol[2]= tmpcol;
 +
 +                              if(efa->v4) {
 +                                      tmpcol= mcol[0];
 +                                      mcol[0]= mcol[3];
 +                                      mcol[3]= tmpcol;
 +                              }
 +                      } else {
 +                              tmpcol= mcol[0];
 +                              mcol[0]= mcol[1];
 +                              mcol[1]= tmpcol;
 +
 +                              if(efa->v4) {
 +                                      tmpcol= mcol[2];
 +                                      mcol[2]= mcol[3];
 +                                      mcol[3]= tmpcol;
 +                              }
 +                      }
 +                      change = 1;
 +              }
 +      }
 +
 +      BKE_mesh_end_editmesh(obedit->data, em);
 +
 +      if(!change)
 +              return OPERATOR_CANCELLED;
 +
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +#endif
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_uvs_rotate(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Rotate UVs";
 +      ot->idname= "MESH_OT_uvs_rotate";
 +
 +      /* api callbacks */
 +      ot->exec= mesh_rotate_uvs;
 +      ot->poll= ED_operator_editmesh;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      /* props */
 +      RNA_def_enum(ot->srna, "direction", direction_items, DIRECTION_CW, "Direction", "Direction to rotate UVs around.");
 +}
 +
 +//void MESH_OT_uvs_mirror(wmOperatorType *ot)
 +void MESH_OT_uvs_reverse(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Reverse UVs";
 +      ot->idname= "MESH_OT_uvs_reverse";
 +
 +      /* api callbacks */
 +      ot->exec= mesh_reverse_uvs;
 +      ot->poll= ED_operator_editmesh;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      /* props */
 +      //RNA_def_enum(ot->srna, "axis", axis_items, DIRECTION_CW, "Axis", "Axis to mirror UVs around.");
 +}
 +
 +void MESH_OT_colors_rotate(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Rotate Colors";
 +      ot->idname= "MESH_OT_colors_rotate";
 +
 +      /* api callbacks */
 +      ot->exec= mesh_rotate_colors;
 +      ot->poll= ED_operator_editmesh;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      /* props */
 +      RNA_def_enum(ot->srna, "direction", direction_items, DIRECTION_CW, "Direction", "Direction to rotate edge around.");
 +}
 +
 +void MESH_OT_colors_reverse(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Reverse Colors";
 +      ot->idname= "MESH_OT_colors_reverse";
 +
 +      /* api callbacks */
 +      ot->exec= mesh_reverse_colors;
 +      ot->poll= ED_operator_editmesh;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      /* props */
 +      //RNA_def_enum(ot->srna, "axis", axis_items, DIRECTION_CW, "Axis", "Axis to mirror colors around.");
 +}
 +
 +
 +static int merge_firstlast(BMEditMesh *em, int first, int uvmerge, wmOperator *wmop)
 +{
 +      BMVert *mergevert;
 +      BMEditSelection *ese;
 +
 +      /* do sanity check in mergemenu in edit.c ?*/
 +      if(first == 0){
 +              ese = em->bm->selected.last;
 +              mergevert= (BMVert*)ese->data;
 +      }
 +      else{
 +              ese = em->bm->selected.first;
 +              mergevert = (BMVert*)ese->data;
 +      }
 +
 +      if (!BM_TestHFlag(mergevert, BM_SELECT))
 +              return OPERATOR_CANCELLED;
 +      
 +      if (uvmerge) {
 +              if (!EDBM_CallOpf(em, wmop, "pointmerge_facedata verts=%hv snapv=%e", BM_SELECT, mergevert))
 +                      return OPERATOR_CANCELLED;
 +      }
 +
 +      if (!EDBM_CallOpf(em, wmop, "pointmerge verts=%hv mergeco=%v", BM_SELECT, mergevert->co))
 +              return OPERATOR_CANCELLED;
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +static int merge_target(BMEditMesh *em, Scene *scene, View3D *v3d, Object *ob, 
 +                        int target, int uvmerge, wmOperator *wmop)
 +{
 +      BMIter iter;
 +      BMVert *v;
 +      float *vco=NULL, co[3], cent[3] = {0.0f, 0.0f, 0.0f}, fac;
 +      int i;
 +
 +      if (target) {
 +              vco = give_cursor(scene, v3d);
 +              VECCOPY(co, vco);
 +              mul_m4_v3(ob->imat, co);
 +      } else {
 +              i = 0;
 +              BM_ITER(v, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
 +                      if (!BM_TestHFlag(v, BM_SELECT))
 +                              continue;
 +                      VECADD(cent, cent, v->co);
 +                      i++;
 +              }
 +              
 +              if (!i)
 +                      return OPERATOR_CANCELLED;
 +
 +              fac = 1.0f / (float)i;
 +              mul_v3_fl(cent, fac);
 +              copy_v3_v3(co, cent);
 +              vco = co;
 +      }
 +
 +      if (!vco)
 +              return OPERATOR_CANCELLED;
 +      
 +      if (uvmerge) {
 +              if (!EDBM_CallOpf(em, wmop, "vert_average_facedata verts=%hv", BM_SELECT))
 +                      return OPERATOR_CANCELLED;
 +      }
 +
 +      if (!EDBM_CallOpf(em, wmop, "pointmerge verts=%hv mergeco=%v", BM_SELECT, co))
 +              return OPERATOR_CANCELLED;
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +static int merge_exec(bContext *C, wmOperator *op)
 +{
 +      Scene *scene= CTX_data_scene(C);
 +      View3D *v3d = CTX_wm_view3d(C);
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +      int status= 0, uvs= RNA_boolean_get(op->ptr, "uvs");
 +
 +      switch(RNA_enum_get(op->ptr, "type")) {
 +              case 3:
 +                      status = merge_target(em, scene, v3d, obedit, 0, uvs, op);
 +                      break;
 +              case 4:
 +                      status = merge_target(em, scene, v3d, obedit, 1, uvs, op);
 +                      break;
 +              case 1:
 +                      status = merge_firstlast(em, 0, uvs, op);
 +                      break;
 +              case 6:
 +                      status = merge_firstlast(em, 1, uvs, op);
 +                      break;
 +              case 5:
 +                      status = 1;
 +                      if (!EDBM_CallOpf(em, op, "collapse edges=%he", BM_SELECT))
 +                              status = 0;
 +                      break;
 +      }
 +
 +      if(!status)
 +              return OPERATOR_CANCELLED;
 +
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +static EnumPropertyItem merge_type_items[]= {
 +      {6, "FIRST", 0, "At First", ""},
 +      {1, "LAST", 0, "At Last", ""},
 +      {3, "CENTER", 0, "At Center", ""},
 +      {4, "CURSOR", 0, "At Cursor", ""},
 +      {5, "COLLAPSE", 0, "Collapse", ""},
 +      {0, NULL, 0, NULL, NULL}};
 +
 +static EnumPropertyItem *merge_type_itemf(bContext *C, PointerRNA *UNUSED(ptr), int *free)
 +{     
 +      Object *obedit;
 +      EnumPropertyItem *item= NULL;
 +      int totitem= 0;
 +      
 +      if(!C) /* needed for docs */
 +              return merge_type_items;
 +      
 +      obedit= CTX_data_edit_object(C);
 +      if(obedit && obedit->type == OB_MESH) {
 +              BMEditMesh *em= ((Mesh*)obedit->data)->edit_btmesh;
 +
 +              if(em->selectmode & SCE_SELECT_VERTEX) {
 +                      if(em->bm->selected.first && em->bm->selected.last &&
 +                              ((BMEditSelection*)em->bm->selected.first)->type == BM_VERT && ((BMEditSelection*)em->bm->selected.last)->type == BM_VERT) {
 +                              RNA_enum_items_add_value(&item, &totitem, merge_type_items, 6);
 +                              RNA_enum_items_add_value(&item, &totitem, merge_type_items, 1);
 +                      }
 +                      else if(em->bm->selected.first && ((BMEditSelection*)em->bm->selected.first)->type == BM_VERT)
 +                              RNA_enum_items_add_value(&item, &totitem, merge_type_items, 1);
 +                      else if(em->bm->selected.last && ((BMEditSelection*)em->bm->selected.last)->type == BM_VERT)
 +                              RNA_enum_items_add_value(&item, &totitem, merge_type_items, 6);
 +              }
 +
 +              RNA_enum_items_add_value(&item, &totitem, merge_type_items, 3);
 +              RNA_enum_items_add_value(&item, &totitem, merge_type_items, 4);
 +              RNA_enum_items_add_value(&item, &totitem, merge_type_items, 5);
 +              RNA_enum_item_end(&item, &totitem);
 +
 +              *free= 1;
 +
 +              return item;
 +      }
 +      
 +      return NULL;
 +}
 +
 +void MESH_OT_merge(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Merge";
 +      ot->idname= "MESH_OT_merge";
 +
 +      /* api callbacks */
 +      ot->exec= merge_exec;
 +      ot->invoke= WM_menu_invoke;
 +      ot->poll= ED_operator_editmesh;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      /* properties */
 +      ot->prop= RNA_def_enum(ot->srna, "type", merge_type_items, 3, "Type", "Merge method to use.");
 +      RNA_def_enum_funcs(ot->prop, merge_type_itemf);
 +      RNA_def_boolean(ot->srna, "uvs", 1, "UVs", "Move UVs according to merge.");
 +}
 +
 +
 +static int removedoublesflag_exec(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +      BMOperator bmop;
 +      int count;
 +
 +      EDBM_InitOpf(em, &bmop, op, "finddoubles verts=%hv dist=%f", 
 +              BM_SELECT, RNA_float_get(op->ptr, "mergedist"));
 +      BMO_Exec_Op(em->bm, &bmop);
 +
 +      count = BMO_CountSlotMap(em->bm, &bmop, "targetmapout");
 +
 +      if (!EDBM_CallOpf(em, op, "weldverts targetmap=%s", &bmop, "targetmapout")) {
 +              BMO_Finish_Op(em->bm, &bmop);
 +              return OPERATOR_CANCELLED;
 +      }
 +
 +      if (!EDBM_FinishOp(em, &bmop, op, 1))
 +              return OPERATOR_CANCELLED;
 +
 +      /*we need a better way of reporting this, since this doesn't work
 +        with the last operator panel correctly.
 +      if(count)
 +      {
 +              sprintf(msg, "Removed %d vertices", count);
 +              BKE_report(op->reports, RPT_INFO, msg);
 +      }
 +      */
 +
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_remove_doubles(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Remove Doubles";
 +      ot->idname= "MESH_OT_remove_doubles";
 +
 +      /* api callbacks */
 +      ot->exec= removedoublesflag_exec;
 +      ot->poll= ED_operator_editmesh;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      RNA_def_float(ot->srna, "mergedist", 0.0001f, 0.000001f, 50.0f, 
 +              "Merge Distance", 
 +              "Minimum distance between elements to merge.", 0.00001, 10.0);
 +}
 +
 +/************************ Vertex Path Operator *************************/
 +
 +typedef struct PathNode {
 +      int u;
 +      int visited;
 +      ListBase edges;
 +} PathNode;
 +
 +typedef struct PathEdge {
 +      struct PathEdge *next, *prev;
 +      int v;
 +      float w;
 +} PathEdge;
 +
 +
 +
 +int select_vertex_path_exec(bContext *C, wmOperator *op)
 +{
 +      Object *ob = CTX_data_edit_object(C);
 +      BMEditMesh *em = ((Mesh*)ob->data)->edit_btmesh;
 +      BMOperator bmop;
 +      BMEditSelection *sv, *ev;
 +
 +      /* get the type from RNA */
 +      int type = RNA_enum_get(op->ptr, "type");
 +
 +      sv = em->bm->selected.last;
 +      if( sv != NULL )
 +              ev = sv->prev;
 +      else return OPERATOR_CANCELLED;
 +      if( ev == NULL )
 +              return OPERATOR_CANCELLED;
 +
 +      if( sv->type != BM_VERT || ev->type != BM_VERT )
 +              return OPERATOR_CANCELLED;
 +
 +      /* initialize the bmop using EDBM api, which does various ui error reporting and other stuff */
 +      EDBM_InitOpf(em, &bmop, op, "vertexshortestpath startv=%e endv=%e type=%d", sv->data, ev->data, type);
 +
 +      /* execute the operator */
 +      BMO_Exec_Op(em->bm, &bmop);
 +
 +      /* DO NOT clear the existing selection */
 +      /* EDBM_clear_flag_all(em, BM_SELECT); */
 +
 +      /* select the output */
 +      BMO_HeaderFlag_Buffer(em->bm, &bmop, "vertout", BM_SELECT, BM_ALL);
 +
 +      /* finish the operator */
 +      if( !EDBM_FinishOp(em, &bmop, op, 1) )
 +              return OPERATOR_CANCELLED;
 +
 +      EDBM_selectmode_flush(em);
 +
 +      /* dependencies graph and notification stuff */
 +/*    DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_OBJECT | ND_GEOM_SELECT, ob);
 +*/
 +      DAG_id_tag_update(ob->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, ob->data);
 +
 +
 +      /* we succeeded */
 +      return OPERATOR_FINISHED;
 +#if 0
 +      Object *obedit= CTX_data_edit_object(C);
 +      EditMesh *em= BKE_mesh_get_editmesh((Mesh *)obedit->data);
 +      EditVert *eve, *s, *t;
 +      EditEdge *eed;
 +      EditSelection *ese;
 +      PathEdge *newpe, *currpe;
 +      PathNode *currpn;
 +      PathNode *Q;
 +      int v, *previous, pathvert, pnindex; /*pnindex redundant?*/
 +      int unbalanced, totnodes;
 +      short physical;
 +      float *cost;
 +      Heap *heap; /*binary heap for sorting pointers to PathNodes based upon a 'cost'*/
 +
 +      s = t = NULL;
 +
 +      ese = ((EditSelection*)em->selected.last);
 +      if(ese && ese->type == EDITVERT && ese->prev && ese->prev->type == EDITVERT){
 +              physical= pupmenu("Distance Method? %t|Edge Length%x1|Topological%x0");
 +
 +              t = (EditVert*)ese->data;
 +              s = (EditVert*)ese->prev->data;
 +
 +              /*need to find out if t is actually reachable by s....*/
 +              for(eve=em->verts.first; eve; eve=eve->next){
 +                      eve->f1 = 0;
 +              }
 +
 +              s->f1 = 1;
 +
 +              unbalanced = 1;
 +              totnodes = 1;
 +              while(unbalanced){
 +                      unbalanced = 0;
 +                      for(eed=em->edges.first; eed; eed=eed->next){
 +                              if(!eed->h){
 +                                      if(eed->v1->f1 && !eed->v2->f1){
 +                                                      eed->v2->f1 = 1;
 +                                                      totnodes++;
 +                                                      unbalanced = 1;
 +                                      }
 +                                      else if(eed->v2->f1 && !eed->v1->f1){
 +                                                      eed->v1->f1 = 1;
 +                                                      totnodes++;
 +                                                      unbalanced = 1;
 +                                      }
 +                              }
 +                      }
 +              }
 +
 +              if(s->f1 && t->f1){ /* t can be reached by s */
 +                      Q = MEM_callocN(sizeof(PathNode)*totnodes, "Path Select Nodes");
 +                      totnodes = 0;
 +                      for(eve=em->verts.first; eve; eve=eve->next){
 +                              if(eve->f1){
 +                                      Q[totnodes].u = totnodes;
 +                                      Q[totnodes].edges.first = 0;
 +                                      Q[totnodes].edges.last = 0;
 +                                      Q[totnodes].visited = 0;
 +                                      eve->tmp.p = &(Q[totnodes]);
 +                                      totnodes++;
 +                              }
 +                              else eve->tmp.p = NULL;
 +                      }
 +
 +                      for(eed=em->edges.first; eed; eed=eed->next){
 +                              if(!eed->h){
 +                                      if(eed->v1->f1){
 +                                              currpn = ((PathNode*)eed->v1->tmp.p);
 +
 +                                              newpe = MEM_mallocN(sizeof(PathEdge), "Path Edge");
 +                                              newpe->v = ((PathNode*)eed->v2->tmp.p)->u;
 +                                              if(physical){
 +                                                              newpe->w = len_v3v3(eed->v1->co, eed->v2->co);
 +                                              }
 +                                              else newpe->w = 1;
 +                                              newpe->next = 0;
 +                                              newpe->prev = 0;
 +                                              BLI_addtail(&(currpn->edges), newpe);
 +                                      }
 +                                      if(eed->v2->f1){
 +                                              currpn = ((PathNode*)eed->v2->tmp.p);
 +                                              newpe = MEM_mallocN(sizeof(PathEdge), "Path Edge");
 +                                              newpe->v = ((PathNode*)eed->v1->tmp.p)->u;
 +                                              if(physical){
 +                                                              newpe->w = len_v3v3(eed->v1->co, eed->v2->co);
 +                                              }
 +                                              else newpe->w = 1;
 +                                              newpe->next = 0;
 +                                              newpe->prev = 0;
 +                                              BLI_addtail(&(currpn->edges), newpe);
 +                                      }
 +                              }
 +                      }
 +
 +                      heap = BLI_heap_new();
 +                      cost = MEM_callocN(sizeof(float)*totnodes, "Path Select Costs");
 +                      previous = MEM_callocN(sizeof(int)*totnodes, "PathNode indices");
 +
 +                      for(v=0; v < totnodes; v++){
 +                              cost[v] = 1000000;
 +                              previous[v] = -1; /*array of indices*/
 +                      }
 +
 +                      pnindex = ((PathNode*)s->tmp.p)->u;
 +                      cost[pnindex] = 0;
 +                      BLI_heap_insert(heap,  0.0f, SET_INT_IN_POINTER(pnindex));
 +
 +                      while( !BLI_heap_empty(heap) ){
 +
 +                              pnindex = GET_INT_FROM_POINTER(BLI_heap_popmin(heap));
 +                              currpn = &(Q[pnindex]);
 +
 +                              if(currpn == (PathNode*)t->tmp.p) /*target has been reached....*/
 +                                      break;
 +
 +                              for(currpe=currpn->edges.first; currpe; currpe=currpe->next){
 +                                      if(!Q[currpe->v].visited){
 +                                              if( cost[currpe->v] > (cost[currpn->u ] + currpe->w) ){
 +                                                      cost[currpe->v] = cost[currpn->u] + currpe->w;
 +                                                      previous[currpe->v] = currpn->u;
 +                                                      Q[currpe->v].visited = 1;
 +                                                      BLI_heap_insert(heap, cost[currpe->v], SET_INT_IN_POINTER(currpe->v));
 +                                              }
 +                                      }
 +                              }
 +                      }
 +
 +                      pathvert = ((PathNode*)t->tmp.p)->u;
 +                      while(pathvert != -1){
 +                              for(eve=em->verts.first; eve; eve=eve->next){
 +                                      if(eve->f1){
 +                                              if( ((PathNode*)eve->tmp.p)->u == pathvert) eve->f |= SELECT;
 +                                      }
 +                              }
 +                              pathvert = previous[pathvert];
 +                      }
 +
 +                      for(v=0; v < totnodes; v++) BLI_freelistN(&(Q[v].edges));
 +                      MEM_freeN(Q);
 +                      MEM_freeN(cost);
 +                      MEM_freeN(previous);
 +                      BLI_heap_free(heap, NULL);
 +                      EM_select_flush(em);
 +              }
 +      }
 +      else {
 +              BKE_mesh_end_editmesh(obedit->data, em);
 +              BKE_report(op->reports, RPT_ERROR, "Path Selection requires that exactly two vertices be selected");
 +              return OPERATOR_CANCELLED;
 +      }
 +
 +      WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
 +      BKE_mesh_end_editmesh(obedit->data, em);
 +#endif
 +}
 +
 +void MESH_OT_select_vertex_path(wmOperatorType *ot)
 +{
 +      static const EnumPropertyItem type_items[] = {
 +              {VPATH_SELECT_EDGE_LENGTH, "EDGE_LENGTH", 0, "Edge Length", NULL},
 +              {VPATH_SELECT_TOPOLOGICAL, "TOPOLOGICAL", 0, "Topological", NULL},
 +              {0, NULL, 0, NULL, NULL}};
 +
 +      /* identifiers */
 +      ot->name= "Select Vertex Path";
 +      ot->idname= "MESH_OT_select_vertex_path";
 +
 +      /* api callbacks */
 +      ot->exec= select_vertex_path_exec;
 +      ot->invoke= WM_menu_invoke;
 +      ot->poll= ED_operator_editmesh;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      /* properties */
 +      RNA_def_enum(ot->srna, "type", type_items, VPATH_SELECT_EDGE_LENGTH, "Type", "Method to compute distance.");
 +}
 +/********************** Rip Operator *************************/
 +
 +#if 0
 +/* helper for below */
 +static void mesh_rip_setface(EditMesh *em, EditFace *sefa)
 +{
 +      /* put new vertices & edges in best face */
 +      if(sefa->v1->tmp.v) sefa->v1= sefa->v1->tmp.v;
 +      if(sefa->v2->tmp.v) sefa->v2= sefa->v2->tmp.v;
 +      if(sefa->v3->tmp.v) sefa->v3= sefa->v3->tmp.v;
 +      if(sefa->v4 && sefa->v4->tmp.v) sefa->v4= sefa->v4->tmp.v;
 +
 +      sefa->e1= addedgelist(em, sefa->v1, sefa->v2, sefa->e1);
 +      sefa->e2= addedgelist(em, sefa->v2, sefa->v3, sefa->e2);
 +      if(sefa->v4) {
 +              sefa->e3= addedgelist(em, sefa->v3, sefa->v4, sefa->e3);
 +              sefa->e4= addedgelist(em, sefa->v4, sefa->v1, sefa->e4);
 +      }
 +      else
 +              sefa->e3= addedgelist(em, sefa->v3, sefa->v1, sefa->e3);
 +
 +}
 +#endif
 +
 +/* helper to find edge for edge_rip */
 +static float mesh_rip_edgedist(ARegion *ar, float mat[][4], float *co1, float *co2, short *mval)
 +{
 +      float vec1[3], vec2[3], mvalf[2];
 +
 +      view3d_project_float(ar, co1, vec1, mat);
 +      view3d_project_float(ar, co2, vec2, mat);
 +      mvalf[0]= (float)mval[0];
 +      mvalf[1]= (float)mval[1];
 +
 +      return dist_to_line_segment_v2(mvalf, vec1, vec2);
 +}
 +
 +/* based on mouse cursor position, it defines how is being ripped */
 +static int mesh_rip_invoke(bContext *C, wmOperator *op, wmEvent *event)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      ARegion *ar= CTX_wm_region(C);
 +      View3D *v3d = CTX_wm_view3d(C);
 +      RegionView3D *rv3d= CTX_wm_region_view3d(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +      BMOperator bmop;
 +      BMBVHTree *bvhtree;
 +      BMOIter siter;
 +      BMIter iter, eiter, liter;
 +      BMLoop *l;
 +      BMEdge *e, *e2, *closest = NULL;
 +      BMVert *v;
 +      int side = 0, i, singlesel = 0;
 +      float projectMat[4][4], fmval[3] = {event->mval[0], event->mval[1], 0.0f};
 +      float dist = FLT_MAX, d;
 +
 +      view3d_get_object_project_mat(rv3d, obedit, projectMat);
 +
 +      BM_ITER(e, &iter, em->bm, BM_EDGES_OF_MESH, NULL) {
 +              if (BM_TestHFlag(e, BM_SELECT))
 +                      BMINDEX_SET(e, 1);
 +              else BMINDEX_SET(e, 0);
 +      }
 +
 +      /*handle case of one vert selected.  we identify
 +        the closest edge around that vert to the mouse cursor,
 +        then rip the two adjacent edges in the vert fan.*/
 +      if (em->bm->totvertsel == 1 && em->bm->totedgesel == 0 && em->bm->totfacesel == 0) {
 +              singlesel = 1;
 +
 +              /*find selected vert*/
 +              BM_ITER(v, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
 +                      if (BM_TestHFlag(v, BM_SELECT))
 +                              break;
 +              }
 +
 +              /*this should be impossible, but sanity checks are a good thing*/
 +              if (!v)
 +                      return OPERATOR_CANCELLED;
 +
 +              /*find closest edge to mouse cursor*/
 +              e2 = NULL;
 +              BM_ITER(e, &iter, em->bm, BM_EDGES_OF_VERT, v) {
 +                      d = mesh_rip_edgedist(ar, projectMat, e->v1->co, e->v2->co, event->mval);
 +                      if (d < dist) {
 +                              dist = d;
 +                              e2 = e;
 +                      }
 +              }
 +
 +              if (!e2)
 +                      return OPERATOR_CANCELLED;
 +
 +              /*rip two adjacent edges*/
 +              if (BM_Edge_FaceCount(e2) == 1) {
 +                      l = e2->l;
 +                      e = BM_OtherFaceLoop(e2, l->f, v)->e;
 +
 +                      BMINDEX_SET(e, 1);
 +                      BM_SetHFlag(e, BM_SELECT);
 +              } else if (BM_Edge_FaceCount(e2) == 2) {
 +                      l = e2->l;
 +                      e = BM_OtherFaceLoop(e2, l->f, v)->e;
 +                      BMINDEX_SET(e, 1);
 +                      BM_SetHFlag(e, BM_SELECT);
 +                      
 +                      l = e2->l->radial_next;
 +                      e = BM_OtherFaceLoop(e2, l->f, v)->e;
 +                      BMINDEX_SET(e, 1);
 +                      BM_SetHFlag(e, BM_SELECT);
 +              }
 +
 +              dist = FLT_MAX;
 +      } else {
 +              /*expand edge selection*/
 +              BM_ITER(v, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
 +                      e2 = NULL;
 +                      i = 0;
 +                      BM_ITER(e, &eiter, em->bm, BM_EDGES_OF_VERT, v) {
 +                              if (BMINDEX_GET(e)) {
 +                                      e2 = e;
 +                                      i++;
 +                              }
 +                      }
 +                      
 +                      if (i == 1 && e2->l) {
 +                              l = BM_OtherFaceLoop(e2, e2->l->f, v);
 +                              l = (BMLoop*)l->radial_next;
 +                              l = BM_OtherFaceLoop(l->e, l->f, v);
 +
 +                              if (l)
 +                                      BM_Select(em->bm, l->e, 1);
 +                      }
 +              }
 +      }
 +
 +      if (!EDBM_InitOpf(em, &bmop, op, "edgesplit edges=%he", BM_SELECT)) {
 +              return OPERATOR_CANCELLED;
 +      }
 +      
 +      BMO_Exec_Op(em->bm, &bmop);
 +
 +      /*build bvh tree for edge visibility tests*/
 +      bvhtree = BMBVH_NewBVH(em);
 +
 +      for (i=0; i<2; i++) {
 +              BMO_ITER(e, &siter, em->bm, &bmop, i ? "edgeout2":"edgeout1", BM_EDGE) {
 +                      float cent[3] = {0, 0, 0}, mid[4], vec[3];
 +
 +                      if (!BMBVH_EdgeVisible(bvhtree, e, ar, v3d, obedit) || !e->l)
 +                              continue;
 +
 +                      /*method for calculating distance:
 +                      
 +                        for each edge: calculate face center, then made a vector
 +                        from edge midpoint to face center.  offset edge midpoint
 +                        by a small amount along this vector.*/
 +                      BM_ITER(l, &liter, em->bm, BM_LOOPS_OF_FACE, e->l->f) {
 +                              add_v3_v3v3(cent, cent, l->v->co);
 +                      }
 +                      mul_v3_fl(cent, 1.0f/(float)e->l->f->len);
 +
 +                      add_v3_v3v3(mid, e->v1->co, e->v2->co);
 +                      mul_v3_fl(mid, 0.5f);
 +                      sub_v3_v3v3(vec, cent, mid);
 +                      normalize_v3(vec);
 +                      mul_v3_fl(vec, 0.01f);
 +                      add_v3_v3v3(mid, mid, vec);
 +
 +                      /*yay we have our comparison point, now project it*/
 +                      view3d_project_float(ar, mid, mid, projectMat);
 +
 +                      vec[0] = fmval[0] - mid[0];
 +                      vec[1] = fmval[1] - mid[1];
 +                      d = vec[0]*vec[0] + vec[1]*vec[1];
 +
 +                      if (d < dist) {
 +                              side = i;
 +                              closest = e;
 +                              dist = d;
 +                      }
 +              }
 +      }
 +
 +      EDBM_clear_flag_all(em, BM_SELECT);
 +      BMO_HeaderFlag_Buffer(em->bm, &bmop, side?"edgeout2":"edgeout1", BM_SELECT, BM_EDGE);
 +
 +      BM_ITER(e, &iter, em->bm, BM_EDGES_OF_MESH, NULL) {
 +              if (BM_TestHFlag(e, BM_SELECT))
 +                      BMINDEX_SET(e, 1);
 +              else BMINDEX_SET(e, 0);
 +      }
 +
 +      /*constrict edge selection again*/
 +      BM_ITER(v, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
 +              e2 = NULL;
 +              i = 0;
 +              BM_ITER(e, &eiter, em->bm, BM_EDGES_OF_VERT, v) {
 +                      if (BMINDEX_GET(e)) {
 +                              e2 = e;
 +                              i++;
 +                      }
 +              }
 +              
 +              if (i == 1)  {
 +                      if (singlesel)
 +                              BM_Select(em->bm, v, 0);
 +                      else
 +                              BM_Select(em->bm, e2, 0);
 +              }
 +      }
 +
 +      EDBM_selectmode_flush(em);
 +      
 +      if (!EDBM_FinishOp(em, &bmop, op, 1)) {
 +              BMBVH_FreeBVH(bvhtree);
 +              return OPERATOR_CANCELLED;
 +      }
 +      
 +      BMBVH_FreeBVH(bvhtree);
 +
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;
 +#if 0 //BMESH_TODO
 +      ARegion *ar= CTX_wm_region(C);
 +      RegionView3D *rv3d= ar->regiondata;
 +      Object *obedit= CTX_data_edit_object(C);
 +      EditMesh *em= BKE_mesh_get_editmesh((Mesh *)obedit->data);
 +      EditVert *eve, *nextve;
 +      EditEdge *eed, *seed= NULL;
 +      EditFace *efa, *sefa= NULL;
 +      float projectMat[4][4], vec[3], dist, mindist;
 +      short doit= 1, *mval= event->mval;
 +
 +      /* select flush... vertices are important */
 +      EM_selectmode_set(em);
 +
 +      view3d_get_object_project_mat(rv3d, obedit, projectMat);
 +
 +      /* find best face, exclude triangles and break on face select or faces with 2 edges select */
 +      mindist= 1000000.0f;
 +      for(efa= em->faces.first; efa; efa=efa->next) {
 +              if( efa->f & 1)
 +                      break;
 +              if(efa->v4 && faceselectedOR(efa, SELECT) ) {
 +                      int totsel=0;
 +
 +                      if(efa->e1->f & SELECT) totsel++;
 +                      if(efa->e2->f & SELECT) totsel++;
 +                      if(efa->e3->f & SELECT) totsel++;
 +                      if(efa->e4->f & SELECT) totsel++;
 +
 +                      if(totsel>1)
 +                              break;
 +                      view3d_project_float(ar, efa->cent, vec, projectMat);
 +                      dist= sqrt( (vec[0]-mval[0])*(vec[0]-mval[0]) + (vec[1]-mval[1])*(vec[1]-mval[1]) );
 +                      if(dist<mindist) {
 +                              mindist= dist;
 +                              sefa= efa;
 +                      }
 +              }
 +      }
 +
 +      if(efa) {
 +              BKE_report(op->reports, RPT_ERROR, "Can't perform ripping with faces selected this way");
 +              BKE_mesh_end_editmesh(obedit->data, em);
 +              return OPERATOR_CANCELLED;
 +      }
 +      if(sefa==NULL) {
 +              BKE_report(op->reports, RPT_ERROR, "No proper selection or faces included");
 +              BKE_mesh_end_editmesh(obedit->data, em);
 +              return OPERATOR_CANCELLED;
 +      }
 +
 +
 +      /* duplicate vertices, new vertices get selected */
 +      for(eve = em->vert