diff --git a/packages/redgeometry/src/primitives/bezier.ts b/packages/redgeometry/src/primitives/bezier.ts index 778a3aa..5bdda5d 100644 --- a/packages/redgeometry/src/primitives/bezier.ts +++ b/packages/redgeometry/src/primitives/bezier.ts @@ -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; @@ -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 }; @@ -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); @@ -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); @@ -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); diff --git a/packages/redgeometry/src/primitives/box.ts b/packages/redgeometry/src/primitives/box.ts index c7e61e2..f8ca24b 100644 --- a/packages/redgeometry/src/primitives/box.ts +++ b/packages/redgeometry/src/primitives/box.ts @@ -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 }; } @@ -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 }; } diff --git a/packages/redgeometry/src/primitives/color.ts b/packages/redgeometry/src/primitives/color.ts index 95c2264..d481764 100644 --- a/packages/redgeometry/src/primitives/color.ts +++ b/packages/redgeometry/src/primitives/color.ts @@ -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 }; } diff --git a/packages/redgeometry/src/primitives/edge.ts b/packages/redgeometry/src/primitives/edge.ts index 52a68ce..3a3b101 100644 --- a/packages/redgeometry/src/primitives/edge.ts +++ b/packages/redgeometry/src/primitives/edge.ts @@ -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 { @@ -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 }; @@ -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 }; diff --git a/packages/redgeometry/src/primitives/matrix.ts b/packages/redgeometry/src/primitives/matrix.ts index 101e64d..8346a9e 100644 --- a/packages/redgeometry/src/primitives/matrix.ts +++ b/packages/redgeometry/src/primitives/matrix.ts @@ -9,6 +9,22 @@ export type MatrixElements3 = FixedSizeArray; export type MatrixElements4A = FixedSizeArray; export type MatrixElements4 = FixedSizeArray; +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, diff --git a/packages/redgeometry/src/primitives/polygon.ts b/packages/redgeometry/src/primitives/polygon.ts index c7d1622..e066c03 100644 --- a/packages/redgeometry/src/primitives/polygon.ts +++ b/packages/redgeometry/src/primitives/polygon.ts @@ -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[]; @@ -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 @@ -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); } diff --git a/packages/redgeometry/src/primitives/quaternion.ts b/packages/redgeometry/src/primitives/quaternion.ts index eeaaeb1..863d4b9 100644 --- a/packages/redgeometry/src/primitives/quaternion.ts +++ b/packages/redgeometry/src/primitives/quaternion.ts @@ -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, @@ -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. * diff --git a/packages/redgeometry/src/primitives/ray.ts b/packages/redgeometry/src/primitives/ray.ts index 2b8c34c..1a880f9 100644 --- a/packages/redgeometry/src/primitives/ray.ts +++ b/packages/redgeometry/src/primitives/ray.ts @@ -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 { @@ -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 }; @@ -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 };