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