Curves: Add simplify_curve_attribute function #118560

Merged
Falk David merged 17 commits from filedescriptor/blender:curves-resample-adaptive into main 2024-03-26 15:28:22 +01:00
Member

Compute an index masks of points to remove to simplify the curve attribute using the Ramer-Douglas-Peucker algorithm.

The Ramer-Douglas-Peucker algorithm finds a set of points in a polyline to remove so that the overall shape of the polyline is similar. How similar can be controlled by the distance epsilon.

The function takes a GSpan so it can be used with any attribute (that has a type float, float2, or float3).

Compute an index masks of points to remove to simplify the curve attribute using the Ramer-Douglas-Peucker algorithm. The Ramer-Douglas-Peucker algorithm finds a set of points in a polyline to remove so that the overall shape of the polyline is similar. How similar can be controlled by the distance `epsilon`. The function takes a `GSpan` so it can be used with any attribute (that has a type `float`, `float2`, or `float3`).
Falk David added 1 commit 2024-02-21 12:42:09 +01:00
78a58a075f Curves: Add resample adaptive algorithm
The Ramer-Douglas-Peucker algorithm finds a set of points in a polyline to remove so that the overall shape of the polyline is similar. How similar can be controlled by the distance `epsilon`.

The name "resample adaptive" comes from grease pencil. Other suggestions are welcome.
Falk David requested review from Hans Goudey 2024-02-21 12:42:52 +01:00
Member

On monkey character with the simplify modifier, this crashes at an assertion:

BLI_assert failed: source/blender/blenlib/BLI_offset_indices.hh:35, OffsetIndices(), at 'offsets_.size() < 2 || std::is_sorted(offsets_.begin(), offsets_.end())'

inside

*this = curves_copy_point_selection(*this, points_to_copy, propagation_info);

in

  dst_curves.remove_points(IndexMask::from_bools(points_to_delete, memory), {});

Not sure about why exactly.

On monkey character with the simplify modifier, this crashes at an assertion: ``` BLI_assert failed: source/blender/blenlib/BLI_offset_indices.hh:35, OffsetIndices(), at 'offsets_.size() < 2 || std::is_sorted(offsets_.begin(), offsets_.end())' ``` inside ``` *this = curves_copy_point_selection(*this, points_to_copy, propagation_info); ``` in ``` dst_curves.remove_points(IndexMask::from_bools(points_to_delete, memory), {}); ``` Not sure about why exactly.
Falk David added 1 commit 2024-02-21 14:15:59 +01:00
buildbot/vexp-code-patch-linux-x86_64 Build done. Details
buildbot/vexp-code-patch-lint Build done. Details
buildbot/vexp-code-patch-windows-amd64 Build done. Details
buildbot/vexp-code-patch-darwin-arm64 Build done. Details
buildbot/vexp-code-patch-darwin-x86_64 Build done. Details
buildbot/vexp-code-patch-coordinator Build done. Details
91f9881084
Fixes
Hans Goudey reviewed 2024-02-21 14:42:25 +01:00
Hans Goudey left a comment
Member

Nice. Where is this intended to be used?

  • Naming it closer to curves_ramer_douglas_peucker_simplify might be nice. The "resample" functions are all about sampling evenly spaced points on the curve currently. It could be helpful to keep that distinction. Especially since this is just removing points.
  • In the same vein, it might be nice to make that separation clearer by putting this in a new file instead.
  • Can the code be shared with the operator somehow, instead of duplicated?
