Skip to content

Commit

Permalink
Use more readonly types
Browse files Browse the repository at this point in the history
  • Loading branch information
yzrmn committed Dec 12, 2024
1 parent 1e92ed8 commit ae45593
Show file tree
Hide file tree
Showing 13 changed files with 157 additions and 81 deletions.
9 changes: 7 additions & 2 deletions packages/redgeometry/src/core/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ export enum PathCommandType {
Close,
}

export type Path2Like = {
readonly points: Point2Like[];
readonly commands: PathCommand[];
};

export type PathCommandMove = { type: PathCommandType.Move };
export type PathCommandLine = { type: PathCommandType.Linear };
export type PathCommandQuad = { type: PathCommandType.Quadratic };
Expand Down Expand Up @@ -76,13 +81,13 @@ export class Path2 implements PathSink2 {
return new Path2([], []);
}

public static fromObject(obj: { points: Point2Like[]; commands: PathCommand[] }): Path2 {
public static fromObject(obj: Path2Like): 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[] } {
public static toObject(path: Path2): Path2Like {
const commands = path.commands.map((c) => ({ ...c }));
const points = path.points.map((p) => Point2.toObject(p));
return { commands, points };
Expand Down
41 changes: 33 additions & 8 deletions packages/redgeometry/src/primitives/bezier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,31 @@ export enum CurveType {
BezierR,
}

export type Bezier1Curve2Like = {
readonly p0: Point2;
readonly p1: Point2;
};

export type Bezier2Curve2Like = {
readonly p0: Point2;
readonly p1: Point2;
readonly p2: Point2;
};

export type Bezier3Curve2Like = {
readonly p0: Point2;
readonly p1: Point2;
readonly p2: Point2;
readonly p3: Point2;
};

export type BezierRCurve2Like = {
readonly p0: Point2;
readonly p1: Point2;
readonly p2: Point2;
readonly w: number;
};

export type BezierCurve2 = Bezier1Curve2 | Bezier2Curve2 | Bezier3Curve2 | BezierRCurve2;

export class Bezier1Curve2 {
Expand All @@ -40,14 +65,14 @@ export class Bezier1Curve2 {
return CurveType.Bezier1;
}

public static fromArray(data: number[], offset = 0): Bezier1Curve2 {
public static fromArray(data: ArrayLike<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 {
public static fromObject(obj: Bezier1Curve2Like): Bezier1Curve2 {
const p0 = Point2.fromObject(obj.p0);
const p1 = Point2.fromObject(obj.p1);
return new Bezier1Curve2(p0, p1);
Expand Down Expand Up @@ -198,15 +223,15 @@ export class Bezier2Curve2 {
return CurveType.Bezier2;
}

public static fromArray(data: number[], offset = 0): Bezier2Curve2 {
public static fromArray(data: ArrayLike<number>, offset = 0): Bezier2Curve2 {
const p0 = Point2.fromArray(data, offset);
const p1 = Point2.fromArray(data, offset + 2);
const p2 = Point2.fromArray(data, offset + 4);

return new Bezier2Curve2(p0, p1, p2);
}

public static fromObject(obj: { p0: Point2Like; p1: Point2Like; p2: Point2Like }): Bezier2Curve2 {
public static fromObject(obj: Bezier2Curve2Like): Bezier2Curve2 {
const p0 = Point2.fromObject(obj.p0);
const p1 = Point2.fromObject(obj.p1);
const p2 = Point2.fromObject(obj.p2);
Expand Down Expand Up @@ -567,7 +592,7 @@ export class Bezier3Curve2 {
return CurveType.Bezier3;
}

public static fromArray(data: number[], offset = 0): Bezier3Curve2 {
public static fromArray(data: ArrayLike<number>, offset = 0): Bezier3Curve2 {
const p0 = Point2.fromArray(data, offset);
const p1 = Point2.fromArray(data, offset + 2);
const p2 = Point2.fromArray(data, offset + 4);
Expand All @@ -576,7 +601,7 @@ export class Bezier3Curve2 {
return new Bezier3Curve2(p0, p1, p2, p3);
}

public static fromObject(obj: { p0: Point2Like; p1: Point2Like; p2: Point2Like; p3: Point2Like }): Bezier3Curve2 {
public static fromObject(obj: Bezier3Curve2Like): Bezier3Curve2 {
const p0 = Point2.fromObject(obj.p0);
const p1 = Point2.fromObject(obj.p1);
const p2 = Point2.fromObject(obj.p2);
Expand Down Expand Up @@ -946,7 +971,7 @@ export class BezierRCurve2 {
return CurveType.BezierR;
}

public static fromArray(data: number[], offset = 0): BezierRCurve2 {
public static fromArray(data: ArrayLike<number>, offset = 0): BezierRCurve2 {
const p0 = Point2.fromArray(data, offset);
const p1 = Point2.fromArray(data, offset + 2);
const p2 = Point2.fromArray(data, offset + 4);
Expand All @@ -963,7 +988,7 @@ export class BezierRCurve2 {
return new BezierRCurve2(p0, p1, p2, w);
}

public static fromObject(obj: { p0: Point2Like; p1: Point2Like; p2: Point2Like; w: number }): BezierRCurve2 {
public static fromObject(obj: BezierRCurve2Like): BezierRCurve2 {
const p0 = Point2.fromObject(obj.p0);
const p1 = Point2.fromObject(obj.p1);
const p2 = Point2.fromObject(obj.p2);
Expand Down
24 changes: 20 additions & 4 deletions packages/redgeometry/src/primitives/box.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@ import type { Matrix3, Matrix3A, Matrix4, Matrix4A } from "./matrix.js";
import { Point2, Point3 } from "./point.js";
import type { Ray2, Ray3 } from "./ray.js";

export type Box2Like = {
readonly x0: number;
readonly x1: number;
readonly y0: number;
readonly y1: number;
};

export type Box3Like = {
readonly x0: number;
readonly x1: number;
readonly y0: number;
readonly y1: number;
readonly z0: number;
readonly z1: number;
};

export class Box2 {
public x0: number;
public x1: number;
Expand All @@ -27,11 +43,11 @@ export class Box2 {
);
}

public static fromArray(data: number[], offset = 0): Box2 {
public static fromArray(data: ArrayLike<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 {
public static fromObject(obj: Box2Like): Box2 {
return new Box2(obj.x0, obj.y0, obj.x1, obj.y1);
}

Expand Down Expand Up @@ -223,7 +239,7 @@ export class Box3 {
);
}

public static fromArray(data: number[], offset = 0): Box3 {
public static fromArray(data: ArrayLike<number>, offset = 0): Box3 {
return new Box3(
data[offset],
data[offset + 1],
Expand All @@ -234,7 +250,7 @@ export class Box3 {
);
}

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

Expand Down
11 changes: 9 additions & 2 deletions packages/redgeometry/src/primitives/color.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { clamp } from "../utility/scalar.js";

export type ColorRgbaLike = {
readonly a: number;
readonly b: number;
readonly g: number;
readonly r: number;
};

export class ColorRgba {
public a: number;
public b: number;
Expand All @@ -13,7 +20,7 @@ export class ColorRgba {
this.a = a;
}

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

Expand Down Expand Up @@ -51,7 +58,7 @@ 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 {
public static fromObject(obj: ColorRgbaLike): ColorRgba {
return new ColorRgba(obj.r, obj.g, obj.b, obj.a);
}

Expand Down
12 changes: 6 additions & 6 deletions packages/redgeometry/src/primitives/complex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { eqApproxAbs, eqApproxRel } from "../utility/scalar.js";
import { Point2 } from "./point.js";
import { Vector2 } from "./vector.js";

export interface ComplexLike {
a: number;
b: number;
}
export type ComplexLike = {
readonly a: number;
readonly b: number;
};

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

Expand All @@ -23,7 +23,7 @@ export class Complex implements ComplexLike {
return new Complex(1, 0);
}

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

Expand Down
18 changes: 14 additions & 4 deletions packages/redgeometry/src/primitives/edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ import { Point2, Point3, type Point2Like, type Point3Like } from "./point.js";
import { Ray2, Ray3 } from "./ray.js";
import type { Vector2, Vector3 } from "./vector.js";

export type Edge2Like = {
readonly p0: Point2;
readonly p1: Point2;
};

export type Edge3Like = {
readonly p0: Point3;
readonly p1: Point3;
};

export class Edge2 {
public p0: Point2;
public p1: Point2;
Expand Down Expand Up @@ -42,14 +52,14 @@ export class Edge2 {
}
}

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

return new Edge2(p0, p1);
}

public static fromObject(obj: { p0: Point2Like; p1: Point2Like }): Edge2 {
public static fromObject(obj: Edge2Like): Edge2 {
const p0 = Point2.fromObject(obj.p0);
const p1 = Point2.fromObject(obj.p1);
return new Edge2(p0, p1);
Expand Down Expand Up @@ -390,14 +400,14 @@ export class Edge3 {
this.p1 = p1;
}

public static fromArray(data: number[], offset = 0): Edge3 {
public static fromArray(data: ArrayLike<number>, offset = 0): Edge3 {
const p0 = Point3.fromArray(data, offset);
const p1 = Point3.fromArray(data, offset + 3);

return new Edge3(p0, p1);
}

public static fromObject(obj: { p0: Point3Like; p1: Point3Like }): Edge3 {
public static fromObject(obj: Edge3Like): Edge3 {
const p0 = Point3.fromObject(obj.p0);
const p1 = Point3.fromObject(obj.p1);
return new Edge3(p0, p1);
Expand Down
30 changes: 15 additions & 15 deletions packages/redgeometry/src/primitives/point.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { eqApproxAbs, eqApproxRel, lerp, roundToPrecision } from "../utility/scalar.js";
import { Vector2, Vector3 } from "./vector.js";

export interface Point2Like {
x: number;
y: number;
}

export interface Point3Like {
x: number;
y: number;
z: number;
}

export class Point2 implements Point2Like {
export type Point2Like = {
readonly x: number;
readonly y: number;
};

export type Point3Like = {
readonly x: number;
readonly y: number;
readonly z: number;
};

export class Point2 {
public x: number;
public y: number;

Expand All @@ -28,7 +28,7 @@ export class Point2 implements Point2Like {
return new Point2(0, 0);
}

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

Expand Down Expand Up @@ -161,7 +161,7 @@ export class Point2 implements Point2Like {
}
}

export class Point3 implements Point3Like {
export class Point3 {
public x: number;
public y: number;
public z: number;
Expand All @@ -179,7 +179,7 @@ export class Point3 implements Point3Like {
return new Point3(0, 0, 0);
}

public static fromArray(data: number[], offset = 0): Point3 {
public static fromArray(data: ArrayLike<number>, offset = 0): Point3 {
return new Point3(data[offset], data[offset + 1], data[offset + 2]);
}

Expand Down
16 changes: 8 additions & 8 deletions packages/redgeometry/src/primitives/quaternion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ export enum RotationOrder {
ZYX,
}

export interface QuaternionLike {
a: number;
b: number;
c: number;
d: number;
}
export type QuaternionLike = {
readonly a: number;
readonly b: number;
readonly c: number;
readonly d: number;
};

/**
* A quaternion to be used for 3D rotations.
Expand All @@ -26,7 +26,7 @@ export interface QuaternionLike {
* - https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation
* - https://danceswithcode.net/engineeringnotes/quaternions/quaternions.html
*/
export class Quaternion implements QuaternionLike {
export class Quaternion {
public a: number;
public b: number;
public c: number;
Expand All @@ -43,7 +43,7 @@ export class Quaternion implements QuaternionLike {
return new Quaternion(1, 0, 0, 0);
}

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

Expand Down
Loading

0 comments on commit ae45593

Please sign in to comment.