Skip to content

Shapemetry API


Shapemetry API / PolyShape

Class: PolyShape

Defined in: polyShape/PolyShape.ts:46

A 2D region: an array of Shape2d rings plus a transformation. Backed by Clipper for boolean/offset. Outer ring CCW, holes CW.

Constructors

Constructor

new PolyShape(shapes, transformation?): PolyShape

Defined in: polyShape/PolyShape.ts:50

Parameters

shapes

Shape2d[]

transformation?

Transformation = ...

Returns

PolyShape

Properties

shapes

shapes: Shape2d[]

Defined in: polyShape/PolyShape.ts:47


transformation

transformation: Transformation

Defined in: polyShape/PolyShape.ts:48

Accessors

plane

Get Signature

get plane(): Plane

Defined in: polyShape/PolyShape.ts:56

Derived from transformation: origin + local-Z as world normal.

Returns

Plane


isClosed

Get Signature

get isClosed(): boolean

Defined in: polyShape/PolyShape.ts:63

Returns

boolean

Methods

boundingBox()

boundingBox(): BoundingBox2d

Defined in: polyShape/PolyShape.ts:65

Returns

BoundingBox2d


length()

length(): number

Defined in: polyShape/PolyShape.ts:72

Returns

number


transform()

transform(m): PolyShape

Defined in: polyShape/PolyShape.ts:79

2D transform applied in local frame; 3D transformation preserved.

Parameters

m

Transformation2d

Returns

PolyShape


reverse()

reverse(): PolyShape

Defined in: polyShape/PolyShape.ts:83

Returns

PolyShape


clone()

clone(): PolyShape

Defined in: polyShape/PolyShape.ts:87

Returns

PolyShape


toPolylines()

toPolylines(tol): Point2d[][]

Defined in: polyShape/PolyShape.ts:92

Sample each shape to a polyline (no first/last repeat).

Parameters

tol

number

Returns

Point2d[][]


toNurbsCurves()

toNurbsCurves(): NurbsCurve[]

Defined in: polyShape/PolyShape.ts:96

Returns

NurbsCurve[]


fillet()

fillet(radius): PolyShape

Defined in: polyShape/PolyShape.ts:100

Parameters

radius

number

Returns

PolyShape


chamfer()

chamfer(distance): PolyShape

Defined in: polyShape/PolyShape.ts:104

Parameters

distance

number

Returns

PolyShape


offset()

offset(distance): PolyShape

Defined in: polyShape/PolyShape.ts:108

Parameters

distance

number

Returns

PolyShape


thicken()

thicken(distance, joinType?, endType?): PolyShape

Defined in: polyShape/PolyShape.ts:112

Parameters

distance

number

joinType?

OffsetJoinType = 'round'

endType?

OffsetEndType = 'round'

Returns

PolyShape


addShape()

addShape(input): this

Defined in: polyShape/PolyShape.ts:123

Add a Shape2d, single segment, or polyline points (in-place). Polyline points auto-close when first/last coincide. Closed input → appended as its own Shape2d. Open input → merged with an existing open Shape2d that shares an endpoint (reversed if connected at opposite ends); otherwise appended as a new open Shape2d.

Parameters

input

Point2d[] | ShapeSegment2d | Shape2d

Returns

this


toRings()

toRings(tol?): Point2d[][]

Defined in: polyShape/PolyShape.ts:168

Sampled rings (closed; first/last do NOT repeat). For Clipper-ready data.

Parameters

tol?

number = 0.5

Returns

Point2d[][]


toRegions()

toRegions(tol?): Region

Defined in: polyShape/PolyShape.ts:173

Group sampled rings into polygons (outer + holes) by clipper orientation.

Parameters

tol?

number = 0.5

Returns

Region


area()

area(tol?): number

Defined in: polyShape/PolyShape.ts:215

Even-odd fill area of the sampled rings: a ring nested inside an ODD number of other rings is a hole and SUBTRACTS, an even nesting depth ADDS. This is the same even-odd rule containsPoint/triangulate use, so all three agree on identical geometry — including same-winding nested rings, where the old |Σ signedArea| summed magnitudes instead of alternating (a hole drawn with the same orientation as its outer was added, not subtracted). Winding is irrelevant under even-odd; depth parity decides the sign. (signedArea keeps the raw winding-sum for callers that want the non-zero rule.)

Parameters

tol?

number = 0.5

Returns

number


signedArea()

signedArea(tol?): number

Defined in: polyShape/PolyShape.ts:244

Sum of signed areas of sampled rings.

Parameters

tol?

number = 0.5

Returns

number


containsPoint()

containsPoint(p, tol?): boolean

