Welcome to mirror list, hosted at ThFree Co, Russian Federation.

editmesh_extrude.c « mesh « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4841de3c856911ea91d420e83a60d68ff167deee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
/*
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2004 by Blender Foundation.
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): Joseph Eagar
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file blender/editors/mesh/editmesh_extrude.c
 *  \ingroup edmesh
 */

#include "DNA_modifier_types.h"
#include "DNA_object_types.h"

#include "BLI_math.h"
#include "BLI_listbase.h"

#include "BKE_layer.h"
#include "BKE_context.h"
#include "BKE_report.h"
#include "BKE_editmesh.h"
#include "BKE_global.h"
#include "BKE_idprop.h"

#include "RNA_define.h"
#include "RNA_access.h"

#include "WM_api.h"
#include "WM_types.h"
#include "WM_message.h"

#include "ED_mesh.h"
#include "ED_screen.h"
#include "ED_transform.h"
#include "ED_view3d.h"
#include "ED_manipulator_library.h"

#include "UI_resources.h"

#include "MEM_guardedalloc.h"

#include "mesh_intern.h"  /* own include */

#define USE_MANIPULATOR

/* -------------------------------------------------------------------- */
/** \name Extrude Internal Utilities
 * \{ */

static void edbm_extrude_edge_exclude_mirror(
        Object *obedit, BMEditMesh *em,
        const char hflag,
        BMOperator *op, BMOpSlot *slot_edges_exclude)
{
	BMesh *bm = em->bm;
	ModifierData *md;

	/* If a mirror modifier with clipping is on, we need to adjust some
	 * of the cases above to handle edges on the line of symmetry.
	 */
	for (md = obedit->modifiers.first; md; md = md->next) {
		if ((md->type == eModifierType_Mirror) && (md->mode & eModifierMode_Realtime)) {
			MirrorModifierData *mmd = (MirrorModifierData *) md;

			if (mmd->flag & MOD_MIR_CLIPPING) {
				BMIter iter;
				BMEdge *edge;

				float mtx[4][4];
				if (mmd->mirror_ob) {
					float imtx[4][4];
					invert_m4_m4(imtx, mmd->mirror_ob->obmat);
					mul_m4_m4m4(mtx, imtx, obedit->obmat);
				}

				BM_ITER_MESH (edge, &iter, bm, BM_EDGES_OF_MESH) {
					if (BM_elem_flag_test(edge, hflag) &&
					    BM_edge_is_boundary(edge) &&
					    BM_elem_flag_test(edge->l->f, hflag))
					{
						float co1[3], co2[3];

						copy_v3_v3(co1, edge->v1->co);
						copy_v3_v3(co2, edge->v2->co);

						if (mmd->mirror_ob) {
							mul_v3_m4v3(co1, mtx, co1);
							mul_v3_m4v3(co2, mtx, co2);
						}

						if (mmd->flag & MOD_MIR_AXIS_X) {
							if ((fabsf(co1[0]) < mmd->tolerance) &&
							    (fabsf(co2[0]) < mmd->tolerance))
							{
								BMO_slot_map_empty_insert(op, slot_edges_exclude, edge);
							}
						}
						if (mmd->flag & MOD_MIR_AXIS_Y) {
							if ((fabsf(co1[1]) < mmd->tolerance) &&
							    (fabsf(co2[1]) < mmd->tolerance))
							{
								BMO_slot_map_empty_insert(op, slot_edges_exclude, edge);
							}
						}
						if (mmd->flag & MOD_MIR_AXIS_Z) {
							if ((fabsf(co1[2]) < mmd->tolerance) &&
							    (fabsf(co2[2]) < mmd->tolerance))
							{
								BMO_slot_map_empty_insert(op, slot_edges_exclude, edge);
							}
						}
					}
				}
			}
		}
	}
}

/* individual face extrude */
/* will use vertex normals for extrusion directions, so *nor is unaffected */
static bool edbm_extrude_discrete_faces(BMEditMesh *em, wmOperator *op, const char hflag)
{
	BMOIter siter;
	BMIter liter;
	BMFace *f;
	BMLoop *l;
	BMOperator bmop;

	EDBM_op_init(
	        em, &bmop, op,
	        "extrude_discrete_faces faces=%hf use_select_history=%b",
	        hflag, true);

	/* deselect original verts */
	EDBM_flag_disable_all(em, BM_ELEM_SELECT);

	BMO_op_exec(em->bm, &bmop);

	BMO_ITER (f, &siter, bmop.slots_out, "faces.out", BM_FACE) {
		BM_face_select_set(em->bm, f, true);

		/* set face vertex normals to face normal */
		BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
			copy_v3_v3(l->v->no, f->no);
		}
	}

	if (!EDBM_op_finish(em, &bmop, op, true)) {
		return false;
	}

	return true;
}

