Skip to content

Commit

Permalink
chore(Vector3): remove cross method (Orillusion#243)
Browse files Browse the repository at this point in the history
Replace cross with crossProduct method
  • Loading branch information
hellmor authored Jul 15, 2023
1 parent e9e2f83 commit 79a998a
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/Engine3D.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ export class Engine3D {
type: 'HDRSKY',
sky: null,
skyExposure: 1.0,
defaultFar: 65536,//can't be to big
defaultFar: 65536,//can't be too big
defaultNear: 1,
},
light: {
Expand Down
4 changes: 2 additions & 2 deletions src/io/RayCastMeshDetail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class RayCastMeshDetail {
let E2 = v2.subtract(v0, Vector3.HELP_4);

// P
let P = ray.direction.cross(E2, Vector3.HELP_5);
let P = ray.direction.crossProduct(E2, Vector3.HELP_5);

// determinant
let det = dot(E1, P);
Expand Down Expand Up @@ -106,7 +106,7 @@ export class RayCastMeshDetail {
}

// Q
let Q = T.cross(E1, Vector3.HELP_1);
let Q = T.crossProduct(E1, Vector3.HELP_1);

// Calculate v and make sure u + v <= 1
let v = dot(ray.direction, Q);
Expand Down
2 changes: 1 addition & 1 deletion src/loader/parser/i3dm/I3DMLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export class I3DMLoader extends I3DMLoaderBase {
if (NORMAL_UP) {
temp.tempUp.set(NORMAL_UP[i * 3 + 0], NORMAL_UP[i * 3 + 1], NORMAL_UP[i * 3 + 2]);
temp.tempRight.set(NORMAL_RIGHT[i * 3 + 0], NORMAL_RIGHT[i * 3 + 1], NORMAL_RIGHT[i * 3 + 2]);
temp.tempRight.cross(temp.tempUp, temp.tempFwd).normalize();
temp.tempRight.crossProduct(temp.tempUp, temp.tempFwd).normalize();
temp.tempMat.makeBasis(temp.tempRight, temp.tempUp, temp.tempFwd);
temp.tempQuat.setFromRotationMatrix(temp.tempMat);
} else {
Expand Down
8 changes: 4 additions & 4 deletions src/math/MathUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,9 @@ export class MathUtil {
* @param to Vector 2
* @returns The Angle between two vectors
*/
public static angle_360(from, to) {
public static angle_360(from: Vector3, to: Vector3) {
let v3 = Vector3.HELP_0;
from.cross(to, v3);
from.crossProduct(to, v3);
if (v3.z > 0) {
return MathUtil.angle(from, to);
}
Expand Down Expand Up @@ -776,8 +776,8 @@ export function normalizeFast(inV: Vector3) {
/**
* @internal
*/
export function cross(lhs: Vector3, rhs: Vector3) {
return new Vector3(lhs.y * rhs.z - lhs.z * rhs.y, lhs.z * rhs.x - lhs.x * rhs.z, lhs.x * rhs.y - lhs.y * rhs.x);
export function crossProduct(lhs: Vector3, rhs: Vector3) {
return lhs.crossProduct(rhs);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/math/Matrix4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ export class Matrix4 {
zAxis.normalize();
}

xAxis = up.cross(zAxis, xAxis).normalize();
xAxis = up.crossProduct(zAxis, xAxis).normalize();

let yAxis = zAxis.crossProduct(xAxis, Vector3.HELP_2).normalize();

Expand Down
4 changes: 2 additions & 2 deletions src/math/Ray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ export class Ray {
v2.subtract(v0, this._E2);

// P
dir.cross(this._E2, this._P);
dir.crossProduct(this._E2, this._P);

// determinant
let det = this._E1.dotProduct(this._P);
Expand All @@ -291,7 +291,7 @@ export class Ray {
if (face.u < 0.0 || face.u > det) return null;

// Q
this._T.cross(this._E1, this._Q);
this._T.crossProduct(this._E1, this._Q);

// Calculate v and make sure u + v <= 1
face.v = dir.dotProduct(this._Q);
Expand Down
2 changes: 1 addition & 1 deletion src/math/Triangle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class Triangle {
let edge1 = new Vector3(v2.x - v1.x, v2.y - v1.y, v2.z - v1.z);
let edge2 = new Vector3(v3.x - v1.x, v3.y - v1.y, v3.z - v1.z);

let normal = edge2.cross(edge1);
let normal = edge2.crossProduct(edge1);
normal.normalize();
return normal;
}
Expand Down
49 changes: 22 additions & 27 deletions src/math/Vector3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,15 +344,6 @@ export class Vector3 {
return space;
}

public static cross(a: Vector3, b: Vector3, target: Vector3 = null): Vector3 {
target = target || new Vector3();
target.x = a.y * b.z - a.z * b.y;
target.y = a.z * b.x - a.x * b.z;
target.z = a.x * b.y - a.y * b.x;
target.w = 1;
return target;
}

/**
* Take the dot product of two vectors.
* @param a Vector a
Expand Down Expand Up @@ -521,6 +512,20 @@ export class Vector3 {
return target;
}

public subVectors(a: Vector3, b: Vector3): this {
this.x = a.x - b.x;
this.y = a.y - b.y;
this.z = a.z - b.z;
return this;
}

public distanceToSquared(v: Vector3): number {
let dx = this.x - v.x;
let dy = this.y - v.y;
let dz = this.z - v.z;
return dx * dx + dy * dy + dz * dz;
}

public addXYZW(x: number, y: number, z: number, w: number, target: Vector3 = null): Vector3 {
target ||= new Vector3();

Expand Down Expand Up @@ -557,21 +562,6 @@ export class Vector3 {
return v;
}

/**
* You take the cross product of two vectors,
* The cross product is going to be the perpendicular vector between these two vectors
* @param a Take the cross product of another vector
* @returns Vector3 returns the cross product vector
*/
public crossProduct(a: Vector3, target: Vector3 = null): Vector3 {
target = target || new Vector3();
target.x = this.y * a.z - this.z * a.y;
target.y = this.z * a.x - this.x * a.z;
target.z = this.x * a.y - this.y * a.x;
target.w = 1;
return target;
}

/**
* Subtract two vectors and assign the result to yourself
* @param a Minus vector
Expand Down Expand Up @@ -1018,12 +1008,12 @@ export class Vector3 {
}

/**
* The cross product of two Vector3s is this cross product of a
* You take the cross product of two vectors,
* The cross product is going to be the perpendicular vector between these two vectors
* @param a Take the cross product of another vector
* @returns Vector3 Returns the cross product vector
* @returns Vector3 returns the cross product vector
*/
public cross(a: Vector3, target: Vector3 = null): Vector3 {
public crossProduct(a: Vector3, target: Vector3 = null): Vector3 {
target = target || new Vector3();
target.x = this.y * a.z - this.z * a.y;
target.y = this.z * a.x - this.x * a.z;
Expand All @@ -1032,6 +1022,11 @@ export class Vector3 {
return target;
}

public crossVectors(a: Vector3, b: Vector3): this {
a.crossProduct(b, this);
return this;
}

public multiplyScalar(scalar: number) {
this.x *= scalar;
this.y *= scalar;
Expand Down

0 comments on commit 79a998a

Please sign in to comment.