Skip to content

Commit

Permalink
Harmonize arithmetic operations API
Browse files Browse the repository at this point in the history
  • Loading branch information
yzrmn committed Aug 24, 2024
1 parent 2e2a6c5 commit 3271bee
Show file tree
Hide file tree
Showing 22 changed files with 186 additions and 180 deletions.
6 changes: 3 additions & 3 deletions packages/redgeometry-app/src/parts/gpu-cube.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,10 @@ function cameraMoveSystem(world: World): void {
let v = new Vector3(x, y, z);

if (!v.isZero()) {
v = v.unitOrZero().mul(delta * vel);
v = camRot.mulVec(v);
v = v.unitOrZero().mulS(delta * vel);
v = camRot.mulV(v);

camPos = camPos.addVec(v);
camPos = camPos.addV(v);
}

transform.rotation = camRot;
Expand Down
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.mulPt(e.p0);
const p1 = m.mulPt(e.p1);
const p0 = m.mulP(e.p0);
const p1 = m.mulP(e.p1);
const pp0 = Point2.fromObject(p0);
const pp1 = Point2.fromObject(p1);
output.push(new Edge2(pp0, pp1));
Expand Down
4 changes: 2 additions & 2 deletions packages/redgeometry-app/src/utility/straight-skeleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class KineticVertex {

public static getVelocity(n1: Vector2, n2: Vector2): Vector2 {
const k = n1.add(n2);
return k.mul(2).div(k.lenSq());
return k.mulS(2).divS(k.lenSq());
}

public static getWavefrontEdgeCcw(e: MeshEdge2): MeshEdge2 {
Expand Down Expand Up @@ -79,7 +79,7 @@ export class KineticVertex {
}

public getPositionAt(t: number): Point2 {
return this.orig.addMulVec(this.vel, t - this.t0);
return this.orig.addMulV(this.vel, t - this.t0);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/redgeometry/src/core/path-flatten.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export class PathFlattenIncremental2 implements PathFlatten2 {

for (let t = step; t < 1; t += step) {
// Evaluate points (Horner's method)
const p = qa.mul(t).add(qb).mul(t).addPt(qc);
const p = qa.mulS(t).add(qb).mulS(t).addP(qc);
output.lineTo(p);
}

Expand Down
16 changes: 8 additions & 8 deletions packages/redgeometry/src/core/path-offset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,15 +246,15 @@ export class PathOffsetIncremental2 implements PathOffset2 {
}

private offsetLinear(p1: Point2, m: Vector2): void {
const v = m.unit().normal().mul(this.d);
const v = m.unit().normal().mulS(this.d);

this.buffer.lineTo(p1.addVec(v));
this.buffer.lineTo(p1.addV(v));
}

private offsetMove(p0: Point2, m: Vector2): void {
const v = m.unit().normal().mul(this.d);
const v = m.unit().normal().mulS(this.d);

this.buffer.moveTo(p0.addVec(v));
this.buffer.moveTo(p0.addV(v));
}

private offsetQuadratic(c0: Bezier2Curve2): void {
Expand Down Expand Up @@ -536,15 +536,15 @@ export class PathOffsetRecursive2 implements PathOffset2 {
}

private offsetLinear(p1: Point2, m: Vector2): void {
const v = m.unit().normal().mul(this.d);
const v = m.unit().normal().mulS(this.d);

this.buffer.lineTo(p1.addVec(v));
this.buffer.lineTo(p1.addV(v));
}

private offsetMove(p0: Point2, m: Vector2): void {
const v = m.unit().normal().mul(this.d);
const v = m.unit().normal().mulS(this.d);

this.buffer.moveTo(p0.addVec(v));
this.buffer.moveTo(p0.addV(v));
}

private offsetQuadratic(c0: Bezier2Curve2): void {
Expand Down
40 changes: 20 additions & 20 deletions packages/redgeometry/src/core/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,15 @@ export class Path2 implements PathSink2 {

this.moveTo(pc);

this.lineTo(mat.mulVec(v1).toPoint());
this.lineTo(mat.mulV(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.mulVec(vc).toPoint();
const p2 = mat.mulVec(v1).toPoint();
const p1 = mat.mulV(vc).toPoint();
const p2 = mat.mulV(v1).toPoint();
this.arcTo(p1, p2);

vc = vc.normal().neg();
Expand All @@ -244,14 +244,14 @@ export class Path2 implements PathSink2 {

// Calculate the remaining control point
vc = v1.add(v2);
vc = vc.mul(2).div(vc.dot(vc));
vc = vc.mulS(2).divS(vc.dot(vc));

// This is actually half of the remaining cos. It is required that `v1 dot v2 > -1` holds
// 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.mulVec(vc).toPoint();
const p2 = mat.mulVec(v2).toPoint();
const p1 = mat.mulV(vc).toPoint();
const p2 = mat.mulV(v2).toPoint();
this.conicTo(p1, p2, cos);

this.close();
Expand Down Expand Up @@ -591,9 +591,9 @@ export class Path2 implements PathSink2 {
const mat = Matrix3A.fromRotation(cos, -sin);

// Vector from center (transformed midpoint)
let v = p0.sub(p1).mul(0.5);
let v = p0.sub(p1).mulS(0.5);

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

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

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

// New vector from center (unit midpoint)
v = pp1.sub(pp0).mul(0.5);
v = pp1.sub(pp0).mulS(0.5);

let pc = pp0.addVec(v);
let pc = pp0.addV(v);

// If `lenght^2 >= 1` the point is already the center
const len2 = v.lenSq();
Expand All @@ -628,12 +628,12 @@ export class Path2 implements PathSink2 {
const f = Math.sqrt(1 / len2 - 1);

// TODO: Investigate correctness of `normal.neg`
v = v.normal().neg().mul(f);
v = v.normal().neg().mulS(f);

if (largeArc !== sweep) {
pc = pc.addVec(v);
pc = pc.addV(v);
} else {
pc = pc.subVec(v);
pc = pc.subV(v);
}
}

Expand Down Expand Up @@ -692,8 +692,8 @@ export class Path2 implements PathSink2 {
v1 = v1.normal().neg();

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

this.arcTo(pp0, pp1);

Expand All @@ -704,10 +704,10 @@ export class Path2 implements PathSink2 {

// Calculate the remaining control point
v = v1.add(v2);
v = v.mul(2).div(v.dot(v));
v = v.mulS(2).divS(v.dot(v));

// Final arc segment
pp0 = mat.mulVec(v).toPoint();
pp0 = mat.mulV(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 @@ -739,7 +739,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.mulPt(points[i]);
points[i] = mat.mulP(points[i]);
}
}
}
6 changes: 3 additions & 3 deletions packages/redgeometry/src/internal/bezier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,17 +285,17 @@ function getWinding(t: number, px: number, x: number, yy: number): number {
}

function sampleArcLengthQuadratic(wz: number, xz: number, qqa: Vector2, qqb: Vector2): number {
const v = qqa.mul(xz).add(qqb);
const v = qqa.mulS(xz).add(qqb);
return wz * v.len();
}

function sampleArcLengthCubic(wz: number, xz: number, qqa: Vector2, qqb: Vector2, qqc: Vector2): number {
const v = qqa.mul(xz).add(qqb).mul(xz).add(qqc);
const v = qqa.mulS(xz).add(qqb).mulS(xz).add(qqc);
return wz * v.len();
}

function sampleArcLengthConic(wz: number, xz: number, qqa: Vector3, qqb: Vector3, qqc: Point3): number {
const vv = qqa.mul(xz).add(qqb).mul(xz).addPt(qqc);
const vv = qqa.mulS(xz).add(qqb).mulS(xz).addP(qqc);
const v = Vector2.fromXYW(vv.x, vv.y, vv.z * vv.z);
return wz * v.len();
}
44 changes: 22 additions & 22 deletions packages/redgeometry/src/internal/path-offset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ export function offsetQuadraticSimple(path: Path2, c: Bezier2Curve2, d: number):

v1 = v1.add(v2);

v1 = v1.mul(2 * d).div(v1.lenSq());
v2 = v2.mul(d);
v1 = v1.mulS(2 * d).divS(v1.lenSq());
v2 = v2.mulS(d);

path.quadTo(c.p1.addVec(v1), c.p2.addVec(v2));
path.quadTo(c.p1.addV(v1), c.p2.addV(v2));
}
}

Expand Down Expand Up @@ -70,38 +70,38 @@ export function insertOuterJoin(

switch (join) {
case JoinType.Bevel: {
path.lineTo(p.addMulVec(n1, d));
path.lineTo(p.addMulV(n1, d));

break;
}
case JoinType.Miter: {
let k = n0.add(n1);

k = k.mul(2 * d).div(k.lenSq());
k = k.mulS(2 * d).divS(k.lenSq());

if (k.lenSq() <= mld * mld) {
path.lineTo(p.addVec(k));
path.lineTo(p.addV(k));
}

path.lineTo(p.addMulVec(n1, d));
path.lineTo(p.addMulV(n1, d));

break;
}
case JoinType.MiterClip: {
let k = n0.add(n1);

k = k.mul(2 * d).div(k.lenSq());
k = k.mulS(2 * d).divS(k.lenSq());

const pp0 = p.addMulVec(n0, d);
const pp2 = p.addMulVec(n1, d);
const pp0 = p.addMulV(n0, d);
const pp2 = p.addMulV(n1, d);

if (k.lenSq() <= mld * mld) {
// Same as miter join
path.lineTo(p.addVec(k));
path.lineTo(p.addV(k));
} else if (n0.dot(n1) <= COS_ACUTE) {
// Join is too sharp ('k' is approaching infinity)
path.lineTo(pp0.addMulVec(n0.normal(), -mld));
path.lineTo(pp2.addMulVec(n1.normal(), mld));
path.lineTo(pp0.addMulV(n0.normal(), -mld));
path.lineTo(pp2.addMulV(n1.normal(), mld));
} else {
const kov = k.dot(p.sub(pp0));
const kok = k.dot(k);
Expand All @@ -110,7 +110,7 @@ export function insertOuterJoin(

// Fall back to bevel otherwise
if (t > 0) {
const pp1 = p.addVec(k);
const pp1 = p.addV(k);

path.lineTo(pp0.lerp(pp1, t));
path.lineTo(pp2.lerp(pp1, t));
Expand All @@ -122,19 +122,19 @@ export function insertOuterJoin(
break;
}
case JoinType.Round: {
const pp0 = p.addMulVec(n0, d);
const pp2 = p.addMulVec(n1, d);
const pp0 = p.addMulV(n0, d);
const pp2 = p.addMulV(n1, d);

if (n0.dot(n1) < 0) {
// Obtuse angle (2 segments)
const nm = pp2.sub(pp0).unitOrZero().normal();

let k = n0.add(nm);

k = k.mul(2 * d).div(k.lenSq());
k = k.mulS(2 * d).divS(k.lenSq());

const pc1 = p.addVec(k);
const pp1 = p.addMulVec(nm, d);
const pc1 = p.addV(k);
const pp1 = p.addMulV(nm, d);
const pc2 = pc1.lerp(pp1, 2);

const w = BezierRCurve2.getWeightFromVectors(p, pc1, pp1);
Expand All @@ -145,9 +145,9 @@ export function insertOuterJoin(
// Acute angle (1 segment)
let k = n0.add(n1);

k = k.mul(2 * d).div(k.lenSq());
k = k.mulS(2 * d).divS(k.lenSq());

const pc = p.addVec(k);
const pc = p.addV(k);

const w = BezierRCurve2.getWeightFromVectors(p, pc, pp2);

Expand All @@ -167,5 +167,5 @@ export function insertInnerJoin(path: Path2, p: Point2, n1: Vector2, d: number):
path.lineTo(p);

// Bevel join
path.lineTo(p.addMulVec(n1, d));
path.lineTo(p.addMulV(n1, d));
}
Loading

0 comments on commit 3271bee

Please sign in to comment.