Files
test2/source/blender/modifiers/intern/MOD_edgesplit.cc

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

197 lines
5.6 KiB
C++
Raw Normal View History

/* SPDX-FileCopyrightText: 2005 Blender Authors
*
* SPDX-License-Identifier: GPL-2.0-or-later */
/** \file
* \ingroup modifiers
*
2021-10-24 19:31:08 +11:00
* Edge Split modifier
*
* Splits edges in the mesh according to sharpness flag
2021-10-24 19:31:08 +11:00
* or edge angle (can be used to achieve auto-smoothing)
*/
#include "BLI_math_vector.h"
#include "BLI_utildefines.h"
#include "BLT_translation.h"
#include "DNA_defaults.h"
#include "DNA_mesh_types.h"
#include "DNA_object_types.h"
#include "DNA_screen_types.h"
#include "BKE_context.hh"
#include "BKE_mesh.hh"
2023-11-14 09:30:40 +01:00
#include "BKE_modifier.hh"
#include "BKE_screen.hh"
#include "UI_interface.hh"
#include "UI_resources.hh"
#include "RNA_access.hh"
#include "RNA_prototypes.h"
#include "bmesh.hh"
#include "bmesh_tools.hh"
#include "MOD_modifiertypes.hh"
#include "MOD_ui_common.hh"
Geometry: add utility to check for bad geometry element index dependence Sometimes .blend files have compatibility issues between Blender versions, because .blend files depended on the specific order of geometry elements generated by some nodes/modifiers (#112746, #113018). While we make guarantees about the order in some places, that is relatively rare, because it makes future improvements much harder. The functionality in this patch makes it easier for users to notice when they depend on things that are not expected to be stable between Blender builds. This is achieved by adding a new global flag which indicates whether some algorithms should randomize their output. The functionality can be toggled on or off by searching for `Set Geometry Randomization`. If there are no differences (or acceptable minor ones) when the flag is on or off, one can be reasonably sure that one does not on unspecified behavior (can't be 100% sure though, because randomization might be missing in some places). If there are big differences, one should consider fixing the file before it comes to an actual breakage in the next Blender version. Currently, the setting is only available when `Developer Extras` is turned on, because the setting is in no menu. With this patch, if we get bug reports with compatibility issues caused by depending on indices, one of the following three cases should always apply: * We actually accidentally broke something, which requires a fix commit. * Turning on geometry randomization shows that the .blend file depends on things it shouldn't depend on. In this case the user has to fix the file. * We are missing geometry randomization somewhere, which requires a fix commit. Pull Request: https://projects.blender.org/blender/blender/pulls/113030
2023-09-29 21:44:36 +02:00
#include "GEO_randomize.hh"
Geometry Nodes: initial scattering and geometry nodes This is the initial merge from the geometry-nodes branch. Nodes: * Attribute Math * Boolean * Edge Split * Float Compare * Object Info * Point Distribute * Point Instance * Random Attribute * Random Float * Subdivision Surface * Transform * Triangulate It includes the initial evaluation of geometry node groups in the Geometry Nodes modifier. Notes on the Generic attribute access API The API adds an indirection for attribute access. That has the following benefits: * Most code does not have to care about how an attribute is stored internally. This is mainly necessary, because we have to deal with "legacy" attributes such as vertex weights and attributes that are embedded into other structs such as vertex positions. * When reading from an attribute, we generally don't care what domain the attribute is stored on. So we want to abstract away the interpolation that that adapts attributes from one domain to another domain (this is not actually implemented yet). Other possible improvements for later iterations include: * Actually implement interpolation between domains. * Don't use inheritance for the different attribute types. A single class for read access and one for write access might be enough, because we know all the ways in which attributes are stored internally. We don't want more different internal structures in the future. On the contrary, ideally we can consolidate the different storage formats in the future to reduce the need for this indirection. * Remove the need for heap allocations when creating attribute accessors. It includes commits from: * Dalai Felinto * Hans Goudey * Jacques Lucke * Léo Depoix
2020-12-02 13:25:25 +01:00
/* For edge split modifier node. */
Mesh *doEdgeSplit(const Mesh *mesh, EdgeSplitModifierData *emd);
Mesh *doEdgeSplit(const Mesh *mesh, EdgeSplitModifierData *emd)
{
Mesh *result;
BMesh *bm;
BMIter iter;
BMEdge *e;
const float threshold = cosf(emd->split_angle + 0.000000175f);
const bool do_split_angle = (emd->flags & MOD_EDGESPLIT_FROMANGLE) != 0 &&
emd->split_angle < float(M_PI);
const bool do_split_all = do_split_angle && emd->split_angle < FLT_EPSILON;
const bool calc_face_normals = do_split_angle && !do_split_all;
BMeshCreateParams create_params{};
BMeshFromMeshParams convert_params{};
convert_params.calc_face_normal = calc_face_normals;
convert_params.calc_vert_normal = false;
convert_params.add_key_index = false;
convert_params.use_shapekey = false;
convert_params.active_shapekey = 0;
convert_params.cd_mask_extra.vmask = CD_MASK_ORIGINDEX;
convert_params.cd_mask_extra.emask = CD_MASK_ORIGINDEX;
convert_params.cd_mask_extra.pmask = CD_MASK_ORIGINDEX;
bm = BKE_mesh_to_bmesh_ex(mesh, &create_params, &convert_params);
if (do_split_angle) {
BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
/* check for 1 edge having 2 face users */
BMLoop *l1, *l2;
2012-05-06 13:38:33 +00:00
if ((l1 = e->l) && (l2 = e->l->radial_next) != l1) {
if (/* 3+ faces on this edge, always split */
UNLIKELY(l1 != l2->radial_next) ||
/* O degree angle setting, we want to split on all edges. */
do_split_all ||
/* 2 face edge - check angle. */
(dot_v3v3(l1->f->no, l2->f->no) < threshold))
{
BM_elem_flag_enable(e, BM_ELEM_TAG);
}
}
}
}
2011-06-05 00:54:14 +00:00
if (emd->flags & MOD_EDGESPLIT_FROMFLAG) {
BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
/* check for 2 or more edge users */
if ((e->l) && (e->l->next != e->l)) {
if (!BM_elem_flag_test(e, BM_ELEM_SMOOTH)) {
BM_elem_flag_enable(e, BM_ELEM_TAG);
}
}
}
2011-06-05 00:54:14 +00:00
}
BM_mesh_edgesplit(bm, false, true, false);
/* Uncomment for troubleshooting. */
// BM_mesh_validate(bm);
result = BKE_mesh_from_bmesh_for_eval_nomain(bm, nullptr, mesh);
BM_mesh_free(bm);
Geometry: add utility to check for bad geometry element index dependence Sometimes .blend files have compatibility issues between Blender versions, because .blend files depended on the specific order of geometry elements generated by some nodes/modifiers (#112746, #113018). While we make guarantees about the order in some places, that is relatively rare, because it makes future improvements much harder. The functionality in this patch makes it easier for users to notice when they depend on things that are not expected to be stable between Blender builds. This is achieved by adding a new global flag which indicates whether some algorithms should randomize their output. The functionality can be toggled on or off by searching for `Set Geometry Randomization`. If there are no differences (or acceptable minor ones) when the flag is on or off, one can be reasonably sure that one does not on unspecified behavior (can't be 100% sure though, because randomization might be missing in some places). If there are big differences, one should consider fixing the file before it comes to an actual breakage in the next Blender version. Currently, the setting is only available when `Developer Extras` is turned on, because the setting is in no menu. With this patch, if we get bug reports with compatibility issues caused by depending on indices, one of the following three cases should always apply: * We actually accidentally broke something, which requires a fix commit. * Turning on geometry randomization shows that the .blend file depends on things it shouldn't depend on. In this case the user has to fix the file. * We are missing geometry randomization somewhere, which requires a fix commit. Pull Request: https://projects.blender.org/blender/blender/pulls/113030
2023-09-29 21:44:36 +02:00
blender::geometry::debug_randomize_mesh_order(result);
return result;
}
static void init_data(ModifierData *md)
{
2012-05-06 13:38:33 +00:00
EdgeSplitModifierData *emd = (EdgeSplitModifierData *)md;
BLI_assert(MEMCMP_STRUCT_AFTER_IS_ZERO(emd, modifier));
MEMCPY_STRUCT_AFTER(emd, DNA_struct_default_get(EdgeSplitModifierData), modifier);
}
static Mesh *modify_mesh(ModifierData *md, const ModifierEvalContext * /*ctx*/, Mesh *mesh)
{
Mesh *result;
2012-05-06 13:38:33 +00:00
EdgeSplitModifierData *emd = (EdgeSplitModifierData *)md;
if (!(emd->flags & (MOD_EDGESPLIT_FROMANGLE | MOD_EDGESPLIT_FROMFLAG))) {
return mesh;
}
2019-01-30 11:02:06 +01:00
result = doEdgeSplit(mesh, emd);
return result;
}
static void panel_draw(const bContext * /*C*/, Panel *panel)
{
uiLayout *row, *sub;
uiLayout *layout = panel->layout;
PointerRNA *ptr = modifier_panel_get_property_pointers(panel, nullptr);
uiLayoutSetPropSep(layout, true);
row = uiLayoutRowWithHeading(layout, true, IFACE_("Edge Angle"));
uiItemR(row, ptr, "use_edge_angle", UI_ITEM_NONE, "", ICON_NONE);
sub = uiLayoutRow(row, true);
uiLayoutSetActive(sub, RNA_boolean_get(ptr, "use_edge_angle"));
uiItemR(sub, ptr, "split_angle", UI_ITEM_NONE, "", ICON_NONE);
uiItemR(layout, ptr, "use_edge_sharp", UI_ITEM_NONE, IFACE_("Sharp Edges"), ICON_NONE);
modifier_panel_end(layout, ptr);
}
static void panel_register(ARegionType *region_type)
{
modifier_panel_register(region_type, eModifierType_EdgeSplit, panel_draw);
}
ModifierTypeInfo modifierType_EdgeSplit = {
/*idname*/ "EdgeSplit",
/*name*/ N_("EdgeSplit"),
/*struct_name*/ "EdgeSplitModifierData",
/*struct_size*/ sizeof(EdgeSplitModifierData),
/*srna*/ &RNA_EdgeSplitModifier,
/*type*/ ModifierTypeType::Constructive,
/*flags*/ eModifierTypeFlag_AcceptsMesh | eModifierTypeFlag_AcceptsCVs |
2012-05-06 13:38:33 +00:00
eModifierTypeFlag_SupportsMapping | eModifierTypeFlag_SupportsEditmode |
eModifierTypeFlag_EnableInEditmode,
/*icon*/ ICON_MOD_EDGESPLIT,
/*copy_data*/ BKE_modifier_copydata_generic,
/*deform_verts*/ nullptr,
/*deform_matrices*/ nullptr,
/*deform_verts_EM*/ nullptr,
/*deform_matrices_EM*/ nullptr,
/*modify_mesh*/ modify_mesh,
/*modify_geometry_set*/ nullptr,
/*init_data*/ init_data,
/*required_data_mask*/ nullptr,
/*free_data*/ nullptr,
/*is_disabled*/ nullptr,
/*update_depsgraph*/ nullptr,
/*depends_on_time*/ nullptr,
/*depends_on_normals*/ nullptr,
/*foreach_ID_link*/ nullptr,
/*foreach_tex_link*/ nullptr,
/*free_runtime_data*/ nullptr,
/*panel_register*/ panel_register,
/*blend_write*/ nullptr,
/*blend_read*/ nullptr,
/*foreach_cache*/ nullptr,
};