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

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

231 lines
6.3 KiB
C++
Raw Normal View History

/* SPDX-FileCopyrightText: 2005 Blender Authors
*
* SPDX-License-Identifier: GPL-2.0-or-later */
/** \file
* \ingroup modifiers
*/
#include "BLI_math_matrix.h"
#include "BLI_math_vector.h"
#include "BLI_utildefines.h"
#include "BLT_translation.hh"
#include "DNA_defaults.h"
#include "DNA_mesh_types.h"
#include "DNA_object_types.h"
#include "DNA_screen_types.h"
#include "BKE_bvhutils.hh"
2024-01-15 12:44:04 -05:00
#include "BKE_lib_id.hh"
#include "BKE_mesh.hh"
#include "UI_interface.hh"
#include "UI_resources.hh"
#include "RNA_prototypes.hh"
#include "DEG_depsgraph.hh"
#include "DEG_depsgraph_query.hh"
#include "MOD_modifiertypes.hh"
#include "MOD_ui_common.hh"
#include "MEM_guardedalloc.h"
static void init_data(ModifierData *md)
{
2012-05-06 13:38:33 +00:00
SurfaceModifierData *surmd = (SurfaceModifierData *)md;
2018-06-17 17:04:27 +02:00
BLI_assert(MEMCMP_STRUCT_AFTER_IS_ZERO(surmd, modifier));
MEMCPY_STRUCT_AFTER(surmd, DNA_struct_default_get(SurfaceModifierData), modifier);
}
static void copy_data(const ModifierData *md_src, ModifierData *md_dst, const int flag)
{
SurfaceModifierData *surmd_dst = (SurfaceModifierData *)md_dst;
BKE_modifier_copydata_generic(md_src, md_dst, flag);
memset(&surmd_dst->runtime, 0, sizeof(surmd_dst->runtime));
}
static void free_data(ModifierData *md)
{
2012-05-06 13:38:33 +00:00
SurfaceModifierData *surmd = (SurfaceModifierData *)md;
if (surmd) {
if (surmd->runtime.bvhtree) {
free_bvhtree_from_mesh(surmd->runtime.bvhtree);
MEM_SAFE_FREE(surmd->runtime.bvhtree);
}
if (surmd->runtime.mesh) {
BKE_id_free(nullptr, surmd->runtime.mesh);
surmd->runtime.mesh = nullptr;
}
MEM_SAFE_FREE(surmd->runtime.vert_positions_prev);
2018-06-17 17:04:27 +02:00
MEM_SAFE_FREE(surmd->runtime.vert_velocities);
}
}
static bool depends_on_time(Scene * /*scene*/, ModifierData * /*md*/)
{
return true;
}
static void deform_verts(ModifierData *md,
const ModifierEvalContext *ctx,
Mesh *mesh,
blender::MutableSpan<blender::float3> positions)
{
2012-05-06 13:38:33 +00:00
SurfaceModifierData *surmd = (SurfaceModifierData *)md;
const int cfra = int(DEG_get_ctime(ctx->depsgraph));
/* Free mesh and BVH cache. */
if (surmd->runtime.bvhtree) {
free_bvhtree_from_mesh(surmd->runtime.bvhtree);
MEM_SAFE_FREE(surmd->runtime.bvhtree);
}
if (surmd->runtime.mesh) {
BKE_id_free(nullptr, surmd->runtime.mesh);
surmd->runtime.mesh = nullptr;
}
if (mesh) {
surmd->runtime.mesh = BKE_mesh_copy_for_eval(*mesh);
}
if (!ctx->object->pd) {
printf("SurfaceModifier deform_verts: Should not happen!\n");
return;
}
if (surmd->runtime.mesh) {
uint mesh_verts_num = 0, i = 0;
int init = 0;
surmd->runtime.mesh->vert_positions_for_write().copy_from(positions);
surmd->runtime.mesh->tag_positions_changed();
mesh_verts_num = surmd->runtime.mesh->verts_num;
if ((mesh_verts_num != surmd->runtime.verts_num) ||
(surmd->runtime.vert_positions_prev == nullptr) ||
(surmd->runtime.vert_velocities == nullptr) || (cfra != surmd->runtime.cfra_prev + 1))
{
MEM_SAFE_FREE(surmd->runtime.vert_positions_prev);
MEM_SAFE_FREE(surmd->runtime.vert_velocities);
surmd->runtime.vert_positions_prev = static_cast<float(*)[3]>(
MEM_calloc_arrayN(mesh_verts_num, sizeof(float[3]), __func__));
surmd->runtime.vert_velocities = static_cast<float(*)[3]>(
MEM_calloc_arrayN(mesh_verts_num, sizeof(float[3]), __func__));
surmd->runtime.verts_num = mesh_verts_num;
init = 1;
}
/* convert to global coordinates and calculate velocity */
blender::MutableSpan<blender::float3> positions =
surmd->runtime.mesh->vert_positions_for_write();
Mesh: Move positions to a generic attribute **Changes** As described in T93602, this patch removes all use of the `MVert` struct, replacing it with a generic named attribute with the name `"position"`, consistent with other geometry types. Variable names have been changed from `verts` to `positions`, to align with the attribute name and the more generic design (positions are not vertices, they are just an attribute stored on the point domain). This change is made possible by previous commits that moved all other data out of `MVert` to runtime data or other generic attributes. What remains is mostly a simple type change. Though, the type still shows up 859 times, so the patch is quite large. One compromise is that now `CD_MASK_BAREMESH` now contains `CD_PROP_FLOAT3`. With the general move towards generic attributes over custom data types, we are removing use of these type masks anyway. **Benefits** The most obvious benefit is reduced memory usage and the benefits that brings in memory-bound situations. `float3` is only 3 bytes, in comparison to `MVert` which was 4. When there are millions of vertices this starts to matter more. The other benefits come from using a more generic type. Instead of writing algorithms specifically for `MVert`, code can just use arrays of vectors. This will allow eliminating many temporary arrays or wrappers used to extract positions. Many possible improvements aren't implemented in this patch, though I did switch simplify or remove the process of creating temporary position arrays in a few places. The design clarity that "positions are just another attribute" brings allows removing explicit copying of vertices in some procedural operations-- they are just processed like most other attributes. **Performance** This touches so many areas that it's hard to benchmark exhaustively, but I observed some areas as examples. * The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster. * The Spring splash screen went from ~4.3 to ~4.5 fps. * The subdivision surface modifier/node was slightly faster RNA access through Python may be slightly slower, since now we need a name lookup instead of just a custom data type lookup for each index. **Future Improvements** * Remove uses of "vert_coords" functions: * `BKE_mesh_vert_coords_alloc` * `BKE_mesh_vert_coords_get` * `BKE_mesh_vert_coords_apply{_with_mat4}` * Remove more hidden copying of positions * General simplification now possible in many areas * Convert more code to C++ to use `float3` instead of `float[3]` * Currently `reinterpret_cast` is used for those C-API functions Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
for (i = 0; i < mesh_verts_num; i++) {
float *vec = positions[i];
mul_m4_v3(ctx->object->object_to_world().ptr(), vec);
if (init) {
zero_v3(surmd->runtime.vert_velocities[i]);
}
else {
sub_v3_v3v3(surmd->runtime.vert_velocities[i], vec, surmd->runtime.vert_positions_prev[i]);
}
copy_v3_v3(surmd->runtime.vert_positions_prev[i], vec);
}
surmd->runtime.cfra_prev = cfra;
const bool has_face = surmd->runtime.mesh->faces_num > 0;
const bool has_edge = surmd->runtime.mesh->edges_num > 0;
if (has_face || has_edge) {
surmd->runtime.bvhtree = static_cast<BVHTreeFromMesh *>(
MEM_callocN(sizeof(BVHTreeFromMesh), __func__));
if (has_face) {
BKE_bvhtree_from_mesh_get(
surmd->runtime.bvhtree, surmd->runtime.mesh, BVHTREE_FROM_CORNER_TRIS, 2);
}
else if (has_edge) {
BKE_bvhtree_from_mesh_get(
surmd->runtime.bvhtree, surmd->runtime.mesh, BVHTREE_FROM_EDGES, 2);
}
}
}
}
static void panel_draw(const bContext * /*C*/, Panel *panel)
{
uiLayout *layout = panel->layout;
PointerRNA *ptr = modifier_panel_get_property_pointers(panel, nullptr);
uiItemL(layout, RPT_("Settings are inside the Physics tab"), ICON_NONE);
modifier_panel_end(layout, ptr);
}
static void panel_register(ARegionType *region_type)
{
modifier_panel_register(region_type, eModifierType_Surface, panel_draw);
}
static void blend_read(BlendDataReader * /*reader*/, ModifierData *md)
{
SurfaceModifierData *surmd = (SurfaceModifierData *)md;
memset(&surmd->runtime, 0, sizeof(surmd->runtime));
}
ModifierTypeInfo modifierType_Surface = {
/*idname*/ "Surface",
/*name*/ N_("Surface"),
/*struct_name*/ "SurfaceModifierData",
/*struct_size*/ sizeof(SurfaceModifierData),
/*srna*/ &RNA_SurfaceModifier,
/*type*/ ModifierTypeType::OnlyDeform,
/*flags*/ eModifierTypeFlag_AcceptsMesh | eModifierTypeFlag_AcceptsCVs |
2012-05-06 13:38:33 +00:00
eModifierTypeFlag_NoUserAdd,
/*icon*/ ICON_MOD_PHYSICS,
/*copy_data*/ copy_data,
/*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*/ nullptr,
/*free_data*/ free_data,
/*is_disabled*/ nullptr,
/*update_depsgraph*/ nullptr,
/*depends_on_time*/ depends_on_time,
/*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*/ blend_read,
/*foreach_cache*/ nullptr,
};