Contour

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

The Contour class is a 2D path on one slice plane: a closed loop (an outline, a hole, a wall) or an open polyline (a fill line). It is the common currency of the fabrication libraries: slicing produces contours, path generation turns them into more contours, and the G-code writer prints them. Closed loops do not repeat their first point at the end. Outer loops run counter-clockwise and holes clockwise once ContourGenerator has oriented them, so the material is always on the left.

Example Code

use Libraries.Fabrication.Geometry.Contour

class Main
    action Main
        Contour square
        square:AddPoint(0, 0)
        square:AddPoint(10, 0)
        square:AddPoint(10, 10)
        square:AddPoint(0, 10)
        square:closed = true
        output square:Area()                    // 100
        output square:ContainsPoint(5, 5)       // true
    end
end

Variables

  • integer wallIndex: Which wall this is, 0 being the outermost (PathGenerator); -1 if not a wall.
  • integer planeIndex: The slice plane (layer) this path lies on, counting from 0 at the bed.
  • boolean isHole: Which wall this is, 0 being the outermost (PathGenerator); -1 if not a wall.
  • Libraries.Containers.Array ys: How many loops of the same plane surround this one (ContourGenerator).
  • boolean closed: True for a loop whose last point connects back to its first.
  • Libraries.Containers.Array xs: True for a loop whose last point connects back to its first.
  • integer role: What the path prints as, one of the PathRole constants; -1 until PathOrderer sets it.
  • integer depth: How many loops of the same plane surround this one (ContourGenerator).
  • boolean keepShort: True for thin-island center lines and tip dots: PathOrderer keeps them even when they are shorter than its minimum line length, since they are the only thing printed for that island.

Actions

AddPoint(number x, number y)

Adds a point at the end, unless it is exactly the same as the last one.

Parameters

  • x number: The x coordinate (mm).
  • y number: The y coordinate (mm).

Example

use Libraries.Fabrication.Geometry.Contour

Contour path
path:AddPoint(0, 0)
path:AddPoint(10, 0)
path:AddPoint(10, 0)     // the same point again: not added
output path:GetSize()     // 2

AppendAll(Libraries.Fabrication.Geometry.Contour other)

Adds every point of another contour at the end (see AddPoint).

Parameters

Example

use Libraries.Fabrication.Geometry.Contour

Contour first
first:AddPoint(0, 0)
first:AddPoint(10, 0)
Contour second
second:AddPoint(10, 5)
second:AddPoint(0, 5)
first:AppendAll(second)
output first:GetSize()    // 4

Area()

The area enclosed by the loop.

Returns

number: The unsigned area (mm^2).

Example

use Libraries.Fabrication.Geometry.Contour

Contour square
square:AddPoint(0, 0)
square:AddPoint(20, 0)
square:AddPoint(20, 20)
square:AddPoint(0, 20)
square:closed = true
output square:Area()     // 400

ContainsPoint(number px, number py)

The even-odd point-in-polygon test (ray casting).

Parameters

  • px number: The x coordinate of the point.
  • py number: The y coordinate of the point.

Returns

boolean: True if the point is inside the loop; always false for fewer than 3 points.

Example

use Libraries.Fabrication.Geometry.Contour

Contour square
square:AddPoint(0, 0)
square:AddPoint(20, 0)
square:AddPoint(20, 20)
square:AddPoint(0, 20)
square:closed = true
output square:ContainsPoint(5, 5)        // true
output square:ContainsPoint(25, 5)       // false

Copy()

A copy with its own point lists and every field copied.

Returns

Libraries.Fabrication.Geometry.Contour: The copy.

Example

use Libraries.Fabrication.Geometry.Contour

Contour square
square:AddPoint(0, 0)
square:AddPoint(20, 0)
square:AddPoint(20, 20)
square:AddPoint(0, 20)
square:closed = true
Contour copy = square:Copy()
copy:Reverse()
output square:SignedArea2() > 0     // true: the original keeps its direction

DropClosingDuplicate()

Removes the last point if it repeats the first (a loop written closed).

Example

use Libraries.Fabrication.Geometry.Contour

Contour loop
loop:AddPoint(0, 0)
loop:AddPoint(10, 0)
loop:AddPoint(10, 10)
loop:AddPoint(0, 0)
loop:DropClosingDuplicate()
loop:closed = true
output loop:GetSize()     // 3

GetSize()

The number of points.

Returns

integer: The number of points.

Example

use Libraries.Fabrication.Geometry.Contour

