Skip to content

Commit

Permalink
feat(geom): add IAttributed impls for all shape types
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jun 24, 2022
1 parent 35799db commit ccb40f1
Show file tree
Hide file tree
Showing 22 changed files with 125 additions and 11 deletions.
4 changes: 4 additions & 0 deletions packages/geom/src/api/aabb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export class AABB implements AABBLike {
);
}

withAttribs(attribs: Attribs): AABB {
return new AABB(this.pos, this.size, attribs);
}

max() {
return add3([], this.pos, this.size);
}
Expand Down
7 changes: 5 additions & 2 deletions packages/geom/src/api/apc.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import type { Attribs, IShape, PCLike } from "@thi.ng/geom-api";
import type { Attribs, PCLike } from "@thi.ng/geom-api";
import type { Vec } from "@thi.ng/vectors";

export abstract class APC implements PCLike {
constructor(public points: Vec[] = [], public attribs?: Attribs) {}

abstract get type(): number | string;
abstract copy(): IShape;

abstract copy(): APC;

abstract withAttribs(attribs: Attribs): APC;

*[Symbol.iterator]() {
yield* this.points;
Expand Down
13 changes: 13 additions & 0 deletions packages/geom/src/api/arc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ export class Arc implements IHiccupShape, IHiccupPathSegment {
);
}

withAttribs(attribs: Attribs): Arc {
return new Arc(
this.pos,
this.r,
this.axis,
this.start,
this.end,
this.xl,
this.cw,
attribs
);
}

equiv(o: any) {
return (
o instanceof Arc &&
Expand Down
4 changes: 4 additions & 0 deletions packages/geom/src/api/bpatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ export class BPatch extends APC implements IHiccupShape {
return <BPatch>__copyShape(BPatch, this);
}

withAttribs(attribs: Attribs): BPatch {
return new BPatch(this.points, attribs);
}

edges() {
const p = this.points;
return [
Expand Down
4 changes: 4 additions & 0 deletions packages/geom/src/api/circle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export class Circle implements IHiccupShape {
return new Circle(set([], this.pos), this.r, __copyAttribs(this));
}

withAttribs(attribs: Attribs): Circle {
return new Circle(this.pos, this.r, attribs);
}

toHiccup() {
return ["circle", this.attribs, this.pos, this.r];
}
Expand Down
6 changes: 5 additions & 1 deletion packages/geom/src/api/cubic.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IHiccupPathSegment } from "@thi.ng/geom-api";
import type { Attribs, IHiccupPathSegment } from "@thi.ng/geom-api";
import { __copyShape } from "../internal/copy.js";
import { APC } from "./apc.js";

Expand All @@ -11,6 +11,10 @@ export class Cubic extends APC implements IHiccupPathSegment {
return <Cubic>__copyShape(Cubic, this);
}

withAttribs(attribs: Attribs): Cubic {
return new Cubic(this.points, attribs);
}

toHiccup() {
return [
"path",
Expand Down
4 changes: 4 additions & 0 deletions packages/geom/src/api/ellipse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export class Ellipse implements IHiccupShape {
);
}

withAttribs(attribs: Attribs): Ellipse {
return new Ellipse(this.pos, this.r, attribs);
}

toHiccup() {
return ["ellipse", this.attribs, this.pos, this.r];
}
Expand Down
4 changes: 4 additions & 0 deletions packages/geom/src/api/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export class Group implements IHiccupShape {
return new Group(__copyAttribs(this), this.children.map(fn));
}

withAttribs(attribs: Attribs): Group {
return new Group(attribs, this.children);
}

equiv(o: any) {
return o instanceof Group && equiv(this.children, o.children);
}
Expand Down
10 changes: 9 additions & 1 deletion packages/geom/src/api/line.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import type { IHiccupPathSegment, IHiccupShape } from "@thi.ng/geom-api";
import type {
Attribs,
IHiccupPathSegment,
IHiccupShape,
} from "@thi.ng/geom-api";
import { __copyShape } from "../internal/copy.js";
import { APC } from "./apc.js";

Expand All @@ -11,6 +15,10 @@ export class Line extends APC implements IHiccupShape, IHiccupPathSegment {
return <Line>__copyShape(Line, this);
}

withAttribs(attribs: Attribs): Line {
return new Line(this.points, attribs);
}

toHiccup() {
return ["line", this.attribs, this.points[0], this.points[1]];
}
Expand Down
6 changes: 6 additions & 0 deletions packages/geom/src/api/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ export class Path implements IHiccupShape {
return p;
}

withAttribs(attribs: Attribs): Path {
const res = new Path(this.segments, attribs);
res.closed = true;
return res;
}

equiv(o: any) {
return o instanceof Path && equiv(this.segments, o.segments);
}
Expand Down
4 changes: 4 additions & 0 deletions packages/geom/src/api/plane.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export class Plane implements IHiccupShape {
return new Plane(set3([], this.normal), this.w, __copyAttribs(this));
}

withAttribs(attribs: Attribs): Plane {
return new Plane(this.normal, this.w, attribs);
}

toHiccup() {
return ["plane", this.attribs, this.normal, this.w];
}
Expand Down
10 changes: 9 additions & 1 deletion packages/geom/src/api/points.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IHiccupShape } from "@thi.ng/geom-api";
import type { Attribs, IHiccupShape } from "@thi.ng/geom-api";
import { __copyShape } from "../internal/copy.js";
import { APC } from "./apc.js";

Expand All @@ -11,6 +11,10 @@ export class Points extends APC implements IHiccupShape {
return <Points>__copyShape(Points, this);
}

withAttribs(attribs: Attribs): Points {
return new Points(this.points, attribs);
}

toHiccup() {
return ["points", this.attribs, this.points];
}
Expand All @@ -25,6 +29,10 @@ export class Points3 extends APC implements IHiccupShape {
return <Points3>__copyShape(Points3, this);
}

withAttribs(attribs: Attribs): Points3 {
return new Points3(this.points, attribs);
}

toHiccup() {
return ["points3", this.attribs, this.points];
}
Expand Down
6 changes: 5 additions & 1 deletion packages/geom/src/api/polygon.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IHiccupShape } from "@thi.ng/geom-api";
import type { Attribs, IHiccupShape } from "@thi.ng/geom-api";
import { __copyShape } from "../internal/copy.js";
import { APC } from "./apc.js";

Expand All @@ -11,6 +11,10 @@ export class Polygon extends APC implements IHiccupShape {
return <Polygon>__copyShape(Polygon, this);
}

withAttribs(attribs: Attribs): Polygon {
return new Polygon(this.points, attribs);
}

toHiccup() {
return ["polygon", this.attribs, this.points];
}
Expand Down
10 changes: 9 additions & 1 deletion packages/geom/src/api/polyline.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import type { IHiccupPathSegment, IHiccupShape } from "@thi.ng/geom-api";
import type {
Attribs,
IHiccupPathSegment,
IHiccupShape,
} from "@thi.ng/geom-api";
import { __copyShape } from "../internal/copy.js";
import { APC } from "./apc.js";

Expand All @@ -11,6 +15,10 @@ export class Polyline extends APC implements IHiccupShape, IHiccupPathSegment {
return <Polyline>__copyShape(Polyline, this);
}

withAttribs(attribs: Attribs): Polyline {
return new Polyline(this.points, attribs);
}

toHiccup() {
return ["polyline", { ...this.attribs, fill: "none" }, this.points];
}
Expand Down
6 changes: 5 additions & 1 deletion packages/geom/src/api/quad.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IHiccupShape } from "@thi.ng/geom-api";
import type { Attribs, IHiccupShape } from "@thi.ng/geom-api";
import { __copyShape } from "../internal/copy.js";
import { APC } from "./apc.js";

Expand All @@ -11,6 +11,10 @@ export class Quad extends APC implements IHiccupShape {
return <Quad>__copyShape(Quad, this);
}

withAttribs(attribs: Attribs): Quad {
return new Quad(this.points, attribs);
}

toHiccup() {
return ["polygon", this.attribs, this.points];
}
Expand Down
6 changes: 5 additions & 1 deletion packages/geom/src/api/quad3.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IHiccupShape } from "@thi.ng/geom-api";
import type { Attribs, IHiccupShape } from "@thi.ng/geom-api";
import { __copyShape } from "../internal/copy.js";
import { APC } from "./apc.js";

Expand All @@ -11,6 +11,10 @@ export class Quad3 extends APC implements IHiccupShape {
return <Quad3>__copyShape(Quad3, this);
}

withAttribs(attribs: Attribs): Quad3 {
return new Quad3(this.points, attribs);
}

toHiccup() {
return ["polygon", this.attribs, this.points];
}
Expand Down
10 changes: 9 additions & 1 deletion packages/geom/src/api/quadratic.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import type { IHiccupPathSegment, IHiccupShape } from "@thi.ng/geom-api";
import type {
Attribs,
IHiccupPathSegment,
IHiccupShape,
} from "@thi.ng/geom-api";
import { __copyShape } from "../internal/copy.js";
import { APC } from "./apc.js";

Expand All @@ -11,6 +15,10 @@ export class Quadratic extends APC implements IHiccupShape, IHiccupPathSegment {
return <Quadratic>__copyShape(Quadratic, this);
}

withAttribs(attribs: Attribs): Quadratic {
return new Quadratic(this.points, attribs);
}

toHiccup() {
return [
"path",
Expand Down
4 changes: 4 additions & 0 deletions packages/geom/src/api/ray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export class Ray implements IHiccupShape {
);
}

withAttribs(attribs: Attribs): Ray {
return new Ray(this.pos, this.dir, attribs);
}

toHiccup() {
return [
"line",
Expand Down
4 changes: 4 additions & 0 deletions packages/geom/src/api/rect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export class Rect implements AABBLike, IHiccupShape {
);
}

withAttribs(attribs: Attribs): Rect {
return new Rect(this.pos, this.size, attribs);
}

max() {
return add2([], this.pos, this.size);
}
Expand Down
4 changes: 4 additions & 0 deletions packages/geom/src/api/sphere.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export class Sphere implements IHiccupShape {
return new Sphere(set3([], this.pos), this.r, __copyAttribs(this));
}

withAttribs(attribs: Attribs): Sphere {
return new Sphere(this.pos, this.r, attribs);
}

toHiccup() {
return ["sphere", this.attribs, this.pos, this.r];
}
Expand Down
4 changes: 4 additions & 0 deletions packages/geom/src/api/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export class Text implements IHiccupShape {
return new Text(set([], this.pos), this.body, __copyAttribs(this));
}

withAttribs(attribs: Attribs): Text {
return new Text(this.pos, this.body, attribs);
}

toHiccup() {
return ["text", this.attribs, this.pos, this.body];
}
Expand Down
6 changes: 5 additions & 1 deletion packages/geom/src/api/triangle.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IHiccupShape } from "@thi.ng/geom-api";
import type { Attribs, IHiccupShape } from "@thi.ng/geom-api";
import { __copyShape } from "../internal/copy.js";
import { APC } from "./apc.js";

Expand All @@ -11,6 +11,10 @@ export class Triangle extends APC implements IHiccupShape {
return <Triangle>__copyShape(Triangle, this);
}

withAttribs(attribs: Attribs): Triangle {
return new Triangle(this.points, attribs);
}

toHiccup() {
return ["polygon", this.attribs, this.points];
}
Expand Down

0 comments on commit ccb40f1

Please sign in to comment.