2
* ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18
* The Original Code is Copyright (C) 2013 by Blender Foundation.
19
* All rights reserved.
21
* Contributor(s): Campbell Barton
23
* ***** END GPL LICENSE BLOCK *****
26
/** \file blender/editors/mesh/editmesh_bisect.c
30
#include "MEM_guardedalloc.h"
32
#include "DNA_object_types.h"
36
#include "BKE_global.h"
37
#include "BKE_context.h"
38
#include "BKE_editmesh.h"
40
#include "RNA_define.h"
41
#include "RNA_access.h"
47
#include "ED_screen.h"
48
#include "ED_view3d.h"
51
#include "mesh_intern.h" /* own include */
53
static int mesh_bisect_exec(bContext *C, wmOperator *op);
55
/* -------------------------------------------------------------------- */
65
static bool mesh_bisect_interactive_calc(
66
bContext *C, wmOperator *op,
68
float plane_co[3], float plane_no[3])
70
wmGesture *gesture = op->customdata;
73
ARegion *ar = CTX_wm_region(C);
74
RegionView3D *rv3d = ar->regiondata;
76
int x_start = RNA_int_get(op->ptr, "xstart");
77
int y_start = RNA_int_get(op->ptr, "ystart");
78
int x_end = RNA_int_get(op->ptr, "xend");
79
int y_end = RNA_int_get(op->ptr, "yend");
81
/* reference location (some point in front of the view) for finding a point on a plane */
82
const float *co_ref = rv3d->ofs;
83
float co_a_ss[2] = {x_start, y_start}, co_b_ss[2] = {x_end, y_end}, co_delta_ss[2];
84
float co_a[3], co_b[3];
85
const float zfac = ED_view3d_calc_zfac(rv3d, co_ref, NULL);
87
opdata = gesture->userdata;
90
ED_view3d_win_to_vector(ar, co_a_ss, co_a);
93
sub_v2_v2v2(co_delta_ss, co_a_ss, co_b_ss);
94
ED_view3d_win_to_delta(ar, co_delta_ss, co_b, zfac);
96
/* cross both to get a normal */
97
cross_v3_v3v3(plane_no, co_a, co_b);
98
normalize_v3(plane_no); /* not needed but nicer for user */
100
/* point on plane, can use either start or endpoint */
101
ED_view3d_win_to_3d(ar, co_ref, co_a_ss, plane_co);
103
if (opdata->is_first == false)
104
EDBM_redo_state_restore(opdata->mesh_backup, em, false);
106
opdata->is_first = false;
111
static int mesh_bisect_invoke(bContext *C, wmOperator *op, const wmEvent *event)
115
/* if the properties are set or there is no rv3d,
116
* skip model and exec immediately */
118
if ((CTX_wm_region_view3d(C) == NULL) ||
119
(RNA_struct_property_is_set(op->ptr, "plane_co") &&
120
RNA_struct_property_is_set(op->ptr, "plane_no")))
122
return mesh_bisect_exec(C, op);
125
ret = WM_gesture_straightline_invoke(C, op, event);
126
if (ret & OPERATOR_RUNNING_MODAL) {
127
View3D *v3d = CTX_wm_view3d(C);
129
wmGesture *gesture = op->customdata;
132
Object *obedit = CTX_data_edit_object(C);
133
BMEditMesh *em = BKE_editmesh_from_object(obedit);
135
opdata = MEM_mallocN(sizeof(BisectData), "inset_operator_data");
136
opdata->mesh_backup = EDBM_redo_state_store(em);
137
opdata->is_first = true;
138
gesture->userdata = opdata;
140
/* misc other vars */
141
G.moving = G_TRANSFORM_EDIT;
142
opdata->twtype = v3d->twtype;
148
static void edbm_bisect_exit(bContext *C, BisectData *opdata)
150
View3D *v3d = CTX_wm_view3d(C);
151
EDBM_redo_state_free(&opdata->mesh_backup, NULL, false);
152
v3d->twtype = opdata->twtype;
156
static int mesh_bisect_modal(bContext *C, wmOperator *op, const wmEvent *event)
158
wmGesture *gesture = op->customdata;
159
BisectData *opdata = gesture->userdata;
160
BisectData opdata_back = *opdata; /* annoyance, WM_gesture_straightline_modal, frees */
163
ret = WM_gesture_straightline_modal(C, op, event);
165
if (ret & (OPERATOR_FINISHED | OPERATOR_CANCELLED)) {
166
edbm_bisect_exit(C, &opdata_back);
172
/* End Model Helpers */
173
/* -------------------------------------------------------------------- */
177
static int mesh_bisect_exec(bContext *C, wmOperator *op)
179
Scene *scene = CTX_data_scene(C);
181
/* both can be NULL, fallbacks values are used */
182
View3D *v3d = CTX_wm_view3d(C);
183
RegionView3D *rv3d = ED_view3d_context_rv3d(C);
185
Object *obedit = CTX_data_edit_object(C);
186
BMEditMesh *em = BKE_editmesh_from_object(obedit);
193
const float thresh = RNA_float_get(op->ptr, "threshold");
194
const bool use_fill = RNA_boolean_get(op->ptr, "use_fill");
195
const bool clear_inner = RNA_boolean_get(op->ptr, "clear_inner");
196
const bool clear_outer = RNA_boolean_get(op->ptr, "clear_outer");
198
PropertyRNA *prop_plane_co;
199
PropertyRNA *prop_plane_no;
201
prop_plane_co = RNA_struct_find_property(op->ptr, "plane_co");
202
if (RNA_property_is_set(op->ptr, prop_plane_co)) {
203
RNA_property_float_get_array(op->ptr, prop_plane_co, plane_co);
206
copy_v3_v3(plane_co, give_cursor(scene, v3d));
207
RNA_property_float_set_array(op->ptr, prop_plane_co, plane_co);
210
prop_plane_no = RNA_struct_find_property(op->ptr, "plane_no");
211
if (RNA_property_is_set(op->ptr, prop_plane_no)) {
212
RNA_property_float_get_array(op->ptr, prop_plane_no, plane_no);
216
copy_v3_v3(plane_no, rv3d->viewinv[1]);
220
plane_no[0] = plane_no[1] = 0.0f; plane_no[2] = 1.0f;
222
RNA_property_float_set_array(op->ptr, prop_plane_no, plane_no);
227
/* -------------------------------------------------------------------- */
229
/* Note: keep this isolated, exec can work wihout this */
230
if ((op->customdata != NULL) &&
231
mesh_bisect_interactive_calc(C, op, em, plane_co, plane_no))
233
/* write back to the props */
234
RNA_property_float_set_array(op->ptr, prop_plane_no, plane_no);
235
RNA_property_float_set_array(op->ptr, prop_plane_co, plane_co);
238
/* -------------------------------------------------------------------- */
244
invert_m4_m4(imat, obedit->obmat);
245
mul_m4_v3(imat, plane_co);
246
mul_mat3_m4_v3(imat, plane_no);
248
EDBM_op_init(em, &bmop, op,
249
"bisect_plane geom=%hvef plane_co=%v plane_no=%v dist=%f clear_inner=%b clear_outer=%b",
250
BM_ELEM_SELECT, plane_co, plane_no, thresh, clear_inner, clear_outer);
251
BMO_op_exec(bm, &bmop);
253
EDBM_flag_disable_all(em, BM_ELEM_SELECT);
256
float normal_fill[3];
257
BMOperator bmop_fill;
258
BMOperator bmop_attr;
260
normalize_v3_v3(normal_fill, plane_no);
261
if (clear_outer == true && clear_inner == false) {
262
negate_v3(normal_fill);
267
bm, &bmop_fill, op->flag,
268
"triangle_fill edges=%S normal=%v use_dissolve=%b",
269
&bmop, "geom_cut.out", normal_fill, true);
270
BMO_op_exec(bm, &bmop_fill);
272
/* Copy Attributes */
273
BMO_op_initf(bm, &bmop_attr, op->flag,
274
"face_attribute_fill faces=%S use_normals=%b use_data=%b",
275
&bmop_fill, "geom.out", false, true);
276
BMO_op_exec(bm, &bmop_attr);
278
BMO_slot_buffer_hflag_enable(bm, bmop_fill.slots_out, "geom.out", BM_FACE, BM_ELEM_SELECT, true);
280
BMO_op_finish(bm, &bmop_attr);
281
BMO_op_finish(bm, &bmop_fill);
284
BMO_slot_buffer_hflag_enable(bm, bmop.slots_out, "geom_cut.out", BM_VERT | BM_EDGE, BM_ELEM_SELECT, true);
286
if (!EDBM_op_finish(em, &bmop, op, true)) {
287
return OPERATOR_CANCELLED;
290
EDBM_update_generic(em, true, true);
291
EDBM_selectmode_flush(em);
292
return OPERATOR_FINISHED;
297
void MESH_OT_bisect(struct wmOperatorType *ot)
303
ot->description = "Cut geometry along a plane";
304
ot->idname = "MESH_OT_bisect";
307
ot->exec = mesh_bisect_exec;
308
ot->invoke = mesh_bisect_invoke;
309
ot->modal = mesh_bisect_modal;
310
ot->cancel = WM_gesture_straightline_cancel;
311
ot->poll = ED_operator_editmesh;
314
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
317
prop = RNA_def_float_vector(ot->srna, "plane_co", 3, NULL, -FLT_MAX, FLT_MAX,
318
"Plane Point", "A point on the plane", -FLT_MAX, FLT_MAX);
319
RNA_def_property_flag(prop, PROP_SKIP_SAVE);
320
prop = RNA_def_float_vector(ot->srna, "plane_no", 3, NULL, -FLT_MAX, FLT_MAX,
321
"Plane Normal", "The direction the plane points", -FLT_MAX, FLT_MAX);
322
RNA_def_property_flag(prop, PROP_SKIP_SAVE);
324
RNA_def_boolean(ot->srna, "use_fill", false, "Fill", "Fill in the cut");
325
RNA_def_boolean(ot->srna, "clear_inner", false, "Clear Inner", "Remove geometry behind the plane");
326
RNA_def_boolean(ot->srna, "clear_outer", false, "Clear Outer", "Remove geometry in front of the plane");
328
RNA_def_float(ot->srna, "threshold", 0.0001, 0.0, 10.0, "Axis Threshold", "", 0.00001, 0.1);
330
WM_operator_properties_gesture_straightline(ot, CURSOR_EDIT);