1
0
Fork 0
freesewing/designs/penelope/src/utils.mjs

161 lines
5.4 KiB
JavaScript
Raw Normal View History

2022-09-29 22:30:35 +00:00
function dartCalcFront(options, seatWaistDiff, waist, nrOfDarts) {
2019-08-21 11:11:05 -07:00
return (
2022-09-29 22:30:35 +00:00
((waist * options.dartMinimumWidth +
2019-08-21 11:11:05 -07:00
(Math.max(
2022-09-29 22:30:35 +00:00
Math.min(seatWaistDiff, waist * options.dartMaximumDifference),
waist * options.dartMinimumDifference
2019-08-21 11:11:05 -07:00
) -
2022-09-29 22:30:35 +00:00
waist * options.dartMinimumDifference) /
2019-08-21 11:11:05 -07:00
4) /
nrOfDarts) *
(0.5 + options.dartToSideSeamFactor)
2020-01-04 14:23:08 +01:00
)
2019-08-21 11:11:05 -07:00
}
2022-09-29 22:30:35 +00:00
function dartCalcBack(options, seatWaistDiff, waist, nrOfDarts) {
2019-08-21 11:11:05 -07:00
return (
2022-09-29 22:30:35 +00:00
((waist * options.dartMinimumWidth +
2019-08-21 11:11:05 -07:00
(seatWaistDiff -
2022-09-29 22:30:35 +00:00
waist * options.dartBackControl1 -
(seatWaistDiff - waist * options.dartBackControl1) / options.dartBackControl2) /
2019-08-21 11:11:05 -07:00
options.dartBackControl3) /
nrOfDarts) *
(0.5 + options.dartToSideSeamFactor)
2020-01-04 14:23:08 +01:00
)
2019-08-21 11:11:05 -07:00
}
2021-02-14 18:13:46 -08:00
function dartCalc(store, options, seat, seatEase, waist, waistEase) {
2020-01-04 14:23:08 +01:00
seat += seatEase
waist += waistEase
2023-09-26 23:09:49 +00:00
const seatWaistDiff = Math.max(seat - waist, 0)
2019-08-21 11:11:05 -07:00
2020-01-04 14:23:08 +01:00
let nrOfDarts = options.nrOfDarts
2019-08-21 11:11:05 -07:00
2022-09-29 22:30:35 +00:00
let frontDartSize = dartCalcFront(options, seatWaistDiff, waist, nrOfDarts)
2019-08-21 11:11:05 -07:00
// If the front darts are too small and we have more than one, remove one.
if (frontDartSize <= options.dartMinimumWidth * nrOfDarts && nrOfDarts > 1) {
2020-01-04 14:23:08 +01:00
nrOfDarts--
2022-09-29 22:30:35 +00:00
frontDartSize = dartCalcFront(options, seatWaistDiff, waist, nrOfDarts)
2019-08-21 11:11:05 -07:00
}
// See if the dart created by the side seam becomes too small:
if (seatWaistDiff / 4 - frontDartSize < options.dartSideMinimum) {
2020-01-04 14:23:08 +01:00
frontDartSize = 0
2019-08-21 11:11:05 -07:00
}
2022-09-29 22:30:35 +00:00
let backDartSize = dartCalcBack(options, seatWaistDiff, waist, nrOfDarts)
2019-08-21 11:11:05 -07:00
// If the back darts are too small and we have more than one, remove one.
if (backDartSize < options.dartMinimumWidth * nrOfDarts && nrOfDarts > 1) {
2020-01-04 14:23:08 +01:00
nrOfDarts = 1
2022-09-29 22:30:35 +00:00
frontDartSize = dartCalcFront(options, seatWaistDiff, waist, nrOfDarts)
backDartSize = dartCalcBack(options, seatWaistDiff, waist, nrOfDarts)
2019-08-21 11:11:05 -07:00
}
2021-03-09 17:43:06 +01:00
store.set('frontDartSize', frontDartSize)
store.set('backDartSize', backDartSize)
store.set('nrOfDarts', nrOfDarts)
2019-08-21 11:11:05 -07:00
}
/**
* Method to add a dart onto a curve
* The dart is added at an 90 degree angle with the curve for a certain depth and Width
* @param part The part that will provide that Paths
* @param curvePath The curve the dart needs to divide
* @param distance Distance from $p1 where the middle of the dart will be
* @param dartSize The width of the dart opening at the curve
* @param dartDepth The depth of the dart
*
* @return Object with three path attributes; left, dart, right
*/
function addDartToCurve(part, curvePath, distance, dartSize, dartDepth) {
2022-09-29 22:30:35 +00:00
const { options } = part.shorthand()
2019-08-21 11:11:05 -07:00
2022-09-29 22:30:35 +00:00
if (dartSize > curvePath.length()) {
// Curve too small to fit dart!
return null
}
2022-09-29 22:30:35 +00:00
if (distance < dartSize / 1.9) {
distance = dartSize / 2.1
}
if (curvePath.length() - distance < dartSize) {
distance = curvePath.length() - dartSize / 1.95
}
2023-09-26 23:09:49 +00:00
const dartMiddle = curvePath.shiftAlong(distance)
const curvePaths = curvePath.split(dartMiddle)
2022-09-29 22:30:35 +00:00
if (curvePaths[0].length() < dartSize / 2 || curvePaths[1].length() < dartSize / 2) {
// Curve too small to fit dart!
return null
}
2023-09-26 23:09:49 +00:00
const dartLeft = curvePaths[0].reverse().shiftAlong(dartSize / 2)
const dartRight = curvePaths[1].shiftAlong(dartSize / 2)
2019-08-21 11:11:05 -07:00
2023-09-26 23:09:49 +00:00
const distanceFactor = 0.0015
const leftCPdistance = Math.min(
2019-08-21 11:11:05 -07:00
curvePaths[0].length() * distanceFactor,
curvePaths[0].ops[1].to.dist(curvePaths[0].ops[1].cp2)
2020-01-04 14:23:08 +01:00
)
2023-09-26 23:09:49 +00:00
const rightCPdistance = Math.min(
2019-08-21 11:11:05 -07:00
curvePaths[1].length() * distanceFactor,
curvePaths[1].ops[0].to.dist(curvePaths[1].ops[1].cp1)
2020-01-04 14:23:08 +01:00
)
2019-08-21 11:11:05 -07:00
2023-09-26 23:09:49 +00:00
const dartBottom = dartMiddle.shift(dartLeft.angle(dartRight) - 90, dartDepth)
2019-08-21 11:11:05 -07:00
2023-09-26 23:09:49 +00:00
const leftDartCP = dartLeft.shift(dartLeft.angle(dartBottom) - 90, leftCPdistance)
const rightDartCP = dartRight.shift(dartRight.angle(dartBottom) + 90, rightCPdistance)
2019-08-21 11:11:05 -07:00
2023-09-26 23:09:49 +00:00
const curveLeftOfDart = new part.Path()
2019-08-21 11:11:05 -07:00
.move(curvePaths[0].ops[0].to)
.curve(curvePaths[0].ops[1].cp1, leftDartCP, dartLeft)
.hide()
2023-09-26 23:09:49 +00:00
const curveRightOfDart = new part.Path()
2019-08-21 11:11:05 -07:00
.move(dartRight)
.curve(rightDartCP, curvePaths[1].ops[1].cp2, curvePaths[1].ops[1].to)
.hide()
2019-08-21 11:11:05 -07:00
2023-09-26 23:09:49 +00:00
let dart = null
2022-09-29 22:30:35 +00:00
if (options.curvedDarts) {
if (
dartBottom.angle(dartLeft) - dartBottom.angle(dartRight) <
options.curvedDartControlAngle * 2
) {
dart = new part.Path().move(dartLeft).line(dartBottom).line(dartRight).hide()
} else {
2023-09-26 23:09:49 +00:00
const dartBottomCp2 = dartBottom
2022-09-29 22:30:35 +00:00
.shiftFractionTowards(dartMiddle, options.curvedDartBottomControlOffset)
.rotate(options.curvedDartControlAngle, dartBottom)
2023-09-26 23:09:49 +00:00
const dartBottomCp1 = dartBottom
2022-09-29 22:30:35 +00:00
.shiftFractionTowards(dartMiddle, options.curvedDartBottomControlOffset)
.rotate(360 - options.curvedDartControlAngle, dartBottom)
2023-09-26 23:09:49 +00:00
const dartLeftCp1 = dartLeft.shiftFractionTowards(
2022-09-29 22:30:35 +00:00
dartBottom,
options.curvedDartTopControlOffset
)
2023-09-26 23:09:49 +00:00
const dartRightCp2 = dartRight.shiftFractionTowards(
2022-09-29 22:30:35 +00:00
dartBottom,
options.curvedDartTopControlOffset
)
dart = new part.Path()
.move(dartLeft)
.curve(dartLeftCp1, dartBottomCp2, dartBottom)
.curve(dartBottomCp1, dartRightCp2, dartRight)
.hide()
}
} else {
dart = new part.Path().move(dartLeft).line(dartBottom).line(dartRight).hide()
}
2019-08-21 11:11:05 -07:00
2023-09-26 23:09:49 +00:00
const curveWithDart = {
2019-08-21 11:11:05 -07:00
left: curveLeftOfDart,
dart: dart,
2021-04-24 10:16:31 +02:00
right: curveRightOfDart,
2020-01-04 14:23:08 +01:00
}
2019-08-21 11:11:05 -07:00
2020-01-04 14:23:08 +01:00
return curveWithDart
2019-08-21 11:11:05 -07:00
}
2020-01-04 14:23:08 +01:00
export { addDartToCurve, dartCalc }