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