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