Files
test/source/blender/modifiers/intern/MOD_smooth.cc

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

253 lines
7.4 KiB
C++
Raw Normal View History

/* SPDX-FileCopyrightText: 2005 Blender Authors
*
* SPDX-License-Identifier: GPL-2.0-or-later */
/** \file
* \ingroup modifiers
*/
#include "MEM_guardedalloc.h"
#include "BLI_math_vector.h"
#include "BLI_utildefines.h"
#include "BLT_translation.h"
#include "DNA_defaults.h"
#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_object_types.h"
#include "DNA_screen_types.h"
#include "BKE_context.hh"
2024-01-29 18:57:16 -05:00
#include "BKE_deform.hh"
#include "BKE_editmesh.hh"
#include "BKE_lib_id.hh"
#include "BKE_mesh.hh"
#include "BKE_mesh_wrapper.hh"
#include "BKE_particle.h"
#include "BKE_screen.hh"
#include "UI_interface.hh"
#include "UI_resources.hh"
#include "RNA_access.hh"
#include "RNA_prototypes.h"
#include "MOD_modifiertypes.hh"
#include "MOD_ui_common.hh"
#include "MOD_util.hh"
static void init_data(ModifierData *md)
{
2012-05-06 13:38:33 +00:00
SmoothModifierData *smd = (SmoothModifierData *)md;
BLI_assert(MEMCMP_STRUCT_AFTER_IS_ZERO(smd, modifier));
MEMCPY_STRUCT_AFTER(smd, DNA_struct_default_get(SmoothModifierData), modifier);
}
static bool is_disabled(const Scene * /*scene*/, ModifierData *md, bool /*use_render_params*/)
{
2012-05-06 13:38:33 +00:00
SmoothModifierData *smd = (SmoothModifierData *)md;
const short flag = smd->flag & (MOD_SMOOTH_X | MOD_SMOOTH_Y | MOD_SMOOTH_Z);
/* disable if modifier is off for X, Y and Z or if factor is 0 */
if (smd->fac == 0.0f || flag == 0) {
return true;
}
return false;
}
static void required_data_mask(ModifierData *md, CustomData_MeshMasks *r_cddata_masks)
{
SmoothModifierData *smd = (SmoothModifierData *)md;
/* Ask for vertex-groups if we need them. */
if (smd->defgrp_name[0] != '\0') {
r_cddata_masks->vmask |= CD_MASK_MDEFORMVERT;
}
}
static void smoothModifier_do(
SmoothModifierData *smd, Object *ob, Mesh *mesh, float (*vertexCos)[3], int verts_num)
{
if (mesh == nullptr) {
return;
}
float(*accumulated_vecs)[3] = static_cast<float(*)[3]>(
MEM_calloc_arrayN(size_t(verts_num), sizeof(*accumulated_vecs), __func__));
if (!accumulated_vecs) {
return;
}
uint *accumulated_vecs_count = static_cast<uint *>(
MEM_calloc_arrayN(size_t(verts_num), sizeof(*accumulated_vecs_count), __func__));
if (!accumulated_vecs_count) {
MEM_freeN(accumulated_vecs);
return;
}
const float fac_new = smd->fac;
const float fac_orig = 1.0f - fac_new;
const bool invert_vgroup = (smd->flag & MOD_SMOOTH_INVERT_VGROUP) != 0;
Mesh: Move edges to a generic attribute Implements #95966, as the final step of #95965. This commit changes the storage of mesh edge vertex indices from the `MEdge` type to the generic `int2` attribute type. This follows the general design for geometry and the attribute system, where the data storage type and the usage semantics are separated. The main benefit of the change is reduced memory usage-- the requirements of storing mesh edges is reduced by 1/3. For example, this saves 8MB on a 1 million vertex grid. This also gives performance benefits to any memory-bound mesh processing algorithm that uses edges. Another benefit is that all of the edge's vertex indices are contiguous. In a few cases, it's helpful to process all of them as `Span<int>` rather than `Span<int2>`. Similarly, the type is more likely to match a generic format used by a library, or code that shouldn't know about specific Blender `Mesh` types. Various Notes: - The `.edge_verts` name is used to reflect a mapping between domains, similar to `.corner_verts`, etc. The period means that it the data shouldn't change arbitrarily by the user or procedural operations. - `edge[0]` is now used instead of `edge.v1` - Signed integers are used instead of unsigned to reduce the mixing of signed-ness, which can be error prone. - All of the previously used core mesh data types (`MVert`, `MEdge`, `MLoop`, `MPoly` are now deprecated. Only generic types are used). - The `vec2i` DNA type is used in the few C files where necessary. Pull Request: https://projects.blender.org/blender/blender/pulls/106638
2023-04-17 13:47:41 +02:00
const blender::Span<blender::int2> edges = mesh->edges();
Mesh: Remove redundant custom data pointers For copy-on-write, we want to share attribute arrays between meshes where possible. Mutable pointers like `Mesh.mvert` make that difficult by making ownership vague. They also make code more complex by adding redundancy. The simplest solution is just removing them and retrieving layers from `CustomData` as needed. Similar changes have already been applied to curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of the pointers generally makes code more obvious and more reusable. Mesh data is now accessed with a C++ API (`Mesh::edges()` or `Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`). The CoW changes this commit makes possible are described in T95845 and T95842, and started in D14139 and D14140. The change also simplifies the ongoing mesh struct-of-array refactors from T95965. **RNA/Python Access Performance** Theoretically, accessing mesh elements with the RNA API may become slower, since the layer needs to be found on every random access. However, overhead is already high enough that this doesn't make a noticible differenc, and performance is actually improved in some cases. Random access can be up to 10% faster, but other situations might be a bit slower. Generally using `foreach_get/set` are the best way to improve performance. See the differential revision for more discussion about Python performance. Cycles has been updated to use raw pointers and the internal Blender mesh types, mostly because there is no sense in having this overhead when it's already compiled with Blender. In my tests this roughly halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million face grid). Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
const MDeformVert *dvert;
int defgrp_index;
2018-06-29 19:02:19 +02:00
MOD_get_vgroup(ob, mesh, smd->defgrp_name, &dvert, &defgrp_index);
for (int j = 0; j < smd->repeat; j++) {
if (j != 0) {
memset(accumulated_vecs, 0, sizeof(*accumulated_vecs) * size_t(verts_num));
memset(accumulated_vecs_count, 0, sizeof(*accumulated_vecs_count) * size_t(verts_num));
}
2023-03-02 16:23:17 +13:00
for (const int i : edges.index_range()) {
float fvec[3];
Mesh: Move edges to a generic attribute Implements #95966, as the final step of #95965. This commit changes the storage of mesh edge vertex indices from the `MEdge` type to the generic `int2` attribute type. This follows the general design for geometry and the attribute system, where the data storage type and the usage semantics are separated. The main benefit of the change is reduced memory usage-- the requirements of storing mesh edges is reduced by 1/3. For example, this saves 8MB on a 1 million vertex grid. This also gives performance benefits to any memory-bound mesh processing algorithm that uses edges. Another benefit is that all of the edge's vertex indices are contiguous. In a few cases, it's helpful to process all of them as `Span<int>` rather than `Span<int2>`. Similarly, the type is more likely to match a generic format used by a library, or code that shouldn't know about specific Blender `Mesh` types. Various Notes: - The `.edge_verts` name is used to reflect a mapping between domains, similar to `.corner_verts`, etc. The period means that it the data shouldn't change arbitrarily by the user or procedural operations. - `edge[0]` is now used instead of `edge.v1` - Signed integers are used instead of unsigned to reduce the mixing of signed-ness, which can be error prone. - All of the previously used core mesh data types (`MVert`, `MEdge`, `MLoop`, `MPoly` are now deprecated. Only generic types are used). - The `vec2i` DNA type is used in the few C files where necessary. Pull Request: https://projects.blender.org/blender/blender/pulls/106638
2023-04-17 13:47:41 +02:00
const uint idx1 = edges[i][0];
const uint idx2 = edges[i][1];
mid_v3_v3v3(fvec, vertexCos[idx1], vertexCos[idx2]);
accumulated_vecs_count[idx1]++;
add_v3_v3(accumulated_vecs[idx1], fvec);
accumulated_vecs_count[idx2]++;
add_v3_v3(accumulated_vecs[idx2], fvec);
}
const short flag = smd->flag;
if (dvert) {
Mesh: Remove redundant custom data pointers For copy-on-write, we want to share attribute arrays between meshes where possible. Mutable pointers like `Mesh.mvert` make that difficult by making ownership vague. They also make code more complex by adding redundancy. The simplest solution is just removing them and retrieving layers from `CustomData` as needed. Similar changes have already been applied to curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of the pointers generally makes code more obvious and more reusable. Mesh data is now accessed with a C++ API (`Mesh::edges()` or `Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`). The CoW changes this commit makes possible are described in T95845 and T95842, and started in D14139 and D14140. The change also simplifies the ongoing mesh struct-of-array refactors from T95965. **RNA/Python Access Performance** Theoretically, accessing mesh elements with the RNA API may become slower, since the layer needs to be found on every random access. However, overhead is already high enough that this doesn't make a noticible differenc, and performance is actually improved in some cases. Random access can be up to 10% faster, but other situations might be a bit slower. Generally using `foreach_get/set` are the best way to improve performance. See the differential revision for more discussion about Python performance. Cycles has been updated to use raw pointers and the internal Blender mesh types, mostly because there is no sense in having this overhead when it's already compiled with Blender. In my tests this roughly halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million face grid). Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
const MDeformVert *dv = dvert;
for (int i = 0; i < verts_num; i++, dv++) {
float *vco_orig = vertexCos[i];
if (accumulated_vecs_count[i] > 0) {
mul_v3_fl(accumulated_vecs[i], 1.0f / float(accumulated_vecs_count[i]));
}
float *vco_new = accumulated_vecs[i];
const float f_vgroup = invert_vgroup ? (1.0f - BKE_defvert_find_weight(dv, defgrp_index)) :
BKE_defvert_find_weight(dv, defgrp_index);
if (f_vgroup <= 0.0f) {
continue;
}
const float f_new = f_vgroup * fac_new;
const float f_orig = 1.0f - f_new;
if (flag & MOD_SMOOTH_X) {
vco_orig[0] = f_orig * vco_orig[0] + f_new * vco_new[0];
}
if (flag & MOD_SMOOTH_Y) {
vco_orig[1] = f_orig * vco_orig[1] + f_new * vco_new[1];
}
if (flag & MOD_SMOOTH_Z) {
vco_orig[2] = f_orig * vco_orig[2] + f_new * vco_new[2];
}
}
}
else { /* no vertex group */
for (int i = 0; i < verts_num; i++) {
float *vco_orig = vertexCos[i];
if (accumulated_vecs_count[i] > 0) {
mul_v3_fl(accumulated_vecs[i], 1.0f / float(accumulated_vecs_count[i]));
}
float *vco_new = accumulated_vecs[i];
if (flag & MOD_SMOOTH_X) {
vco_orig[0] = fac_orig * vco_orig[0] + fac_new * vco_new[0];
}
if (flag & MOD_SMOOTH_Y) {
vco_orig[1] = fac_orig * vco_orig[1] + fac_new * vco_new[1];
}
if (flag & MOD_SMOOTH_Z) {
vco_orig[2] = fac_orig * vco_orig[2] + fac_new * vco_new[2];
}
}
}
}
MEM_freeN(accumulated_vecs);
MEM_freeN(accumulated_vecs_count);
}
static void deform_verts(ModifierData *md,
const ModifierEvalContext *ctx,
Mesh *mesh,
blender::MutableSpan<blender::float3> positions)
{
SmoothModifierData *smd = (SmoothModifierData *)md;
smoothModifier_do(
smd, ctx->object, mesh, reinterpret_cast<float(*)[3]>(positions.data()), positions.size());
}
static void panel_draw(const bContext * /*C*/, Panel *panel)
{
uiLayout *row, *col;
uiLayout *layout = panel->layout;
const eUI_Item_Flag toggles_flag = UI_ITEM_R_TOGGLE | UI_ITEM_R_FORCE_BLANK_DECORATE;
PointerRNA ob_ptr;
PointerRNA *ptr = modifier_panel_get_property_pointers(panel, &ob_ptr);
uiLayoutSetPropSep(layout, true);
row = uiLayoutRowWithHeading(layout, true, IFACE_("Axis"));
uiItemR(row, ptr, "use_x", toggles_flag, nullptr, ICON_NONE);
uiItemR(row, ptr, "use_y", toggles_flag, nullptr, ICON_NONE);
uiItemR(row, ptr, "use_z", toggles_flag, nullptr, ICON_NONE);
col = uiLayoutColumn(layout, false);
uiItemR(col, ptr, "factor", UI_ITEM_NONE, nullptr, ICON_NONE);
uiItemR(col, ptr, "iterations", UI_ITEM_NONE, nullptr, ICON_NONE);
modifier_vgroup_ui(layout, ptr, &ob_ptr, "vertex_group", "invert_vertex_group", nullptr);
modifier_panel_end(layout, ptr);
}
static void panel_register(ARegionType *region_type)
{
modifier_panel_register(region_type, eModifierType_Smooth, panel_draw);
}
ModifierTypeInfo modifierType_Smooth = {
/*idname*/ "Smooth",
/*name*/ N_("Smooth"),
/*struct_name*/ "SmoothModifierData",
/*struct_size*/ sizeof(SmoothModifierData),
/*srna*/ &RNA_SmoothModifier,
/*type*/ ModifierTypeType::OnlyDeform,
/*flags*/ eModifierTypeFlag_AcceptsMesh | eModifierTypeFlag_AcceptsCVs |
2012-05-06 13:38:33 +00:00
eModifierTypeFlag_SupportsEditmode,
/*icon*/ ICON_MOD_SMOOTH,
/*copy_data*/ BKE_modifier_copydata_generic,
/*deform_verts*/ deform_verts,
/*deform_matrices*/ nullptr,
/*deform_verts_EM*/ nullptr,
/*deform_matrices_EM*/ nullptr,
/*modify_mesh*/ nullptr,
/*modify_geometry_set*/ nullptr,
/*init_data*/ init_data,
/*required_data_mask*/ required_data_mask,
/*free_data*/ nullptr,
/*is_disabled*/ is_disabled,
/*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,
};