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