Skip to content

Commit

Permalink
Change point API
Browse files Browse the repository at this point in the history
  • Loading branch information
yzrmn committed Jul 27, 2024
1 parent ec36431 commit 125a34c
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 76 deletions.
2 changes: 1 addition & 1 deletion packages/redgeometry-app/src/parts/gpu-cube.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ function cameraMoveSystem(world: World): void {
v = v.unitOrZero().mul(delta * vel);
v = camRot.mulVec(v);

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

transform.rotation = camRot;
Expand Down
2 changes: 1 addition & 1 deletion packages/redgeometry-app/src/utility/straight-skeleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class KineticVertex {
}

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

Expand Down
8 changes: 4 additions & 4 deletions packages/redgeometry/src/core/path-offset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,13 @@ export class PathOffsetIncremental2 implements PathOffset2 {
private offsetLinear(p1: Point2, m: Vector2): void {
const v = m.unit().normal().mul(this.d);

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

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

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

private offsetQuadratic(c0: Bezier2Curve2): void {
Expand Down Expand Up @@ -538,13 +538,13 @@ export class PathOffsetRecursive2 implements PathOffset2 {
private offsetLinear(p1: Point2, m: Vector2): void {
const v = m.unit().normal().mul(this.d);

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

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

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

private offsetQuadratic(c0: Bezier2Curve2): void {
Expand Down
6 changes: 3 additions & 3 deletions packages/redgeometry/src/core/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ export class Path2 implements PathSink2 {
// New vector from center (unit midpoint)
v = pp1.sub(pp0).mul(0.5);

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

// If `lenght^2 >= 1` the point is already the center
const len2 = v.lenSq();
Expand All @@ -631,9 +631,9 @@ export class Path2 implements PathSink2 {
v = v.normal().neg().mul(f);

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

Expand Down
32 changes: 16 additions & 16 deletions packages/redgeometry/src/internal/path-offset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function offsetQuadraticSimple(path: Path2, c: Bezier2Curve2, d: number):
v1 = v1.mul(2 * d).div(v1.lenSq());
v2 = v2.mul(d);

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

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

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

break;
}
Expand All @@ -80,10 +80,10 @@ export function insertOuterJoin(
k = k.mul(2 * d).div(k.lenSq());

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

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

break;
}
Expand All @@ -92,16 +92,16 @@ export function insertOuterJoin(

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

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

if (k.lenSq() <= mld * mld) {
// Same as miter join
path.lineTo(p.add(k));
path.lineTo(p.addVec(k));
} else if (n0.dot(n1) <= COS_ACUTE) {
// Join is too sharp ('k' is approaching infinity)
path.lineTo(pp0.addMul(n0.normal(), -mld));
path.lineTo(pp2.addMul(n1.normal(), mld));
path.lineTo(pp0.addMulVec(n0.normal(), -mld));
path.lineTo(pp2.addMulVec(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.add(k);
const pp1 = p.addVec(k);

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

if (n0.dot(n1) < 0) {
// Obtuse angle (2 segments)
Expand All @@ -133,8 +133,8 @@ export function insertOuterJoin(

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

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

const w = BezierRCurve2.getWeightFromVectors(p, pc1, pp1);
Expand All @@ -147,7 +147,7 @@ export function insertOuterJoin(

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

const pc = p.add(k);
const pc = p.addVec(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.addMul(n1, d));
path.lineTo(p.addMulVec(n1, d));
}
23 changes: 12 additions & 11 deletions packages/redgeometry/src/internal/path-stroke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,15 +295,15 @@ export class StrokeState {
private insertLinearStroke(p: Point2, m: Vector2): void {
const v = m.unit().normal().mul(this.distance);

this.left.lineTo(p.add(v));
this.right.lineTo(p.add(v.neg()));
this.left.lineTo(p.addVec(v));
this.right.lineTo(p.subVec(v));
}

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

this.left.moveTo(p0.add(v));
this.right.moveTo(p0.add(v.neg()));
this.left.moveTo(p0.addVec(v));
this.right.moveTo(p0.subVec(v));
}

private insertQuadraticDegenerateDashStroke(p0: Point2, p1: Point2, p2: Point2): void {
Expand Down Expand Up @@ -384,8 +384,8 @@ export class StrokeState {
v1 = v1.mul(2 * d).div(v1.lenSq());
v2 = v2.mul(d);

this.left.quadTo(c.p1.add(v1), c.p2.add(v2));
this.right.quadTo(c.p1.add(v1.neg()), c.p2.add(v2.neg()));
this.left.quadTo(c.p1.addVec(v1), c.p2.addVec(v2));
this.right.quadTo(c.p1.subVec(v1), c.p2.subVec(v2));
}
}

Expand Down Expand Up @@ -456,8 +456,8 @@ export function insertStrokeCap(path: Path2, p1: Point2, cap: CapType | CustomCa
}

const v = p1.sub(p0).mul(0.5).normal();
path.lineTo(p0.add(v));
path.lineTo(p1.add(v));
path.lineTo(p0.addVec(v));
path.lineTo(p1.addVec(v));
path.lineTo(p1);

break;
Expand All @@ -469,9 +469,10 @@ export function insertStrokeCap(path: Path2, p1: Point2, cap: CapType | CustomCa
break;
}

const v = p1.sub(p0).mul(0.5).normal();
path.arcTo(p0.add(v), p0.add(v.sub(v.normal())));
path.arcTo(p1.add(v), p1);
const v0 = p1.sub(p0).mul(0.5).normal();
const v1 = v0.sub(v0.normal());
path.arcTo(p0.addVec(v0), p0.addVec(v1));
path.arcTo(p1.addVec(v0), p1);

break;
}
Expand Down
10 changes: 8 additions & 2 deletions packages/redgeometry/src/primitives/bezier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,10 @@ export class Bezier2Curve2 {
const pp = qa.mul(2 * t).add(qb);
const ppp = qa.mul(2);

return p.addMul(pp.normal(), pp.lenSq() / pp.cross(ppp));
const v = pp.normal();
const f = pp.lenSq() / pp.cross(ppp);

return p.addMulVec(v, f);
}

public getOffsetCuspParameter(rad: number): [number, number] {
Expand Down Expand Up @@ -714,7 +717,10 @@ export class Bezier3Curve2 {
.add(qc);
const ppp = qa.mul(6 * t).addMul(qb, 2);

return p.addMul(pp.normal(), pp.lenSq() / pp.cross(ppp));
const v = pp.normal();
const f = pp.lenSq() / pp.cross(ppp);

return p.addMulVec(v, f);
}

public getInflectionParameter(): [number, number] {
Expand Down
14 changes: 10 additions & 4 deletions packages/redgeometry/src/primitives/edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,8 @@ export class Edge2 {

public normal(): Edge2 {
const vn = this.vector().normal();
return new Edge2(this.p0, this.p0.add(vn));
const p1 = this.p0.addVec(vn);
return new Edge2(this.p0, p1);
}

public reverse(): Edge2 {
Expand All @@ -370,7 +371,9 @@ export class Edge2 {
}

public translate(v: Vector2): Edge2 {
return new Edge2(this.p0.add(v), this.p1.add(v));
const p0 = this.p0.addVec(v);
const p1 = this.p1.addVec(v);
return new Edge2(p0, p1);
}

public vector(): Vector2 {
Expand Down Expand Up @@ -465,7 +468,8 @@ export class Edge3 {

public getNormalAround(v: Vector3): Edge3 {
const vn = this.vector().cross(v);
return new Edge3(this.p0, this.p0.add(vn));
const p1 = this.p0.addVec(vn);
return new Edge3(this.p0, p1);
}

/**
Expand Down Expand Up @@ -515,7 +519,9 @@ export class Edge3 {
}

public translate(v: Vector3): Edge3 {
return new Edge3(this.p0.add(v), this.p1.add(v));
const p0 = this.p0.addVec(v);
const p1 = this.p1.addVec(v);
return new Edge3(p0, p1);
}

public vector(): Vector3 {
Expand Down
Loading

0 comments on commit 125a34c

Please sign in to comment.