ergogen/src/outlines.js

234 lines
8.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')
2021-01-01 00:50:04 +01:00
const o = require('./operation')
2020-07-04 23:23:14 +02:00
const Point = require('./point')
2021-01-01 00:50:04 +01:00
const prep = require('./prepare')
2022-01-10 13:44:57 +01:00
const anchor = require('./anchor').parse
2022-01-09 22:56:05 +01:00
const filter = require('./filter').parse
2020-06-02 22:59:05 +02:00
2022-01-11 22:40:09 +01:00
const binding = (base, bbox, point, units) => {
2020-07-10 22:40:34 +02:00
2022-01-09 22:56:05 +01:00
let bind = a.trbl(point.meta.bind || 0, `${point.meta.name}.bind`)(units)
// if it's a mirrored key, we swap the left and right bind values
if (point.meta.mirrored) {
bind = [bind[0], bind[3], bind[2], bind[1]]
2020-06-28 22:35:53 +02:00
}
2022-01-11 22:40:09 +01:00
const bt = Math.max(bbox.high[1], 0) + Math.max(bind[0], 0)
const br = Math.max(bbox.high[0], 0) + Math.max(bind[1], 0)
const bd = Math.min(bbox.low[1], 0) - Math.max(bind[2], 0)
const bl = Math.min(bbox.low[0], 0) - Math.max(bind[3], 0)
2020-06-30 22:29:14 +02:00
2022-01-09 22:56:05 +01:00
if (bind[0] || bind[1]) base = u.union(base, u.rect(br, bt))
if (bind[1] || bind[2]) base = u.union(base, u.rect(br, -bd, [0, bd]))
if (bind[2] || bind[3]) base = u.union(base, u.rect(-bl, -bd, [bl, bd]))
if (bind[3] || bind[0]) base = u.union(base, u.rect(-bl, bt, [bl, 0]))
2020-06-30 22:29:14 +02:00
2022-01-09 22:56:05 +01:00
return base
}
2020-06-30 22:29:14 +02:00
2022-01-10 13:44:57 +01:00
const rectangle = (config, name, points, outlines, units) => {
2022-01-09 22:56:05 +01:00
// prepare params
a.unexpected(config, `${name}`, ['size', 'corner', 'bevel'])
2022-01-16 20:36:19 +01:00
const size = a.wh(config.size, `${name}.size`)(units)
2022-01-09 22:56:05 +01:00
const rec_units = prep.extend({
sx: size[0],
sy: size[1]
}, units)
2022-01-16 20:36:19 +01:00
const corner = a.sane(config.corner || 0, `${name}.corner`, 'number')(rec_units)
const bevel = a.sane(config.bevel || 0, `${name}.bevel`, 'number')(rec_units)
2022-01-09 22:56:05 +01:00
2022-01-16 20:36:19 +01:00
// return shape function and its units
return [(point, bound) => {
2022-01-09 22:56:05 +01:00
const error = (dim, val) => `Rectangle for "${name}" isn't ${dim} enough for its corner and bevel (${val} - 2 * ${corner} - 2 * ${bevel} <= 0)!`
const [w, h] = size
const mod = 2 * (corner + bevel)
const cw = w - mod
a.assert(cw >= 0, error('wide', w))
const ch = h - mod
a.assert(ch >= 0, error('tall', h))
let rect = new m.models.Rectangle(cw, ch)
if (bevel) {
rect = u.poly([
[-bevel, 0],
[-bevel, ch],
[0, ch + bevel],
[cw, ch + bevel],
[cw + bevel, ch],
[cw + bevel, 0],
[cw, -bevel],
[0, -bevel]
])
}
if (corner > 0) rect = m.model.outline(rect, corner, 0)
2022-01-16 20:36:19 +01:00
rect = m.model.moveRelative(rect, [-cw/2, -ch/2])
2022-01-11 22:40:09 +01:00
if (bound) {
const bbox = {high: [w/2, h/2], low: [-w/2, -h/2]}
rect = binding(rect, bbox, point, rec_units)
}
2022-01-10 13:44:57 +01:00
rect = point.position(rect)
2020-07-10 22:40:34 +02:00
2022-01-10 13:44:57 +01:00
return rect
2022-01-16 20:36:19 +01:00
}, rec_units]
2022-01-09 22:56:05 +01:00
}
2021-01-01 00:50:04 +01:00
2022-01-10 13:44:57 +01:00
const circle = (config, name, points, outlines, units) => {
2021-01-01 00:50:04 +01:00
2022-01-10 13:44:57 +01:00
// prepare params
a.unexpected(config, `${name}`, ['radius'])
const radius = a.sane(config.radius, `${name}.radius`, 'number')(units)
const circ_units = prep.extend({
r: radius
}, units)
2020-07-10 22:40:34 +02:00
2022-01-16 20:36:19 +01:00
// return shape function and its units
return [(point, bound) => {
2022-01-10 13:44:57 +01:00
let circle = u.circle([0, 0], radius)
2022-01-11 22:40:09 +01:00
if (bound) {
const bbox = {high: [radius, radius], low: [-radius, -radius]}
circle = binding(circle, bbox, point, circ_units)
}
2022-01-10 13:44:57 +01:00
circle = point.position(circle)
return circle
2022-01-16 20:36:19 +01:00
}, circ_units]
2022-01-10 13:44:57 +01:00
}
2020-06-30 22:29:14 +02:00
2022-01-10 13:44:57 +01:00
const polygon = (config, name, points, outlines, units) => {
2021-12-15 23:05:40 +01:00
2022-01-10 13:44:57 +01:00
// prepare params
a.unexpected(config, `${name}`, ['points'])
const poly_points = a.sane(config.points, `${name}.points`, 'array')()
2020-06-30 22:29:14 +02:00
2022-01-16 20:36:19 +01:00
// return shape function and its units
return [(point, bound) => {
2022-01-10 13:44:57 +01:00
const parsed_points = []
2022-01-16 20:36:19 +01:00
// the point starts at [0, 0] as it will be positioned later
// but we keep the metadata for potential mirroring purposes
let last_anchor = new Point(0, 0, 0, point.meta)
2022-01-10 13:44:57 +01:00
let poly_index = -1
for (const poly_point of poly_points) {
const poly_name = `${name}.points[${++poly_index}]`
2022-02-27 11:11:45 +01:00
last_anchor = anchor(poly_point, poly_name, points, last_anchor)(units)
2022-01-10 13:44:57 +01:00
parsed_points.push(last_anchor.p)
}
let poly = u.poly(parsed_points)
2022-01-11 22:40:09 +01:00
if (bound) {
const bbox = u.bbox(parsed_points)
poly = binding(poly, bbox, point, units)
}
2022-01-10 13:44:57 +01:00
poly = point.position(poly)
return poly
2022-01-16 20:36:19 +01:00
}, units]
2022-01-10 13:44:57 +01:00
}
2020-07-21 19:35:33 +02:00
2022-01-10 13:44:57 +01:00
const outline = (config, name, points, outlines, units) => {
2020-06-28 22:35:53 +02:00
2022-01-10 13:44:57 +01:00
// prepare params
a.unexpected(config, `${name}`, ['name', 'fillet', 'expand', 'origin'])
a.assert(outlines[config.name], `Field "${name}.name" does not name an existing outline!`)
const fillet = a.sane(config.fillet || 0, `${name}.fillet`, 'number')(units)
const expand = a.sane(config.expand || 0, `${name}.expand`, 'number')(units)
2022-01-11 22:40:09 +01:00
const joints = a.in(a.sane(config.joints || 0, `${name}.joints`, 'number')(units), `${name}.joints`, [0, 1, 2])
2022-01-16 20:36:19 +01:00
const origin = anchor(config.origin || {}, `${name}.origin`, points)(units)
2020-07-10 22:40:34 +02:00
2022-01-16 20:36:19 +01:00
// return shape function and its units
return [(point, bound) => {
2022-01-10 13:44:57 +01:00
let o = u.deepcopy(outlines[config.name])
2022-01-11 22:40:09 +01:00
o = origin.unposition(o)
2022-01-10 13:44:57 +01:00
if (fillet) {
for (const [index, chain] of m.model.findChains(o).entries()) {
o.models[`fillet_${index}`] = m.chain.fillet(chain, fillet)
2020-11-08 16:37:02 +01:00
}
2020-06-30 22:29:14 +02:00
}
2022-01-11 22:40:09 +01:00
if (expand) {
o = m.model.outline(o, Math.abs(expand), joints, (expand < 0), {farPoint: u.farPoint})
2020-07-10 22:40:34 +02:00
}
2020-06-28 22:35:53 +02:00
2022-01-11 22:40:09 +01:00
if (bound) {
const bbox = m.measure.modelExtents(o)
o = binding(o, bbox, point, units)
}
2022-01-10 13:44:57 +01:00
o = point.position(o)
return o
2022-01-16 20:36:19 +01:00
}, units]
2020-06-28 22:35:53 +02:00
}
2022-01-09 22:56:05 +01:00
const whats = {
rectangle,
2022-01-10 13:44:57 +01:00
circle,
polygon,
outline
2022-01-09 22:56:05 +01:00
}
2021-01-01 00:50:04 +01:00
exports.parse = (config = {}, points = {}, units = {}) => {
2020-06-30 22:29:14 +02:00
2022-01-09 22:56:05 +01:00
// output outlines will be collected here
2020-07-04 23:23:14 +02:00
const outlines = {}
2022-01-09 22:56:05 +01:00
// the config must be an actual object so that the exports have names
config = a.sane(config, 'outlines', 'object')()
for (let [outline_name, parts] of Object.entries(config)) {
// placeholder for the current outline
outlines[outline_name] = {models: {}}
// each export can consist of multiple parts
// either sub-objects or arrays are fine...
2021-01-01 00:50:04 +01:00
if (a.type(parts)() == 'array') {
parts = {...parts}
}
2022-01-16 20:36:19 +01:00
parts = a.sane(parts, `outlines.${outline_name}`, 'object')()
2022-01-09 22:56:05 +01:00
for (let [part_name, part] of Object.entries(parts)) {
2022-01-09 22:56:05 +01:00
2022-01-16 20:36:19 +01:00
const name = `outlines.${outline_name}.${part_name}`
2022-01-09 22:56:05 +01:00
// string part-shortcuts are expanded first
2021-01-01 00:50:04 +01:00
if (a.type(part)() == 'string') {
part = o.operation(part, {outline: Object.keys(outlines)})
}
2020-07-03 22:29:39 +02:00
2022-01-09 22:56:05 +01:00
// process keys that are common to all part declarations
const operation = u[a.in(part.operation || 'add', `${name}.operation`, ['add', 'subtract', 'intersect', 'stack'])]
2022-01-10 13:44:57 +01:00
const what = a.in(part.what || 'outline', `${name}.what`, ['rectangle', 'circle', 'polygon', 'outline'])
2022-01-11 22:40:09 +01:00
const bound_by_default = ['rectangle']
const bound = part.bound === undefined ? bound_by_default.includes(what) : !!part.bound
const mirror = a.sane(part.mirror || false, `${name}.mirror`, 'boolean')()
2022-01-10 13:44:57 +01:00
// `where` is delayed until we have all, potentially what-dependent units
2022-01-16 20:36:19 +01:00
// default where is [0, 0], as per filter parsing
const original_where = part.where // need to save, so the delete's don't get rid of it below
const where = units => filter(original_where, `${name}.where`, points, units, mirror)
2022-01-09 22:56:05 +01:00
2022-01-10 13:44:57 +01:00
// these keys are then removed, so ops can check their own unexpected keys without interference
2022-01-09 22:56:05 +01:00
delete part.operation
2022-01-11 22:40:09 +01:00
delete part.what
2022-01-09 22:56:05 +01:00
delete part.bound
delete part.mirror
2022-01-10 13:44:57 +01:00
delete part.where
2022-01-09 22:56:05 +01:00
// a prototype "shape" maker (and its units) are computed
2022-01-10 13:44:57 +01:00
const [shape_maker, shape_units] = whats[what](part, name, points, outlines, units)
2022-01-09 22:56:05 +01:00
2022-01-10 13:44:57 +01:00
// and then the shape is repeated for all where positions
2022-01-09 22:56:05 +01:00
for (const w of where(shape_units)) {
2022-01-10 13:44:57 +01:00
const shape = shape_maker(w, bound)
outlines[outline_name] = operation(outlines[outline_name], shape)
2022-01-09 22:56:05 +01:00
}
2020-07-03 22:29:39 +02:00
}
2020-07-04 23:23:14 +02:00
2022-01-10 13:44:57 +01:00
// final adjustments
2022-01-09 22:56:05 +01:00
m.model.originate(outlines[outline_name])
m.model.simplify(outlines[outline_name])
2020-06-30 22:29:14 +02:00
}
2020-07-04 23:23:14 +02:00
return outlines
2022-01-09 22:56:05 +01:00
}