Skip to content

Commit

Permalink
refactor(geom): update pointInside & classifyPoint impls (delegate)
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jan 28, 2019
1 parent 895102d commit 226645f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
10 changes: 6 additions & 4 deletions packages/geom/src/ops/classify-point.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { defmulti, MultiFn2O } from "@thi.ng/defmulti";
import { IShape, Type } from "@thi.ng/geom-api";
import { classifyPointInTriangle2 } from "@thi.ng/geom-poly-utils";
import { EPS, sign } from "@thi.ng/math";
import { distSq, ReadonlyVec } from "@thi.ng/vectors";
import { classifyPointInCircle, classifyPointInTriangle2 } from "@thi.ng/geom-isec";
import { EPS } from "@thi.ng/math";
import { ReadonlyVec } from "@thi.ng/vectors";
import { Circle, Triangle } from "../api";
import { dispatch } from "../internal/dispatch";

Expand All @@ -12,10 +12,12 @@ classifyPoint.addAll({

[Type.CIRCLE]:
($: Circle, p: ReadonlyVec, eps = EPS) =>
sign($.r * $.r - distSq($.pos, p), eps),
classifyPointInCircle(p, $.pos, $.r, eps),

[Type.TRIANGLE]:
({ points }: Triangle, p: ReadonlyVec, eps = EPS) =>
classifyPointInTriangle2(p, points[0], points[1], points[2], eps),

});

classifyPoint.isa(Type.SPHERE, Type.CIRCLE);
32 changes: 20 additions & 12 deletions packages/geom/src/ops/point-inside.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { defmulti } from "@thi.ng/defmulti";
import { IShape, Type } from "@thi.ng/geom-api";
import { pointInPoly2, pointInTriangle2 } from "@thi.ng/geom-poly-utils";
import {
distSq,
pointInAABB,
pointInCircle,
pointInPolygon2,
pointInRect,
pointInTriangle2,
pointInSegment2
} from "@thi.ng/geom-isec";
import {
isInArray,
ReadonlyVec,
Vec
Expand All @@ -13,7 +19,8 @@ import {
Points,
Polygon,
Rect,
Triangle
Triangle,
Line
} from "../api";
import { dispatch } from "../internal/dispatch";

Expand All @@ -22,27 +29,28 @@ export const pointInside = defmulti<IShape, ReadonlyVec, boolean>(dispatch);
pointInside.addAll({

[Type.AABB]:
({ pos, size }: AABB, [x, y, z]: ReadonlyVec) =>
x >= pos[0] && x <= pos[0] + size[0] &&
y >= pos[1] && y <= pos[1] + size[1] &&
z >= pos[2] && z <= pos[2] + size[2],
($: AABB, p: ReadonlyVec) =>
pointInAABB(p, $.pos, $.size),

[Type.CIRCLE]:
($: Circle, p) =>
distSq($.pos, p) <= $.r * $.r,
pointInCircle(p, $.pos, $.r),

[Type.LINE]:
($: Line, p) =>
pointInSegment2(p, $.points[0], $.points[1]),

[Type.POINTS]:
({ points }: Points, p) =>
isInArray(p, points),

[Type.POLYGON]:
($: Polygon, p) =>
pointInPoly2($.points, p) > 0,
pointInPolygon2(p, $.points) > 0,

[Type.RECT]:
({ pos, size }: Rect, [x, y]: ReadonlyVec) =>
x >= pos[0] && x <= pos[0] + size[0] &&
y >= pos[1] && y <= pos[1] + size[1],
($: Rect, p: ReadonlyVec) =>
pointInRect(p, $.pos, $.size),

[Type.TRIANGLE]:
(tri: Triangle, p: ReadonlyVec) =>
Expand Down

0 comments on commit 226645f

Please sign in to comment.