PolygonOperations

Package: Libraries.Fabrication.Geometry · Inherits from: Libraries.Language.Object

The PolygonOperations class does boolean operations on closed loops (union, intersection, difference) by winding number, the way a Clipper-style library does: every edge is cut where it crosses another, each piece is kept when the winding numbers on its two sides straddle the requested level, and the kept pieces are chained back into loops. Results are oriented like ContourGenerator's: outer loops counter-clockwise, holes clockwise with isHole set. Lower level use (PolygonOffsetter and TreeSupport do this): Clear, then AddPoints / AddContours for any number of loops, then Resolve(1, plane) for everything with positive winding.

Example Code

use Libraries.Containers.Array
use Libraries.Fabrication.Geometry.Contour
use Libraries.Fabrication.Geometry.PolygonOperations

class Main
    action Main
        Array<Contour> a        // closed loops on plane 0
        Array<Contour> b
        PolygonOperations ops
        Array<Contour> both = ops:Intersect(a, b, 0)
        Array<Contour> either = ops:Union(a, b, 0)
        Array<Contour> onlyA = ops:Difference(a, b, 0)
    end
end

Actions

AddContours(Libraries.Containers.Array<Libraries.Fabrication.Geometry.Contour> loops, boolean reversed)

Adds every closed loop of the list (reversed = true subtracts them instead).

Parameters

Example

use Libraries.Containers.Array
use Libraries.Fabrication.Geometry.Contour
use Libraries.Fabrication.Geometry.PolygonOperations

Contour square
square:AddPoint(0, 0)
square:AddPoint(20, 0)
square:AddPoint(20, 20)
square:AddPoint(0, 20)
square:closed = true
Contour moved
moved:AddPoint(10, 10)
moved:AddPoint(30, 10)
moved:AddPoint(30, 30)
moved:AddPoint(10, 30)
moved:closed = true
Array<Contour> a
a:Add(square)
Array<Contour> b
b:Add(moved)
PolygonOperations ops
ops:AddContours(a, false)
ops:AddContours(b, true)       // subtract b
Array<Contour> difference = ops:Resolve(1, 0)

AddPoints(Libraries.Containers.Array<number> xs, Libraries.Containers.Array<number> ys, boolean reversed)

Adds one closed loop given as point lists (reversed = true subtracts it instead).

Parameters

Example

use Libraries.Containers.Array
use Libraries.Fabrication.Geometry.Contour
use Libraries.Fabrication.Geometry.PolygonOperations

Array<number> xs
Array<number> ys
xs:Add(0)
ys:Add(0)
xs:Add(10)
ys:Add(0)
xs:Add(10)
ys:Add(10)
PolygonOperations ops
ops:AddPoints(xs, ys, false)
Array<Contour> triangle = ops:Resolve(1, 0)

Clear()

Forgets the loops added so far.

Example

use Libraries.Containers.Array
use Libraries.Fabrication.Geometry.Contour
use Libraries.Fabrication.Geometry.PolygonOperations

Contour square
square:AddPoint(0, 0)
square:AddPoint(20, 0)
square:AddPoint(20, 20)
square:AddPoint(0, 20)
square:closed = true
Contour moved
moved:AddPoint(10, 10)
moved:AddPoint(30, 10)
moved:AddPoint(30, 30)
moved:AddPoint(10, 30)
moved:closed = true
Array<Contour> a
a:Add(square)
Array<Contour> b
b:Add(moved)
PolygonOperations ops
ops:AddContours(a, false)
ops:Clear()
ops:AddContours(b, false)
Array<Contour> justB = ops:Resolve(1, 0)

Difference(Libraries.Containers.Array<Libraries.Fabrication.Geometry.Contour> a, Libraries.Containers.Array<Libraries.Fabrication.Geometry.Contour> b, integer planeIndex)

The area covered by a but not by b.

Parameters

Returns

Libraries.Containers.Array<Libraries.Fabrication.Geometry.Contour>: Counter-clockwise outer loops and clockwise holes.

Example

use Libraries.Containers.Array
use Libraries.Fabrication.Geometry.Contour
use Libraries.Fabrication.Geometry.PolygonOperations

Contour square
square:AddPoint(0, 0)
square:AddPoint(20, 0)
square:AddPoint(20, 20)
square:AddPoint(0, 20)
square:closed = true
Contour moved
moved:AddPoint(10, 10)
moved:AddPoint(30, 10)
moved:AddPoint(30, 30)
moved:AddPoint(10, 30)
moved:closed = true
Array<Contour> a
a:Add(square)
Array<Contour> b
b:Add(moved)
PolygonOperations ops
Array<Contour> rest = ops:Difference(a, b, 0)
Contour notched = rest:Get(0)
output notched:Area()      // 300

GetBrokenChains()

How many loops the last Resolve dropped because their pieces didn't chain into a loop (degenerate input).

Returns

integer: How many loops the last Resolve dropped because their pieces didn't chain into a loop (degenerate input).

Example

use Libraries.Fabrication.Geometry.PolygonOperations

PolygonOperations ops
output ops:GetBrokenChains()        // 0 until a Resolve meets degenerate input

Intersect(Libraries.Containers.Array<Libraries.Fabrication.Geometry.Contour> a, Libraries.Containers.Array<Libraries.Fabrication.Geometry.Contour> b, integer planeIndex)

The area covered by both a and b.

Parameters

Returns

Libraries.Containers.Array<Libraries.Fabrication.Geometry.Contour>: Counter-clockwise outer loops and clockwise holes.

Example

use Libraries.Containers.Array
use Libraries.Fabrication.Geometry.Contour
use Libraries.Fabrication.Geometry.PolygonOperations

Contour square
square:AddPoint(0, 0)
square:AddPoint(20, 0)
square:AddPoint(20, 20)
square:AddPoint(0, 20)
square:closed = true
Contour moved
moved:AddPoint(10, 10)
moved:AddPoint(30, 10)
moved:AddPoint(30, 30)
moved:AddPoint(10, 30)
moved:closed = true
Array<Contour> a
a:Add(square)
Array<Contour> b
b:Add(moved)
PolygonOperations ops
Array<Contour> both = ops:Intersect(a, b, 0)
Contour overlap = both:Get(0)
output overlap:Area()      // 100

Resolve(integer minWinding, integer planeIndex)

Builds the loops around everything whose winding number is at least minWinding (1 = union, 2 = intersection of two non-overlapping sets).

Parameters

  • minWinding integer: The smallest winding number kept (1 or more).
  • planeIndex integer: The plane the result is on.

Returns

Libraries.Containers.Array<Libraries.Fabrication.Geometry.Contour>: Counter-clockwise outer loops and clockwise holes. The loops added stay added; Clear starts again.

Example

use Libraries.Containers.Array
use Libraries.Fabrication.Geometry.Contour
use Libraries.Fabrication.Geometry.PolygonOperations

Contour square
square:AddPoint(0, 0)
square:AddPoint(20, 0)
square:AddPoint(20, 20)
square:AddPoint(0, 20)
square:closed = true
Contour moved
moved:AddPoint(10, 10)
moved:AddPoint(30, 10)
moved:AddPoint(30, 30)
moved:AddPoint(10, 30)
moved:closed = true
Array<Contour> a
a:Add(square)
Array<Contour> b
b:Add(moved)
PolygonOperations ops
ops:AddContours(a, false)
ops:AddContours(b, false)
Array<Contour> overlap = ops:Resolve(2, 0)     // where both squares are

Union(Libraries.Containers.Array<Libraries.Fabrication.Geometry.Contour> a, Libraries.Containers.Array<Libraries.Fabrication.Geometry.Contour> b, integer planeIndex)

The area covered by a or b (or both), on the given plane.

Parameters

Returns

Libraries.Containers.Array<Libraries.Fabrication.Geometry.Contour>: Counter-clockwise outer loops and clockwise holes.

Example

use Libraries.Containers.Array
use Libraries.Fabrication.Geometry.Contour
use Libraries.Fabrication.Geometry.PolygonOperations

Contour square
square:AddPoint(0, 0)
square:AddPoint(20, 0)
square:AddPoint(20, 20)
square:AddPoint(0, 20)
square:closed = true
Contour moved
moved:AddPoint(10, 10)
moved:AddPoint(30, 10)
moved:AddPoint(30, 30)
moved:AddPoint(10, 30)
moved:closed = true
Array<Contour> a
a:Add(square)
Array<Contour> b
b:Add(moved)
PolygonOperations ops
Array<Contour> either = ops:Union(a, b, 0)
Contour outline = either:Get(0)
output outline:Area()      // 700

Inherited Actions

3 actions from Libraries.Language.Object