Skip to content

Commit

Permalink
Change vector API
Browse files Browse the repository at this point in the history
  • Loading branch information
yzrmn committed Jul 31, 2024
1 parent 125a34c commit 916ab62
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 26 deletions.
4 changes: 2 additions & 2 deletions packages/redgeometry/src/primitives/bezier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1102,9 +1102,9 @@ export class BezierRCurve2 {
}

public getProjectivePoints(): [Point3, Point3, Point3] {
const p0 = Point3.fromXY(this.p0.x, this.p0.y);
const p0 = Point3.fromXYW(this.p0.x, this.p0.y, 1);
const p1 = Point3.fromXYW(this.p1.x, this.p1.y, this.w);
const p2 = Point3.fromXY(this.p2.x, this.p2.y);
const p2 = Point3.fromXYW(this.p2.x, this.p2.y, 1);

return [p0, p1, p2];
}
Expand Down
4 changes: 0 additions & 4 deletions packages/redgeometry/src/primitives/point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,6 @@ export class Point3 implements Point3Like {
return new Point3(obj.x, obj.y, obj.z);
}

public static fromXY(x: number, y: number): Point3 {
return new Point3(x, y, 1);
}

public static fromXYW(x: number, y: number, w: number): Point3 {
return new Point3(w * x, w * y, w);
}
Expand Down
8 changes: 4 additions & 4 deletions packages/redgeometry/src/primitives/quaternion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ export class Quaternion implements QuaternionLike {
// Vector halfway between `v1` and `v2`
const vu = v1u.add(v2u).unitOrZero();

// If `v` is zero then `dot = 0` and `cross` just needs to be any normal of `v1`
const dot = v1u.dot(vu);
const cross = vu.isZero() ? v1u.normalAny() : v1u.cross(vu);
// If `vu` is zero then `vd = 0` and `vn` just needs to be any normal of `v1`
const vd = v1u.dot(vu);
const vn = vu.isZero() ? v1u.normalAroundAny() : v1u.normalAround(vu);

return new Quaternion(dot, cross.x, cross.y, cross.z);
return new Quaternion(vd, vn.x, vn.y, vn.z);
}

public static fromRotationEuler(angleX: number, angleY: number, angleZ: number, order: RotationOrder): Quaternion {
Expand Down
76 changes: 60 additions & 16 deletions packages/redgeometry/src/primitives/vector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ export class Vector2 implements Vector2Like {
return { x: v.x, y: v.y };
}

public abs(): Vector2 {
const x = Math.abs(this.x);
const y = Math.abs(this.y);
return new Vector2(x, y);
}

/**
* Returns the sum of the current vector and a vector `v`.
*/
Expand Down Expand Up @@ -216,6 +222,13 @@ export class Vector2 implements Vector2Like {
return new Vector2(this.x - v.x, this.y - v.y);
}

/**
* Substracts the current vector from a point `p`.
*/
public subPt(p: Point2): Point2 {
return new Point2(p.x - this.x, p.y - this.y);
}

public toArray(): [number, number] {
return [this.x, this.y];
}
Expand Down Expand Up @@ -292,8 +305,8 @@ export class Vector3 implements Vector3Like {
return new Vector3(obj.x, obj.y, obj.z);
}

public static fromXY(x: number, y: number): Vector3 {
return new Vector3(x, y, 1);
public static fromXYW(x: number, y: number, w: number): Vector3 {
return new Vector3(w * x, w * y, w);
}

public static fromXYZW(x: number, y: number, z: number, w: number): Vector3 {
Expand All @@ -304,6 +317,13 @@ export class Vector3 implements Vector3Like {
return { x: v.x, y: v.y, z: v.z };
}

public abs(): Vector3 {
const x = Math.abs(this.x);
const y = Math.abs(this.y);
const z = Math.abs(this.z);
return new Vector3(x, y, z);
}

/**
* Returns the sum of the current vector and a vector `v`.
*/
Expand Down Expand Up @@ -404,16 +424,23 @@ export class Vector3 implements Vector3Like {
return new Vector3(-this.x, -this.y, -this.z);
}

public normalAround(v: Vector3): Vector3 {
return this.cross(v);
}

/**
* Returns a non-zero normal vector.
* Returns the normal vector around the most appropriate axis.
*/
public normalAny(): Vector3 {
if (this.x !== 0) {
return this.normalY();
} else if (this.y !== 0) {
return this.normalZ();
public normalAroundAny(): Vector3 {
const absX = Math.abs(this.x);
const absY = Math.abs(this.y);
const absZ = Math.abs(this.z);

// Use the two biggest absolute values
if (absX <= absY) {
return absX <= absZ ? this.normalAroundX() : this.normalAroundZ();
} else {
return this.normalX();
return absY <= absZ ? this.normalAroundY() : this.normalAroundZ();
}
}

Expand All @@ -422,8 +449,10 @@ export class Vector3 implements Vector3Like {
*
* The normal is defined by the cross product: \
* `(x, y, z) cross (1, 0, 0) == (0, z, -y)`
*
* Note: Might return a zero vector.
*/
public normalX(): Vector3 {
public normalAroundX(): Vector3 {
return new Vector3(0, this.z, -this.y);
}

Expand All @@ -432,8 +461,10 @@ export class Vector3 implements Vector3Like {
*
* The normal is defined by the cross product: \
* `(x, y, z) cross (0, 1, 0) == (-z, 0, x)`
*
* Note: Might return a zero vector.
*/
public normalY(): Vector3 {
public normalAroundY(): Vector3 {
return new Vector3(-this.z, 0, this.x);
}

Expand All @@ -442,15 +473,24 @@ export class Vector3 implements Vector3Like {
*
* The normal is defined by the cross product: \
* `(x, y, z) cross (0, 0, 1) == (y, -x, 0)`
*
* Note: Might return a zero vector.
*/
public normalZ(): Vector3 {
public normalAroundZ(): Vector3 {
return new Vector3(this.y, -this.x, 0);
}

public sub(v: Vector3): Vector3 {
return new Vector3(this.x - v.x, this.y - v.y, this.z - v.z);
}

/**
* Substracts the current vector from a point `p`.
*/
public subPt(p: Point3): Point3 {
return new Point3(p.x - this.x, p.y - this.y, p.z - this.z);
}

public toArray(): [number, number, number] {
return [this.x, this.y, this.z];
}
Expand Down Expand Up @@ -536,14 +576,18 @@ export class Vector4 implements Vector4Like {
return new Vector4(obj.x, obj.y, obj.z, obj.w);
}

public static fromXYZ(x: number, y: number, z: number): Vector4 {
return new Vector4(x, y, z, 1);
}

public static toObject(v: Vector4): Vector4Like {
return { x: v.x, y: v.y, z: v.z, w: v.w };
}

public abs(): Vector4 {
const x = Math.abs(this.x);
const y = Math.abs(this.y);
const z = Math.abs(this.z);
const w = Math.abs(this.w);
return new Vector4(x, y, z, w);
}

/**
* Returns the sum of the current vector and a vector `v`.
*/
Expand Down

0 comments on commit 916ab62

Please sign in to comment.