Skip to content

Commit

Permalink
feat(geom-resample): add sampleUniformX/Y() fns
Browse files Browse the repository at this point in the history
- update deps (add thi.ng/geom-isec)
- update pkg
  • Loading branch information
postspectacular committed Jan 5, 2023
1 parent 5b2adf3 commit 88f9ec5
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/geom-resample/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@thi.ng/checks": "^3.3.6",
"@thi.ng/geom-api": "^3.3.24",
"@thi.ng/geom-closest-point": "^2.1.38",
"@thi.ng/geom-isec": "^2.1.41",
"@thi.ng/math": "^5.3.18",
"@thi.ng/vectors": "^7.5.30"
},
Expand Down Expand Up @@ -80,6 +81,9 @@
".": {
"default": "./index.js"
},
"./axis": {
"default": "./axis.js"
},
"./resample": {
"default": "./resample.js"
},
Expand Down
79 changes: 79 additions & 0 deletions packages/geom-resample/src/axis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { intersectRayPolyline } from "@thi.ng/geom-isec/ray-poly";
import type { ReadonlyVec } from "@thi.ng/vectors";

/**
* Samples 2D polyline at uniform step distance in `[x1..x2]` interval along
* X-axis. Returns array of sampled points.
*
* @remarks
* The polyline is assumed to fully cover the given X interval, no gaps are
* allowed (or checked for). Sampling is done via ray-line intersection with the
* ray cast into Y+ direction, starting at given `startY`.
*
* Also see {@link sampleUniformY}.
*
* @example
* ```ts
* sampleUniformX([[0, 0], [3, 5], [5, 2]], 0, 5)
* // [
* // [ 0, 0 ],
* // [ 1, 1.666... ],
* // [ 2, 3.333... ],
* // [ 3, 5 ],
* // [ 4, 3.5 ],
* // [ 5, 2 ]
* // ]
* ```
*
* @param pts
* @param x1
* @param x2
* @param step
* @param startY
*/
export const sampleUniformX = (
pts: ReadonlyVec[],
x1: number,
x2: number,
step = 1,
startY = -1
) => {
const res: ReadonlyVec[] = [];
for (let x = x1; x <= x2; x += step) {
res.push(
<ReadonlyVec>(
intersectRayPolyline([x, startY], [0, 1], pts, false).isec!
)
);
}
return res;
};

/**
* Similar to {@link sampleUniformX}, but samples 2D polyline at uniform step
* distance in `[y1..y2]` interval along Y-axis. Returns array of sampled
* points.
*
* @param pts
* @param y1
* @param y2
* @param step
* @param startX
*/
export const sampleUniformY = (
pts: ReadonlyVec[],
y1: number,
y2: number,
step = 1,
startX = -1
) => {
const res: ReadonlyVec[] = [];
for (let y = y1; y <= y2; y += step) {
res.push(
<ReadonlyVec>(
intersectRayPolyline([startX, y], [1, 0], pts, false).isec!
)
);
}
return res;
};
1 change: 1 addition & 0 deletions packages/geom-resample/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./axis.js";
export * from "./resample.js";
export * from "./sampler.js";
export * from "./simplify.js";
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3369,6 +3369,7 @@ __metadata:
"@thi.ng/checks": ^3.3.6
"@thi.ng/geom-api": ^3.3.24
"@thi.ng/geom-closest-point": ^2.1.38
"@thi.ng/geom-isec": ^2.1.41
"@thi.ng/math": ^5.3.18
"@thi.ng/testament": ^0.3.8
"@thi.ng/vectors": ^7.5.30
Expand Down

0 comments on commit 88f9ec5

Please sign in to comment.