Skip to content

feat: [GSoC'26] Initial MVP for 2D Free-Form Deformation Layer - #3724

Open
ahmedfathy0-0 wants to merge 83 commits into
synfig:masterfrom
ahmedfathy0-0:feat/free_form_deform
Open

ahmedfathy0-0 wants to merge 83 commits into
synfig:masterfrom
ahmedfathy0-0:feat/free_form_deform

Conversation

@ahmedfathy0-0

@ahmedfathy0-0 ahmedfathy0-0 commented Mar 26, 2026

Copy link
Copy Markdown

This PR implements the complete 2D Free-Form Deformation (FFD) GSoC project. It introduces a powerful new distortion layer that allows artists to warp artwork using customizable 2D grids or automatically fitted custom triangle meshes.

Resolves #3792

What's Included:

Core FFD Features

  • Layer_FreeFormDeform: A new layer type to handle high-performance, timeline-keyframeable mesh deformations.
  • Dual Interpolation Engine: Deform images using Bilinear interpolation or Catmull-Rom spline interpolation (adjustable via a Smoothness parameter).
  • Bone Linking: FFD control points can be linked directly to Bones, allowing the deformation mesh to be driven by a skeletal rig for character animation.

Grid Mode

  • Customizable Grids: Generate and edit regular grids of any size (e.g., 4x4, 10x10).
  • Smart Bounding Box Fitting: Automatically calculates the tightest oriented bounding box around the artwork (using PCA and alpha-mask analysis) so the grid wraps perfectly, even on off-center or rotated images.

Custom Mesh Mode

  • Automatic Silhouette Tracing: Detects the artwork's alpha channel and automatically builds a boundary mesh using morphological dilation and Moore-neighborhood tracing.
  • Delaunay Triangulation: Automatically builds the interior mesh structure using Bowyer-Watson Delaunay triangulation with a Centroid-In-Polygon (CIP) filter for concave shapes.
  • Mesh Editing UI: Full suite of on-canvas topology editing tools. Left-click/Right-click to add and remove points, and use edge-context menus to perform Split, Swap, Collapse, and Subdivide operations.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new Free-Form Deformation layer to the Synfig core, allowing for mesh-based distortions via a grid of control points. The changes include the layer implementation, header, and integration into the build system. A review comment points out that the grid initialization in the constructor is hardcoded to 3x3, which creates a tight coupling with default parameters and could lead to bugs if those defaults are changed.

Comment thread synfig-core/src/synfig/layers/layer_freeformdeform.cpp Outdated
@mohamedAdhamc
mohamedAdhamc marked this pull request as draft April 3, 2026 19:03
@zaynopenapp

Copy link
Copy Markdown

I hope you can create something like this: custom point vertices, so the mesh can generate points similar to BLine points, making it possible to create shape key controllers later on — similar to how it works in MOHO.

point_mesh

@codacy-production

codacy-production Bot commented May 26, 2026

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 367 complexity · 49 duplication

Metric Results
Complexity 367
Duplication 49

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@ahmedfathy0-0

Copy link
Copy Markdown
Author

to be updated with the new changes

Key Changes

  • FFD Tool Implementation: Added the new Free-Form Deformation tool to the toolbox, including custom controls for grid subdivisions (X/Y) and smoothness (state_ffd.cpp).
  • Grid Point Interpolation: Added interpolation logic so that when the user changes the grid subdivisions (resizes the grid), the existing control points adjust dynamically to preserve the shape.
  • Visual Grid Connections: Implemented visual grid lines connecting the control ducks in the workspace, making it easier for users to see and manipulate the lattice (duckmatic.cpp).
  • UI & Icons: Added custom vector icons for the FFD tool and layer, and registered them within the application. but i hope we can change the icon later (the icon used is the same I used in the provided Images)

Next Updates

  • Context Fitting: Enhanceing the FFD layer with source corner parameters, allowing the deformation grid to automatically fit the bounding box of the underlying context/image.

I will be very thankful if you guys can provided me any comments for the new changes

@mohamedAdhamc

image image image

@ahmedfathy0-0

Copy link
Copy Markdown
Author

Latest Updates:

  1. Automated Grouping & Validation: For now, I've restricted FFD creation to a single Switch layer, so it currently works perfectly with imported images (I am actively looking into how to support shapes next). It also automatically encapsulates the Switch and FFD layers into a new PasteCanvas group. This safely transfers the transformation properties to the parent group, preventing any double-transformation misalignment.
  2. Accurate Bounds Resolution: The grid now automatically adapts to the image's exact dimensions, filling its bounding box perfectly.
  3. Undoable UI Controls: I added a "Reset Grid" button to the FFD tool options, allowing users to easily undo and reset any manual adjustments made to the grid points.

@mohamedAdhamc

Copy link
Copy Markdown
Member

Hello @ahmedfathy0-0 , the pictures look good. I will try it out and start reviewing it soon.:) hopefully by tonight.

