Skip to content

Clipping rectangle with open path #746

@rath3t

Description

@rath3t

Hi Clipper2 community, thanks for this awesome library.
I though about a very simple problem clipping away parts of a rectangle with open paths with C++

image

At then end I want the right-hand side a closed path, where the lower right hand part is gone and replaced by the red line.

I have three questions here:

  1. Since Clipperlib2 only supports subjects to be open, my scenario is not working out of the box, is it?
  2. As a fallback I can construct at least the lines that trims my rectangle ( the bold red line). To store all the information i need later, to reconstruct by hand my clipped rectangle I'm using the USINGZ feature. This I do by the following code:
using namespace Clipper2Lib;

    // create rectangle
    PathsD rectPath;
    rectPath.push_back({});
    for (int i=0; i<4 ; ++i)
      rectPath[0].push_back({corners[i][0],corners[i][1],i+1}); // flag indices of rectangle

    ClipperD c(5);
    int intersectionCounter= 5;

   std::map<int,std::tuple<int,int,PointD>> tracker;

    c.SetZCallback([&](const PointD& e1bot, const PointD& e1top,
  const PointD& e2bot, const PointD& e2top, PointD& pt) {

      tracker.insert({{intersectionCounter++},{e2top.z,e2bot.z,PointD({pt.x,pt.y})}});
      pt.z=intersectionCounter++; // not needed since the rectangle is gone anyway at the end
    });

    PathsD trimmingCurve;
    // ... create curve

    c.AddOpenSubject(trimmingCurve);
    c.AddClip(rectPath);
    PathsD clippedOpenEdges;
    PathsD clippedClosedEdges;
    PolyTreeD tree;
    // c.Execute(ClipType::Intersection,FillRule::NonZero,tree); // returns empty tree, is this expected?
    c.Execute(ClipType::Intersection,FillRule::EvenOdd,clippedClosedEdges,clippedOpenEdges)
   //clippedClosedEdges is empty 
   // clippedOpenEdges contains red line

   //reconstruct clipped rectangle with tracker and clippedOpenEdges 

Now with my tracker, I can reconstruct the full trimmed rectangle, from the information how I pierced my edges of the rectangle.

I feel this is quite overengineered and I wonder if there is an easier solution.
3. Since my solutions depends on setting a ZCallback on my rectangle path, I cannot use RectClipLines for my scenario, can I?

Thanks for your time.

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