Refactor: Mesh Merge: deduplicate code, no interpolation for edges #109836

Merged
Germano Cavalcante merged 1 commits from mano-wii/blender:weld_improvements into main 2023-07-10 14:55:46 +02:00

With 74772c6920, the merge by distance code for meshes now supports
optional interpolation of custom data for vertices.

As this can be advantageous for performance and memory, it seems
convenient to support the same for edges (and in the future polygons
and corners).

So this commit reworks the code that finds the edge groups to match the
same used for vertices and thus deduplicating and simplifying the code.

Conveniently the edge_ctx_map array is no longer needed and can be
removed to save memory.

However it was necessary to create another array (which is usually
smaller) called double_edges (to match double_verts).


Results:

The profiling result depends on some factors such as:

  • interpolate or not interpolate customdata;
  • affect a large or small portion of the mesh;

The best case is when the customdata is not interpolated and the operation affects large portions of the mesh:

Before: After: Factor:
1256.61 ms 1073.218 ms -14.44%

The worst case is when the customdata is not interpolated and the operation affects small portions of the mesh:

Before: After: Factor:
1077.50 ms 1086.7 ms +0.85%

Other cases:

Customdata is interpolated and the operation affects large portions of the mesh:

Before: After: Factor:
241.99 ms 217.40 ms -10.14%

Customdata is interpolated and the operation affects small portions of the mesh:

Before: After: Factor:
671.165 ms 659.575 ms -1.725%
With 74772c6920, the merge by distance code for meshes now supports optional interpolation of custom data for vertices. As this can be advantageous for performance and memory, it seems convenient to support the same for edges (and in the future polygons and corners). So this commit reworks the code that finds the edge groups to match the same used for vertices and thus deduplicating and simplifying the code. Conveniently the `edge_ctx_map` array is no longer needed and can be removed to save memory. However it was necessary to create another array (which is usually smaller) called `double_edges` (to match `double_verts`). --- **Results:** The profiling result depends on some factors such as: - interpolate or not interpolate customdata; - affect a large or small portion of the mesh; The best case is when the customdata **is not** interpolated and the operation affects **large** portions of the mesh: | Before: | After: | Factor: | | ---------- | ---------- | ------- | | 1256.61 ms | 1073.218 ms| -14.44% | The worst case is when the customdata **is not** interpolated and the operation affects **small** portions of the mesh: | Before: | After: | Factor: | | --------- | --------- | ------- | | 1077.50 ms| 1086.7 ms| +0.85% | **Other cases:** Customdata **is** interpolated and the operation affects **large** portions of the mesh: | Before: | After: | Factor: | | -------- | -------- | ------- | | 241.99 ms| 217.40 ms| -10.14% | Customdata **is** interpolated and the operation affects **small** portions of the mesh: | Before: | After: | Factor: | | --------- | --------- | ------- | | 671.165 ms| 659.575 ms| -1.725% |
Germano Cavalcante requested review from Hans Goudey 2023-07-07 21:03:45 +02:00
Germano Cavalcante force-pushed weld_improvements from 539ecefe59 to 6191b04d8c 2023-07-08 19:04:46 +02:00 Compare
Germano Cavalcante force-pushed weld_improvements from 6191b04d8c to 696349bd9a 2023-07-08 19:07:58 +02:00 Compare
Germano Cavalcante merged commit 113004687d into main 2023-07-10 14:55:46 +02:00
Germano Cavalcante deleted branch weld_improvements 2023-07-10 14:55:47 +02:00
Hans Goudey requested changes 2023-07-10 14:56:42 +02:00
@ -90,3 +83,1 @@
Array<int> vert_group_map; /* Maps the vertex group offset from the target vert index. */
Array<int> vert_groups_offs;
Array<int> vert_groups_buffer;
Vector<int> double_verts;
Member

Could you add a comment explaining what "double" means in this context, and what the indices refer to?

Could you add a comment explaining what "double" means in this context, and what the indices refer to?
mano-wii marked this conversation as resolved
@ -164,3 +149,3 @@
}
}
BLI_assert(kills == supposed_kill_len);
BLI_assert(kills == expected_kill_len);
Member

Probably needs a UNUSED_VARS_NDEBUG

