ContourSimplifier

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

The ContourSimplifier class removes points that hardly change a path's shape, with the Ramer-Douglas-Peucker algorithm: a point is kept only if leaving it out would move the path by more than the tolerance. A closed loop is first split at its first point and the point farthest from it, so the result keeps the loop's order (and so its direction).

Example Code

use Libraries.Fabrication.Geometry.Contour
use Libraries.Fabrication.Geometry.ContourSimplifier

class Main
    action Main
        Contour line
        line:AddPoint(0, 0)
        line:AddPoint(5, 0.001)
        line:AddPoint(10, 0)
        line:AddPoint(10, 10)
        ContourSimplifier simplifier
        simplifier:Simplify(line, 0.01)
        output line:GetSize()       // 3: (5, 0.001) was within 0.01 of the line
    end
end

Actions

SimplifiedLoop(Libraries.Fabrication.Geometry.Contour loop, number tolerance)

A simplified copy of a closed loop, for outlines where a sliver is better dropped than kept.

Parameters

Returns

Libraries.Fabrication.Geometry.Contour: A new closed loop on the same plane, or undefined if fewer than 3 points remain or its area is below tolerance squared.

Example

use Libraries.Fabrication.Geometry.Contour
use Libraries.Fabrication.Geometry.ContourSimplifier

Contour sliver
sliver:AddPoint(0, 0)
sliver:AddPoint(10, 0)
sliver:AddPoint(10, 0.001)
sliver:AddPoint(0, 0.001)
sliver:closed = true
ContourSimplifier simplifier
Contour kept = simplifier:SimplifiedLoop(sliver, 0.1)
output kept = undefined        // true: too thin to keep

Simplify(Libraries.Fabrication.Geometry.Contour contour, number tolerance)

Simplifies a contour in place. Contours with fewer than 4 points, or a tolerance of 0 or less, are left as they are, and so is a closed loop that would keep fewer than 3 points.

Parameters

Example

use Libraries.Fabrication.Geometry.Contour
use Libraries.Fabrication.Geometry.ContourSimplifier

Contour edge
edge:AddPoint(0, 0)
edge:AddPoint(5, 0.001)
edge:AddPoint(10, 0)
edge:AddPoint(10, 10)
ContourSimplifier simplifier
simplifier:Simplify(edge, 0.01)
output edge:GetSize()       // 3: the nearly straight point is gone

Inherited Actions

3 actions from Libraries.Language.Object