/* extrudes individual edges */
static bool edbm_extrude_edges_indiv(BMEditMesh *em, wmOperator *op, const char hflag)
{
	BMesh *bm = em->bm;
	BMOperator bmop;

	EDBM_op_init(
	        em, &bmop, op,
	        "extrude_edge_only edges=%he use_select_history=%b",
	        hflag, true);

	/* deselect original verts */
	BM_SELECT_HISTORY_BACKUP(bm);
	EDBM_flag_disable_all(em, BM_ELEM_SELECT);
	BM_SELECT_HISTORY_RESTORE(bm);

	BMO_op_exec(em->bm, &bmop);
	BMO_slot_buffer_hflag_enable(em->bm, bmop.slots_out, "geom.out", BM_VERT | BM_EDGE, BM_ELEM_SELECT, true);

	if (!EDBM_op_finish(em, &bmop, op, true)) {
		return false;
	}

	return true;
}

/* extrudes individual vertices */
static bool edbm_extrude_verts_indiv(BMEditMesh *em, wmOperator *op, const char hflag)
{
	BMOperator bmop;

	EDBM_op_init(
	        em, &bmop, op,
	        "extrude_vert_indiv verts=%hv use_select_history=%b",
	        hflag, true);

	/* deselect original verts */
	BMO_slot_buffer_hflag_disable(em->bm, bmop.slots_in, "verts", BM_VERT, BM_ELEM_SELECT, true);

	BMO_op_exec(em->bm, &bmop);
	BMO_slot_buffer_hflag_enable(em->bm, bmop.slots_out, "verts.out", BM_VERT, BM_ELEM_SELECT, true);

	if (!EDBM_op_finish(em, &bmop, op, true)) {
		return false;
	}

	return true;
}

static char edbm_extrude_htype_from_em_select(BMEditMesh *em)
{
	char htype = BM_ALL_NOLOOP;

	if (em->selectmode & SCE_SELECT_VERTEX) {
		/* pass */
	}
	else if (em->selectmode & SCE_SELECT_EDGE) {
		htype &= ~BM_VERT;
	}
	else {
		htype &= ~(BM_VERT | BM_EDGE);
	}

	if (em->bm->totedgesel == 0) {
		htype &= ~(BM_EDGE | BM_FACE);
	}
	else if (em->bm->totfacesel == 0) {
		htype &= ~BM_FACE;
	}

	return htype;
}

