Files
test/source/blender/modifiers/intern/MOD_remesh.cc
Hans Goudey 7966cd16d6 Mesh: Replace MPoly struct with offset indices
Implements #95967.

Currently the `MPoly` struct is 12 bytes, and stores the index of a
face's first corner and the number of corners/verts/edges. Polygons
and corners are always created in order by Blender, meaning each
face's corners will be after the previous face's corners. We can take
advantage of this fact and eliminate the redundancy in mesh face
storage by only storing a single integer corner offset for each face.
The size of the face is then encoded by the offset of the next face.
The size of a single integer is 4 bytes, so this reduces memory
usage by 3 times.

The same method is used for `CurvesGeometry`, so Blender already has
an abstraction to simplify using these offsets called `OffsetIndices`.
This class is used to easily retrieve a range of corner indices for
each face. This also gives the opportunity for sharing some logic with
curves.

Another benefit of the change is that the offsets and sizes stored in
`MPoly` can no longer disagree with each other. Storing faces in the
order of their corners can simplify some code too.

Face/polygon variables now use the `IndexRange` type, which comes with
quite a few utilities that can simplify code.

Some:
- The offset integer array has to be one longer than the face count to
  avoid a branch for every face, which means the data is no longer part
  of the mesh's `CustomData`.
- We lose the ability to "reference" an original mesh's offset array
  until more reusable CoW from #104478 is committed. That will be added
  in a separate commit.
- Since they aren't part of `CustomData`, poly offsets often have to be
  copied manually.
- To simplify using `OffsetIndices` in many places, some functions and
  structs in headers were moved to only compile in C++.
- All meshes created by Blender use the same order for faces and face
  corners, but just in case, meshes with mismatched order are fixed by
  versioning code.
- `MeshPolygon.totloop` is no longer editable in RNA. This API break is
  necessary here unfortunately. It should be worth it in 3.6, since
  that's the best way to allow loading meshes from 4.0, which is
  important for an LTS version.

Pull Request: https://projects.blender.org/blender/blender/pulls/105938
2023-04-04 20:39:28 +02:00

292 lines
8.2 KiB
C++

