Skip to content
This repository was archived by the owner on Oct 28, 2026. It is now read-only.
This repository was archived by the owner on Oct 28, 2026. It is now read-only.

Public BuildTree64() equivalent? #927

Closed

Description

@kintel

We (OpenSCAD) are using Clipper2 to perform 2D polygon offset operations.
When offsetting, we prefer writing the result to a PolyTree structure, as that allows us to later walk the tree and easily identify holes as we go.

However, we also want to perform the recommended simplification between offset operations, and I haven't found a convenient way of achieving that:

  • SimplifyPath() doesn't accept a PolyTree parameter (only Path and Paths).
  • BuildTree64(), which converts from Paths to PolyTree, is private.

Are there any recommended ways of obtaining a simplified PolyTree result from an offset result?
Barring a recommended way, what would be the best way forward, e.g. Add a new SimplifyPath() variant, make BuildTree64() public, or building a copy of one of those in user space?

Activity

  1. seghier commented on Jan 7, 2025

    @seghier

    Hi @kintel
    SimplifyPath() doesn't accept a PolyTree parameter (only Path and Paths).

    Did you try PolyTreeToPaths64 and PolyTreeToPathsD

  2. kintel commented on Jan 7, 2025

    @kintel
    ContributorAuthor

    Hi @kintel SimplifyPath() doesn't accept a PolyTree parameter (only Path and Paths).

    Did you try PolyTreeToPaths64 and PolyTreeToPathsD

    Yes, but I'm missing a Paths64ToPolyTree().

  3. seghier commented on Jan 7, 2025

    @seghier

    In my plugin sometimes i add custom functions or classes (in this case ClipperUtils) maybe you can use the same way

    namespace Clipper2Lib
    {
        public static class ClipperUtils
        {
            public static PolyTreeD PathsDToPolyTree(PathsD pathsD)
            {
                PolyTreeD polyTreeD = new PolyTreeD();
                ClipperD clipper = new ClipperD();
    
                // Add paths to the Clipper instance
                clipper.AddSubject(pathsD);
    
                // Execute a union operation to populate the PolyTreeD
                clipper.Execute(ClipType.Union, FillRule.NonZero, polyTreeD);
    
                return polyTreeD;
            }
    
            public static PolyTree64 Paths64ToPolyTree(Paths64 paths64)
            {
                PolyTree64 polyTree64 = new PolyTree64();
                Clipper64 clipper = new Clipper64();
    
                // Add paths to the Clipper instance
                clipper.AddSubject(paths64);
    
                // Execute a union operation to populate the PolyTree64
                clipper.Execute(ClipType.Union, FillRule.NonZero, polyTree64);
    
                return polyTree64;
            }
        }
    }
    
  4. kintel commented on Jan 7, 2025

    @kintel
    ContributorAuthor

    Thanks! It feels a bit expensive to perform a union just to convert, considering that the Paths64 already came straight from Clipper.

    In the meantime I'll look into writing my own simplification function, operating on PolyTrees..

  5. kintel commented on Jan 10, 2025

    @kintel
    ContributorAuthor

    OK, looks like a PolyTree implementation is pretty easy, at least for closed curves:

    void SimplifyPolyTree(const Clipper2Lib::PolyPath64& polytree, double epsilon, Clipper2Lib::PolyPath64& result) {
      for (const auto& child : polytree) {
        Clipper2Lib::PolyPath64 *newchild = result.AddChild(Clipper2Lib::SimplifyPath(child->Polygon(), epsilon));
        SimplifyPolyTree(*child, epsilon, *newchild);
      }
    }
    
  6. Repository owner locked and limited conversation to collaborators on Jan 11, 2025
  7. converted this issue into a discussion #930 on Jan 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions