|
| 1 | +import * as ClipperLib from "clipper-lib"; |
| 2 | + |
| 3 | +// IntPoint |
| 4 | +const pt: ClipperLib.IntPoint = { X: 10, Y: 20 }; |
| 5 | +const ptZ: ClipperLib.IntPoint = { X: 10, Y: 20, Z: 5 }; |
| 6 | + |
| 7 | +// Path / Paths |
| 8 | +const path: ClipperLib.Path = [ |
| 9 | + { X: 0, Y: 0 }, |
| 10 | + { X: 100, Y: 0 }, |
| 11 | + { X: 100, Y: 100 }, |
| 12 | + { X: 0, Y: 100 }, |
| 13 | +]; |
| 14 | +const paths: ClipperLib.Paths = [path]; |
| 15 | + |
| 16 | +// Enums |
| 17 | +const ct: ClipperLib.ClipType = ClipperLib.ClipType.ctUnion; |
| 18 | +const pt2: ClipperLib.PolyType = ClipperLib.PolyType.ptSubject; |
| 19 | +const pft: ClipperLib.PolyFillType = ClipperLib.PolyFillType.pftEvenOdd; |
| 20 | +const jt: ClipperLib.JoinType = ClipperLib.JoinType.jtRound; |
| 21 | +const et: ClipperLib.EndType = ClipperLib.EndType.etClosedPolygon; |
| 22 | + |
| 23 | + |
| 24 | +// Clipper - constructor |
| 25 | +const cpr = new ClipperLib.Clipper(); |
| 26 | +const cpr2 = new ClipperLib.Clipper( |
| 27 | + ClipperLib.InitOptions.ioStrictlySimple | ClipperLib.InitOptions.ioPreserveCollinear, |
| 28 | +); |
| 29 | + |
| 30 | +// Clipper - instance methods |
| 31 | +cpr.AddPath(path, ClipperLib.PolyType.ptSubject, true); |
| 32 | +cpr.AddPaths(paths, ClipperLib.PolyType.ptClip, true); |
| 33 | +const solution: ClipperLib.Paths = []; |
| 34 | +const success: boolean = cpr.Execute(ClipperLib.ClipType.ctIntersection, solution); |
| 35 | +const bounds: ClipperLib.IntRect = cpr.GetBounds(); |
| 36 | +cpr.Clear(); |
| 37 | + |
| 38 | +// Clipper - properties |
| 39 | +cpr.PreserveCollinear = true; |
| 40 | +cpr.ReverseSolution = false; |
| 41 | +cpr.StrictlySimple = true; |
| 42 | + |
| 43 | +// Clipper - static methods |
| 44 | +const area: number = ClipperLib.Clipper.Area(path); |
| 45 | +const cleaned: ClipperLib.Path = ClipperLib.Clipper.CleanPolygon(path, 1.1); |
| 46 | +const cleanedPaths: ClipperLib.Paths = ClipperLib.Clipper.CleanPolygons(paths, 1.1); |
| 47 | +const orientation: boolean = ClipperLib.Clipper.Orientation(path); |
| 48 | +const inPoly: number = ClipperLib.Clipper.PointInPolygon(pt, path); |
| 49 | +ClipperLib.Clipper.ReversePath(path); |
| 50 | +ClipperLib.Clipper.ReversePaths(paths); |
| 51 | +const simplified: ClipperLib.Paths = ClipperLib.Clipper.SimplifyPolygon(path); |
| 52 | +const simplifiedPaths: ClipperLib.Paths = ClipperLib.Clipper.SimplifyPolygons(paths); |
| 53 | + |
| 54 | +// Clipper - PolyTree |
| 55 | +const polytree = new ClipperLib.PolyTree(); |
| 56 | +cpr.AddPath(path, ClipperLib.PolyType.ptSubject, true); |
| 57 | +cpr.Execute(ClipperLib.ClipType.ctUnion, polytree); |
| 58 | +const closedPaths: ClipperLib.Paths = ClipperLib.Clipper.ClosedPathsFromPolyTree(polytree); |
| 59 | +const openPaths: ClipperLib.Paths = ClipperLib.Clipper.OpenPathsFromPolyTree(polytree); |
| 60 | +const allPaths: ClipperLib.Paths = ClipperLib.Clipper.PolyTreeToPaths(polytree); |
| 61 | +const total: number = polytree.Total(); |
| 62 | +const first: ClipperLib.PolyNode = polytree.GetFirst(); |
| 63 | +polytree.Clear(); |
| 64 | + |
| 65 | +// PolyNode |
| 66 | +const node = new ClipperLib.PolyNode(); |
| 67 | +const childCount: number = node.ChildCount(); |
| 68 | +const childs: ClipperLib.PolyNode[] = node.Childs(); |
| 69 | +const contour: ClipperLib.Path = node.Contour(); |
| 70 | +const next: ClipperLib.PolyNode = node.GetNext(); |
| 71 | +const isHole: boolean = node.IsHole(); |
| 72 | +const parent: ClipperLib.PolyNode = node.Parent(); |
| 73 | +const isOpen: boolean = node.IsOpen; |
| 74 | + |
| 75 | +// Clipper - Minkowski |
| 76 | +const minkDiff: ClipperLib.Paths = ClipperLib.Clipper.MinkowskiDiff(path, path); |
| 77 | +const minkSum: ClipperLib.Paths = ClipperLib.Clipper.MinkowskiSum(path, path, true); |
| 78 | + |
| 79 | +// Clipper - deprecated OffsetPaths |
| 80 | +const offsetted: ClipperLib.Paths = ClipperLib.Clipper.OffsetPaths(paths, 10); |
| 81 | + |
| 82 | +// ClipperOffset |
| 83 | +const co = new ClipperLib.ClipperOffset(); |
| 84 | +const co2 = new ClipperLib.ClipperOffset(2.0, 0.25); |
| 85 | +co.AddPath(path, ClipperLib.JoinType.jtRound, ClipperLib.EndType.etClosedPolygon); |
| 86 | +co.AddPaths(paths, ClipperLib.JoinType.jtSquare, ClipperLib.EndType.etClosedPolygon); |
| 87 | +const offsetSolution: ClipperLib.Paths = []; |
| 88 | +co.Execute(offsetSolution, 10); |
| 89 | +co.ArcTolerance = 0.25; |
| 90 | +co.MiterLimit = 2.0; |
| 91 | +co.Clear(); |
| 92 | + |
| 93 | +// JS Helper functions |
| 94 | +ClipperLib.JS.ScaleUpPath(path, 100); |
| 95 | +ClipperLib.JS.ScaleUpPaths(paths, 100); |
| 96 | +ClipperLib.JS.ScaleDownPath(path, 100); |
| 97 | +ClipperLib.JS.ScaleDownPaths(paths, 100); |
| 98 | +const areaOf: number = ClipperLib.JS.AreaOfPolygon(path); |
| 99 | +const areasOf: number = ClipperLib.JS.AreaOfPolygons(paths); |
| 100 | +const boundsPath: ClipperLib.IntRect = ClipperLib.JS.BoundsOfPath(path); |
| 101 | +const boundsPaths: ClipperLib.IntRect = ClipperLib.JS.BoundsOfPaths(paths); |
| 102 | +const cloned: ClipperLib.Paths = ClipperLib.JS.Clone(paths); |
| 103 | +const cleanedJS: ClipperLib.Path = ClipperLib.JS.Clean(path, 1.0); |
| 104 | +const lightened: ClipperLib.Path = ClipperLib.JS.Lighten(path, 1.0); |
| 105 | +const perimPath: number = ClipperLib.JS.PerimeterOfPath(path, true); |
| 106 | +const perimPaths: number = ClipperLib.JS.PerimeterOfPaths(paths, true); |
| 107 | +const exPolys: ClipperLib.ExPolygons = ClipperLib.JS.PolyTreeToExPolygons(polytree); |
| 108 | +const fromEx: ClipperLib.Paths = ClipperLib.JS.ExPolygonsToPaths(exPolys); |
0 commit comments