Skip to content

Commit

Permalink
Clean up point and vector primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
yzrmn committed Sep 11, 2024
1 parent 2dd8dcf commit e740b6b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 31 deletions.
2 changes: 1 addition & 1 deletion packages/redgeometry/src/core/mesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1255,7 +1255,7 @@ export class Mesh2 {
}
}

if (p1.lt(p2)) {
if (p1.x !== p2.x ? p1.x < p2.x : p1.y < p2.y) {
// log.infoDebug("{} -> Regular (Interior Above)", p1);
this.monotoneDelete(status, e0, e1);
this.monotoneInsert(status, e1);
Expand Down
14 changes: 8 additions & 6 deletions packages/redgeometry/src/core/snapround.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,18 +347,20 @@ export class SnapRound2 {

for (let i = 0; i < segments.length; i++) {
const s = segments[i];
const p0 = s.p0;
const p1 = s.p1;

if (s.p0.eq(s.p1)) {
if (p0.eq(p1)) {
// Ignore empty edges
continue;
}

if (s.p0.lt(s.p1)) {
events.push(new SnapRoundSweepEvent2(s.p0, s.p1, s, i, true));
events.push(new SnapRoundSweepEvent2(s.p1, s.p0, s, i, false));
if (p0.x !== p1.x ? p0.x < p1.x : p0.y < p1.y) {
events.push(new SnapRoundSweepEvent2(p0, p1, s, i, true));
events.push(new SnapRoundSweepEvent2(p1, p0, s, i, false));
} else {
events.push(new SnapRoundSweepEvent2(s.p1, s.p0, s, i, true));
events.push(new SnapRoundSweepEvent2(s.p0, s.p1, s, i, false));
events.push(new SnapRoundSweepEvent2(p1, p0, s, i, true));
events.push(new SnapRoundSweepEvent2(p0, p1, s, i, false));
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/redgeometry/src/internal/path-sweep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,16 @@ export function createSweepEventQueue(snapRound: SnapRound2): PathSweepEvent2[]

for (const seg of edgeSegments) {
// Fetch points, weight becomes winding
const wind = seg.ref.weight;
const p0 = seg.p0;
const p1 = seg.p1;
const wind = seg.ref.weight;

if (p0.eq(p1)) {
// Ignore empty edges
continue;
}

if (p0.lt(p1)) {
if (p0.x !== p1.x ? p0.x < p1.x : p0.y < p1.y) {
queue.push(new PathSweepEvent2(p0, p1, seg, wind, true));
queue.push(new PathSweepEvent2(p1, p0, seg, wind, false));
} else {
Expand Down
16 changes: 0 additions & 16 deletions packages/redgeometry/src/primitives/point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,6 @@ export class Point2 implements Point2Like {
return eqApproxRel(this.x, p.x, eps) && eqApproxRel(this.y, p.y, eps);
}

public gt(p: Point2): boolean {
return this.x === p.x ? this.y > p.y : this.x > p.x;
}

public gte(p: Point2): boolean {
return this.x === p.x ? this.y >= p.y : this.x > p.x;
}

public isFinite(): boolean {
return Number.isFinite(this.x) && Number.isFinite(this.y);
}
Expand All @@ -145,14 +137,6 @@ export class Point2 implements Point2Like {
return new Point2(x, y);
}

public lt(p: Point2): boolean {
return this.x === p.x ? this.y < p.y : this.x < p.x;
}

public lte(p: Point2): boolean {
return this.x === p.x ? this.y <= p.y : this.x < p.x;
}

public sub(p: Point2): Vector2 {
return new Vector2(this.x - p.x, this.y - p.y);
}
Expand Down
22 changes: 16 additions & 6 deletions packages/redgeometry/src/primitives/vector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ export interface Vector3Like {
}

export interface Vector4Like {
w: number;
x: number;
y: number;
z: number;
w: number;
}

export class Vector2 implements Vector2Like {
Expand Down Expand Up @@ -157,9 +157,9 @@ export class Vector2 implements Vector2Like {
}

/**
* Returns the cross product of the current vector and `v` as a scalar value.
* Returns the 2D cross product of the current vector and `v` as a scalar value.
*
* The 2D cross product is defined by z-value of the 3D cross product: \
* The 2D cross product is defined by the magnitude of the 3D cross product: \
* `(x1, y1, 0) cross (x2, y2, 0) == (0, 0, x1 * y2 - y1 * x2)`
*
* Identity relating to the dot product: `v1 cross v2 === v1 dot normal(v2)`
Expand Down Expand Up @@ -295,7 +295,12 @@ export class Vector2 implements Vector2Like {

public unitOrZero(): Vector2 {
const len = this.len();
return len > 0 ? this.divS(len) : Vector2.createZero();

if (len === 0) {
return Vector2.createZero();
}

return this.divS(len);
}
}

Expand Down Expand Up @@ -628,15 +633,20 @@ export class Vector3 implements Vector3Like {

public unitOrZero(): Vector3 {
const len = this.len();
return len > 0 ? this.divS(len) : Vector3.createZero();

if (len === 0) {
return Vector3.createZero();
}

return this.divS(len);
}
}

export class Vector4 implements Vector4Like {
public w: number;
public x: number;
public y: number;
public z: number;
public w: number;

public constructor(x: number, y: number, z: number, w: number) {
this.x = x;
Expand Down

0 comments on commit e740b6b

Please sign in to comment.