Skip to content

Commit

Permalink
fix(geom): update various transform op impls for ComplexPolygon
Browse files Browse the repository at this point in the history
- minor other internal refactoring
  • Loading branch information
postspectacular committed Jun 2, 2024
1 parent bfaa0aa commit 1e12707
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 49 deletions.
3 changes: 2 additions & 1 deletion packages/geom/src/rotate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ export const rotate = <RotateFn>defmulti<any, number, IShape2>(
complexpoly: ($: ComplexPolygon, theta) =>
new ComplexPolygon(
rotate($.boundary, theta),
$.children.map((child) => rotate(child, theta))
$.children.map((child) => rotate(child, theta)),
__copyAttribs($)
),

cubic: tx(Cubic),
Expand Down
18 changes: 6 additions & 12 deletions packages/geom/src/scale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@ import { isNumber } from "@thi.ng/checks/is-number";
import type { MultiFn2 } from "@thi.ng/defmulti";
import { defmulti } from "@thi.ng/defmulti/defmulti";
import { unsupported } from "@thi.ng/errors/unsupported";
import type {
IHiccupShape2,
IShape,
IShape2,
IShape3,
PathSegment2,
} from "@thi.ng/geom-api";
import type { IShape, IShape2, IShape3, PathSegment2 } from "@thi.ng/geom-api";
import type { ReadonlyVec } from "@thi.ng/vectors";
import { mul2, mul3 } from "@thi.ng/vectors/mul";
import { mulN2, mulN3 } from "@thi.ng/vectors/muln";
Expand Down Expand Up @@ -121,8 +115,9 @@ export const scale = <ScaleFn>defmulti<any, number | ReadonlyVec, IShape>(

complexpoly: ($: ComplexPolygon, delta) =>
new ComplexPolygon(
<Polygon>scale($.boundary, delta),
$.children.map((child) => <Polygon>scale(child, delta))
scale($.boundary, delta),
$.children.map((child) => scale(child, delta)),
__copyAttribs($)
),

cubic: tx(Cubic),
Expand All @@ -136,15 +131,14 @@ export const scale = <ScaleFn>defmulti<any, number | ReadonlyVec, IShape>(
);
},

group: ($: Group, delta) =>
$.copyTransformed((x) => <IHiccupShape2>scale(x, delta)),
group: ($: Group, delta) => $.copyTransformed((x) => scale(x, delta)),

line: tx(Line),

path: ($: Path, delta) => {
delta = __asVec(delta);
const $scaleSegments = __segmentTransformer<PathSegment2>(
(geo) => <IShape2>scale(geo, delta),
(geo) => scale(geo, delta),
(p) => mul2([], p, <ReadonlyVec>delta)
);
return new Path(
Expand Down
21 changes: 6 additions & 15 deletions packages/geom/src/transform-vertices.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import type { Fn } from "@thi.ng/api";
import type { MultiFn2 } from "@thi.ng/defmulti";
import { defmulti } from "@thi.ng/defmulti/defmulti";
import type {
IHiccupShape2,
IShape,
IShape2,
IShape3,
PathSegment2,
} from "@thi.ng/geom-api";
import type { IShape, IShape2, IShape3, PathSegment2 } from "@thi.ng/geom-api";
import type { ReadonlyMat } from "@thi.ng/matrices";
import type { ReadonlyVec } from "@thi.ng/vectors";
import type { Arc } from "./api/arc.js";
Expand Down Expand Up @@ -110,26 +104,23 @@ export const transformVertices = <TransformVerticesFn>(

complexpoly: ($: ComplexPolygon, fn) =>
new ComplexPolygon(
<Polygon>transformVertices($.boundary, fn),
$.children.map(
(child) => <Polygon>transformVertices(child, fn)
)
transformVertices($.boundary, fn),
$.children.map((child) => transformVertices(child, fn)),
__copyAttribs($)
),

cubic: tx(Cubic),

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

line: tx(Line),

path: ($: Path, fn) => {
const $transformSegments = __segmentTransformer<PathSegment2>(
(geo) => {
__ensureNoArc(geo);
return <IShape2>transformVertices(geo, fn);
return transformVertices(geo, fn);
},
fn
);
Expand Down
16 changes: 5 additions & 11 deletions packages/geom/src/transform.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import type { MultiFn2 } from "@thi.ng/defmulti";
import { defmulti } from "@thi.ng/defmulti/defmulti";
import type {
IHiccupShape2,
IShape,
IShape2,
IShape3,
PathSegment2,
} from "@thi.ng/geom-api";
import type { IShape, IShape2, IShape3, PathSegment2 } from "@thi.ng/geom-api";
import type { ReadonlyMat } from "@thi.ng/matrices";
import { mulV } from "@thi.ng/matrices/mulv";
import type { Arc } from "./api/arc.js";
Expand Down Expand Up @@ -110,16 +104,16 @@ export const transform = <TransformFn>defmulti<any, ReadonlyMat, IShape>(

complexpoly: ($: ComplexPolygon, mat) =>
new ComplexPolygon(
<Polygon>transform($.boundary, mat),
$.children.map((child) => <Polygon>transform(child, mat))
transform($.boundary, mat),
$.children.map((child) => transform(child, mat)),
__copyAttribs($)
),

cubic: tx(Cubic),

cubic3: tx(Cubic3),

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

line: tx(Line),

Expand Down
16 changes: 6 additions & 10 deletions packages/geom/src/translate.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import type { MultiFn2 } from "@thi.ng/defmulti";
import { defmulti } from "@thi.ng/defmulti/defmulti";
import type {
IHiccupShape2,
IShape,
IShape2,
PathSegment2,
} from "@thi.ng/geom-api";
import type { IShape, PathSegment2 } from "@thi.ng/geom-api";
import type { ReadonlyVec } from "@thi.ng/vectors";
import { add2, add3 } from "@thi.ng/vectors/add";
import { set2, set3 } from "@thi.ng/vectors/set";
Expand Down Expand Up @@ -97,8 +92,9 @@ export const translate = <TranslateFn>defmulti<any, ReadonlyVec, IShape>(

complexpoly: ($: ComplexPolygon, delta) =>
new ComplexPolygon(
<Polygon>translate($.boundary, delta),
$.children.map((child) => <Polygon>translate(child, delta))
translate($.boundary, delta),
$.children.map((child) => translate(child, delta)),
__copyAttribs($)
),

cubic: tx(Cubic),
Expand All @@ -111,13 +107,13 @@ export const translate = <TranslateFn>defmulti<any, ReadonlyVec, IShape>(
),

group: ($: Group, delta) =>
$.copyTransformed((x) => <IHiccupShape2>translate(x, delta)),
$.copyTransformed((x) => translate(x, delta)),

line: tx(Line),

path: ($: Path, delta: ReadonlyVec) => {
const $translateSegments = __segmentTransformer<PathSegment2>(
(geo) => <IShape2>translate(geo, delta),
(geo) => translate(geo, delta),
(p) => add2([], p, delta)
);
return new Path(
Expand Down

0 comments on commit 1e12707

Please sign in to comment.