ergogen/src/outline.js

164 lines
6.4 KiB
JavaScript
Raw Normal View History

2020-05-30 15:17:53 +02:00
const m = require('makerjs')
2020-05-31 22:59:51 +02:00
const u = require('./utils')
2020-06-28 22:35:53 +02:00
const a = require('./assert')
2020-06-02 22:59:05 +02:00
2020-07-03 22:29:39 +02:00
const rectangle = (w, h, corner, bevel, name='') => {
const error = (dim, val) => `Rectangle for "${name}" isn't ${dim} enough for its corner and bevel (${val} - ${corner} - ${bevel} <= 0)!`
const cw = w - corner - bevel
a.assert(cw >= 0, error('wide', w))
ch = h - corner - bevel
a.assert(ch >= 0, error('tall', h))
let res = m.models.Rectangle(w, h)
res = m.model.outline(res, bevel, 2)
res = m.model.outline(res, corner, 0)
return m.model.moveRelative(res, [corner + bevel, corner + bevel])
2020-06-28 22:35:53 +02:00
}
const parse_glue = exports._parse_glue = (config = {}, points = {}) => {
a.detect_unexpected(config, 'outline.glue', ['top', 'bottom', 'waypoints', 'extra'])
for (const y in ['top', 'bottom']) {
a.detect_unexpected(config[y], `outline.glue.${y}`, ['left', 'right'])
config[y].left = a.anchor(config[y].left, `outline.glue.${y}.left`, points)
if (a.type(config[y].right) != 'number') {
config[y].right = a.anchor(config[y].right, `outline.glue.${y}.right`, points)
}
}
2020-06-30 22:29:14 +02:00
config.waypoints = a.sane(config.waypoints || [], 'outline.glue.waypoints', 'array')
let wi = 0
config.waypoints = config.waypoints.map(w => {
const name = `outline.glue.waypoints[${++wi}]`
a.detect_unexpected(w, name, ['percent', 'width'])
w.percent = a.sane(w.percent, name + '.percent', 'number')
w.width = a.wh(w.width, name + '.width')
return w
})
// TODO: handle glue.extra (or revoke it from the docs)
return (export_name, params) => {
2020-07-03 22:29:39 +02:00
a.detect_unexpected(params, `${export_name}`, ['side', 'size', 'corner', 'bevel'])
const side = a.in(params.side, `${export_name}.side`, ['left', 'right', 'middle', 'both', 'glue'])
const size = a.wh(params.size, `${export_name}.size`)
const corner = a.sane(params.corner || 0, `${export_name}.corner`, 'number')
const bevel = a.sane(params.bevel || 0, `${export_name}.bevel`, 'number')
let left = {paths: {}}
let right = {paths: {}}
if (['left', 'right', 'middle', 'both'].includes(params.side)) {
for (const [pname, p] of Object.entries(points)) {
let from_x = -size[0] / 2, to_x = size[0] / 2
let from_y = -size[1] / 2, to_y = size[1] / 2
let bind = a.trbl(p.meta.bind || 0, `${pname}.bind`)
// if it's a mirrored key, we swap the left and right bind values
if (p.meta.mirrored) {
bind = [bind[0], bind[3], bind[2], bind[1]]
}
from_x -= bind[3]
to_x += bind[1]
from_y -= bind[2]
to_y += bind[0]
let rect = rectangle(to_x - from_x, to_y - from_y, corner, bevel, `${export_name}.size`)
rect = m.model.move(rect, [from_x, from_y])
rect = p.position(rect)
if (p.meta.mirrored) {
right = m.model.combineUnion(right, rect)
} else {
left = m.model.combineUnion(left, rect)
}
}
}
if (params.side == 'left') return left
if (params.side == 'right') return right
2020-06-30 22:29:14 +02:00
2020-07-03 22:29:39 +02:00
let glue = {paths: {}}
if (['middle', 'both', 'glue'].includes(params.side)) {
2020-06-30 22:29:14 +02:00
const get_line = (anchor) => {
if (a.type(anchor) == 'number') {
return u.line([anchor, -1000], [anchor, 1000])
}
2020-06-28 22:35:53 +02:00
2020-06-30 22:29:14 +02:00
let from = anchor.clone()
let to = anchor.add([anchor.meta.mirrored ? -1 : 1, 0])
to = to.rotate(anchor.r, anchor.p).p
2020-06-28 22:35:53 +02:00
2020-06-30 22:29:14 +02:00
return u.line(from, to)
}
const tll = get_line(config.top.left)
const trl = get_line(config.top.right)
const tip = m.path.converge(tll, trl)
const tlp = u.eq(tll.origin, tip) ? tll.end : tll.origin
const trp = u.eq(trl.origin, tip) ? trl.end : trl.origin
const bll = get_line(config.bottom.left)
const brl = get_line(config.bottom.right)
const bip = m.path.converge(bll, brl)
const blp = u.eq(bll.origin, bip) ? bll.end : bll.origin
const brp = u.eq(brl.origin, bip) ? brl.end : brl.origin
const left_waypoints = []
const right_waypoints = []
for (const w of config.waypoints) {
const percent = w.percent / 100
const center_x = tip[0] + percent * (bip[0] - tip[0])
const center_y = tip[1] + percent * (bip[1] - tip[1])
const left_x = center_x - (w.left || w.width / 2)
const right_x = center_x + (w.right || w.width / 2)
left_waypoints.push([left_x, center_y])
right_waypoints.unshift([right_x, center_y])
}
let waypoints
const is_split = a.type(config.top.right) == 'number'
if (is_split) {
waypoints = [tip, tlp]
.concat(left_waypoints)
.concat([blp, bip])
} else {
waypoints = [trp, tip, tlp]
.concat(left_waypoints)
.concat([blp, bip, brp])
.concat(right_waypoints)
}
glue = u.poly(waypoints)
}
2020-07-03 22:29:39 +02:00
if (params.side == 'glue') return glue
2020-06-30 22:29:14 +02:00
2020-07-03 22:29:39 +02:00
let both = m.model.combineUnion(u.deepcopy(left), glue)
both = m.model.combineUnion(both, u.deepcopy(right))
if (params.side == 'both') return both
2020-06-28 22:35:53 +02:00
2020-07-03 22:29:39 +02:00
let middle = m.model.combineSubtraction(both, left)
middle = m.model.combineSubtraction(both, right)
return middle
2020-06-30 22:29:14 +02:00
}
2020-06-28 22:35:53 +02:00
}
exports.parse = (config = {}, points = {}) => {
a.detect_unexpected(config, 'outline', ['glue', 'exports'])
const glue = parse_glue(config.glue, points)
2020-06-30 22:29:14 +02:00
config = a.sane(config, 'outline.exports', 'object')
2020-07-03 22:29:39 +02:00
for (const [key, parts] of Object.entries(config)) {
let index = 0
for (const part of parts) {
const name = `outline.exports.${key}[${++index}]`
part.op = a.in(part.op || 'add', `${name}.op`, ['add', 'sub', 'diff'])
part.type = a.in(part.type, `${name}.type`, ['keys', 'rectangle', 'circle', 'polygon', 'ref'])
}
2020-06-30 22:29:14 +02:00
}
2020-06-28 22:35:53 +02:00
}