Skip to content

Commit

Permalink
perf(geom): update rotate() impls to reuse precomputed matrices
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed May 27, 2024
1 parent f80f67a commit 00e33f0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
16 changes: 11 additions & 5 deletions packages/geom/src/internal/rotate.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import type { PCLike, PCLikeConstructor } from "@thi.ng/geom-api";
import { mulV22, mulV33 } from "@thi.ng/matrices/mulv";
import { rotation22 } from "@thi.ng/matrices/rotation";
import { rotationAroundAxis33 } from "@thi.ng/matrices/rotation-around-axis";
import type { ReadonlyVec } from "@thi.ng/vectors";
import { rotate } from "@thi.ng/vectors/rotate";
import { rotateAroundAxis3 } from "@thi.ng/vectors/rotate-around-axis";
import { __copyAttribs } from "./copy.js";

export const __rotatedPoints = (pts: ReadonlyVec[], theta: number) =>
pts.map((x) => rotate([], x, theta));
export const __rotatedPoints = (pts: ReadonlyVec[], theta: number) => {
const mat = rotation22([], theta);
return pts.map((x) => mulV22([], x, mat));
};

export const __rotatedPoints3 = (
pts: ReadonlyVec[],
axis: ReadonlyVec,
theta: number
) => pts.map((x) => rotateAroundAxis3([], x, axis, theta));
) => {
const mat = rotationAroundAxis33([], axis, theta);
return pts.map((x) => mulV33([], x, mat));
};

export const __rotatedShape =
<T extends PCLike>(ctor: PCLikeConstructor<T>) =>
Expand Down
14 changes: 8 additions & 6 deletions packages/geom/src/rotate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { MultiFn2 } from "@thi.ng/defmulti";
import { defmulti } from "@thi.ng/defmulti/defmulti";
import type { IHiccupShape2, IShape2, PathSegment2 } from "@thi.ng/geom-api";
import type { IShape2, PathSegment2 } from "@thi.ng/geom-api";
import { mulV22 } from "@thi.ng/matrices/mulv";
import { rotation22 } from "@thi.ng/matrices/rotation";
import { rotate as $rotate } from "@thi.ng/vectors/rotate";
import type { Arc } from "./api/arc.js";
import { BPatch } from "./api/bpatch.js";
Expand Down Expand Up @@ -82,24 +84,24 @@ export const rotate = <RotateFn>defmulti<any, number, IShape2>(

complexpoly: ($: ComplexPolygon, theta) =>
new ComplexPolygon(
<Polygon>rotate($.boundary, theta),
$.children.map((child) => <Polygon>rotate(child, theta))
rotate($.boundary, theta),
$.children.map((child) => rotate(child, theta))
),

cubic: tx(Cubic),

group: ($: Group, theta) =>
$.copyTransformed((x) => <IHiccupShape2>rotate(x, theta)),
group: ($: Group, theta) => $.copyTransformed((x) => rotate(x, theta)),

line: tx(Line),

path: ($: Path, theta) => {
const mat = rotation22([], theta);
const $rotateSegments = __segmentTransformer<PathSegment2>(
(geo) => {
__ensureNoArc(geo);
return rotate(geo, theta);
},
(p) => $rotate([], p, theta)
(p) => mulV22([], p, mat)
);
return new Path(
$rotateSegments($.segments),
Expand Down

0 comments on commit 00e33f0

Please sign in to comment.