ergogen/src/points.js

326 lines
11 KiB
JavaScript
Raw Normal View History

2020-05-28 22:18:41 +02:00
const m = require('makerjs')
2020-05-31 20:33:23 +02:00
const u = require('./utils')
2020-06-27 20:13:06 +02:00
const a = require('./assert')
2021-01-01 00:50:04 +01:00
const prep = require('./prepare')
2021-07-18 16:03:45 +02:00
const anchor_lib = require('./anchor')
2020-06-26 21:00:10 +02:00
const push_rotation = exports._push_rotation = (list, angle, origin) => {
2020-05-29 22:35:49 +02:00
let candidate = origin
for (const r of list) {
candidate = m.point.rotate(candidate, r.angle, r.origin)
}
list.push({
angle: angle,
origin: candidate
})
}
2021-01-01 00:50:04 +01:00
const render_zone = exports._render_zone = (zone_name, zone, anchor, global_key, units) => {
2020-06-27 20:13:06 +02:00
// zone-wide sanitization
2021-05-22 19:06:57 +02:00
a.unexpected(zone, `points.zones.${zone_name}`, ['columns', 'rows', 'key'])
2020-06-27 20:13:06 +02:00
// the anchor comes from "above", because it needs other zones too (for references)
2021-01-01 00:50:04 +01:00
const cols = a.sane(zone.columns || {}, `points.zones.${zone_name}.columns`, 'object')()
const zone_wide_rows = a.sane(zone.rows || {}, `points.zones.${zone_name}.rows`, 'object')()
2020-06-27 20:13:06 +02:00
for (const [key, val] of Object.entries(zone_wide_rows)) {
2021-01-01 00:50:04 +01:00
zone_wide_rows[key] = a.sane(val || {}, `points.zones.${zone_name}.rows.${key}`, 'object')()
2020-06-27 20:13:06 +02:00
}
2021-01-01 00:50:04 +01:00
const zone_wide_key = a.sane(zone.key || {}, `points.zones.${zone_name}.key`, 'object')()
2020-06-27 20:13:06 +02:00
// algorithm prep
2020-05-29 22:35:49 +02:00
2020-05-28 22:18:41 +02:00
const points = {}
2020-05-30 15:17:53 +02:00
const rotations = []
// transferring the anchor rotation to "real" rotations
rotations.push({
2020-05-29 22:35:49 +02:00
angle: anchor.r,
2020-05-30 15:17:53 +02:00
origin: anchor.p
})
2020-06-27 20:13:06 +02:00
// column layout
2021-07-18 00:39:33 +02:00
if (!Object.keys(cols).length) {
cols.default = {}
}
2020-08-16 15:34:04 +02:00
let first_col = true
2020-08-08 17:26:17 +02:00
for (let [col_name, col] of Object.entries(cols)) {
2020-06-27 20:13:06 +02:00
// column-level sanitization
2020-08-08 17:26:17 +02:00
col = col || {}
2021-05-22 19:06:57 +02:00
a.unexpected(
2020-06-27 20:13:06 +02:00
col,
`points.zones.${zone_name}.columns.${col_name}`,
2020-08-16 16:11:48 +02:00
['stagger', 'spread', 'rotate', 'origin', 'rows', 'row_overrides', 'key']
2020-06-27 20:13:06 +02:00
)
col.stagger = a.sane(
col.stagger || 0,
`points.zones.${zone_name}.columns.${col_name}.stagger`,
'number'
2021-01-01 00:50:04 +01:00
)(units)
2020-06-27 20:13:06 +02:00
col.spread = a.sane(
2021-01-01 00:50:04 +01:00
col.spread || (first_col ? 0 : 'u'),
2020-06-27 20:13:06 +02:00
`points.zones.${zone_name}.columns.${col_name}.spread`,
'number'
2021-01-01 00:50:04 +01:00
)(units)
2020-06-27 20:13:06 +02:00
col.rotate = a.sane(
col.rotate || 0,
`points.zones.${zone_name}.columns.${col_name}.rotate`,
'number'
2021-01-01 00:50:04 +01:00
)(units)
2020-06-27 20:13:06 +02:00
col.origin = a.xy(
col.origin || [0, 0],
2021-01-01 00:50:04 +01:00
`points.zones.${zone_name}.columns.${col_name}.origin`
)(units)
2020-08-16 16:11:48 +02:00
let override = false
2020-06-27 20:13:06 +02:00
col.rows = a.sane(
col.rows || {},
`points.zones.${zone_name}.columns.${col_name}.rows`,
'object'
2021-01-01 00:50:04 +01:00
)()
2020-08-16 16:11:48 +02:00
if (col.row_overrides) {
override = true
col.rows = a.sane(
col.row_overrides,
`points.zones.${zone_name}.columns.${col_name}.row_overrides`,
'object'
2021-01-01 00:50:04 +01:00
)()
2020-08-16 16:11:48 +02:00
}
2020-06-27 20:13:06 +02:00
for (const [key, val] of Object.entries(col.rows)) {
col.rows[key] = a.sane(
val || {},
`points.zones.${zone_name}.columns.${col_name}.rows.${key}`,
'object'
2021-01-01 00:50:04 +01:00
)()
2020-06-27 20:13:06 +02:00
}
col.key = a.sane(
col.key || {},
`points.zones.${zone_name}.columns.${col_name}.key`,
'object'
2021-01-01 00:50:04 +01:00
)()
2020-06-27 20:13:06 +02:00
2020-08-16 16:11:48 +02:00
// propagating object key to name field
2020-06-27 20:13:06 +02:00
col.name = col_name
2020-08-16 16:11:48 +02:00
// combining row data from zone-wide defs and col-specific defs
// (while also handling potential overrides)
const actual_rows = override ? Object.keys(col.rows)
2021-01-01 00:50:04 +01:00
: Object.keys(prep.extend(zone_wide_rows, col.rows))
if (!actual_rows.length) {
actual_rows.push('default')
}
2020-06-27 20:13:06 +02:00
// setting up column-level anchor
2020-08-16 15:34:04 +02:00
anchor.x += col.spread
anchor.y += col.stagger
2020-05-30 15:17:53 +02:00
const col_anchor = anchor.clone()
// clear potential rotations, as they will get re-applied anyway
// and we don't want to apply them twice...
col_anchor.r = 0
2020-08-16 15:34:04 +02:00
// applying col-level rotation (cumulatively, for the next columns as well)
if (col.rotate) {
push_rotation(
rotations,
col.rotate,
col_anchor.clone().shift(col.origin, false).p
)
}
// getting key config through the 5-level extension
2020-06-27 20:13:06 +02:00
2020-06-26 21:00:10 +02:00
const keys = []
2020-06-27 20:13:06 +02:00
const default_key = {
shift: [0, 0],
rotate: 0,
2021-01-01 00:50:04 +01:00
padding: 'u',
width: 1,
height: 1,
2020-06-27 20:13:06 +02:00
skip: false,
asym: 'both'
}
2020-08-16 16:11:48 +02:00
for (const row of actual_rows) {
2021-01-01 00:50:04 +01:00
const key = prep.extend(
2020-06-27 20:13:06 +02:00
default_key,
2020-07-04 23:23:14 +02:00
global_key,
2020-06-27 20:13:06 +02:00
zone_wide_key,
col.key,
zone_wide_rows[row] || {},
col.rows[row] || {}
)
key.name = key.name || `${zone_name}_${col_name}_${row}`
key.colrow = `${col_name}_${row}`
2021-01-01 00:50:04 +01:00
key.shift = a.xy(key.shift, `${key.name}.shift`)(units)
key.rotate = a.sane(key.rotate, `${key.name}.rotate`, 'number')(units)
key.width = a.sane(key.width, `${key.name}.width`, 'number')(units)
key.height = a.sane(key.height, `${key.name}.height`, 'number')(units)
key.padding = a.sane(key.padding, `${key.name}.padding`, 'number')(units)
key.skip = a.sane(key.skip, `${key.name}.skip`, 'boolean')()
2020-08-16 16:11:48 +02:00
key.asym = a.in(key.asym, `${key.name}.asym`, ['left', 'right', 'both'])
2020-06-26 21:00:10 +02:00
key.col = col
key.row = row
keys.push(key)
2020-06-03 22:16:45 +02:00
}
2020-06-27 20:13:06 +02:00
// actually laying out keys
2020-06-26 21:00:10 +02:00
for (const key of keys) {
2020-05-30 15:17:53 +02:00
let point = col_anchor.clone()
for (const r of rotations) {
point.rotate(r.angle, r.origin)
}
point.shift(key.shift)
point.r += key.rotate
2020-06-26 21:00:10 +02:00
point.meta = key
points[key.name] = point
2020-06-27 20:13:06 +02:00
col_anchor.y += key.padding
2020-05-29 22:35:49 +02:00
}
2020-08-16 15:34:04 +02:00
first_col = false
2020-05-30 15:17:53 +02:00
}
2020-05-29 22:35:49 +02:00
2020-05-30 15:17:53 +02:00
return points
}
2020-05-29 22:35:49 +02:00
2021-01-01 00:50:04 +01:00
const parse_axis = exports._parse_axis = (config, name, points, units) => {
if (!['number', 'undefined'].includes(a.type(config)(units))) {
const mirror_obj = a.sane(config || {}, name, 'object')()
const distance = a.sane(mirror_obj.distance || 0, `${name}.distance`, 'number')(units)
delete mirror_obj.distance
2021-07-18 16:03:45 +02:00
let axis = anchor_lib.parse(mirror_obj, name, points)(units).x
axis += distance / 2
return axis
} else return config
}
2020-05-29 22:35:49 +02:00
const perform_mirror = exports._perform_mirror = (point, axis) => {
if (axis !== undefined) {
point.meta.mirrored = false
if (point.meta.asym == 'left') return ['', null]
const mp = point.clone().mirror(axis)
const mirrored_name = `mirror_${point.meta.name}`
2021-01-01 00:50:04 +01:00
mp.meta = prep.extend(mp.meta, mp.meta.mirror || {})
mp.meta.name = mirrored_name
mp.meta.colrow = `mirror_${mp.meta.colrow}`
mp.meta.mirrored = true
if (point.meta.asym == 'right') {
point.meta.skip = true
}
return [mirrored_name, mp]
}
return ['', null]
}
2020-05-29 22:35:49 +02:00
exports.parse = (config, units) => {
2021-01-01 00:50:04 +01:00
// config sanitization
2021-07-18 00:39:33 +02:00
a.unexpected(config, 'points', ['zones', 'key', 'rotate', 'mirror'])
const zones = a.sane(config.zones, 'points.zones', 'object')()
2021-01-01 00:50:04 +01:00
const global_key = a.sane(config.key || {}, 'points.key', 'object')()
const global_rotate = a.sane(config.rotate || 0, 'points.rotate', 'number')(units)
const global_mirror = config.mirror
let points = {}
let mirrored_points = {}
let all_points = {}
2021-01-01 00:50:04 +01:00
// rendering zones
for (let [zone_name, zone] of Object.entries(zones)) {
// extracting keys that are handled here, not at the zone render level
2021-07-18 16:03:45 +02:00
const anchor = anchor_lib.parse(zone.anchor || {}, `points.zones.${zone_name}.anchor`, all_points)(units)
2021-01-01 00:50:04 +01:00
const rotate = a.sane(zone.rotate || 0, `points.zones.${zone_name}.rotate`, 'number')(units)
const mirror = zone.mirror
delete zone.anchor
delete zone.rotate
delete zone.mirror
// creating new points
2021-01-01 00:50:04 +01:00
const new_points = render_zone(zone_name, zone, anchor, global_key, units)
// adjusting new points
for (const [new_name, new_point] of Object.entries(new_points)) {
// issuing a warning for duplicate keys
if (Object.keys(points).includes(new_name)) {
throw new Error(`Key "${new_name}" defined more than once!`)
}
// per-zone rotation
if (rotate) {
new_point.rotate(rotate)
2020-08-11 22:21:28 +02:00
}
}
// adding new points so that they can be referenced from now on
2020-08-11 22:21:28 +02:00
points = Object.assign(points, new_points)
all_points = Object.assign(all_points, points)
// per-zone mirroring for the new keys
2021-01-01 00:50:04 +01:00
const axis = parse_axis(mirror, `points.zones.${zone_name}.mirror`, all_points, units)
if (axis) {
for (const new_point of Object.values(new_points)) {
const [mname, mp] = perform_mirror(new_point, axis)
if (mp) {
mirrored_points[mname] = mp
all_points[mname] = mp
}
}
}
2020-05-30 15:17:53 +02:00
}
2020-05-29 22:35:49 +02:00
// merging regular and early-mirrored points
points = Object.assign(points, mirrored_points)
2020-06-27 20:13:06 +02:00
// applying global rotation
for (const point of Object.values(points)) {
if (global_rotate) {
point.rotate(global_rotate)
2020-05-28 22:18:41 +02:00
}
2020-05-29 22:35:49 +02:00
}
2020-05-28 22:18:41 +02:00
// global mirroring for points that haven't been mirrored yet
2021-01-01 00:50:04 +01:00
const global_axis = parse_axis(global_mirror, `points.mirror`, points, units)
const global_mirrored_points = {}
for (const point of Object.values(points)) {
if (global_axis && point.mirrored === undefined) {
const [mname, mp] = perform_mirror(point, global_axis)
if (mp) {
global_mirrored_points[mname] = mp
2020-05-31 20:33:23 +02:00
}
2020-05-30 15:17:53 +02:00
}
2020-05-28 22:18:41 +02:00
}
2020-06-27 20:13:06 +02:00
// merging the global-mirrored points as well
points = Object.assign(points, global_mirrored_points)
2020-06-27 20:13:06 +02:00
// removing temporary points
2020-05-30 15:17:53 +02:00
const filtered = {}
for (const [k, p] of Object.entries(points)) {
2020-06-26 21:00:10 +02:00
if (p.meta.skip) continue
2020-05-30 15:17:53 +02:00
filtered[k] = p
}
2020-05-28 22:18:41 +02:00
// done
return filtered
2020-06-28 22:35:53 +02:00
}
2021-07-18 16:26:30 +02:00
exports.visualize = (points, units) => {
const models = {}
const x_unit = units.visual_x || (units.u - 1)
const y_unit = units.visual_y || (units.u - 1)
2020-06-28 22:35:53 +02:00
for (const [pname, p] of Object.entries(points)) {
2021-07-25 20:31:51 +02:00
const w = p.meta.width * x_unit
const h = p.meta.height * y_unit
const rect = u.rect(w, h, [-w/2, -h/2])
models[pname] = p.position(rect)
2020-06-28 22:35:53 +02:00
}
return {models: models}
2020-05-28 22:18:41 +02:00
}