Nice. Where is this intended to be used? - Naming it closer to `curves_ramer_douglas_peucker_simplify` might be nice. The "resample" functions are all about sampling evenly spaced points on the curve currently. It could be helpful to keep that distinction. Especially since this is just removing points. - In the same vein, it might be nice to make that separation clearer by putting this in a new file instead. - Can the code be shared with the operator somehow, instead of duplicated?
@ -514,0 +609,4 @@
/* Distance functions for `ramer_douglas_peucker_simplify`. */
const auto dist_function_positions =
[positions](int64_t first_index, int64_t last_index, int64_t index) {
Member

[positions] -> [&]

`[positions]` -> `[&]`
filedescriptor marked this conversation as resolved
Iliya Katushenock reviewed 2024-02-21 14:44:51 +01:00
@ -514,0 +607,4 @@
const VArray<bool> cyclic = src_curves.cyclic();
const OffsetIndices<int> points_by_curve = src_curves.points_by_curve();
/* Distance functions for `ramer_douglas_peucker_simplify`. */

In case this function can be replaced by any other, i'd like to see some notes about interface of this. Mainly this due to this is additional reference level in a hot loop. In other word, if there is no resaon to use other function, its metter to inline this function in ramer_douglas_peucker. Or by using template, or just by move dist_to_line_v3 call to the loop.

In case this function can be replaced by any other, i'd like to see some notes about interface of this. Mainly this due to this is additional reference level in a hot loop. In other word, if there is no resaon to use other function, its metter to inline this function in `ramer_douglas_peucker`. Or by using template, or just by move `dist_to_line_v3` call to the loop.
Author
Member

Originally, there where different distance functions.
Ideally, you want the resulting curves to look almost the same. This means that you need to take into account changes in thickness, opacity, color, etc.

I'm not sure what a good API for this would be. "Compute distances for these attributes too".

Originally, there where different distance functions. Ideally, you want the resulting curves to look almost the same. This means that you need to take into account changes in thickness, opacity, color, etc. I'm not sure what a good API for this would be. "Compute distances for these attributes too".
filedescriptor marked this conversation as resolved
Author
Member

This is intended to be used for the grease pencil modifier. Maybe we can use it for the operator too at some point.

This is intended to be used for the grease pencil modifier. Maybe we can use it for the operator too at some point.
Author
Member

@blender-bot build

@blender-bot build

Just to make sure, it is something similar to #114628?

Just to make sure, it is something similar to https://projects.blender.org/blender/blender/pulls/114628?
Author
Member

Just to make sure, it is something similar to #114628?

No, De Casteljau's algorithm is something different.

> Just to make sure, it is something similar to https://projects.blender.org/blender/blender/pulls/114628? No, De Casteljau's algorithm is something different.
Falk David added 3 commits 2024-02-29 17:24:17 +01:00
buildbot/vexp-code-patch-lint Build done. Details
buildbot/vexp-code-patch-linux-x86_64 Build done. Details
buildbot/vexp-code-patch-darwin-arm64 Build done. Details
buildbot/vexp-code-patch-darwin-x86_64 Build done. Details
buildbot/vexp-code-patch-windows-amd64 Build done. Details
buildbot/vexp-code-patch-coordinator Build done. Details
a1550b287c
Merge branch 'main' into curves-resample-adaptive
Author
Member

@blender-bot build

@blender-bot build
Falk David changed title from Curves: Add resample adaptive algorithm to WIP: Curves: Add resample adaptive algorithm 2024-03-20 14:05:43 +01:00
Iliya Katushenock approved these changes 2024-03-20 14:10:24 +01:00
Dismissed
Iliya Katushenock left a comment
Member

Interesting to check this is geometry node with support of multiple attributes in dist_function

Interesting to check this is geometry node with support of multiple attributes in `dist_function`
@ -0,0 +45,4 @@
float max_dist = -1.0f;
int max_index = -1;
for (const int64_t index : inside_range) {
const float dist = dist_function(sub_range.first(), sub_range.last(), index);

BLI_assert(dist >= 0.0f);

`BLI_assert(dist >= 0.0f);`
filedescriptor marked this conversation as resolved
@ -0,0 +76,4 @@
return;
}
const bool is_last_segment_selected = (curve_selection.first() && curve_selection.last());

(curve_selection.first() && curve_selection.last()) -> curve_selection.first() && curve_selection.last()

`(curve_selection.first() && curve_selection.last())` -> `curve_selection.first() && curve_selection.last()`
filedescriptor marked this conversation as resolved
Author
Member

I think a better approach might be something similar to smooth_curve_attribute:

  • Return an IndexMask of points that can be removed based on some input attribute (GSpan).
  • The function handles float, and float3 separately.

This means that we can combine the IndexMasks together to actaully remove points.
It's important that you can remove detail based on multiple attributes together. E.g. simplify a grease pencil stroke based on position, radius and opacity without loosing critical detail like the radii getting larger or the stroke end opacity fading out.

I think a better approach might be something similar to `smooth_curve_attribute`: * Return an `IndexMask` of points that can be removed based on some input attribute (`GSpan`). * The function handles `float`, and `float3` separately. This means that we can combine the `IndexMask`s together to actaully remove points. It's important that you can remove detail based on multiple attributes together. E.g. simplify a grease pencil stroke based on position, radius and opacity without loosing critical detail like the radii getting larger or the stroke end opacity fading out.

Sound like how update_elimination_mask_for_close_points are work.

Sound like how `update_elimination_mask_for_close_points` are work.
Author
Member

Looks like that is a similar idea yes.

Looks like that is a similar idea yes.
Falk David added 2 commits 2024-03-26 13:10:17 +01:00
Falk David added 1 commit 2024-03-26 13:12:20 +01:00
buildbot/vexp-code-patch-lint Build done. Details
buildbot/vexp-code-patch-linux-x86_64 Build done. Details
buildbot/vexp-code-patch-darwin-x86_64 Build done. Details
buildbot/vexp-code-patch-darwin-arm64 Build done. Details
buildbot/vexp-code-patch-windows-amd64 Build done. Details
buildbot/vexp-code-patch-coordinator Build done. Details
77e7f302b3
Pass cyclic VArray as const reference
Author
Member

@blender-bot build

@blender-bot build
Falk David added 2 commits 2024-03-26 13:58:17 +01:00
Falk David changed title from WIP: Curves: Add resample adaptive algorithm to Curves: Add resample adaptive algorithm 2024-03-26 13:58:33 +01:00
Falk David changed title from Curves: Add resample adaptive algorithm to Curves: Add `simplify_curve_attribute` function 2024-03-26 14:02:03 +01:00
Hans Goudey requested changes 2024-03-26 14:04:48 +01:00
Dismissed
@ -0,0 +13,4 @@
namespace blender::geometry {
/* Computes a "perpendicular distance" value for the generic attribute data based on the
Member

/** with a newline

`/**` with a newline
filedescriptor marked this conversation as resolved
@ -0,0 +57,4 @@
/* Mark all points to be kept. */
points_to_delete.slice(range).fill(false);
Stack<IndexRange> stack;
Member

Maybe give this a slightly larger inline buffer to avoid allocations for every curve

Maybe give this a slightly larger inline buffer to avoid allocations for every curve
filedescriptor marked this conversation as resolved
@ -0,0 +130,4 @@
const OffsetIndices<int> points_by_curve,
const VArray<bool> &cyclic,
const float epsilon,
GSpan attribute_data,
Member

const GSpan attribute_data

`const GSpan attribute_data`
filedescriptor marked this conversation as resolved
@ -0,0 +138,4 @@
points_by_curve, curves_selection, true, points_to_delete.as_mutable_span());
curves_selection.foreach_index(GrainSize(512), [&](const int64_t curve_i) {
const IndexRange points = points_by_curve[curve_i];
if (epsilon > 0.0f) {
Member

Can you return early at the beginning of this function in this case rather than checking inside the loop?

Can you return early at the beginning of this function in this case rather than checking inside the loop?
filedescriptor marked this conversation as resolved
@ -0,0 +144,4 @@
if constexpr (std::is_same_v<T, float> || std::is_same_v<T, float2> ||
std::is_same_v<T, float3>)
{
curve_simplifiy(points,
Member

It seems clearer to slice all the input data so it's all local to the curve in curve_simplifiy

It seems clearer to slice all the input data so it's all local to the curve in `curve_simplifiy`
filedescriptor marked this conversation as resolved
Iliya Katushenock reviewed 2024-03-26 14:10:10 +01:00
@ -0,0 +3,4 @@
* SPDX-License-Identifier: GPL-2.0-or-later */
#include "BLI_array_utils.hh"
#include "BLI_math_geom.h"

Unused?

Unused?
filedescriptor marked this conversation as resolved
@ -0,0 +4,4 @@
#include "BLI_array_utils.hh"
#include "BLI_math_geom.h"
#include "BLI_set.hh"

Unused.

Unused.
filedescriptor marked this conversation as resolved
@ -0,0 +31,4 @@
const int64_t last_index,
const int64_t index)
{
const float3 ray_dir = positions[last_index] - positions[first_index];

Not sure how compiler will inline this function, but part of things inside of this one can be computed once outside of loop.

Not sure how compiler will inline this function, but part of things inside of this one can be computed once outside of loop.
Author
Member

Seems like that wouldn't make a difference.

Seems like that wouldn't make a difference.
@ -0,0 +65,4 @@
if (sub_range.size() < 3) {
continue;
}
const IndexRange inside_range = sub_range.drop_front(1).drop_back(1);

@HooglyBoogly / @filedescriptor
Can we use MutableSpan<bool> instead of IndexRange directly, do you think its might be better?

@HooglyBoogly / @filedescriptor Can we use `MutableSpan<bool>` instead of `IndexRange` directly, do you think its might be better?
Author
Member

The index manipulation in this function was finicky to get right, so I was trying not to change it too much 😅. I would welcome any improvement.

The index manipulation in this function was finicky to get right, so I was trying not to change it too much 😅. I would welcome any improvement.
filedescriptor marked this conversation as resolved
Falk David added 4 commits 2024-03-26 14:21:31 +01:00
Falk David added 1 commit 2024-03-26 14:34:50 +01:00
Falk David added 1 commit 2024-03-26 14:53:37 +01:00
Falk David requested review from Hans Goudey 2024-03-26 14:54:16 +01:00
Falk David requested review from Iliya Katushenock 2024-03-26 14:54:17 +01:00
Hans Goudey approved these changes 2024-03-26 14:56:21 +01:00
Falk David added 1 commit 2024-03-26 14:58:41 +01:00
buildbot/vexp-code-patch-lint Build done. Details
buildbot/vexp-code-patch-linux-x86_64 Build done. Details
buildbot/vexp-code-patch-darwin-x86_64 Build done. Details
buildbot/vexp-code-patch-darwin-arm64 Build done. Details
buildbot/vexp-code-patch-windows-amd64 Build done. Details
buildbot/vexp-code-patch-coordinator Build done. Details
0fbf054734
Fix spelling
Author
Member

@blender-bot build

@blender-bot build
Iliya Katushenock approved these changes 2024-03-26 15:05:24 +01:00
Falk David merged commit f8ef2b3e78 into main 2024-03-26 15:28:22 +01:00
Falk David deleted branch curves-resample-adaptive 2024-03-26 15:28:25 +01:00
Sign in to join this conversation.
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
4 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#118560
No description provided.