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) 2010 by Nicholas Bishop
19 * All rights reserved.
21 * The Original Code is: all of this file.
25 * ***** END GPL LICENSE BLOCK *****
27 * Implements the PBVH node hiding operator
31 /** \file blender/editors/sculpt_paint/paint_hide.c
35 #include "MEM_guardedalloc.h"
37 #include "BLI_bitmap.h"
38 #include "BLI_math_vector.h"
39 #include "BLI_utildefines.h"
41 #include "DNA_mesh_types.h"
42 #include "DNA_meshdata_types.h"
43 #include "DNA_object_types.h"
44 #include "DNA_scene_types.h"
48 #include "BKE_context.h"
49 #include "BKE_DerivedMesh.h"
51 #include "BKE_multires.h"
52 #include "BKE_paint.h"
53 #include "BKE_subsurf.h"
55 #include "BIF_glutil.h"
60 #include "ED_screen.h"
61 #include "ED_view3d.h"
63 #include "RNA_access.h"
64 #include "RNA_define.h"
68 #include "paint_intern.h"
69 #include "sculpt_intern.h" /* for undo push */
73 /* return true if the element should be hidden/shown */
74 static bool is_effected(PartialVisArea area,
79 if (area == PARTIALVIS_ALL)
81 else if (area == PARTIALVIS_MASKED) {
85 bool inside = isect_point_planes_v3(planes, 4, co);
86 return ((inside && area == PARTIALVIS_INSIDE) ||
87 (!inside && area == PARTIALVIS_OUTSIDE));
91 static void partialvis_update_mesh(Object *ob,
94 PartialVisAction action,
100 const float *paint_mask;
101 const int *vert_indices;
103 bool any_changed = false, any_visible = false;
105 BKE_pbvh_node_num_verts(pbvh, node, NULL, &totvert);
106 BKE_pbvh_node_get_verts(pbvh, node, &vert_indices, &mvert);
107 paint_mask = CustomData_get_layer(&me->vdata, CD_PAINT_MASK);
109 sculpt_undo_push_node(ob, node, SCULPT_UNDO_HIDDEN);
111 for (i = 0; i < totvert; i++) {
112 MVert *v = &mvert[vert_indices[i]];
113 float vmask = paint_mask ? paint_mask[vert_indices[i]] : 0;
115 /* hide vertex if in the hide volume */
116 if (is_effected(area, planes, v->co, vmask)) {
117 if (action == PARTIALVIS_HIDE)
124 if (!(v->flag & ME_HIDE))
129 BKE_pbvh_node_mark_rebuild_draw(node);
130 BKE_pbvh_node_fully_hidden_set(node, !any_visible);
134 /* Hide or show elements in multires grids with a special GridFlags
135 * customdata layer. */
136 static void partialvis_update_grids(Object *ob,
139 PartialVisAction action,
145 BLI_bitmap **grid_hidden;
146 int *grid_indices, totgrid, i;
147 bool any_changed = false, any_visible = false;
151 BKE_pbvh_node_get_grids(pbvh, node,
152 &grid_indices, &totgrid, NULL, NULL,
154 grid_hidden = BKE_pbvh_grid_hidden(pbvh);
155 BKE_pbvh_get_grid_key(pbvh, &key);
157 sculpt_undo_push_node(ob, node, SCULPT_UNDO_HIDDEN);
159 for (i = 0; i < totgrid; i++) {
161 int g = grid_indices[i], x, y;
162 BLI_bitmap *gh = grid_hidden[g];
166 case PARTIALVIS_HIDE:
167 /* create grid flags data */
168 gh = grid_hidden[g] = BLI_BITMAP_NEW(key.grid_area,
169 "partialvis_update_grids");
171 case PARTIALVIS_SHOW:
172 /* entire grid is visible, nothing to show */
176 else if (action == PARTIALVIS_SHOW && area == PARTIALVIS_ALL) {
177 /* special case if we're showing all, just free the
180 grid_hidden[g] = NULL;
186 for (y = 0; y < key.grid_size; y++) {
187 for (x = 0; x < key.grid_size; x++) {
188 CCGElem *elem = CCG_grid_elem(&key, grids[g], x, y);
189 const float *co = CCG_elem_co(&key, elem);
190 float mask = key.has_mask ? *CCG_elem_mask(&key, elem) : 0.0f;
192 /* skip grid element if not in the effected area */
193 if (is_effected(area, planes, co, mask)) {
194 /* set or clear the hide flag */
195 BLI_BITMAP_SET(gh, y * key.grid_size + x,
196 action == PARTIALVIS_HIDE);
201 /* keep track of whether any elements are still hidden */
202 if (BLI_BITMAP_TEST(gh, y * key.grid_size + x))
209 /* if everything in the grid is now visible, free the grid
213 grid_hidden[g] = NULL;
217 /* mark updates if anything was hidden/shown */
219 BKE_pbvh_node_mark_rebuild_draw(node);
220 BKE_pbvh_node_fully_hidden_set(node, !any_visible);
221 multires_mark_as_modified(ob, MULTIRES_HIDDEN_MODIFIED);
225 static void partialvis_update_bmesh_verts(BMesh *bm,
227 PartialVisAction action,
233 GSetIterator gs_iter;
235 GSET_ITER (gs_iter, verts) {
236 BMVert *v = BLI_gsetIterator_getKey(&gs_iter);
237 float *vmask = CustomData_bmesh_get(&bm->vdata,
241 /* hide vertex if in the hide volume */
242 if (is_effected(area, planes, v->co, *vmask)) {
243 if (action == PARTIALVIS_HIDE)
244 BM_elem_flag_enable(v, BM_ELEM_HIDDEN);
246 BM_elem_flag_disable(v, BM_ELEM_HIDDEN);
247 (*any_changed) = true;
250 if (!BM_elem_flag_test(v, BM_ELEM_HIDDEN))
251 (*any_visible) = true;
255 static void partialvis_update_bmesh_faces(GSet *faces)
257 GSetIterator gs_iter;
259 GSET_ITER (gs_iter, faces) {
260 BMFace *f = BLI_gsetIterator_getKey(&gs_iter);
262 if (paint_is_bmesh_face_hidden(f))
263 BM_elem_flag_enable(f, BM_ELEM_HIDDEN);
265 BM_elem_flag_disable(f, BM_ELEM_HIDDEN);
269 static void partialvis_update_bmesh(Object *ob,
272 PartialVisAction action,
277 GSet *unique, *other, *faces;
278 bool any_changed = false, any_visible = false;
280 bm = BKE_pbvh_get_bmesh(pbvh);
281 unique = BKE_pbvh_bmesh_node_unique_verts(node);
282 other = BKE_pbvh_bmesh_node_other_verts(node);
283 faces = BKE_pbvh_bmesh_node_faces(node);
285 sculpt_undo_push_node(ob, node, SCULPT_UNDO_HIDDEN);
287 partialvis_update_bmesh_verts(bm,
295 partialvis_update_bmesh_verts(bm,
303 /* finally loop over node faces and tag the ones that are fully hidden */
304 partialvis_update_bmesh_faces(faces);
307 BKE_pbvh_node_mark_rebuild_draw(node);
308 BKE_pbvh_node_fully_hidden_set(node, !any_visible);
312 static void rect_from_props(rcti *rect, PointerRNA *ptr)
314 rect->xmin = RNA_int_get(ptr, "xmin");
315 rect->ymin = RNA_int_get(ptr, "ymin");
316 rect->xmax = RNA_int_get(ptr, "xmax");
317 rect->ymax = RNA_int_get(ptr, "ymax");
320 static void clip_planes_from_rect(bContext *C,
321 float clip_planes[4][4],
326 bglMats mats = {{0}};
328 view3d_operator_needs_opengl(C);
329 view3d_set_viewcontext(C, &vc);
330 view3d_get_transformation(vc.ar, vc.rv3d, vc.obact, &mats);
331 ED_view3d_clipping_calc(&bb, clip_planes, &mats, rect);
332 negate_m4(clip_planes);
335 /* If mode is inside, get all PBVH nodes that lie at least partially
336 * inside the clip_planes volume. If mode is outside, get all nodes
337 * that lie at least partially outside the volume. If showing all, get
339 static void get_pbvh_nodes(PBVH *pbvh,
342 float clip_planes[4][4],
345 BKE_pbvh_SearchCallback cb = NULL;
347 /* select search callback */
349 case PARTIALVIS_INSIDE:
350 cb = BKE_pbvh_node_planes_contain_AABB;
352 case PARTIALVIS_OUTSIDE:
353 cb = BKE_pbvh_node_planes_exclude_AABB;
356 case PARTIALVIS_MASKED:
360 BKE_pbvh_search_gather(pbvh, cb, clip_planes, nodes, totnode);
363 static int hide_show_exec(bContext *C, wmOperator *op)
365 ARegion *ar = CTX_wm_region(C);
366 Object *ob = CTX_data_active_object(C);
368 PartialVisAction action;
374 float clip_planes[4][4];
378 /* read operator properties */
379 action = RNA_enum_get(op->ptr, "action");
380 area = RNA_enum_get(op->ptr, "area");
381 rect_from_props(&rect, op->ptr);
383 clip_planes_from_rect(C, clip_planes, &rect);
385 dm = mesh_get_derived_final(CTX_data_scene(C), ob, CD_MASK_BAREMESH);
386 pbvh = dm->getPBVH(ob, dm);
387 ob->sculpt->pbvh = pbvh;
389 get_pbvh_nodes(pbvh, &nodes, &totnode, clip_planes, area);
390 pbvh_type = BKE_pbvh_type(pbvh);
394 case PARTIALVIS_HIDE:
395 sculpt_undo_push_begin("Hide area");
397 case PARTIALVIS_SHOW:
398 sculpt_undo_push_begin("Show area");
402 for (i = 0; i < totnode; i++) {
405 partialvis_update_mesh(ob, pbvh, nodes[i], action, area, clip_planes);
408 partialvis_update_grids(ob, pbvh, nodes[i], action, area, clip_planes);
411 partialvis_update_bmesh(ob, pbvh, nodes[i], action, area, clip_planes);
420 sculpt_undo_push_end();
422 /* ensure that edges and faces get hidden as well (not used by
423 * sculpt but it looks wrong when entering editmode otherwise) */
424 if (pbvh_type == PBVH_FACES) {
425 BKE_mesh_flush_hidden_from_verts(me);
428 ED_region_tag_redraw(ar);
430 return OPERATOR_FINISHED;
433 static int hide_show_invoke(bContext *C, wmOperator *op, const wmEvent *event)
435 PartialVisArea area = RNA_enum_get(op->ptr, "area");
437 if (!ELEM(area, PARTIALVIS_ALL, PARTIALVIS_MASKED))
438 return WM_gesture_border_invoke(C, op, event);
440 return op->type->exec(C, op);
443 void PAINT_OT_hide_show(struct wmOperatorType *ot)
445 static const EnumPropertyItem action_items[] = {
446 {PARTIALVIS_HIDE, "HIDE", 0, "Hide", "Hide vertices"},
447 {PARTIALVIS_SHOW, "SHOW", 0, "Show", "Show vertices"},
448 {0, NULL, 0, NULL, NULL}
451 static const EnumPropertyItem area_items[] = {
452 {PARTIALVIS_OUTSIDE, "OUTSIDE", 0, "Outside", "Hide or show vertices outside the selection"},
453 {PARTIALVIS_INSIDE, "INSIDE", 0, "Inside", "Hide or show vertices inside the selection"},
454 {PARTIALVIS_ALL, "ALL", 0, "All", "Hide or show all vertices"},
455 {PARTIALVIS_MASKED, "MASKED", 0, "Masked", "Hide or show vertices that are masked (minimum mask value of 0.5)"},
456 {0, NULL, 0, NULL, NULL}
460 ot->name = "Hide/Show";
461 ot->idname = "PAINT_OT_hide_show";
462 ot->description = "Hide/show some vertices";
465 ot->invoke = hide_show_invoke;
466 ot->modal = WM_gesture_border_modal;
467 ot->exec = hide_show_exec;
468 /* sculpt-only for now */
469 ot->poll = sculpt_mode_poll_view3d;
471 ot->flag = OPTYPE_REGISTER;
474 RNA_def_enum(ot->srna, "action", action_items, PARTIALVIS_HIDE,
475 "Action", "Whether to hide or show vertices");
476 RNA_def_enum(ot->srna, "area", area_items, PARTIALVIS_INSIDE,
477 "Area", "Which vertices to hide or show");
479 WM_operator_properties_border(ot);