Probably needs a `UNUSED_VARS_NDEBUG`
mano-wii marked this conversation as resolved
@ -446,3 +363,1 @@
if (we.vert_a == we.vert_b) {
we.flag = ELEM_COLLAPSED;
const int vert_a = (v_dest_1 != OUT_OF_CONTEXT) ? v_dest_1 : v1;
Member

Can remove the double negative by switching v_dest_1 and changing != to ==

Can remove the double negative by switching `v_dest_1` and changing `!=` to `==`
@ -478,2 +389,3 @@
* \return r_edge_double_kill_len: Number of edges to be destroyed by merging or collapsing.
*/
static void weld_edge_find_doubles(int remain_edge_ctx_len,
static void weld_edge_find_doubles(Span<WeldEdge> wedge,
Member

Suggest weld_edges as a variable name.

  • There's no real need to use an abbreviation here, the name isn't that long
  • Using plural helps to make it clear further that it's many items, not just one
Suggest `weld_edges` as a variable name. - There's no real need to use an abbreviation here, the name isn't that long - Using plural helps to make it clear further that it's many items, not just one
@ -620,3 +475,1 @@
r_edge_groups_verts[wgroups_len] = {we->vert_a, we->vert_b};
r_edge_groups_map[i] = wgroups_len;
wgroups_len++;
const WeldEdge &we_b = wedge[e_ctx_b];
Member

Fully support changing pointers to references where possible, but the change makes the diff noisy, and makes it harder to see the larger changes. Maybe this could be committed separately?

Fully support changing pointers to references where possible, but the change makes the diff noisy, and makes it harder to see the larger changes. Maybe this could be committed separately?
@ -1449,0 +1274,4 @@
* \param double_elems: Affected elements in `dest_map` that will compose groups.
* Used for performance only.
*
* \return r_groups_map: Map that points out the group of elements that an element belongs to.
Member

Probably worth specifying "source" or "result" whenever you use the word "element," that's the confusing part

Probably worth specifying "source" or "result" whenever you use the word "element," that's the confusing part
@ -1449,0 +1282,4 @@
Span<int> double_elems,
Array<int> &r_groups_map,
Array<int> &r_groups_buffer,
Array<int> &r_groups_offs)
Member

r_groups_offs -> r_groups_offets

The extra two letters really help :P

`r_groups_offs` -> `r_groups_offets` The extra two letters really help :P
@ -1449,0 +1305,4 @@
}
/* Add +1 to allow calculation of the length of the last group. */
r_groups_offs.reinitialize(wgroups_len + 1);
Member

"reinitialize and fill" can be replaced with r_group_offs = Array<int>(wgroups_len + 1, 0);
That might be a bit faster in the future actually, since Array could use calloc in that case.
But it's also just shorter.

"reinitialize and fill" can be replaced with `r_group_offs = Array<int>(wgroups_len + 1, 0);` That might be a bit faster in the future actually, since `Array` could use `calloc` in that case. But it's also just shorter.
@ -1449,0 +1308,4 @@
r_groups_offs.reinitialize(wgroups_len + 1);
r_groups_offs.fill(0);
for (const int elem_orig : double_elems) {
Member

Reading this, I'm not sure. But could this be implemented with array_utils::count_indices? Nice to standardize these patterns where possible to make them more recognizable.

Reading this, I'm not sure. But could this be implemented with `array_utils::count_indices`? Nice to standardize these patterns where possible to make them more recognizable.
@ -1449,0 +1315,4 @@
}
int offs = 0;
for (const int i : IndexRange(wgroups_len)) {
Member

Use offset_indices::accumulate_counts_to_offsets

Use `offset_indices::accumulate_counts_to_offsets`
@ -1449,0 +1324,4 @@
r_groups_buffer.reinitialize(offs);
BLI_assert(r_groups_buffer.size() == double_elems.size());
/* Use a reverse for loop to ensure that indexes are assigned in ascending order. */
Member

indexes -> indices

`indexes` -> `indices`
@ -1544,0 +1434,4 @@
Span<int> double_elems,
const int dest_size,
const bool do_mix_data,
Array<int> &r_final_map)
Member

It's confusing that the custom data merging process is also building the "final map". If those two processes could be separated, I think the whole process would be easier to understand. It's also worth simplifying custom data interpolation, since it's often a performance bottleneck, and if it's simpler it's easier to optimize.

It's confusing that the custom data merging process is also building the "final map". If those two processes could be separated, I think the whole process would be easier to understand. It's also worth simplifying custom data interpolation, since it's often a performance bottleneck, and if it's simpler it's easier to optimize.
@ -1544,0 +1476,4 @@
if (do_mix_data) {
const int grp_index = groups_map[i];
const int grp_buffer_offs = groups_offs[grp_index];
const int grp_buffer_len = groups_offs[grp_index + 1] - grp_buffer_offs;
Member

Declare an OffsetIndices variable above to avoid repeating this boilerplate

Declare an `OffsetIndices` variable above to avoid repeating this boilerplate
@ -1544,0 +1506,4 @@
}
if (finalize_map) {
for (int i : r_final_map.index_range()) {
Member

int i -> const int i

`int i` -> `const int i`
Sign in to join this conversation.
No reviewers
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#109836
No description provided.