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) 2012 by Nicholas Bishop
19 * All rights reserved.
21 * The Original Code is: all of this file.
25 * ***** END GPL LICENSE BLOCK *****
29 /** \file blender/editors/sculpt_paint/paint_mask.c
33 #include "MEM_guardedalloc.h"
35 #include "DNA_meshdata_types.h"
36 #include "DNA_object_types.h"
38 #include "BIF_glutil.h"
40 #include "BLI_bitmap_draw_2d.h"
41 #include "BLI_math_matrix.h"
42 #include "BLI_math_geom.h"
43 #include "BLI_utildefines.h"
44 #include "BLI_lasso.h"
49 #include "BKE_context.h"
50 #include "BKE_DerivedMesh.h"
51 #include "BKE_multires.h"
52 #include "BKE_paint.h"
53 #include "BKE_subsurf.h"
55 #include "RNA_access.h"
56 #include "RNA_define.h"
61 #include "ED_screen.h"
62 #include "ED_sculpt.h"
63 #include "ED_view3d.h"
67 #include "paint_intern.h"
68 #include "sculpt_intern.h" /* for undo push */
72 static EnumPropertyItem mode_items[] = {
73 {PAINT_MASK_FLOOD_VALUE, "VALUE", 0, "Value", "Set mask to the level specified by the 'value' property"},
74 {PAINT_MASK_FLOOD_VALUE_INVERSE, "VALUE_INVERSE", 0, "Value Inverted", "Set mask to the level specified by the inverted 'value' property"},
75 {PAINT_MASK_INVERT, "INVERT", 0, "Invert", "Invert the mask"},
79 static void mask_flood_fill_set_elem(float *elem,
80 PaintMaskFloodMode mode,
84 case PAINT_MASK_FLOOD_VALUE:
87 case PAINT_MASK_FLOOD_VALUE_INVERSE:
88 (*elem) = 1.0f - value;
90 case PAINT_MASK_INVERT:
91 (*elem) = 1.0f - (*elem);
96 typedef struct MaskTaskData {
102 PaintMaskFloodMode mode;
104 float (*clip_planes_final)[4];
107 static void mask_flood_fill_task_cb(void *userdata, const int i)
109 MaskTaskData *data = userdata;
111 PBVHNode *node = data->nodes[i];
113 const PaintMaskFloodMode mode = data->mode;
114 const float value = data->value;
118 sculpt_undo_push_node(data->ob, node, SCULPT_UNDO_MASK);
120 BKE_pbvh_vertex_iter_begin(data->pbvh, node, vi, PBVH_ITER_UNIQUE) {
121 mask_flood_fill_set_elem(vi.mask, mode, value);
122 } BKE_pbvh_vertex_iter_end;
124 BKE_pbvh_node_mark_redraw(node);
126 BKE_pbvh_node_mark_normals_update(node);
129 static int mask_flood_fill_exec(bContext *C, wmOperator *op)
131 ARegion *ar = CTX_wm_region(C);
132 struct Scene *scene = CTX_data_scene(C);
133 Object *ob = CTX_data_active_object(C);
134 PaintMaskFloodMode mode;
140 Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
142 mode = RNA_enum_get(op->ptr, "mode");
143 value = RNA_float_get(op->ptr, "value");
145 BKE_sculpt_update_mesh_elements(scene, sd, ob, false, true);
146 pbvh = ob->sculpt->pbvh;
147 multires = (BKE_pbvh_type(pbvh) == PBVH_GRIDS);
149 BKE_pbvh_search_gather(pbvh, NULL, NULL, &nodes, &totnode);
151 sculpt_undo_push_begin("Mask flood fill");
153 MaskTaskData data = {
154 .ob = ob, .pbvh = pbvh, .nodes = nodes, .multires = multires,
155 .mode = mode, .value = value,
158 BLI_task_parallel_range(
159 0, totnode, &data, mask_flood_fill_task_cb,
160 ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT));
163 multires_mark_as_modified(ob, MULTIRES_COORDS_MODIFIED);
165 sculpt_undo_push_end(C);
170 ED_region_tag_redraw(ar);
172 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
174 return OPERATOR_FINISHED;
177 void PAINT_OT_mask_flood_fill(struct wmOperatorType *ot)
180 ot->name = "Mask Flood Fill";
181 ot->idname = "PAINT_OT_mask_flood_fill";
182 ot->description = "Fill the whole mask with a given value, or invert its values";
185 ot->exec = mask_flood_fill_exec;
186 ot->poll = sculpt_mode_poll;
188 ot->flag = OPTYPE_REGISTER;
191 RNA_def_enum(ot->srna, "mode", mode_items, PAINT_MASK_FLOOD_VALUE, "Mode", NULL);
192 RNA_def_float(ot->srna, "value", 0, 0, 1, "Value",
193 "Mask level to use when mode is 'Value'; zero means no masking and one is fully masked", 0, 1);
196 /* Box select, operator is VIEW3D_OT_select_border, defined in view3d_select.c */
198 static bool is_effected(float planes[4][4], const float co[3])
200 return isect_point_planes_v3(planes, 4, co);
203 static void flip_plane(float out[4], const float in[4], const char symm)
205 if (symm & PAINT_SYMM_X)
209 if (symm & PAINT_SYMM_Y)
213 if (symm & PAINT_SYMM_Z)
221 static void mask_box_select_task_cb(void *userdata, const int i)
223 MaskTaskData *data = userdata;
225 PBVHNode *node = data->nodes[i];
227 const PaintMaskFloodMode mode = data->mode;
228 const float value = data->value;
229 float (*clip_planes_final)[4] = data->clip_planes_final;
232 bool any_masked = false;
234 BKE_pbvh_vertex_iter_begin(data->pbvh, node, vi, PBVH_ITER_UNIQUE) {
235 if (is_effected(clip_planes_final, vi.co)) {
239 sculpt_undo_push_node(data->ob, node, SCULPT_UNDO_MASK);
241 BKE_pbvh_node_mark_redraw(node);
243 BKE_pbvh_node_mark_normals_update(node);
245 mask_flood_fill_set_elem(vi.mask, mode, value);
247 } BKE_pbvh_vertex_iter_end;
250 int ED_sculpt_mask_box_select(struct bContext *C, ViewContext *vc, const rcti *rect, bool select, bool UNUSED(extend))
252 Sculpt *sd = vc->scene->toolsettings->sculpt;
254 bglMats mats = {{0}};
255 float clip_planes[4][4];
256 float clip_planes_final[4][4];
257 ARegion *ar = vc->ar;
258 struct Scene *scene = vc->scene;
259 Object *ob = vc->obact;
260 PaintMaskFloodMode mode;
265 int totnode, symmpass;
266 int symm = sd->paint.symmetry_flags & PAINT_SYMM_AXIS_ALL;
268 mode = PAINT_MASK_FLOOD_VALUE;
269 value = select ? 1.0 : 0.0;
271 /* transform the clip planes in object space */
272 view3d_get_transformation(vc->ar, vc->rv3d, vc->obact, &mats);
273 ED_view3d_clipping_calc(&bb, clip_planes, &mats, rect);
274 negate_m4(clip_planes);
276 BKE_sculpt_update_mesh_elements(scene, sd, ob, false, true);
277 pbvh = ob->sculpt->pbvh;
278 multires = (BKE_pbvh_type(pbvh) == PBVH_GRIDS);
280 sculpt_undo_push_begin("Mask box fill");
282 for (symmpass = 0; symmpass <= symm; ++symmpass) {
285 (symm != 5 || symmpass != 3) &&
286 (symm != 6 || (symmpass != 3 && symmpass != 5))))
290 /* flip the planes symmetrically as needed */
292 flip_plane(clip_planes_final[j], clip_planes[j], symmpass);
295 BKE_pbvh_search_gather(pbvh, BKE_pbvh_node_planes_contain_AABB, clip_planes_final, &nodes, &totnode);
297 MaskTaskData data = {
298 .ob = ob, .pbvh = pbvh, .nodes = nodes, .multires = multires,
299 .mode = mode, .value = value, .clip_planes_final = clip_planes_final,
302 BLI_task_parallel_range(
303 0, totnode, &data, mask_box_select_task_cb,
304 ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT));
312 multires_mark_as_modified(ob, MULTIRES_COORDS_MODIFIED);
314 sculpt_undo_push_end(C);
316 ED_region_tag_redraw(ar);
318 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
320 return OPERATOR_FINISHED;
323 typedef struct LassoMaskData {
324 struct ViewContext *vc;
325 float projviewobjmat[4][4];
328 rcti rect; /* bounding box for scanfilling */
331 MaskTaskData task_data;
335 /* Lasso select. This could be defined as part of VIEW3D_OT_select_lasso, still the shortcuts conflict,
336 * so we will use a separate operator */
338 static bool is_effected_lasso(LassoMaskData *data, float co[3])
344 flip_v3_v3(co_final, co, data->symmpass);
345 /* first project point to 2d space */
346 ED_view3d_project_float_v2_m4(data->vc->ar, co_final, scr_co_f, data->projviewobjmat);
348 scr_co_s[0] = scr_co_f[0];
349 scr_co_s[1] = scr_co_f[1];
351 /* clip against screen, because lasso is limited to screen only */
352 if ((scr_co_s[0] < data->rect.xmin) ||
353 (scr_co_s[1] < data->rect.ymin) ||
354 (scr_co_s[0] >= data->rect.xmax) ||
355 (scr_co_s[1] >= data->rect.ymax))
360 scr_co_s[0] -= data->rect.xmin;
361 scr_co_s[1] -= data->rect.ymin;
363 return BLI_BITMAP_TEST_BOOL(data->px, scr_co_s[1] * data->width + scr_co_s[0]);
366 static void mask_lasso_px_cb(int x, int x_end, int y, void *user_data)
368 LassoMaskData *data = user_data;
369 int index = (y * data->width) + x;
370 int index_end = (y * data->width) + x_end;
372 BLI_BITMAP_ENABLE(data->px, index);
373 } while (++index != index_end);
376 static void mask_gesture_lasso_task_cb(void *userdata, const int i)
378 LassoMaskData *lasso_data = userdata;
379 MaskTaskData *data = &lasso_data->task_data;
381 PBVHNode *node = data->nodes[i];
383 const PaintMaskFloodMode mode = data->mode;
384 const float value = data->value;
387 bool any_masked = false;
389 BKE_pbvh_vertex_iter_begin(data->pbvh, node, vi, PBVH_ITER_UNIQUE) {
390 if (is_effected_lasso(lasso_data, vi.co)) {
394 sculpt_undo_push_node(data->ob, node, SCULPT_UNDO_MASK);
396 BKE_pbvh_node_mark_redraw(node);
398 BKE_pbvh_node_mark_normals_update(node);
401 mask_flood_fill_set_elem(vi.mask, mode, value);
403 } BKE_pbvh_vertex_iter_end;
406 static int paint_mask_gesture_lasso_exec(bContext *C, wmOperator *op)
409 const int (*mcords)[2] = WM_gesture_lasso_path_to_array(C, op, &mcords_tot);
412 float clip_planes[4][4], clip_planes_final[4][4];
414 bglMats mats = {{0}};
418 struct Scene *scene = CTX_data_scene(C);
419 Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
420 int symm = sd->paint.symmetry_flags & PAINT_SYMM_AXIS_ALL;
423 int totnode, symmpass;
425 PaintMaskFloodMode mode = RNA_enum_get(op->ptr, "mode");
426 float value = RNA_float_get(op->ptr, "value");
428 /* Calculations of individual vertices are done in 2D screen space to diminish the amount of
429 * calculations done. Bounding box PBVH collision is not computed against enclosing rectangle
431 view3d_set_viewcontext(C, &vc);
432 view3d_get_transformation(vc.ar, vc.rv3d, vc.obact, &mats);
434 /* lasso data calculations */
437 ED_view3d_ob_project_mat_get(vc.rv3d, ob, data.projviewobjmat);
439 BLI_lasso_boundbox(&data.rect, mcords, mcords_tot);
440 data.width = data.rect.xmax - data.rect.xmin;
441 data.px = BLI_BITMAP_NEW(data.width * (data.rect.ymax - data.rect.ymin), __func__);
443 BLI_bitmap_draw_2d_poly_v2i_n(
444 data.rect.xmin, data.rect.ymin, data.rect.xmax, data.rect.ymax,
446 mask_lasso_px_cb, &data);
448 ED_view3d_clipping_calc(&bb, clip_planes, &mats, &data.rect);
449 negate_m4(clip_planes);
451 BKE_sculpt_update_mesh_elements(scene, sd, ob, false, true);
452 pbvh = ob->sculpt->pbvh;
453 multires = (BKE_pbvh_type(pbvh) == PBVH_GRIDS);
455 sculpt_undo_push_begin("Mask lasso fill");
457 for (symmpass = 0; symmpass <= symm; ++symmpass) {
458 if ((symmpass == 0) ||
460 (symm != 5 || symmpass != 3) &&
461 (symm != 6 || (symmpass != 3 && symmpass != 5))))
465 /* flip the planes symmetrically as needed */
467 flip_plane(clip_planes_final[j], clip_planes[j], symmpass);
470 data.symmpass = symmpass;
472 /* gather nodes inside lasso's enclosing rectangle (should greatly help with bigger meshes) */
473 BKE_pbvh_search_gather(pbvh, BKE_pbvh_node_planes_contain_AABB, clip_planes_final, &nodes, &totnode);
475 data.task_data.ob = ob;
476 data.task_data.pbvh = pbvh;
477 data.task_data.nodes = nodes;
478 data.task_data.multires = multires;
479 data.task_data.mode = mode;
480 data.task_data.value = value;
482 BLI_task_parallel_range(
483 0, totnode, &data, mask_gesture_lasso_task_cb,
484 ((sd->flags & SCULPT_USE_OPENMP) && (totnode > SCULPT_THREADED_LIMIT)));
492 multires_mark_as_modified(ob, MULTIRES_COORDS_MODIFIED);
494 sculpt_undo_push_end(C);
496 ED_region_tag_redraw(vc.ar);
497 MEM_freeN((void *)mcords);
500 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
502 return OPERATOR_FINISHED;
504 return OPERATOR_PASS_THROUGH;
507 void PAINT_OT_mask_lasso_gesture(wmOperatorType *ot)
509 ot->name = "Mask Lasso Gesture";
510 ot->idname = "PAINT_OT_mask_lasso_gesture";
511 ot->description = "Add mask within the lasso as you move the brush";
513 ot->invoke = WM_gesture_lasso_invoke;
514 ot->modal = WM_gesture_lasso_modal;
515 ot->exec = paint_mask_gesture_lasso_exec;
517 ot->poll = sculpt_mode_poll;
519 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
522 WM_operator_properties_gesture_lasso(ot);
524 RNA_def_enum(ot->srna, "mode", mode_items, PAINT_MASK_FLOOD_VALUE, "Mode", NULL);
525 RNA_def_float(ot->srna, "value", 1.0, 0, 1.0, "Value",
526 "Mask level to use when mode is 'Value'; zero means no masking and one is fully masked", 0, 1);