Skip to content

Commit

Permalink
add mask border back
Browse files Browse the repository at this point in the history
  • Loading branch information
bobnik committed Sep 20, 2023
1 parent 18ff9f9 commit 82ae25d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/features/effects/Mask.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ const options = {
title: "Invert",
type: "checkbox",
},
maskBorder: {
title: "Draw border",
type: "checkbox",
}
}

export default class Mask extends Effect {
Expand Down Expand Up @@ -64,6 +68,7 @@ export default class Mask extends Effect {
maskMinimizeMoves: false,
maskMachine: "rectangle",
maskInvert: false,
maskBorder: false,
},
}
}
Expand Down Expand Up @@ -112,7 +117,7 @@ export default class Mask extends Effect {
maxRadius: effect.width / 2,
mask: true,
})
vertices = machine.polish(vertices)
vertices = machine.polish(vertices, { border: effect.maskBorder })
}

return vertices.map((vertex) => {
Expand Down
4 changes: 3 additions & 1 deletion src/features/machines/Machine.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { vertexRoundP, annotateVertices } from "@/common/geometry"
import { vertexRoundP, annotateVertices, downsample } from "@/common/geometry"

export const machineOptions = {
name: {
Expand Down Expand Up @@ -40,11 +40,13 @@ export default class Machine {
this.layerInfo = layerInfo

this.enforceLimits().cleanVertices().limitPrecision().optimizePerimeter()
if (this.layerInfo.border) this.outlinePerimeter()
if (this.layerInfo.start) this.addStartPoint()
if (this.layerInfo.end) this.addEndPoint()

// second call to limit precision for final cleanup
this.limitPrecision()

return this.vertices
}

Expand Down
20 changes: 19 additions & 1 deletion src/features/machines/PolarMachine.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { angle, onSegment, arc, annotateVertex } from "@/common/geometry"
import {
angle,
onSegment,
arc,
annotateVertex,
subsample,
circle,
} from "@/common/geometry"
import Victor from "victor"
import Machine, { machineOptions } from "./Machine"

Expand Down Expand Up @@ -143,6 +150,17 @@ export default class PolarMachine extends Machine {
return arc(this.state.maxRadius, start.angle(), end.angle())
}

outlinePerimeter() {
const last = this.vertices[this.vertices.length - 1]

if (last) {
this.vertices = this.vertices.concat(
circle(this.state.maxRadius, parseInt((last.angle() * 64) / Math.PI)),
)
}
return this
}

// Returns whether a given path lies on the perimeter of the circle.
onPerimeter(v1, v2, delta = 1) {
let rm = Math.pow(this.state.maxRadius, 2)
Expand Down
20 changes: 20 additions & 0 deletions src/features/machines/RectMachine.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,26 @@ export default class RectMachine extends Machine {
return (rDx < delta && dx < delta) || (rDy < delta && dy < delta)
}

outlinePerimeter() {
const last = this.vertices[this.vertices.length - 1]

if (last) {
const s = this.nearestPerimeterVertex(last)
const idx = this.nearestCornerIndex(s)
const corners = [
s,
cloneVertex(this.corners[idx]),
cloneVertex(this.corners[(idx + 1) % 4]),
cloneVertex(this.corners[(idx + 2) % 4]),
cloneVertex(this.corners[(idx + 3) % 4]),
cloneVertex(this.corners[idx]),
]
this.vertices = this.vertices.concat(corners)
}

return this
}

// Given two perimeter points, traces the shortest valid path between them (stays on
// perimeter). Returns a list of intermediate points on that path (if any).
// On further consideration, this could be redone using Dijsktra's algorithm, I believe,
Expand Down

0 comments on commit 82ae25d

Please sign in to comment.