Merge branch 'master' into blender2.8
[blender.git] / source / blender / editors / mesh / editmesh_extrude.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_extrude.c
29  *  \ingroup edmesh
30  */
31
32 #include "DNA_modifier_types.h"
33 #include "DNA_object_types.h"
34
35 #include "BLI_math.h"
36 #include "BLI_listbase.h"
37
38 #include "BKE_layer.h"
39 #include "BKE_context.h"
40 #include "BKE_report.h"
41 #include "BKE_editmesh.h"
42
43 #include "RNA_define.h"
44 #include "RNA_access.h"
45
46 #include "WM_api.h"
47 #include "WM_types.h"
48
49 #include "ED_mesh.h"
50 #include "ED_screen.h"
51 #include "ED_transform.h"
52 #include "ED_view3d.h"
53 #include "ED_manipulator_library.h"
54
55 #include "UI_resources.h"
56
57 #include "MEM_guardedalloc.h"
58
59 #include "mesh_intern.h"  /* own include */
60
61 #define USE_MANIPULATOR
62
63 /* -------------------------------------------------------------------- */
64 /** \name Extrude Internal Utilities
65  * \{ */
66
67 static void edbm_extrude_edge_exclude_mirror(
68         Object *obedit, BMEditMesh *em,
69         const char hflag,
70         BMOperator *op, BMOpSlot *slot_edges_exclude)
71 {
72         BMesh *bm = em->bm;
73         ModifierData *md;
74
75         /* If a mirror modifier with clipping is on, we need to adjust some
76          * of the cases above to handle edges on the line of symmetry.
77          */
78         for (md = obedit->modifiers.first; md; md = md->next) {
79                 if ((md->type == eModifierType_Mirror) && (md->mode & eModifierMode_Realtime)) {
80                         MirrorModifierData *mmd = (MirrorModifierData *) md;
81
82                         if (mmd->flag & MOD_MIR_CLIPPING) {
83                                 BMIter iter;
84                                 BMEdge *edge;
85
86                                 float mtx[4][4];
87                                 if (mmd->mirror_ob) {
88                                         float imtx[4][4];
89                                         invert_m4_m4(imtx, mmd->mirror_ob->obmat);
90                                         mul_m4_m4m4(mtx, imtx, obedit->obmat);
91                                 }
92
93                                 BM_ITER_MESH (edge, &iter, bm, BM_EDGES_OF_MESH) {
94                                         if (BM_elem_flag_test(edge, hflag) &&
95                                             BM_edge_is_boundary(edge) &&
96                                             BM_elem_flag_test(edge->l->f, hflag))
97                                         {
98                                                 float co1[3], co2[3];
99
100                                                 copy_v3_v3(co1, edge->v1->co);
101                                                 copy_v3_v3(co2, edge->v2->co);
102
103                                                 if (mmd->mirror_ob) {
104                                                         mul_v3_m4v3(co1, mtx, co1);
105                                                         mul_v3_m4v3(co2, mtx, co2);
106                                                 }
107
108                                                 if (mmd->flag & MOD_MIR_AXIS_X) {
109                                                         if ((fabsf(co1[0]) < mmd->tolerance) &&
110                                                             (fabsf(co2[0]) < mmd->tolerance))
111                                                         {
112                                                                 BMO_slot_map_empty_insert(op, slot_edges_exclude, edge);
113                                                         }
114                                                 }
115                                                 if (mmd->flag & MOD_MIR_AXIS_Y) {
116                                                         if ((fabsf(co1[1]) < mmd->tolerance) &&
117                                                             (fabsf(co2[1]) < mmd->tolerance))
118                                                         {
119                                                                 BMO_slot_map_empty_insert(op, slot_edges_exclude, edge);
120                                                         }
121                                                 }
122                                                 if (mmd->flag & MOD_MIR_AXIS_Z) {
123                                                         if ((fabsf(co1[2]) < mmd->tolerance) &&
124                                                             (fabsf(co2[2]) < mmd->tolerance))
125                                                         {
126                                                                 BMO_slot_map_empty_insert(op, slot_edges_exclude, edge);
127                                                         }
128                                                 }
129                                         }
130                                 }
131                         }
132                 }
133         }
134 }
135
136 /* individual face extrude */
137 /* will use vertex normals for extrusion directions, so *nor is unaffected */
138 static bool edbm_extrude_discrete_faces(BMEditMesh *em, wmOperator *op, const char hflag)
139 {
140         BMOIter siter;
141         BMIter liter;
142         BMFace *f;
143         BMLoop *l;
144         BMOperator bmop;
145
146         EDBM_op_init(
147                 em, &bmop, op,
148                 "extrude_discrete_faces faces=%hf use_select_history=%b",
149                 hflag, true);
150
151         /* deselect original verts */
152         EDBM_flag_disable_all(em, BM_ELEM_SELECT);
153
154         BMO_op_exec(em->bm, &bmop);
155
156         BMO_ITER (f, &siter, bmop.slots_out, "faces.out", BM_FACE) {
157                 BM_face_select_set(em->bm, f, true);
158
159                 /* set face vertex normals to face normal */
160                 BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
161                         copy_v3_v3(l->v->no, f->no);
162                 }
163         }
164
165         if (!EDBM_op_finish(em, &bmop, op, true)) {
166                 return false;
167         }
168
169         return true;
170 }
171
172 /* extrudes individual edges */
173 static bool edbm_extrude_edges_indiv(BMEditMesh *em, wmOperator *op, const char hflag)
174 {
175         BMesh *bm = em->bm;
176         BMOperator bmop;
177
178         EDBM_op_init(
179                 em, &bmop, op,
180                 "extrude_edge_only edges=%he use_select_history=%b",
181                 hflag, true);
182
183         /* deselect original verts */
184         BM_SELECT_HISTORY_BACKUP(bm);
185         EDBM_flag_disable_all(em, BM_ELEM_SELECT);
186         BM_SELECT_HISTORY_RESTORE(bm);
187
188         BMO_op_exec(em->bm, &bmop);
189         BMO_slot_buffer_hflag_enable(em->bm, bmop.slots_out, "geom.out", BM_VERT | BM_EDGE, BM_ELEM_SELECT, true);
190
191         if (!EDBM_op_finish(em, &bmop, op, true)) {
192                 return false;
193         }
194
195         return true;
196 }
197
198 /* extrudes individual vertices */
199 static bool edbm_extrude_verts_indiv(BMEditMesh *em, wmOperator *op, const char hflag)
200 {
201         BMOperator bmop;
202
203         EDBM_op_init(
204                 em, &bmop, op,
205                 "extrude_vert_indiv verts=%hv use_select_history=%b",
206                 hflag, true);
207
208         /* deselect original verts */
209         BMO_slot_buffer_hflag_disable(em->bm, bmop.slots_in, "verts", BM_VERT, BM_ELEM_SELECT, true);
210
211         BMO_op_exec(em->bm, &bmop);
212         BMO_slot_buffer_hflag_enable(em->bm, bmop.slots_out, "verts.out", BM_VERT, BM_ELEM_SELECT, true);
213
214         if (!EDBM_op_finish(em, &bmop, op, true)) {
215                 return false;
216         }
217
218         return true;
219 }
220
221 static char edbm_extrude_htype_from_em_select(BMEditMesh *em)
222 {
223         char htype = BM_ALL_NOLOOP;
224
225         if (em->selectmode & SCE_SELECT_VERTEX) {
226                 /* pass */
227         }
228         else if (em->selectmode & SCE_SELECT_EDGE) {
229                 htype &= ~BM_VERT;
230         }
231         else {
232                 htype &= ~(BM_VERT | BM_EDGE);
233         }
234
235         if (em->bm->totedgesel == 0) {
236                 htype &= ~(BM_EDGE | BM_FACE);
237         }
238         else if (em->bm->totfacesel == 0) {
239                 htype &= ~BM_FACE;
240         }
241
242         return htype;
243 }
244
245 static bool edbm_extrude_ex(
246         Object *obedit, BMEditMesh *em,
247         char htype, const char hflag,
248         const bool use_mirror,
249         const bool use_select_history)
250 {
251         BMesh *bm = em->bm;
252         BMOIter siter;
253         BMOperator extop;
254         BMElem *ele;
255
256         /* needed to remove the faces left behind */
257         if (htype & BM_FACE) {
258                 htype |= BM_EDGE;
259         }
260
261         BMO_op_init(bm, &extop, BMO_FLAG_DEFAULTS, "extrude_face_region");
262         BMO_slot_bool_set(extop.slots_in, "use_select_history", use_select_history);
263         BMO_slot_buffer_from_enabled_hflag(bm, &extop, extop.slots_in, "geom", htype, hflag);
264
265         if (use_mirror) {
266                 BMOpSlot *slot_edges_exclude;
267                 slot_edges_exclude = BMO_slot_get(extop.slots_in, "edges_exclude");
268
269                 edbm_extrude_edge_exclude_mirror(obedit, em, hflag, &extop, slot_edges_exclude);
270         }
271
272         BM_SELECT_HISTORY_BACKUP(bm);
273         EDBM_flag_disable_all(em, BM_ELEM_SELECT);
274         BM_SELECT_HISTORY_RESTORE(bm);
275
276         BMO_op_exec(bm, &extop);
277
278         BMO_ITER (ele, &siter, extop.slots_out, "geom.out", BM_ALL_NOLOOP) {
279                 BM_elem_select_set(bm, ele, true);
280         }
281
282         BMO_op_finish(bm, &extop);
283
284         return true;
285 }
286
287 /** \} */
288
289 /* -------------------------------------------------------------------- */
290 /** \name Extrude Repeat Operator
291  * \{ */
292
293 static int edbm_extrude_repeat_exec(bContext *C, wmOperator *op)
294 {
295         Object *obedit = CTX_data_edit_object(C);
296         BMEditMesh *em = BKE_editmesh_from_object(obedit);
297         RegionView3D *rv3d = CTX_wm_region_view3d(C);
298
299         const int steps = RNA_int_get(op->ptr, "steps");
300
301         const float offs = RNA_float_get(op->ptr, "offset");
302         float dvec[3], tmat[3][3], bmat[3][3];
303         short a;
304
305         /* dvec */
306         normalize_v3_v3_length(dvec, rv3d->persinv[2], offs);
307
308         /* base correction */
309         copy_m3_m4(bmat, obedit->obmat);
310         invert_m3_m3(tmat, bmat);
311         mul_m3_v3(tmat, dvec);
312
313         for (a = 0; a < steps; a++) {
314                 edbm_extrude_ex(obedit, em, BM_ALL_NOLOOP, BM_ELEM_SELECT, false, false);
315
316                 BMO_op_callf(
317                         em->bm, BMO_FLAG_DEFAULTS,
318                         "translate vec=%v verts=%hv",
319                         dvec, BM_ELEM_SELECT);
320         }
321
322         EDBM_mesh_normals_update(em);
323
324         EDBM_update_generic(em, true, true);
325
326         return OPERATOR_FINISHED;
327 }
328
329 void MESH_OT_extrude_repeat(wmOperatorType *ot)
330 {
331         /* identifiers */
332         ot->name = "Extrude Repeat Mesh";
333         ot->description = "Extrude selected vertices, edges or faces repeatedly";
334         ot->idname = "MESH_OT_extrude_repeat";
335
336         /* api callbacks */
337         ot->exec = edbm_extrude_repeat_exec;
338         ot->poll = ED_operator_editmesh_view3d;
339
340         /* flags */
341         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
342
343         /* props */
344         RNA_def_float_distance(ot->srna, "offset", 2.0f, 0.0f, 1e12f, "Offset", "", 0.0f, 100.0f);
345         RNA_def_int(ot->srna, "steps", 10, 0, 1000000, "Steps", "", 0, 180);
346 }
347
348 /** \} */
349
350
351 /* -------------------------------------------------------------------- */
352 /** \name Extrude Manipulator
353  * \{ */
354
355 #ifdef USE_MANIPULATOR
356
357 typedef struct ManipulatorExtrudeGroup {
358         /* Extrude . */
359         struct wmManipulator *axis_arrow;
360         /* Redo Z-axis translation. */
361         struct wmManipulator *axis_redo;
362 } ManipulatorExtrudeGroup;
363
364 static bool manipulator_mesh_extrude_poll(const bContext *C, wmManipulatorGroupType *wgt)
365 {
366         WorkSpace *workspace = CTX_wm_workspace(C);
367         if (!STREQ(workspace->tool.manipulator_group, "MESH_WGT_extrude") ||
368             !ED_operator_editmesh_view3d((bContext *)C))
369         {
370                 WM_manipulator_group_type_unlink_delayed_ptr(wgt);
371                 return false;
372         }
373         return true;
374 }
375
376 static void manipulator_mesh_extrude_setup(const bContext *UNUSED(C), wmManipulatorGroup *mgroup)
377 {
378         const int constraint_z[3] = {0, 0, 1};
379
380         struct ManipulatorExtrudeGroup *man = MEM_callocN(sizeof(ManipulatorExtrudeGroup), __func__);
381         mgroup->customdata = man;
382
383         const wmManipulatorType *wt_arrow = WM_manipulatortype_find("MANIPULATOR_WT_arrow_3d", true);
384         const wmManipulatorType *wt_grab = WM_manipulatortype_find("MANIPULATOR_WT_grab_3d", true);
385
386         man->axis_arrow = WM_manipulator_new_ptr(wt_arrow, mgroup, NULL);
387         man->axis_redo = WM_manipulator_new_ptr(wt_grab, mgroup, NULL);
388
389         UI_GetThemeColor3fv(TH_MANIPULATOR_PRIMARY, man->axis_arrow->color);
390         UI_GetThemeColor3fv(TH_MANIPULATOR_SECONDARY, man->axis_redo->color);
391
392         WM_manipulator_set_scale(man->axis_arrow, 2.0);
393         WM_manipulator_set_scale(man->axis_redo, 2.0);
394
395         RNA_enum_set(man->axis_arrow->ptr, "draw_style", ED_MANIPULATOR_ARROW_STYLE_NORMAL);
396         RNA_enum_set(man->axis_redo->ptr, "draw_style", ED_MANIPULATOR_GRAB_STYLE_RING_2D);
397
398         WM_manipulator_set_flag(man->axis_redo, WM_MANIPULATOR_DRAW_VALUE, true);
399
400         /* New extrude. */
401         {
402                 wmOperatorType *ot = WM_operatortype_find("MESH_OT_extrude_region_move", true);
403                 PointerRNA *ptr = WM_manipulator_operator_set(man->axis_arrow, 0, ot, NULL);
404                 {
405                         PointerRNA macroptr = RNA_pointer_get(ptr, "TRANSFORM_OT_translate");
406                         RNA_boolean_set(&macroptr, "release_confirm", true);
407                         RNA_boolean_set_array(&macroptr, "constraint_axis", constraint_z);
408                 }
409         }
410         /* Adjust extrude. */
411         {
412                 wmOperatorType *ot = WM_operatortype_find("MESH_OT_extrude_region_move", true);
413                 PointerRNA *ptr = WM_manipulator_operator_set(man->axis_redo, 0, ot, NULL);
414                 {
415                         PointerRNA macroptr = RNA_pointer_get(ptr, "TRANSFORM_OT_translate");
416                         RNA_boolean_set(&macroptr, "release_confirm", true);
417                         RNA_boolean_set_array(&macroptr, "constraint_axis", constraint_z);
418                 }
419                 wmManipulatorOpElem *mpop = WM_manipulator_operator_get(man->axis_redo, 0);
420                 mpop->is_redo = true;
421         }
422 }
423
424 static void manipulator_mesh_extrude_refresh(const bContext *C, wmManipulatorGroup *mgroup)
425 {
426         ManipulatorExtrudeGroup *man = mgroup->customdata;
427
428         WM_manipulator_set_flag(man->axis_arrow, WM_MANIPULATOR_HIDDEN, true);
429         WM_manipulator_set_flag(man->axis_redo, WM_MANIPULATOR_HIDDEN, true);
430
431         struct TransformBounds tbounds;
432         if (!ED_transform_calc_manipulator_stats(C, false, &tbounds)) {
433                 return;
434         }
435
436         /* Needed for normal orientation. */
437         copy_m4_m3(man->axis_arrow->matrix_basis, tbounds.axis);
438         copy_m4_m3(man->axis_redo->matrix_basis, tbounds.axis);
439
440         WM_manipulator_set_matrix_location(man->axis_arrow, tbounds.center);
441         WM_manipulator_set_matrix_location(man->axis_redo, tbounds.center);
442
443         wmOperator *op = WM_operator_last_redo(C);
444         bool has_redo = (op && STREQ(op->type->idname, "MESH_OT_extrude_region_move"));
445
446         WM_manipulator_set_flag(man->axis_arrow, WM_MANIPULATOR_HIDDEN, false);
447         WM_manipulator_set_flag(man->axis_redo, WM_MANIPULATOR_HIDDEN, !has_redo);
448
449         /* Redo with current settings. */
450         if (has_redo) {
451                 /* We could also access this from 'ot->last_properties' */
452
453                 wmOperator *op_transform = op->macro.last;
454                 wmManipulatorOpElem *mpop = WM_manipulator_operator_get(man->axis_redo, 0);
455
456                 PointerRNA macroptr = RNA_pointer_get(&mpop->ptr, "TRANSFORM_OT_translate");
457
458                 float value[4];
459                 RNA_float_get_array(op_transform->ptr, "value", value);
460                 RNA_float_set_array(&macroptr, "value", value);
461
462                 /* Currently has glitch in re-applying. */
463 #if 0
464                 int constraint_axis[3];
465                 RNA_boolean_get_array(op_transform->ptr, "constraint_axis", constraint_axis);
466                 RNA_boolean_set_array(&macroptr, "constraint_axis", constraint_axis);
467 #endif
468         }
469 }
470
471 static void MESH_WGT_extrude(struct wmManipulatorGroupType *wgt)
472 {
473         wgt->name = "Mesh Extrude";
474         wgt->idname = "MESH_WGT_extrude";
475
476         wgt->flag = WM_MANIPULATORGROUPTYPE_3D;
477
478         wgt->mmap_params.spaceid = SPACE_VIEW3D;
479         wgt->mmap_params.regionid = RGN_TYPE_WINDOW;
480
481         wgt->poll = manipulator_mesh_extrude_poll;
482         wgt->setup = manipulator_mesh_extrude_setup;
483         wgt->refresh = manipulator_mesh_extrude_refresh;
484 }
485
486 #endif  /* USE_MANIPULATOR */
487
488 /** \} */
489
490 /* -------------------------------------------------------------------- */
491 /** \name Extrude Operator
492  * \{ */
493
494 /* generic extern called extruder */
495 static bool edbm_extrude_mesh(Object *obedit, BMEditMesh *em, wmOperator *op)
496 {
497         bool changed = false;
498         const char htype = edbm_extrude_htype_from_em_select(em);
499         enum {NONE = 0, ELEM_FLAG, VERT_ONLY, EDGE_ONLY} nr;
500
501         if (em->selectmode & SCE_SELECT_VERTEX) {
502                 if      (em->bm->totvertsel == 0) nr = NONE;
503                 else if (em->bm->totvertsel == 1) nr = VERT_ONLY;
504                 else if (em->bm->totedgesel == 0) nr = VERT_ONLY;
505                 else                              nr = ELEM_FLAG;
506         }
507         else if (em->selectmode & SCE_SELECT_EDGE) {
508                 if      (em->bm->totedgesel == 0) nr = NONE;
509                 else if (em->bm->totfacesel == 0) nr = EDGE_ONLY;
510                 else                              nr = ELEM_FLAG;
511         }
512         else {
513                 if      (em->bm->totfacesel == 0) nr = NONE;
514                 else                              nr = ELEM_FLAG;
515         }
516
517         switch (nr) {
518                 case NONE:
519                         return false;
520                 case ELEM_FLAG:
521                         changed = edbm_extrude_ex(obedit, em, htype, BM_ELEM_SELECT, true, true);
522                         break;
523                 case VERT_ONLY:
524                         changed = edbm_extrude_verts_indiv(em, op, BM_ELEM_SELECT);
525                         break;
526                 case EDGE_ONLY:
527                         changed = edbm_extrude_edges_indiv(em, op, BM_ELEM_SELECT);
528                         break;
529         }
530
531         if (changed) {
532                 return true;
533         }
534         else {
535                 BKE_report(op->reports, RPT_ERROR, "Not a valid selection for extrude");
536                 return false;
537         }
538 }
539
540 /* extrude without transform */
541 static int edbm_extrude_region_exec(bContext *C, wmOperator *op)
542 {
543         ViewLayer *view_layer = CTX_data_view_layer(C);
544         uint objects_len = 0;
545         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);
546
547         for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
548                 Object *obedit = objects[ob_index];
549                 BMEditMesh *em = BKE_editmesh_from_object(obedit);
550                 if (em->bm->totvertsel == 0) {
551                         continue;
552                 }
553
554                 edbm_extrude_mesh(obedit, em, op);
555                 /* This normally happens when pushing undo but modal operators
556                  * like this one don't push undo data until after modal mode is
557                  * done.*/
558                 EDBM_mesh_normals_update(em);
559
560                 EDBM_update_generic(em, true, true);
561         }
562         MEM_freeN(objects);
563         return OPERATOR_FINISHED;
564 }
565
566 void MESH_OT_extrude_region(wmOperatorType *ot)
567 {
568         /* identifiers */
569         ot->name = "Extrude Region";
570         ot->idname = "MESH_OT_extrude_region";
571         ot->description = "Extrude region of faces";
572
573         /* api callbacks */
574         //ot->invoke = mesh_extrude_region_invoke;
575         ot->exec = edbm_extrude_region_exec;
576         ot->poll = ED_operator_editmesh;
577
578         /* flags */
579         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
580
581         Transform_Properties(ot, P_NO_DEFAULTS | P_MIRROR_DUMMY);
582
583 #ifdef USE_MANIPULATOR
584         WM_manipulatorgrouptype_append(MESH_WGT_extrude);
585 #endif
586 }
587
588 /** \} */
589
590 /* -------------------------------------------------------------------- */
591 /** \name Extrude Verts Operator
592  * \{ */
593
594 static int edbm_extrude_verts_exec(bContext *C, wmOperator *op)
595 {
596         ViewLayer *view_layer = CTX_data_view_layer(C);
597         uint objects_len = 0;
598         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);
599
600         for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
601                 Object *obedit = objects[ob_index];
602                 BMEditMesh *em = BKE_editmesh_from_object(obedit);
603                 if (em->bm->totvertsel == 0) {
604                         continue;
605                 }
606
607                 edbm_extrude_verts_indiv(em, op, BM_ELEM_SELECT);
608
609                 EDBM_update_generic(em, true, true);
610         }
611         MEM_freeN(objects);
612
613         return OPERATOR_FINISHED;
614 }
615
616 void MESH_OT_extrude_verts_indiv(wmOperatorType *ot)
617 {
618         /* identifiers */
619         ot->name = "Extrude Only Vertices";
620         ot->idname = "MESH_OT_extrude_verts_indiv";
621         ot->description = "Extrude individual vertices only";
622
623         /* api callbacks */
624         ot->exec = edbm_extrude_verts_exec;
625         ot->poll = ED_operator_editmesh;
626
627         /* flags */
628         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
629
630         /* to give to transform */
631         Transform_Properties(ot, P_NO_DEFAULTS | P_MIRROR_DUMMY);
632 }
633
634 /** \} */
635
636 /* -------------------------------------------------------------------- */
637 /** \name Extrude Edges Operator
638  * \{ */
639
640 static int edbm_extrude_edges_exec(bContext *C, wmOperator *op)
641 {
642         ViewLayer *view_layer = CTX_data_view_layer(C);
643         uint objects_len = 0;
644         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);
645
646         for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
647                 Object *obedit = objects[ob_index];
648                 BMEditMesh *em = BKE_editmesh_from_object(obedit);
649                 if (em->bm->totedgesel == 0) {
650                         continue;
651                 }
652
653                 edbm_extrude_edges_indiv(em, op, BM_ELEM_SELECT);
654
655                 EDBM_update_generic(em, true, true);
656         }
657         MEM_freeN(objects);
658
659         return OPERATOR_FINISHED;
660 }
661
662 void MESH_OT_extrude_edges_indiv(wmOperatorType *ot)
663 {
664         /* identifiers */
665         ot->name = "Extrude Only Edges";
666         ot->idname = "MESH_OT_extrude_edges_indiv";
667         ot->description = "Extrude individual edges only";
668
669         /* api callbacks */
670         ot->exec = edbm_extrude_edges_exec;
671         ot->poll = ED_operator_editmesh;
672
673         /* flags */
674         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
675
676         /* to give to transform */
677         Transform_Properties(ot, P_NO_DEFAULTS | P_MIRROR_DUMMY);
678 }
679
680 /** \} */
681
682 /* -------------------------------------------------------------------- */
683 /** \name Extrude Faces Operator
684  * \{ */
685
686 static int edbm_extrude_faces_exec(bContext *C, wmOperator *op)
687 {
688         ViewLayer *view_layer = CTX_data_view_layer(C);
689         uint objects_len = 0;
690         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);
691
692         for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
693                 Object *obedit = objects[ob_index];
694                 BMEditMesh *em = BKE_editmesh_from_object(obedit);
695                 if (em->bm->totfacesel == 0) {
696                         continue;
697                 }
698
699                 edbm_extrude_discrete_faces(em, op, BM_ELEM_SELECT);
700
701                 EDBM_update_generic(em, true, true);
702         }
703         MEM_freeN(objects);
704
705         return OPERATOR_FINISHED;
706 }
707
708 void MESH_OT_extrude_faces_indiv(wmOperatorType *ot)
709 {
710         /* identifiers */
711         ot->name = "Extrude Individual Faces";
712         ot->idname = "MESH_OT_extrude_faces_indiv";
713         ot->description = "Extrude individual faces only";
714
715         /* api callbacks */
716         ot->exec = edbm_extrude_faces_exec;
717         ot->poll = ED_operator_editmesh;
718
719         /* flags */
720         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
721
722         Transform_Properties(ot, P_NO_DEFAULTS | P_MIRROR_DUMMY);
723 }
724
725 /** \} */
726
727 /* -------------------------------------------------------------------- */
728 /** \name Dupli-Extrude Operator
729  *
730  * Add-click-mesh (extrude) operator.
731  * \{ */
732
733 static int edbm_dupli_extrude_cursor_invoke(bContext *C, wmOperator *op, const wmEvent *event)
734 {
735         ViewContext vc;
736         BMVert *v1;
737         BMIter iter;
738         float center[3];
739         uint verts_len;
740         bool use_proj;
741
742         em_setup_viewcontext(C, &vc);
743
744         invert_m4_m4(vc.obedit->imat, vc.obedit->obmat);
745
746         ED_view3d_init_mats_rv3d(vc.obedit, vc.rv3d);
747
748         use_proj = ((vc.scene->toolsettings->snap_flag & SCE_SNAP) &&
749                     (vc.scene->toolsettings->snap_mode == SCE_SNAP_MODE_FACE));
750
751         zero_v3(center);
752         verts_len = 0;
753
754         BM_ITER_MESH (v1, &iter, vc.em->bm, BM_VERTS_OF_MESH) {
755                 if (BM_elem_flag_test(v1, BM_ELEM_SELECT)) {
756                         add_v3_v3(center, v1->co);
757                         verts_len += 1;
758                 }
759         }
760
761         /* call extrude? */
762         if (verts_len != 0) {
763                 const char extrude_htype = edbm_extrude_htype_from_em_select(vc.em);
764                 const bool rot_src = RNA_boolean_get(op->ptr, "rotate_source");
765                 BMEdge *eed;
766                 float mat[3][3];
767                 float vec[3], ofs[3];
768                 float nor[3] = {0.0, 0.0, 0.0};
769
770                 /* 2D normal calc */
771                 const float mval_f[2] = {(float)event->mval[0],
772                                          (float)event->mval[1]};
773
774                 mul_v3_fl(center, 1.0f / (float)verts_len);
775
776                 /* check for edges that are half selected, use for rotation */
777                 bool done = false;
778                 BM_ITER_MESH (eed, &iter, vc.em->bm, BM_EDGES_OF_MESH) {
779                         if (BM_elem_flag_test(eed, BM_ELEM_SELECT)) {
780                                 float co1[2], co2[2];
781
782                                 if ((ED_view3d_project_float_object(vc.ar, eed->v1->co, co1, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_OK) &&
783                                     (ED_view3d_project_float_object(vc.ar, eed->v2->co, co2, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_OK))
784                                 {
785                                         /* 2D rotate by 90d while adding.
786                                          *  (x, y) = (y, -x)
787                                          *
788                                          * accumulate the screenspace normal in 2D,
789                                          * with screenspace edge length weighting the result. */
790                                         if (line_point_side_v2(co1, co2, mval_f) >= 0.0f) {
791                                                 nor[0] +=  (co1[1] - co2[1]);
792                                                 nor[1] += -(co1[0] - co2[0]);
793                                         }
794                                         else {
795                                                 nor[0] +=  (co2[1] - co1[1]);
796                                                 nor[1] += -(co2[0] - co1[0]);
797                                         }
798                                         done = true;
799                                 }
800                         }
801                 }
802
803                 if (done) {
804                         float view_vec[3], cross[3];
805
806                         /* convert the 2D nomal into 3D */
807                         mul_mat3_m4_v3(vc.rv3d->viewinv, nor); /* worldspace */
808                         mul_mat3_m4_v3(vc.obedit->imat, nor); /* local space */
809
810                         /* correct the normal to be aligned on the view plane */
811                         mul_v3_mat3_m4v3(view_vec, vc.obedit->imat, vc.rv3d->viewinv[2]);
812                         cross_v3_v3v3(cross, nor, view_vec);
813                         cross_v3_v3v3(nor, view_vec, cross);
814                         normalize_v3(nor);
815                 }
816
817                 /* center */
818                 copy_v3_v3(ofs, center);
819
820                 mul_m4_v3(vc.obedit->obmat, ofs);  /* view space */
821                 ED_view3d_win_to_3d_int(vc.v3d, vc.ar, ofs, event->mval, ofs);
822                 mul_m4_v3(vc.obedit->imat, ofs); // back in object space
823
824                 sub_v3_v3(ofs, center);
825
826                 /* calculate rotation */
827                 unit_m3(mat);
828                 if (done) {
829                         float angle;
830
831                         normalize_v3_v3(vec, ofs);
832
833                         angle = angle_normalized_v3v3(vec, nor);
834
835                         if (angle != 0.0f) {
836                                 float axis[3];
837
838                                 cross_v3_v3v3(axis, nor, vec);
839
840                                 /* halve the rotation if its applied twice */
841                                 if (rot_src) {
842                                         angle *= 0.5f;
843                                 }
844
845                                 axis_angle_to_mat3(mat, axis, angle);
846                         }
847                 }
848
849                 if (rot_src) {
850                         EDBM_op_callf(vc.em, op, "rotate verts=%hv cent=%v matrix=%m3",
851                                       BM_ELEM_SELECT, center, mat);
852
853                         /* also project the source, for retopo workflow */
854                         if (use_proj)
855                                 EMBM_project_snap_verts(C, vc.ar, vc.em);
856                 }
857
858                 edbm_extrude_ex(vc.obedit, vc.em, extrude_htype, BM_ELEM_SELECT, true, true);
859                 EDBM_op_callf(vc.em, op, "rotate verts=%hv cent=%v matrix=%m3",
860                               BM_ELEM_SELECT, center, mat);
861                 EDBM_op_callf(vc.em, op, "translate verts=%hv vec=%v",
862                               BM_ELEM_SELECT, ofs);
863         }
864         else {
865                 const float *cursor = ED_view3d_cursor3d_get(vc.scene, vc.v3d);
866                 BMOperator bmop;
867                 BMOIter oiter;
868
869                 copy_v3_v3(center, cursor);
870                 ED_view3d_win_to_3d_int(vc.v3d, vc.ar, center, event->mval, center);
871
872                 mul_m4_v3(vc.obedit->imat, center); // back in object space
873
874                 EDBM_op_init(vc.em, &bmop, op, "create_vert co=%v", center);
875                 BMO_op_exec(vc.em->bm, &bmop);
876
877                 BMO_ITER (v1, &oiter, bmop.slots_out, "vert.out", BM_VERT) {
878                         BM_vert_select_set(vc.em->bm, v1, true);
879                 }
880
881                 if (!EDBM_op_finish(vc.em, &bmop, op, true)) {
882                         return OPERATOR_CANCELLED;
883                 }
884         }
885
886         if (use_proj)
887                 EMBM_project_snap_verts(C, vc.ar, vc.em);
888
889         /* This normally happens when pushing undo but modal operators
890          * like this one don't push undo data until after modal mode is
891          * done. */
892         EDBM_mesh_normals_update(vc.em);
893
894         EDBM_update_generic(vc.em, true, true);
895
896         return OPERATOR_FINISHED;
897 }
898
899 void MESH_OT_dupli_extrude_cursor(wmOperatorType *ot)
900 {
901         /* identifiers */
902         ot->name = "Duplicate or Extrude to Cursor";
903         ot->idname = "MESH_OT_dupli_extrude_cursor";
904         ot->description = "Duplicate and extrude selected vertices, edges or faces towards the mouse cursor";
905
906         /* api callbacks */
907         ot->invoke = edbm_dupli_extrude_cursor_invoke;
908         ot->poll = ED_operator_editmesh_region_view3d;
909
910         /* flags */
911         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
912
913         RNA_def_boolean(ot->srna, "rotate_source", true, "Rotate Source", "Rotate initial selection giving better shape");
914 }
915
916 /** \} */