Skip to content

Commit

Permalink
Clean up primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
yzrmn committed Dec 22, 2024
1 parent b10d9ad commit ac54432
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 46 deletions.
46 changes: 23 additions & 23 deletions packages/redgeometry/src/primitives/bezier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,40 @@ import { Edge2 } from "./edge.js";
import { Point2, Point3, type Point2Like } from "./point.js";
import { Vector2, type Vector3 } from "./vector.js";

export enum CurveType {
Bezier1,
Bezier2,
Bezier3,
BezierR,
}

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

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

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

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

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

export enum CurveType {
Bezier1,
Bezier2,
Bezier3,
BezierR,
}

export class Bezier1Curve2 {
public p0: Point2;
public p1: Point2;
Expand Down Expand Up @@ -85,7 +85,7 @@ export class Bezier1Curve2 {
return new Bezier1Curve2(p0, p1);
}

public static toObject(c: Bezier1Curve2): { p0: Point2Like; p1: Point2Like } {
public static toObject(c: Bezier1Curve2): Bezier1Curve2Like {
const p0 = Point2.toObject(c.p0);
const p1 = Point2.toObject(c.p1);
return { p0, p1 };
Expand Down Expand Up @@ -246,7 +246,7 @@ export class Bezier2Curve2 {
return new Bezier2Curve2(p0, p1, p2);
}

public static toObject(c: Bezier2Curve2): { p0: Point2Like; p1: Point2Like; p2: Point2Like } {
public static toObject(c: Bezier2Curve2): Bezier2Curve2Like {
const p0 = Point2.toObject(c.p0);
const p1 = Point2.toObject(c.p1);
const p2 = Point2.toObject(c.p2);
Expand Down Expand Up @@ -627,7 +627,7 @@ export class Bezier3Curve2 {
return new Bezier3Curve2(p0, p1, p2, p3);
}

public static toObject(c: Bezier3Curve2): { p0: Point2Like; p1: Point2Like; p2: Point2Like; p3: Point2Like } {
public static toObject(c: Bezier3Curve2): Bezier3Curve2Like {
const p0 = Point2.toObject(c.p0);
const p1 = Point2.toObject(c.p1);
const p2 = Point2.toObject(c.p2);
Expand Down Expand Up @@ -1031,7 +1031,7 @@ 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 } {
public static toObject(c: BezierRCurve2): BezierRCurve2Like {
const p0 = Point2.toObject(c.p0);
const p1 = Point2.toObject(c.p1);
const p2 = Point2.toObject(c.p2);
Expand Down
4 changes: 2 additions & 2 deletions packages/redgeometry/src/primitives/box.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class Box2 {
return new Box2(x0, y0, x1, y1);
}

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

Expand Down Expand Up @@ -276,7 +276,7 @@ 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 } {
public static toObject(box: Box3): Box3Like {
return { x0: box.x0, y0: box.y0, z0: box.z0, x1: box.x1, y1: box.y1, z1: box.z1 };
}

Expand Down
2 changes: 1 addition & 1 deletion packages/redgeometry/src/primitives/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class 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 } {
public static toObject(c: ColorRgba): ColorRgbaLike {
return { r: c.r, g: c.g, b: c.b, a: c.a };
}

Expand Down
12 changes: 6 additions & 6 deletions packages/redgeometry/src/primitives/edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import { Ray2, Ray3 } from "./ray.js";
import type { Vector2, Vector3 } from "./vector.js";

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

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

export class Edge2 {
Expand Down Expand Up @@ -278,7 +278,7 @@ export class Edge2 {
return e1.p0.eq(e2.p1) && e1.p1.eq(e2.p0);
}

public static toObject(e: Edge2): { p0: Point2Like; p1: Point2Like } {
public static toObject(e: Edge2): Edge2Like {
const p0 = Point2.toObject(e.p0);
const p1 = Point2.toObject(e.p1);
return { p0, p1 };
Expand Down Expand Up @@ -432,7 +432,7 @@ export class Edge3 {
return e1.p0.eq(e2.p1) && e1.p1.eq(e2.p0);
}

public static toObject(e: Edge3): { p0: Point3Like; p1: Point3Like } {
public static toObject(e: Edge3): Edge3Like {
const p0 = Point3.toObject(e.p0);
const p1 = Point3.toObject(e.p1);
return { p0, p1 };
Expand Down
16 changes: 16 additions & 0 deletions packages/redgeometry/src/primitives/matrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@ export type MatrixElements3 = FixedSizeArray<number, 9>;
export type MatrixElements4A = FixedSizeArray<number, 12>;
export type MatrixElements4 = FixedSizeArray<number, 16>;

export type Matrix3ALike = {
readonly elements: MatrixElements3A;
};

export type Matrix3Like = {
readonly elements: MatrixElements3;
};

export type Matrix4ALike = {
readonly elements: MatrixElements4A;
};

export type Matrix4Like = {
readonly elements: MatrixElements4;
};

export enum MatrixType {
Affine,
Projective,
Expand Down
16 changes: 15 additions & 1 deletion packages/redgeometry/src/primitives/polygon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { Polygon2EdgeIterator } from "../internal/iterator.js";
import { Box2 } from "./box.js";
import { Edge2 } from "./edge.js";
import type { Matrix3, Matrix3A } from "./matrix.js";
import { Point2 } from "./point.js";
import { Point2, type Point2Like } from "./point.js";

export type Polygon2Like = {
readonly points: Point2Like[];
};

export class Polygon2 {
public points: Point2[];
Expand All @@ -16,6 +20,11 @@ export class Polygon2 {
return new Polygon2([]);
}

public static fromObject(obj: Polygon2Like): Polygon2 {
const points = obj.points.map((p) => Point2.fromObject(p));
return new Polygon2(points);
}

public static isAreaIntersection(poly1: Polygon2, poly2: Polygon2, isNonZero: boolean): boolean {
if (Polygon2.isEdgeIntersection(poly1, poly2)) {
// Polygons intersect
Expand Down Expand Up @@ -72,6 +81,11 @@ export class Polygon2 {
return true;
}

public static toObject(path: Polygon2): Polygon2Like {
const points = path.points.map((p) => Point2.toObject(p));
return { points };
}

public addPoint(p: Point2): void {
this.points.push(p);
}
Expand Down
14 changes: 7 additions & 7 deletions packages/redgeometry/src/primitives/quaternion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ import { eqApproxAbs, eqApproxRel, lerp } from "../utility/scalar.js";
import { Point3 } from "./point.js";
import { Vector3 } from "./vector.js";

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

export enum RotationOrder {
XYZ,
XZY,
Expand All @@ -12,13 +19,6 @@ export enum RotationOrder {
ZYX,
}

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

/**
* A quaternion to be used for 3D rotations.
*
Expand Down
12 changes: 6 additions & 6 deletions packages/redgeometry/src/primitives/ray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { Point2, Point3, type Point2Like, type Point3Like } from "./point.js";
import { Vector2, Vector3, type Vector2Like, type Vector3Like } from "./vector.js";

export type Ray2Like = {
readonly p: Point2;
readonly v: Vector2;
readonly p: Point2Like;
readonly v: Vector2Like;
};

export type Ray3Like = {
readonly p: Point3;
readonly v: Vector3;
readonly p: Point3Like;
readonly v: Vector3Like;
};

export class Ray2 {
Expand Down Expand Up @@ -80,7 +80,7 @@ export class Ray2 {
return [t, u];
}

public static toObject(ray: Ray2): { p: Point2Like; v: Vector2Like } {
public static toObject(ray: Ray2): Ray2Like {
const p = Point2.toObject(ray.p);
const v = Vector2.toObject(ray.v);
return { p, v };
Expand Down Expand Up @@ -212,7 +212,7 @@ export class Ray3 {
return [t1, t2];
}

public static toObject(ray: Ray3): { p: Point3Like; v: Vector3Like } {
public static toObject(ray: Ray3): Ray3Like {
const p = Point3.toObject(ray.p);
const v = Vector3.toObject(ray.v);
return { p, v };
Expand Down

0 comments on commit ac54432

Please sign in to comment.