GPv3: Merge by distance #113917

Open
opened 2023-10-19 07:40:29 +02:00 by Pratik Borhade · 6 comments
Member

Merge from selected/all points based on threshold value

  • Define operator GREASE_PENCIL_OT_stroke_merge_by_distance in grease_pencil_edit.cc
  • Create operator property threshold (int)
  • Iterate through all the drawings at current scene frame (retrieve_editable_drawings)
    • Get geometry of this drawing: bke::CurvesGeometry &curves = drawing.strokes_for_write();
    • Define an array merge_indices with the size of the points as well as a counter for the number of points to be merged for later.
    • Loop though each individual curve using threading::parallel_for(curves.curves_range(), ...
      • Get a selection mask of the selected points in the curve (see for example ed::greasepencil::smooth_curve_attribute)
      • Get the selection ranges using IndexMask::to_ranges()
      • For every selection range:
        • Create an array distances that computes the accumulated distance for every point to the first point (e.g. the fist point has a distance of 0, every next point has a distances of prev_distance + distance to previous point)
        • Build a KDTree_1d for the distances (BLI_kdtree_1d_new, BLI_kdtree_1d_insert, BLI_kdtree_1d_balance)
        • Use BLI_kdtree_1d_calc_duplicates_fast to find the points that need to be merged. The return value is the number of points that need to be merged, e.g. remove. Make sure to accumulate this count into the counter declared above so that we can calculate the size of the final CurvesGeometry.
        • Write the resulting indices from BLI_kdtree_1d_calc_duplicates_fast into merge_indices. Make sure to use the correct offsets.
    • Create a new CurvesGeometry with the same number of curves and calculate the number of points (remaining after merges).
    • Calculate the new offsets using the merge_indicies array (see logic in point_merge_by_distance.cc) as well as a OffsetIndices<int> map_offsets.
    • Transfer all the point attributes:
      • Iterate over all source attributes and use bke::attribute_math::DefaultMixer<T> mixer to mix the values for all the destination points. Use the map_offsets to find the source points to mix the attributes from (There might only be one point if it just need to be copied, that's fine).
    • Copy all the curve attributes
Merge from selected/all points based on threshold value - Define operator `GREASE_PENCIL_OT_stroke_merge_by_distance` in `grease_pencil_edit.cc` - Create operator property `threshold` (int) - Iterate through all the drawings at current scene frame (`retrieve_editable_drawings`) - Get geometry of this drawing: `bke::CurvesGeometry &curves = drawing.strokes_for_write();` - Define an array `merge_indices` with the size of the points as well as a counter for the number of points to be merged for later. - Loop though each individual curve using `threading::parallel_for(curves.curves_range(), ...` - Get a selection mask of the selected points in the curve (see for example `ed::greasepencil::smooth_curve_attribute`) - Get the selection ranges using `IndexMask::to_ranges()` - For every selection range: - Create an array `distances` that computes the accumulated distance for every point to the first point (e.g. the fist point has a distance of 0, every next point has a distances of prev_distance + distance to previous point) - Build a `KDTree_1d` for the distances (`BLI_kdtree_1d_new`, `BLI_kdtree_1d_insert`, `BLI_kdtree_1d_balance`) - Use `BLI_kdtree_1d_calc_duplicates_fast` to find the points that need to be merged. The return value is the number of points that need to be merged, e.g. remove. Make sure to accumulate this count into the counter declared above so that we can calculate the size of the final `CurvesGeometry`. - Write the resulting indices from `BLI_kdtree_1d_calc_duplicates_fast` into `merge_indices`. Make sure to use the correct offsets. - Create a new `CurvesGeometry` with the same number of curves and calculate the number of points (remaining after merges). - Calculate the new offsets using the `merge_indicies` array (see logic in `point_merge_by_distance.cc`) as well as a `OffsetIndices<int> map_offsets`. - Transfer all the point attributes: - Iterate over all source attributes and use `bke::attribute_math::DefaultMixer<T> mixer` to mix the values for all the destination points. Use the `map_offsets` to find the source points to mix the attributes from (There might only be one point if it just need to be copied, that's fine). - Copy all the curve attributes
Pratik Borhade added the
Priority
Normal
Module
Grease Pencil
Type
To Do
labels 2023-10-19 07:40:44 +02:00
Pratik Borhade added this to the Grease Pencil project 2023-10-19 07:40:59 +02:00
Member

I think the actual merging is fairly tricky here, at least doing it in a performant way.

The same behavior should be used as a geometry node (possibly the existing "Merge by Distance" node), that's something to keep in mind.

Rather than having an "unselected" toggle, IMO it would be simpler and clearer to just require selecting every point for that case. All it would require is first pressing "A".

I think the actual merging is fairly tricky here, at least doing it in a performant way. The same behavior should be used as a geometry node (possibly the existing "Merge by Distance" node), that's something to keep in mind. Rather than having an "unselected" toggle, IMO it would be simpler and clearer to just require selecting every point for that case. All it would require is first pressing "A".
Member

After a discussion with @HooglyBoogly I edited the task to reflect the changes.

After a discussion with @HooglyBoogly I edited the task to reflect the changes.
Contributor

While solving this issue I got stuck at Create an array distances that computes the accumulated distance for every point to the first point (e.g. the fist point has a distance of 0, every next point has a distances of prev_distance + distance to previous point) How will I get the points and calculate distance for every selection range , could you give me an overview of how should I approach this and steps after this. Thank you.

While solving this issue I got stuck at ```Create an array distances that computes the accumulated distance for every point to the first point (e.g. the fist point has a distance of 0, every next point has a distances of prev_distance + distance to previous point)``` How will I get the points and calculate distance for every selection range , could you give me an overview of how should I approach this and steps after this. Thank you.
Contributor

Hello. Is this task currently being worked on?

Hello. Is this task currently being worked on?
Member

@Chao-Li We have a way of merging by distance in the simplify modifier now. I guess the same function could be used to implement the operator.

@Chao-Li We have a way of merging by distance in the simplify modifier now. I guess the same function could be used to implement the operator.
Contributor

Let me take a look there. Thank you for the pointer!

Let me take a look there. Thank you for the pointer!
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
5 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#113917
No description provided.