ergogen/src/utils.js

83 lines
1.7 KiB
JavaScript
Raw Normal View History

2020-05-31 20:33:23 +02:00
const m = require('makerjs')
2021-01-01 21:46:01 +01:00
exports.deepcopy = value => {
if (value === undefined) return undefined
return JSON.parse(JSON.stringify(value))
}
2020-05-31 20:33:23 +02:00
2021-01-01 00:50:04 +01:00
const deep = exports.deep = (obj, key, val) => {
2020-12-26 15:17:15 +01:00
const levels = key.split('.')
const last = levels.pop()
let step = obj
for (const level of levels) {
step[level] = step[level] || {}
step = step[level]
}
2021-01-01 00:50:04 +01:00
if (val === undefined) return step[last]
2020-12-26 15:17:15 +01:00
step[last] = val
return obj
}
2020-06-16 22:24:46 +02:00
const eq = exports.eq = (a=[], b=[]) => {
return a[0] === b[0] && a[1] === b[1]
2020-05-31 22:59:51 +02:00
}
const line = exports.line = (a, b) => {
return new m.paths.Line(a, b)
}
exports.circle = (p, r) => {
2020-07-18 22:58:44 +02:00
return {paths: {circle: new m.paths.Circle(p, r)}}
2020-05-31 22:59:51 +02:00
}
2020-07-10 22:40:34 +02:00
exports.rect = (w, h, o=[0, 0]) => {
2020-06-02 22:59:05 +02:00
const res = {
2020-05-31 22:59:51 +02:00
top: line([0, h], [w, h]),
right: line([w, h], [w, 0]),
bottom: line([w, 0], [0, 0]),
left: line([0, 0], [0, h])
2020-06-02 22:59:05 +02:00
}
return m.model.move({paths: res}, o)
2020-05-31 22:59:51 +02:00
}
exports.poly = (arr) => {
let counter = 0
let prev = arr[arr.length - 1]
const res = {
paths: {}
}
for (const p of arr) {
2020-06-06 23:07:14 +02:00
if (eq(prev, p)) continue
2020-05-31 22:59:51 +02:00
res.paths['p' + (++counter)] = line(prev, p)
prev = p
}
return res
2020-06-02 22:59:05 +02:00
}
2020-07-10 22:40:34 +02:00
const farPoint = [1234.1234, 2143.56789]
exports.union = (a, b) => {
return m.model.combine(a, b, false, true, false, true, {
farPoint
})
}
exports.subtract = (a, b) => {
return m.model.combine(a, b, false, true, true, false, {
farPoint
})
}
exports.intersect = (a, b) => {
return m.model.combine(a, b, true, false, true, false, {
farPoint
})
}
exports.stack = (a, b) => {
return {
models: {
a, b
}
}
}