This repository has been archived on 2023-10-09. You can view files and clone it, but cannot push or open issues or pull requests.
Files
blender-archive/source/blender/editors/sculpt_paint/paint_hide.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

467 lines
13 KiB
C
Raw Normal View History

/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2020-05-09 17:14:35 +10:00
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2010 by Nicholas Bishop
* All rights reserved.
* Implements the PBVH node hiding operator
*/
/** \file
* \ingroup edsculpt
*/
#include "MEM_guardedalloc.h"
#include "BLI_bitmap.h"
#include "BLI_math_vector.h"
#include "BLI_utildefines.h"
#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_object_types.h"
#include "DNA_scene_types.h"
#include "BKE_ccg.h"
#include "BKE_context.h"
#include "BKE_mesh.h"
#include "BKE_multires.h"
#include "BKE_paint.h"
#include "BKE_pbvh.h"
#include "BKE_subsurf.h"
#include "DEG_depsgraph.h"
#include "WM_api.h"
#include "WM_types.h"
#include "ED_screen.h"
#include "ED_view3d.h"
#include "RNA_access.h"
#include "RNA_define.h"
#include "bmesh.h"
#include "paint_intern.h"
/* For undo push. */
#include "sculpt_intern.h"
/* Return true if the element should be hidden/shown. */
2018-07-02 18:45:26 +02:00
static bool is_effected(PartialVisArea area,
float planes[4][4],
const float co[3],
const float mask)
{
2019-04-22 09:19:45 +10:00
if (area == PARTIALVIS_ALL) {
return true;
2019-04-22 09:19:45 +10:00
}
if (area == PARTIALVIS_MASKED) {
return mask > 0.5f;
}
bool inside = isect_point_planes_v3(planes, 4, co);
return ((inside && area == PARTIALVIS_INSIDE) || (!inside && area == PARTIALVIS_OUTSIDE));
}
2018-07-02 18:45:26 +02:00
static void partialvis_update_mesh(Object *ob,
PBVH *pbvh,
PBVHNode *node,
PartialVisAction action,
PartialVisArea area,
float planes[4][4])
{
Mesh *me = ob->data;
MVert *mvert;
const float *paint_mask;
const int *vert_indices;
int totvert, i;
bool any_changed = false, any_visible = false;
BKE_pbvh_node_num_verts(pbvh, node, NULL, &totvert);
BKE_pbvh_node_get_verts(pbvh, node, &vert_indices, &mvert);
paint_mask = CustomData_get_layer(&me->vdata, CD_PAINT_MASK);
SCULPT_undo_push_node(ob, node, SCULPT_UNDO_HIDDEN);
for (i = 0; i < totvert; i++) {
MVert *v = &mvert[vert_indices[i]];
float vmask = paint_mask ? paint_mask[vert_indices[i]] : 0;
/* Hide vertex if in the hide volume. */
if (is_effected(area, planes, v->co, vmask)) {
2019-04-22 09:19:45 +10:00
if (action == PARTIALVIS_HIDE) {
v->flag |= ME_HIDE;
2019-04-22 09:19:45 +10:00
}
else {
v->flag &= ~ME_HIDE;
2019-04-22 09:19:45 +10:00
}
any_changed = true;
}
2019-04-22 09:19:45 +10:00
if (!(v->flag & ME_HIDE)) {
any_visible = true;
2019-04-22 09:19:45 +10:00
}
}
if (any_changed) {
BKE_pbvh_node_mark_rebuild_draw(node);
BKE_pbvh_node_fully_hidden_set(node, !any_visible);
}
}
/* Hide or show elements in multires grids with a special GridFlags
2012-04-22 11:54:53 +00:00
* customdata layer. */
static void partialvis_update_grids(Depsgraph *depsgraph,
Object *ob,
2018-07-02 18:45:26 +02:00
PBVH *pbvh,
PBVHNode *node,
PartialVisAction action,
PartialVisArea area,
float planes[4][4])
{
CCGElem **grids;
BLI_bitmap **grid_hidden;
int *grid_indices, totgrid;
bool any_changed = false, any_visible = false;
/* Get PBVH data. */
2018-07-02 18:45:26 +02:00
BKE_pbvh_node_get_grids(pbvh, node, &grid_indices, &totgrid, NULL, NULL, &grids);
grid_hidden = BKE_pbvh_grid_hidden(pbvh);
CCGKey key = *BKE_pbvh_get_grid_key(pbvh);
SCULPT_undo_push_node(ob, node, SCULPT_UNDO_HIDDEN);
for (int i = 0; i < totgrid; i++) {
int any_hidden = 0;
int g = grid_indices[i];
BLI_bitmap *gh = grid_hidden[g];
if (!gh) {
switch (action) {
case PARTIALVIS_HIDE:
/* Create grid flags data. */
2018-07-02 18:45:26 +02:00
gh = grid_hidden[g] = BLI_BITMAP_NEW(key.grid_area, "partialvis_update_grids");
break;
case PARTIALVIS_SHOW:
/* Entire grid is visible, nothing to show. */
continue;
}
}
else if (action == PARTIALVIS_SHOW && area == PARTIALVIS_ALL) {
/* Special case if we're showing all, just free the grid. */
MEM_freeN(gh);
grid_hidden[g] = NULL;
any_changed = true;
any_visible = true;
continue;
}
for (int y = 0; y < key.grid_size; y++) {
for (int x = 0; x < key.grid_size; x++) {
CCGElem *elem = CCG_grid_elem(&key, grids[g], x, y);
const float *co = CCG_elem_co(&key, elem);
float mask = key.has_mask ? *CCG_elem_mask(&key, elem) : 0.0f;
/* Skip grid element if not in the effected area. */
if (is_effected(area, planes, co, mask)) {
/* Set or clear the hide flag. */
2018-07-02 18:45:26 +02:00
BLI_BITMAP_SET(gh, y * key.grid_size + x, action == PARTIALVIS_HIDE);
any_changed = true;
}
/* Keep track of whether any elements are still hidden. */
2019-04-22 09:19:45 +10:00
if (BLI_BITMAP_TEST(gh, y * key.grid_size + x)) {
any_hidden = true;
2019-04-22 09:19:45 +10:00
}
else {
any_visible = true;
2019-04-22 09:19:45 +10:00
}
}
}
/* If everything in the grid is now visible, free the grid flags. */
if (!any_hidden) {
MEM_freeN(gh);
grid_hidden[g] = NULL;
}
}
/* Mark updates if anything was hidden/shown. */
if (any_changed) {
BKE_pbvh_node_mark_rebuild_draw(node);
BKE_pbvh_node_fully_hidden_set(node, !any_visible);
multires_mark_as_modified(depsgraph, ob, MULTIRES_HIDDEN_MODIFIED);
}
}
2018-07-02 18:45:26 +02:00
static void partialvis_update_bmesh_verts(BMesh *bm,
GSet *verts,
PartialVisAction action,
PartialVisArea area,
float planes[4][4],
bool *any_changed,
bool *any_visible)
2012-12-30 18:29:56 +00:00
{
GSetIterator gs_iter;
GSET_ITER (gs_iter, verts) {
BMVert *v = BLI_gsetIterator_getKey(&gs_iter);
2018-07-02 18:45:26 +02:00
float *vmask = CustomData_bmesh_get(&bm->vdata, v->head.data, CD_PAINT_MASK);
/* Hide vertex if in the hide volume. */
2012-12-30 18:29:56 +00:00
if (is_effected(area, planes, v->co, *vmask)) {
2019-04-22 09:19:45 +10:00
if (action == PARTIALVIS_HIDE) {
2012-12-30 18:29:56 +00:00
BM_elem_flag_enable(v, BM_ELEM_HIDDEN);
2019-04-22 09:19:45 +10:00
}
else {
2012-12-30 18:29:56 +00:00
BM_elem_flag_disable(v, BM_ELEM_HIDDEN);
2019-04-22 09:19:45 +10:00
}
(*any_changed) = true;
2012-12-30 18:29:56 +00:00
}
2019-04-22 09:19:45 +10:00
if (!BM_elem_flag_test(v, BM_ELEM_HIDDEN)) {
(*any_visible) = true;
2019-04-22 09:19:45 +10:00
}
2012-12-30 18:29:56 +00:00
}
}
static void partialvis_update_bmesh_faces(GSet *faces)
{
GSetIterator gs_iter;
GSET_ITER (gs_iter, faces) {
BMFace *f = BLI_gsetIterator_getKey(&gs_iter);
2019-04-22 09:19:45 +10:00
if (paint_is_bmesh_face_hidden(f)) {
BM_elem_flag_enable(f, BM_ELEM_HIDDEN);
2019-04-22 09:19:45 +10:00
}
else {
BM_elem_flag_disable(f, BM_ELEM_HIDDEN);
2019-04-22 09:19:45 +10:00
}
}
}
2018-07-02 18:45:26 +02:00
static void partialvis_update_bmesh(Object *ob,
PBVH *pbvh,
PBVHNode *node,
PartialVisAction action,
PartialVisArea area,
float planes[4][4])
2012-12-30 18:29:56 +00:00
{
BMesh *bm;
GSet *unique, *other, *faces;
bool any_changed = false, any_visible = false;
bm = BKE_pbvh_get_bmesh(pbvh);
unique = BKE_pbvh_bmesh_node_unique_verts(node);
other = BKE_pbvh_bmesh_node_other_verts(node);
faces = BKE_pbvh_bmesh_node_faces(node);
SCULPT_undo_push_node(ob, node, SCULPT_UNDO_HIDDEN);
2018-07-02 18:45:26 +02:00
partialvis_update_bmesh_verts(bm, unique, action, area, planes, &any_changed, &any_visible);
2018-07-02 18:45:26 +02:00
partialvis_update_bmesh_verts(bm, other, action, area, planes, &any_changed, &any_visible);
/* Finally loop over node faces and tag the ones that are fully hidden. */
partialvis_update_bmesh_faces(faces);
2012-12-30 18:29:56 +00:00
if (any_changed) {
BKE_pbvh_node_mark_rebuild_draw(node);
BKE_pbvh_node_fully_hidden_set(node, !any_visible);
2012-12-30 18:29:56 +00:00
}
}
static void rect_from_props(rcti *rect, PointerRNA *ptr)
{
rect->xmin = RNA_int_get(ptr, "xmin");
rect->ymin = RNA_int_get(ptr, "ymin");
rect->xmax = RNA_int_get(ptr, "xmax");
rect->ymax = RNA_int_get(ptr, "ymax");
}
static void clip_planes_from_rect(bContext *C,
Depsgraph *depsgraph,
float clip_planes[4][4],
const rcti *rect)
{
ViewContext vc;
BoundBox bb;
view3d_operator_needs_opengl(C);
ED_view3d_viewcontext_init(C, &vc, depsgraph);
ED_view3d_clipping_calc(&bb, clip_planes, vc.region, vc.obact, rect);
}
/* If mode is inside, get all PBVH nodes that lie at least partially
2012-04-22 11:54:53 +00:00
* inside the clip_planes volume. If mode is outside, get all nodes
* that lie at least partially outside the volume. If showing all, get
* all nodes. */
2018-07-02 18:45:26 +02:00
static void get_pbvh_nodes(
PBVH *pbvh, PBVHNode ***nodes, int *totnode, float clip_planes[4][4], PartialVisArea mode)
{
BKE_pbvh_SearchCallback cb = NULL;
/* Select search callback. */
switch (mode) {
case PARTIALVIS_INSIDE:
cb = BKE_pbvh_node_frustum_contain_AABB;
break;
case PARTIALVIS_OUTSIDE:
cb = BKE_pbvh_node_frustum_exclude_AABB;
break;
case PARTIALVIS_ALL:
case PARTIALVIS_MASKED:
break;
}
PBVHFrustumPlanes frustum = {.planes = clip_planes, .num_planes = 4};
BKE_pbvh_search_gather(pbvh, cb, &frustum, nodes, totnode);
}
static int hide_show_exec(bContext *C, wmOperator *op)
{
ARegion *region = CTX_wm_region(C);
Object *ob = CTX_data_active_object(C);
Refactor access to dependency graph This change ensures that operators which needs access to evaluated data first makes sure there is a dependency graph. Other accesses to the dependency graph made it more explicit about whether they just need a valid dependency graph pointer or whether they expect the graph to be already evaluated. This replaces OPTYPE_USE_EVAL_DATA which is now removed. Some general rules about usage of accessors: - Drawing is expected to happen from a fully evaluated dependency graph. There is now a function to access it, which will in the future control that dependency graph is actually evaluated. This check is not yet done because there are some things to be taken care about first: for example, post-update hooks might leave scene in a state where something is still tagged for update. - All operators which needs to access evaluated state must use CTX_data_ensure_evaluated_depsgraph(). This function replaces OPTYPE_USE_EVAL_DATA. The call is generally to be done in the very beginning of the operator, prior other logic (unless this is some comprehensive operator which might or might not need access to an evaluated state). This call is never to be used from a loop. If some utility function requires evaluated state of dependency graph the graph is to be passed as an explicit argument. This way it is clear that no evaluation happens in a loop or something like this. - All cases which needs to know dependency graph pointer, but which doesn't want to actually evaluate it can use old-style function CTX_data_depsgraph_pointer(), assuming that underlying code will ensure dependency graph is evaluated prior to accessing it. - The new functions are replacing OPTYPE_USE_EVAL_DATA, so now it is explicit and local about where dependency graph is being ensured. This commit also contains some fixes of wrong usage of evaluation functions on original objects. Ideally should be split out, but in reality with all the APIs being renamed is quite tricky. Fixes T67454: Blender crash on rapid undo and select Speculation here is that sometimes undo and selection operators are sometimes handled in the same event loop iteration, which leaves non-evaluated dependency graph. Fixes T67973: Crash on Fix Deforms operator Fixes T67902: Crash when undo a loop cut Reviewers: brecht Reviewed By: brecht Subscribers: lichtwerk Maniphest Tasks: T67454 Differential Revision: https://developer.blender.org/D5343
2019-07-25 16:36:22 +02:00
Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
Mesh *me = ob->data;
PartialVisAction action;
PartialVisArea area;
PBVH *pbvh;
PBVHNode **nodes;
PBVHType pbvh_type;
float clip_planes[4][4];
rcti rect;
int totnode;
/* Read operator properties. */
action = RNA_enum_get(op->ptr, "action");
area = RNA_enum_get(op->ptr, "area");
rect_from_props(&rect, op->ptr);
clip_planes_from_rect(C, depsgraph, clip_planes, &rect);
pbvh = BKE_sculpt_object_pbvh_ensure(depsgraph, ob);
BLI_assert(ob->sculpt->pbvh == pbvh);
get_pbvh_nodes(pbvh, &nodes, &totnode, clip_planes, area);
pbvh_type = BKE_pbvh_type(pbvh);
negate_m4(clip_planes);
/* Start undo. */
switch (action) {
case PARTIALVIS_HIDE:
SCULPT_undo_push_begin(ob, "Hide area");
break;
case PARTIALVIS_SHOW:
SCULPT_undo_push_begin(ob, "Show area");
break;
}
for (int i = 0; i < totnode; i++) {
switch (pbvh_type) {
case PBVH_FACES:
partialvis_update_mesh(ob, pbvh, nodes[i], action, area, clip_planes);
break;
case PBVH_GRIDS:
partialvis_update_grids(depsgraph, ob, pbvh, nodes[i], action, area, clip_planes);
break;
2012-12-30 18:29:56 +00:00
case PBVH_BMESH:
partialvis_update_bmesh(ob, pbvh, nodes[i], action, area, clip_planes);
break;
}
}
2019-04-22 09:19:45 +10:00
if (nodes) {
MEM_freeN(nodes);
2019-04-22 09:19:45 +10:00
}
/* End undo. */
SCULPT_undo_push_end();
/* Ensure that edges and faces get hidden as well (not used by
* sculpt but it looks wrong when entering editmode otherwise). */
if (pbvh_type == PBVH_FACES) {
BKE_mesh_flush_hidden_from_verts(me);
}
SCULPT_visibility_sync_all_vertex_to_face_sets(ob->sculpt);
DEG_id_tag_update(&ob->id, ID_RECALC_SHADING);
ED_region_tag_redraw(region);
return OPERATOR_FINISHED;
}
static int hide_show_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
PartialVisArea area = RNA_enum_get(op->ptr, "area");
2019-04-22 09:19:45 +10:00
if (!ELEM(area, PARTIALVIS_ALL, PARTIALVIS_MASKED)) {
return WM_gesture_box_invoke(C, op, event);
2019-04-22 09:19:45 +10:00
}
return op->type->exec(C, op);
}
void PAINT_OT_hide_show(struct wmOperatorType *ot)
{
static const EnumPropertyItem action_items[] = {
{PARTIALVIS_HIDE, "HIDE", 0, "Hide", "Hide vertices"},
{PARTIALVIS_SHOW, "SHOW", 0, "Show", "Show vertices"},
{0, NULL, 0, NULL, NULL},
};
static const EnumPropertyItem area_items[] = {
{PARTIALVIS_OUTSIDE, "OUTSIDE", 0, "Outside", "Hide or show vertices outside the selection"},
{PARTIALVIS_INSIDE, "INSIDE", 0, "Inside", "Hide or show vertices inside the selection"},
{PARTIALVIS_ALL, "ALL", 0, "All", "Hide or show all vertices"},
{PARTIALVIS_MASKED,
"MASKED",
0,
"Masked",
"Hide or show vertices that are masked (minimum mask value of 0.5)"},
{0, NULL, 0, NULL, NULL},
};
/* Identifiers. */
ot->name = "Hide/Show";
ot->idname = "PAINT_OT_hide_show";
ot->description = "Hide/show some vertices";
/* API callbacks. */
ot->invoke = hide_show_invoke;
ot->modal = WM_gesture_box_modal;
ot->exec = hide_show_exec;
/* Sculpt-only for now. */
ot->poll = SCULPT_mode_poll_view3d;
ot->flag = OPTYPE_REGISTER;
/* RNA. */
RNA_def_enum(ot->srna,
"action",
action_items,
PARTIALVIS_HIDE,
"Action",
"Whether to hide or show vertices");
RNA_def_enum(
ot->srna, "area", area_items, PARTIALVIS_INSIDE, "Area", "Which vertices to hide or show");
WM_operator_properties_border(ot);
}