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