/* SPDX-License-Identifier: GPL-2.0-or-later
* Copyright 2011 by Nicholas Bishop. */
/** \file
* \ingroup modifiers
*/
#include "MEM_guardedalloc.h"
#include "BLI_math_base.h"
#include "BLI_threads.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_modifier_types.h"
#include "DNA_object_types.h"
#include "DNA_screen_types.h"
#include "BKE_context.h"
#include "BKE_mesh.hh"
#include "BKE_mesh_remesh_voxel.h"
#include "BKE_mesh_runtime.h"
#include "BKE_screen.h"
#include "UI_interface.h"
#include "UI_resources.h"
#include "RNA_access.h"
#include "RNA_prototypes.h"
#include "MOD_modifiertypes.h"
#include "MOD_ui_common.h"
#include <cstdlib>
#include <cstring>
#ifdef WITH_MOD_REMESH
# include "BLI_math_vector.h"
# include "dualcon.h"
#endif
static void initData(ModifierData *md)
{
RemeshModifierData *rmd = (RemeshModifierData *)md;
BLI_assert(MEMCMP_STRUCT_AFTER_IS_ZERO(rmd, modifier));
MEMCPY_STRUCT_AFTER(rmd, DNA_struct_default_get(RemeshModifierData), modifier);
}
#ifdef WITH_MOD_REMESH
static void init_dualcon_mesh(DualConInput *input, Mesh *mesh)
{
memset(input, 0, sizeof(DualConInput));
input->co = (DualConCo)BKE_mesh_vert_positions(mesh);
input->co_stride = sizeof(float[3]);
input->totco = mesh->totvert;
input->mloop = (DualConLoop)mesh->corner_verts().data();
input->loop_stride = sizeof(int);
input->looptri = (DualConTri)mesh->looptris().data();
input->tri_stride = sizeof(MLoopTri);
input->tottri = BKE_mesh_runtime_looptri_len(mesh);
INIT_MINMAX(input->min, input->max);
BKE_mesh_minmax(mesh, input->min, input->max);
}
/* simple structure to hold the output: a CDDM and two counters to
* keep track of the current elements */
typedef struct {
Mesh *mesh;
float (*vert_positions)[3];
int *poly_offsets;
int *corner_verts;
int curvert, curface;
} DualConOutput;
/* allocate and initialize a DualConOutput */
static void *dualcon_alloc_output(int totvert, int totquad)
{
DualConOutput *output;
if (!(output = MEM_cnew<DualConOutput>(__func__))) {
return nullptr;
}
output->mesh = BKE_mesh_new_nomain(totvert, 0, 4 * totquad, totquad);
output->vert_positions = BKE_mesh_vert_positions_for_write(output->mesh);
output->poly_offsets = output->mesh->poly_offsets_for_write().data();
output->corner_verts = output->mesh->corner_verts_for_write().data();
return output;
}
static void dualcon_add_vert(void *output_v, const float co[3])
{
DualConOutput *output = static_cast<DualConOutput *>(output_v);
BLI_assert(output->curvert < output->mesh->totvert);
copy_v3_v3(output->vert_positions[output->curvert], co);
output->curvert++;
}
static void dualcon_add_quad(void *output_v, const int vert_indices[4])
{
DualConOutput *output = static_cast<DualConOutput *>(output_v);
Mesh *mesh = output->mesh;
int i;
BLI_assert(output->curface < mesh->totpoly);
UNUSED_VARS_NDEBUG(mesh);
output->poly_offsets[output->curface] = output->curface * 4;
for (i = 0; i < 4; i++) {
output->corner_verts[output->curface * 4 + i] = vert_indices[i];
}
output->curface++;
}
static Mesh *modifyMesh(ModifierData *md, const ModifierEvalContext * /*ctx*/, Mesh *mesh)
{
RemeshModifierData *rmd;
DualConOutput *output;
DualConInput input;
Mesh *result;
DualConFlags flags = DualConFlags(0);
DualConMode mode = DualConMode(0);
rmd = (RemeshModifierData *)md;
if (rmd->mode == MOD_REMESH_VOXEL) {
/* OpenVDB modes. */
if (rmd->voxel_size == 0.0f) {
return nullptr;
}
result = BKE_mesh_remesh_voxel(mesh, rmd->voxel_size, rmd->adaptivity, 0.0f);
if (result == nullptr) {
return nullptr;
}
}
else {
/* Dualcon modes. */
init_dualcon_mesh(&input, mesh);
if (rmd->flag & MOD_REMESH_FLOOD_FILL) {
flags = DualConFlags(flags | DUALCON_FLOOD_FILL);
}
switch (rmd->mode) {
case MOD_REMESH_CENTROID:
mode = DUALCON_CENTROID;
break;
case MOD_REMESH_MASS_POINT:
mode = DUALCON_MASS_POINT;
break;
case MOD_REMESH_SHARP_FEATURES:
mode = DUALCON_SHARP_FEATURES;
break;
case MOD_REMESH_VOXEL:
/* Should have been processed before as an OpenVDB operation. */
BLI_assert(false);
break;
}
/* TODO(jbakker): Dualcon crashes when run in parallel. Could be related to incorrect
* input data or that the library isn't thread safe.
* This was identified when changing the task isolation's during #76553. */
static ThreadMutex dualcon_mutex = BLI_MUTEX_INITIALIZER;
BLI_mutex_lock(&dualcon_mutex);
output = static_cast<DualConOutput *>(dualcon(&input,
dualcon_alloc_output,
dualcon_add_vert,
dualcon_add_quad,
flags,
mode,
rmd->threshold,
rmd->hermite_num,
rmd->scale,
rmd->depth));
BLI_mutex_unlock(&dualcon_mutex);
output->mesh->poly_offsets_for_write().last() = output->mesh->totloop;
result = output->mesh;
MEM_freeN(output);
}
BKE_mesh_smooth_flag_set(result, rmd->flag & MOD_REMESH_SMOOTH_SHADING);
BKE_mesh_copy_parameters_for_eval(result, mesh);
BKE_mesh_calc_edges(result, true, false);
return result;
}
#else /* !WITH_MOD_REMESH */
static Mesh *modifyMesh(ModifierData * /*md*/, const ModifierEvalContext * /*ctx*/, Mesh *mesh)
{
return mesh;
}
#endif /* !WITH_MOD_REMESH */
static void panel_draw(const bContext * /*C*/, Panel *panel)
{
uiLayout *layout = panel->layout;
#ifdef WITH_MOD_REMESH
uiLayout *row, *col;
PointerRNA ob_ptr;
PointerRNA *ptr = modifier_panel_get_property_pointers(panel, &ob_ptr);
int mode = RNA_enum_get(ptr, "mode");
uiItemR(layout, ptr, "mode", UI_ITEM_R_EXPAND, nullptr, ICON_NONE);
uiLayoutSetPropSep(layout, true);
col = uiLayoutColumn(layout, false);
if (mode == MOD_REMESH_VOXEL) {
uiItemR(col, ptr, "voxel_size", 0, nullptr, ICON_NONE);
uiItemR(col, ptr, "adaptivity", 0, nullptr, ICON_NONE);
}
else {
uiItemR(col, ptr, "octree_depth", 0, nullptr, ICON_NONE);
uiItemR(col, ptr, "scale", 0, nullptr, ICON_NONE);
if (mode == MOD_REMESH_SHARP_FEATURES) {
uiItemR(col, ptr, "sharpness", 0, nullptr, ICON_NONE);
}
uiItemR(layout, ptr, "use_remove_disconnected", 0, nullptr, ICON_NONE);
row = uiLayoutRow(layout, false);
uiLayoutSetActive(row, RNA_boolean_get(ptr, "use_remove_disconnected"));
uiItemR(layout, ptr, "threshold", 0, nullptr, ICON_NONE);
}
uiItemR(layout, ptr, "use_smooth_shade", 0, nullptr, ICON_NONE);
modifier_panel_end(layout, ptr);
#else /* WITH_MOD_REMESH */
uiItemL(layout, TIP_("Built without Remesh modifier"), ICON_NONE);
#endif /* WITH_MOD_REMESH */
}
static void panelRegister(ARegionType *region_type)
{
modifier_panel_register(region_type, eModifierType_Remesh, panel_draw);
}
ModifierTypeInfo modifierType_Remesh = {
/*name*/ N_("Remesh"),
/*structName*/ "RemeshModifierData",
/*structSize*/ sizeof(RemeshModifierData),
/*srna*/ &RNA_RemeshModifier,
/*type*/ eModifierTypeType_Nonconstructive,
/*flags*/ eModifierTypeFlag_AcceptsMesh | eModifierTypeFlag_AcceptsCVs |
eModifierTypeFlag_SupportsEditmode,
/*icon*/ ICON_MOD_REMESH,
/*copyData*/ BKE_modifier_copydata_generic,
/*deformVerts*/ nullptr,
/*deformMatrices*/ nullptr,
/*deformVertsEM*/ nullptr,
/*deformMatricesEM*/ nullptr,
/*modifyMesh*/ modifyMesh,
/*modifyGeometrySet*/ nullptr,
/*initData*/ initData,
/*requiredDataMask*/ nullptr,
/*freeData*/ nullptr,
/*isDisabled*/ nullptr,
/*updateDepsgraph*/ nullptr,
/*dependsOnTime*/ nullptr,
/*dependsOnNormals*/ nullptr,
/*foreachIDLink*/ nullptr,
/*foreachTexLink*/ nullptr,
/*freeRuntimeData*/ nullptr,
/*panelRegister*/ panelRegister,
/*blendWrite*/ nullptr,
/*blendRead*/ nullptr,
};