static bool edbm_extrude_ex(
        Object *obedit, BMEditMesh *em,
        char htype, const char hflag,
        const bool use_mirror,
        const bool use_select_history)
{
	BMesh *bm = em->bm;
	BMOIter siter;
	BMOperator extop;
	BMElem *ele;

	/* needed to remove the faces left behind */
	if (htype & BM_FACE) {
		htype |= BM_EDGE;
	}

	BMO_op_init(bm, &extop, BMO_FLAG_DEFAULTS, "extrude_face_region");
	BMO_slot_bool_set(extop.slots_in, "use_select_history", use_select_history);
	BMO_slot_buffer_from_enabled_hflag(bm, &extop, extop.slots_in, "geom", htype, hflag);

	if (use_mirror) {
		BMOpSlot *slot_edges_exclude;
		slot_edges_exclude = BMO_slot_get(extop.slots_in, "edges_exclude");

		edbm_extrude_edge_exclude_mirror(obedit, em, hflag, &extop, slot_edges_exclude);
	}

	BM_SELECT_HISTORY_BACKUP(bm);
	EDBM_flag_disable_all(em, BM_ELEM_SELECT);
	BM_SELECT_HISTORY_RESTORE(bm);

	BMO_op_exec(bm, &extop);

	BMO_ITER (ele, &siter, extop.slots_out, "geom.out", BM_ALL_NOLOOP) {
		BM_elem_select_set(bm, ele, true);
	}

	BMO_op_finish(bm, &extop);

	return true;
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Extrude Repeat Operator
 * \{ */

static int edbm_extrude_repeat_exec(bContext *C, wmOperator *op)
{
	RegionView3D *rv3d = CTX_wm_region_view3d(C);
	const int steps = RNA_int_get(op->ptr, "steps");
	const float offs = RNA_float_get(op->ptr, "offset");
	float dvec[3], tmat[3][3], bmat[3][3];
	short a;

	ViewLayer *view_layer = CTX_data_view_layer(C);
	uint objects_len = 0;
	Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);

	for (uint ob_index = 0; ob_index < objects_len; ob_index++) {

		Object *obedit = objects[ob_index];
		BMEditMesh *em = BKE_editmesh_from_object(obedit);

		/* dvec */
		normalize_v3_v3_length(dvec, rv3d->persinv[2], offs);

		/* base correction */
		copy_m3_m4(bmat, obedit->obmat);
		invert_m3_m3(tmat, bmat);
		mul_m3_v3(tmat, dvec);

		for (a = 0; a < steps; a++) {
			edbm_extrude_ex(obedit, em, BM_ALL_NOLOOP, BM_ELEM_SELECT, false, false);

			BMO_op_callf(
				em->bm, BMO_FLAG_DEFAULTS,
				"translate vec=%v verts=%hv",
				dvec, BM_ELEM_SELECT);
		}

		EDBM_mesh_normals_update(em);

		EDBM_update_generic(em, true, true);
	}

	MEM_freeN(objects);

	return OPERATOR_FINISHED;
}

void MESH_OT_extrude_repeat(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Extrude Repeat Mesh";
	ot->description = "Extrude selected vertices, edges or faces repeatedly";
	ot->idname = "MESH_OT_extrude_repeat";

	/* api callbacks */
	ot->exec = edbm_extrude_repeat_exec;
	ot->poll = ED_operator_editmesh_view3d;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;

	/* props */
	RNA_def_float_distance(ot->srna, "offset", 2.0f, 0.0f, 1e12f, "Offset", "", 0.0f, 100.0f);
	RNA_def_int(ot->srna, "steps", 10, 0, 1000000, "Steps", "", 0, 180);
}

/** \} */


/* -------------------------------------------------------------------- */
/** \name Extrude Manipulator
 * \{ */

#ifdef USE_MANIPULATOR

const float extrude_button_scale = 0.15f;
const float extrude_button_offset_scale = 1.5f;
const float extrude_arrow_scale = 1.0f;
const float extrude_arrow_xyz_axis_scale = 1.0f;
const float extrude_arrow_normal_axis_scale = 1.75f;

static const uchar shape_plus[] = {
	0x5f, 0xfb, 0x40, 0xee, 0x25, 0xda, 0x11, 0xbf, 0x4, 0xa0, 0x0, 0x80, 0x4, 0x5f, 0x11,
	0x40, 0x25, 0x25, 0x40, 0x11, 0x5f, 0x4, 0x7f, 0x0, 0xa0, 0x4, 0xbf, 0x11, 0xda, 0x25,
	0xee, 0x40, 0xfb, 0x5f, 0xff, 0x7f, 0xfb, 0xa0, 0xee, 0xbf, 0xda, 0xda, 0xbf, 0xee,
	0xa0, 0xfb, 0x80, 0xff, 0x6e, 0xd7, 0x92, 0xd7, 0x92, 0x90, 0xd8, 0x90, 0xd8, 0x6d,
	0x92, 0x6d, 0x92, 0x27, 0x6e, 0x27, 0x6e, 0x6d, 0x28, 0x6d, 0x28, 0x90, 0x6e,
	0x90, 0x6e, 0xd7, 0x80, 0xff, 0x5f, 0xfb, 0x5f, 0xfb,
};

typedef struct ManipulatorExtrudeGroup {

	/* XYZ & normal. */
	struct wmManipulator *invoke_xyz_no[4];
	struct wmManipulator *adjust_xyz_no[5];

	struct {
		float normal_mat3[3][3];  /* use Z axis for normal. */
		int orientation_type;
	} data;

	wmOperatorType *ot_extrude;
} ManipulatorExtrudeGroup;

static void manipulator_mesh_extrude_orientation_matrix_set(
        struct ManipulatorExtrudeGroup *man, const float mat[3][3])
{
	for (int i = 0; i < 3; i++) {
		/* Set orientation without location. */
		for (int j = 0; j < 3; j++) {
			copy_v3_v3(man->adjust_xyz_no[i]->matrix_basis[j], mat[j]);
		}
		/* nop when (i == 2). */
		swap_v3_v3(man->adjust_xyz_no[i]->matrix_basis[i], man->adjust_xyz_no[i]->matrix_basis[2]);
		/* Orient to normal gives generally less awkward results. */
		if (man->data.orientation_type != V3D_MANIP_NORMAL) {
			if (dot_v3v3(man->adjust_xyz_no[i]->matrix_basis[2], man->data.normal_mat3[2]) < 0.0f) {
				negate_v3(man->adjust_xyz_no[i]->matrix_basis[2]);
			}
		}
		mul_v3_v3fl(
		        man->invoke_xyz_no[i]->matrix_offset[3],
		        man->adjust_xyz_no[i]->matrix_basis[2],
		        (extrude_arrow_xyz_axis_scale * extrude_button_offset_scale) / extrude_button_scale);
	}
}

static bool manipulator_mesh_extrude_poll(const bContext *C, wmManipulatorGroupType *wgt)
{
	ScrArea *sa = CTX_wm_area(C);
	bToolRef_Runtime *tref_rt = sa->runtime.tool ? sa->runtime.tool->runtime : NULL;
	if ((tref_rt == NULL) ||
	    !STREQ(wgt->idname, tref_rt->manipulator_group) ||
	    !ED_operator_editmesh_view3d((bContext *)C))
	{
		WM_manipulator_group_type_unlink_delayed_ptr(wgt);
		return false;
	}
	return true;
}

static void manipulator_mesh_extrude_setup(const bContext *UNUSED(C), wmManipulatorGroup *mgroup)
{
	struct ManipulatorExtrudeGroup *man = MEM_callocN(sizeof(ManipulatorExtrudeGroup), __func__);
	mgroup->customdata = man;

	const wmManipulatorType *wt_arrow = WM_manipulatortype_find("MANIPULATOR_WT_arrow_3d", true);
	const wmManipulatorType *wt_grab = WM_manipulatortype_find("MANIPULATOR_WT_button_2d", true);

	for (int i = 0; i < 4; i++) {
		man->adjust_xyz_no[i] = WM_manipulator_new_ptr(wt_arrow, mgroup, NULL);
		man->invoke_xyz_no[i] = WM_manipulator_new_ptr(wt_grab, mgroup, NULL);
		man->invoke_xyz_no[i]->flag |= WM_MANIPULATOR_DRAW_OFFSET_SCALE;
	}

	{
		PropertyRNA *prop = RNA_struct_find_property(man->invoke_xyz_no[3]->ptr, "shape");
		for (int i = 0; i < 4; i++) {
			RNA_property_string_set_bytes(
			        man->invoke_xyz_no[i]->ptr, prop,
			        (const char *)shape_plus, ARRAY_SIZE(shape_plus));
		}
	}

	man->ot_extrude = WM_operatortype_find("MESH_OT_extrude_context_move", true);

	for (int i = 0; i < 3; i++) {
		UI_GetThemeColor3fv(TH_AXIS_X + i, man->invoke_xyz_no[i]->color);
		UI_GetThemeColor3fv(TH_AXIS_X + i, man->adjust_xyz_no[i]->color);
	}
	UI_GetThemeColor3fv(TH_MANIPULATOR_PRIMARY, man->invoke_xyz_no[3]->color);
	UI_GetThemeColor3fv(TH_MANIPULATOR_PRIMARY, man->adjust_xyz_no[3]->color);

	for (int i = 0; i < 4; i++) {
		WM_manipulator_set_scale(man->invoke_xyz_no[i], extrude_button_scale);
		WM_manipulator_set_scale(man->adjust_xyz_no[i], extrude_arrow_scale);
	}
	WM_manipulator_set_scale(man->adjust_xyz_no[3], extrude_arrow_normal_axis_scale);

	for (int i = 0; i < 4; i++) {
	}

	for (int i = 0; i < 4; i++) {
		WM_manipulator_set_flag(man->adjust_xyz_no[i], WM_MANIPULATOR_DRAW_VALUE, true);
	}

	/* XYZ & normal axis extrude. */
	for (int i = 0; i < 4; i++) {
		PointerRNA *ptr = WM_manipulator_operator_set(man->invoke_xyz_no[i], 0, man->ot_extrude, NULL);
		{
			int constraint[3] = {0, 0, 0};
			constraint[MIN2(i, 2)] = 1;
			PointerRNA macroptr = RNA_pointer_get(ptr, "TRANSFORM_OT_translate");
			RNA_boolean_set(&macroptr, "release_confirm", true);
			RNA_boolean_set_array(&macroptr, "constraint_axis", constraint);
		}
	}

	/* Adjust extrude. */
	for (int i = 0; i < 4; i++) {
		PointerRNA *ptr = WM_manipulator_operator_set(man->adjust_xyz_no[i], 0, man->ot_extrude, NULL);
		{
			int constraint[3] = {0, 0, 0};
			constraint[MIN2(i, 2)] = 1;
			PointerRNA macroptr = RNA_pointer_get(ptr, "TRANSFORM_OT_translate");
			RNA_boolean_set(&macroptr, "release_confirm", true);
			RNA_boolean_set_array(&macroptr, "constraint_axis", constraint);
		}
		wmManipulatorOpElem *mpop = WM_manipulator_operator_get(man->adjust_xyz_no[i], 0);
		mpop->is_redo = true;
	}
}

static void manipulator_mesh_extrude_refresh(const bContext *C, wmManipulatorGroup *mgroup)
{
	ManipulatorExtrudeGroup *man = mgroup->customdata;

	for (int i = 0; i < 4; i++) {
		WM_manipulator_set_flag(man->invoke_xyz_no[i], WM_MANIPULATOR_HIDDEN, true);
		WM_manipulator_set_flag(man->adjust_xyz_no[i], WM_MANIPULATOR_HIDDEN, true);
	}

	if (G.moving) {
		return;
	}

	Scene *scene = CTX_data_scene(C);
	man->data.orientation_type = scene->orientation_type;
	bool use_normal = (man->data.orientation_type != V3D_MANIP_NORMAL);
	const int axis_len_used = use_normal ? 4 : 3;

	struct TransformBounds tbounds;

	if (use_normal) {
		struct TransformBounds tbounds_normal;
		if (!ED_transform_calc_manipulator_stats(
		            C, &(struct TransformCalcParams){
		                .orientation_type = V3D_MANIP_NORMAL + 1,
		            }, &tbounds_normal))
		{
			unit_m3(tbounds_normal.axis);
		}
		copy_m3_m3(man->data.normal_mat3, tbounds_normal.axis);
	}

	/* TODO(campbell): run second since this modifies the 3D view, it should not. */
	if (!ED_transform_calc_manipulator_stats(
	            C, &(struct TransformCalcParams){
	                .orientation_type = man->data.orientation_type + 1,
	            }, &tbounds))
	{
		return;
	}

	/* Main axis is normal. */
	if (!use_normal) {
		copy_m3_m3(man->data.normal_mat3, tbounds.axis);
	}

	/* Offset the add icon. */
	mul_v3_v3fl(
	        man->invoke_xyz_no[3]->matrix_offset[3],
	        man->data.normal_mat3[2],
	        (extrude_arrow_normal_axis_scale * extrude_button_offset_scale) / extrude_button_scale);

	/* Needed for normal orientation. */
	manipulator_mesh_extrude_orientation_matrix_set(man, tbounds.axis);
	if (use_normal) {
		copy_m4_m3(man->adjust_xyz_no[3]->matrix_basis, man->data.normal_mat3);
	}

	/* Location. */
	for (int i = 0; i < axis_len_used; i++) {
		WM_manipulator_set_matrix_location(man->invoke_xyz_no[i], tbounds.center);
		WM_manipulator_set_matrix_location(man->adjust_xyz_no[i], tbounds.center);
	}

	wmOperator *op = WM_operator_last_redo(C);
	bool has_redo = (op && op->type == man->ot_extrude);

	/* Un-hide. */
	for (int i = 0; i < axis_len_used; i++) {
		WM_manipulator_set_flag(man->invoke_xyz_no[i], WM_MANIPULATOR_HIDDEN, false);
		WM_manipulator_set_flag(man->adjust_xyz_no[i], WM_MANIPULATOR_HIDDEN, !has_redo);
	}

	/* Operator properties. */
	if (use_normal) {
		wmManipulatorOpElem *mpop = WM_manipulator_operator_get(man->invoke_xyz_no[3], 0);
		PointerRNA macroptr = RNA_pointer_get(&mpop->ptr, "TRANSFORM_OT_translate");
		RNA_enum_set(&macroptr, "constraint_orientation", V3D_MANIP_NORMAL);
	}

	/* Redo with current settings. */
	if (has_redo) {
		wmOperator *op_transform = op->macro.last;
		float value[4];
		RNA_float_get_array(op_transform->ptr, "value", value);
		int constraint_axis[3];
		RNA_boolean_get_array(op_transform->ptr, "constraint_axis", constraint_axis);
		int orientation_type = RNA_enum_get(op_transform->ptr, "constraint_orientation");

		/* We could also access this from 'ot->last_properties' */
		for (int i = 0; i < 4; i++) {
			if ((i != 3) ?
			    (orientation_type == man->data.orientation_type && constraint_axis[i]) :
			    (orientation_type == V3D_MANIP_NORMAL && constraint_axis[2]))
			{
				wmManipulatorOpElem *mpop = WM_manipulator_operator_get(man->adjust_xyz_no[i], 0);

				PointerRNA macroptr = RNA_pointer_get(&mpop->ptr, "TRANSFORM_OT_translate");

				RNA_float_set_array(&macroptr, "value", value);
				RNA_boolean_set_array(&macroptr, "constraint_axis", constraint_axis);
				RNA_enum_set(&macroptr, "constraint_orientation", orientation_type);
			}
			else {
				/* TODO(campbell): ideally we could adjust all,
				 * this is complicated by how operator redo and the transform macro works. */
				WM_manipulator_set_flag(man->adjust_xyz_no[i], WM_MANIPULATOR_HIDDEN, true);
			}
		}
	}

	for (int i = 0; i < 4; i++) {
		RNA_enum_set(
		        man->invoke_xyz_no[i]->ptr,
		        "draw_options",
		        (man->adjust_xyz_no[i]->flag & WM_MANIPULATOR_HIDDEN) ?
		        ED_MANIPULATOR_BUTTON_SHOW_HELPLINE : 0);
	}
}

static void manipulator_mesh_extrude_draw_prepare(const bContext *C, wmManipulatorGroup *mgroup)
{
	ManipulatorExtrudeGroup *man = mgroup->customdata;
	switch (man->data.orientation_type) {
		case V3D_MANIP_VIEW:
		{
			RegionView3D *rv3d = CTX_wm_region_view3d(C);
			float mat[3][3];
			copy_m3_m4(mat, rv3d->viewinv);
			normalize_m3(mat);
			manipulator_mesh_extrude_orientation_matrix_set(man, mat);
			break;
		}
	}
}

static void manipulator_mesh_extrude_message_subscribe(
        const bContext *C, wmManipulatorGroup *mgroup, struct wmMsgBus *mbus)
{
	ARegion *ar = CTX_wm_region(C);

	/* Subscribe to view properties */
	wmMsgSubscribeValue msg_sub_value_mpr_tag_refresh = {
		.owner = ar,
		.user_data = mgroup->parent_mmap,
		.notify = WM_manipulator_do_msg_notify_tag_refresh,
	};

	{
		WM_msg_subscribe_rna_anon_prop(mbus, Scene, transform_orientation, &msg_sub_value_mpr_tag_refresh);
	}

}

static void MESH_WGT_extrude(struct wmManipulatorGroupType *wgt)
{
	wgt->name = "Mesh Extrude";
	wgt->idname = "MESH_WGT_extrude";

	wgt->flag = WM_MANIPULATORGROUPTYPE_3D;

	wgt->mmap_params.spaceid = SPACE_VIEW3D;
	wgt->mmap_params.regionid = RGN_TYPE_WINDOW;

	wgt->poll = manipulator_mesh_extrude_poll;
	wgt->setup = manipulator_mesh_extrude_setup;
	wgt->refresh = manipulator_mesh_extrude_refresh;
	wgt->draw_prepare = manipulator_mesh_extrude_draw_prepare;
	wgt->message_subscribe = manipulator_mesh_extrude_message_subscribe;
}

#endif  /* USE_MANIPULATOR */

/** \} */

/* -------------------------------------------------------------------- */
/** \name Extrude Operator
 * \{ */

/* generic extern called extruder */
static bool edbm_extrude_mesh(Object *obedit, BMEditMesh *em, wmOperator *op)
{
	bool changed = false;
	const char htype = edbm_extrude_htype_from_em_select(em);
	enum {NONE = 0, ELEM_FLAG, VERT_ONLY, EDGE_ONLY} nr;

	if (em->selectmode & SCE_SELECT_VERTEX) {
		if      (em->bm->totvertsel == 0) nr = NONE;
		else if (em->bm->totvertsel == 1) nr = VERT_ONLY;
		else if (em->bm->totedgesel == 0) nr = VERT_ONLY;
		else                              nr = ELEM_FLAG;
	}
	else if (em->selectmode & SCE_SELECT_EDGE) {
		if      (em->bm->totedgesel == 0) nr = NONE;
		else if (em->bm->totfacesel == 0) nr = EDGE_ONLY;
		else                              nr = ELEM_FLAG;
	}
	else {
		if      (em->bm->totfacesel == 0) nr = NONE;
		else                              nr = ELEM_FLAG;
	}

	switch (nr) {
		case NONE:
			return false;
		case ELEM_FLAG:
			changed = edbm_extrude_ex(obedit, em, htype, BM_ELEM_SELECT, true, true);
			break;
		case VERT_ONLY:
			changed = edbm_extrude_verts_indiv(em, op, BM_ELEM_SELECT);
			break;
		case EDGE_ONLY:
			changed = edbm_extrude_edges_indiv(em, op, BM_ELEM_SELECT);
			break;
	}

	if (changed) {
		return true;
	}
	else {
		BKE_report(op->reports, RPT_ERROR, "Not a valid selection for extrude");
		return false;
	}
}

/* extrude without transform */
static int edbm_extrude_region_exec(bContext *C, wmOperator *op)
{
	ViewLayer *view_layer = CTX_data_view_layer(C);
	uint objects_len = 0;
	Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);

	for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
		Object *obedit = objects[ob_index];
		BMEditMesh *em = BKE_editmesh_from_object(obedit);
		if (em->bm->totvertsel == 0) {
			continue;
		}

		if (!edbm_extrude_mesh(obedit, em, op)) {
			continue;
		}
		/* This normally happens when pushing undo but modal operators
		 * like this one don't push undo data until after modal mode is
		 * done.*/
		EDBM_mesh_normals_update(em);

		EDBM_update_generic(em, true, true);
	}
	MEM_freeN(objects);
	return OPERATOR_FINISHED;
}

