-
-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(geom): add centerOfWeight(), centroidOfBounds(), update centroid()
- update centroid() impls for complex poly & poly, moved to centerOfWeight() - update centroid() to support more types - update pkg exports
- Loading branch information
1 parent
a4dec6b
commit 9048aac
Showing
6 changed files
with
111 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import type { Maybe } from "@thi.ng/api"; | ||
import type { MultiFn1O } from "@thi.ng/defmulti"; | ||
import { DEFAULT, defmulti } from "@thi.ng/defmulti/defmulti"; | ||
import { | ||
centerOfWeight2, | ||
complexCenterOfWeight2, | ||
} from "@thi.ng/geom-poly-utils/center-of-weight"; | ||
import type { Vec } from "@thi.ng/vectors"; | ||
import type { IShape } from "./api.js"; | ||
import type { ComplexPolygon } from "./api/complex-polygon.js"; | ||
import type { Polygon } from "./api/polygon.js"; | ||
import { centroid } from "./centroid.js"; | ||
import { __dispatch } from "./internal/dispatch.js"; | ||
|
||
/** | ||
* Attempts to compute the center of weight (CoW) for given `shape`, otherwise | ||
* returns `undefined`. | ||
* | ||
* @remarks | ||
* The actual CoW is only available for these types: | ||
* | ||
* - {@link ComplexPolygon} | ||
* - {@link Polygon} | ||
* | ||
* For all other shapes, the normal {@link centroid} calculation will be used. | ||
* Also see {@link centroidOfBounds}. | ||
* | ||
* @param shape | ||
* @param out | ||
*/ | ||
export const centerOfWeight: MultiFn1O<IShape, Vec, Maybe<Vec>> = defmulti< | ||
any, | ||
Maybe<Vec>, | ||
Maybe<Vec> | ||
>( | ||
__dispatch, | ||
{}, | ||
{ | ||
[DEFAULT]: ($: IShape, out?: Vec) => centroid($, out), | ||
|
||
complexpoly: ($: ComplexPolygon, out?) => | ||
complexCenterOfWeight2( | ||
$.boundary.points, | ||
$.children.map((c) => c.points), | ||
out | ||
), | ||
|
||
poly: ($: Polygon, out) => centerOfWeight2($.points, out), | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import type { Vec } from "@thi.ng/vectors"; | ||
import type { IShape } from "./api.js"; | ||
import { bounds } from "./bounds.js"; | ||
import { centroid } from "./centroid.js"; | ||
|
||
/** | ||
* Attempts to compute the bounding box of given `shape` and if available, | ||
* returns its centroid (or writes it to `out`, if given). Otherwise returns | ||
* `undefined`. | ||
* | ||
* @param shape | ||
* @param out | ||
*/ | ||
export const centroidOfBounds = (shape: IShape, out?: Vec) => { | ||
const b = bounds(shape); | ||
return b ? centroid(b, out) : undefined; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters