Skip to content

Commit

Permalink
feat(geom): add various dummy op impls for Dummy shape type
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jun 3, 2024
1 parent b6e18bc commit 05eeb9f
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 3 deletions.
3 changes: 3 additions & 0 deletions packages/geom/src/bounds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export type BoundsFn = {
* - {@link BPatch}
* - {@link Circle}
* - {@link Cubic}
* - {@link Dummy} (returns `undefined`)
* - {@link Ellipse}
* - {@link Group}
* - {@link Line}
Expand Down Expand Up @@ -134,6 +135,8 @@ export const bounds = <BoundsFn>defmulti<any, Maybe<number>, Maybe<AABBLike>>(
margin
),

dummy: () => undefined,

ellipse: ($: Ellipse, margin = 0) => {
const r = addN2([], $.r, margin);
return new Rect(sub2([], $.pos, r), mul2(null, [2, 2], r));
Expand Down
2 changes: 2 additions & 0 deletions packages/geom/src/center.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export const center = <CenterFn>(
circle: ($: Circle, origin = ZERO2) =>
new Circle(set2([], origin), $.r, __copyAttribs($.attribs)),

dummy: ($) => $,

ellipse: ($: Ellipse, origin = ZERO2) =>
new Ellipse(
set2([], origin),
Expand Down
3 changes: 3 additions & 0 deletions packages/geom/src/centroid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { __dispatch } from "./internal/dispatch.js";
* - {@link Circle}
* - {@link ComplexPolygon}
* - {@link Cubic}
* - {@link Dummy} (returns `undefined`)
* - {@link Ellipse}
* - {@link Group}
* - {@link Line}
Expand Down Expand Up @@ -82,6 +83,8 @@ export const centroid: MultiFn1O<IShape, Vec, Maybe<Vec>> = defmulti<
out
),

dummy: () => undefined,

group: ($: Group, out?) => {
const b = bounds($);
return b ? centroid(b, out) : undefined;
Expand Down
3 changes: 3 additions & 0 deletions packages/geom/src/clip-convex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ const __clipVertices = ($: IShape, boundary: IShape | ReadonlyVec[]) => {
*
* - {@link Circle}
* - {@link ComplexPolygon}
* - {@link Dummy}
* - {@link Ellipse}
* - {@link Group}
* - {@link Line}
Expand Down Expand Up @@ -115,6 +116,8 @@ export const clipConvex = <ClipConvexFn>(
];
},

dummy: ($) => [$],

group: ({ children, attribs }: Group, boundary) => {
boundary = ensureVertices(boundary);
const clipped: IHiccupShape2[] = [];
Expand Down
2 changes: 2 additions & 0 deletions packages/geom/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export * from "./api/circle.js";
export * from "./api/complex-polygon.js";
export * from "./api/cubic.js";
export * from "./api/cubic3.js";
export * from "./api/dummy.js";
export * from "./api/ellipse.js";
export * from "./api/group.js";
export * from "./api/group3.js";
Expand Down Expand Up @@ -40,6 +41,7 @@ export * from "./complex-polygon.js";
export * from "./complex-polygon-from-path.js";
export * from "./cubic.js";
export * from "./cubic3.js";
export * from "./dummy.js";
export * from "./ellipse.js";
export * from "./group.js";
export * from "./group3.js";
Expand Down
11 changes: 10 additions & 1 deletion packages/geom/src/point-inside.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { MultiFn2 } from "@thi.ng/defmulti";
import { defmulti } from "@thi.ng/defmulti/defmulti";
import { DEFAULT, defmulti } from "@thi.ng/defmulti/defmulti";
import type { IShape } from "@thi.ng/geom-api";
import {
pointInAABB,
Expand All @@ -15,6 +15,7 @@ import { isInArray } from "@thi.ng/vectors/eqdelta";
import type { AABB } from "./api/aabb.js";
import type { Circle } from "./api/circle.js";
import type { ComplexPolygon } from "./api/complex-polygon.js";
import type { Group } from "./api/group.js";
import type { Line } from "./api/line.js";
import type { Points } from "./api/points.js";
import type { Polygon } from "./api/polygon.js";
Expand All @@ -32,6 +33,8 @@ import { __dispatch } from "./internal/dispatch.js";
* - {@link AABB}
* - {@link Circle}
* - {@link ComplexPolygon}
* - {@link Group}
* - {@link Group3}
* - {@link Line} (if `p` is on line segment)
* - {@link Points} (i.e. if `p` is one of the points in the cloud)
* - {@link Points3} (same as w/ Points)
Expand All @@ -52,11 +55,14 @@ export const pointInside: MultiFn2<IShape, ReadonlyVec, boolean> = defmulti<
>(
__dispatch,
{
group3: "group",
points3: "points",
quad: "poly",
sphere: "circle",
},
{
[DEFAULT]: () => false,

aabb: ($: AABB, p: ReadonlyVec) => pointInAABB(p, $.pos, $.size),

circle: ($: Circle, p) => pointInCircle(p, $.pos, $.r),
Expand All @@ -66,6 +72,9 @@ export const pointInside: MultiFn2<IShape, ReadonlyVec, boolean> = defmulti<
? !$.children.some((child) => pointInPolygon2(p, child.points))
: false,

group: ($: Group, p) =>
$.children.some((child) => pointInside(child, p)),

line: ($: Line, p) => pointInSegment(p, $.points[0], $.points[1]),

points: ({ points }: Points, p) => isInArray(p, points),
Expand Down
3 changes: 3 additions & 0 deletions packages/geom/src/rotate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export type RotateFn = {
* - {@link Circle}
* - {@link ComplexPolygon}
* - {@link Cubic}
* - {@link Dummy}
* - {@link Ellipse}
* - {@link Group}
* - {@link Line}
Expand Down Expand Up @@ -93,6 +94,8 @@ export const rotate = <RotateFn>defmulti<any, number, IShape2>(
__copyAttribs($.attribs)
),

dummy: ($) => $,

cubic: tx(Cubic),

group: ($: Group, theta) => $.copyTransformed((x) => rotate(x, theta)),
Expand Down
3 changes: 3 additions & 0 deletions packages/geom/src/scale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export type ScaleFn = {
* - {@link ComplexPolygon}
* - {@link Cubic}
* - {@link Cubic3}
* - {@link Dummy} (returns `undefined`)
* - {@link Ellipse}
* - {@link Group}
* - {@link Group3}
Expand Down Expand Up @@ -149,6 +150,8 @@ export const scale = <ScaleFn>defmulti<any, number | ReadonlyVec, IShape>(

cubic3: tx(Cubic3),

dummy: ($) => $,

ellipse: ($: Ellipse, delta) => {
delta = __asVec(delta);
return new Ellipse(
Expand Down
3 changes: 3 additions & 0 deletions packages/geom/src/transform-vertices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export type TransformVerticesFn = {
* - {@link ComplexPolygon}
* - {@link Cubic}
* - {@link Cubic3}
* - {@link Dummy}
* - {@link Ellipse}
* - {@link Group}
* - {@link Group3}
Expand Down Expand Up @@ -131,6 +132,8 @@ export const transformVertices = <TransformVerticesFn>(

cubic3: tx(Cubic3),

dummy: ($) => $,

group: ($: Group, fn) =>
$.copyTransformed((x) => transformVertices(x, fn)),

Expand Down
7 changes: 5 additions & 2 deletions packages/geom/src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { MultiFn2 } from "@thi.ng/defmulti";
import { defmulti } from "@thi.ng/defmulti/defmulti";
import type { Attribs, IShape, IShape2, IShape3 } from "@thi.ng/geom-api";
import type { ReadonlyMat } from "@thi.ng/matrices";
import { mulV, mulV23, mulV44 } from "@thi.ng/matrices/mulv";
import { mulV23, mulV44 } from "@thi.ng/matrices/mulv";
import type { Vec } from "@thi.ng/vectors";
import type { Arc } from "./api/arc.js";
import { BPatch } from "./api/bpatch.js";
Expand Down Expand Up @@ -76,6 +76,7 @@ export type TransformFn = {
* - {@link ComplexPolygon}
* - {@link Cubic}
* - {@link Cubic3}
* - {@link Dummy}
* - {@link Ellipse}
* - {@link Group}
* - {@link Group3}
Expand Down Expand Up @@ -126,6 +127,8 @@ export const transform = <TransformFn>defmulti<any, ReadonlyMat, IShape>(

cubic3: tx(Cubic3),

dummy: ($) => $,

group: ($: Group, mat) => $.copyTransformed((x) => transform(x, mat)),

line: tx(Line),
Expand Down Expand Up @@ -162,7 +165,7 @@ export const transform = <TransformFn>defmulti<any, ReadonlyMat, IShape>(
transform(new Quad(vertices($), __copyAttribs($.attribs)), mat),

text: ($: Text, mat) =>
new Text(mulV([], mat, $.pos!), $.body, __copyAttribs($.attribs)),
new Text(mulV23([], mat, $.pos!), $.body, __copyAttribs($.attribs)),

tri: tx(Triangle),

Expand Down
4 changes: 4 additions & 0 deletions packages/geom/src/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ export type TranslateFn = {
* - {@link ComplexPolygon}
* - {@link Cubic}
* - {@link Cubic3}
* - {@link Dummy}
* - {@link Ellipse}
* - {@link Group}
* - {@link Group3}
* - {@link Line}
* - {@link Path}
* - {@link Points}
Expand Down Expand Up @@ -110,6 +112,8 @@ export const translate = <TranslateFn>defmulti<any, ReadonlyVec, IShape>(

cubic3: tx(Cubic3),

dummy: ($) => $,

ellipse: ($: Ellipse, delta) =>
new Ellipse(
add2([], $.pos, delta),
Expand Down
4 changes: 4 additions & 0 deletions packages/geom/src/vertices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import { peek } from "@thi.ng/arrays/peek";
* - {@link Circle}
* - {@link ComplexPolygon}
* - {@link Cubic}
* - {@link Dummy}
* - {@link Ellipse}
* - {@link Group}
* - {@link Line}
Expand Down Expand Up @@ -91,6 +92,7 @@ export const vertices: MultiFn1O<
{
bpatch: "points",
cubic3: "cubic",
group3: "group",
line: "polyline",
line3: "polyline",
path3: "path",
Expand Down Expand Up @@ -160,6 +162,8 @@ export const vertices: MultiFn1O<
cubic: ($: Cubic, opts?) =>
sampleCubic($.points, __sampleAttribs(opts, $.attribs)),

dummy: () => [],

ellipse: ($: Ellipse, opts = DEFAULT_SAMPLES) => {
opts = __sampleAttribs(opts, $.attribs)!;
const buf: Vec[] = [];
Expand Down

0 comments on commit 05eeb9f

Please sign in to comment.