Contour square
square:AddPoint(0, 0)
square:AddPoint(20, 0)
square:AddPoint(20, 20)
square:AddPoint(0, 20)
square:closed = true
output square:GetSize()       // 4

HeadX()

The x coordinate of the first point.

Returns

number: The x coordinate (mm); 0 for an empty contour.

Example

use Libraries.Fabrication.Geometry.Contour

Contour square
square:AddPoint(0, 0)
square:AddPoint(20, 0)
square:AddPoint(20, 20)
square:AddPoint(0, 20)
square:closed = true
output square:HeadX() + ", " + square:HeadY()      // 0, 0

HeadY()

The y coordinate of the first point.

Returns

number: The y coordinate (mm); 0 for an empty contour.

Example

use Libraries.Fabrication.Geometry.Contour

Contour line
line:AddPoint(1, 2)
line:AddPoint(5, 2)
output line:HeadY()     // 2

IsInside(Libraries.Fabrication.Geometry.Contour other)

True if this closed loop lies inside the other one. It is tested at a point just inside this loop, off the middle of its longest edge, rather than at a vertex: two loops that touch (two meshes side by side) share vertices, and a shared vertex can count as inside either loop.

Parameters

Returns

boolean: True if the test point is inside other; false if either has fewer than 3 points.

Example

use Libraries.Fabrication.Geometry.Contour

Contour square
square:AddPoint(0, 0)
square:AddPoint(20, 0)
square:AddPoint(20, 20)
square:AddPoint(0, 20)
square:closed = true
Contour hole
hole:AddPoint(5, 5)
hole:AddPoint(5, 15)
hole:AddPoint(15, 15)
hole:AddPoint(15, 5)
hole:closed = true
output hole:IsInside(square)     // true
output square:IsInside(hole)     // false

NearestVertexDistance2(number x, number y)

The squared distance from a point to the closest vertex.

Parameters

  • x number: The x coordinate of the point.
  • y number: The y coordinate of the point.

Returns

number: The squared distance; 0 for an empty contour.

Example

use Libraries.Fabrication.Geometry.Contour

Contour square
square:AddPoint(0, 0)
square:AddPoint(20, 0)
square:AddPoint(20, 20)
square:AddPoint(0, 20)
square:closed = true
output square:NearestVertexDistance2(23, 24)     // 25: 5 mm from (20, 20)

NearestVertexIndex(number x, number y)

The vertex closest to a point.

Parameters

  • x number: The x coordinate of the point.
  • y number: The y coordinate of the point.

Returns

integer: The index of the closest vertex; 0 for an empty contour.

Example

use Libraries.Fabrication.Geometry.Contour

Contour square
square:AddPoint(0, 0)
square:AddPoint(20, 0)
square:AddPoint(20, 20)
square:AddPoint(0, 20)
square:closed = true
output square:NearestVertexIndex(19, 18)       // 2, the corner at (20, 20)

Reverse()

Reverses the order of the points (and so the direction of a loop).

Example

use Libraries.Fabrication.Geometry.Contour

Contour square
square:AddPoint(0, 0)
square:AddPoint(20, 0)
square:AddPoint(20, 20)
square:AddPoint(0, 20)
square:closed = true
square:Reverse()
output square:SignedArea2() < 0     // true: now clockwise, like a hole

SignedArea2()

Twice the signed area of the loop (the shoelace sum).

Returns

number: Positive for a counter-clockwise loop, negative for a clockwise one.

Example

use Libraries.Fabrication.Geometry.Contour

Contour square
square:AddPoint(0, 0)
square:AddPoint(20, 0)
square:AddPoint(20, 20)
square:AddPoint(0, 20)
square:closed = true
output square:SignedArea2()      // 800: counter-clockwise

TailX()

The x coordinate of the last point.

Returns

number: The x coordinate (mm); 0 for an empty contour.

Example

use Libraries.Fabrication.Geometry.Contour

Contour line
line:AddPoint(1, 2)
line:AddPoint(5, 2)
output line:TailX()     // 5

TailY()

The y coordinate of the last point.

Returns

number: The y coordinate (mm); 0 for an empty contour.

Example

use Libraries.Fabrication.Geometry.Contour

Contour square
square:AddPoint(0, 0)
square:AddPoint(20, 0)
square:AddPoint(20, 20)
square:AddPoint(0, 20)
square:closed = true
output square:TailY()        // 20

Inherited Actions

3 actions from Libraries.Language.Object