Refactor, units, tests
This commit is contained in:
parent
519c34bc60
commit
cd90705ba1
24 changed files with 1221 additions and 1222 deletions
107
src/outlines.js
107
src/outlines.js
|
@ -1,7 +1,10 @@
|
|||
const m = require('makerjs')
|
||||
const u = require('./utils')
|
||||
const a = require('./assert')
|
||||
const o = require('./operation')
|
||||
const Point = require('./point')
|
||||
const prep = require('./prepare')
|
||||
const make_anchor = require('./anchor')
|
||||
|
||||
const rectangle = (w, h, corner, bevel, name='') => {
|
||||
const error = (dim, val) => `Rectangle for "${name}" isn't ${dim} enough for its corner and bevel (${val} - 2 * ${corner} - 2 * ${bevel} <= 0)!`
|
||||
|
@ -17,44 +20,29 @@ const rectangle = (w, h, corner, bevel, name='') => {
|
|||
return m.model.moveRelative(res, [corner + bevel, corner + bevel])
|
||||
}
|
||||
|
||||
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 = {}) => {
|
||||
const layout = exports._layout = (config = {}, points = {}, units = {}) => {
|
||||
|
||||
// Glue config sanitization
|
||||
|
||||
const parsed_glue = u.deepcopy(a.sane(config, 'outlines.glue', 'object'))
|
||||
const parsed_glue = u.deepcopy(a.sane(config, 'outlines.glue', 'object')())
|
||||
for (let [gkey, gval] of Object.entries(parsed_glue)) {
|
||||
gval = a.inherit('outlines.glue', gkey, config)
|
||||
a.detect_unexpected(gval, `outlines.glue.${gkey}`, ['top', 'bottom', 'waypoints', 'extra'])
|
||||
|
||||
for (const y of ['top', 'bottom']) {
|
||||
a.detect_unexpected(gval[y], `outlines.glue.${gkey}.${y}`, ['left', 'right'])
|
||||
gval[y].left = relative_anchor(gval[y].left, `outlines.glue.${gkey}.${y}.left`, points)
|
||||
if (a.type(gval[y].right) != 'number') {
|
||||
gval[y].right = relative_anchor(gval[y].right, `outlines.glue.${gkey}.${y}.right`, points)
|
||||
gval[y].left = make_anchor(gval[y].left, `outlines.glue.${gkey}.${y}.left`, points)
|
||||
if (a.type(gval[y].right)(units) != 'number') {
|
||||
gval[y].right = make_anchor(gval[y].right, `outlines.glue.${gkey}.${y}.right`, points)
|
||||
}
|
||||
}
|
||||
|
||||
gval.waypoints = a.sane(gval.waypoints || [], `outlines.glue.${gkey}.waypoints`, 'array')
|
||||
gval.waypoints = a.sane(gval.waypoints || [], `outlines.glue.${gkey}.waypoints`, 'array')(units)
|
||||
let wi = 0
|
||||
gval.waypoints = gval.waypoints.map(w => {
|
||||
const name = `outlines.glue.${gkey}.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')
|
||||
w.percent = a.sane(w.percent, name + '.percent', 'number')(units)
|
||||
w.width = a.wh(w.width, name + '.width')(units)
|
||||
return w
|
||||
})
|
||||
|
||||
|
@ -69,12 +57,19 @@ const layout = exports._layout = (config = {}, points = {}) => {
|
|||
// Layout params sanitization
|
||||
|
||||
a.detect_unexpected(params, `${export_name}`, expected.concat(['side', 'tags', 'glue', 'size', 'corner', 'bevel', 'bound']))
|
||||
const size = a.wh(params.size, `${export_name}.size`)(units)
|
||||
const relative_units = prep.extend({
|
||||
sx: size[0],
|
||||
sy: size[1]
|
||||
}, units)
|
||||
|
||||
|
||||
|
||||
const side = a.in(params.side, `${export_name}.side`, ['left', 'right', 'middle', 'both', 'glue'])
|
||||
const tags = a.sane(params.tags || [], `${export_name}.tags`, 'array')
|
||||
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')
|
||||
const bound = a.sane(params.bound === undefined ? true : params.bound, `${export_name}.bound`, 'boolean')
|
||||
const tags = a.sane(params.tags || [], `${export_name}.tags`, 'array')()
|
||||
const corner = a.sane(params.corner || 0, `${export_name}.corner`, 'number')(relative_units)
|
||||
const bevel = a.sane(params.bevel || 0, `${export_name}.bevel`, 'number')(relative_units)
|
||||
const bound = a.sane(params.bound === undefined ? true : params.bound, `${export_name}.bound`, 'boolean')()
|
||||
|
||||
// Actual layout
|
||||
|
||||
|
@ -100,7 +95,7 @@ const layout = exports._layout = (config = {}, points = {}) => {
|
|||
|
||||
// extra binding "material", if necessary
|
||||
if (bound) {
|
||||
let bind = a.trbl(p.meta.bind || 0, `${pname}.bind`)
|
||||
let bind = a.trbl(p.meta.bind || 0, `${pname}.bind`)(relative_units)
|
||||
// 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]]
|
||||
|
@ -133,18 +128,17 @@ const layout = exports._layout = (config = {}, points = {}) => {
|
|||
if (bound && ['middle', 'both', 'glue'].includes(side)) {
|
||||
|
||||
const default_glue_name = Object.keys(parsed_glue)[0]
|
||||
const computed_glue_name = a.sane(params.glue || default_glue_name, `${export_name}.glue`, 'string')
|
||||
const computed_glue_name = a.sane(params.glue || default_glue_name, `${export_name}.glue`, 'string')()
|
||||
const glue_def = parsed_glue[computed_glue_name]
|
||||
a.assert(glue_def, `Field "${export_name}.glue" does not name a valid glue!`)
|
||||
|
||||
const get_line = (anchor) => {
|
||||
if (a.type(anchor) == 'number') {
|
||||
if (a.type(anchor)(relative_units) == 'number') {
|
||||
return u.line([anchor, -1000], [anchor, 1000])
|
||||
}
|
||||
|
||||
// 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()
|
||||
// if it wasn't a number, then it's a (possibly relative) achor
|
||||
const from = anchor(relative_units).clone()
|
||||
const to = from.clone().shift([from.meta.mirrored ? -1 : 1, 0])
|
||||
|
||||
return u.line(from.p, to.p)
|
||||
|
@ -182,7 +176,7 @@ const layout = exports._layout = (config = {}, points = {}) => {
|
|||
}
|
||||
|
||||
let waypoints
|
||||
const is_split = a.type(glue_def.top.right) == 'number'
|
||||
const is_split = a.type(glue_def.top.right)(relative_units) == 'number'
|
||||
if (is_split) {
|
||||
waypoints = [tip, tlp]
|
||||
.concat(left_waypoints)
|
||||
|
@ -210,24 +204,23 @@ const layout = exports._layout = (config = {}, points = {}) => {
|
|||
}
|
||||
}
|
||||
|
||||
exports.parse = (config = {}, points = {}) => {
|
||||
exports.parse = (config = {}, points = {}, units = {}) => {
|
||||
a.detect_unexpected(config, 'outline', ['glue', 'exports'])
|
||||
const layout_fn = layout(config.glue, points)
|
||||
const layout_fn = layout(config.glue, points, units)
|
||||
|
||||
const outlines = {}
|
||||
|
||||
const ex = a.sane(config.exports || {}, 'outlines.exports', 'object')
|
||||
const ex = a.sane(config.exports || {}, 'outlines.exports', 'object')()
|
||||
for (let [key, parts] of Object.entries(ex)) {
|
||||
parts = a.inherit('outlines.exports', key, ex)
|
||||
if (a.type(parts) == 'array') {
|
||||
if (a.type(parts)() == 'array') {
|
||||
parts = {...parts}
|
||||
}
|
||||
parts = a.sane(parts, `outlines.exports.${key}`, 'object')
|
||||
parts = a.sane(parts, `outlines.exports.${key}`, 'object')()
|
||||
let result = {models: {}}
|
||||
for (let [part_name, part] of Object.entries(parts)) {
|
||||
const name = `outlines.exports.${key}.${part_name}`
|
||||
if (a.type(part) == 'string') {
|
||||
part = a.op_str(part, {outline: Object.keys(outlines)})
|
||||
if (a.type(part)() == 'string') {
|
||||
part = o.operation(part, {outline: Object.keys(outlines)})
|
||||
}
|
||||
const expected = ['type', 'operation']
|
||||
part.type = a.in(part.type || 'outline', `${name}.type`, ['keys', 'rectangle', 'circle', 'polygon', 'outline'])
|
||||
|
@ -246,45 +239,49 @@ exports.parse = (config = {}, points = {}) => {
|
|||
break
|
||||
case 'rectangle':
|
||||
a.detect_unexpected(part, name, expected.concat(['ref', 'shift', 'rotate', 'size', 'corner', 'bevel', 'mirror']))
|
||||
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')
|
||||
const rect_mirror = a.sane(part.mirror || false, `${name}.mirror`, 'boolean')
|
||||
const size = a.wh(part.size, `${name}.size`)(units)
|
||||
const rec_units = prep.extend({
|
||||
sx: size[0],
|
||||
sy: size[1]
|
||||
}, units)
|
||||
anchor = a.anchor(part, name, points, false)(rec_units)
|
||||
const corner = a.sane(part.corner || 0, `${name}.corner`, 'number')(rec_units)
|
||||
const bevel = a.sane(part.bevel || 0, `${name}.bevel`, 'number')(rec_units)
|
||||
const rect_mirror = a.sane(part.mirror || false, `${name}.mirror`, 'boolean')()
|
||||
const rect = rectangle(size[0], size[1], corner, bevel, name)
|
||||
arg = anchor.position(u.deepcopy(rect))
|
||||
if (rect_mirror) {
|
||||
const mirror_part = u.deepcopy(part)
|
||||
a.assert(mirror_part.ref, `Field "${name}.ref" must be speficied if mirroring is required!`)
|
||||
mirror_part.ref = `mirror_${mirror_part.ref}`
|
||||
anchor = a.anchor(mirror_part, name, points, false)
|
||||
anchor = make_anchor(mirror_part, name, points, false)(rec_units)
|
||||
const mirror_rect = m.model.moveRelative(u.deepcopy(rect), [-size[0], 0])
|
||||
arg = u.union(arg, anchor.position(mirror_rect))
|
||||
}
|
||||
break
|
||||
case 'circle':
|
||||
a.detect_unexpected(part, name, expected.concat(['ref', 'shift', 'rotate', 'radius', 'mirror']))
|
||||
anchor = a.anchor(part, name, points, false)
|
||||
const radius = a.sane(part.radius, `${name}.radius`, 'number')
|
||||
const circle_mirror = a.sane(part.mirror || false, `${name}.mirror`, 'boolean')
|
||||
anchor = make_anchor(part, name, points, false)(units)
|
||||
const radius = a.sane(part.radius, `${name}.radius`, 'number')(units)
|
||||
const circle_mirror = a.sane(part.mirror || false, `${name}.mirror`, 'boolean')()
|
||||
arg = u.circle(anchor.p, radius)
|
||||
if (circle_mirror) {
|
||||
const mirror_part = u.deepcopy(part)
|
||||
a.assert(mirror_part.ref, `Field "${name}.ref" must be speficied if mirroring is required!`)
|
||||
mirror_part.ref = `mirror_${mirror_part.ref}`
|
||||
anchor = a.anchor(mirror_part, name, points, false)
|
||||
anchor = make_anchor(mirror_part, name, points, false)(units)
|
||||
arg = u.union(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 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}]`
|
||||
const anchor = a.anchor(poly_point, poly_name, points, true, last_anchor)
|
||||
const anchor = make_anchor(poly_point, poly_name, points, true, last_anchor)(units)
|
||||
parsed_points.push(anchor.p)
|
||||
}
|
||||
arg = u.poly(parsed_points)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue