svn merge ^/trunk/blender -r46380:HEAD
[blender.git] / source / blender / editors / mesh / editmesh_tools.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2004 by Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Joseph Eagar
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/mesh/editmesh_tools.c
29  *  \ingroup edmesh
30  */
31
32 #include "MEM_guardedalloc.h"
33
34 #include "DNA_material_types.h"
35 #include "DNA_mesh_types.h"
36 #include "DNA_modifier_types.h"
37 #include "DNA_object_types.h"
38 #include "DNA_scene_types.h"
39
40 #include "RNA_define.h"
41 #include "RNA_access.h"
42
43 #include "BLI_blenlib.h"
44 #include "BLI_math.h"
45 #include "BLI_rand.h"
46
47 #include "BKE_material.h"
48 #include "BKE_context.h"
49 #include "BKE_cdderivedmesh.h"
50 #include "BKE_depsgraph.h"
51 #include "BKE_object.h"
52 #include "BKE_report.h"
53 #include "BKE_texture.h"
54 #include "BKE_main.h"
55 #include "BKE_tessmesh.h"
56
57 #include "WM_api.h"
58 #include "WM_types.h"
59
60 #include "ED_mesh.h"
61 #include "ED_object.h"
62 #include "ED_screen.h"
63 #include "ED_transform.h"
64 #include "ED_uvedit.h"
65 #include "ED_view3d.h"
66
67 #include "RE_render_ext.h"
68
69 #include "UI_interface.h"
70
71 #include "mesh_intern.h"
72
73 /* allow accumulated normals to form a new direction but don't
74  * accept direct opposite directions else they will cancel each other out */
75 static void add_normal_aligned(float nor[3], const float add[3])
76 {
77         if (dot_v3v3(nor, add) < -0.9999f) {
78                 sub_v3_v3(nor, add);
79         }
80         else {
81                 add_v3_v3(nor, add);
82         }
83 }
84
85 static int edbm_subdivide_exec(bContext *C, wmOperator *op)
86 {
87         Object *obedit = CTX_data_edit_object(C);
88         BMEditMesh *em = BMEdit_FromObject(obedit);
89         int cuts = RNA_int_get(op->ptr, "number_cuts");
90         float smooth = 0.292f * RNA_float_get(op->ptr, "smoothness");
91         float fractal = RNA_float_get(op->ptr, "fractal") / 2.5f;
92
93         if (RNA_boolean_get(op->ptr, "quadtri") && 
94             RNA_enum_get(op->ptr, "quadcorner") == SUBD_STRAIGHT_CUT)
95         {
96                 RNA_enum_set(op->ptr, "quadcorner", SUBD_INNERVERT);
97         }
98         
99         BM_mesh_esubdivide(em->bm, BM_ELEM_SELECT,
100                            smooth, fractal,
101                            cuts,
102                            SUBDIV_SELECT_ORIG, RNA_enum_get(op->ptr, "quadcorner"),
103                            RNA_boolean_get(op->ptr, "quadtri"), TRUE,
104                            RNA_int_get(op->ptr, "seed"));
105
106         EDBM_update_generic(C, em, TRUE);
107
108         return OPERATOR_FINISHED;
109 }
110
111 /* Note, these values must match delete_mesh() event values */
112 static EnumPropertyItem prop_mesh_cornervert_types[] = {
113         {SUBD_INNERVERT,     "INNERVERT", 0,      "Inner Vert", ""},
114         {SUBD_PATH,          "PATH", 0,           "Path", ""},
115         {SUBD_STRAIGHT_CUT,  "STRAIGHT_CUT", 0,   "Straight Cut", ""},
116         {SUBD_FAN,           "FAN", 0,            "Fan", ""},
117         {0, NULL, 0, NULL, NULL}
118 };
119
120 void MESH_OT_subdivide(wmOperatorType *ot)
121 {
122         PropertyRNA *prop;
123
124         /* identifiers */
125         ot->name = "Subdivide";
126         ot->description = "Subdivide selected edges";
127         ot->idname = "MESH_OT_subdivide";
128
129         /* api callbacks */
130         ot->exec = edbm_subdivide_exec;
131         ot->poll = ED_operator_editmesh;
132
133         /* flags */
134         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
135
136         /* properties */
137         prop = RNA_def_int(ot->srna, "number_cuts", 1, 1, INT_MAX, "Number of Cuts", "", 1, 10);
138         /* avoid re-using last var because it can cause _very_ high poly meshes and annoy users (or worse crash) */
139         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
140
141         RNA_def_float(ot->srna, "smoothness", 0.0f, 0.0f, FLT_MAX, "Smoothness", "Smoothness factor", 0.0f, 1.0f);
142
143         RNA_def_boolean(ot->srna, "quadtri", 0, "Quad/Tri Mode", "Tries to prevent ngons");
144         RNA_def_enum(ot->srna, "quadcorner", prop_mesh_cornervert_types, SUBD_STRAIGHT_CUT,
145                      "Quad Corner Type", "How to subdivide quad corners (anything other than Straight Cut will prevent ngons)");
146
147         RNA_def_float(ot->srna, "fractal", 0.0f, 0.0f, FLT_MAX, "Fractal", "Fractal randomness factor", 0.0f, 1000.0f);
148         RNA_def_int(ot->srna, "seed", 0, 0, 10000, "Random Seed", "Seed for the random number generator", 0, 50);
149 }
150
151
152 void EMBM_project_snap_verts(bContext *C, ARegion *ar, Object *obedit, BMEditMesh *em)
153 {
154         BMIter iter;
155         BMVert *eve;
156
157         BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
158                 if (BM_elem_flag_test(eve, BM_ELEM_SELECT)) {
159                         float mval[2], vec[3], no_dummy[3];
160                         int dist_dummy;
161                         mul_v3_m4v3(vec, obedit->obmat, eve->co);
162                         project_float_noclip(ar, vec, mval);
163                         if (snapObjectsContext(C, mval, &dist_dummy, vec, no_dummy, SNAP_NOT_OBEDIT)) {
164                                 mul_v3_m4v3(eve->co, obedit->imat, vec);
165                         }
166                 }
167         }
168 }
169
170
171 /* individual face extrude */
172 /* will use vertex normals for extrusion directions, so *nor is unaffected */
173 static short edbm_extrude_face_indiv(BMEditMesh *em, wmOperator *op, const char hflag, float *UNUSED(nor))
174 {
175         BMOIter siter;
176         BMIter liter;
177         BMFace *f;
178         BMLoop *l;
179         BMOperator bmop;
180
181         EDBM_op_init(em, &bmop, op, "extrude_face_indiv faces=%hf", hflag);
182
183         /* deselect original verts */
184         EDBM_flag_disable_all(em, BM_ELEM_SELECT);
185
186         BMO_op_exec(em->bm, &bmop);
187         
188         BMO_ITER (f, &siter, em->bm, &bmop, "faceout", BM_FACE) {
189                 BM_face_select_set(em->bm, f, TRUE);
190
191                 /* set face vertex normals to face normal */
192                 BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
193                         copy_v3_v3(l->v->no, f->no);
194                 }
195         }
196
197         if (!EDBM_op_finish(em, &bmop, op, TRUE)) {
198                 return 0;
199         }
200
201         return 's'; // s is shrink/fatten
202 }
203
204 /* extrudes individual edges */
205 static short edbm_extrude_edges_indiv(BMEditMesh *em, wmOperator *op, const char hflag, float *UNUSED(nor))
206 {
207         BMOperator bmop;
208
209         EDBM_op_init(em, &bmop, op, "extrude_edge_only edges=%he", hflag);
210
211         /* deselect original verts */
212         EDBM_flag_disable_all(em, BM_ELEM_SELECT);
213
214         BMO_op_exec(em->bm, &bmop);
215         BMO_slot_buffer_hflag_enable(em->bm, &bmop, "geomout", BM_VERT | BM_EDGE, BM_ELEM_SELECT, TRUE);
216
217         if (!EDBM_op_finish(em, &bmop, op, TRUE)) {
218                 return 0;
219         }
220
221         return 'n'; // n is normal grab
222 }
223
224 /* extrudes individual vertices */
225 static short edbm_extrude_verts_indiv(BMEditMesh *em, wmOperator *op, const char hflag, float *UNUSED(nor))
226 {
227         BMOperator bmop;
228
229         EDBM_op_init(em, &bmop, op, "extrude_vert_indiv verts=%hv", hflag);
230
231         /* deselect original verts */
232         BMO_slot_buffer_hflag_disable(em->bm, &bmop, "verts", BM_VERT, BM_ELEM_SELECT, TRUE);
233
234         BMO_op_exec(em->bm, &bmop);
235         BMO_slot_buffer_hflag_enable(em->bm, &bmop, "vertout", BM_VERT, BM_ELEM_SELECT, TRUE);
236
237         if (!EDBM_op_finish(em, &bmop, op, TRUE)) {
238                 return 0;
239         }
240
241         return 'g'; // g is grab
242 }
243
244 static short edbm_extrude_edge(Object *obedit, BMEditMesh *em, const char hflag, float nor[3])
245 {
246         BMesh *bm = em->bm;
247         BMIter iter;
248         BMOIter siter;
249         BMOperator extop;
250         BMEdge *edge;
251         BMFace *f;
252         ModifierData *md;
253         BMElem *ele;
254         
255         BMO_op_init(bm, &extop, "extrude_face_region");
256         BMO_slot_buffer_from_enabled_hflag(bm, &extop, "edgefacein", BM_VERT | BM_EDGE | BM_FACE, hflag);
257
258         /* If a mirror modifier with clipping is on, we need to adjust some 
259          * of the cases above to handle edges on the line of symmetry.
260          */
261         md = obedit->modifiers.first;
262         for (; md; md = md->next) {
263                 if ((md->type == eModifierType_Mirror) && (md->mode & eModifierMode_Realtime)) {
264                         MirrorModifierData *mmd = (MirrorModifierData *) md;
265                 
266                         if (mmd->flag & MOD_MIR_CLIPPING) {
267                                 float mtx[4][4];
268                                 if (mmd->mirror_ob) {
269                                         float imtx[4][4];
270                                         invert_m4_m4(imtx, mmd->mirror_ob->obmat);
271                                         mult_m4_m4m4(mtx, imtx, obedit->obmat);
272                                 }
273
274                                 for (edge = BM_iter_new(&iter, bm, BM_EDGES_OF_MESH, NULL);
275                                      edge;
276                                      edge = BM_iter_step(&iter))
277                                 {
278                                         if (BM_elem_flag_test(edge, hflag) &&
279                                             BM_edge_is_boundary(edge) &&
280                                             BM_elem_flag_test(edge->l->f, hflag))
281                                         {
282                                                 float co1[3], co2[3];
283
284                                                 copy_v3_v3(co1, edge->v1->co);
285                                                 copy_v3_v3(co2, edge->v2->co);
286
287                                                 if (mmd->mirror_ob) {
288                                                         mul_v3_m4v3(co1, mtx, co1);
289                                                         mul_v3_m4v3(co2, mtx, co2);
290                                                 }
291
292                                                 if (mmd->flag & MOD_MIR_AXIS_X) {
293                                                         if ((fabsf(co1[0]) < mmd->tolerance) &&
294                                                             (fabsf(co2[0]) < mmd->tolerance))
295                                                         {
296                                                                 BMO_slot_map_ptr_insert(bm, &extop, "exclude", edge, NULL);
297                                                         }
298                                                 }
299                                                 if (mmd->flag & MOD_MIR_AXIS_Y) {
300                                                         if ((fabsf(co1[1]) < mmd->tolerance) &&
301                                                             (fabsf(co2[1]) < mmd->tolerance))
302                                                         {
303                                                                 BMO_slot_map_ptr_insert(bm, &extop, "exclude", edge, NULL);
304                                                         }
305                                                 }
306                                                 if (mmd->flag & MOD_MIR_AXIS_Z) {
307                                                         if ((fabsf(co1[2]) < mmd->tolerance) &&
308                                                             (fabsf(co2[2]) < mmd->tolerance))
309                                                         {
310                                                                 BMO_slot_map_ptr_insert(bm, &extop, "exclude", edge, NULL);
311                                                         }
312                                                 }
313                                         }
314                                 }
315                         }
316                 }
317         }
318
319         EDBM_flag_disable_all(em, BM_ELEM_SELECT);
320
321         BMO_op_exec(bm, &extop);
322
323         zero_v3(nor);
324         
325         BMO_ITER (ele, &siter, bm, &extop, "geomout", BM_ALL) {
326                 BM_elem_select_set(bm, ele, TRUE);
327
328                 if (ele->head.htype == BM_FACE) {
329                         f = (BMFace *)ele;
330                         add_normal_aligned(nor, f->no);
331                 };
332         }
333
334         normalize_v3(nor);
335
336         BMO_op_finish(bm, &extop);
337
338         /* grab / normal constraint */
339         return is_zero_v3(nor) ? 'g' : 'n';
340 }
341
342 static short edbm_extrude_vert(Object *obedit, BMEditMesh *em, const char hflag, float nor[3])
343 {
344         BMIter iter;
345         BMEdge *eed;
346                 
347         /* ensure vert flags are consistent for edge selections */
348         BM_ITER_MESH (eed, &iter, em->bm, BM_EDGES_OF_MESH) {
349                 if (BM_elem_flag_test(eed, hflag)) {
350                         if (hflag & BM_ELEM_SELECT) {
351                                 BM_vert_select_set(em->bm, eed->v1, TRUE);
352                                 BM_vert_select_set(em->bm, eed->v2, TRUE);
353                         }
354
355                         BM_elem_flag_enable(eed->v1, hflag & ~BM_ELEM_SELECT);
356                         BM_elem_flag_enable(eed->v2, hflag & ~BM_ELEM_SELECT);
357                 }
358                 else {
359                         if (BM_elem_flag_test(eed->v1, hflag) && BM_elem_flag_test(eed->v2, hflag)) {
360                                 if (hflag & BM_ELEM_SELECT) {
361                                         BM_edge_select_set(em->bm, eed, TRUE);
362                                 }
363
364                                 BM_elem_flag_enable(eed, hflag & ~BM_ELEM_SELECT);
365                         }
366                 }
367         }
368
369         return edbm_extrude_edge(obedit, em, hflag, nor);
370 }
371
372 static int edbm_extrude_repeat_exec(bContext *C, wmOperator *op)
373 {
374         Object *obedit = CTX_data_edit_object(C);
375         BMEditMesh *em = BMEdit_FromObject(obedit);
376         RegionView3D *rv3d = CTX_wm_region_view3d(C);
377                 
378         int steps = RNA_int_get(op->ptr, "steps");
379         
380         float offs = RNA_float_get(op->ptr, "offset");
381         float dvec[3], tmat[3][3], bmat[3][3], nor[3] = {0.0, 0.0, 0.0};
382         short a;
383
384         /* dvec */
385         normalize_v3_v3(dvec, rv3d->persinv[2]);
386         mul_v3_fl(dvec, offs);
387
388         /* base correction */
389         copy_m3_m4(bmat, obedit->obmat);
390         invert_m3_m3(tmat, bmat);
391         mul_m3_v3(tmat, dvec);
392
393         for (a = 0; a < steps; a++) {
394                 edbm_extrude_edge(obedit, em, BM_ELEM_SELECT, nor);
395                 //BMO_op_callf(em->bm, "extrude_face_region edgefacein=%hef", BM_ELEM_SELECT);
396                 BMO_op_callf(em->bm, "translate vec=%v verts=%hv", (float *)dvec, BM_ELEM_SELECT);
397                 //extrudeflag(obedit, em, SELECT, nor);
398                 //translateflag(em, SELECT, dvec);
399         }
400         
401         EDBM_mesh_normals_update(em);
402
403         EDBM_update_generic(C, em, TRUE);
404
405         return OPERATOR_FINISHED;
406 }
407
408 void MESH_OT_extrude_repeat(wmOperatorType *ot)
409 {
410         /* identifiers */
411         ot->name = "Extrude Repeat Mesh";
412         ot->description = "Extrude selected vertices, edges or faces repeatedly";
413         ot->idname = "MESH_OT_extrude_repeat";
414         
415         /* api callbacks */
416         ot->exec = edbm_extrude_repeat_exec;
417         ot->poll = ED_operator_editmesh_view3d;
418         
419         /* flags */
420         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
421         
422         /* props */
423         RNA_def_float(ot->srna, "offset", 2.0f, 0.0f, 100.0f, "Offset", "", 0.0f, FLT_MAX);
424         RNA_def_int(ot->srna, "steps", 10, 0, 180, "Steps", "", 0, INT_MAX);
425 }
426
427 /* generic extern called extruder */
428 static int edbm_extrude_mesh(Scene *scene, Object *obedit, BMEditMesh *em, wmOperator *op, float *norin)
429 {
430         short nr, transmode = 0;
431         float stacknor[3] = {0.0f, 0.0f, 0.0f};
432         float *nor = norin ? norin : stacknor;
433
434         zero_v3(nor);
435
436         if (em->selectmode & SCE_SELECT_VERTEX) {
437                 if (em->bm->totvertsel == 0) nr = 0;
438                 else if (em->bm->totvertsel == 1) nr = 4;
439                 else if (em->bm->totedgesel == 0) nr = 4;
440                 else if (em->bm->totfacesel == 0)
441                         nr = 3;  // pupmenu("Extrude %t|Only Edges%x3|Only Vertices%x4");
442                 else if (em->bm->totfacesel == 1)
443                         nr = 1;  // pupmenu("Extrude %t|Region %x1|Only Edges%x3|Only Vertices%x4");
444                 else 
445                         nr = 1;  // pupmenu("Extrude %t|Region %x1||Individual Faces %x2|Only Edges%x3|Only Vertices%x4");
446         }
447         else if (em->selectmode & SCE_SELECT_EDGE) {
448                 if (em->bm->totedgesel == 0) nr = 0;
449                 
450                 nr = 1;
451 #if 0
452                 else if (em->totedgesel == 1) nr = 3;
453                 else if (em->totfacesel == 0) nr = 3;
454                 else if (em->totfacesel == 1)
455                         nr = 1;  // pupmenu("Extrude %t|Region %x1|Only Edges%x3");
456                 else
457                         nr = 1;  // pupmenu("Extrude %t|Region %x1||Individual Faces %x2|Only Edges%x3");
458 #endif
459         }
460         else {
461                 if (em->bm->totfacesel == 0) nr = 0;
462                 else if (em->bm->totfacesel == 1) nr = 1;
463                 else
464                         nr = 1;  // pupmenu("Extrude %t|Region %x1||Individual Faces %x2");
465         }
466
467         if (nr < 1) return 'g';
468
469         if (nr == 1 && (em->selectmode & SCE_SELECT_VERTEX))
470                 transmode = edbm_extrude_vert(obedit, em, BM_ELEM_SELECT, nor);
471         else if (nr == 1) transmode = edbm_extrude_edge(obedit, em, BM_ELEM_SELECT, nor);
472         else if (nr == 4) transmode = edbm_extrude_verts_indiv(em, op, BM_ELEM_SELECT, nor);
473         else if (nr == 3) transmode = edbm_extrude_edges_indiv(em, op, BM_ELEM_SELECT, nor);
474         else transmode = edbm_extrude_face_indiv(em, op, BM_ELEM_SELECT, nor);
475         
476         if (transmode == 0) {
477                 BKE_report(op->reports, RPT_ERROR, "Not a valid selection for extrude");
478         }
479         else {
480                 
481                 /* We need to force immediate calculation here because
482                  * transform may use derived objects (which are now stale).
483                  *
484                  * This shouldn't be necessary, derived queries should be
485                  * automatically building this data if invalid. Or something.
486                  */
487 //              DAG_object_flush_update(scene, obedit, OB_RECALC_DATA);
488                 BKE_object_handle_update(scene, obedit);
489
490                 /* individual faces? */
491 //              BIF_TransformSetUndo("Extrude");
492                 if (nr == 2) {
493 //                      initTransform(TFM_SHRINKFATTEN, CTX_NO_PET|CTX_NO_MIRROR);
494 //                      Transform();
495                 }
496                 else {
497 //                      initTransform(TFM_TRANSLATION, CTX_NO_PET|CTX_NO_MIRROR);
498                         if (transmode == 'n') {
499                                 mul_m4_v3(obedit->obmat, nor);
500                                 sub_v3_v3v3(nor, nor, obedit->obmat[3]);
501 //                              BIF_setSingleAxisConstraint(nor, "along normal");
502                         }
503 //                      Transform();
504                 }
505         }
506         
507         return transmode;
508 }
509
510 /* extrude without transform */
511 static int edbm_extrude_region_exec(bContext *C, wmOperator *op)
512 {
513         Scene *scene = CTX_data_scene(C);
514         Object *obedit = CTX_data_edit_object(C);
515         BMEditMesh *em = BMEdit_FromObject(obedit);
516         
517         edbm_extrude_mesh(scene, obedit, em, op, NULL);
518
519         /* This normally happens when pushing undo but modal operators
520          * like this one don't push undo data until after modal mode is
521          * done.*/
522         EDBM_mesh_normals_update(em);
523
524         EDBM_update_generic(C, em, TRUE);
525         
526         return OPERATOR_FINISHED;
527 }
528
529 void MESH_OT_extrude_region(wmOperatorType *ot)
530 {
531         /* identifiers */
532         ot->name = "Extrude Region";
533         ot->idname = "MESH_OT_extrude_region";
534         ot->description = "Extrude region of faces";
535         
536         /* api callbacks */
537         //ot->invoke = mesh_extrude_region_invoke;
538         ot->exec = edbm_extrude_region_exec;
539         ot->poll = ED_operator_editmesh;
540         
541         /* flags */
542         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
543
544         RNA_def_boolean(ot->srna, "mirror", 0, "Mirror Editing", "");
545 }
546
547 static int edbm_extrude_verts_exec(bContext *C, wmOperator *op)
548 {
549         Object *obedit = CTX_data_edit_object(C);
550         BMEditMesh *em = BMEdit_FromObject(obedit);
551         float nor[3];
552
553         edbm_extrude_verts_indiv(em, op, BM_ELEM_SELECT, nor);
554         
555         WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit);
556         
557         return OPERATOR_FINISHED;
558 }
559
560 void MESH_OT_extrude_verts_indiv(wmOperatorType *ot)
561 {
562         /* identifiers */
563         ot->name = "Extrude Only Vertices";
564         ot->idname = "MESH_OT_extrude_verts_indiv";
565         ot->description = "Extrude individual vertices only";
566         
567         /* api callbacks */
568         ot->exec = edbm_extrude_verts_exec;
569         ot->poll = ED_operator_editmesh;
570         
571         /* flags */
572         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
573
574         /* to give to transform */
575         RNA_def_boolean(ot->srna, "mirror", 0, "Mirror Editing", "");
576 }
577
578 static int edbm_extrude_edges_exec(bContext *C, wmOperator *op)
579 {
580         Object *obedit = CTX_data_edit_object(C);
581         BMEditMesh *em = BMEdit_FromObject(obedit);
582         float nor[3];
583
584         edbm_extrude_edges_indiv(em, op, BM_ELEM_SELECT, nor);
585         
586         WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit);
587         
588         return OPERATOR_FINISHED;
589 }
590
591 void MESH_OT_extrude_edges_indiv(wmOperatorType *ot)
592 {
593         /* identifiers */
594         ot->name = "Extrude Only Edges";
595         ot->idname = "MESH_OT_extrude_edges_indiv";
596         ot->description = "Extrude individual edges only";
597         
598         /* api callbacks */
599         ot->exec = edbm_extrude_edges_exec;
600         ot->poll = ED_operator_editmesh;
601         
602         /* flags */
603         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
604
605         /* to give to transform */
606         RNA_def_boolean(ot->srna, "mirror", 0, "Mirror Editing", "");
607 }
608
609 static int edbm_extrude_faces_exec(bContext *C, wmOperator *op)
610 {
611         Object *obedit = CTX_data_edit_object(C);
612         BMEditMesh *em = BMEdit_FromObject(obedit);
613         float nor[3];
614
615         edbm_extrude_face_indiv(em, op, BM_ELEM_SELECT, nor);
616         
617         EDBM_update_generic(C, em, TRUE);
618         
619         return OPERATOR_FINISHED;
620 }
621
622 void MESH_OT_extrude_faces_indiv(wmOperatorType *ot)
623 {
624         /* identifiers */
625         ot->name = "Extrude Individual Faces";
626         ot->idname = "MESH_OT_extrude_faces_indiv";
627         ot->description = "Extrude individual faces only";
628         
629         /* api callbacks */
630         ot->exec = edbm_extrude_faces_exec;
631         ot->poll = ED_operator_editmesh;
632         
633         /* flags */
634         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
635
636         RNA_def_boolean(ot->srna, "mirror", 0, "Mirror Editing", "");
637 }
638
639 /* ******************** (de)select all operator **************** */
640
641 static int edbm_select_all_exec(bContext *C, wmOperator *op)
642 {
643         Object *obedit = CTX_data_edit_object(C);
644         BMEditMesh *em = BMEdit_FromObject(obedit);
645         int action = RNA_enum_get(op->ptr, "action");
646         
647         switch (action) {
648                 case SEL_TOGGLE:
649                         EDBM_select_toggle_all(em);
650                         break;
651                 case SEL_SELECT:
652                         EDBM_flag_enable_all(em, BM_ELEM_SELECT);
653                         break;
654                 case SEL_DESELECT:
655                         EDBM_flag_disable_all(em, BM_ELEM_SELECT);
656                         break;
657                 case SEL_INVERT:
658                         EDBM_select_swap(em);
659                         EDBM_selectmode_flush(em);
660                         break;
661         }
662
663         WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit);
664
665         return OPERATOR_FINISHED;
666 }
667
668 void MESH_OT_select_all(wmOperatorType *ot)
669 {
670         /* identifiers */
671         ot->name = "(De)select All";
672         ot->idname = "MESH_OT_select_all";
673         ot->description = "(De)select all vertices, edges or faces";
674         
675         /* api callbacks */
676         ot->exec = edbm_select_all_exec;
677         ot->poll = ED_operator_editmesh;
678         
679         /* flags */
680         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
681
682         WM_operator_properties_select_all(ot);
683 }
684
685 static int edbm_faces_select_interior_exec(bContext *C, wmOperator *UNUSED(op))
686 {
687         Object *obedit = CTX_data_edit_object(C);
688         BMEditMesh *em = BMEdit_FromObject(obedit);
689
690         if (EDBM_select_interior_faces(em)) {
691                 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit);
692
693                 return OPERATOR_FINISHED;
694         }
695         else {
696                 return OPERATOR_CANCELLED;
697         }
698
699 }
700
701 void MESH_OT_select_interior_faces(wmOperatorType *ot)
702 {
703         /* identifiers */
704         ot->name = "Select Interior Faces";
705         ot->idname = "MESH_OT_select_interior_faces";
706         ot->description = "Select faces where all edges have more than 2 face users";
707
708         /* api callbacks */
709         ot->exec = edbm_faces_select_interior_exec;
710         ot->poll = ED_operator_editmesh;
711
712         /* flags */
713         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
714 }
715
716 /* *************** add-click-mesh (extrude) operator ************** */
717 static int edbm_dupli_extrude_cursor_invoke(bContext *C, wmOperator *op, wmEvent *event)
718 {
719         ViewContext vc;
720         BMVert *v1;
721         BMIter iter;
722         float min[3], max[3];
723         int done = 0;
724         short use_proj;
725         
726         em_setup_viewcontext(C, &vc);
727         
728         use_proj = ((vc.scene->toolsettings->snap_flag & SCE_SNAP) &&
729                     (vc.scene->toolsettings->snap_mode == SCE_SNAP_MODE_FACE));
730
731         INIT_MINMAX(min, max);
732         
733         BM_ITER_MESH (v1, &iter, vc.em->bm, BM_VERTS_OF_MESH) {
734                 if (BM_elem_flag_test(v1, BM_ELEM_SELECT)) {
735                         DO_MINMAX(v1->co, min, max);
736                         done = 1;
737                 }
738         }
739
740         /* call extrude? */
741         if (done) {
742                 const short rot_src = RNA_boolean_get(op->ptr, "rotate_source");
743                 BMEdge *eed;
744                 float vec[3], cent[3], mat[3][3];
745                 float nor[3] = {0.0, 0.0, 0.0};
746
747                 /* 2D normal calc */
748                 float mval_f[2];
749
750                 mval_f[0] = (float)event->mval[0];
751                 mval_f[1] = (float)event->mval[1];
752
753                 /* check for edges that are half selected, use for rotation */
754                 done = 0;
755                 BM_ITER_MESH (eed, &iter, vc.em->bm, BM_EDGES_OF_MESH) {
756                         if (BM_elem_flag_test(eed, BM_ELEM_SELECT)) {
757                                 float co1[3], co2[3];
758                                 mul_v3_m4v3(co1, vc.obedit->obmat, eed->v1->co);
759                                 mul_v3_m4v3(co2, vc.obedit->obmat, eed->v2->co);
760                                 project_float_noclip(vc.ar, co1, co1);
761                                 project_float_noclip(vc.ar, co2, co2);
762
763                                 /* 2D rotate by 90d while adding.
764                                  *  (x, y) = (y, -x)
765                                  *
766                                  * accumulate the screenspace normal in 2D,
767                                  * with screenspace edge length weighting the result. */
768                                 if (line_point_side_v2(co1, co2, mval_f) >= 0.0f) {
769                                         nor[0] +=  (co1[1] - co2[1]);
770                                         nor[1] += -(co1[0] - co2[0]);
771                                 }
772                                 else {
773                                         nor[0] +=  (co2[1] - co1[1]);
774                                         nor[1] += -(co2[0] - co1[0]);
775                                 }
776                         }
777                         done = 1;
778                 }
779
780                 if (done) {
781                         float view_vec[3], cross[3];
782
783                         /* convert the 2D nomal into 3D */
784                         mul_mat3_m4_v3(vc.rv3d->viewinv, nor); /* worldspace */
785                         mul_mat3_m4_v3(vc.obedit->imat, nor); /* local space */
786
787                         /* correct the normal to be aligned on the view plane */
788                         copy_v3_v3(view_vec, vc.rv3d->viewinv[2]);
789                         mul_mat3_m4_v3(vc.obedit->imat, view_vec);
790                         cross_v3_v3v3(cross, nor, view_vec);
791                         cross_v3_v3v3(nor, view_vec, cross);
792                         normalize_v3(nor);
793                 }
794                 
795                 /* center */
796                 mid_v3_v3v3(cent, min, max);
797                 copy_v3_v3(min, cent);
798
799                 mul_m4_v3(vc.obedit->obmat, min);  /* view space */
800                 view3d_get_view_aligned_coordinate(&vc, min, event->mval, TRUE);
801                 mul_m4_v3(vc.obedit->imat, min); // back in object space
802
803                 sub_v3_v3(min, cent);
804                 
805                 /* calculate rotation */
806                 unit_m3(mat);
807                 if (done) {
808                         float angle;
809
810                         normalize_v3_v3(vec, min);
811
812                         angle = angle_normalized_v3v3(vec, nor);
813
814                         if (angle != 0.0f) {
815                                 float axis[3];
816
817                                 cross_v3_v3v3(axis, nor, vec);
818
819                                 /* halve the rotation if its applied twice */
820                                 if (rot_src) {
821                                         angle *= 0.5f;
822                                 }
823
824                                 axis_angle_to_mat3(mat, axis, angle);
825                         }
826                 }
827                 
828                 if (rot_src) {
829                         EDBM_op_callf(vc.em, op, "rotate verts=%hv cent=%v mat=%m3",
830                                       BM_ELEM_SELECT, cent, mat);
831
832                         /* also project the source, for retopo workflow */
833                         if (use_proj)
834                                 EMBM_project_snap_verts(C, vc.ar, vc.obedit, vc.em);
835                 }
836
837                 edbm_extrude_edge(vc.obedit, vc.em, BM_ELEM_SELECT, nor);
838                 EDBM_op_callf(vc.em, op, "rotate verts=%hv cent=%v mat=%m3",
839                               BM_ELEM_SELECT, cent, mat);
840                 EDBM_op_callf(vc.em, op, "translate verts=%hv vec=%v",
841                               BM_ELEM_SELECT, min);
842         }
843         else {
844                 float *curs = give_cursor(vc.scene, vc.v3d);
845                 BMOperator bmop;
846                 BMOIter oiter;
847                 
848                 copy_v3_v3(min, curs);
849                 view3d_get_view_aligned_coordinate(&vc, min, event->mval, 0);
850
851                 invert_m4_m4(vc.obedit->imat, vc.obedit->obmat);
852                 mul_m4_v3(vc.obedit->imat, min); // back in object space
853                 
854                 EDBM_op_init(vc.em, &bmop, op, "makevert co=%v", min);
855                 BMO_op_exec(vc.em->bm, &bmop);
856
857                 BMO_ITER (v1, &oiter, vc.em->bm, &bmop, "newvertout", BM_VERT) {
858                         BM_vert_select_set(vc.em->bm, v1, TRUE);
859                 }
860
861                 if (!EDBM_op_finish(vc.em, &bmop, op, TRUE)) {
862                         return OPERATOR_CANCELLED;
863                 }
864         }
865
866         if (use_proj)
867                 EMBM_project_snap_verts(C, vc.ar, vc.obedit, vc.em);
868
869         /* This normally happens when pushing undo but modal operators
870          * like this one don't push undo data until after modal mode is
871          * done. */
872         EDBM_mesh_normals_update(vc.em);
873
874         EDBM_update_generic(C, vc.em, TRUE);
875
876         return OPERATOR_FINISHED;
877 }
878
879 void MESH_OT_dupli_extrude_cursor(wmOperatorType *ot)
880 {
881         /* identifiers */
882         ot->name = "Duplicate or Extrude at 3D Cursor";
883         ot->idname = "MESH_OT_dupli_extrude_cursor";
884         ot->description = "Duplicate and extrude selected vertices, edges or faces towards the mouse cursor";
885         
886         /* api callbacks */
887         ot->invoke = edbm_dupli_extrude_cursor_invoke;
888         ot->poll = ED_operator_editmesh;
889         
890         /* flags */
891         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
892
893         RNA_def_boolean(ot->srna, "rotate_source", 1, "Rotate Source", "Rotate initial selection giving better shape");
894 }
895
896 /* Note, these values must match delete_mesh() event values */
897 static EnumPropertyItem prop_mesh_delete_types[] = {
898         {0, "VERT",      0, "Vertices", ""},
899         {1,  "EDGE",      0, "Edges", ""},
900         {2,  "FACE",      0, "Faces", ""},
901         {3,  "EDGE_FACE", 0, "Only Edges & Faces", ""},
902         {4,  "ONLY_FACE", 0, "Only Faces", ""},
903         {0, NULL, 0, NULL, NULL}
904 };
905
906 static int edbm_delete_exec(bContext *C, wmOperator *op)
907 {
908         Object *obedit = CTX_data_edit_object(C);
909         BMEditMesh *em = BMEdit_FromObject(obedit);
910         int type = RNA_enum_get(op->ptr, "type");
911
912         if (type == 0) {
913                 if (!EDBM_op_callf(em, op, "del geom=%hv context=%i", BM_ELEM_SELECT, DEL_VERTS)) /* Erase Vertices */
914                         return OPERATOR_CANCELLED;
915         }
916         else if (type == 1) {
917                 if (!EDBM_op_callf(em, op, "del geom=%he context=%i", BM_ELEM_SELECT, DEL_EDGES)) /* Erase Edges */
918                         return OPERATOR_CANCELLED;
919         }
920         else if (type == 2) {
921                 if (!EDBM_op_callf(em, op, "del geom=%hf context=%i", BM_ELEM_SELECT, DEL_FACES)) /* Erase Faces */
922                         return OPERATOR_CANCELLED;
923         }
924         else if (type == 3) {
925                 if (!EDBM_op_callf(em, op, "del geom=%hef context=%i", BM_ELEM_SELECT, DEL_EDGESFACES)) /* Edges and Faces */
926                         return OPERATOR_CANCELLED;
927         }
928         else if (type == 4) {
929                 //"Erase Only Faces";
930                 if (!EDBM_op_callf(em, op, "del geom=%hf context=%i",
931                                    BM_ELEM_SELECT, DEL_ONLYFACES))
932                 {
933                         return OPERATOR_CANCELLED;
934                 }
935         }
936
937         EDBM_flag_disable_all(em, BM_ELEM_SELECT);
938
939         EDBM_update_generic(C, em, TRUE);
940         
941         return OPERATOR_FINISHED;
942 }
943
944 void MESH_OT_delete(wmOperatorType *ot)
945 {
946         /* identifiers */
947         ot->name = "Delete";
948         ot->description = "Delete selected vertices, edges or faces";
949         ot->idname = "MESH_OT_delete";
950         
951         /* api callbacks */
952         ot->invoke = WM_menu_invoke;
953         ot->exec = edbm_delete_exec;
954         
955         ot->poll = ED_operator_editmesh;
956         
957         /* flags */
958         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
959
960         /* props */
961         ot->prop = RNA_def_enum(ot->srna, "type", prop_mesh_delete_types, 0, "Type", "Method used for deleting mesh data");
962 }
963
964 static int edbm_collapse_edge_exec(bContext *C, wmOperator *op)
965 {
966         Object *obedit = CTX_data_edit_object(C);
967         BMEditMesh *em = BMEdit_FromObject(obedit);
968
969         if (!EDBM_op_callf(em, op, "collapse edges=%he", BM_ELEM_SELECT))
970                 return OPERATOR_CANCELLED;
971
972         EDBM_update_generic(C, em, TRUE);
973
974         return OPERATOR_FINISHED;
975 }
976
977 void MESH_OT_edge_collapse(wmOperatorType *ot)
978 {
979         /* identifiers */
980         ot->name = "Edge Collapse";
981         ot->description = "Collapse selected edges";
982         ot->idname = "MESH_OT_edge_collapse";
983
984         /* api callbacks */
985         ot->exec = edbm_collapse_edge_exec;
986         ot->poll = ED_operator_editmesh;
987
988         /* flags */
989         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
990 }
991
992 static int edbm_collapse_edge_loop_exec(bContext *C, wmOperator *op)
993 {
994         Object *obedit = CTX_data_edit_object(C);
995         BMEditMesh *em = BMEdit_FromObject(obedit);
996
997         if (!EDBM_op_callf(em, op, "dissolve_edge_loop edges=%he", BM_ELEM_SELECT))
998                 return OPERATOR_CANCELLED;
999
1000         EDBM_update_generic(C, em, TRUE);
1001
1002         return OPERATOR_FINISHED;
1003 }
1004
1005 void MESH_OT_edge_collapse_loop(wmOperatorType *ot)
1006 {
1007         /* identifiers */
1008         ot->name = "Edge Collapse Loop";
1009         ot->description = "Collapse selected edge loops";
1010         ot->idname = "MESH_OT_edge_collapse_loop";
1011
1012         /* api callbacks */
1013         ot->exec = edbm_collapse_edge_loop_exec;
1014         ot->poll = ED_operator_editmesh;
1015
1016         /* flags */
1017         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1018 }
1019
1020 static int edbm_add_edge_face__smooth_get(BMesh *bm)
1021 {
1022         BMEdge *e;
1023         BMIter iter;
1024
1025         unsigned int vote_on_smooth[2] = {0, 0};
1026
1027         BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
1028                 if (BM_elem_flag_test(e, BM_ELEM_SELECT) && e->l) {
1029                         vote_on_smooth[BM_elem_flag_test_bool(e->l->f, BM_ELEM_SMOOTH)]++;
1030                 }
1031         }
1032
1033         return (vote_on_smooth[0] < vote_on_smooth[1]);
1034 }
1035
1036 static int edbm_add_edge_face_exec(bContext *C, wmOperator *op)
1037 {
1038         BMOperator bmop;
1039         Object *obedit = CTX_data_edit_object(C);
1040         BMEditMesh *em = BMEdit_FromObject(obedit);
1041         const short use_smooth = edbm_add_edge_face__smooth_get(em->bm);
1042         /* when this is used to dissolve we could avoid this, but checking isnt too slow */
1043
1044         if (!EDBM_op_init(em, &bmop, op,
1045                           "contextual_create geom=%hfev mat_nr=%i use_smooth=%b",
1046                           BM_ELEM_SELECT, em->mat_nr, use_smooth))
1047         {
1048                 return OPERATOR_CANCELLED;
1049         }
1050         
1051         BMO_op_exec(em->bm, &bmop);
1052         BMO_slot_buffer_hflag_enable(em->bm, &bmop, "faceout", BM_FACE, BM_ELEM_SELECT, TRUE);
1053
1054         if (!EDBM_op_finish(em, &bmop, op, TRUE)) {
1055                 return OPERATOR_CANCELLED;
1056         }
1057
1058         EDBM_update_generic(C, em, TRUE);
1059         
1060         return OPERATOR_FINISHED;
1061 }
1062
1063 void MESH_OT_edge_face_add(wmOperatorType *ot)
1064 {
1065         /* identifiers */
1066         ot->name = "Make Edge/Face";
1067         ot->description = "Add an edge or face to selected";
1068         ot->idname = "MESH_OT_edge_face_add";
1069         
1070         /* api callbacks */
1071         ot->exec = edbm_add_edge_face_exec;
1072         ot->poll = ED_operator_editmesh;
1073         
1074         /* flags */
1075         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1076 }
1077
1078 /* ************************* SEAMS AND EDGES **************** */
1079
1080 static int edbm_mark_seam(bContext *C, wmOperator *op)
1081 {
1082         Scene *scene = CTX_data_scene(C);
1083         Object *obedit = CTX_data_edit_object(C);
1084         Mesh *me = ((Mesh *)obedit->data);
1085         BMEditMesh *em = BMEdit_FromObject(obedit);
1086         BMesh *bm = em->bm;
1087         BMEdge *eed;
1088         BMIter iter;
1089         int clear = RNA_boolean_get(op->ptr, "clear");
1090         
1091         /* auto-enable seams drawing */
1092         if (clear == 0) {
1093                 me->drawflag |= ME_DRAWSEAMS;
1094         }
1095
1096         if (clear) {
1097                 BM_ITER_MESH (eed, &iter, bm, BM_EDGES_OF_MESH) {
1098                         if (!BM_elem_flag_test(eed, BM_ELEM_SELECT) || BM_elem_flag_test(eed, BM_ELEM_HIDDEN))
1099                                 continue;
1100                         
1101                         BM_elem_flag_disable(eed, BM_ELEM_SEAM);
1102                 }
1103         }
1104         else {
1105                 BM_ITER_MESH (eed, &iter, bm, BM_EDGES_OF_MESH) {
1106                         if (!BM_elem_flag_test(eed, BM_ELEM_SELECT) || BM_elem_flag_test(eed, BM_ELEM_HIDDEN))
1107                                 continue;
1108                         BM_elem_flag_enable(eed, BM_ELEM_SEAM);
1109                 }
1110         }
1111
1112         ED_uvedit_live_unwrap(scene, obedit);
1113         EDBM_update_generic(C, em, TRUE);
1114
1115         return OPERATOR_FINISHED;
1116 }
1117
1118 void MESH_OT_mark_seam(wmOperatorType *ot)
1119 {
1120         /* identifiers */
1121         ot->name = "Mark Seam";
1122         ot->idname = "MESH_OT_mark_seam";
1123         ot->description = "(Un)mark selected edges as a seam";
1124         
1125         /* api callbacks */
1126         ot->exec = edbm_mark_seam;
1127         ot->poll = ED_operator_editmesh;
1128         
1129         /* flags */
1130         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1131         
1132         RNA_def_boolean(ot->srna, "clear", 0, "Clear", "");
1133 }
1134
1135 static int edbm_mark_sharp(bContext *C, wmOperator *op)
1136 {
1137         Object *obedit = CTX_data_edit_object(C);
1138         Mesh *me = ((Mesh *)obedit->data);
1139         BMEditMesh *em = BMEdit_FromObject(obedit);
1140         BMesh *bm = em->bm;
1141         BMEdge *eed;
1142         BMIter iter;
1143         int clear = RNA_boolean_get(op->ptr, "clear");
1144
1145         /* auto-enable sharp edge drawing */
1146         if (clear == 0) {
1147                 me->drawflag |= ME_DRAWSHARP;
1148         }
1149
1150         if (!clear) {
1151                 BM_ITER_MESH (eed, &iter, bm, BM_EDGES_OF_MESH) {
1152                         if (!BM_elem_flag_test(eed, BM_ELEM_SELECT) || BM_elem_flag_test(eed, BM_ELEM_HIDDEN))
1153                                 continue;
1154                         
1155                         BM_elem_flag_disable(eed, BM_ELEM_SMOOTH);
1156                 }
1157         }
1158         else {
1159                 BM_ITER_MESH (eed, &iter, bm, BM_EDGES_OF_MESH) {
1160                         if (!BM_elem_flag_test(eed, BM_ELEM_SELECT) || BM_elem_flag_test(eed, BM_ELEM_HIDDEN))
1161                                 continue;
1162                         
1163                         BM_elem_flag_enable(eed, BM_ELEM_SMOOTH);
1164                 }
1165         }
1166
1167         EDBM_update_generic(C, em, TRUE);
1168
1169         return OPERATOR_FINISHED;
1170 }
1171
1172 void MESH_OT_mark_sharp(wmOperatorType *ot)
1173 {
1174         /* identifiers */
1175         ot->name = "Mark Sharp";
1176         ot->idname = "MESH_OT_mark_sharp";
1177         ot->description = "(Un)mark selected edges as sharp";
1178         
1179         /* api callbacks */
1180         ot->exec = edbm_mark_sharp;
1181         ot->poll = ED_operator_editmesh;
1182         
1183         /* flags */
1184         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1185         
1186         RNA_def_boolean(ot->srna, "clear", 0, "Clear", "");
1187 }
1188
1189
1190 static int edbm_vert_connect(bContext *C, wmOperator *op)
1191 {
1192         Object *obedit = CTX_data_edit_object(C);
1193         BMEditMesh *em = BMEdit_FromObject(obedit);
1194         BMesh *bm = em->bm;
1195         BMOperator bmop;
1196         int len = 0;
1197         
1198         if (!EDBM_op_init(em, &bmop, op, "connectverts verts=%hv", BM_ELEM_SELECT)) {
1199                 return OPERATOR_CANCELLED;
1200         }
1201         BMO_op_exec(bm, &bmop);
1202         len = BMO_slot_get(&bmop, "edgeout")->len;
1203         if (!EDBM_op_finish(em, &bmop, op, TRUE)) {
1204                 return OPERATOR_CANCELLED;
1205         }
1206         
1207         EDBM_update_generic(C, em, TRUE);
1208
1209         return len ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
1210 }
1211
1212 void MESH_OT_vert_connect(wmOperatorType *ot)
1213 {
1214         /* identifiers */
1215         ot->name = "Vertex Connect";
1216         ot->idname = "MESH_OT_vert_connect";
1217         ot->description = "Connect 2 vertices of a face by an edge, splitting the face in two";
1218         
1219         /* api callbacks */
1220         ot->exec = edbm_vert_connect;
1221         ot->poll = ED_operator_editmesh;
1222         
1223         /* flags */
1224         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1225 }
1226
1227 static int edbm_edge_split_exec(bContext *C, wmOperator *op)
1228 {
1229         Object *obedit = CTX_data_edit_object(C);
1230         BMEditMesh *em = BMEdit_FromObject(obedit);
1231         BMesh *bm = em->bm;
1232         BMOperator bmop;
1233         int len = 0;
1234         
1235         if (!EDBM_op_init(em, &bmop, op, "edgesplit edges=%he", BM_ELEM_SELECT)) {
1236                 return OPERATOR_CANCELLED;
1237         }
1238         BMO_op_exec(bm, &bmop);
1239         len = BMO_slot_get(&bmop, "edgeout")->len;
1240         if (!EDBM_op_finish(em, &bmop, op, TRUE)) {
1241                 return OPERATOR_CANCELLED;
1242         }
1243         
1244         EDBM_update_generic(C, em, TRUE);
1245
1246         return len ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
1247 }
1248
1249 void MESH_OT_edge_split(wmOperatorType *ot)
1250 {
1251         /* identifiers */
1252         ot->name = "Edge Split";
1253         ot->idname = "MESH_OT_edge_split";
1254         ot->description = "Split selected edges so that each neighbor face gets its own copy";
1255         
1256         /* api callbacks */
1257         ot->exec = edbm_edge_split_exec;
1258         ot->poll = ED_operator_editmesh;
1259         
1260         /* flags */
1261         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1262 }
1263
1264 /****************** add duplicate operator ***************/
1265
1266 static int edbm_duplicate_exec(bContext *C, wmOperator *op)
1267 {
1268         Object *ob = CTX_data_edit_object(C);
1269         BMEditMesh *em = BMEdit_FromObject(ob);
1270         BMOperator bmop;
1271
1272         EDBM_op_init(em, &bmop, op, "dupe geom=%hvef", BM_ELEM_SELECT);
1273         
1274         BMO_op_exec(em->bm, &bmop);
1275         EDBM_flag_disable_all(em, BM_ELEM_SELECT);
1276
1277         BMO_slot_buffer_hflag_enable(em->bm, &bmop, "newout", BM_ALL, BM_ELEM_SELECT, TRUE);
1278
1279         if (!EDBM_op_finish(em, &bmop, op, TRUE)) {
1280                 return OPERATOR_CANCELLED;
1281         }
1282
1283         EDBM_update_generic(C, em, TRUE);
1284         
1285         return OPERATOR_FINISHED;
1286 }
1287
1288 static int edbm_duplicate_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1289 {
1290         WM_cursor_wait(1);
1291         edbm_duplicate_exec(C, op);
1292         WM_cursor_wait(0);
1293         
1294         return OPERATOR_FINISHED;
1295 }
1296
1297 void MESH_OT_duplicate(wmOperatorType *ot)
1298 {
1299         /* identifiers */
1300         ot->name = "Duplicate";
1301         ot->description = "Duplicate selected vertices, edges or faces";
1302         ot->idname = "MESH_OT_duplicate";
1303         
1304         /* api callbacks */
1305         ot->invoke = edbm_duplicate_invoke;
1306         ot->exec = edbm_duplicate_exec;
1307         
1308         ot->poll = ED_operator_editmesh;
1309         
1310         /* to give to transform */
1311         RNA_def_int(ot->srna, "mode", TFM_TRANSLATION, 0, INT_MAX, "Mode", "", 0, INT_MAX);
1312 }
1313
1314 static int edbm_flip_normals_exec(bContext *C, wmOperator *op)
1315 {
1316         Object *obedit = CTX_data_edit_object(C);
1317         BMEditMesh *em = BMEdit_FromObject(obedit);
1318         
1319         if (!EDBM_op_callf(em, op, "reversefaces faces=%hf", BM_ELEM_SELECT))
1320                 return OPERATOR_CANCELLED;
1321         
1322         EDBM_update_generic(C, em, TRUE);
1323
1324         return OPERATOR_FINISHED;
1325 }
1326
1327 void MESH_OT_flip_normals(wmOperatorType *ot)
1328 {
1329         /* identifiers */
1330         ot->name = "Flip Normals";
1331         ot->description = "Flip the direction of selected faces' normals (and of their vertices)";
1332         ot->idname = "MESH_OT_flip_normals";
1333         
1334         /* api callbacks */
1335         ot->exec = edbm_flip_normals_exec;
1336         ot->poll = ED_operator_editmesh;
1337         
1338         /* flags */
1339         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1340 }
1341
1342 static const EnumPropertyItem direction_items[] = {
1343         {DIRECTION_CW, "CW", 0, "Clockwise", ""},
1344         {DIRECTION_CCW, "CCW", 0, "Counter Clockwise", ""},
1345         {0, NULL, 0, NULL, NULL}};
1346
1347 /* only accepts 1 selected edge, or 2 selected faces */
1348 static int edbm_edge_rotate_selected_exec(bContext *C, wmOperator *op)
1349 {
1350         Object *obedit = CTX_data_edit_object(C);
1351         BMEditMesh *em = BMEdit_FromObject(obedit);
1352         BMOperator bmop;
1353         BMEdge *eed;
1354         BMIter iter;
1355         const int do_ccw = RNA_enum_get(op->ptr, "direction") == 1;
1356         int tot = 0;
1357
1358         if (em->bm->totedgesel == 0) {
1359                 BKE_report(op->reports, RPT_ERROR, "Select edges or face pairs for edge loops to rotate about");
1360                 return OPERATOR_CANCELLED;
1361         }
1362
1363         /* first see if we have two adjacent faces */
1364         BM_ITER_MESH (eed, &iter, em->bm, BM_EDGES_OF_MESH) {
1365                 BM_elem_flag_disable(eed, BM_ELEM_TAG);
1366                 if (BM_elem_flag_test(eed, BM_ELEM_SELECT)) {
1367                         BMFace *fa, *fb;
1368                         if (BM_edge_face_pair(eed, &fa, &fb)) {
1369                                 /* if both faces are selected we rotate between them,
1370                                  * otherwise - rotate between 2 unselected - but not mixed */
1371                                 if (BM_elem_flag_test(fa, BM_ELEM_SELECT) == BM_elem_flag_test(fb, BM_ELEM_SELECT)) {
1372                                         BM_elem_flag_enable(eed, BM_ELEM_TAG);
1373                                         tot++;
1374                                 }
1375                         }
1376                 }
1377         }
1378         
1379         /* ok, we don't have two adjacent faces, but we do have two selected ones.
1380          * that's an error condition.*/
1381         if (tot == 0) {
1382                 BKE_report(op->reports, RPT_ERROR, "Could not find any selected edges that can be rotated");
1383                 return OPERATOR_CANCELLED;
1384         }
1385         
1386         EDBM_op_init(em, &bmop, op, "edgerotate edges=%he ccw=%b", BM_ELEM_TAG, do_ccw);
1387
1388         /* avoids leaving old verts selected which can be a problem running multiple times,
1389          * since this means the edges become selected around the face which then attempt to rotate */
1390         BMO_slot_buffer_hflag_disable(em->bm, &bmop, "edges", BM_EDGE, BM_ELEM_SELECT, TRUE);
1391
1392         BMO_op_exec(em->bm, &bmop);
1393         /* edges may rotate into hidden vertices, if this does _not_ run we get an ilogical state */
1394         BMO_slot_buffer_hflag_disable(em->bm, &bmop, "edgeout", BM_EDGE, BM_ELEM_HIDDEN, TRUE);
1395         BMO_slot_buffer_hflag_enable(em->bm, &bmop, "edgeout", BM_EDGE, BM_ELEM_SELECT, TRUE);
1396         EDBM_selectmode_flush(em);
1397
1398         if (!EDBM_op_finish(em, &bmop, op, TRUE)) {
1399                 return OPERATOR_CANCELLED;
1400         }
1401
1402         EDBM_update_generic(C, em, TRUE);
1403
1404         return OPERATOR_FINISHED;
1405 }
1406
1407 void MESH_OT_edge_rotate(wmOperatorType *ot)
1408 {
1409         /* identifiers */
1410         ot->name = "Rotate Selected Edge";
1411         ot->description = "Rotate selected edge or adjoining faces";
1412         ot->idname = "MESH_OT_edge_rotate";
1413
1414         /* api callbacks */
1415         ot->exec = edbm_edge_rotate_selected_exec;
1416         ot->poll = ED_operator_editmesh;
1417
1418         /* flags */
1419         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1420
1421         /* props */
1422         RNA_def_enum(ot->srna, "direction", direction_items, DIRECTION_CW, "Direction", "Direction to rotate edge around");
1423 }
1424
1425
1426 static int edbm_hide_exec(bContext *C, wmOperator *op)
1427 {
1428         Object *obedit = CTX_data_edit_object(C);
1429         BMEditMesh *em = BMEdit_FromObject(obedit);
1430         
1431         EDBM_mesh_hide(em, RNA_boolean_get(op->ptr, "unselected"));
1432
1433         EDBM_update_generic(C, em, TRUE);
1434
1435         return OPERATOR_FINISHED;
1436 }
1437
1438 void MESH_OT_hide(wmOperatorType *ot)
1439 {
1440         /* identifiers */
1441         ot->name = "Hide Selection";
1442         ot->idname = "MESH_OT_hide";
1443         ot->description = "Hide (un)selected vertices, edges or faces";
1444         
1445         /* api callbacks */
1446         ot->exec = edbm_hide_exec;
1447         ot->poll = ED_operator_editmesh;
1448
1449         /* flags */
1450         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1451         
1452         /* props */
1453         RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected");
1454 }
1455
1456 static int edbm_reveal_exec(bContext *C, wmOperator *UNUSED(op))
1457 {
1458         Object *obedit = CTX_data_edit_object(C);
1459         BMEditMesh *em = BMEdit_FromObject(obedit);
1460         
1461         EDBM_mesh_reveal(em);
1462
1463         EDBM_update_generic(C, em, TRUE);
1464
1465         return OPERATOR_FINISHED;
1466 }
1467
1468 void MESH_OT_reveal(wmOperatorType *ot)
1469 {
1470         /* identifiers */
1471         ot->name = "Reveal Hidden";
1472         ot->idname = "MESH_OT_reveal";
1473         ot->description = "Reveal all hidden vertices, edges and faces";
1474         
1475         /* api callbacks */
1476         ot->exec = edbm_reveal_exec;
1477         ot->poll = ED_operator_editmesh;
1478         
1479         /* flags */
1480         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1481 }
1482
1483 static int edbm_normals_make_consistent_exec(bContext *C, wmOperator *op)
1484 {
1485         Object *obedit = CTX_data_edit_object(C);
1486         BMEditMesh *em = BMEdit_FromObject(obedit);
1487         
1488         /* doflip has to do with bmesh_rationalize_normals, it's an internal
1489          * thing */
1490         if (!EDBM_op_callf(em, op, "righthandfaces faces=%hf do_flip=%b", BM_ELEM_SELECT, TRUE))
1491                 return OPERATOR_CANCELLED;
1492
1493         if (RNA_boolean_get(op->ptr, "inside"))
1494                 EDBM_op_callf(em, op, "reversefaces faces=%hf", BM_ELEM_SELECT);
1495
1496         EDBM_update_generic(C, em, TRUE);
1497
1498         return OPERATOR_FINISHED;
1499 }
1500
1501 void MESH_OT_normals_make_consistent(wmOperatorType *ot)
1502 {
1503         /* identifiers */
1504         ot->name = "Make Normals Consistent";
1505         ot->description = "Make face and vertex normals point either outside or inside the mesh";
1506         ot->idname = "MESH_OT_normals_make_consistent";
1507         
1508         /* api callbacks */
1509         ot->exec = edbm_normals_make_consistent_exec;
1510         ot->poll = ED_operator_editmesh;
1511         
1512         /* flags */
1513         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1514         
1515         RNA_def_boolean(ot->srna, "inside", 0, "Inside", "");
1516 }
1517
1518
1519
1520 static int edbm_do_smooth_vertex_exec(bContext *C, wmOperator *op)
1521 {
1522         Object *obedit = CTX_data_edit_object(C);
1523         BMEditMesh *em = BMEdit_FromObject(obedit);
1524         ModifierData *md;
1525         int mirrx = FALSE, mirry = FALSE, mirrz = FALSE;
1526         int i, repeat;
1527         float clipdist = 0.0f;
1528
1529         /* mirror before smooth */
1530         if (((Mesh *)obedit->data)->editflag & ME_EDIT_MIRROR_X) {
1531                 EDBM_verts_mirror_cache_begin(em, TRUE);
1532         }
1533
1534         /* if there is a mirror modifier with clipping, flag the verts that
1535          * are within tolerance of the plane(s) of reflection 
1536          */
1537         for (md = obedit->modifiers.first; md; md = md->next) {
1538                 if (md->type == eModifierType_Mirror && (md->mode & eModifierMode_Realtime)) {
1539                         MirrorModifierData *mmd = (MirrorModifierData *)md;
1540                 
1541                         if (mmd->flag & MOD_MIR_CLIPPING) {
1542                                 if (mmd->flag & MOD_MIR_AXIS_X)
1543                                         mirrx = TRUE;
1544                                 if (mmd->flag & MOD_MIR_AXIS_Y)
1545                                         mirry = TRUE;
1546                                 if (mmd->flag & MOD_MIR_AXIS_Z)
1547                                         mirrz = TRUE;
1548
1549                                 clipdist = mmd->tolerance;
1550                         }
1551                 }
1552         }
1553
1554         repeat = RNA_int_get(op->ptr, "repeat");
1555         if (!repeat)
1556                 repeat = 1;
1557         
1558         for (i = 0; i < repeat; i++) {
1559                 if (!EDBM_op_callf(em, op,
1560                                    "vertexsmooth verts=%hv mirror_clip_x=%b mirror_clip_y=%b mirror_clip_z=%b clipdist=%f",
1561                                    BM_ELEM_SELECT, mirrx, mirry, mirrz, clipdist))
1562                 {
1563                         return OPERATOR_CANCELLED;
1564                 }
1565         }
1566
1567         /* apply mirror */
1568         if (((Mesh *)obedit->data)->editflag & ME_EDIT_MIRROR_X) {
1569                 EDBM_verts_mirror_apply(em, BM_ELEM_SELECT, 0);
1570                 EDBM_verts_mirror_cache_end(em);
1571         }
1572
1573         EDBM_update_generic(C, em, TRUE);
1574
1575         return OPERATOR_FINISHED;
1576 }       
1577         
1578 void MESH_OT_vertices_smooth(wmOperatorType *ot)
1579 {
1580         /* identifiers */
1581         ot->name = "Smooth Vertex";
1582         ot->description = "Flatten angles of selected vertices";
1583         ot->idname = "MESH_OT_vertices_smooth";
1584         
1585         /* api callbacks */
1586         ot->exec = edbm_do_smooth_vertex_exec;
1587         ot->poll = ED_operator_editmesh;
1588         
1589         /* flags */
1590         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1591
1592         RNA_def_int(ot->srna, "repeat", 1, 1, 100, "Number of times to smooth the mesh", "", 1, INT_MAX);
1593 }
1594
1595 /********************** Smooth/Solid Operators *************************/
1596
1597 static void mesh_set_smooth_faces(BMEditMesh *em, short smooth)
1598 {
1599         BMIter iter;
1600         BMFace *efa;
1601
1602         if (em == NULL) return;
1603         
1604         BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
1605                 if (BM_elem_flag_test(efa, BM_ELEM_SELECT)) {
1606                         BM_elem_flag_set(efa, BM_ELEM_SMOOTH, smooth);
1607                 }
1608         }
1609 }
1610
1611 static int edbm_faces_shade_smooth_exec(bContext *C, wmOperator *UNUSED(op))
1612 {
1613         Object *obedit = CTX_data_edit_object(C);
1614         BMEditMesh *em = BMEdit_FromObject(obedit);
1615
1616         mesh_set_smooth_faces(em, 1);
1617
1618         EDBM_update_generic(C, em, FALSE);
1619
1620         return OPERATOR_FINISHED;
1621 }
1622
1623 void MESH_OT_faces_shade_smooth(wmOperatorType *ot)
1624 {
1625         /* identifiers */
1626         ot->name = "Shade Smooth";
1627         ot->description = "Display faces smooth (using vertex normals)";
1628         ot->idname = "MESH_OT_faces_shade_smooth";
1629
1630         /* api callbacks */
1631         ot->exec = edbm_faces_shade_smooth_exec;
1632         ot->poll = ED_operator_editmesh;
1633
1634         /* flags */
1635         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1636 }
1637
1638 static int edbm_faces_shade_flat_exec(bContext *C, wmOperator *UNUSED(op))
1639 {
1640         Object *obedit = CTX_data_edit_object(C);
1641         BMEditMesh *em = BMEdit_FromObject(obedit);
1642
1643         mesh_set_smooth_faces(em, 0);
1644
1645         EDBM_update_generic(C, em, FALSE);
1646
1647         return OPERATOR_FINISHED;
1648 }
1649
1650 void MESH_OT_faces_shade_flat(wmOperatorType *ot)
1651 {
1652         /* identifiers */
1653         ot->name = "Shade Flat";
1654         ot->description = "Display faces flat";
1655         ot->idname = "MESH_OT_faces_shade_flat";
1656
1657         /* api callbacks */
1658         ot->exec = edbm_faces_shade_flat_exec;
1659         ot->poll = ED_operator_editmesh;
1660
1661         /* flags */
1662         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1663 }
1664
1665
1666 /********************** UV/Color Operators *************************/
1667
1668 static int edbm_rotate_uvs_exec(bContext *C, wmOperator *op)
1669 {
1670         Object *ob = CTX_data_edit_object(C);
1671         BMEditMesh *em = BMEdit_FromObject(ob);
1672         BMOperator bmop;
1673
1674         /* get the direction from RNA */
1675         int dir = RNA_enum_get(op->ptr, "direction");
1676
1677         /* initialize the bmop using EDBM api, which does various ui error reporting and other stuff */
1678         EDBM_op_init(em, &bmop, op, "face_rotateuvs faces=%hf dir=%i", BM_ELEM_SELECT, dir);
1679
1680         /* execute the operator */
1681         BMO_op_exec(em->bm, &bmop);
1682
1683         /* finish the operator */
1684         if (!EDBM_op_finish(em, &bmop, op, TRUE)) {
1685                 return OPERATOR_CANCELLED;
1686         }
1687
1688         EDBM_update_generic(C, em, FALSE);
1689
1690         /* we succeeded */
1691         return OPERATOR_FINISHED;
1692 }
1693
1694 static int edbm_reverse_uvs_exec(bContext *C, wmOperator *op)
1695 {
1696         Object *ob = CTX_data_edit_object(C);
1697         BMEditMesh *em = BMEdit_FromObject(ob);
1698         BMOperator bmop;
1699
1700         /* initialize the bmop using EDBM api, which does various ui error reporting and other stuff */
1701         EDBM_op_init(em, &bmop, op, "face_reverseuvs faces=%hf", BM_ELEM_SELECT);
1702
1703         /* execute the operator */
1704         BMO_op_exec(em->bm, &bmop);
1705
1706         /* finish the operator */
1707         if (!EDBM_op_finish(em, &bmop, op, TRUE)) {
1708                 return OPERATOR_CANCELLED;
1709         }
1710
1711         EDBM_update_generic(C, em, FALSE);
1712
1713         /* we succeeded */
1714         return OPERATOR_FINISHED;
1715 }
1716
1717 static int edbm_rotate_colors_exec(bContext *C, wmOperator *op)
1718 {
1719         Object *ob = CTX_data_edit_object(C);
1720         BMEditMesh *em = BMEdit_FromObject(ob);
1721         BMOperator bmop;
1722
1723         /* get the direction from RNA */
1724         int dir = RNA_enum_get(op->ptr, "direction");
1725
1726         /* initialize the bmop using EDBM api, which does various ui error reporting and other stuff */
1727         EDBM_op_init(em, &bmop, op, "face_rotatecolors faces=%hf dir=%i", BM_ELEM_SELECT, dir);
1728
1729         /* execute the operator */
1730         BMO_op_exec(em->bm, &bmop);
1731
1732         /* finish the operator */
1733         if (!EDBM_op_finish(em, &bmop, op, TRUE)) {
1734                 return OPERATOR_CANCELLED;
1735         }
1736
1737         /* dependencies graph and notification stuff */
1738         EDBM_update_generic(C, em, FALSE);
1739
1740         /* we succeeded */
1741         return OPERATOR_FINISHED;
1742 }
1743
1744
1745 static int edbm_reverse_colors_exec(bContext *C, wmOperator *op)
1746 {
1747         Object *ob = CTX_data_edit_object(C);
1748         BMEditMesh *em = BMEdit_FromObject(ob);
1749         BMOperator bmop;
1750
1751         /* initialize the bmop using EDBM api, which does various ui error reporting and other stuff */
1752         EDBM_op_init(em, &bmop, op, "face_reversecolors faces=%hf", BM_ELEM_SELECT);
1753
1754         /* execute the operator */
1755         BMO_op_exec(em->bm, &bmop);
1756
1757         /* finish the operator */
1758         if (!EDBM_op_finish(em, &bmop, op, TRUE)) {
1759                 return OPERATOR_CANCELLED;
1760         }
1761
1762         EDBM_update_generic(C, em, FALSE);
1763
1764         /* we succeeded */
1765         return OPERATOR_FINISHED;
1766 }
1767
1768 void MESH_OT_uvs_rotate(wmOperatorType *ot)
1769 {
1770         /* identifiers */
1771         ot->name = "Rotate UVs";
1772         ot->idname = "MESH_OT_uvs_rotate";
1773         ot->description = "Rotate UV coordinates inside faces";
1774
1775         /* api callbacks */
1776         ot->exec = edbm_rotate_uvs_exec;
1777         ot->poll = ED_operator_editmesh;
1778
1779         /* flags */
1780         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1781
1782         /* props */
1783         RNA_def_enum(ot->srna, "direction", direction_items, DIRECTION_CW, "Direction", "Direction to rotate UVs around");
1784 }
1785
1786 //void MESH_OT_uvs_mirror(wmOperatorType *ot)
1787 void MESH_OT_uvs_reverse(wmOperatorType *ot)
1788 {
1789         /* identifiers */
1790         ot->name = "Reverse UVs";
1791         ot->idname = "MESH_OT_uvs_reverse";
1792         ot->description = "Flip direction of UV coordinates inside faces";
1793
1794         /* api callbacks */
1795         ot->exec = edbm_reverse_uvs_exec;
1796         ot->poll = ED_operator_editmesh;
1797
1798         /* flags */
1799         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1800
1801         /* props */
1802         //RNA_def_enum(ot->srna, "axis", axis_items, DIRECTION_CW, "Axis", "Axis to mirror UVs around");
1803 }
1804
1805 void MESH_OT_colors_rotate(wmOperatorType *ot)
1806 {
1807         /* identifiers */
1808         ot->name = "Rotate Colors";
1809         ot->idname = "MESH_OT_colors_rotate";
1810         ot->description = "Rotate vertex colors inside faces";
1811
1812         /* api callbacks */
1813         ot->exec = edbm_rotate_colors_exec;
1814         ot->poll = ED_operator_editmesh;
1815
1816         /* flags */
1817         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1818
1819         /* props */
1820         RNA_def_enum(ot->srna, "direction", direction_items, DIRECTION_CCW, "Direction", "Direction to rotate edge around");
1821 }
1822
1823 void MESH_OT_colors_reverse(wmOperatorType *ot)
1824 {
1825         /* identifiers */
1826         ot->name = "Reverse Colors";
1827         ot->idname = "MESH_OT_colors_reverse";
1828         ot->description = "Flip direction of vertex colors inside faces";
1829
1830         /* api callbacks */
1831         ot->exec = edbm_reverse_colors_exec;
1832         ot->poll = ED_operator_editmesh;
1833
1834         /* flags */
1835         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1836
1837         /* props */
1838         //RNA_def_enum(ot->srna, "axis", axis_items, DIRECTION_CW, "Axis", "Axis to mirror colors around");
1839 }
1840
1841
1842 static int merge_firstlast(BMEditMesh *em, int first, int uvmerge, wmOperator *wmop)
1843 {
1844         BMVert *mergevert;
1845         BMEditSelection *ese;
1846
1847         /* do sanity check in mergemenu in edit.c ?*/
1848         if (first == 0) {
1849                 ese = em->bm->selected.last;
1850                 mergevert = (BMVert *)ese->ele;
1851         }
1852         else {
1853                 ese = em->bm->selected.first;
1854                 mergevert = (BMVert *)ese->ele;
1855         }
1856
1857         if (!BM_elem_flag_test(mergevert, BM_ELEM_SELECT))
1858                 return OPERATOR_CANCELLED;
1859         
1860         if (uvmerge) {
1861                 if (!EDBM_op_callf(em, wmop, "pointmerge_facedata verts=%hv snapv=%e", BM_ELEM_SELECT, mergevert))
1862                         return OPERATOR_CANCELLED;
1863         }
1864
1865         if (!EDBM_op_callf(em, wmop, "pointmerge verts=%hv mergeco=%v", BM_ELEM_SELECT, mergevert->co))
1866                 return OPERATOR_CANCELLED;
1867
1868         return OPERATOR_FINISHED;
1869 }
1870
1871 static int merge_target(BMEditMesh *em, Scene *scene, View3D *v3d, Object *ob, 
1872                         int target, int uvmerge, wmOperator *wmop)
1873 {
1874         BMIter iter;
1875         BMVert *v;
1876         float *vco = NULL, co[3], cent[3] = {0.0f, 0.0f, 0.0f};
1877
1878         if (target) {
1879                 vco = give_cursor(scene, v3d);
1880                 copy_v3_v3(co, vco);
1881                 mul_m4_v3(ob->imat, co);
1882         }
1883         else {
1884                 float fac;
1885                 int i = 0;
1886                 BM_ITER_MESH (v, &iter, em->bm, BM_VERTS_OF_MESH) {
1887                         if (!BM_elem_flag_test(v, BM_ELEM_SELECT))
1888                                 continue;
1889                         add_v3_v3(cent, v->co);
1890                         i++;
1891                 }
1892                 
1893                 if (!i)
1894                         return OPERATOR_CANCELLED;
1895
1896                 fac = 1.0f / (float)i;
1897                 mul_v3_fl(cent, fac);
1898                 copy_v3_v3(co, cent);
1899                 vco = co;
1900         }
1901
1902         if (!vco)
1903                 return OPERATOR_CANCELLED;
1904         
1905         if (uvmerge) {
1906                 if (!EDBM_op_callf(em, wmop, "vert_average_facedata verts=%hv", BM_ELEM_SELECT))
1907                         return OPERATOR_CANCELLED;
1908         }
1909
1910         if (!EDBM_op_callf(em, wmop, "pointmerge verts=%hv mergeco=%v", BM_ELEM_SELECT, co))
1911                 return OPERATOR_CANCELLED;
1912
1913         return OPERATOR_FINISHED;
1914 }
1915
1916 static int edbm_merge_exec(bContext *C, wmOperator *op)
1917 {
1918         Scene *scene = CTX_data_scene(C);
1919         View3D *v3d = CTX_wm_view3d(C);
1920         Object *obedit = CTX_data_edit_object(C);
1921         BMEditMesh *em = BMEdit_FromObject(obedit);
1922         int status = 0, uvs = RNA_boolean_get(op->ptr, "uvs");
1923
1924         switch (RNA_enum_get(op->ptr, "type")) {
1925                 case 3:
1926                         status = merge_target(em, scene, v3d, obedit, 0, uvs, op);
1927                         break;
1928                 case 4:
1929                         status = merge_target(em, scene, v3d, obedit, 1, uvs, op);
1930                         break;
1931                 case 1:
1932                         status = merge_firstlast(em, 0, uvs, op);
1933                         break;
1934                 case 6:
1935                         status = merge_firstlast(em, 1, uvs, op);
1936                         break;
1937                 case 5:
1938                         status = 1;
1939                         if (!EDBM_op_callf(em, op, "collapse edges=%he", BM_ELEM_SELECT))
1940                                 status = 0;
1941                         break;
1942         }
1943
1944         if (!status)
1945                 return OPERATOR_CANCELLED;
1946
1947         EDBM_update_generic(C, em, TRUE);
1948
1949         return OPERATOR_FINISHED;
1950 }
1951
1952 static EnumPropertyItem merge_type_items[] = {
1953         {6, "FIRST", 0, "At First", ""},
1954         {1, "LAST", 0, "At Last", ""},
1955         {3, "CENTER", 0, "At Center", ""},
1956         {4, "CURSOR", 0, "At Cursor", ""},
1957         {5, "COLLAPSE", 0, "Collapse", ""},
1958         {0, NULL, 0, NULL, NULL}};
1959
1960 static EnumPropertyItem *merge_type_itemf(bContext *C, PointerRNA *UNUSED(ptr),  PropertyRNA *UNUSED(prop), int *free)
1961 {       
1962         Object *obedit;
1963         EnumPropertyItem *item = NULL;
1964         int totitem = 0;
1965         
1966         if (!C) /* needed for docs */
1967                 return merge_type_items;
1968         
1969         obedit = CTX_data_edit_object(C);
1970         if (obedit && obedit->type == OB_MESH) {
1971                 BMEditMesh *em = BMEdit_FromObject(obedit);
1972
1973                 if (em->selectmode & SCE_SELECT_VERTEX) {
1974                         if (em->bm->selected.first && em->bm->selected.last &&
1975                             ((BMEditSelection *)em->bm->selected.first)->htype == BM_VERT &&
1976                             ((BMEditSelection *)em->bm->selected.last)->htype == BM_VERT)
1977                         {
1978                                 RNA_enum_items_add_value(&item, &totitem, merge_type_items, 6);
1979                                 RNA_enum_items_add_value(&item, &totitem, merge_type_items, 1);
1980                         }
1981                         else if (em->bm->selected.first && ((BMEditSelection *)em->bm->selected.first)->htype == BM_VERT) {
1982                                 RNA_enum_items_add_value(&item, &totitem, merge_type_items, 6);
1983                         }
1984                         else if (em->bm->selected.last && ((BMEditSelection *)em->bm->selected.last)->htype == BM_VERT) {
1985                                 RNA_enum_items_add_value(&item, &totitem, merge_type_items, 1);
1986                         }
1987                 }
1988
1989                 RNA_enum_items_add_value(&item, &totitem, merge_type_items, 3);
1990                 RNA_enum_items_add_value(&item, &totitem, merge_type_items, 4);
1991                 RNA_enum_items_add_value(&item, &totitem, merge_type_items, 5);
1992                 RNA_enum_item_end(&item, &totitem);
1993
1994                 *free = 1;
1995
1996                 return item;
1997         }
1998         
1999         return NULL;
2000 }
2001
2002 void MESH_OT_merge(wmOperatorType *ot)
2003 {
2004         /* identifiers */
2005         ot->name = "Merge";
2006         ot->description = "Merge selected vertices";
2007         ot->idname = "MESH_OT_merge";
2008
2009         /* api callbacks */
2010         ot->exec = edbm_merge_exec;
2011         ot->invoke = WM_menu_invoke;
2012         ot->poll = ED_operator_editmesh;
2013
2014         /* flags */
2015         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2016
2017         /* properties */
2018         ot->prop = RNA_def_enum(ot->srna, "type", merge_type_items, 3, "Type", "Merge method to use");
2019         RNA_def_enum_funcs(ot->prop, merge_type_itemf);
2020         RNA_def_boolean(ot->srna, "uvs", 1, "UVs", "Move UVs according to merge");
2021 }
2022
2023
2024 static int edbm_remove_doubles_exec(bContext *C, wmOperator *op)
2025 {
2026         Object *obedit = CTX_data_edit_object(C);
2027         BMEditMesh *em = BMEdit_FromObject(obedit);
2028         BMOperator bmop;
2029         int count;
2030
2031         EDBM_op_init(em, &bmop, op, "finddoubles verts=%hv dist=%f", BM_ELEM_SELECT, RNA_float_get(op->ptr, "mergedist"));
2032         BMO_op_exec(em->bm, &bmop);
2033
2034         count = BMO_slot_map_count(em->bm, &bmop, "targetmapout");
2035
2036         if (!EDBM_op_callf(em, op, "weldverts targetmap=%s", &bmop, "targetmapout")) {
2037                 BMO_op_finish(em->bm, &bmop);
2038                 return OPERATOR_CANCELLED;
2039         }
2040
2041         if (!EDBM_op_finish(em, &bmop, op, TRUE)) {
2042                 return OPERATOR_CANCELLED;
2043         }
2044         
2045         BKE_reportf(op->reports, RPT_INFO, "Removed %d vert%s", count, (count == 1) ? "ex" : "ices");
2046
2047         EDBM_update_generic(C, em, TRUE);
2048
2049         return OPERATOR_FINISHED;
2050 }
2051
2052 void MESH_OT_remove_doubles(wmOperatorType *ot)
2053 {
2054         /* identifiers */
2055         ot->name = "Remove Doubles";
2056         ot->description = "Remove duplicate vertices";
2057         ot->idname = "MESH_OT_remove_doubles";
2058
2059         /* api callbacks */
2060         ot->exec = edbm_remove_doubles_exec;
2061         ot->poll = ED_operator_editmesh;
2062
2063         /* flags */
2064         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2065
2066         RNA_def_float(ot->srna, "mergedist", 0.0001f, 0.000001f, 50.0f, 
2067                       "Merge Distance",
2068                       "Minimum distance between elements to merge", 0.00001, 10.0);
2069 }
2070
2071 /************************ Vertex Path Operator *************************/
2072
2073 typedef struct PathNode {
2074         /* int u; */       /* UNUSED */
2075         /* int visited; */ /* UNUSED */
2076         ListBase edges;
2077 } PathNode;
2078
2079 typedef struct PathEdge {
2080         struct PathEdge *next, *prev;
2081         int v;
2082         float w;
2083 } PathEdge;
2084
2085
2086
2087 static int edbm_select_vertex_path_exec(bContext *C, wmOperator *op)
2088 {
2089         Object *ob = CTX_data_edit_object(C);
2090         BMEditMesh *em = BMEdit_FromObject(ob);
2091         BMOperator bmop;
2092         BMEditSelection *sv, *ev;
2093
2094         /* get the type from RNA */
2095         int type = RNA_enum_get(op->ptr, "type");
2096
2097         sv = em->bm->selected.last;
2098         if (sv != NULL)
2099                 ev = sv->prev;
2100         else return OPERATOR_CANCELLED;
2101         if (ev == NULL)
2102                 return OPERATOR_CANCELLED;
2103
2104         if ((sv->htype != BM_VERT) || (ev->htype != BM_VERT))
2105                 return OPERATOR_CANCELLED;
2106
2107         /* initialize the bmop using EDBM api, which does various ui error reporting and other stuff */
2108         EDBM_op_init(em, &bmop, op, "vertexshortestpath startv=%e endv=%e type=%i", sv->ele, ev->ele, type);
2109
2110         /* execute the operator */
2111         BMO_op_exec(em->bm, &bmop);
2112
2113         /* DO NOT clear the existing selection */
2114         /* EDBM_flag_disable_all(em, BM_ELEM_SELECT); */
2115
2116         /* select the output */
2117         BMO_slot_buffer_hflag_enable(em->bm, &bmop, "vertout", BM_ALL, BM_ELEM_SELECT, TRUE);
2118
2119         /* finish the operator */
2120         if (!EDBM_op_finish(em, &bmop, op, TRUE)) {
2121                 return OPERATOR_CANCELLED;
2122         }
2123
2124         EDBM_selectmode_flush(em);
2125
2126         EDBM_update_generic(C, em, FALSE);
2127
2128         /* we succeeded */
2129         return OPERATOR_FINISHED;
2130 }
2131
2132 void MESH_OT_select_vertex_path(wmOperatorType *ot)
2133 {
2134         static const EnumPropertyItem type_items[] = {
2135                 {VPATH_SELECT_EDGE_LENGTH, "EDGE_LENGTH", 0, "Edge Length", NULL},
2136                 {VPATH_SELECT_TOPOLOGICAL, "TOPOLOGICAL", 0, "Topological", NULL},
2137                 {0, NULL, 0, NULL, NULL}
2138         };
2139
2140         /* identifiers */
2141         ot->name = "Select Vertex Path";
2142         ot->idname = "MESH_OT_select_vertex_path";
2143         ot->description = "Selected vertex path between two vertices";
2144
2145         /* api callbacks */
2146         ot->exec = edbm_select_vertex_path_exec;
2147         ot->poll = ED_operator_editmesh;
2148
2149         /* flags */
2150         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2151
2152         /* properties */
2153         RNA_def_enum(ot->srna, "type", type_items, VPATH_SELECT_EDGE_LENGTH, "Type", "Method to compute distance");
2154 }
2155 /********************** Rip Operator *************************/
2156
2157 /************************ Shape Operators *************************/
2158
2159 /* BMESH_TODO this should be properly encapsulated in a bmop.  but later.*/
2160 static void shape_propagate(BMEditMesh *em, wmOperator *op)
2161 {
2162         BMIter iter;
2163         BMVert *eve = NULL;
2164         float *co;
2165         int i, totshape = CustomData_number_of_layers(&em->bm->vdata, CD_SHAPEKEY);
2166
2167         if (!CustomData_has_layer(&em->bm->vdata, CD_SHAPEKEY)) {
2168                 BKE_report(op->reports, RPT_ERROR, "Mesh does not have shape keys");
2169                 return;
2170         }
2171         
2172         BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
2173                 if (!BM_elem_flag_test(eve, BM_ELEM_SELECT) || BM_elem_flag_test(eve, BM_ELEM_HIDDEN))
2174                         continue;
2175
2176                 for (i = 0; i < totshape; i++) {
2177                         co = CustomData_bmesh_get_n(&em->bm->vdata, eve->head.data, CD_SHAPEKEY, i);
2178                         copy_v3_v3(co, eve->co);
2179                 }
2180         }
2181
2182 #if 0
2183         //TAG Mesh Objects that share this data
2184         for (base = scene->base.first; base; base = base->next) {
2185                 if (base->object && base->object->data == me) {
2186                         base->object->recalc = OB_RECALC_DATA;
2187                 }
2188         }
2189 #endif
2190 }
2191
2192
2193 static int edbm_shape_propagate_to_all_exec(bContext *C, wmOperator *op)
2194 {
2195         Object *obedit = CTX_data_edit_object(C);
2196         Mesh *me = obedit->data;
2197         BMEditMesh *em = me->edit_btmesh;
2198
2199         shape_propagate(em, op);
2200
2201         EDBM_update_generic(C, em, FALSE);
2202
2203         return OPERATOR_FINISHED;
2204 }
2205
2206
2207 void MESH_OT_shape_propagate_to_all(wmOperatorType *ot)
2208 {
2209         /* identifiers */
2210         ot->name = "Shape Propagate";
2211         ot->description = "Apply selected vertex locations to all other shape keys";
2212         ot->idname = "MESH_OT_shape_propagate_to_all";
2213
2214         /* api callbacks */
2215         ot->exec = edbm_shape_propagate_to_all_exec;
2216         ot->poll = ED_operator_editmesh;
2217
2218         /* flags */
2219         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2220 }
2221
2222 /* BMESH_TODO this should be properly encapsulated in a bmop.  but later.*/
2223 static int edbm_blend_from_shape_exec(bContext *C, wmOperator *op)
2224 {
2225         Object *obedit = CTX_data_edit_object(C);
2226         Mesh *me = obedit->data;
2227         BMEditMesh *em = me->edit_btmesh;
2228         BMVert *eve;
2229         BMIter iter;
2230         float co[3], *sco;
2231         float blend = RNA_float_get(op->ptr, "blend");
2232         int shape = RNA_enum_get(op->ptr, "shape");
2233         int add = RNA_boolean_get(op->ptr, "add");
2234         int totshape;
2235
2236         /* sanity check */
2237         totshape = CustomData_number_of_layers(&em->bm->vdata, CD_SHAPEKEY);
2238         if (totshape == 0 || shape < 0 || shape >= totshape)
2239                 return OPERATOR_CANCELLED;
2240
2241         BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
2242                 if (!BM_elem_flag_test(eve, BM_ELEM_SELECT) || BM_elem_flag_test(eve, BM_ELEM_HIDDEN))
2243                         continue;
2244
2245                 sco = CustomData_bmesh_get_n(&em->bm->vdata, eve->head.data, CD_SHAPEKEY, shape);
2246                 copy_v3_v3(co, sco);
2247
2248
2249                 if (add) {
2250                         mul_v3_fl(co, blend);
2251                         add_v3_v3v3(eve->co, eve->co, co);
2252                 }
2253                 else {
2254                         interp_v3_v3v3(eve->co, eve->co, co, blend);
2255                 }
2256                 
2257                 copy_v3_v3(sco, co);
2258         }
2259
2260         EDBM_update_generic(C, em, TRUE);
2261
2262         return OPERATOR_FINISHED;
2263 }
2264
2265 static EnumPropertyItem *shape_itemf(bContext *C, PointerRNA *UNUSED(ptr),  PropertyRNA *UNUSED(prop), int *free)
2266 {       
2267         Object *obedit = CTX_data_edit_object(C);
2268         BMEditMesh *em;
2269         EnumPropertyItem *item = NULL;
2270         int totitem = 0;
2271
2272         if ((obedit && obedit->type == OB_MESH) &&
2273             (em = BMEdit_FromObject(obedit)) &&
2274             CustomData_has_layer(&em->bm->vdata, CD_SHAPEKEY))
2275         {
2276                 EnumPropertyItem tmp = {0, "", 0, "", ""};
2277                 int a;
2278
2279                 for (a = 0; a < em->bm->vdata.totlayer; a++) {
2280                         if (em->bm->vdata.layers[a].type != CD_SHAPEKEY)
2281                                 continue;
2282
2283                         tmp.value = totitem;
2284                         tmp.identifier = em->bm->vdata.layers[a].name;
2285                         tmp.name = em->bm->vdata.layers[a].name;
2286                         /* RNA_enum_item_add sets totitem itself! */
2287                         RNA_enum_item_add(&item, &totitem, &tmp);
2288                 }
2289         }
2290
2291         RNA_enum_item_end(&item, &totitem);
2292         *free = 1;
2293
2294         return item;
2295 }
2296
2297 void MESH_OT_blend_from_shape(wmOperatorType *ot)
2298 {
2299         PropertyRNA *prop;
2300         static EnumPropertyItem shape_items[] = {{0, NULL, 0, NULL, NULL}};
2301
2302         /* identifiers */
2303         ot->name = "Blend From Shape";
2304         ot->description = "Blend in shape from a shape key";
2305         ot->idname = "MESH_OT_blend_from_shape";
2306
2307         /* api callbacks */
2308         ot->exec = edbm_blend_from_shape_exec;
2309         ot->invoke = WM_operator_props_popup;
2310         ot->poll = ED_operator_editmesh;
2311
2312         /* flags */
2313         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2314
2315         /* properties */
2316         prop = RNA_def_enum(ot->srna, "shape", shape_items, 0, "Shape", "Shape key to use for blending");
2317         RNA_def_enum_funcs(prop, shape_itemf);
2318         RNA_def_float(ot->srna, "blend", 1.0f, -FLT_MAX, FLT_MAX, "Blend", "Blending factor", -2.0f, 2.0f);
2319         RNA_def_boolean(ot->srna, "add", 1, "Add", "Add rather than blend between shapes");
2320 }
2321
2322 /* BMESH_TODO - some way to select on an arbitrary axis */
2323 static int edbm_select_axis_exec(bContext *C, wmOperator *op)
2324 {
2325         Object *obedit = CTX_data_edit_object(C);
2326         BMEditMesh *em = BMEdit_FromObject(obedit);
2327         BMEditSelection *ese = em->bm->selected.last;
2328         int axis = RNA_enum_get(op->ptr, "axis");
2329         int mode = RNA_enum_get(op->ptr, "mode"); /* -1 == aligned, 0 == neg, 1 == pos */
2330
2331         if (ese == NULL || ese->htype != BM_VERT) {
2332                 BKE_report(op->reports, RPT_WARNING, "This operator requires an active vertex (last selected)");
2333                 return OPERATOR_CANCELLED;
2334         }
2335         else {
2336                 BMVert *ev, *act_vert = (BMVert *)ese->ele;
2337                 BMIter iter;
2338                 float value = act_vert->co[axis];
2339                 float limit =  CTX_data_tool_settings(C)->doublimit; // XXX
2340
2341                 if (mode == 0)
2342                         value -= limit;
2343                 else if (mode == 1)
2344                         value += limit;
2345
2346                 BM_ITER_MESH (ev, &iter, em->bm, BM_VERTS_OF_MESH) {
2347                         if (!BM_elem_flag_test(ev, BM_ELEM_HIDDEN)) {
2348                                 switch (mode) {
2349                                         case -1: /* aligned */
2350                                                 if (fabs(ev->co[axis] - value) < limit)
2351                                                         BM_vert_select_set(em->bm, ev, TRUE);
2352                                                 break;
2353                                         case 0: /* neg */
2354                                                 if (ev->co[axis] > value)
2355                                                         BM_vert_select_set(em->bm, ev, TRUE);
2356                                                 break;
2357                                         case 1: /* pos */
2358                                                 if (ev->co[axis] < value)
2359                                                         BM_vert_select_set(em->bm, ev, TRUE);
2360                                                 break;
2361                                 }
2362                         }
2363                 }
2364         }
2365
2366         EDBM_selectmode_flush(em);
2367         WM_event_add_notifier(C, NC_GEOM | ND_DATA, obedit->data);
2368
2369         return OPERATOR_FINISHED;
2370 }
2371
2372 void MESH_OT_select_axis(wmOperatorType *ot)
2373 {
2374         static EnumPropertyItem axis_mode_items[] = {
2375                 {0,  "POSITIVE", 0, "Positive Axis", ""},
2376                 {1,  "NEGATIVE", 0, "Negative Axis", ""},
2377                 {-1, "ALIGNED",  0, "Aligned Axis", ""},
2378                 {0, NULL, 0, NULL, NULL}};
2379
2380         static EnumPropertyItem axis_items_xyz[] = {
2381                 {0, "X_AXIS", 0, "X Axis", ""},
2382                 {1, "Y_AXIS", 0, "Y Axis", ""},
2383                 {2, "Z_AXIS", 0, "Z Axis", ""},
2384                 {0, NULL, 0, NULL, NULL}};
2385
2386         /* identifiers */
2387         ot->name = "Select Axis";
2388         ot->description = "Select all data in the mesh on a single axis";
2389         ot->idname = "MESH_OT_select_axis";
2390
2391         /* api callbacks */
2392         ot->exec = edbm_select_axis_exec;
2393         ot->poll = ED_operator_editmesh;
2394
2395         /* flags */
2396         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2397
2398         /* properties */
2399         RNA_def_enum(ot->srna, "mode", axis_mode_items, 0, "Axis Mode", "Axis side to use when selecting");
2400         RNA_def_enum(ot->srna, "axis", axis_items_xyz, 0, "Axis", "Select the axis to compare each vertex on");
2401 }
2402
2403 static int edbm_solidify_exec(bContext *C, wmOperator *op)
2404 {
2405         Object *obedit = CTX_data_edit_object(C);
2406         Mesh *me = obedit->data;
2407         BMEditMesh *em = me->edit_btmesh;
2408         BMesh *bm = em->bm;
2409         BMOperator bmop;
2410
2411         float thickness = RNA_float_get(op->ptr, "thickness");
2412
2413         if (!EDBM_op_init(em, &bmop, op, "solidify geom=%hf thickness=%f", BM_ELEM_SELECT, thickness)) {
2414                 return OPERATOR_CANCELLED;
2415         }
2416
2417         /* deselect only the faces in the region to be solidified (leave wire
2418          * edges and loose verts selected, as there will be no corresponding
2419          * geometry selected below) */
2420         BMO_slot_buffer_hflag_disable(bm, &bmop, "geom", BM_FACE, BM_ELEM_SELECT, TRUE);
2421
2422         /* run the solidify operator */
2423         BMO_op_exec(bm, &bmop);
2424
2425         /* select the newly generated faces */
2426         BMO_slot_buffer_hflag_enable(bm, &bmop, "geomout", BM_FACE, BM_ELEM_SELECT, TRUE);
2427
2428         if (!EDBM_op_finish(em, &bmop, op, TRUE)) {
2429                 return OPERATOR_CANCELLED;
2430         }
2431
2432         EDBM_update_generic(C, em, TRUE);
2433
2434         return OPERATOR_FINISHED;
2435 }
2436
2437
2438 void MESH_OT_solidify(wmOperatorType *ot)
2439 {
2440         PropertyRNA *prop;
2441         /* identifiers */
2442         ot->name = "Solidify";
2443         ot->description = "Create a solid skin by extruding, compensating for sharp angles";
2444         ot->idname = "MESH_OT_solidify";
2445
2446         /* api callbacks */
2447         ot->exec = edbm_solidify_exec;
2448         ot->poll = ED_operator_editmesh;
2449
2450         /* flags */
2451         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2452
2453         prop = RNA_def_float(ot->srna, "thickness", 0.01f, -FLT_MAX, FLT_MAX, "thickness", "", -10.0f, 10.0f);
2454         RNA_def_property_ui_range(prop, -10, 10, 0.1, 4);
2455 }
2456
2457 #define TRAIL_POLYLINE 1 /* For future use, They don't do anything yet */
2458 #define TRAIL_FREEHAND 2
2459 #define TRAIL_MIXED    3 /* (1|2) */
2460 #define TRAIL_AUTO     4 
2461 #define TRAIL_MIDPOINTS 8
2462
2463 typedef struct CutCurve {
2464         float x;
2465         float y;
2466 } CutCurve;
2467
2468 /* ******************************************************************** */
2469 /* Knife Subdivide Tool.  Subdivides edges intersected by a mouse trail
2470  * drawn by user.
2471  *
2472  * Currently mapped to KKey when in MeshEdit mode.
2473  * Usage:
2474  * - Hit Shift K, Select Centers or Exact
2475  * - Hold LMB down to draw path, hit RETKEY.
2476  * - ESC cancels as expected.
2477  *
2478  * Contributed by Robert Wenzlaff (Det. Thorn).
2479  *
2480  * 2.5 Revamp:
2481  *  - non modal (no menu before cutting)
2482  *  - exit on mouse release
2483  *  - polygon/segment drawing can become handled by WM cb later
2484  *
2485  * bmesh port version
2486  */
2487
2488 #define KNIFE_EXACT     1
2489 #define KNIFE_MIDPOINT  2
2490 #define KNIFE_MULTICUT  3
2491
2492 static EnumPropertyItem knife_items[] = {
2493         {KNIFE_EXACT, "EXACT", 0, "Exact", ""},
2494         {KNIFE_MIDPOINT, "MIDPOINTS", 0, "Midpoints", ""},
2495         {KNIFE_MULTICUT, "MULTICUT", 0, "Multicut", ""},
2496         {0, NULL, 0, NULL, NULL}
2497 };
2498
2499 /* bm_edge_seg_isect() Determines if and where a mouse trail intersects an BMEdge */
2500
2501 static float bm_edge_seg_isect(BMEdge *e, CutCurve *c, int len, char mode,
2502                                struct GHash *gh, int *isected)
2503 {
2504 #define MAXSLOPE 100000
2505         float x11, y11, x12 = 0, y12 = 0, x2max, x2min, y2max;
2506         float y2min, dist, lastdist = 0, xdiff2, xdiff1;
2507         float m1, b1, m2, b2, x21, x22, y21, y22, xi;
2508         float yi, x1min, x1max, y1max, y1min, perc = 0;
2509         float  *scr;
2510         float threshold = 0.0;
2511         int i;
2512         
2513         //threshold = 0.000001; /* tolerance for vertex intersection */
2514         // XXX threshold = scene->toolsettings->select_thresh / 100;
2515         
2516         /* Get screen coords of verts */
2517         scr = BLI_ghash_lookup(gh, e->v1);
2518         x21 = scr[0];
2519         y21 = scr[1];
2520         
2521         scr = BLI_ghash_lookup(gh, e->v2);
2522         x22 = scr[0];
2523         y22 = scr[1];
2524         
2525         xdiff2 = (x22 - x21);
2526         if (xdiff2) {
2527                 m2 = (y22 - y21) / xdiff2;
2528                 b2 = ((x22 * y21) - (x21 * y22)) / xdiff2;
2529         }
2530         else {
2531                 m2 = MAXSLOPE;  /* Verticle slope  */
2532                 b2 = x22;
2533         }
2534
2535         *isected = 0;
2536
2537         /* check for _exact_ vertex intersection first */
2538         if (mode != KNIFE_MULTICUT) {
2539                 for (i = 0; i < len; i++) {
2540                         if (i > 0) {
2541                                 x11 = x12;
2542                                 y11 = y12;
2543                         }
2544                         else {
2545                                 x11 = c[i].x;
2546                                 y11 = c[i].y;
2547                         }
2548                         x12 = c[i].x;
2549                         y12 = c[i].y;
2550                         
2551                         /* test e->v1 */
2552                         if ((x11 == x21 && y11 == y21) || (x12 == x21 && y12 == y21)) {
2553                                 perc = 0;
2554                                 *isected = 1;
2555                                 return perc;
2556                         }
2557                         /* test e->v2 */
2558                         else if ((x11 == x22 && y11 == y22) || (x12 == x22 && y12 == y22)) {
2559                                 perc = 0;
2560                                 *isected = 2;
2561                                 return perc;
2562                         }
2563                 }
2564         }
2565         
2566         /* now check for edge intersect (may produce vertex intersection as well) */
2567         for (i = 0; i < len; i++) {
2568                 if (i > 0) {
2569                         x11 = x12;
2570                         y11 = y12;
2571                 }
2572                 else {
2573                         x11 = c[i].x;
2574                         y11 = c[i].y;
2575                 }
2576                 x12 = c[i].x;
2577                 y12 = c[i].y;
2578                 
2579                 /* Perp. Distance from point to line */
2580                 if (m2 != MAXSLOPE) dist = (y12 - m2 * x12 - b2);  /* /sqrt(m2 * m2 + 1); Only looking for */
2581                 /* change in sign.  Skip extra math */
2582                 else dist = x22 - x12;
2583                 
2584                 if (i == 0) lastdist = dist;
2585                 
2586                 /* if dist changes sign, and intersect point in edge's Bound Box */
2587                 if ((lastdist * dist) <= 0) {
2588                         xdiff1 = (x12 - x11); /* Equation of line between last 2 points */
2589                         if (xdiff1) {
2590                                 m1 = (y12 - y11) / xdiff1;
2591                                 b1 = ((x12 * y11) - (x11 * y12)) / xdiff1;
2592                         }
2593                         else {
2594                                 m1 = MAXSLOPE;
2595                                 b1 = x12;
2596                         }
2597                         x2max = MAX2(x21, x22) + 0.001; /* prevent missed edges   */
2598                         x2min = MIN2(x21, x22) - 0.001; /* due to round off error */
2599                         y2max = MAX2(y21, y22) + 0.001;
2600                         y2min = MIN2(y21, y22) - 0.001;
2601                         
2602                         /* Found an intersect,  calc intersect point */
2603                         if (m1 == m2) { /* co-incident lines */
2604                                 /* cut at 50% of overlap area */
2605                                 x1max = MAX2(x11, x12);
2606                                 x1min = MIN2(x11, x12);
2607                                 xi = (MIN2(x2max, x1max) + MAX2(x2min, x1min)) / 2.0;
2608                                 
2609                                 y1max = MAX2(y11, y12);
2610                                 y1min = MIN2(y11, y12);
2611                                 yi = (MIN2(y2max, y1max) + MAX2(y2min, y1min)) / 2.0;
2612                         }
2613                         else if (m2 == MAXSLOPE) {
2614                                 xi = x22;
2615                                 yi = m1 * x22 + b1;
2616                         }
2617                         else if (m1 == MAXSLOPE) {
2618                                 xi = x12;
2619                                 yi = m2 * x12 + b2;
2620                         }
2621                         else {
2622                                 xi = (b1 - b2) / (m2 - m1);
2623                                 yi = (b1 * m2 - m1 * b2) / (m2 - m1);
2624                         }
2625                         
2626                         /* Intersect inside bounding box of edge?*/
2627                         if ((xi >= x2min) && (xi <= x2max) && (yi <= y2max) && (yi >= y2min)) {
2628                                 /* test for vertex intersect that may be 'close enough'*/
2629                                 if (mode != KNIFE_MULTICUT) {
2630                                         if (xi <= (x21 + threshold) && xi >= (x21 - threshold)) {
2631                                                 if (yi <= (y21 + threshold) && yi >= (y21 - threshold)) {
2632                                                         *isected = 1;
2633                                                         perc = 0;
2634                                                         break;
2635                                                 }
2636                                         }
2637                                         if (xi <= (x22 + threshold) && xi >= (x22 - threshold)) {
2638                                                 if (yi <= (y22 + threshold) && yi >= (y22 - threshold)) {
2639                                                         *isected = 2;
2640                                                         perc = 0;
2641                                                         break;
2642                                                 }
2643                                         }
2644                                 }
2645                                 if ((m2 <= 1.0f) && (m2 >= -1.0f)) perc = (xi - x21) / (x22 - x21);
2646                                 else perc = (yi - y21) / (y22 - y21);  /* lower slope more accurate */
2647                                 //isect = 32768.0 * (perc + 0.0000153); /* Percentage in 1 / 32768ths */
2648                                 
2649                                 break;
2650                         }
2651                 }       
2652                 lastdist = dist;
2653         }
2654         return perc;
2655
2656
2657 #define MAX_CUTS 2048
2658
2659 static int edbm_knife_cut_exec(bContext *C, wmOperator *op)
2660 {
2661         Object *obedit = CTX_data_edit_object(C);
2662         BMEditMesh *em = BMEdit_FromObject(obedit);
2663         BMesh *bm = em->bm;
2664         ARegion *ar = CTX_wm_region(C);
2665         BMVert *bv;
2666         BMIter iter;
2667         BMEdge *be;
2668         BMOperator bmop;
2669         CutCurve curve[MAX_CUTS];
2670         struct GHash *gh;
2671         float isect = 0.0f;
2672         float  *scr, co[4];
2673         int len = 0, isected;
2674         short numcuts = 1, mode = RNA_int_get(op->ptr, "type");
2675         
2676         /* edit-object needed for matrix, and ar->regiondata for projections to work */
2677         if (ELEM3(NULL, obedit, ar, ar->regiondata))
2678                 return OPERATOR_CANCELLED;
2679         
2680         if (bm->totvertsel < 2) {
2681                 //error("No edges are selected to operate on");
2682                 return OPERATOR_CANCELLED;
2683         }
2684
2685         /* get the cut curve */
2686         RNA_BEGIN (op->ptr, itemptr, "path")
2687         {
2688                 RNA_float_get_array(&itemptr, "loc", (float *)&curve[len]);
2689                 len++;
2690                 if (len >= MAX_CUTS) {
2691                         break;
2692                 }
2693         }
2694         RNA_END;
2695         
2696         if (len < 2) {
2697                 return OPERATOR_CANCELLED;
2698         }
2699
2700         /* the floating point coordinates of verts in screen space will be stored in a hash table according to the vertices pointer */
2701         gh = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, "knife cut exec");
2702         for (bv = BM_iter_new(&iter, bm, BM_VERTS_OF_MESH, NULL); bv; bv = BM_iter_step(&iter)) {
2703                 scr = MEM_mallocN(sizeof(float) * 2, "Vertex Screen Coordinates");
2704                 copy_v3_v3(co, bv->co);
2705                 co[3] = 1.0f;
2706                 mul_m4_v4(obedit->obmat, co);
2707                 project_float(ar, co, scr);
2708                 BLI_ghash_insert(gh, bv, scr);
2709         }
2710
2711         if (!EDBM_op_init(em, &bmop, op, "esubd")) {
2712                 return OPERATOR_CANCELLED;
2713         }
2714
2715         /* store percentage of edge cut for KNIFE_EXACT here.*/
2716         for (be = BM_iter_new(&iter, bm, BM_EDGES_OF_MESH, NULL); be; be = BM_iter_step(&iter)) {
2717                 if (BM_elem_flag_test(be, BM_ELEM_SELECT)) {
2718                         isect = bm_edge_seg_isect(be, curve, len, mode, gh, &isected);
2719                         
2720                         if (isect != 0.0f) {
2721                                 if (mode != KNIFE_MULTICUT && mode != KNIFE_MIDPOINT) {
2722                                         BMO_slot_map_float_insert(bm, &bmop,
2723                                                                   "edgepercents",
2724                                                                   be, isect);
2725
2726                                 }
2727                                 BMO_elem_flag_enable(bm, be, 1);
2728                         }
2729                         else {
2730                                 BMO_elem_flag_disable(bm, be, 1);
2731                         }
2732                 }
2733                 else {
2734                         BMO_elem_flag_disable(bm, be, 1);
2735                 }
2736         }
2737         
2738         BMO_slot_buffer_from_enabled_flag(bm, &bmop, "edges", BM_EDGE, 1);
2739
2740         if (mode == KNIFE_MIDPOINT) numcuts = 1;
2741         BMO_slot_int_set(&bmop, "numcuts", numcuts);
2742
2743         BMO_slot_int_set(&bmop, "quadcornertype", SUBD_STRAIGHT_CUT);
2744         BMO_slot_bool_set(&bmop, "use_singleedge", FALSE);
2745         BMO_slot_bool_set(&bmop, "use_gridfill", FALSE);
2746
2747         BMO_slot_float_set(&bmop, "radius", 0);
2748         
2749         BMO_op_exec(bm, &bmop);
2750         if (!EDBM_op_finish(em, &bmop, op, TRUE)) {
2751                 return OPERATOR_CANCELLED;
2752         }
2753         
2754         BLI_ghash_free(gh, NULL, (GHashValFreeFP)MEM_freeN);
2755
2756         EDBM_update_generic(C, em, TRUE);
2757
2758         return OPERATOR_FINISHED;
2759 }
2760
2761 void MESH_OT_knife_cut(wmOperatorType *ot)
2762 {
2763         PropertyRNA *prop;
2764         
2765         ot->name = "Knife Cut";
2766         ot->description = "Cut selected edges and faces into parts";
2767         ot->idname = "MESH_OT_knife_cut";
2768         
2769         ot->invoke = WM_gesture_lines_invoke;
2770         ot->modal = WM_gesture_lines_modal;
2771         ot->exec = edbm_knife_cut_exec;
2772         
2773         ot->poll = EM_view3d_poll;
2774         
2775         /* flags */
2776         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2777         
2778         RNA_def_enum(ot->srna, "type", knife_items, KNIFE_EXACT, "Type", "");
2779         prop = RNA_def_property(ot->srna, "path", PROP_COLLECTION, PROP_NONE);
2780         RNA_def_property_struct_runtime(prop, &RNA_OperatorMousePath);
2781         
2782         /* internal */
2783         RNA_def_int(ot->srna, "cursor", BC_KNIFECURSOR, 0, INT_MAX, "Cursor", "", 0, INT_MAX);
2784 }
2785
2786 static int mesh_separate_selected(Main *bmain, Scene *scene, Base *editbase, wmOperator *wmop)
2787 {
2788         Base *basenew;
2789         BMIter iter;
2790         BMVert *v;
2791         BMEdge *e;
2792         Object *obedit = editbase->object;
2793         Mesh *me = obedit->data;
2794         BMEditMesh *em = me->edit_btmesh;
2795         BMesh *bm_new;
2796         
2797         if (!em)
2798                 return FALSE;
2799                 
2800         bm_new = BM_mesh_create(&bm_mesh_allocsize_default);
2801         CustomData_copy(&em->bm->vdata, &bm_new->vdata, CD_MASK_BMESH, CD_CALLOC, 0);
2802         CustomData_copy(&em->bm->edata, &bm_new->edata, CD_MASK_BMESH, CD_CALLOC, 0);
2803         CustomData_copy(&em->bm->ldata, &bm_new->ldata, CD_MASK_BMESH, CD_CALLOC, 0);
2804         CustomData_copy(&em->bm->pdata, &bm_new->pdata, CD_MASK_BMESH, CD_CALLOC, 0);
2805
2806         CustomData_bmesh_init_pool(&bm_new->vdata, bm_mesh_allocsize_default.totvert, BM_VERT);
2807         CustomData_bmesh_init_pool(&bm_new->edata, bm_mesh_allocsize_default.totedge, BM_EDGE);
2808         CustomData_bmesh_init_pool(&bm_new->ldata, bm_mesh_allocsize_default.totloop, BM_LOOP);
2809         CustomData_bmesh_init_pool(&bm_new->pdata, bm_mesh_allocsize_default.totface, BM_FACE);
2810                 
2811         basenew = ED_object_add_duplicate(bmain, scene, editbase, USER_DUP_MESH);   /* 0 = fully linked */
2812         assign_matarar(basenew->object, give_matarar(obedit), *give_totcolp(obedit)); /* new in 2.5 */
2813
2814         ED_base_object_select(basenew, BA_DESELECT);
2815         
2816         EDBM_op_callf(em, wmop, "dupe geom=%hvef dest=%p", BM_ELEM_SELECT, bm_new);
2817         EDBM_op_callf(em, wmop, "del geom=%hvef context=%i", BM_ELEM_SELECT, DEL_FACES);
2818
2819         /* clean up any loose edges */
2820         BM_ITER_MESH (e, &iter, em->bm, BM_EDGES_OF_MESH) {
2821                 if (BM_elem_flag_test(e, BM_ELEM_HIDDEN))
2822                         continue;
2823
2824                 if (!BM_edge_is_wire(e)) {
2825                         BM_edge_select_set(em->bm, e, FALSE);
2826                 }
2827         }
2828         EDBM_op_callf(em, wmop, "del geom=%hvef context=%i", BM_ELEM_SELECT, DEL_EDGES);
2829
2830         /* clean up any loose verts */
2831         BM_ITER_MESH (v, &iter, em->bm, BM_VERTS_OF_MESH) {
2832                 if (BM_elem_flag_test(v, BM_ELEM_HIDDEN))
2833                         continue;
2834
2835                 if (BM_vert_edge_count(v) != 0) {
2836                         BM_vert_select_set(em->bm, v, FALSE);
2837                 }
2838         }
2839
2840         EDBM_op_callf(em, wmop, "del geom=%hvef context=%i", BM_ELEM_SELECT, DEL_VERTS);
2841
2842         BM_mesh_normals_update(bm_new, TRUE);
2843
2844         BM_mesh_bm_to_me(bm_new, basenew->object->data, FALSE);
2845                 
2846         BM_mesh_free(bm_new);
2847         ((Mesh *)basenew->object->data)->edit_btmesh = NULL;
2848         
2849         return TRUE;
2850 }
2851
2852 static int mesh_separate_material(Main *bmain, Scene *scene, Base *editbase, wmOperator *wmop)
2853 {
2854         BMFace *f_cmp, *f;
2855         BMIter iter;
2856         int result = FALSE;
2857         Object *obedit = editbase->object;
2858         BMEditMesh *em = BMEdit_FromObject(obedit);
2859         BMesh *bm = em->bm;
2860
2861         EDBM_flag_disable_all(em, BM_ELEM_SELECT);
2862
2863         while ((f_cmp = BM_iter_at_index(bm, BM_FACES_OF_MESH, NULL, 0))) {
2864                 const short mat_nr = f_cmp->mat_nr;
2865                 int tot = 0;
2866
2867                 BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
2868                         if (f->mat_nr == mat_nr) {
2869                                 BM_face_select_set(bm, f, TRUE);
2870                                 tot++;
2871                         }
2872                 }
2873
2874                 /* leave the current object with some materials */
2875                 if (tot == bm->totface) {
2876                         break;
2877                 }
2878
2879                 /* Move selection into a separate object */
2880                 result |= mesh_separate_selected(bmain, scene, editbase, wmop);
2881         }
2882
2883         return result;
2884 }
2885
2886 static int mesh_separate_loose(Main *bmain, Scene *scene, Base *editbase, wmOperator *wmop)
2887 {
2888         int i;
2889         BMVert *v;
2890         BMEdge *e;
2891         BMVert *v_seed;
2892         BMWalker walker;
2893         BMIter iter;
2894         int result = FALSE;
2895         Object *obedit = editbase->object;
2896         BMEditMesh *em = BMEdit_FromObject(obedit);
2897         BMesh *bm = em->bm;
2898         int max_iter = bm->totvert;
2899
2900         /* Clear all selected vertices */
2901         EDBM_flag_disable_all(em, BM_ELEM_SELECT);
2902
2903         /* A "while (true)" loop should work here as each iteration should
2904          * select and remove at least one vertex and when all vertices
2905          * are selected the loop will break out. But guard against bad
2906          * behavior by limiting iterations to the number of vertices in the
2907          * original mesh.*/
2908         for (i = 0; i < max_iter; i++) {
2909                 /* Get a seed vertex to start the walk */
2910                 v_seed = NULL;
2911                 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
2912                         v_seed = v;
2913                         break;
2914                 }
2915
2916                 /* No vertices available, can't do anything */
2917                 if (v_seed == NULL) {
2918                         break;
2919                 }
2920
2921                 /* Select the seed explicitly, in case it has no edges */
2922                 BM_vert_select_set(bm, v_seed, TRUE);
2923
2924                 /* Walk from the single vertex, selecting everything connected
2925                  * to it */
2926                 BMW_init(&walker, bm, BMW_SHELL,
2927                          BMW_MASK_NOP, BMW_MASK_NOP, BMW_MASK_NOP,
2928                          BMW_FLAG_NOP, /* BMESH_TODO - should be BMW_FLAG_TEST_HIDDEN ? */
2929                          BMW_NIL_LAY);
2930
2931                 e = BMW_begin(&walker, v_seed);
2932                 for (; e; e = BMW_step(&walker)) {
2933                         BM_vert_select_set(bm, e->v1, TRUE);
2934                         BM_vert_select_set(bm, e->v2, TRUE);
2935                 }
2936                 BMW_end(&walker);
2937                                 
2938                 /* Flush the selection to get edge/face selections matching
2939                  * the vertex selection */
2940                 EDBM_selectmode_flush_ex(em, SCE_SELECT_VERTEX);
2941
2942                 if (bm->totvert == bm->totvertsel) {
2943                         /* Every vertex selected, nothing to separate, work is done */
2944                         break;
2945                 }
2946
2947                 /* Move selection into a separate object */
2948                 result |= mesh_separate_selected(bmain, scene, editbase, wmop);
2949         }
2950
2951         return result;
2952 }
2953
2954 static int edbm_separate_exec(bContext *C, wmOperator *op)
2955 {
2956         Main *bmain = CTX_data_main(C);
2957         Scene *scene = CTX_data_scene(C);
2958         Base *base = CTX_data_active_base(C);
2959         int retval = 0, type = RNA_enum_get(op->ptr, "type");
2960         
2961         if (type == 0)
2962                 retval = mesh_separate_selected(bmain, scene, base, op);
2963         else if (type == 1)
2964                 retval = mesh_separate_material(bmain, scene, base, op);
2965         else if (type == 2)
2966                 retval = mesh_separate_loose(bmain, scene, base, op);
2967
2968         if (retval) {
2969                 BMEditMesh *em = BMEdit_FromObject(base->object);
2970                 EDBM_update_generic(C, em, TRUE);
2971
2972                 return OPERATOR_FINISHED;
2973         }
2974
2975         return OPERATOR_CANCELLED;
2976 }
2977
2978 /* *************** Operator: separate parts *************/
2979
2980 static EnumPropertyItem prop_separate_types[] = {
2981         {0, "SELECTED", 0, "Selection", ""},
2982         {1, "MATERIAL", 0, "By Material", ""},
2983         {2, "LOOSE", 0, "By loose parts", ""},
2984         {0, NULL, 0, NULL, NULL}
2985 };
2986
2987 void MESH_OT_separate(wmOperatorType *ot)
2988 {
2989         /* identifiers */
2990         ot->name = "Separate";
2991         ot->description = "Separate selected geometry into a new mesh";
2992         ot->idname = "MESH_OT_separate";
2993         
2994         /* api callbacks */
2995         ot->invoke = WM_menu_invoke;
2996         ot->exec = edbm_separate_exec;
2997         ot->poll = ED_operator_editmesh;
2998         
2999         /* flags */
3000         ot->flag = OPTYPE_UNDO;
3001         
3002         ot->prop = RNA_def_enum(ot->srna, "type", prop_separate_types, 0, "Type", "");
3003 }
3004
3005
3006 static int edbm_fill_exec(bContext *C, wmOperator *op)
3007 {
3008         Object *obedit = CTX_data_edit_object(C);
3009         BMEditMesh *em = BMEdit_FromObject(obedit);
3010         BMOperator bmop;
3011         
3012         if (!EDBM_op_init(em, &bmop, op, "triangle_fill edges=%he", BM_ELEM_SELECT)) {
3013                 return OPERATOR_CANCELLED;
3014         }
3015         
3016         BMO_op_exec(em->bm, &bmop);
3017         
3018         /* select new geometry */
3019         BMO_slot_buffer_hflag_enable(em->bm, &bmop, "geomout", BM_FACE | BM_EDGE, BM_ELEM_SELECT, TRUE);
3020         
3021         if (!EDBM_op_finish(em, &bmop, op, TRUE)) {
3022                 return OPERATOR_CANCELLED;
3023         }
3024
3025         EDBM_update_generic(C, em, TRUE);
3026         
3027         return OPERATOR_FINISHED;
3028
3029 }
3030
3031 void MESH_OT_fill(wmOperatorType *ot)
3032 {
3033         /* identifiers */
3034         ot->name = "Fill";
3035         ot->idname = "MESH_OT_fill";
3036         ot->description = "Fill a selected edge loop with faces";
3037
3038         /* api callbacks */
3039         ot->exec = edbm_fill_exec;
3040         ot->poll = ED_operator_editmesh;
3041
3042         /* flags */
3043         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
3044 }
3045
3046 static int edbm_beautify_fill_exec(bContext *C, wmOperator *op)
3047 {
3048         Object *obedit = CTX_data_edit_object(C);
3049         BMEditMesh *em = BMEdit_FromObject(obedit);
3050
3051         if (!EDBM_op_callf(em, op, "beautify_fill faces=%hf", BM_ELEM_SELECT))
3052                 return OPERATOR_CANCELLED;
3053
3054         EDBM_update_generic(C, em, TRUE);
3055         
3056         return OPERATOR_FINISHED;
3057 }
3058
3059 void MESH_OT_beautify_fill(wmOperatorType *ot)
3060 {
3061         /* identifiers */
3062         ot->name = "Beautify Fill";
3063         ot->idname = "MESH_OT_beautify_fill";
3064         ot->description = "Rearrange some faces to try to get less degenerated geometry";
3065
3066         /* api callbacks */
3067         ot->exec = edbm_beautify_fill_exec;
3068         ot->poll = ED_operator_editmesh;
3069
3070         /* flags */
3071         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
3072 }
3073
3074 /********************** Quad/Tri Operators *************************/
3075
3076 static int edbm_quads_convert_to_tris_exec(bContext *C, wmOperator *op)
3077 {
3078         Object *obedit = CTX_data_edit_object(C);
3079         BMEditMesh *em = BMEdit_FromObject(obedit);
3080         int use_beauty = RNA_boolean_get(op->ptr, "use_beauty");
3081
3082         if (!EDBM_op_callf(em, op, "triangulate faces=%hf use_beauty=%b", BM_ELEM_SELECT, use_beauty))
3083                 return OPERATOR_CANCELLED;
3084
3085         EDBM_update_generic(C, em, TRUE);
3086
3087         return OPERATOR_FINISHED;
3088 }
3089
3090 void MESH_OT_quads_convert_to_tris(wmOperatorType *ot)
3091 {
3092         /* identifiers */
3093         ot->name = "Triangulate Faces";
3094         ot->idname = "MESH_OT_quads_convert_to_tris";
3095         ot->description = "Triangulate selected faces";
3096
3097         /* api callbacks */
3098         ot->exec = edbm_quads_convert_to_tris_exec;
3099         ot->poll = ED_operator_editmesh;
3100
3101         /* flags */
3102         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
3103
3104         RNA_def_boolean(ot->srna, "use_beauty", 1, "Beauty", "Use best triangulation division (currently quads only)");
3105 }
3106
3107 static int edbm_tris_convert_to_quads_exec(bContext *C, wmOperator *op)
3108 {
3109         Object *obedit = CTX_data_edit_object(C);
3110         BMEditMesh *em = BMEdit_FromObject(obedit);
3111         int dosharp, douvs, dovcols, domaterials;
3112         float limit = RNA_float_get(op->ptr, "limit");
3113
3114         dosharp = RNA_boolean_get(op->ptr, "sharp");
3115         douvs = RNA_boolean_get(op->ptr, "uvs");
3116         dovcols = RNA_boolean_get(op->ptr, "vcols");
3117         domaterials = RNA_boolean_get(op->ptr, "materials");
3118
3119         if (!EDBM_op_callf(em, op,
3120                            "join_triangles faces=%hf limit=%f cmp_sharp=%b cmp_uvs=%b cmp_vcols=%b cmp_materials=%b",
3121                            BM_ELEM_SELECT, limit, dosharp, douvs, dovcols, domaterials))
3122         {
3123                 return OPERATOR_CANCELLED;
3124         }
3125
3126         EDBM_update_generic(C, em, TRUE);
3127
3128         return OPERATOR_FINISHED;
3129 }
3130
3131 static void join_triangle_props(wmOperatorType *ot)
3132 {
3133         PropertyRNA *prop;
3134
3135         prop = RNA_def_float_rotation(ot->srna, "limit", 0, NULL, 0.0f, DEG2RADF(180.0f),
3136                                       "Max Angle", "Angle Limit", 0.0f, DEG2RADF(180.0f));
3137         RNA_def_property_float_default(prop, DEG2RADF(40.0f));
3138
3139         RNA_def_boolean(ot->srna, "uvs", 0, "Compare UVs", "");
3140         RNA_def_boolean(ot->srna, "vcols", 0, "Compare VCols", "");
3141         RNA_def_boolean(ot->srna, "sharp", 0, "Compare Sharp", "");
3142         RNA_def_boolean(ot->srna, "materials", 0, "Compare Materials", "");
3143 }
3144
3145 void MESH_OT_tris_convert_to_quads(wmOperatorType *ot)
3146 {
3147         /* identifiers */
3148         ot->name = "Tris to Quads";
3149         ot->idname = "MESH_OT_tris_convert_to_quads";
3150         ot->description = "Join triangles into quads";
3151
3152         /* api callbacks */
3153         ot->exec = edbm_tris_convert_to_quads_exec;
3154         ot->poll = ED_operator_editmesh;
3155
3156         /* flags */
3157         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
3158
3159         join_triangle_props(ot);
3160 }
3161
3162 static int edbm_dissolve_exec(bContext *C, wmOperator *op)
3163 {
3164         Object *obedit = CTX_data_edit_object(C);
3165         BMEditMesh *em = BMEdit_FromObject(obedit);
3166
3167         int use_verts = RNA_boolean_get(op->ptr, "use_verts");
3168
3169         if (em->selectmode & SCE_SELECT_FACE) {
3170                 if (!EDBM_op_callf(em, op, "dissolve_faces faces=%hf use_verts=%b", BM_ELEM_SELECT, use_verts))
3171                         return OPERATOR_CANCELLED;
3172         }
3173         else if (em->selectmode & SCE_SELECT_EDGE) {
3174                 if (!EDBM_op_callf(em, op, "dissolve_edges edges=%he use_verts=%b", BM_ELEM_SELECT, use_verts))
3175                         return OPERATOR_CANCELLED;
3176         }
3177         else if (em->selectmode & SCE_SELECT_VERTEX) {
3178                 if (!EDBM_op_callf(em, op, "dissolve_verts verts=%hv", BM_ELEM_SELECT))
3179                         return OPERATOR_CANCELLED;
3180         }
3181
3182         EDBM_update_generic(C, em, TRUE);
3183
3184         return OPERATOR_FINISHED;
3185 }
3186
3187 void MESH_OT_dissolve(wmOperatorType *ot)
3188 {
3189         /* identifiers */
3190         ot->name = "Dissolve";
3191         ot->description = "Dissolve geometry";
3192         ot->idname = "MESH_OT_dissolve";
3193
3194         /* api callbacks */
3195         ot->exec = edbm_dissolve_exec;
3196         ot->poll = ED_operator_editmesh;
3197
3198         /* flags */
3199         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
3200
3201         /* TODO, move dissolve into its own operator so this doesnt confuse non-dissolve options */
3202         RNA_def_boolean(ot->srna, "use_verts", 0, "Dissolve Verts",
3203                         "When dissolving faces/edges, also dissolve remaining vertices");
3204 }
3205
3206 static int edbm_dissolve_limited_exec(bContext *C, wmOperator *op)
3207 {
3208         Object *obedit = CTX_data_edit_object(C);
3209         BMEditMesh *em = BMEdit_FromObject(obedit);
3210         float angle_limit = RNA_float_get(op->ptr, "angle_limit");
3211
3212         if (!EDBM_op_callf(em, op,
3213                            "dissolve_limit edges=%he verts=%hv angle_limit=%f",
3214                            BM_ELEM_SELECT, BM_ELEM_SELECT, angle_limit))
3215         {
3216                 return OPERATOR_CANCELLED;
3217         }
3218
3219         EDBM_update_generic(C, em, TRUE);
3220
3221         return OPERATOR_FINISHED;
3222 }
3223
3224 void MESH_OT_dissolve_limited(wmOperatorType *ot)
3225 {
3226         PropertyRNA *prop;
3227
3228         /* identifiers */
3229         ot->name = "Limited Dissolve";
3230         ot->idname = "MESH_OT_dissolve_limited";
3231         ot->description = "Dissolve selected edges and verts, limited by the angle of surrounding geometry";
3232
3233         /* api callbacks */
3234         ot->exec = edbm_dissolve_limited_exec;
3235         ot->poll = ED_operator_editmesh;
3236
3237         /* flags */
3238         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
3239
3240         prop = RNA_def_float_rotation(ot->srna, "angle_limit", 0, NULL, 0.0f, DEG2RADF(180.0f),
3241                                       "Max Angle", "Angle Limit in Degrees", 0.0f, DEG2RADF(180.0f));
3242         RNA_def_property_float_default(prop, DEG2RADF(15.0f));
3243 }
3244
3245 static int edbm_split_exec(bContext *C, wmOperator *op)
3246 {
3247         Object *ob = CTX_data_edit_object(C);
3248         BMEditMesh *em = BMEdit_FromObject(ob);
3249         BMOperator bmop;
3250
3251         EDBM_op_init(em, &bmop, op, "split geom=%hvef use_only_faces=%b", BM_ELEM_SELECT, FALSE);
3252         BMO_op_exec(em->bm, &bmop);
3253         BM_mesh_elem_hflag_disable_all(em->bm, BM_VERT | BM_EDGE | BM_FACE, BM_ELEM_SELECT, FALSE);
3254         BMO_slot_buffer_hflag_enable(em->bm, &bmop, "geomout", BM_ALL, BM_ELEM_SELECT, TRUE);
3255         if (!EDBM_op_finish(em, &bmop, op, TRUE)) {
3256                 return OPERATOR_CANCELLED;
3257         }
3258
3259         /* Geometry has changed, need to recalc normals and looptris */
3260         EDBM_mesh_normals_update(em);
3261
3262         EDBM_update_generic(C, em, TRUE);
3263
3264         return OPERATOR_FINISHED;
3265 }
3266
3267 void MESH_OT_split(wmOperatorType *ot)
3268 {
3269         /* identifiers */
3270         ot->name = "Split";
3271         ot->idname = "MESH_OT_split";
3272         ot->description = "Split off selected geometry from connected unselected geometry";
3273
3274         /* api callbacks */
3275         ot->exec = edbm_split_exec;
3276         ot->poll = ED_operator_editmesh;
3277
3278         /* flags */
3279         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
3280 }
3281
3282
3283 static int edbm_spin_exec(bContext *C, wmOperator *op)
3284 {
3285         Object *obedit = CTX_data_edit_object(C);
3286         BMEditMesh *em = BMEdit_FromObject(obedit);
3287         BMesh *bm = em->bm;
3288         BMOperator spinop;
3289         float cent[3], axis[3], imat[3][3];
3290         float d[3] = {0.0f, 0.0f, 0.0f};
3291         int steps, dupli;
3292         float degr;
3293
3294         RNA_float_get_array(op->ptr, "center", cent);
3295         RNA_float_get_array(op->ptr, "axis", axis);
3296         steps = RNA_int_get(op->ptr, "steps");
3297         degr = RNA_float_get(op->ptr, "degrees");
3298         //if (ts->editbutflag & B_CLOCKWISE)
3299         degr = -degr;
3300         dupli = RNA_boolean_get(op->ptr, "dupli");
3301
3302         /* undo object transformation */
3303         copy_m3_m4(imat, obedit->imat);
3304         sub_v3_v3(cent, obedit->obmat[3]);
3305         mul_m3_v3(imat, cent);
3306         mul_m3_v3(imat, axis);
3307
3308         if (!EDBM_op_init(em, &spinop, op,
3309                           "spin geom=%hvef cent=%v axis=%v dvec=%v steps=%i ang=%f do_dupli=%b",
3310                           BM_ELEM_SELECT, cent, axis, d, steps, degr, dupli))
3311         {