- Switched from Ear-Clipping to Bowyer-Watson Delaunay triangulation in `Layer_FreeFormDeform::triangulate`.
- Removed deprecated helper methods (`is_ccw`, `point_in_triangle`, `is_ear`) from `Layer_FreeFormDeform`.
- Updated `StateFFD_Context` to remove forced sequential bezier perimeter connections during mesh drawing.
- The Custom Mesh tool now naturally draws the Convex Hull and optimal internal mesh connectivity automatically.
- When a user selects 'Free-Form Deformation' from the 'New Layer > Distortions' menu, instead of creating a raw layer without the tool context, Synfig will now seamlessly switch the active toolbox tool to the 'FFD Tool'.
- Reinstated the logic that automatically wraps a selected Switch layer in a new Group when an FFD layer is created.
- Moved this logic from CanvasView::add_layer into StateFFD_Context::on_make_ffd_pressed() so it works natively with the FFD Tool after a Custom Mesh or Grid is built.
- Integrated Choice 2 (Corner Pin method) as requested by the user.
- Automatically computes the global context bounds and injects the 4 extreme corners into both the source and deformed point lists during prepare_mesh().
- This naturally forces the Delaunay triangulation to generate a mesh that covers the entire canvas.
- Prevents the Straight blend mode from clipping the unaffected background areas, while seamlessly replacing the deformed portions without ghosting artifacts.
@ahmedfathy0-0

Copy link
Copy Markdown
Author

now the custom mesh of free form deformation is implemented.
@zaynopenapp can u try it and give me any comments
here is a short video

2026-06-08.12-03-49.mp4

the next comment will be about the implementation description

@ahmedfathy0-0 ahmedfathy0-0 changed the title feat: Initial MVP for 2D Free-Form Deformation Layer [GSoC'26] Initial MVP for 2D Free-Form Deformation Layer Jun 8, 2026
@zaynopenapp

Copy link
Copy Markdown

Wow, this is exactly what I've been searching for for a long time. All my previous attempts to create a mesh like this had failed. This is incredible. Synfig can finally have a mesh that can be controlled much like a BLine. I hope to build a Smart Mesh system in the future, similar to Smart Bones in Moho. Thank you so much for your hard work and for sharing this amazing idea.

When the FFD layer is set to Custom Mesh mode, the 'Reset Grid' button is now visible and functions to reset the deformed mesh points back to their original un-deformed positions (by copying source_points over to grid_points). Previously, it would attempt to compute a standard bounding-box grid, corrupting the custom mesh.
@codacy-production

codacy-production Bot commented Jul 7, 2026

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 2675 complexity · 322 duplication

Metric Results
Complexity 2675
Duplication 322

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@ahmedfathy0-0

Copy link
Copy Markdown
Author

Summary of Last Changes

Here is a summary of the features, optimizations, and bug fixes implemented across the FFD (Free Form Deformation) tool:

1. Automated Contour-Based Mesh Generation (Auto-Mesh)

  • Silhouette Tracing: Implemented automatic silhouette contour-tracing of underlying layers by rendering the alpha channel and caching the surface boundary.

  • Silhouette Filtering (Centroid-in-Polygon): Replaced the old distance-based cull threshold with a robust Centroid-in-Polygon (CIP) algorithm. This automatically clips out Delaunay triangles that fall outside the traced silhouette boundary.

  • Interior Steiner Points: Added automated interior Steiner point generation within the traced boundary contour, providing an even/stable vertex distribution across the mesh.

  • Mesh Margin & Strength Controls: Introduced interactive sliders to configure the mesh margin (clamped to 0–30px) and mesh strength/edge density (clamped to a max of 20.0).

image image

2. Explicit Triangle Mesh Topology & Editing

  • Mesh Persistence: Added support for storing and managing explicit triangle mesh topology inside the FreeFormDeform layer parameters.

  • Manual Edge Editing: Implemented interactive canvas controls for manual mesh manipulation, allowing users to swap, collapse, and split mesh edges in real-time.

image

3. UI/UX & Interaction Polishing

  • Opt-In Bezier Hovering: Added opt-in hover detection for Bezier lines to prevent the FFD tool from stealing focus from other canvas elements.

  • Hover Responsiveness: Improved Bezier hover state tracking to make canvas interactions instantly responsive to mouse movements.

@mohamedAdhamc mohamedAdhamc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a quick intitial review.

Comment thread synfig-studio/images/CMakeLists.txt Outdated
Comment thread synfig-studio/src/gui/states/CMakeLists.txt
Comment thread synfig-studio/src/gui/states/state_ffd.cpp Outdated
Comment thread synfig-studio/src/gui/duckmatic.cpp Outdated
Comment thread synfig-studio/src/gui/duckmatic.cpp Outdated
Comment thread synfig-studio/src/gui/duckmatic.cpp Outdated
Comment thread synfig-studio/src/gui/duckmatic.cpp Outdated
Comment thread synfig-studio/src/gui/workarea.h Outdated
@ahmedfathy0-0
ahmedfathy0-0 force-pushed the feat/free_form_deform branch from 14e3a98 to 6261128 Compare July 23, 2026 13:51
@ahmedfathy0-0
ahmedfathy0-0 force-pushed the feat/free_form_deform branch from 6261128 to 5abd43a Compare July 23, 2026 14:02
@ahmedfathy0-0 ahmedfathy0-0 changed the title [GSoC'26] Initial MVP for 2D Free-Form Deformation Layer feat: [GSoC'26] Initial MVP for 2D Free-Form Deformation Layer Aug 22, 2026
@ahmedfathy0-0
ahmedfathy0-0 marked this pull request as ready for review August 22, 2026 15:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: FFD Project

3 participants