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_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
54 #include "MEM_guardedalloc.h"
55
56 #include "mesh_intern.h"  /* own include */
57
58 #define USE_MANIPULATOR
59
60 #ifdef USE_MANIPULATOR
61 #endif
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 /** \name Extrude Operator
352  * \{ */
353
354 /* generic extern called extruder */
355 static bool edbm_extrude_mesh(Object *obedit, BMEditMesh *em, wmOperator *op)
356 {
357         bool changed = false;
358         const char htype = edbm_extrude_htype_from_em_select(em);
359         enum {NONE = 0, ELEM_FLAG, VERT_ONLY, EDGE_ONLY} nr;
360
361         if (em->selectmode & SCE_SELECT_VERTEX) {
362                 if      (em->bm->totvertsel == 0) nr = NONE;
363                 else if (em->bm->totvertsel == 1) nr = VERT_ONLY;
364                 else if (em->bm->totedgesel == 0) nr = VERT_ONLY;
365                 else                              nr = ELEM_FLAG;
366         }
367         else if (em->selectmode & SCE_SELECT_EDGE) {
368                 if      (em->bm->totedgesel == 0) nr = NONE;
369                 else if (em->bm->totfacesel == 0) nr = EDGE_ONLY;
370                 else                              nr = ELEM_FLAG;
371         }
372         else {
373                 if      (em->bm->totfacesel == 0) nr = NONE;
374                 else                              nr = ELEM_FLAG;
375         }
376
377         switch (nr) {
378                 case NONE:
379                         return false;
380                 case ELEM_FLAG:
381                         changed = edbm_extrude_ex(obedit, em, htype, BM_ELEM_SELECT, true, true);
382                         break;
383                 case VERT_ONLY:
384                         changed = edbm_extrude_verts_indiv(em, op, BM_ELEM_SELECT);
385                         break;
386                 case EDGE_ONLY:
387                         changed = edbm_extrude_edges_indiv(em, op, BM_ELEM_SELECT);
388                         break;
389         }
390
391         if (changed) {
392                 return true;
393         }
394         else {
395                 BKE_report(op->reports, RPT_ERROR, "Not a valid selection for extrude");
396                 return false;
397         }
398 }
399
400 /* extrude without transform */
401 static int edbm_extrude_region_exec(bContext *C, wmOperator *op)
402 {
403         ViewLayer *view_layer = CTX_data_view_layer(C);
404         uint objects_len = 0;
405         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);
406
407         for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
408                 Object *obedit = objects[ob_index];
409                 BMEditMesh *em = BKE_editmesh_from_object(obedit);
410                 if (em->bm->totvertsel == 0) {
411                         continue;
412                 }
413
414                 edbm_extrude_mesh(obedit, em, op);
415                 /* This normally happens when pushing undo but modal operators
416                  * like this one don't push undo data until after modal mode is
417                  * done.*/
418                 EDBM_mesh_normals_update(em);
419
420                 EDBM_update_generic(em, true, true);
421         }
422         MEM_freeN(objects);
423         return OPERATOR_FINISHED;
424 }
425
426 void MESH_OT_extrude_region(wmOperatorType *ot)
427 {
428         /* identifiers */
429         ot->name = "Extrude Region";
430         ot->idname = "MESH_OT_extrude_region";
431         ot->description = "Extrude region of faces";
432
433         /* api callbacks */
434         //ot->invoke = mesh_extrude_region_invoke;
435         ot->exec = edbm_extrude_region_exec;
436         ot->poll = ED_operator_editmesh;
437
438         /* flags */
439         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
440
441         Transform_Properties(ot, P_NO_DEFAULTS | P_MIRROR_DUMMY);
442 }
443
444 /** \} */
445
446 /* -------------------------------------------------------------------- */
447 /** \name Extrude Verts Operator
448  * \{ */
449
450 static int edbm_extrude_verts_exec(bContext *C, wmOperator *op)
451 {
452         ViewLayer *view_layer = CTX_data_view_layer(C);
453         uint objects_len = 0;
454         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);
455
456         for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
457                 Object *obedit = objects[ob_index];
458                 BMEditMesh *em = BKE_editmesh_from_object(obedit);
459                 if (em->bm->totvertsel == 0) {
460                         continue;
461                 }
462
463                 edbm_extrude_verts_indiv(em, op, BM_ELEM_SELECT);
464
465                 EDBM_update_generic(em, true, true);
466         }
467         MEM_freeN(objects);
468
469         return OPERATOR_FINISHED;
470 }
471
472 void MESH_OT_extrude_verts_indiv(wmOperatorType *ot)
473 {
474         /* identifiers */
475         ot->name = "Extrude Only Vertices";
476         ot->idname = "MESH_OT_extrude_verts_indiv";
477         ot->description = "Extrude individual vertices only";
478
479         /* api callbacks */
480         ot->exec = edbm_extrude_verts_exec;
481         ot->poll = ED_operator_editmesh;
482
483         /* flags */
484         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
485
486         /* to give to transform */
487         Transform_Properties(ot, P_NO_DEFAULTS | P_MIRROR_DUMMY);
488 }
489
490 /** \} */
491
492 /* -------------------------------------------------------------------- */
493 /** \name Extrude Edges Operator
494  * \{ */
495
496 static int edbm_extrude_edges_exec(bContext *C, wmOperator *op)
497 {
498         ViewLayer *view_layer = CTX_data_view_layer(C);
499         uint objects_len = 0;
500         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);
501
502         for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
503                 Object *obedit = objects[ob_index];
504                 BMEditMesh *em = BKE_editmesh_from_object(obedit);
505                 if (em->bm->totedgesel == 0) {
506                         continue;
507                 }
508
509                 edbm_extrude_edges_indiv(em, op, BM_ELEM_SELECT);
510
511                 EDBM_update_generic(em, true, true);
512         }
513         MEM_freeN(objects);
514
515         return OPERATOR_FINISHED;
516 }
517
518 void MESH_OT_extrude_edges_indiv(wmOperatorType *ot)
519 {
520         /* identifiers */
521         ot->name = "Extrude Only Edges";
522         ot->idname = "MESH_OT_extrude_edges_indiv";
523         ot->description = "Extrude individual edges only";
524
525         /* api callbacks */
526         ot->exec = edbm_extrude_edges_exec;
527         ot->poll = ED_operator_editmesh;
528
529         /* flags */
530         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
531
532         /* to give to transform */
533         Transform_Properties(ot, P_NO_DEFAULTS | P_MIRROR_DUMMY);
534 }
535
536 /** \} */
537
538 /* -------------------------------------------------------------------- */
539 /** \name Extrude Faces Operator
540  * \{ */
541
542 static int edbm_extrude_faces_exec(bContext *C, wmOperator *op)
543 {
544         ViewLayer *view_layer = CTX_data_view_layer(C);
545         uint objects_len = 0;
546         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);
547
548         for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
549                 Object *obedit = objects[ob_index];
550                 BMEditMesh *em = BKE_editmesh_from_object(obedit);
551                 if (em->bm->totfacesel == 0) {
552                         continue;
553                 }
554
555                 edbm_extrude_discrete_faces(em, op, BM_ELEM_SELECT);
556
557                 EDBM_update_generic(em, true, true);
558         }
559         MEM_freeN(objects);
560
561         return OPERATOR_FINISHED;
562 }
563
564 void MESH_OT_extrude_faces_indiv(wmOperatorType *ot)
565 {
566         /* identifiers */
567         ot->name = "Extrude Individual Faces";
568         ot->idname = "MESH_OT_extrude_faces_indiv";
569         ot->description = "Extrude individual faces only";
570
571         /* api callbacks */
572         ot->exec = edbm_extrude_faces_exec;
573         ot->poll = ED_operator_editmesh;
574
575         /* flags */
576         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
577
578         Transform_Properties(ot, P_NO_DEFAULTS | P_MIRROR_DUMMY);
579 }
580
581 /** \} */
582
583 /* -------------------------------------------------------------------- */
584 /** \name Dupli-Extrude Operator
585  *
586  * Add-click-mesh (extrude) operator.
587  * \{ */
588
589 static int edbm_dupli_extrude_cursor_invoke(bContext *C, wmOperator *op, const wmEvent *event)
590 {
591         ViewContext vc;
592         BMVert *v1;
593         BMIter iter;
594         float center[3];
595         uint verts_len;
596         bool use_proj;
597
598         em_setup_viewcontext(C, &vc);
599
600         invert_m4_m4(vc.obedit->imat, vc.obedit->obmat);
601
602         ED_view3d_init_mats_rv3d(vc.obedit, vc.rv3d);
603
604         use_proj = ((vc.scene->toolsettings->snap_flag & SCE_SNAP) &&
605                     (vc.scene->toolsettings->snap_mode == SCE_SNAP_MODE_FACE));
606
607         zero_v3(center);
608         verts_len = 0;
609
610         BM_ITER_MESH (v1, &iter, vc.em->bm, BM_VERTS_OF_MESH) {
611                 if (BM_elem_flag_test(v1, BM_ELEM_SELECT)) {
612                         add_v3_v3(center, v1->co);
613                         verts_len += 1;
614                 }
615         }
616
617         /* call extrude? */
618         if (verts_len != 0) {
619                 const char extrude_htype = edbm_extrude_htype_from_em_select(vc.em);
620                 const bool rot_src = RNA_boolean_get(op->ptr, "rotate_source");
621                 BMEdge *eed;
622                 float mat[3][3];
623                 float vec[3], ofs[3];
624                 float nor[3] = {0.0, 0.0, 0.0};
625
626                 /* 2D normal calc */
627                 const float mval_f[2] = {(float)event->mval[0],
628                                          (float)event->mval[1]};
629
630                 mul_v3_fl(center, 1.0f / (float)verts_len);
631
632                 /* check for edges that are half selected, use for rotation */
633                 bool done = false;
634                 BM_ITER_MESH (eed, &iter, vc.em->bm, BM_EDGES_OF_MESH) {
635                         if (BM_elem_flag_test(eed, BM_ELEM_SELECT)) {
636                                 float co1[2], co2[2];
637
638                                 if ((ED_view3d_project_float_object(vc.ar, eed->v1->co, co1, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_OK) &&
639                                     (ED_view3d_project_float_object(vc.ar, eed->v2->co, co2, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_OK))
640                                 {
641                                         /* 2D rotate by 90d while adding.
642                                          *  (x, y) = (y, -x)
643                                          *
644                                          * accumulate the screenspace normal in 2D,
645                                          * with screenspace edge length weighting the result. */
646                                         if (line_point_side_v2(co1, co2, mval_f) >= 0.0f) {
647                                                 nor[0] +=  (co1[1] - co2[1]);
648                                                 nor[1] += -(co1[0] - co2[0]);
649                                         }
650                                         else {
651                                                 nor[0] +=  (co2[1] - co1[1]);
652                                                 nor[1] += -(co2[0] - co1[0]);
653                                         }
654                                         done = true;
655                                 }
656                         }
657                 }
658
659                 if (done) {
660                         float view_vec[3], cross[3];
661
662                         /* convert the 2D nomal into 3D */
663                         mul_mat3_m4_v3(vc.rv3d->viewinv, nor); /* worldspace */
664                         mul_mat3_m4_v3(vc.obedit->imat, nor); /* local space */
665
666                         /* correct the normal to be aligned on the view plane */
667                         mul_v3_mat3_m4v3(view_vec, vc.obedit->imat, vc.rv3d->viewinv[2]);
668                         cross_v3_v3v3(cross, nor, view_vec);
669                         cross_v3_v3v3(nor, view_vec, cross);
670                         normalize_v3(nor);
671                 }
672
673                 /* center */
674                 copy_v3_v3(ofs, center);
675
676                 mul_m4_v3(vc.obedit->obmat, ofs);  /* view space */
677                 ED_view3d_win_to_3d_int(vc.v3d, vc.ar, ofs, event->mval, ofs);
678                 mul_m4_v3(vc.obedit->imat, ofs); // back in object space
679
680                 sub_v3_v3(ofs, center);
681
682                 /* calculate rotation */
683                 unit_m3(mat);
684                 if (done) {
685                         float angle;
686
687                         normalize_v3_v3(vec, ofs);
688
689                         angle = angle_normalized_v3v3(vec, nor);
690
691                         if (angle != 0.0f) {
692                                 float axis[3];
693
694                                 cross_v3_v3v3(axis, nor, vec);
695
696                                 /* halve the rotation if its applied twice */
697                                 if (rot_src) {
698                                         angle *= 0.5f;
699                                 }
700
701                                 axis_angle_to_mat3(mat, axis, angle);
702                         }
703                 }
704
705                 if (rot_src) {
706                         EDBM_op_callf(vc.em, op, "rotate verts=%hv cent=%v matrix=%m3",
707                                       BM_ELEM_SELECT, center, mat);
708
709                         /* also project the source, for retopo workflow */
710                         if (use_proj)
711                                 EMBM_project_snap_verts(C, vc.ar, vc.em);
712                 }
713
714                 edbm_extrude_ex(vc.obedit, vc.em, extrude_htype, BM_ELEM_SELECT, true, true);
715                 EDBM_op_callf(vc.em, op, "rotate verts=%hv cent=%v matrix=%m3",
716                               BM_ELEM_SELECT, center, mat);
717                 EDBM_op_callf(vc.em, op, "translate verts=%hv vec=%v",
718                               BM_ELEM_SELECT, ofs);
719         }
720         else {
721                 const float *cursor = ED_view3d_cursor3d_get(vc.scene, vc.v3d);
722                 BMOperator bmop;
723                 BMOIter oiter;
724
725                 copy_v3_v3(center, cursor);
726                 ED_view3d_win_to_3d_int(vc.v3d, vc.ar, center, event->mval, center);
727
728                 mul_m4_v3(vc.obedit->imat, center); // back in object space
729
730                 EDBM_op_init(vc.em, &bmop, op, "create_vert co=%v", center);
731                 BMO_op_exec(vc.em->bm, &bmop);
732
733                 BMO_ITER (v1, &oiter, bmop.slots_out, "vert.out", BM_VERT) {
734                         BM_vert_select_set(vc.em->bm, v1, true);
735                 }
736
737                 if (!EDBM_op_finish(vc.em, &bmop, op, true)) {
738                         return OPERATOR_CANCELLED;
739                 }
740         }
741
742         if (use_proj)
743                 EMBM_project_snap_verts(C, vc.ar, vc.em);
744
745         /* This normally happens when pushing undo but modal operators
746          * like this one don't push undo data until after modal mode is
747          * done. */
748         EDBM_mesh_normals_update(vc.em);
749
750         EDBM_update_generic(vc.em, true, true);
751
752         return OPERATOR_FINISHED;
753 }
754
755 void MESH_OT_dupli_extrude_cursor(wmOperatorType *ot)
756 {
757         /* identifiers */
758         ot->name = "Duplicate or Extrude to Cursor";
759         ot->idname = "MESH_OT_dupli_extrude_cursor";
760         ot->description = "Duplicate and extrude selected vertices, edges or faces towards the mouse cursor";
761
762         /* api callbacks */
763         ot->invoke = edbm_dupli_extrude_cursor_invoke;
764         ot->poll = ED_operator_editmesh_region_view3d;
765
766         /* flags */
767         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
768
769         RNA_def_boolean(ot->srna, "rotate_source", true, "Rotate Source", "Rotate initial selection giving better shape");
770 }
771
772 /** \} */