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