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_listbase.h"
39 #include "BLI_math_vector.h"
41 #include "BLI_utildefines.h"
43 #include "DNA_mesh_types.h"
44 #include "DNA_meshdata_types.h"
45 #include "DNA_object_types.h"
46 #include "DNA_scene_types.h"
49 #include "BKE_context.h"
50 #include "BKE_DerivedMesh.h"
52 #include "BKE_multires.h"
53 #include "BKE_paint.h"
54 #include "BKE_subsurf.h"
56 #include "BIF_glutil.h"
61 #include "ED_screen.h"
62 #include "ED_view3d.h"
64 #include "RNA_access.h"
65 #include "RNA_define.h"
67 #include "paint_intern.h"
68 #include "sculpt_intern.h" /* for undo push */
72 static int planes_contain_v3(float (*planes)[4], int totplane, const float p[3])
76 for (i = 0; i < totplane; i++) {
77 if (dot_v3v3(planes[i], p) + planes[i][3] > 0)
84 /* return true if the element should be hidden/shown */
85 static int is_effected(PartialVisArea area,
90 if (area == PARTIALVIS_ALL)
92 else if (area == PARTIALVIS_MASKED) {
96 int inside = planes_contain_v3(planes, 4, co);
97 return ((inside && area == PARTIALVIS_INSIDE) ||
98 (!inside && area == PARTIALVIS_OUTSIDE));
102 static void partialvis_update_mesh(Object *ob,
105 PartialVisAction action,
113 int any_changed = 0, any_visible = 0, totvert, i;
115 BLI_pbvh_node_num_verts(pbvh, node, NULL, &totvert);
116 BLI_pbvh_node_get_verts(pbvh, node, &vert_indices, &mvert);
117 paint_mask = CustomData_get_layer(&me->vdata, CD_PAINT_MASK);
119 sculpt_undo_push_node(ob, node, SCULPT_UNDO_HIDDEN);
121 for (i = 0; i < totvert; i++) {
122 MVert *v = &mvert[vert_indices[i]];
123 float vmask = paint_mask ? paint_mask[vert_indices[i]] : 0;
125 /* hide vertex if in the hide volume */
126 if (is_effected(area, planes, v->co, vmask)) {
127 if (action == PARTIALVIS_HIDE)
134 if (!(v->flag & ME_HIDE))
139 BLI_pbvh_node_mark_rebuild_draw(node);
140 BLI_pbvh_node_fully_hidden_set(node, !any_visible);
144 /* Hide or show elements in multires grids with a special GridFlags
145 * customdata layer. */
146 static void partialvis_update_grids(Object *ob,
149 PartialVisAction action,
155 BLI_bitmap *grid_hidden;
157 int *grid_indices, totgrid, any_changed, i;
160 BLI_pbvh_node_get_grids(pbvh, node,
161 &grid_indices, &totgrid, NULL, NULL,
163 grid_hidden = BLI_pbvh_grid_hidden(pbvh);
164 BLI_pbvh_get_grid_key(pbvh, &key);
166 sculpt_undo_push_node(ob, node, SCULPT_UNDO_HIDDEN);
169 for (i = 0; i < totgrid; i++) {
171 int g = grid_indices[i], x, y;
172 BLI_bitmap gh = grid_hidden[g];
176 case PARTIALVIS_HIDE:
177 /* create grid flags data */
178 gh = grid_hidden[g] = BLI_BITMAP_NEW(key.grid_area,
179 "partialvis_update_grids");
181 case PARTIALVIS_SHOW:
182 /* entire grid is visible, nothing to show */
186 else if (action == PARTIALVIS_SHOW && area == PARTIALVIS_ALL) {
187 /* special case if we're showing all, just free the
190 grid_hidden[g] = NULL;
196 for (y = 0; y < key.grid_size; y++) {
197 for (x = 0; x < key.grid_size; x++) {
198 CCGElem *elem = CCG_grid_elem(&key, grids[g], x, y);
199 const float *co = CCG_elem_co(&key, elem);
200 float mask = *CCG_elem_mask(&key, elem);
202 /* skip grid element if not in the effected area */
203 if (is_effected(area, planes, co, mask)) {
204 /* set or clear the hide flag */
205 BLI_BITMAP_MODIFY(gh, y * key.grid_size + x,
206 action == PARTIALVIS_HIDE);
211 /* keep track of whether any elements are still hidden */
212 if (BLI_BITMAP_GET(gh, y * key.grid_size + x))
219 /* if everything in the grid is now visible, free the grid
223 grid_hidden[g] = NULL;
227 /* mark updates if anything was hidden/shown */
229 BLI_pbvh_node_mark_rebuild_draw(node);
230 BLI_pbvh_node_fully_hidden_set(node, !any_visible);
231 multires_mark_as_modified(ob, MULTIRES_HIDDEN_MODIFIED);
235 static void rect_from_props(rcti *rect, PointerRNA *ptr)
237 rect->xmin = RNA_int_get(ptr, "xmin");
238 rect->ymin = RNA_int_get(ptr, "ymin");
239 rect->xmax = RNA_int_get(ptr, "xmax");
240 rect->ymax = RNA_int_get(ptr, "ymax");
243 static void clip_planes_from_rect(bContext *C,
244 float clip_planes[4][4],
249 bglMats mats = {{0}};
251 view3d_operator_needs_opengl(C);
252 view3d_set_viewcontext(C, &vc);
253 view3d_get_transformation(vc.ar, vc.rv3d, vc.obact, &mats);
254 ED_view3d_calc_clipping(&bb, clip_planes, &mats, rect);
255 mul_m4_fl(clip_planes, -1.0f);
258 /* If mode is inside, get all PBVH nodes that lie at least partially
259 * inside the clip_planes volume. If mode is outside, get all nodes
260 * that lie at least partially outside the volume. If showing all, get
262 static void get_pbvh_nodes(PBVH *pbvh,
265 float clip_planes[4][4],
268 BLI_pbvh_SearchCallback cb = NULL;
270 /* select search callback */
272 case PARTIALVIS_INSIDE:
273 cb = BLI_pbvh_node_planes_contain_AABB;
275 case PARTIALVIS_OUTSIDE:
276 cb = BLI_pbvh_node_planes_exclude_AABB;
279 case PARTIALVIS_MASKED:
283 BLI_pbvh_search_gather(pbvh, cb, clip_planes, nodes, totnode);
286 static int hide_show_exec(bContext *C, wmOperator *op)
288 ARegion *ar = CTX_wm_region(C);
289 Object *ob = CTX_data_active_object(C);
291 PartialVisAction action;
297 float clip_planes[4][4];
301 /* read operator properties */
302 action = RNA_enum_get(op->ptr, "action");
303 area = RNA_enum_get(op->ptr, "area");
304 rect_from_props(&rect, op->ptr);
306 clip_planes_from_rect(C, clip_planes, &rect);
308 dm = mesh_get_derived_final(CTX_data_scene(C), ob, CD_MASK_BAREMESH);
309 pbvh = dm->getPBVH(ob, dm);
310 ob->sculpt->pbvh = pbvh;
312 get_pbvh_nodes(pbvh, &nodes, &totnode, clip_planes, area);
313 pbvh_type = BLI_pbvh_type(pbvh);
317 case PARTIALVIS_HIDE:
318 sculpt_undo_push_begin("Hide area");
320 case PARTIALVIS_SHOW:
321 sculpt_undo_push_begin("Show area");
325 for (i = 0; i < totnode; i++) {
328 partialvis_update_mesh(ob, pbvh, nodes[i], action, area, clip_planes);
331 partialvis_update_grids(ob, pbvh, nodes[i], action, area, clip_planes);
340 sculpt_undo_push_end();
342 /* ensure that edges and faces get hidden as well (not used by
343 * sculpt but it looks wrong when entering editmode otherwise) */
344 if (pbvh_type == PBVH_FACES) {
345 BKE_mesh_flush_hidden_from_verts(me->mvert, me->mloop,
346 me->medge, me->totedge,
347 me->mpoly, me->totpoly);
350 ED_region_tag_redraw(ar);
352 return OPERATOR_FINISHED;
355 static int hide_show_invoke(bContext *C, wmOperator *op, wmEvent *event)
357 PartialVisArea area = RNA_enum_get(op->ptr, "area");
359 if (!ELEM(area, PARTIALVIS_ALL, PARTIALVIS_MASKED))
360 return WM_border_select_invoke(C, op, event);
362 return op->type->exec(C, op);
365 void PAINT_OT_hide_show(struct wmOperatorType *ot)
367 static EnumPropertyItem action_items[] = {
368 {PARTIALVIS_HIDE, "HIDE", 0, "Hide", "Hide vertices"},
369 {PARTIALVIS_SHOW, "SHOW", 0, "Show", "Show vertices"},
370 {0, NULL, 0, NULL, NULL}
373 static EnumPropertyItem area_items[] = {
374 {PARTIALVIS_OUTSIDE, "OUTSIDE", 0, "Outside", "Hide or show vertices outside the selection"},
375 {PARTIALVIS_INSIDE, "INSIDE", 0, "Inside", "Hide or show vertices inside the selection"},
376 {PARTIALVIS_ALL, "ALL", 0, "All", "Hide or show all vertices"},
377 {PARTIALVIS_MASKED, "MASKED", 0, "Masked", "Hide or show vertices that are masked (minimum mask value of 0.5)"},
378 {0, NULL, 0, NULL, NULL}
382 ot->name = "Hide/Show";
383 ot->idname = "PAINT_OT_hide_show";
384 ot->description = "Hide/show some vertices";
387 ot->invoke = hide_show_invoke;
388 ot->modal = WM_border_select_modal;
389 ot->exec = hide_show_exec;
390 /* sculpt-only for now */
391 ot->poll = sculpt_mode_poll;
393 ot->flag = OPTYPE_REGISTER;
396 RNA_def_enum(ot->srna, "action", action_items, PARTIALVIS_HIDE,
397 "Action", "Whether to hide or show vertices");
398 RNA_def_enum(ot->srna, "area", area_items, PARTIALVIS_INSIDE,
399 "Area", "Which vertices to hide or show");
401 WM_operator_properties_border(ot);