Clipper2 Library

repository·main·Indexed 25 days ago

https://github.com/angusjohnson/clipper2

A high-performance polygon clipping, offsetting, and triangulation library available for C++ (C++17), C# (Standard Library 2.0), and Delphi. It provides boolean operations (intersection, union, difference, XOR), polygon inflating/deflating, and Constrained Delaunay Triangulation. The library can also be accessed from other languages via a C++ compiled DLL using definitions in clipper.export.h.

Tokens
1.4K
Snippets
2
Records
12
Agent score
80%

What's inside Clipper2

  1. Overview of Clipper2 capabilities

    main

    Clipper2 is a library designed for polygon operations, including:

    • Boolean Operations: Performs intersection, union, difference, and XOR on both simple and complex polygons.
    • Polygon Offsetting: Also known as inflating/deflating polygons.
    • Constrained Delaunay Triangulation: Decomposing polygons into triangles.

    Note: The triangulation functionality is currently noted as having bugs and may receive fixes in future updates.

  2. Use Clipper2 for polygon clipping, offsetting, and triangulation

    main
    Clipper2 is a library designed for polygon clipping, offsetting, and triangulation. It is available in C++, C#, and Delphi. This version (Clipper2) is a major update to the original Clipper library (Clipper1), offering improved performance and features.
  3. Access Clipper2 via C++ DLL in other languages

    main
    For performance-critical applications in languages other than C++, you can dynamically link to the exported functions in the C++ compiled Clipper2 library (DLL). This approach is measurably faster than using the native C# or Delphi implementations.
  4. Access Clipper2 from non-C++ languages via DLL

    main

    While Clipper2 is natively written in C++, C#, and Delphi, developers using other programming languages can access its features by dynamically linking to a C++ compiled library (DLL).

    To use Clipper2 in a different language:

    1. Download the latest precompiled DLLs from the Clipper2 Releases.
    2. Use the exported functions and data structures defined in clipper.export.h to interface with the library.
    3. Dynamically link your application to the compiled Windows DLL (supports both 32-bit and 64-bit).
  5. Language requirements and setup for Clipper2

    main

    Clipper2 supports C++, C#, and Delphi Pascal. You can also access the library from other languages by dynamically linking to the exported functions in the C++ compiled Clipper2 library (DLL), which is recommended for performance-critical applications.

    Requirements by Language

    Lang.Requirements
    C++Requires C++17
    C#Uses Standard Library 2.0 (sample code uses .NET6)
    DelphiAny version from Delphi 7 to current
  6. Enable Google Benchmark for C++

    main

    To enable Google Benchmark for performance testing in the C++ implementation, you must set the USE_EXTERNAL_GBENCHMARK option to ON in the CPP/CMakeLists.txt file during the CMake configuration process.

    option(USE_EXTERNAL_GBENCHMARK "Use the googlebenchmark" ON)
  7. Perform polygon intersection with Clipper.Intersect

    main

    To perform a polygon intersection in C#, you can use the Clipper.Intersect method. This requires two sets of paths (Paths64) and a FillRule to determine how the polygons are filled. You can create paths from integer arrays using Clipper.MakePath.

          Paths64 subj = new Paths64();
          Paths64 clip = new Paths64();
          subj.Add(Clipper.MakePath(new int[] { 100, 50, 10, 79, 65, 2, 65, 98, 10, 21 }));
          clip.Add(Clipper.MakePath(new int[] { 98, 63, 4, 68, 77, 8, 52, 100, 19, 12 }));
          Paths64 solution = Clipper.Intersect(subj, clip, FillRule.NonZero);
  8. Reference exported functions in clipper.export.h

    main
    When interfacing with the Clipper2 DLL from external languages, the authoritative definition of all exported functions and their related data structures is located in clipper.export.h. You must use these exact definitions to ensure compatibility with the C++ compiled library.
  9. Perform Constrained Delaunay Triangulation with the Delaunay class

    main

    The Delaunay class provides a mechanism for performing Constrained Delaunay Triangulation on a set of paths. You can initialize the class with an optional boolean to enable or disable Delaunay conformance.

    To triangulate, use the Execute method, passing in a Paths64 object (a collection of paths) and receiving a TriangulateResult via reference to check for success or specific failure modes (like intersecting paths).

  10. Triangulate polygons using Triangulate()

    main

    The Triangulate function performs polygon triangulation. It supports both integer-based paths (Paths64) and double-based paths (PathsD).

    Using integer paths (Paths64)

    Use this overload when your input coordinates are already scaled to 64-bit integers. You must specify whether to use Delaunay triangulation via the useDelaunay boolean.

    Using double paths (PathsD)

    Use this overload when working with floating-point coordinates. You must provide decPlaces (decimal places), which determines the scaling factor used to convert the doubles to integers for the internal triangulation process. The function handles the scaling and un-scaling automatically.

    • pp: Input paths.
    • decPlaces: Number of decimal places to preserve (e.g., 2 for two decimal places).
    • solution: Output path containing the resulting triangles.
    • useDelaunay: If true, uses Delaunay triangulation; otherwise, uses standard triangulation.
  11. Handle TriangulateResult status codes

    main

    When calling Delaunay::Execute, the TriangulateResult enum indicates the outcome of the operation. Common values include:

    • success: Triangulation completed successfully.
    • no_polygons: No valid polygons were found in the input.
    • paths_intersect: The input paths intersect each other, preventing valid triangulation.
    • fail: An internal error occurred during the triangulation process.