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-07-04 23:23:14 +02:00
|
|
|
const Point = require('./point')
|
2020-06-02 22:59:05 +02:00
|
|
|
|
2020-07-03 22:29:39 +02:00
|
|
|
const rectangle = (w, h, corner, bevel, name='') => {
|
2020-07-10 22:40:34 +02:00
|
|
|
const error = (dim, val) => `Rectangle for "${name}" isn't ${dim} enough for its corner and bevel (${val} - 2 * ${corner} - 2 * ${bevel} <= 0)!`
|
|
|
|
|
const mod = 2 * (corner + bevel)
|
|
|
|
|
const cw = w - mod
|
2020-07-03 22:29:39 +02:00
|
|
|
a.assert(cw >= 0, error('wide', w))
|
2020-07-10 22:40:34 +02:00
|
|
|
const ch = h - mod
|
2020-07-03 22:29:39 +02:00
|
|
|
a.assert(ch >= 0, error('tall', h))
|
|
|
|
|
|
2020-07-10 22:40:34 +02:00
|
|
|
let res = new m.models.Rectangle(cw, ch)
|
|
|
|
|
if (bevel > 0) res = m.model.outline(res, bevel, 2)
|
|
|
|
|
if (corner > 0) res = m.model.outline(res, corner, 0)
|
2020-07-03 22:29:39 +02:00
|
|
|
return m.model.moveRelative(res, [corner + bevel, corner + bevel])
|
2020-06-28 22:35:53 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-10 22:40:34 +02:00
|
|
|
const relative_anchor = (decl, name, points={}, check_unexpected=true, default_point=new Point()) => {
|
|
|
|
|
decl.shift = a.wh(decl.shift || [0, 0], name + '.shift')
|
|
|
|
|
const relative = a.sane(decl.relative === undefined ? true : decl.relative, `${name}.relative`, 'boolean')
|
|
|
|
|
delete decl.relative
|
|
|
|
|
if (relative) {
|
|
|
|
|
return size => {
|
|
|
|
|
const copy = u.deepcopy(decl)
|
|
|
|
|
copy.shift = [copy.shift[0] * size[0], copy.shift[1] * size[1]]
|
|
|
|
|
return a.anchor(copy, name, points, check_unexpected, default_point)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return () => a.anchor(decl, name, points, check_unexpected, default_point)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const layout = exports._layout = (config = {}, points = {}) => {
|
|
|
|
|
|
|
|
|
|
// Glue config sanitization
|
2020-06-28 22:35:53 +02:00
|
|
|
|
|
|
|
|
a.detect_unexpected(config, 'outline.glue', ['top', 'bottom', 'waypoints', 'extra'])
|
|
|
|
|
|
2020-07-10 22:40:34 +02:00
|
|
|
|
|
|
|
|
|
2020-07-04 23:23:14 +02:00
|
|
|
for (const y of ['top', 'bottom']) {
|
2020-06-28 22:35:53 +02:00
|
|
|
a.detect_unexpected(config[y], `outline.glue.${y}`, ['left', 'right'])
|
2020-07-10 22:40:34 +02:00
|
|
|
config[y].left = relative_anchor(config[y].left, `outline.glue.${y}.left`, points)
|
2020-06-28 22:35:53 +02:00
|
|
|
if (a.type(config[y].right) != 'number') {
|
2020-07-10 22:40:34 +02:00
|
|
|
config[y].right = relative_anchor(config[y].right, `outline.glue.${y}.right`, points)
|
2020-06-28 22:35:53 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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)
|
|
|
|
|
|
2020-07-04 23:23:14 +02:00
|
|
|
return (params, export_name, expected) => {
|
2020-06-30 22:29:14 +02:00
|
|
|
|
2020-07-10 22:40:34 +02:00
|
|
|
// Layout params sanitization
|
|
|
|
|
|
|
|
|
|
a.detect_unexpected(params, `${export_name}`, expected.concat(['side', 'size', 'corner', 'bevel', 'bound']))
|
2020-07-03 22:29:39 +02:00
|
|
|
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')
|
2020-07-10 22:40:34 +02:00
|
|
|
const bound = a.sane(params.bound === undefined ? true : params.bound, `${export_name}.bound`, 'boolean')
|
|
|
|
|
|
|
|
|
|
// Actual layout
|
2020-07-03 22:29:39 +02:00
|
|
|
|
2020-07-04 23:23:14 +02:00
|
|
|
let left = {models: {}}
|
|
|
|
|
let right = {models: {}}
|
|
|
|
|
if (['left', 'right', 'middle', 'both'].includes(side)) {
|
2020-07-03 22:29:39 +02:00
|
|
|
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
|
|
|
|
|
|
2020-07-10 22:40:34 +02:00
|
|
|
// the original position
|
2020-07-03 22:29:39 +02:00
|
|
|
let rect = rectangle(to_x - from_x, to_y - from_y, corner, bevel, `${export_name}.size`)
|
2020-07-10 22:40:34 +02:00
|
|
|
rect = m.model.moveRelative(rect, [from_x, from_y])
|
|
|
|
|
|
|
|
|
|
// extra binding "material", if necessary
|
|
|
|
|
if (bound) {
|
|
|
|
|
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]]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const bt = to_y + Math.max(bind[0], 0)
|
|
|
|
|
const br = to_x + Math.max(bind[1], 0)
|
|
|
|
|
const bd = from_y - Math.max(bind[2], 0)
|
|
|
|
|
const bl = from_x - Math.max(bind[3], 0)
|
|
|
|
|
|
|
|
|
|
if (bind[0] || bind[1]) rect = u.union(rect, u.rect(br, bt))
|
|
|
|
|
if (bind[1] || bind[2]) rect = u.union(rect, u.rect(br, -bd, [0, bd]))
|
|
|
|
|
if (bind[2] || bind[3]) rect = u.union(rect, u.rect(-bl, -bd, [bl, bd]))
|
|
|
|
|
if (bind[3] || bind[0]) rect = u.union(rect, u.rect(-bl, bt, [bl, 0]))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// positioning and unioning the resulting shape
|
2020-07-03 22:29:39 +02:00
|
|
|
rect = p.position(rect)
|
|
|
|
|
if (p.meta.mirrored) {
|
2020-07-10 22:40:34 +02:00
|
|
|
right = u.union(right, rect)
|
2020-07-03 22:29:39 +02:00
|
|
|
} else {
|
2020-07-10 22:40:34 +02:00
|
|
|
left = u.union(left, rect)
|
2020-07-03 22:29:39 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-04 23:23:14 +02:00
|
|
|
if (side == 'left') return left
|
|
|
|
|
if (side == 'right') return right
|
2020-06-30 22:29:14 +02:00
|
|
|
|
2020-07-04 23:23:14 +02:00
|
|
|
let glue = {models: {}}
|
2020-07-10 22:40:34 +02:00
|
|
|
if (bound && ['middle', 'both', 'glue'].includes(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-07-10 22:40:34 +02:00
|
|
|
// if it wasn't a number, then it's a function returning an achor
|
|
|
|
|
// have to feed it `size` first in case it's relative
|
|
|
|
|
const from = anchor(size).clone()
|
|
|
|
|
const to = from.clone().shift([from.meta.mirrored ? -1 : 1, 0])
|
|
|
|
|
|
|
|
|
|
return u.line(from.p, to.p)
|
2020-06-30 22:29:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 = []
|
2020-07-10 22:40:34 +02:00
|
|
|
|
2020-06-30 22:29:14 +02:00
|
|
|
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])
|
2020-07-10 22:40:34 +02:00
|
|
|
const left_x = center_x - w.width[0]
|
|
|
|
|
const right_x = center_x + w.width[1]
|
2020-06-30 22:29:14 +02:00
|
|
|
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)
|
|
|
|
|
}
|
2020-07-10 22:40:34 +02:00
|
|
|
|
2020-06-30 22:29:14 +02:00
|
|
|
glue = u.poly(waypoints)
|
|
|
|
|
}
|
2020-07-04 23:23:14 +02:00
|
|
|
if (side == 'glue') return glue
|
2020-06-30 22:29:14 +02:00
|
|
|
|
2020-07-10 22:40:34 +02:00
|
|
|
if (side == 'middle') {
|
|
|
|
|
let middle = u.subtract(glue, left)
|
|
|
|
|
middle = u.subtract(middle, right)
|
|
|
|
|
return middle
|
|
|
|
|
}
|
2020-06-28 22:35:53 +02:00
|
|
|
|
2020-07-10 22:40:34 +02:00
|
|
|
let both = u.union(u.deepcopy(left), glue)
|
|
|
|
|
both = u.union(both, u.deepcopy(right))
|
|
|
|
|
return both
|
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'])
|
2020-07-10 22:40:34 +02:00
|
|
|
const layout_fn = layout(config.glue, points)
|
2020-06-30 22:29:14 +02:00
|
|
|
|
2020-07-04 23:23:14 +02:00
|
|
|
const outlines = {}
|
|
|
|
|
|
|
|
|
|
const ex = a.sane(config.exports, 'outline.exports', 'object')
|
|
|
|
|
for (const [key, parts] of Object.entries(ex)) {
|
2020-07-03 22:29:39 +02:00
|
|
|
let index = 0
|
2020-07-04 23:23:14 +02:00
|
|
|
let result = {models: {}}
|
2020-07-03 22:29:39 +02:00
|
|
|
for (const part of parts) {
|
|
|
|
|
const name = `outline.exports.${key}[${++index}]`
|
2020-07-04 23:23:14 +02:00
|
|
|
const expected = ['type', 'operation']
|
2020-07-03 22:29:39 +02:00
|
|
|
part.type = a.in(part.type, `${name}.type`, ['keys', 'rectangle', 'circle', 'polygon', 'ref'])
|
2020-07-10 22:40:34 +02:00
|
|
|
part.operation = a.in(part.operation || 'add', `${name}.operation`, ['add', 'subtract', 'intersect', 'stack'])
|
2020-07-04 23:23:14 +02:00
|
|
|
|
2020-07-10 22:40:34 +02:00
|
|
|
let op = u.union
|
|
|
|
|
if (part.operation == 'subtract') op = u.subtract
|
|
|
|
|
else if (part.operation == 'intersect') op = u.intersect
|
|
|
|
|
else if (part.operation == 'stack') op = u.stack
|
2020-07-04 23:23:14 +02:00
|
|
|
|
|
|
|
|
let arg
|
|
|
|
|
let anchor
|
|
|
|
|
switch (part.type) {
|
|
|
|
|
case 'keys':
|
2020-07-10 22:40:34 +02:00
|
|
|
arg = layout_fn(part, name, expected)
|
2020-07-04 23:23:14 +02:00
|
|
|
break
|
|
|
|
|
case 'rectangle':
|
|
|
|
|
a.detect_unexpected(part, name, expected.concat(['ref', 'shift', 'rotate', 'size', 'corner', 'bevel']))
|
|
|
|
|
anchor = a.anchor(part, name, points, false)
|
|
|
|
|
const size = a.wh(part.size, `${name}.size`)
|
|
|
|
|
const corner = a.sane(part.corner || 0, `${name}.corner`, 'number')
|
|
|
|
|
const bevel = a.sane(part.bevel || 0, `${name}.bevel`, 'number')
|
|
|
|
|
arg = rectangle(size[0], size[1], corner, bevel, name)
|
|
|
|
|
arg = anchor.position(arg)
|
|
|
|
|
break
|
|
|
|
|
case 'circle':
|
|
|
|
|
a.detect_unexpected(part, name, expected.concat(['ref', 'shift', 'rotate', 'radius']))
|
|
|
|
|
anchor = a.anchor(part, name, points, false)
|
|
|
|
|
const radius = a.sane(part.radius, `${name}.radius`, 'number')
|
|
|
|
|
arg = u.circle(anchor.p, radius)
|
|
|
|
|
break
|
|
|
|
|
case 'polygon':
|
|
|
|
|
a.detect_unexpected(part, name, expected.concat(['points']))
|
|
|
|
|
const poly_points = a.sane(part.points, `${name}.points`, 'array')
|
|
|
|
|
const parsed_points = []
|
|
|
|
|
let last_anchor = new Point()
|
|
|
|
|
let poly_index = 0
|
|
|
|
|
for (const poly_point of poly_points) {
|
|
|
|
|
const poly_name = `${name}.points[${++poly_index}]`
|
2020-07-10 22:40:34 +02:00
|
|
|
const anchor = a.anchor(poly_point, poly_name, points, true, last_anchor)
|
2020-07-04 23:23:14 +02:00
|
|
|
parsed_points.push(anchor.p)
|
|
|
|
|
}
|
|
|
|
|
arg = u.poly(parsed_points)
|
|
|
|
|
break
|
|
|
|
|
case 'ref':
|
|
|
|
|
a.assert(outlines[part.name], `Field "${name}.name" does not name an existing outline!`)
|
2020-07-10 22:40:34 +02:00
|
|
|
arg = u.deepcopy(outlines[part.name])
|
2020-07-04 23:23:14 +02:00
|
|
|
break
|
|
|
|
|
}
|
2020-07-03 22:29:39 +02:00
|
|
|
|
2020-07-04 23:23:14 +02:00
|
|
|
result = op(result, arg)
|
2020-07-03 22:29:39 +02:00
|
|
|
}
|
2020-07-04 23:23:14 +02:00
|
|
|
|
|
|
|
|
outlines[key] = result
|
2020-06-30 22:29:14 +02:00
|
|
|
}
|
2020-07-04 23:23:14 +02:00
|
|
|
|
|
|
|
|
return outlines
|
2020-06-28 22:35:53 +02:00
|
|
|
}
|