Clipper——offset polylines twice

Hello! I met problems when I use clipper. I want to offset the polylines inwards at a distance of d and offset the polylines generated at a distance of 0.5 d again.


Based on the fact that clipper would generate contour and holes at the same time, the final results look like the figure below.There are too many incorrect holes which should be replaced by a single polyline. What should I do?

Any suggestions would be sincerely appreciated. Attachment could be visited below.
hand.3dm (117.5 KB)
print path.gh (16.3 KB)

I use C# to offset each polyline instead of offseting all the polylines at the same time. Inefficient but work at least. I am trying to find the way to generate toolpath for 3d concrete printing, any suggestions and discussions would be appreciated.

using StudioAvw.Geometry;

private void RunScript(List<Polyline> polylines, double distance, double tolerance, ref object Contour, ref object Holes)
  {
    {
      List<Polyline> outputContour = new List<Polyline>();
      List<Polyline> outputHoles = new List<Polyline>();
      List<Polyline> pl2 = new List<Polyline>();
      List<Polyline> out1;
      List<Polyline> out2;
      foreach(Polyline pl in polylines){
        pl2.Add(pl);
        Polyline3D.Offset(pl2, Polyline3D.OpenFilletType.Square, Polyline3D.ClosedFilletType.Square, distance, Plane.WorldXY, tolerance, out out1, out out2);
        outputContour.Add(out1.Find(Polyline => Polyline.Count != 0));
        outputHoles.Add(out2.Find(Polyline => Polyline.Count != 0));
        pl2.RemoveAt(0);
      }
      Contour = outputContour;
      Holes = outputHoles;}
  }

Hello
Many things are needed. You must provide all the polylines which are ONE border and THE holes. Then you have to take all clippers results and look if they are inside the shape. … some complementary tools must be done in order to do the job.
See these discussions

Thanks for your reply sincerely. Yes, these discussions are meaningful and I am looking through them.

Well … this is not the way to cut any mustard (i.e. offset inwards/outwards any shape closed and planar Polyline). Study the help “items” (blue, white), understand what they serve/do and try to write a C# that follows this kind of thinking.



1 Like

Inspiring, thanks a lot!

Some hints:

  1. Sample the poly pts in a List (say polyPts).
  2. Mastermind a 100% correct way to get a bisector Vector (say: offVector) inwards or outwards (on a per 3 poly pts (prev - current, next - current) basis) depending on the mode on duty.
  3. Get the corresponding offset pt (i.e. current pt + offVector) and sample it into a List (say: polyOffPts).
  4. For each index i in polyPts get:
    p0 = polyPts[i]; p1 = polyPts[(i+1) % polyPts.Count],
    p0a = polyOffPts[i]; p1a = polyOffPts[(i+1) % polyPts.Count].
  5. If (p0 - p1) is parallel to (p0a - p1a) get a Polyline from the 4 pts and sample it (as Curve) to some List (say: workCrvs).
  6. If (p0 - p1) is anti-parallel to (p0a - p1a) get the ccxPt (in the X from Lines (p0, p0a) and (p1, p1a)), get a Polyline from p0, p1, ccx Pt and sample it (as Curve) to workCrvs.

Now: you are that close (or that far) from the Holly Grail … you’ll need just 2 lines of code (in fact: several for a pro level speedy solution … but let’s stick to code basics) more to solve the inwards/outwards puzzle.

What may be these? That’s the 1M question.

Thank you again for these details and I will try to do it.

May the Force (the Dark option) be with you.


BTW: for eveything you do …some correct result means nothing (even kids can do it - if they try hard). All what matters is speed. For instance this (Poly Contains Pt) is seriously faster than the Curve Contains Pt RC Method:

1 Like

@PeterFotiadis, while getting the direction of this offset vector is trivial, how do you determine its length? I can think of moving the original line segment by its normal vector and finding intersection points with the offset vectors but that seems like a heavy way to - as you say - cut the mustard.

Any hints?

No it’s not (any planar Poly in any Plane).

Here’s an indicative hint (p is the poly pts collection as List):

Of course you can get the Vector vPrev, vNext half angle (in the correct way I do hope - that’s the big thing) and then (after Unitize()) … Point3d offPt = p0 + bisector * D / Math.Sin(angle).

1 Like

Of course! Trigonometry to the rescue!

Thanks a lot @PeterFotiadis!

Warning: this offset puzzle if full of mental traps.

One out of many: Let’s say that someone has a “bright” idea: IF the offsetPoly has the same orientation with the Polyline and if this (and that) happens (see code below) … can we skip the “expensive” proper way to solve it and speed things?

If this stupid “shortcut” executes (Method == 1) yields this result (faulty in various occasions):

While the correct solution is this:

Moral: speed is king … but better safe than sorry.