Skip to content

Commit

Permalink
Add primitive object conversion methods
Browse files Browse the repository at this point in the history
  • Loading branch information
yzrmn committed Jul 10, 2024
1 parent aa44aa9 commit e58e9d2
Show file tree
Hide file tree
Showing 12 changed files with 252 additions and 87 deletions.
4 changes: 2 additions & 2 deletions packages/redgeometry-app/src/parts/matrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ function transformEdges(edges: Edge3[], m: Matrix4): Edge2[] {
for (const e of edges) {
const p0 = m.mulPt(e.p0);
const p1 = m.mulPt(e.p1);
const pp0 = Point2.from(p0);
const pp1 = Point2.from(p1);
const pp0 = Point2.fromObject(p0);
const pp1 = Point2.fromObject(p1);
output.push(new Edge2(pp0, pp1));
}

Expand Down
2 changes: 1 addition & 1 deletion packages/redgeometry-app/src/parts/path-area.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function updateSystem(world: World): void {
const path = createRandomPath(random, generator, count, canvasWidth, canvasHeight);
path.close();

const p = Point2.from(mouse.getCursorPosition());
const p = Point2.fromObject(mouse.getCursorPosition());

world.writeData<AppPartRemoteData>({
dataId: "app-part-remote",
Expand Down
14 changes: 13 additions & 1 deletion packages/redgeometry/src/core/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { copyCommandsReversed, isWindingInside } from "../internal/path.js";
import { CurveType, type BezierCurve2 } from "../primitives/bezier.js";
import { Box2 } from "../primitives/box.js";
import { Matrix3A, type Matrix3 } from "../primitives/matrix.js";
import { Point2 } from "../primitives/point.js";
import { Point2, type Point2Like } from "../primitives/point.js";
import { Polygon2 } from "../primitives/polygon.js";
import { Vector2 } from "../primitives/vector.js";
import { copyArray, copyArrayReversed } from "../utility/array.js";
Expand Down Expand Up @@ -76,6 +76,18 @@ export class Path2 implements PathSink2 {
return new Path2([], []);
}

public static fromObject(obj: { points: Point2Like[]; commands: PathCommand[] }): Path2 {
const commands = obj.commands.map((c) => ({ ...c }));
const points = obj.points.map((p) => Point2.fromObject(p));
return new Path2(commands, points);
}

public static toObject(path: Path2): { points: Point2Like[]; commands: PathCommand[] } {
const commands = path.commands.map((c) => ({ ...c }));
const points = path.points.map((p) => Point2.toObject(p));
return { commands, points };
}

public addArc(p0: Point2, p1: Point2, p2: Point2): void {
this.moveTo(p0);
this.arcTo(p1, p2);
Expand Down
74 changes: 57 additions & 17 deletions packages/redgeometry/src/primitives/bezier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Interval } from "../utility/interval.js";
import { RootType, solveCubic, solveLinear, solveQuadratic } from "../utility/solve.js";
import { Box2 } from "./box.js";
import { Edge2 } from "./edge.js";
import { Point2, Point3 } from "./point.js";
import { Point2, Point3, type Point2Like } from "./point.js";
import { Vector2, type Vector3 } from "./vector.js";

export enum CurveType {
Expand Down Expand Up @@ -40,17 +40,19 @@ export class Bezier1Curve2 {
return CurveType.Bezier1;
}

public static from(obj: { p0: Point2; p1: Point2 }): Bezier1Curve2 {
return new Bezier1Curve2(obj.p0, obj.p1);
}

public static fromArray(data: number[], offset = 0): Bezier1Curve2 {
const p0 = Point2.fromArray(data, offset);
const p1 = Point2.fromArray(data, offset + 2);

return new Bezier1Curve2(p0, p1);
}

public static fromObject(obj: { p0: Point2Like; p1: Point2Like }): Bezier1Curve2 {
const p0 = Point2.fromObject(obj.p0);
const p1 = Point2.fromObject(obj.p1);
return new Bezier1Curve2(p0, p1);
}

public static fromXY(x0: number, y0: number, x1: number, y1: number): Bezier1Curve2 {
const p0 = new Point2(x0, y0);
const p1 = new Point2(x1, y1);
Expand Down Expand Up @@ -79,6 +81,12 @@ export class Bezier1Curve2 {
return result;
}

public static toObject(c: Bezier1Curve2): { p0: Point2Like; p1: Point2Like } {
const p0 = Point2.toObject(c.p0);
const p1 = Point2.toObject(c.p1);
return { p0, p1 };
}

public clone(): Bezier1Curve2 {
return new Bezier1Curve2(this.p0.clone(), this.p1.clone());
}
Expand Down Expand Up @@ -211,10 +219,6 @@ export class Bezier2Curve2 {
return CurveType.Bezier2;
}

public static from(obj: { p0: Point2; p1: Point2; p2: Point2 }): Bezier2Curve2 {
return new Bezier2Curve2(obj.p0, obj.p1, obj.p2);
}

public static fromArray(data: number[], offset = 0): Bezier2Curve2 {
const p0 = Point2.fromArray(data, offset);
const p1 = Point2.fromArray(data, offset + 2);
Expand All @@ -223,6 +227,13 @@ export class Bezier2Curve2 {
return new Bezier2Curve2(p0, p1, p2);
}

public static fromObject(obj: { p0: Point2Like; p1: Point2Like; p2: Point2Like }): Bezier2Curve2 {
const p0 = Point2.fromObject(obj.p0);
const p1 = Point2.fromObject(obj.p1);
const p2 = Point2.fromObject(obj.p2);
return new Bezier2Curve2(p0, p1, p2);
}

public static fromXY(x0: number, y0: number, x1: number, y1: number, x2: number, y2: number): Bezier2Curve2 {
const p0 = new Point2(x0, y0);
const p1 = new Point2(x1, y1);
Expand All @@ -231,6 +242,13 @@ export class Bezier2Curve2 {
return new Bezier2Curve2(p0, p1, p2);
}

public static toObject(c: Bezier2Curve2): { p0: Point2Like; p1: Point2Like; p2: Point2Like } {
const p0 = Point2.toObject(c.p0);
const p1 = Point2.toObject(c.p1);
const p2 = Point2.toObject(c.p2);
return { p0, p1, p2 };
}

public clone(): Bezier2Curve2 {
return new Bezier2Curve2(this.p0.clone(), this.p1.clone(), this.p2.clone());
}
Expand Down Expand Up @@ -567,10 +585,6 @@ export class Bezier3Curve2 {
return CurveType.Bezier3;
}

public static from(obj: { p0: Point2; p1: Point2; p2: Point2; p3: Point2 }): Bezier3Curve2 {
return new Bezier3Curve2(obj.p0, obj.p1, obj.p2, obj.p3);
}

public static fromArray(data: number[], offset = 0): Bezier3Curve2 {
const p0 = Point2.fromArray(data, offset);
const p1 = Point2.fromArray(data, offset + 2);
Expand All @@ -580,6 +594,14 @@ export class Bezier3Curve2 {
return new Bezier3Curve2(p0, p1, p2, p3);
}

public static fromObject(obj: { p0: Point2Like; p1: Point2Like; p2: Point2Like; p3: Point2Like }): Bezier3Curve2 {
const p0 = Point2.fromObject(obj.p0);
const p1 = Point2.fromObject(obj.p1);
const p2 = Point2.fromObject(obj.p2);
const p3 = Point2.fromObject(obj.p3);
return new Bezier3Curve2(p0, p1, p2, p3);
}

public static fromXY(
x0: number,
y0: number,
Expand All @@ -598,6 +620,14 @@ export class Bezier3Curve2 {
return new Bezier3Curve2(p0, p1, p2, p3);
}

public static toObject(c: Bezier3Curve2): { p0: Point2Like; p1: Point2Like; p2: Point2Like; p3: Point2Like } {
const p0 = Point2.toObject(c.p0);
const p1 = Point2.toObject(c.p1);
const p2 = Point2.toObject(c.p2);
const p3 = Point2.toObject(c.p3);
return { p0, p1, p2, p3 };
}

public clone(): Bezier3Curve2 {
return new Bezier3Curve2(this.p0.clone(), this.p1.clone(), this.p2.clone(), this.p3.clone());
}
Expand Down Expand Up @@ -931,10 +961,6 @@ export class BezierRCurve2 {
return CurveType.BezierR;
}

public static from(obj: { p0: Point2; p1: Point2; p2: Point2; w: number }): BezierRCurve2 {
return new BezierRCurve2(obj.p0, obj.p1, obj.p2, obj.w);
}

public static fromArray(data: number[], offset = 0): BezierRCurve2 {
const p0 = Point2.fromArray(data, offset);
const p1 = Point2.fromArray(data, offset + 2);
Expand All @@ -952,6 +978,13 @@ export class BezierRCurve2 {
return new BezierRCurve2(p0, p1, p2, w);
}

public static fromObject(obj: { p0: Point2Like; p1: Point2Like; p2: Point2Like; w: number }): BezierRCurve2 {
const p0 = Point2.fromObject(obj.p0);
const p1 = Point2.fromObject(obj.p1);
const p2 = Point2.fromObject(obj.p2);
return new BezierRCurve2(p0, p1, p2, obj.w);
}

public static fromProjectivePoints(p0: Point3, p1: Point3, p2: Point3): BezierRCurve2 {
const pp0 = Point2.fromXYW(p0.x, p0.y, p0.z);
const pp1 = Point2.fromXYW(p1.x, p1.y, p1.z);
Expand Down Expand Up @@ -988,6 +1021,13 @@ export class BezierRCurve2 {
return v1.dot(v2) / Math.sqrt(v1.lenSq() * v2.lenSq());
}

public static toObject(c: BezierRCurve2): { p0: Point2Like; p1: Point2Like; p2: Point2Like; w: number } {
const p0 = Point2.toObject(c.p0);
const p1 = Point2.toObject(c.p1);
const p2 = Point2.toObject(c.p2);
return { p0, p1, p2, w: c.w };
}

public clone(): BezierRCurve2 {
return new BezierRCurve2(this.p0.clone(), this.p1.clone(), this.p2.clone(), this.w);
}
Expand Down
24 changes: 16 additions & 8 deletions packages/redgeometry/src/primitives/box.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ export class Box2 {
);
}

public static from(obj: { x0: number; y0: number; x1: number; y1: number }): Box2 {
return new Box2(obj.x0, obj.y0, obj.x1, obj.y1);
}

public static fromArray(data: number[], offset = 0): Box2 {
return new Box2(data[offset], data[offset + 1], data[offset + 2], data[offset + 3]);
}

public static fromObject(obj: { x0: number; y0: number; x1: number; y1: number }): Box2 {
return new Box2(obj.x0, obj.y0, obj.x1, obj.y1);
}

public static fromPoints(p0: Point2, p1: Point2): Box2 {
const x0 = Math.min(p0.x, p1.x);
const y0 = Math.min(p0.y, p1.y);
Expand All @@ -53,6 +53,10 @@ export class Box2 {
return new Box2(x0, y0, x1, y1);
}

public static toObject(box: Box2): { x0: number; y0: number; x1: number; y1: number } {
return { x0: box.x0, y0: box.y0, x1: box.x1, y1: box.y1 };
}

public clone(): Box2 {
return new Box2(this.x0, this.y0, this.x1, this.y1);
}
Expand Down Expand Up @@ -195,10 +199,6 @@ export class Box3 {
);
}

public static from(obj: { x0: number; y0: number; z0: number; x1: number; y1: number; z1: number }): Box3 {
return new Box3(obj.x0, obj.y0, obj.z0, obj.x1, obj.y1, obj.z1);
}

public static fromArray(data: number[], offset = 0): Box3 {
return new Box3(
data[offset],
Expand All @@ -210,6 +210,10 @@ export class Box3 {
);
}

public static fromObject(obj: { x0: number; y0: number; z0: number; x1: number; y1: number; z1: number }): Box3 {
return new Box3(obj.x0, obj.y0, obj.z0, obj.x1, obj.y1, obj.z1);
}

public static fromPoints(p0: Point3, p1: Point3): Box3 {
const x0 = Math.min(p0.x, p1.x);
const y0 = Math.min(p0.y, p1.y);
Expand All @@ -232,6 +236,10 @@ export class Box3 {
return new Box3(x0, y0, z0, x1, y1, z1);
}

public static toObject(box: Box3): { x0: number; y0: number; z0: number; x1: number; y1: number; z1: number } {
return { x0: box.x0, y0: box.y0, z0: box.z0, x1: box.x1, y1: box.y1, z1: box.z1 };
}

public clone(): Box3 {
return new Box3(this.x0, this.y0, this.z0, this.x1, this.y1, this.z1);
}
Expand Down
12 changes: 8 additions & 4 deletions packages/redgeometry/src/primitives/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ export class ColorRgba {
this.a = a;
}

public static from(obj: { r: number; g: number; b: number; a: number }): ColorRgba {
return new ColorRgba(obj.r, obj.g, obj.b, obj.a);
}

public static fromArray(data: number[], offset = 0): ColorRgba {
return new ColorRgba(data[offset], data[offset + 1], data[offset + 2], data[offset + 3]);
}
Expand Down Expand Up @@ -55,6 +51,14 @@ export class ColorRgba {
return new ColorRgba(r / 255, g / 255, b / 255, a / 255);
}

public static fromObject(obj: { r: number; g: number; b: number; a: number }): ColorRgba {
return new ColorRgba(obj.r, obj.g, obj.b, obj.a);
}

public static toObject(c: ColorRgba): { r: number; g: number; b: number; a: number } {
return { r: c.r, g: c.g, b: c.b, a: c.a };
}

public clone(): ColorRgba {
return new ColorRgba(this.r, this.g, this.b, this.a);
}
Expand Down
19 changes: 14 additions & 5 deletions packages/redgeometry/src/primitives/complex.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { Point2 } from "./point.js";
import { Vector2 } from "./vector.js";

export interface ComplexLike {
a: number;
b: number;
}

/**
* A complex number to be used for 2D rotations.
*/
export class Complex {
export class Complex implements ComplexLike {
public a: number;
public b: number;

Expand All @@ -17,20 +22,24 @@ export class Complex {
return new Complex(1, 0);
}

public static from(obj: { a: number; b: number }): Complex {
return new Complex(obj.a, obj.b);
}

public static fromArray(data: number[], offset = 0): Complex {
return new Complex(data[offset], data[offset + 1]);
}

public static fromObject(obj: ComplexLike): Complex {
return new Complex(obj.a, obj.b);
}

public static fromRotationAngle(angle: number): Complex {
const sin = Math.sin(angle);
const cos = Math.cos(angle);
return new Complex(cos, sin);
}

public static toObject(z: Complex): ComplexLike {
return { a: z.a, b: z.b };
}

public add(z: Complex): Complex {
return new Complex(this.a + z.a, this.b + z.b);
}
Expand Down
Loading

0 comments on commit e58e9d2

Please sign in to comment.