Skip to content

Commit

Permalink
feat: Add support for straight-line strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
Undistraction committed Jul 28, 2024
1 parent fd27083 commit 00de81f
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 75 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const coonsPatch = getCoonsPatch(
const point = coonsPatch.api.getPoint(0.5, 0.75)

// Get an object wity `xAxis` and `yAxis` keys. Each key contains an Array containing data representing all the sub-curves that make up each curve along that axis.
const curves = coonsPatch.api.getCurves()
const curves = coonsPatch.api.getyLines()

// Get an array of points representing every grid intersection
const intersections = coonsPatch.api.getIntersections()
Expand Down Expand Up @@ -126,7 +126,7 @@ The `coonsPatch` object comprises of two fields, `config` and `api`.

TBD

#### getCurves
#### getyLines

TBD

Expand Down
5 changes: 5 additions & 0 deletions src/const.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ export const INTERPOLATION_STRATEGY_ID = {
LINEAR: 'linear',
EVEN: 'even',
}

export const LINE_STRATEGY_ID = {
STRAIGHT_LINES: `straightLines`,
CURVES: `curves`,
}
31 changes: 23 additions & 8 deletions src/utils/getCoonsPatch.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import memoize from 'fast-memoize'
import { INTERPOLATION_STRATEGY_ID } from '../const'
import { INTERPOLATION_STRATEGY_ID, LINE_STRATEGY_ID } from '../const'
import { interpolatePointOnCurveEvenlySpaced } from './interpolate/even'
import { interpolatePointOnCurveLinear } from './interpolate/linear'
import {
getCurvesOnXAxis,
getCurvesOnYAxis,
getCurveOnXAxis,
getCurveOnYAxis,
getGridIntersections,
getLinesOnXAxis,
getLinesOnYAxis,
getPointOnSurface,
getStraightLineOnXAxis,
getStraightLineOnYAxis,
} from './surface'
import { isArray } from './types'
import {
Expand Down Expand Up @@ -61,6 +65,15 @@ const getCoonsPatch = (boundingCurves, grid) => {
: // Default to even
interpolatePointOnCurveEvenlySpaced({ precision: grid.precision })

const getLineOnXAxis =
grid.lineStrategy === LINE_STRATEGY_ID.CURVES
? getCurveOnXAxis
: getStraightLineOnXAxis
const getLineOnYAxis =
grid.lineStrategy === LINE_STRATEGY_ID.CURVES
? getCurveOnYAxis
: getStraightLineOnYAxis

const getPoint = memoize((ratioX, ratioY) => {
return getPointOnSurface(
boundingCurves,
Expand All @@ -70,18 +83,20 @@ const getCoonsPatch = (boundingCurves, grid) => {
)
})

const getCurves = memoize(() => {
const getLines = memoize(() => {
return {
xAxis: getCurvesOnXAxis(
xAxis: getLinesOnXAxis(
boundingCurves,
columns,
rows,
getLineOnXAxis,
interpolatePointOnCurve
),
yAxis: getCurvesOnYAxis(
yAxis: getLinesOnYAxis(
boundingCurves,
columns,
rows,
getLineOnYAxis,
interpolatePointOnCurve
),
}
Expand All @@ -101,7 +116,7 @@ const getCoonsPatch = (boundingCurves, grid) => {
const getGridCellBounds = memoize((x, y) => {
validateGetSquareArguments(x, y, columns, rows)

const { xAxis, yAxis } = getCurves()
const { xAxis, yAxis } = getLines()

return {
top: yAxis[y][x],
Expand All @@ -128,7 +143,7 @@ const getCoonsPatch = (boundingCurves, grid) => {
},
api: {
getPoint,
getCurves,
getLines,
getIntersections,
getGridCellBounds,
getAllGridCellBounds,
Expand Down
211 changes: 151 additions & 60 deletions src/utils/surface.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,148 @@ export const getPointOnSurface = (
}
}

export const getCurvesOnXAxis = (
export const getStraightLineOnXAxis = (
boundingCurves,
rowRatioTotal,
rowRatio,
rowEndRatio,
columnRatioTotal,
interpolatePointOnCurve
) => {
const startPoint = getPointOnSurface(
boundingCurves,
columnRatioTotal,
rowRatioTotal,
interpolatePointOnCurve
)

const endPoint = getPointOnSurface(
boundingCurves,
columnRatioTotal,
rowEndRatio,
interpolatePointOnCurve
)

return {
startPoint,
endPoint,
controlPoint1: startPoint,
controlPoint2: endPoint,
}
}

export const getStraightLineOnYAxis = (
boundingCurves,
columnRatioTotal,
columnRatio,
columnEndRatio,
rowRatioTotal,
interpolatePointOnCurve
) => {
const startPoint = getPointOnSurface(
boundingCurves,
columnRatioTotal,
rowRatioTotal,
interpolatePointOnCurve
)

const endPoint = getPointOnSurface(
boundingCurves,
columnEndRatio,
rowRatioTotal,
interpolatePointOnCurve
)

return {
startPoint,
endPoint,
controlPoint1: startPoint,
controlPoint2: endPoint,
}
}

export const getCurveOnXAxis = (
boundingCurves,
rowRatioTotal,
rowRatio,
rowEndRatio,
columnRatioTotal,
interpolatePointOnCurve
) => {
const { startPoint, endPoint } = getStraightLineOnXAxis(
boundingCurves,
rowRatioTotal,
rowRatio,
rowEndRatio,
columnRatioTotal,
interpolatePointOnCurve
)

const midPoint1 = getPointOnSurface(
boundingCurves,
columnRatioTotal,
rowRatioTotal + rowRatio * RATIO_MIDPOINT_1,
interpolatePointOnCurve
)

const midPoint2 = getPointOnSurface(
boundingCurves,
columnRatioTotal,
rowRatioTotal + rowRatio * RATIO_MIDPOINT_2,
interpolatePointOnCurve
)

const curve = fitCubicBezierToPoints(
[startPoint, midPoint1, midPoint2, endPoint],
[0, RATIO_MIDPOINT_1, RATIO_MIDPOINT_2, 1]
)

return curve
}

export const getCurveOnYAxis = (
boundingCurves,
columnRatioTotal,
columnRatio,
columnEndRatio,
rowRatioTotal,
interpolatePointOnCurve
) => {
const { startPoint, endPoint } = getStraightLineOnYAxis(
boundingCurves,
columnRatioTotal,
columnRatio,
columnEndRatio,
rowRatioTotal,
interpolatePointOnCurve
)

const midPoint1 = getPointOnSurface(
boundingCurves,
columnRatioTotal + columnRatio * RATIO_MIDPOINT_1,
rowRatioTotal,
interpolatePointOnCurve
)

const midPoint2 = getPointOnSurface(
boundingCurves,
columnRatioTotal + columnRatio * RATIO_MIDPOINT_2,
rowRatioTotal,
interpolatePointOnCurve
)

const curve = fitCubicBezierToPoints(
[startPoint, midPoint1, midPoint2, endPoint],
[0, RATIO_MIDPOINT_1, RATIO_MIDPOINT_2, 1]
)
return curve
}

export const getLinesOnYAxis = (
boundingCurves,
columns,
rows,
getLineOnYAxis,
interpolatePointOnCurve
) => {
const columnsTotalCount = columns.length
Expand All @@ -84,7 +222,7 @@ export const getCurvesOnXAxis = (
}

for (let columnIdx = 0; columnIdx <= columnsTotalCount; columnIdx++) {
const curveSections = []
const lineSections = []
const columnValue = columns[columnIdx]
const columnRatio = columnValue / columnsTotalValue

Expand All @@ -95,56 +233,33 @@ export const getCurvesOnXAxis = (
const rowRatio = rowValue / rowsTotalValue
const rowEndRatio = rowRatioTotal + rowRatio

const startPoint = getPointOnSurface(
const curve = getLineOnYAxis(
boundingCurves,
columnRatioTotal,
rowRatioTotal,
interpolatePointOnCurve,
true
)

const endPoint = getPointOnSurface(
boundingCurves,
columnRatioTotal,
rowRatio,
rowEndRatio,
interpolatePointOnCurve
)
const midPoint1 = getPointOnSurface(
boundingCurves,
columnRatioTotal,
rowRatioTotal + rowRatio * RATIO_MIDPOINT_1,
interpolatePointOnCurve
)

const midPoint2 = getPointOnSurface(
boundingCurves,
columnRatioTotal,
rowRatioTotal + rowRatio * RATIO_MIDPOINT_2,
interpolatePointOnCurve
)

const curve = fitCubicBezierToPoints(
[startPoint, midPoint1, midPoint2, endPoint],
[0, RATIO_MIDPOINT_1, RATIO_MIDPOINT_2, 1]
)

rowRatioTotal = rowRatioTotal + rowRatio

curveSections.push({
lineSections.push({
...curve,
})
}
columnRatioTotal = columnRatioTotal + columnRatio
curves.push(curveSections)
curves.push(lineSections)
}

return curves
}

export const getCurvesOnYAxis = (
export const getLinesOnXAxis = (
boundingCurves,
columns,
rows,
getLineOnXAxis,
interpolatePointOnCurve
) => {
const columnsTotalCount = columns.length
Expand All @@ -162,7 +277,7 @@ export const getCurvesOnYAxis = (
}

for (let rowIdx = 0; rowIdx <= rowsTotalCount; rowIdx++) {
const curveSections = []
const lineSections = []
const rowValue = rows[rowIdx]
const rowRatio = rowValue / rowsTotalValue

Expand All @@ -173,47 +288,23 @@ export const getCurvesOnYAxis = (
const columnRatio = columnValue / columnsTotalValue
const columnEndRatio = columnRatioTotal + columnRatio

const startPoint = getPointOnSurface(
const curve = getLineOnXAxis(
boundingCurves,
columnRatioTotal,
rowRatioTotal,
interpolatePointOnCurve
)

const endPoint = getPointOnSurface(
boundingCurves,
columnRatio,
columnEndRatio,
rowRatioTotal,
interpolatePointOnCurve
)

const midPoint1 = getPointOnSurface(
boundingCurves,
columnRatioTotal + columnRatio * RATIO_MIDPOINT_1,
rowRatioTotal,
interpolatePointOnCurve
)

const midPoint2 = getPointOnSurface(
boundingCurves,
columnRatioTotal + columnRatio * RATIO_MIDPOINT_2,
rowRatioTotal,
interpolatePointOnCurve
)

const curve = fitCubicBezierToPoints(
[startPoint, midPoint1, midPoint2, endPoint],
[0, RATIO_MIDPOINT_1, RATIO_MIDPOINT_2, 1]
)

columnRatioTotal = columnRatioTotal + columnRatio
curveSections.push({
lineSections.push({
...curve,
})
}

rowRatioTotal = rowRatioTotal + rowRatio
curves.push(curveSections)
curves.push(lineSections)
}

return curves
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/patch3x3Grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const fixture3X3Grid = {
)
}
},
getCurves: () => {
getyLines: () => {
return {
xAxis: [
[
Expand Down
Loading

0 comments on commit 00de81f

Please sign in to comment.