void MESH_OT_extrude_region(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Extrude Region";
	ot->idname = "MESH_OT_extrude_region";
	ot->description = "Extrude region of faces";

	/* api callbacks */
	//ot->invoke = mesh_extrude_region_invoke;
	ot->exec = edbm_extrude_region_exec;
	ot->poll = ED_operator_editmesh;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;

	Transform_Properties(ot, P_NO_DEFAULTS | P_MIRROR_DUMMY);
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Extrude Context Operator
 *
 * Guess what to do based on selection.
 * \{ */

/* extrude without transform */
static int edbm_extrude_context_exec(bContext *C, wmOperator *op)
{
	ViewLayer *view_layer = CTX_data_view_layer(C);
	uint objects_len = 0;
	Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);

	for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
		Object *obedit = objects[ob_index];
		BMEditMesh *em = BKE_editmesh_from_object(obedit);
		if (em->bm->totvertsel == 0) {
			continue;
		}

		edbm_extrude_mesh(obedit, em, op);
		/* This normally happens when pushing undo but modal operators
		 * like this one don't push undo data until after modal mode is
		 * done.*/

		EDBM_mesh_normals_update(em);

		EDBM_update_generic(em, true, true);
	}
	MEM_freeN(objects);
	return OPERATOR_FINISHED;
}

void MESH_OT_extrude_context(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Extrude Context";
	ot->idname = "MESH_OT_extrude_context";
	ot->description = "Extrude selection";

	/* api callbacks */
	ot->exec = edbm_extrude_context_exec;
	ot->poll = ED_operator_editmesh;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;

	Transform_Properties(ot, P_NO_DEFAULTS | P_MIRROR_DUMMY);

#ifdef USE_MANIPULATOR
	WM_manipulatorgrouptype_append(MESH_WGT_extrude);
#endif
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Extrude Verts Operator
 * \{ */

static int edbm_extrude_verts_exec(bContext *C, wmOperator *op)
{
	ViewLayer *view_layer = CTX_data_view_layer(C);
	uint objects_len = 0;
	Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);

	for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
		Object *obedit = objects[ob_index];
		BMEditMesh *em = BKE_editmesh_from_object(obedit);
		if (em->bm->totvertsel == 0) {
			continue;
		}

		edbm_extrude_verts_indiv(em, op, BM_ELEM_SELECT);

		EDBM_update_generic(em, true, true);
	}
	MEM_freeN(objects);

	return OPERATOR_FINISHED;
}

void MESH_OT_extrude_verts_indiv(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Extrude Only Vertices";
	ot->idname = "MESH_OT_extrude_verts_indiv";
	ot->description = "Extrude individual vertices only";

	/* api callbacks */
	ot->exec = edbm_extrude_verts_exec;
	ot->poll = ED_operator_editmesh;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;

	/* to give to transform */
	Transform_Properties(ot, P_NO_DEFAULTS | P_MIRROR_DUMMY);
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Extrude Edges Operator
 * \{ */

static int edbm_extrude_edges_exec(bContext *C, wmOperator *op)
{
	ViewLayer *view_layer = CTX_data_view_layer(C);
	uint objects_len = 0;
	Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);

	for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
		Object *obedit = objects[ob_index];
		BMEditMesh *em = BKE_editmesh_from_object(obedit);
		if (em->bm->totedgesel == 0) {
			continue;
		}

		edbm_extrude_edges_indiv(em, op, BM_ELEM_SELECT);

		EDBM_update_generic(em, true, true);
	}
	MEM_freeN(objects);

	return OPERATOR_FINISHED;
}

