Merge branch 'blender2.7'
[blender.git] / source / blender / editors / mesh / editmesh_extrude_screw.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_screw.c
29  *  \ingroup edmesh
30  */
31
32 #include "MEM_guardedalloc.h"
33
34 #include "DNA_object_types.h"
35
36 #include "BLI_math.h"
37
38 #include "BKE_context.h"
39 #include "BKE_editmesh.h"
40 #include "BKE_layer.h"
41 #include "BKE_report.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_view3d.h"
51
52
53
54 #include "mesh_intern.h"  /* own include */
55
56 /* -------------------------------------------------------------------- */
57 /** \name Screw Operator
58  * \{ */
59
60 static int edbm_screw_exec(bContext *C, wmOperator *op)
61 {
62         BMEdge *eed;
63         BMVert *eve, *v1, *v2;
64         BMIter iter, eiter;
65         float dvec[3], nor[3], cent[3], axis[3], v1_co_global[3], v2_co_global[3];
66         int steps, turns;
67         int valence;
68         uint objects_empty_len = 0;
69         uint failed_axis_len = 0;
70         uint failed_vertices_len = 0;
71
72         turns = RNA_int_get(op->ptr, "turns");
73         steps = RNA_int_get(op->ptr, "steps");
74         RNA_float_get_array(op->ptr, "center", cent);
75         RNA_float_get_array(op->ptr, "axis", axis);
76
77         uint objects_len = 0;
78         ViewLayer *view_layer = CTX_data_view_layer(C);
79         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, CTX_wm_view3d(C), &objects_len);
80
81         for (uint ob_index = 0; ob_index < objects_len; ob_index++)     {
82                 Object *obedit = objects[ob_index];
83                 BMEditMesh *em = BKE_editmesh_from_object(obedit);
84                 BMesh *bm = em->bm;
85
86                 if (bm->totvertsel < 2) {
87                         if (bm->totvertsel == 0) {
88                                 objects_empty_len++;
89                         }
90                         continue;
91                 }
92
93                 if (is_zero_v3(axis)) {
94                         failed_axis_len++;
95                         continue;
96                 }
97
98                 /* find two vertices with valence count == 1, more or less is wrong */
99                 v1 = NULL;
100                 v2 = NULL;
101
102                 BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
103                         valence = 0;
104                         BM_ITER_ELEM (eed, &eiter, eve, BM_EDGES_OF_VERT) {
105                                 if (BM_elem_flag_test(eed, BM_ELEM_SELECT)) {
106                                         valence++;
107                                 }
108                         }
109
110                         if (valence == 1) {
111                                 if (v1 == NULL) {
112                                         v1 = eve;
113                                 }
114                                 else if (v2 == NULL) {
115                                         v2 = eve;
116                                 }
117                                 else {
118                                         v1 = NULL;
119                                         break;
120                                 }
121                         }
122                 }
123
124                 if (v1 == NULL || v2 == NULL) {
125                         failed_vertices_len++;
126                         continue;
127                 }
128
129                 copy_v3_v3(nor, obedit->obmat[2]);
130
131                 /* calculate dvec */
132                 mul_v3_m4v3(v1_co_global, obedit->obmat, v1->co);
133                 mul_v3_m4v3(v2_co_global, obedit->obmat, v2->co);
134                 sub_v3_v3v3(dvec, v1_co_global, v2_co_global);
135                 mul_v3_fl(dvec, 1.0f / steps);
136
137                 if (dot_v3v3(nor, dvec) > 0.0f)
138                         negate_v3(dvec);
139
140                 BMOperator spinop;
141                 if (!EDBM_op_init(em, &spinop, op,
142                                   "spin geom=%hvef cent=%v axis=%v dvec=%v steps=%i angle=%f space=%m4 use_duplicate=%b",
143                                   BM_ELEM_SELECT, cent, axis, dvec, turns * steps, DEG2RADF(360.0f * turns), obedit->obmat, false))
144                 {
145                         continue;
146                 }
147
148                 BMO_op_exec(bm, &spinop);
149                 EDBM_flag_disable_all(em, BM_ELEM_SELECT);
150                 BMO_slot_buffer_hflag_enable(bm, spinop.slots_out, "geom_last.out", BM_ALL_NOLOOP, BM_ELEM_SELECT, true);
151
152                 if (!EDBM_op_finish(em, &spinop, op, true)) {
153                         continue;
154                 }
155
156                 EDBM_update_generic(em, true, true);
157         }
158         MEM_freeN(objects);
159
160         if (failed_axis_len == objects_len - objects_empty_len) {
161                 BKE_report(op->reports, RPT_ERROR, "Invalid/unset axis");
162         }
163         else if (failed_vertices_len == objects_len - objects_empty_len) {
164                 BKE_report(op->reports, RPT_ERROR, "You have to select a string of connected vertices too");
165         }
166
167         return OPERATOR_FINISHED;
168 }
169
170 /* get center and axis, in global coords */
171 static int edbm_screw_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
172 {
173         Scene *scene = CTX_data_scene(C);
174         RegionView3D *rv3d = ED_view3d_context_rv3d(C);
175
176         PropertyRNA *prop;
177         prop = RNA_struct_find_property(op->ptr, "center");
178         if (!RNA_property_is_set(op->ptr, prop)) {
179                 RNA_property_float_set_array(op->ptr, prop, scene->cursor.location);
180         }
181         if (rv3d) {
182                 prop = RNA_struct_find_property(op->ptr, "axis");
183                 if (!RNA_property_is_set(op->ptr, prop)) {
184                         RNA_property_float_set_array(op->ptr, prop, rv3d->viewinv[1]);
185                 }
186         }
187
188         return edbm_screw_exec(C, op);
189 }
190
191 void MESH_OT_screw(wmOperatorType *ot)
192 {
193         /* identifiers */
194         ot->name = "Screw";
195         ot->description = "Extrude selected vertices in screw-shaped rotation around the cursor in indicated viewport";
196         ot->idname = "MESH_OT_screw";
197
198         /* api callbacks */
199         ot->invoke = edbm_screw_invoke;
200         ot->exec = edbm_screw_exec;
201         ot->poll = ED_operator_editmesh;
202
203         /* flags */
204         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
205
206         /* props */
207         RNA_def_int(ot->srna, "steps", 9, 1, 100000, "Steps", "Steps", 3, 256);
208         RNA_def_int(ot->srna, "turns", 1, 1, 100000, "Turns", "Turns", 1, 256);
209
210         RNA_def_float_vector(ot->srna, "center", 3, NULL, -1e12f, 1e12f,
211                              "Center", "Center in global view space", -1e4f, 1e4f);
212         RNA_def_float_vector(ot->srna, "axis", 3, NULL, -1.0f, 1.0f,
213                              "Axis", "Axis in global view space", -1.0f, 1.0f);
214 }
215
216 /** \} */