Defined in: polyShape/PolyShape.ts:251

Point-in-region test (even-odd) on sampled rings.

Parameters

p
x

number

y

number

tol?

number = 0.5

Returns

boolean


boolean()

boolean(other, op, tol?): PolyShape

Defined in: polyShape/PolyShape.ts:270

Parameters

other

PolyShape

op

BooleanOp

tol?

number = 0.5

Returns

PolyShape


union()

union(other, tol?): PolyShape

Defined in: polyShape/PolyShape.ts:308

Parameters

other

PolyShape

tol?

number = 0.5

Returns

PolyShape


subtract()

subtract(other, tol?): PolyShape

Defined in: polyShape/PolyShape.ts:309

Parameters

other

PolyShape

tol?

number = 0.5

Returns

PolyShape


intersect()

intersect(other, tol?): PolyShape

Defined in: polyShape/PolyShape.ts:310

Parameters

other

PolyShape

tol?

number = 0.5

Returns

PolyShape


xor()

xor(other, tol?): PolyShape

Defined in: polyShape/PolyShape.ts:311

Parameters

other

PolyShape

tol?

number = 0.5

Returns

PolyShape


cleanup()

cleanup(tol?): PolyShape

Defined in: polyShape/PolyShape.ts:314

Self-union: collapse overlapping rings into clean outer/hole topology.

Parameters

tol?

number = 0.5

Returns

PolyShape


splitByLine()

splitByLine(origin, direction, keepSide?, tol?): PolyShape[]

Defined in: polyShape/PolyShape.ts:326

Split each polygon by an infinite line. Returns split pieces (optionally filtered by which side of the line they fall on).

Parameters

origin
x

number

y

number

direction
x

number

y

number

keepSide?

"left" | "right"

tol?

number = 0.5

Returns

PolyShape[]


triangulate()

triangulate(tol?): object

Defined in: polyShape/PolyShape.ts:431

Group + poly2tri CDT. Returns flat vertex array and index buffer (uses sampled rings).

Parameters

tol?

number = 0.5

Returns

object

vertices

vertices: number[]

indices

indices: number[]


toJSON()

toJSON(): PolyShapeJSON

Defined in: polyShape/PolyShape.ts:452

Returns

PolyShapeJSON


fromJSON()

static fromJSON(d): PolyShape

Defined in: polyShape/PolyShape.ts:458

Parameters

d

PolyShapeJSON

Returns

PolyShape


fromPolyline()

static fromPolyline(points, transformation?): PolyShape

Defined in: polyShape/PolyShape.ts:466

Polyline points. Auto-closes when first/last coincide (within 1e-6).

Parameters

points

Point2d[]

transformation?

Transformation

Returns

PolyShape


rectangle()

static rectangle(center, width, height, transformation?): PolyShape

Defined in: polyShape/PolyShape.ts:477

Axis-aligned rectangle centered at center.

Parameters

center

Point2d

width

number

height

number

transformation?

Transformation

Returns

PolyShape


regularPolygon()

static regularPolygon(center, radius, sides, transformation?): PolyShape

Defined in: polyShape/PolyShape.ts:489

Regular N-gon inscribed in circle of radius. First vertex at angle 0.

Parameters

center

Point2d

radius

number

sides

number

transformation?

Transformation

Returns

PolyShape


circle()

static circle(center, radius, transformation?): PolyShape

Defined in: polyShape/PolyShape.ts:500

Full circle as a single closed Ellipse2d (sweep = 2π).

Parameters

center

Point2d

radius

number

transformation?

Transformation

Returns

PolyShape


ellipse()

static ellipse(center, xAxis, yAxis, transformation?): PolyShape

Defined in: polyShape/PolyShape.ts:509

Ellipse centered at center with axes vectors xAxis, yAxis (allows skew). Built as 4 cubic Béziers with constant k = 0.5522847498307936.

Parameters

center

Point2d

xAxis
x

number

y

number

yAxis
x

number

y

number

transformation?

Transformation

Returns

PolyShape


fromRings()

static fromRings(rings, transformation?): PolyShape

Defined in: polyShape/PolyShape.ts:524

Parameters

rings

Point2d[][]

transformation?

Transformation

Returns

PolyShape


fromRegions()

static fromRegions(regions, transformation?): PolyShape

Defined in: polyShape/PolyShape.ts:533

Parameters

regions

Region

transformation?

Transformation

Returns

PolyShape


union()

static union(regions, tol?): PolyShape

Defined in: polyShape/PolyShape.ts:543

Union of multiple regions. Uses the first region's transformation.

Parameters

regions

PolyShape[]

tol?

number = 0.5

Returns

PolyShape