Still more outline progress
This commit is contained in:
parent
999a5dfad8
commit
066e1a54ea
6 changed files with 132 additions and 62 deletions
|
@ -38,9 +38,9 @@ const numarr = exports.numarr = (raw, name, length) => {
|
|||
|
||||
const xy = exports.xy = (raw, name) => numarr(raw, name, 2)
|
||||
|
||||
exports.wh = (raw, name) => {
|
||||
const wh = exports.wh = (raw, name) => {
|
||||
if (!Array.isArray(raw)) raw = [raw, raw]
|
||||
return a.xy(raw, name)
|
||||
return xy(raw, name)
|
||||
}
|
||||
|
||||
exports.trbl = (raw, name) => {
|
||||
|
@ -49,15 +49,15 @@ exports.trbl = (raw, name) => {
|
|||
return numarr(raw, name, 4)
|
||||
}
|
||||
|
||||
exports.anchor = (raw, name, points={}, check_unexpected=true) => {
|
||||
exports.anchor = (raw, name, points={}, check_unexpected=true, default_point=new Point()) => {
|
||||
if (check_unexpected) detect_unexpected(raw, name, ['ref', 'shift', 'rotate'])
|
||||
let a = new Point()
|
||||
let a = default_point.clone()
|
||||
if (raw.ref !== undefined) {
|
||||
assert(points[raw.ref], `Unknown point reference "${raw.ref}" in anchor "${name}"!`)
|
||||
a = points[raw.ref].clone()
|
||||
}
|
||||
if (raw.shift !== undefined) {
|
||||
const xyval = xy(raw.shift, name + '.shift')
|
||||
const xyval = wh(raw.shift || [0, 0], name + '.shift')
|
||||
a.x += xyval[0]
|
||||
a.y += xyval[1]
|
||||
}
|
||||
|
|
11
src/cli.js
11
src/cli.js
|
@ -52,15 +52,20 @@ try {
|
|||
console.log('Parsing points...')
|
||||
const points = points_lib.parse(config.points)
|
||||
if (args.debug) {
|
||||
fs.writeJSONSync(path.join(args.o, 'points.json'), points, {spaces: 4})
|
||||
const rect = u.rect(18, 18, [-9, -9])
|
||||
const points_demo = points_lib.position(points, rect)
|
||||
io.dump_model(points_demo, path.join(args.o, 'points_demo'), args.debug)
|
||||
io.dump_model(points_demo, path.join(args.o, 'points/points_demo'), args.debug)
|
||||
fs.writeJSONSync(path.join(args.o, 'points/points.json'), points, {spaces: 4})
|
||||
}
|
||||
|
||||
// outlines
|
||||
|
||||
// console.log('Generating outlines...')
|
||||
console.log('Generating outlines...')
|
||||
const outlines = outline_lib.parse(config.outline, points)
|
||||
for (const [name, outline] of Object.entries(outlines)) {
|
||||
if (!args.debug && name.startsWith('_')) continue
|
||||
io.dump_model(outline, path.join(args.o, `outline/${name}`), args.debug)
|
||||
}
|
||||
|
||||
// goodbye
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
const m = require('makerjs')
|
||||
const u = require('./utils')
|
||||
const a = require('./assert')
|
||||
const Point = require('./point')
|
||||
|
||||
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)!`
|
||||
|
@ -19,7 +20,7 @@ const parse_glue = exports._parse_glue = (config = {}, points = {}) => {
|
|||
|
||||
a.detect_unexpected(config, 'outline.glue', ['top', 'bottom', 'waypoints', 'extra'])
|
||||
|
||||
for (const y in ['top', 'bottom']) {
|
||||
for (const y of ['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') {
|
||||
|
@ -39,17 +40,17 @@ const parse_glue = exports._parse_glue = (config = {}, points = {}) => {
|
|||
|
||||
// TODO: handle glue.extra (or revoke it from the docs)
|
||||
|
||||
return (export_name, params) => {
|
||||
return (params, export_name, expected) => {
|
||||
|
||||
a.detect_unexpected(params, `${export_name}`, ['side', 'size', 'corner', 'bevel'])
|
||||
a.detect_unexpected(params, `${export_name}`, expected.concat(['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)) {
|
||||
let left = {models: {}}
|
||||
let right = {models: {}}
|
||||
if (['left', 'right', 'middle', 'both'].includes(side)) {
|
||||
for (const [pname, p] of Object.entries(points)) {
|
||||
|
||||
let from_x = -size[0] / 2, to_x = size[0] / 2
|
||||
|
@ -77,11 +78,11 @@ const parse_glue = exports._parse_glue = (config = {}, points = {}) => {
|
|||
}
|
||||
}
|
||||
}
|
||||
if (params.side == 'left') return left
|
||||
if (params.side == 'right') return right
|
||||
if (side == 'left') return left
|
||||
if (side == 'right') return right
|
||||
|
||||
let glue = {paths: {}}
|
||||
if (['middle', 'both', 'glue'].includes(params.side)) {
|
||||
let glue = {models: {}}
|
||||
if (['middle', 'both', 'glue'].includes(side)) {
|
||||
|
||||
const get_line = (anchor) => {
|
||||
if (a.type(anchor) == 'number') {
|
||||
|
@ -135,11 +136,11 @@ const parse_glue = exports._parse_glue = (config = {}, points = {}) => {
|
|||
|
||||
glue = u.poly(waypoints)
|
||||
}
|
||||
if (params.side == 'glue') return glue
|
||||
if (side == 'glue') return glue
|
||||
|
||||
let both = m.model.combineUnion(u.deepcopy(left), glue)
|
||||
both = m.model.combineUnion(both, u.deepcopy(right))
|
||||
if (params.side == 'both') return both
|
||||
if (side == 'both') return both
|
||||
|
||||
let middle = m.model.combineSubtraction(both, left)
|
||||
middle = m.model.combineSubtraction(both, right)
|
||||
|
@ -151,14 +152,68 @@ exports.parse = (config = {}, points = {}) => {
|
|||
a.detect_unexpected(config, 'outline', ['glue', 'exports'])
|
||||
const glue = parse_glue(config.glue, points)
|
||||
|
||||
config = a.sane(config, 'outline.exports', 'object')
|
||||
for (const [key, parts] of Object.entries(config)) {
|
||||
const outlines = {}
|
||||
|
||||
const ex = a.sane(config.exports, 'outline.exports', 'object')
|
||||
for (const [key, parts] of Object.entries(ex)) {
|
||||
let index = 0
|
||||
let result = {models: {}}
|
||||
for (const part of parts) {
|
||||
const name = `outline.exports.${key}[${++index}]`
|
||||
part.op = a.in(part.op || 'add', `${name}.op`, ['add', 'sub', 'diff'])
|
||||
const expected = ['type', 'operation']
|
||||
part.type = a.in(part.type, `${name}.type`, ['keys', 'rectangle', 'circle', 'polygon', 'ref'])
|
||||
part.operation = a.in(part.operation || 'add', `${name}.operation`, ['add', 'subtract', 'intersect'])
|
||||
|
||||
let op = m.model.combineUnion
|
||||
if (part.operation == 'subtract') op = m.model.combineSubtraction
|
||||
else if (part.operation == 'intersect') op = m.model.combineIntersection
|
||||
|
||||
let arg
|
||||
let anchor
|
||||
switch (part.type) {
|
||||
case 'keys':
|
||||
arg = glue(part, name, expected)
|
||||
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 = m.model.move(arg, [-size[0]/2, -size[1]/2]) // center
|
||||
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}]`
|
||||
const anchor = a.anchor(point, point_name, points, true, last_anchor)
|
||||
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!`)
|
||||
arg = outlines[part.name]
|
||||
break
|
||||
}
|
||||
|
||||
result = op(result, arg)
|
||||
}
|
||||
|
||||
outlines[key] = result
|
||||
}
|
||||
|
||||
return outlines
|
||||
}
|
|
@ -42,7 +42,7 @@ const push_rotation = exports._push_rotation = (list, angle, origin) => {
|
|||
})
|
||||
}
|
||||
|
||||
const render_zone = exports._render_zone = (zone_name, zone, anchor) => {
|
||||
const render_zone = exports._render_zone = (zone_name, zone, anchor, global_key) => {
|
||||
|
||||
// zone-wide sanitization
|
||||
|
||||
|
@ -141,6 +141,7 @@ const render_zone = exports._render_zone = (zone_name, zone, anchor) => {
|
|||
for (const row of Object.keys(actual_rows)) {
|
||||
const key = extend(
|
||||
default_key,
|
||||
global_key,
|
||||
zone_wide_key,
|
||||
col.key,
|
||||
zone_wide_rows[row] || {},
|
||||
|
@ -198,16 +199,17 @@ const render_zone = exports._render_zone = (zone_name, zone, anchor) => {
|
|||
|
||||
exports.parse = (config = {}) => {
|
||||
|
||||
a.detect_unexpected(config, 'points', ['zones', 'rotate', 'mirror'])
|
||||
a.detect_unexpected(config, 'points', ['zones', 'key', 'rotate', 'mirror'])
|
||||
|
||||
let points = {}
|
||||
|
||||
// getting original points
|
||||
|
||||
const zones = a.sane(config.zones || {}, 'points.zones', 'object')
|
||||
const global_key = a.sane(config.key || {}, 'points.key', 'object')
|
||||
for (const [zone_name, zone] of Object.entries(zones)) {
|
||||
const anchor = a.anchor(zone.anchor || new Point(), `points.zones.${zone_name}.anchor`, points)
|
||||
points = Object.assign(points, render_zone(zone_name, zone, anchor))
|
||||
const anchor = a.anchor(zone.anchor || {}, `points.zones.${zone_name}.anchor`, points)
|
||||
points = Object.assign(points, render_zone(zone_name, zone, anchor, global_key))
|
||||
}
|
||||
|
||||
// applying global rotation
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue