Anybody Use SwiftClipper Here? Can't Instantiate A Class Without Init.

Hi. Beginner here.

https://github.com/lhuanyu/SwiftClipper

I am trying out Swift Clipper and when clipping arrays of CGPoint, I get the error message ClipperError(message: "Error: PolyTree struct is needed for open path clipping.")

Problem is, I cannot instantiate PolyTree class because there is no init() inside of it.

https://github.com/lhuanyu/SwiftClipper/blob/master/Sources/SwiftClipper/PolyNode.swift

Does this mean it is best I copy the whole library source code to my project so I can at least add an init() so I can instantiate it?

This is a simple method I am using to loop through each CGPoint array and union them.

func cb(_ e: [[CGPoint]]) -> [CGPoint] {
    var t: [CGPoint] = []
    if !e.isEmpty && e.count > 1 {
      t = e[0]
      var paths = Paths()
      let c = Clipper()
      for o in (1..<e.count) {
        do {
          paths.removeAll()
          c.clear()
          c.addPath(t, .subject, false)
          c.addPath(e[o], .subject, false)
          try c.execute(clipType: .union, solution: &paths)
          t = paths.first ?? []
        } catch {
          print(error)
        }
      }
    }
    return t
  }

Thoughts? Is this the only way to instantiate? By copying the source files to my project and add the init constructor there?

Or, is there a hack to forcefully add an init constructor of a lib? (Though I do not think this is possible)

Where are you using PolyTree in your code ? Note: naming var with a single letter is a recommended practice.

it is supposed to be in the .execute method. :polytree parameter and instead of instantiating paths = Paths() it should be paths= PolyTree() but gives a compile time error because there is no init in the class.

I am trying out Swift Clipper

What are you going to do with the output of the algorithm? Does it need to be numerically robust?

In my experience, it's easy to write geometric algorithms but very difficult to write them robustly. Non-robust algorithms will produce outputs with subtle problems that can cause hard to find bugs in the downstream code.

So please show the code that generates the error. And post the PolyTree class, for convenience.

@Claude31 @endecotp

My goal is to create a convex hull to show the path of a typhoon. I did this in Android using gpcj https://github.com/ChristianLutz/gpcj and GrahamScan classes in Java.

I think the GrahamScan source in Swift that I am using is ok, found here (pasting the link. too many source code classes if i paste them all here)

https://github.com/raywenderlich/swift-algorithm-club/tree/master/Convex%20Hull

I think the issue is somewhere in the SwiftClipper library since I get that error if the points result in an open path clipping and I cannot instantiate a PolyTree.

So currently only the first convex hull is drawn but the rest result in that error message.

The PolyTree class is in this link

https://github.com/lhuanyu/SwiftClipper/blob/master/Sources/SwiftClipper/PolyNode.swift

And as for using it .... here is the function

func getHullPoints(_ points: [[CGPoint]]) -> [CGPoint] {
    var hullPoints: [CGPoint] = []
    if !points.isEmpty && points.count > 1 {
      hullPoints = points[0]
      var paths = Paths()
      let clipper = Clipper()
      for o in (1..<points.count) {
        do {
          paths.removeAll()
          clipper.clear()
          clipper.addPath(hullPoints, .subject, false)
          clipper.addPath(points[o], .subject, false)
          try clipper.execute(clipType: .difference, solution: &paths)
          hullPoints = paths.first ?? []
        } catch {
          print(error)
        }
      }
    }
    return hullPoints
  }

Yep, sorry for the 1 letter variable. i just copied that from somewhere

The error is triggered in try clipper.execute() with that error message about open path clipping and PolyTree is needed. So the issue is i cannot instantiate it since there is no public init. Is the only way for this to copy the source code to my project and place an init so i can instantiate it?

 it says im posting something that is not permitted

You need to spell out some URLs to defeat the filter, i.e. put spaces between the characters to break it up, or replace . with DOT, etc.

My goal is to create a convex hull to show the path of a typhoon.

Could you post maybe a screenshot from your Android app to show what result you want? I'm a bit confused because I see the path as likely concave, not convex.

https://github.com/raywenderlich/swift-algorithm-club/tree/master/Convex%20Hull

That's not numerically robust.

Are you doing the calculations on the geographic coordinates, or on the screen coordinates?

This is the graham scan in java that i used. i didnt know there is a thing called concave hull. i thought theyre convex only.

frankly the easiest would have been to convert the libs i use to swift but still new to ios, i do not think i would be able to convert them easily so i opted to try and look for possible alternative libs.

triple w dot ime.usp.br/~pf/sedgewick-wayne/algs4

then add GrahamScan dot java at the end of the url.

In the image that you linked to in your comment, the orange shape is not convex. Its bottom edge is concave.

triple w dot ime.usp.br/~pf/sedgewick-wayne/algs4 then add GrahamScan dot java at the end of the url.

It actually says right at the top of that code: "May be floating-point issues if x- and y-coordinates are not integers."

This is exactly the numerical stability issue that I am referring to.

Anybody Use SwiftClipper Here? Can't Instantiate A Class Without Init.
 
 
Q