Skip to content

Commit

Permalink
Clean up mulPt and mulVec
Browse files Browse the repository at this point in the history
  • Loading branch information
yzrmn committed May 13, 2024
1 parent dfb7fb6 commit c4a552d
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 112 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 @@ -162,8 +162,8 @@ function transformEdges(edges: Edge3[], m: Matrix4): Edge2[] {
const output: Edge2[] = [];

for (const e of edges) {
const p0 = m.mulPt3(e.p0);
const p1 = m.mulPt3(e.p1);
const p0 = m.mulPt(e.p0);
const p1 = m.mulPt(e.p1);
const pp0 = Point2.from(p0);
const pp1 = Point2.from(p1);
output.push(new Edge2(pp0, pp1));
Expand Down
24 changes: 12 additions & 12 deletions packages/redgeometry/src/core/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,15 @@ export class Path2 implements PathSink2 {

this.moveTo(pc);

this.lineTo(mat.mulVec2(v1).toPoint());
this.lineTo(mat.mulVec(v1).toPoint());

// Iteratively process 90 degree segments
while (a > 0.5 * Math.PI + 0.005) {
// TODO: Investigate correctness of `normal.neg`
v1 = v1.normal().neg();

const p1 = mat.mulVec2(vc).toPoint();
const p2 = mat.mulVec2(v1).toPoint();
const p1 = mat.mulVec(vc).toPoint();
const p2 = mat.mulVec(v1).toPoint();
this.arcTo(p1, p2);

vc = vc.normal().neg();
Expand All @@ -238,8 +238,8 @@ export class Path2 implements PathSink2 {
// but we can safely assume it does (only critical for angles close to 180 degrees)
cos = Math.sqrt(0.5 * v1.dot(v2) + 0.5);

const p1 = mat.mulVec2(vc).toPoint();
const p2 = mat.mulVec2(v2).toPoint();
const p1 = mat.mulVec(vc).toPoint();
const p2 = mat.mulVec(v2).toPoint();
this.conicTo(p1, p2, cos);

this.close();
Expand Down Expand Up @@ -581,7 +581,7 @@ export class Path2 implements PathSink2 {
// Vector from center (transformed midpoint)
let v = p0.sub(p1).mul(0.5);

v = mat.mulVec2(v);
v = mat.mulVec(v);

// Radii (see https://www.w3.org/TR/SVG/implnote.html#ArcCorrectionOutOfRangeRadii)
let sx = Math.abs(rx);
Expand All @@ -601,8 +601,8 @@ export class Path2 implements PathSink2 {
mat.scale(1 / sx, 1 / sy);

// Calculate unit coordinates
let pp0 = mat.mulPt2(p0);
let pp1 = mat.mulPt2(p1);
let pp0 = mat.mulPt(p0);
let pp1 = mat.mulPt(p1);

// New vector from center (unit midpoint)
v = pp1.sub(pp0).mul(0.5);
Expand Down Expand Up @@ -680,8 +680,8 @@ export class Path2 implements PathSink2 {
v1 = v1.normal().neg();

// Transformed points of the arc segment
pp0 = mat.mulVec2(v).toPoint();
pp1 = mat.mulVec2(v1).toPoint();
pp0 = mat.mulVec(v).toPoint();
pp1 = mat.mulVec(v1).toPoint();

this.arcTo(pp0, pp1);

Expand All @@ -695,7 +695,7 @@ export class Path2 implements PathSink2 {
v = v.mul(2).div(v.dot(v));

// Final arc segment
pp0 = mat.mulVec2(v).toPoint();
pp0 = mat.mulVec(v).toPoint();
pp1 = p1;

// This is actually half of the remaining cos. It is required that `v1 dot v2 > -1` holds
Expand Down Expand Up @@ -727,7 +727,7 @@ export class Path2 implements PathSink2 {
public transform(mat: Matrix3 | Matrix3A): void {
const points = this.points;
for (let i = 0; i < points.length; i++) {
points[i] = mat.mulPt2(points[i]);
points[i] = mat.mulPt(points[i]);
}
}
}
4 changes: 2 additions & 2 deletions packages/redgeometry/src/primitives/box.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class Box2 {
}

public encloseWithTransform(p: Point2, mat: Matrix3 | Matrix3A): void {
const pp = mat.mulPt2(p);
const pp = mat.mulPt(p);
this.enclose(pp);
}

Expand Down Expand Up @@ -266,7 +266,7 @@ export class Box3 {
}

public encloseWithTransform(p: Point3, mat: Matrix4 | Matrix4A): void {
const pp = mat.mulPt3(p);
const pp = mat.mulPt(p);
this.enclose(pp);
}

Expand Down
88 changes: 8 additions & 80 deletions packages/redgeometry/src/primitives/matrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ export class Matrix3A {
* | 0 0 1 | | 1 |
* ```
*/
public mulPt2(p: Point2): Point2 {
public mulPt(p: Point2): Point2 {
const e = this.elements;

const x = e[0] * p.x + e[2] * p.y + e[4];
Expand Down Expand Up @@ -263,7 +263,7 @@ export class Matrix3A {
* | 0 0 1 | | 1 |
* ```
*/
public mulVec2(v: Vector2): Vector2 {
public mulVec(v: Vector2): Vector2 {
const e = this.elements;

const x = e[0] * v.x + e[2] * v.y + e[4];
Expand All @@ -272,23 +272,6 @@ export class Matrix3A {
return new Vector2(x, y);
}

/**
* ```
* | e0 e2 e4 | | x |
* | e1 e3 e5 | * | y |
* | 0 0 1 | | z |
* ```
*/
public mulVec3(v: Vector3): Vector3 {
const e = this.elements;

const x = e[0] * v.x + e[2] * v.y + e[4] * v.z;
const y = e[1] * v.x + e[3] * v.y + e[5] * v.z;
const z = v.z;

return new Vector3(x, y, z);
}

/**
* ```
* | z0 z2 0 | | e0 e2 e4 |
Expand Down Expand Up @@ -781,7 +764,7 @@ export class Matrix3 {
* | e2 e5 e8 | | 1 |
* ```
*/
public mulPt2(p: Point2): Point2 {
public mulPt(p: Point2): Point2 {
const e = this.elements;

const x = e[0] * p.x + e[3] * p.y + e[6];
Expand Down Expand Up @@ -817,31 +800,14 @@ export class Matrix3 {
this.set(e0, e1, e2, e3, e4, e5, e6, e7, e8);
}

/**
* ```
* | e0 e3 e6 | | x |
* | e1 e4 e7 | * | y |
* | e2 e5 e8 | | 1 |
* ```
*/
public mulVec2(v: Vector2): Vector2 {
const e = this.elements;

const x = e[0] * v.x + e[3] * v.y + e[6];
const y = e[1] * v.x + e[4] * v.y + e[7];
const w = e[2] * v.x + e[5] * v.y + e[8];

return Vector2.fromXYW(x, y, w);
}

/**
* ```
* | e0 e3 e6 | | x |
* | e1 e4 e7 | * | y |
* | e2 e5 e8 | | z |
* ```
*/
public mulVec3(v: Vector3): Vector3 {
public mulVec(v: Vector3): Vector3 {
const e = this.elements;

const x = e[0] * v.x + e[3] * v.y + e[6] * v.z;
Expand Down Expand Up @@ -1482,7 +1448,7 @@ export class Matrix4A {
* | 0 0 0 1 | | 1 |
* ```
*/
public mulPt3(p: Point3): Point3 {
public mulPt(p: Point3): Point3 {
const e = this.elements;

const x = e[0] * p.x + e[3] * p.y + e[6] * p.z + e[9];
Expand Down Expand Up @@ -1531,7 +1497,7 @@ export class Matrix4A {
* | 0 0 0 1 | | 1 |
* ```
*/
public mulVec3(v: Vector3): Vector3 {
public mulVec(v: Vector3): Vector3 {
const e = this.elements;

const x = e[0] * v.x + e[3] * v.y + e[6] * v.z + e[9];
Expand All @@ -1541,25 +1507,6 @@ export class Matrix4A {
return new Vector3(x, y, z);
}

/**
* ```
* | e0 e3 e6 e9 | | x |
* | e1 e4 e7 e10 | * | y |
* | e2 e5 e8 e11 | | z |
* | 0 0 0 1 | | w |
* ```
*/
public mulVec4(v: Vector4): Vector4 {
const e = this.elements;

const x = e[0] * v.x + e[3] * v.y + e[6] * v.z + e[9] * v.w;
const y = e[1] * v.x + e[4] * v.y + e[7] * v.z + e[10] * v.w;
const z = e[2] * v.x + e[5] * v.y + e[8] * v.z + e[11] * v.w;
const w = v.w;

return new Vector4(x, y, z, w);
}

/**
* ```
* | q0 q3 q6 0 | | e0 e3 e6 e9 |
Expand Down Expand Up @@ -2509,7 +2456,7 @@ export class Matrix4 {
* | e3 e7 e11 e15 | | 1 |
* ```
*/
public mulPt3(p: Point3): Point3 {
public mulPt(p: Point3): Point3 {
const e = this.elements;

const x = e[0] * p.x + e[4] * p.y + e[8] * p.z + e[12];
Expand Down Expand Up @@ -2555,25 +2502,6 @@ export class Matrix4 {
this.set(e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15);
}

/**
* ```
* | e0 e4 e8 e12 | | x |
* | e1 e5 e9 e13 | * | y |
* | e2 e6 e10 e14 | | z |
* | e3 e7 e11 e15 | | 1 |
* ```
*/
public mulVec3(v: Vector3): Vector3 {
const e = this.elements;

const x = e[0] * v.x + e[4] * v.y + e[8] * v.z + e[12];
const y = e[1] * v.x + e[5] * v.y + e[9] * v.z + e[13];
const z = e[2] * v.x + e[6] * v.y + e[10] * v.z + e[14];
const w = e[3] * v.x + e[7] * v.y + e[11] * v.z + e[15];

return Vector3.fromXYZW(x, y, z, w);
}

/**
* ```
* | e0 e4 e8 e12 | | x |
Expand All @@ -2582,7 +2510,7 @@ export class Matrix4 {
* | e3 e7 e11 e15 | | w |
* ```
*/
public mulVec4(v: Vector4): Vector4 {
public mulVec(v: Vector4): Vector4 {
const e = this.elements;

const x = e[0] * v.x + e[4] * v.y + e[8] * v.z + e[12] * v.w;
Expand Down
2 changes: 1 addition & 1 deletion packages/redgeometry/src/primitives/polygon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ export class Polygon2 {
public transform(mat: Matrix3 | Matrix3A): void {
const points = this.points;
for (let i = 0; i < points.length; i++) {
points[i] = mat.mulPt2(points[i]);
points[i] = mat.mulPt(points[i]);
}
}
}
4 changes: 2 additions & 2 deletions packages/redgeometry/tests/primitives/complex.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ test("Quaternion - mulPt/mulVec", () => {
const p = new Point2(1, 2);
const v = new Vector2(1, 2);

const p1 = mat.mulPt2(p);
const v1 = mat.mulVec2(v);
const p1 = mat.mulPt(p);
const v1 = mat.mulVec(v);
const p2 = z.mulPt(p);
const v2 = z.mulVec(v);

Expand Down
22 changes: 11 additions & 11 deletions packages/redgeometry/tests/primitives/matrix.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Complex } from "../../src/index.js";
import { Matrix3, Matrix3A, Matrix4, Matrix4A } from "../../src/primitives/matrix.js";
import { Point2, Point3 } from "../../src/primitives/point.js";
import { Quaternion, RotationOrder } from "../../src/primitives/quaternion.js";
import { Vector2, Vector3 } from "../../src/primitives/vector.js";
import { Vector2, Vector3, Vector4 } from "../../src/primitives/vector.js";
import {
expectToBeCloseComplex,
expectToBeClosePoint2,
Expand Down Expand Up @@ -49,7 +49,7 @@ test("Matrix3A - mulPt", () => {
const mat = Matrix3A.createIdentity();
const p = new Point2(1, 2);

const mulPt = mat.mulPt2(p);
const mulPt = mat.mulPt(p);

expect(mulPt).toEqual(p);
});
Expand All @@ -58,7 +58,7 @@ test("Matrix3A - mulVec", () => {
const mat = Matrix3A.createIdentity();
const v = new Vector2(1, 2);

const mulVec = mat.mulVec2(v);
const mulVec = mat.mulVec(v);

expect(mulVec).toEqual(v);
});
Expand Down Expand Up @@ -139,16 +139,16 @@ test("Matrix3 - mulPt", () => {
const mat = Matrix3.createIdentity();
const p = new Point2(1, 2);

const mulPt = mat.mulPt2(p);
const mulPt = mat.mulPt(p);

expect(mulPt).toEqual(p);
});

test("Matrix3 - mulVec", () => {
const mat = Matrix3.createIdentity();
const v = new Vector2(1, 2);
const v = new Vector3(1, 2, 3);

const mulVec = mat.mulVec2(v);
const mulVec = mat.mulVec(v);

expect(mulVec).toEqual(v);
});
Expand Down Expand Up @@ -229,7 +229,7 @@ test("Matrix4A - mulPt", () => {
const mat = Matrix4A.createIdentity();
const p = new Point3(1, 2, 3);

const mulPt = mat.mulPt3(p);
const mulPt = mat.mulPt(p);

expect(mulPt).toEqual(p);
});
Expand All @@ -238,7 +238,7 @@ test("Matrix4A - mulVec", () => {
const mat = Matrix4A.createIdentity();
const v = new Vector3(1, 2, 3);

const mulVec = mat.mulVec3(v);
const mulVec = mat.mulVec(v);

expect(mulVec).toEqual(v);
});
Expand Down Expand Up @@ -357,16 +357,16 @@ test("Matrix4 - mulPt", () => {
const mat = Matrix4.createIdentity();
const p = new Point3(1, 2, 3);

const mulPt = mat.mulPt3(p);
const mulPt = mat.mulPt(p);

expect(mulPt).toEqual(p);
});

test("Matrix4 - mulVec", () => {
const mat = Matrix4.createIdentity();
const v = new Vector3(1, 2, 3);
const v = new Vector4(1, 2, 3, 4);

const mulVec = mat.mulVec3(v);
const mulVec = mat.mulVec(v);

expect(mulVec).toEqual(v);
});
Expand Down
4 changes: 2 additions & 2 deletions packages/redgeometry/tests/primitives/quaternion.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ test("Quaternion - mulPt/mulVec", () => {
const p = new Point3(1, 2, 3);
const v = new Vector3(1, 2, 3);

const p1 = mat3.mul(mat2).mul(mat1).mulPt3(p);
const v1 = mat3.mul(mat2).mul(mat1).mulVec3(v);
const p1 = mat3.mul(mat2).mul(mat1).mulPt(p);
const v1 = mat3.mul(mat2).mul(mat1).mulVec(v);
const p2 = q3.mul(q2).mul(q1).mulPt(p);
const v2 = q3.mul(q2).mul(q1).mulVec(v);
const p3 = qa.mulPt(p);
Expand Down

0 comments on commit c4a552d

Please sign in to comment.