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