Skip to content

Commit

Permalink
perf(geom): internal update shape attrib copying
Browse files Browse the repository at this point in the history
- rename __copyAttribsRaw => __copyAttribs
- avoid extraneous fn calls
  • Loading branch information
postspectacular committed Jun 2, 2024
1 parent 1e12707 commit fe0609a
Show file tree
Hide file tree
Showing 42 changed files with 164 additions and 119 deletions.
2 changes: 1 addition & 1 deletion packages/geom/src/api/aabb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class AABB implements AABBLike, IHiccupShape3<AABB> {
return new AABB(
set3([], this.pos),
set3([], this.size),
__copyAttribs(this)
__copyAttribs(this.attribs)
);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/geom/src/api/arc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class Arc implements IHiccupShape2<Arc>, IHiccupPathSegment {
this.end,
this.xl,
this.cw,
__copyAttribs(this)
__copyAttribs(this.attribs)
);
}

Expand Down
6 changes: 5 additions & 1 deletion packages/geom/src/api/circle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ export class Circle implements IHiccupShape2<Circle> {
) {}

copy(): Circle {
return new Circle(set2([], this.pos), this.r, __copyAttribs(this));
return new Circle(
set2([], this.pos),
this.r,
__copyAttribs(this.attribs)
);
}

withAttribs(attribs: Attribs) {
Expand Down
2 changes: 1 addition & 1 deletion packages/geom/src/api/complex-polygon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class ComplexPolygon implements IHiccupShape2<ComplexPolygon> {
return new ComplexPolygon(
this.boundary.copy(),
this.children.map((h) => h.copy()),
__copyAttribs(this)
__copyAttribs(this.attribs)
);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/geom/src/api/cubic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class Cubic
}

copy(): Cubic {
return <Cubic>__copyShape(Cubic, this);
return __copyShape(Cubic, this);
}

withAttribs(attribs: Attribs): Cubic {
Expand Down
2 changes: 1 addition & 1 deletion packages/geom/src/api/cubic3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class Cubic3
}

copy(): Cubic3 {
return <Cubic3>__copyShape(Cubic3, this);
return __copyShape(Cubic3, this);
}

withAttribs(attribs: Attribs): Cubic3 {
Expand Down
2 changes: 1 addition & 1 deletion packages/geom/src/api/ellipse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class Ellipse implements IHiccupShape2<Ellipse> {
return new Ellipse(
set2([], this.pos),
set2([], this.r),
__copyAttribs(this)
__copyAttribs(this.attribs)
);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/geom/src/api/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class Group implements IClear, IHiccupShape2<Group> {
}

copyTransformed(fn: FnU<IHiccupShape2>) {
return new Group(__copyAttribs(this), this.children.map(fn));
return new Group(__copyAttribs(this.attribs), this.children.map(fn));
}

withAttribs(attribs: Attribs) {
Expand Down
2 changes: 1 addition & 1 deletion packages/geom/src/api/group3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class Group3 implements IClear, IHiccupShape3<Group3> {
}

copyTransformed(fn: FnU<IHiccupShape3>) {
return new Group3(__copyAttribs(this), this.children.map(fn));
return new Group3(__copyAttribs(this.attribs), this.children.map(fn));
}

withAttribs(attribs: Attribs) {
Expand Down
2 changes: 1 addition & 1 deletion packages/geom/src/api/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class Path implements IClear, IHiccupShape2<Path> {
const p = new Path(
this.segments.map(__copySegment),
this.subPaths.map((sub) => sub.map(__copySegment)),
__copyAttribs(this)
__copyAttribs(this.attribs)
);
return p;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/geom/src/api/path3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class Path3 implements IClear, IHiccupShape3<Path3> {
const p = new Path3(
this.segments.map(__copySegment),
this.subPaths.map((sub) => sub.map(__copySegment)),
__copyAttribs(this)
__copyAttribs(this.attribs)
);
return p;
}
Expand Down
6 changes: 5 additions & 1 deletion packages/geom/src/api/plane.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ export class Plane implements IHiccupShape3<Plane> {
) {}

copy(): Plane {
return new Plane(set3([], this.normal), this.w, __copyAttribs(this));
return new Plane(
set3([], this.normal),
this.w,
__copyAttribs(this.attribs)
);
}

withAttribs(attribs: Attribs): Plane {
Expand Down
2 changes: 1 addition & 1 deletion packages/geom/src/api/points3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class Points3 extends APC implements IHiccupShape3<Points3> {
readonly dim = 3;

copy(): Points3 {
return <Points3>__copyShape(Points3, this);
return __copyShape(Points3, this);
}

withAttribs(attribs: Attribs) {
Expand Down
2 changes: 1 addition & 1 deletion packages/geom/src/api/ray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class Ray implements IHiccupShape2<Ray> {
return new Ray(
set2([], this.pos),
set2([], this.dir),
__copyAttribs(this)
__copyAttribs(this.attribs)
);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/geom/src/api/ray3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class Ray3 implements IHiccupShape3<Ray3> {
return new Ray3(
set3([], this.pos),
set3([], this.dir),
__copyAttribs(this)
__copyAttribs(this.attribs)
);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/geom/src/api/rect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class Rect implements AABBLike, IHiccupShape2<Rect> {
return new Rect(
set2([], this.pos),
set2([], this.size),
__copyAttribs(this)
__copyAttribs(this.attribs)
);
}

Expand Down
6 changes: 5 additions & 1 deletion packages/geom/src/api/sphere.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ export class Sphere implements IHiccupShape3<Sphere> {
) {}

copy(): Sphere {
return new Sphere(set3([], this.pos), this.r, __copyAttribs(this));
return new Sphere(
set3([], this.pos),
this.r,
__copyAttribs(this.attribs)
);
}

withAttribs(attribs: Attribs) {
Expand Down
6 changes: 5 additions & 1 deletion packages/geom/src/api/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ export class Text implements IHiccupShape2<Text> {
constructor(public pos: Vec, public body: any, public attribs?: Attribs) {}

copy(): Text {
return new Text(set([], this.pos), this.body, __copyAttribs(this));
return new Text(
set([], this.pos),
this.body,
__copyAttribs(this.attribs)
);
}

withAttribs(attribs: Attribs) {
Expand Down
4 changes: 2 additions & 2 deletions packages/geom/src/as-cubic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import { arc } from "./arc.js";
import { asPolygon } from "./as-polygon.js";
import { cubicFromArc, cubicFromLine, cubicFromQuadratic } from "./cubic.js";
import { cubicFromLine3, cubicFromQuadratic3 } from "./cubic3.js";
import { __copyAttribsRaw } from "./internal/copy.js";
import { __copyAttribs } from "./internal/copy.js";
import { __dispatch } from "./internal/dispatch.js";

export interface AsCubicOpts extends CubicOpts {
Expand Down Expand Up @@ -273,4 +273,4 @@ const __polyCubic = <T extends Cubic | Cubic3>(
};

const __attribs = (opts?: Partial<AsCubicOpts>, attribs?: Attribs) =>
attribs && opts?.attribs !== false ? __copyAttribsRaw(attribs) : undefined;
attribs && opts?.attribs !== false ? __copyAttribs(attribs) : undefined;
26 changes: 14 additions & 12 deletions packages/geom/src/as-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { Polyline } from "./api/polyline.js";
import { asCubic } from "./as-cubic.js";
import { asPolygon } from "./as-polygon.js";
import { asPolyline } from "./as-polyline.js";
import { __copyAttribs, __copyAttribsRaw } from "./internal/copy.js";
import { __copyAttribs } from "./internal/copy.js";
import { __dispatch } from "./internal/dispatch.js";
import { pathFromCubics } from "./path.js";
import { pathFromCubics3 } from "./path3.js";
Expand Down Expand Up @@ -82,7 +82,7 @@ export const asPath = <AsPathFn>(
{ boundary, children, attribs }: ComplexPolygon,
opts
) => {
attribs = __copyAttribsRaw(attribs);
attribs = __copyAttribs(attribs);
if (opts?.linear) {
return __linearPath(
boundary,
Expand Down Expand Up @@ -122,7 +122,7 @@ export const asPath = <AsPathFn>(
const __defaultImpl = (src: IShape, opts?: Partial<CubicOpts>) =>
(src.dim === 2 ? pathFromCubics : pathFromCubics3)(
<any>asCubic(src, { attribs: false, ...opts }),
__copyAttribs(src)
__copyAttribs(src.attribs)
);

/**
Expand Down Expand Up @@ -165,20 +165,22 @@ function __lineSegments(
return segments;
}

/**
* TODO update to support Path3
*
* @internal
*/
const __linearPath = (shape: APC, subPaths: PathSegment[][], closed = false) =>
shape.dim === 2
/** @internal */
const __linearPath = (
shape: APC,
subPaths: PathSegment[][],
closed = false
) => {
const attribs = __copyAttribs(shape.attribs);
return shape.dim === 2
? new Path(
__lineSegments(Line, shape.points, closed),
<PathSegment2[][]>subPaths,
__copyAttribs(shape)
attribs
)
: new Path3(
__lineSegments(Line3, shape.points, closed),
<PathSegment3[][]>subPaths,
__copyAttribs(shape)
attribs
);
};
4 changes: 2 additions & 2 deletions packages/geom/src/as-sector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { copy } from "@thi.ng/vectors/copy";
import type { Arc } from "./api/arc.js";
import { Line } from "./api/line.js";
import { Path } from "./api/path.js";
import { __copyAttribsRaw } from "./internal/copy.js";
import { __copyAttribs } from "./internal/copy.js";

/**
* Converts given arc into a closed path describing a sector (using the arc's
Expand All @@ -23,5 +23,5 @@ export const asSector = (arc: Arc, attribs?: Attribs) =>
{ type: "z" },
],
[],
attribs || __copyAttribsRaw(arc.attribs)
attribs || __copyAttribs(arc.attribs)
);
12 changes: 8 additions & 4 deletions packages/geom/src/center.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,21 @@ export const center = <CenterFn>(
$.end,
$.xl,
$.cw,
__copyAttribs($)
__copyAttribs($.attribs)
),

circle: ($: Circle, origin = ZERO2) =>
new Circle(set2([], origin), $.r, __copyAttribs($)),
new Circle(set2([], origin), $.r, __copyAttribs($.attribs)),

ellipse: ($: Ellipse, origin = ZERO2) =>
new Ellipse(set2([], origin), set2([], $.r), __copyAttribs($)),
new Ellipse(
set2([], origin),
set2([], $.r),
__copyAttribs($.attribs)
),

sphere: ($: Sphere, origin = ZERO3) =>
new Sphere(set3([], origin), $.r, __copyAttribs($)),
new Sphere(set3([], origin), $.r, __copyAttribs($.attribs)),
}
)
);
18 changes: 12 additions & 6 deletions packages/geom/src/clip-convex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ export type ClipConvexFn = {
const __clipVertices = ($: IShape, boundary: IShape | ReadonlyVec[]) => {
boundary = ensureVertices(boundary);
const pts = sutherlandHodgeman(vertices($), boundary, centroid(boundary));
return pts.length ? [new Polygon(pts, __copyAttribs($))] : undefined;
return pts.length
? [new Polygon(pts, __copyAttribs($.attribs))]
: undefined;
};

/**
Expand Down Expand Up @@ -102,7 +104,11 @@ export const clipConvex = <ClipConvexFn>(
if (clipped.length) res.push(new Polygon(clipped));
}
return [
new ComplexPolygon(res[0], res.slice(1), __copyAttribs($)),
new ComplexPolygon(
res[0],
res.slice(1),
__copyAttribs($.attribs)
),
];
},

Expand All @@ -125,7 +131,7 @@ export const clipConvex = <ClipConvexFn>(
ensureVertices(boundary)
);
return segments && segments.length
? [new Line(segments[0], __copyAttribs($))]
? [new Line(segments[0], __copyAttribs($.attribs))]
: undefined;
},

Expand All @@ -144,11 +150,11 @@ export const clipConvex = <ClipConvexFn>(
const res = new ComplexPolygon(
clipped[0],
[],
__copyAttribs($)
__copyAttribs($.attribs)
);
for (let sub of $.subPaths) {
clipped = __clipVertices(
new Path(sub, [], __copyAttribs($)),
new Path(sub, [], __copyAttribs($.attribs)),
boundary
);
if (clipped) {
Expand All @@ -167,7 +173,7 @@ export const clipConvex = <ClipConvexFn>(
centroid(boundary)
);
return pts.length
? [new Polygon(pts, __copyAttribs($))]
? [new Polygon(pts, __copyAttribs($.attribs))]
: undefined;
},

Expand Down
6 changes: 3 additions & 3 deletions packages/geom/src/convex-hull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ export const convexHull = <ConvexHullFn>defmulti<any, IShape>(
complexpoly: ($: ComplexPolygon) => convexHull($.boundary),

group: ($: IShape) =>
new Polygon(grahamScan2(vertices($)), __copyAttribs($)),
new Polygon(grahamScan2(vertices($)), __copyAttribs($.attribs)),

points: ($: PCLike) =>
new Polygon(grahamScan2($.points), __copyAttribs($)),
new Polygon(grahamScan2($.points), __copyAttribs($.attribs)),

tri: ($: IShape) => new Polygon(vertices($), __copyAttribs($)),
tri: ($: IShape) => new Polygon(vertices($), __copyAttribs($.attribs)),
}
);
4 changes: 2 additions & 2 deletions packages/geom/src/convolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export const convolve = <ConvoleFn>(
$.children.map((c) =>
__convolve(Polygon, convolveClosed, c, k, t, iter)
),
__copyAttribs($)
__copyAttribs($.attribs)
),

poly: ($: Polygon, k, t, iter) =>
Expand All @@ -118,7 +118,7 @@ const __convolve = <T extends PCLike>(
kernel: number[],
t?: number,
iter?: number
) => new ctor(fn($.points, kernel, t, iter), __copyAttribs($));
) => new ctor(fn($.points, kernel, t, iter), __copyAttribs($.attribs));

export const KERNEL_BOX = $BOX;

Expand Down
2 changes: 1 addition & 1 deletion packages/geom/src/cubic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function cubic(...args: any[]) {

export const cubicFromArc = (arc: Arc) =>
_arc(arc.pos, arc.r, arc.axis, arc.start, arc.end).map(
(c) => new Cubic(c, __copyAttribs(arc))
(c) => new Cubic(c, __copyAttribs(arc.attribs))
);

export const cubicFromLine = (a: Vec, b: Vec, attribs?: Attribs) =>
Expand Down
Loading

0 comments on commit fe0609a

Please sign in to comment.