void MESH_OT_extrude_edges_indiv(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Extrude Only Edges";
	ot->idname = "MESH_OT_extrude_edges_indiv";
	ot->description = "Extrude individual edges only";

	/* api callbacks */
	ot->exec = edbm_extrude_edges_exec;
	ot->poll = ED_operator_editmesh;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;

	/* to give to transform */
	Transform_Properties(ot, P_NO_DEFAULTS | P_MIRROR_DUMMY);
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Extrude Faces Operator
 * \{ */

static int edbm_extrude_faces_exec(bContext *C, wmOperator *op)
{
	ViewLayer *view_layer = CTX_data_view_layer(C);
	uint objects_len = 0;
	Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);

	for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
		Object *obedit = objects[ob_index];
		BMEditMesh *em = BKE_editmesh_from_object(obedit);
		if (em->bm->totfacesel == 0) {
			continue;
		}

		edbm_extrude_discrete_faces(em, op, BM_ELEM_SELECT);

		EDBM_update_generic(em, true, true);
	}
	MEM_freeN(objects);

	return OPERATOR_FINISHED;
}

void MESH_OT_extrude_faces_indiv(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Extrude Individual Faces";
	ot->idname = "MESH_OT_extrude_faces_indiv";
	ot->description = "Extrude individual faces only";

	/* api callbacks */
	ot->exec = edbm_extrude_faces_exec;
	ot->poll = ED_operator_editmesh;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;

	Transform_Properties(ot, P_NO_DEFAULTS | P_MIRROR_DUMMY);
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Dupli-Extrude Operator
 *
 * Add-click-mesh (extrude) operator.
 * \{ */

static int edbm_dupli_extrude_cursor_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
	ViewContext vc;
	BMVert *v1;
	BMIter iter;
	float center[3];
	uint verts_len;
	bool use_proj;

	em_setup_viewcontext(C, &vc);

	invert_m4_m4(vc.obedit->imat, vc.obedit->obmat);

	ED_view3d_init_mats_rv3d(vc.obedit, vc.rv3d);

	use_proj = ((vc.scene->toolsettings->snap_flag & SCE_SNAP) &&
	            (vc.scene->toolsettings->snap_mode == SCE_SNAP_MODE_FACE));

	zero_v3(center);
	verts_len = 0;

	BM_ITER_MESH (v1, &iter, vc.em->bm, BM_VERTS_OF_MESH) {
		if (BM_elem_flag_test(v1, BM_ELEM_SELECT)) {
			add_v3_v3(center, v1->co);
			verts_len += 1;
		}
	}

	/* call extrude? */
	if (verts_len != 0) {
		const char extrude_htype = edbm_extrude_htype_from_em_select(vc.em);
		const bool rot_src = RNA_boolean_get(op->ptr, "rotate_source");
		BMEdge *eed;
		float mat[3][3];
		float vec[3], ofs[3];
		float nor[3] = {0.0, 0.0, 0.0};

		/* 2D normal calc */
		const float mval_f[2] = {(float)event->mval[0],
		                         (float)event->mval[1]};

		mul_v3_fl(center, 1.0f / (float)verts_len);

		/* check for edges that are half selected, use for rotation */
		bool done = false;
		BM_ITER_MESH (eed, &iter, vc.em->bm, BM_EDGES_OF_MESH) {
			if (BM_elem_flag_test(eed, BM_ELEM_SELECT)) {
				float co1[2], co2[2];

				if ((ED_view3d_project_float_object(vc.ar, eed->v1->co, co1, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_OK) &&
				    (ED_view3d_project_float_object(vc.ar, eed->v2->co, co2, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_OK))
				{
					/* 2D rotate by 90d while adding.
					 *  (x, y) = (y, -x)
					 *
					 * accumulate the screenspace normal in 2D,
					 * with screenspace edge length weighting the result. */
					if (line_point_side_v2(co1, co2, mval_f) >= 0.0f) {
						nor[0] +=  (co1[1] - co2[1]);
						nor[1] += -(co1[0] - co2[0]);
					}
					else {
						nor[0] +=  (co2[1] - co1[1]);
						nor[1] += -(co2[0] - co1[0]);
					}
					done = true;
				}
			}
		}

		if (done) {
			float view_vec[3], cross[3];

			/* convert the 2D nomal into 3D */
			mul_mat3_m4_v3(vc.rv3d->viewinv, nor); /* worldspace */
			mul_mat3_m4_v3(vc.obedit->imat, nor); /* local space */

			/* correct the normal to be aligned on the view plane */
			mul_v3_mat3_m4v3(view_vec, vc.obedit->imat, vc.rv3d->viewinv[2]);
			cross_v3_v3v3(cross, nor, view_vec);
			cross_v3_v3v3(nor, view_vec, cross);
			normalize_v3(nor);
		}

		/* center */
		copy_v3_v3(ofs, center);

		mul_m4_v3(vc.obedit->obmat, ofs);  /* view space */
		ED_view3d_win_to_3d_int(vc.v3d, vc.ar, ofs, event->mval, ofs);
		mul_m4_v3(vc.obedit->imat, ofs); // back in object space

		sub_v3_v3(ofs, center);

		/* calculate rotation */
		unit_m3(mat);
		if (done) {
			float angle;

			normalize_v3_v3(vec, ofs);

			angle = angle_normalized_v3v3(vec, nor);

			if (angle != 0.0f) {
				float axis[3];

				cross_v3_v3v3(axis, nor, vec);

				/* halve the rotation if its applied twice */
				if (rot_src) {
					angle *= 0.5f;
				}

				axis_angle_to_mat3(mat, axis, angle);
			}
		}

		if (rot_src) {
			EDBM_op_callf(vc.em, op, "rotate verts=%hv cent=%v matrix=%m3",
			              BM_ELEM_SELECT, center, mat);

			/* also project the source, for retopo workflow */
			if (use_proj)
				EMBM_project_snap_verts(C, vc.ar, vc.em);
		}

		edbm_extrude_ex(vc.obedit, vc.em, extrude_htype, BM_ELEM_SELECT, true, true);
		EDBM_op_callf(vc.em, op, "rotate verts=%hv cent=%v matrix=%m3",
		              BM_ELEM_SELECT, center, mat);
		EDBM_op_callf(vc.em, op, "translate verts=%hv vec=%v",
		              BM_ELEM_SELECT, ofs);
	}
	else {
		const float *cursor = ED_view3d_cursor3d_get(vc.scene, vc.v3d)->location;
		BMOperator bmop;
		BMOIter oiter;

		copy_v3_v3(center, cursor);
		ED_view3d_win_to_3d_int(vc.v3d, vc.ar, center, event->mval, center);

		mul_m4_v3(vc.obedit->imat, center); // back in object space

		EDBM_op_init(vc.em, &bmop, op, "create_vert co=%v", center);
		BMO_op_exec(vc.em->bm, &bmop);

		BMO_ITER (v1, &oiter, bmop.slots_out, "vert.out", BM_VERT) {
			BM_vert_select_set(vc.em->bm, v1, true);
		}

		if (!EDBM_op_finish(vc.em, &bmop, op, true)) {
			return OPERATOR_CANCELLED;
		}
	}

	if (use_proj)
		EMBM_project_snap_verts(C, vc.ar, vc.em);

	/* This normally happens when pushing undo but modal operators
	 * like this one don't push undo data until after modal mode is
	 * done. */
	EDBM_mesh_normals_update(vc.em);

	EDBM_update_generic(vc.em, true, true);

	return OPERATOR_FINISHED;
}

void MESH_OT_dupli_extrude_cursor(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Duplicate or Extrude to Cursor";
	ot->idname = "MESH_OT_dupli_extrude_cursor";
	ot->description = "Duplicate and extrude selected vertices, edges or faces towards the mouse cursor";

	/* api callbacks */
	ot->invoke = edbm_dupli_extrude_cursor_invoke;
	ot->poll = ED_operator_editmesh_region_view3d;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;

	RNA_def_boolean(ot->srna, "rotate_source", true, "Rotate Source", "Rotate initial selection giving better shape");
}

/** \} */