Skip to content

Commit

Permalink
feat(geom): enable TS strict compiler flags (refactor)
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jun 4, 2019
1 parent b9752ba commit aa10de0
Show file tree
Hide file tree
Showing 31 changed files with 267 additions and 125 deletions.
8 changes: 4 additions & 4 deletions packages/geom/src/internal/collate.ts
@@ -1,4 +1,4 @@
import { Vec, StridedVec } from "@thi.ng/vectors";
import { StridedVec, Vec } from "@thi.ng/vectors";

export interface CollateOpts {
buf: Vec;
Expand Down Expand Up @@ -27,9 +27,9 @@ export const collateWith = (
fn: (
buf: Vec,
src: Iterable<Readonly<StridedVec>>,
start,
cstride,
estride
start: number,
cstride: number,
estride: number
) => Vec,
pts: StridedVec[],
opts: Partial<CollateOpts>,
Expand Down
17 changes: 13 additions & 4 deletions packages/geom/src/ops/arc-length.ts
@@ -1,10 +1,19 @@
import { defmulti, MultiFn1 } from "@thi.ng/defmulti";
import { IObjectOf } from "@thi.ng/api";
import { defmulti, Implementation1 } from "@thi.ng/defmulti";
import { IShape, Type } from "@thi.ng/geom-api";
import { perimeter } from "@thi.ng/geom-poly-utils";
import { PI, TAU } from "@thi.ng/math";
import { dist } from "@thi.ng/vectors";
import {
Circle,
Ellipse,
Group,
Line,
Polygon,
Rect,
Triangle
} from "../api";
import { dispatch } from "../internal/dispatch";
import { Circle, Ellipse, Group, Line, Polygon, Rect, Triangle } from "../api";

/**
* Returns the arc length / perimeter / circumference of the given
Expand All @@ -24,9 +33,9 @@ import { Circle, Ellipse, Group, Line, Polygon, Rect, Triangle } from "../api";
* - Triangle
*
*/
export const arcLength: MultiFn1<IShape, number> = defmulti(dispatch);
export const arcLength = defmulti<IShape, number>(dispatch);

arcLength.addAll({
arcLength.addAll(<IObjectOf<Implementation1<unknown, number>>>{
[Type.CIRCLE]: ($: Circle) => TAU * $.r,

[Type.ELLIPSE]: ({ r: [a, b] }: Ellipse) =>
Expand Down
5 changes: 3 additions & 2 deletions packages/geom/src/ops/area.ts
@@ -1,4 +1,5 @@
import { defmulti, MultiFn1O } from "@thi.ng/defmulti";
import { IObjectOf } from "@thi.ng/api";
import { defmulti, Implementation1O, MultiFn1O } from "@thi.ng/defmulti";
import { IShape, Type } from "@thi.ng/geom-api";
import { polyArea2 } from "@thi.ng/geom-poly-utils";
import { PI } from "@thi.ng/math";
Expand Down Expand Up @@ -51,7 +52,7 @@ import { dispatch } from "../internal/dispatch";
*/
export const area: MultiFn1O<IShape, boolean, number> = defmulti(dispatch);

area.addAll({
area.addAll(<IObjectOf<Implementation1O<unknown, boolean, number>>>{
[Type.AABB]: ({ size: [w, h, d] }: AABB) => 2 * (w * h + w * d + h * d),

[Type.ARC]:
Expand Down
20 changes: 16 additions & 4 deletions packages/geom/src/ops/as-cubic.ts
@@ -1,15 +1,27 @@
import { defmulti } from "@thi.ng/defmulti";
import { IObjectOf } from "@thi.ng/api";
import { defmulti, Implementation1 } from "@thi.ng/defmulti";
import { IShape, Type } from "@thi.ng/geom-api";
import { EPS, HALF_PI, roundEps, sincos } from "@thi.ng/math";
import {
EPS,
HALF_PI,
roundEps,
sincos
} from "@thi.ng/math";
import { mapcat } from "@thi.ng/transducers";
import { add2, magSq2 } from "@thi.ng/vectors";
import {
Arc,
Cubic,
Line,
Path,
Quadratic
} from "../api";
import { cubicFromLine, cubicFromQuadratic } from "../ctors/cubic";
import { dispatch } from "../internal/dispatch";
import { Arc, Cubic, Line, Path, Quadratic } from "../api";

export const asCubic = defmulti<IShape, Cubic[]>(dispatch);

asCubic.addAll({
asCubic.addAll(<IObjectOf<Implementation1<unknown, Cubic[]>>>{
[Type.ARC]: ($: Arc) => {
const p = $.pointAtTheta($.start);
const q = $.pointAtTheta($.end);
Expand Down
13 changes: 9 additions & 4 deletions packages/geom/src/ops/as-polyline.ts
@@ -1,4 +1,5 @@
import { defmulti, MultiFn1O } from "@thi.ng/defmulti";
import { IObjectOf } from "@thi.ng/api";
import { defmulti, Implementation1O, MultiFn1O } from "@thi.ng/defmulti";
import { IShape, SamplingOpts, Type } from "@thi.ng/geom-api";
import { Path, Polyline } from "../api";
import { dispatch } from "../internal/dispatch";
Expand All @@ -10,8 +11,12 @@ export const asPolyline: MultiFn1O<
Polyline
> = defmulti(dispatch);

asPolyline.addAll({
[Type.POINTS]: ($, opts) =>
asPolyline.addAll(<
IObjectOf<
Implementation1O<unknown, number | Partial<SamplingOpts>, Polyline>
>
>{
[Type.POINTS]: ($: IShape, opts) =>
new Polyline(vertices($, opts), { ...$.attribs }),

[Type.PATH]: ($: Path, opts) => {
Expand All @@ -21,7 +26,7 @@ asPolyline.addAll({
});
},

[Type.POLYGON]: ($, opts) => {
[Type.POLYGON]: ($: IShape, opts) => {
const pts = vertices($, opts);
return new Polyline(pts.concat([pts[0]]), { ...$.attribs });
}
Expand Down
26 changes: 19 additions & 7 deletions packages/geom/src/ops/bounds.ts
@@ -1,9 +1,21 @@
import { defmulti } from "@thi.ng/defmulti";
import { AABBLike, IShape, PathSegment, PCLike, Type } from "@thi.ng/geom-api";
import { IObjectOf } from "@thi.ng/api";
import { defmulti, Implementation1 } from "@thi.ng/defmulti";
import {
AABBLike,
IShape,
PathSegment,
PCLike,
Type
} from "@thi.ng/geom-api";
import { bounds as arcBounds } from "@thi.ng/geom-arc";
import { bounds as _bounds } from "@thi.ng/geom-poly-utils";
import { cubicBounds, quadraticBounds } from "@thi.ng/geom-splines";
import { comp, filter, iterator1, map } from "@thi.ng/transducers";
import {
comp,
filter,
iterator1,
map
} from "@thi.ng/transducers";
import {
max,
MAX2,
Expand All @@ -15,9 +27,6 @@ import {
sub2,
subN2
} from "@thi.ng/vectors";
import { rectFromMinMax } from "../ctors/rect";
import { collBounds } from "../internal/coll-bounds";
import { dispatch } from "../internal/dispatch";
import {
Arc,
Circle,
Expand All @@ -29,10 +38,13 @@ import {
Quadratic,
Rect
} from "../api";
import { rectFromMinMax } from "../ctors/rect";
import { collBounds } from "../internal/coll-bounds";
import { dispatch } from "../internal/dispatch";

export const bounds = defmulti<IShape, AABBLike>(dispatch);

bounds.addAll({
bounds.addAll(<IObjectOf<Implementation1<unknown, AABBLike>>>{
[Type.ARC]: ($: Arc) =>
rectFromMinMax(...arcBounds($.pos, $.r, $.axis, $.start, $.end)),

Expand Down
26 changes: 22 additions & 4 deletions packages/geom/src/ops/center.ts
@@ -1,10 +1,28 @@
import { DEFAULT, defmulti, MultiFn1O } from "@thi.ng/defmulti";
import { IObjectOf } from "@thi.ng/api";
import {
DEFAULT,
defmulti,
Implementation1O,
MultiFn1O
} from "@thi.ng/defmulti";
import { IShape, Type } from "@thi.ng/geom-api";
import { ReadonlyVec, set2, set3, submN, ZERO2, ZERO3 } from "@thi.ng/vectors";
import {
ReadonlyVec,
set2,
set3,
submN,
ZERO2,
ZERO3
} from "@thi.ng/vectors";
import {
Arc,
Circle,
Ellipse,
Sphere
} from "../api";
import { dispatch } from "../internal/dispatch";
import { centroid } from "./centroid";
import { translate } from "./translate";
import { Arc, Circle, Ellipse, Sphere } from "../api";

export const center: MultiFn1O<IShape, ReadonlyVec, IShape> = defmulti(
dispatch
Expand All @@ -14,7 +32,7 @@ center.add(DEFAULT, ($, origin = ZERO3) =>
translate($, submN(null, centroid($), origin, -1))
);

center.addAll({
center.addAll(<IObjectOf<Implementation1O<unknown, ReadonlyVec, IShape>>>{
[Type.ARC]: ($: Arc, origin = ZERO2) =>
new Arc(
set2([], origin),
Expand Down
5 changes: 3 additions & 2 deletions packages/geom/src/ops/centroid.ts
@@ -1,4 +1,5 @@
import { defmulti, MultiFn1O } from "@thi.ng/defmulti";
import { IObjectOf } from "@thi.ng/api";
import { defmulti, Implementation1O, MultiFn1O } from "@thi.ng/defmulti";
import {
AABBLike,
IShape,
Expand Down Expand Up @@ -28,7 +29,7 @@ import { bounds } from "./bounds";

export const centroid: MultiFn1O<IShape, Vec, Vec> = defmulti(dispatch);

centroid.addAll({
centroid.addAll(<IObjectOf<Implementation1O<unknown, Vec, Vec>>>{
[Type.CIRCLE]: ($: Circle, out?) => set(out || [], $.pos),

[Type.GROUP]: ($: Group) => centroid(bounds($)),
Expand Down
9 changes: 6 additions & 3 deletions packages/geom/src/ops/classify-point.ts
@@ -1,4 +1,5 @@
import { defmulti, MultiFn2O } from "@thi.ng/defmulti";
import { IObjectOf } from "@thi.ng/api";
import { defmulti, Implementation2O, MultiFn2O } from "@thi.ng/defmulti";
import { IShape, Type } from "@thi.ng/geom-api";
import { classifyPointInCircle, classifyPointInTriangle2 } from "@thi.ng/geom-isec";
import { EPS, sign } from "@thi.ng/math";
Expand All @@ -11,9 +12,11 @@ export const classifyPoint: MultiFn2O<
ReadonlyVec,
number,
number
> = defmulti(dispatch);
> = defmulti(<any>dispatch);

classifyPoint.addAll({
classifyPoint.addAll(<
IObjectOf<Implementation2O<unknown, ReadonlyVec, number, number>>
>{
[Type.CIRCLE]: ($: Circle, p, eps = EPS) =>
classifyPointInCircle(p, $.pos, $.r, eps),

Expand Down
7 changes: 4 additions & 3 deletions packages/geom/src/ops/clip-convex.ts
@@ -1,4 +1,5 @@
import { defmulti } from "@thi.ng/defmulti";
import { IObjectOf } from "@thi.ng/api";
import { defmulti, Implementation2 } from "@thi.ng/defmulti";
import { IShape, Type } from "@thi.ng/geom-api";
import { sutherlandHodgeman } from "@thi.ng/geom-clip";
import { Polygon } from "../api";
Expand All @@ -8,7 +9,7 @@ import { vertices } from "./vertices";

export const clipConvex = defmulti<IShape, IShape, Polygon>(dispatch);

clipConvex.addAll({
clipConvex.addAll(<IObjectOf<Implementation2<unknown, unknown, Polygon>>>{
[Type.POLYGON]: ($: Polygon, boundary: IShape) =>
new Polygon(
sutherlandHodgeman(
Expand All @@ -19,7 +20,7 @@ clipConvex.addAll({
{ ...$.attribs }
),

[Type.RECT]: ($, boundary: IShape) =>
[Type.RECT]: ($: IShape, boundary: IShape) =>
new Polygon(
sutherlandHodgeman(
vertices($),
Expand Down
13 changes: 8 additions & 5 deletions packages/geom/src/ops/closest-point.ts
@@ -1,4 +1,5 @@
import { defmulti, MultiFn2O } from "@thi.ng/defmulti";
import { IObjectOf } from "@thi.ng/api";
import { defmulti, Implementation2O, MultiFn2O } from "@thi.ng/defmulti";
import { IShape, PCLike, Type } from "@thi.ng/geom-api";
import { closestPoint as closestPointArc } from "@thi.ng/geom-arc";
import {
Expand Down Expand Up @@ -30,11 +31,13 @@ import {
} from "../api";
import { dispatch } from "../internal/dispatch";

export const closestPoint: MultiFn2O<IShape, ReadonlyVec, Vec, Vec> = defmulti(
dispatch
);
export const closestPoint: MultiFn2O<IShape, ReadonlyVec, Vec, Vec> = defmulti(<
any
>dispatch);

closestPoint.addAll({
closestPoint.addAll(<
IObjectOf<Implementation2O<unknown, ReadonlyVec, Vec, Vec>>
>{
[Type.AABB]: ($: AABB, p, out = []) =>
closestPointAABB(p, $.pos, add3([], $.pos, $.size), out),

Expand Down
9 changes: 5 additions & 4 deletions packages/geom/src/ops/convex-hull.ts
@@ -1,4 +1,5 @@
import { defmulti } from "@thi.ng/defmulti";
import { IObjectOf } from "@thi.ng/api";
import { defmulti, Implementation1 } from "@thi.ng/defmulti";
import { IShape, PCLike, Type } from "@thi.ng/geom-api";
import { grahamScan2 } from "@thi.ng/geom-hull";
import { Polygon } from "../api";
Expand All @@ -7,13 +8,13 @@ import { vertices } from "./vertices";

export const convexHull = defmulti<IShape, IShape>(dispatch);

convexHull.addAll({
[Type.GROUP]: ($) => new Polygon(vertices($), { ...$.attribs }),
convexHull.addAll(<IObjectOf<Implementation1<unknown, IShape>>>{
[Type.GROUP]: ($: IShape) => new Polygon(vertices($), { ...$.attribs }),

[Type.POINTS]: ($: PCLike) =>
new Polygon(grahamScan2($.points), { ...$.attribs }),

[Type.TRIANGLE]: ($) => $.copy()
[Type.TRIANGLE]: ($: IShape) => $.copy()
});

convexHull.isa(Type.CIRCLE, Type.TRIANGLE);
Expand Down
13 changes: 11 additions & 2 deletions packages/geom/src/ops/edges.ts
@@ -1,4 +1,5 @@
import { defmulti, MultiFn1O } from "@thi.ng/defmulti";
import { IObjectOf } from "@thi.ng/api";
import { defmulti, Implementation1O, MultiFn1O } from "@thi.ng/defmulti";
import { IShape, SamplingOpts, Type } from "@thi.ng/geom-api";
import { VecPair } from "@thi.ng/vectors";
import { Polygon, Polyline, Rect } from "../api";
Expand All @@ -12,7 +13,15 @@ export const edges: MultiFn1O<
Iterable<VecPair>
> = defmulti(dispatch);

edges.addAll({
edges.addAll(<
IObjectOf<
Implementation1O<
unknown,
number | Partial<SamplingOpts>,
Iterable<VecPair>
>
>
>{
[Type.POLYGON]: ($: Polygon) => edgeIterator($.points, true),

[Type.POLYLINE]: ($: Polyline) => edgeIterator($.points),
Expand Down
18 changes: 14 additions & 4 deletions packages/geom/src/ops/fit-into-bounds.ts
@@ -1,6 +1,16 @@
import { IShape } from "@thi.ng/geom-api";
import { concat, ReadonlyMat, scale23, translation23 } from "@thi.ng/matrices";
import { div2, neg, ReadonlyVec } from "@thi.ng/vectors";
import {
concat,
ReadonlyMat,
scale23,
translation23
} from "@thi.ng/matrices";
import {
div2,
neg,
ReadonlyVec,
Vec
} from "@thi.ng/vectors";
import { Rect } from "../api";
import { collBounds } from "../internal/coll-bounds";
import { bounds } from "./bounds";
Expand Down Expand Up @@ -39,8 +49,8 @@ export const fitAllIntoBounds2 = (shapes: IShape[], dest: Rect) => {
const s = w > 0 && h > 0 ? Math.min(w, h) : w > 0 ? w : h;
const smat = scale23([], s);
const b = center(transform(src, smat), centroid(dest));
const c1 = [];
const c2 = [];
const c1: Vec = [];
const c2: Vec = [];
const res: IShape[] = [];
for (let i = shapes.length; --i >= 0; ) {
const s = shapes[i];
Expand Down

0 comments on commit aa10de0

Please sign in to comment.