Skip to content

Commit

Permalink
Moved Spherical and Polar to math.polar.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
james-pre committed Sep 7, 2022
1 parent 226b012 commit 006be27
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 113 deletions.
117 changes: 117 additions & 0 deletions packages/dev/core/src/Maths/math.polar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/* eslint-disable @typescript-eslint/naming-convention */
import type { DeepImmutable, Nullable, float } from "../types";
import {Vector2, Vector3} from "./math.vector";

/**
* Class used to store (r, theta) vector representation
*/
export class Polar {
public radius: number;
public theta: number;

/**
* Creates a new Polar object
* @param radius the radius of the vector
* @param theta the angle of the vector
*/
constructor(radius: number, theta: number){
this.radius = radius;
this.theta = theta;
}

/**
* Gets the rectangular coordinates of the current Polar
* @param the reference to assign the result
* @returns the updated reference
*/
public toVector2ToRef(ref: Vector2): Vector2{
Vector2.FromPolarToRef(this, ref);
return ref;
}

/**
* Gets the rectangular coordinates of the current Polar
* @returns the rectangular coordinates
*/
public toVector2(): Vector2{
return Vector2.FromPolar(this);
}

/**
* Converts a given Vector2 to its polar coordinates
* @param v the Vector2 to convert
* @param ref the reference to assign the result
* @returns the updated reference
*/
public static FromVector2ToRef(v: Vector2, ref: Polar): Polar{
const theta = Math.sign(v.y) * Math.acos(v.x / v.length());
ref.radius = v.length();
ref.theta = theta;
return ref
}

/**
* Converts a given Vector2 to its polar coordinates
* @param v the Vector2 to convert
* @returns a Polar
*/
public static FromVector2(v: Vector2): Polar{
let polar = new Polar();
Polar.FromVector2ToRef(v, polar);
return polar;
}
}

export class Spherical {
public length: number;
public theta: number;
public phi: number;

constructor(length, theta, phi){
this.length = length;
this.theta = theta;
this.phi = phi;
}

/**
* Assigns the rectangular coordinates of the current Spherical to a Vector3
* @param ref the Vector3 to update
* @returns the updated Vector3
*/
public toVector3ToRef(ref: DeepImmutable<Vector3>): Vector3{
return Vector3.FromSphericalToRef(ref);
}

/**
* Gets a Vector3 from the current spherical coordinates
* @returns the Vector3
*/
public toVector3(): Vector3{
return Vector3.FromSpherical(this);
}

/**
* Assigns the spherical coordinates from a Vector3
* @param vector the vector to convert
* @param ref the Spherical to update
* @returns the updated ref
*/
public static FromVector3ToRef(vector: DeepImmutable<Vector3>, ref: Spherical): Spherical{
let rotation = Vector3.PitchYawRollForDirectionChange(Vector3.Zero(), vector);
ref.radius = vector.length();
ref.theta = rotation.x;
ref.phi = rotation.y;
return ref;
}

/**
* Gets a Spherical from a Vector3
* @param vector defines the vector in (x, y, z) coordinate space
* @returns a new Spherical
*/
public static FromVector3(vector: DeepImmutable<Vector3>): Spherical{
let spherical = new Spherical();
Spherical.FromVector3ToRef(vector, spherical);
return spherical;
}
}
114 changes: 1 addition & 113 deletions packages/dev/core/src/Maths/math.vector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { RegisterClass } from "../Misc/typeStore";
import type { Plane } from "./math.plane";
import { PerformanceConfigurator } from "../Engines/performanceConfigurator";
import { EngineStore } from "../Engines/engineStore";
import {Polar, Spherical} from "./math.polar";

type TransformNode = import("../Meshes/transformNode").TransformNode;

Expand Down Expand Up @@ -859,65 +860,6 @@ export class Vector2 {
}
}

/**
* Class used to store (r, theta) vector representation
*/
export class Polar {
public radius: number;
public theta: number;

/**
* Creates a new Polar object
* @param radius the radius of the vector
* @param theta the angle of the vector
*/
constructor(radius: number, theta: number){
this.radius = radius;
this.theta = theta;
}

/**
* Gets the rectangular coordinates of the current Polar
* @param the reference to assign the result
* @returns the updated reference
*/
public toVector2ToRef(ref: Vector2): Vector2{
Vector2.FromPolarToRef(this, ref);
return ref;
}

/**
* Gets the rectangular coordinates of the current Polar
* @returns the rectangular coordinates
*/
public toVector2(): Vector2{
return Vector2.FromPolar(this);
}

/**
* Converts a given Vector2 to its polar coordinates
* @param v the Vector2 to convert
* @param ref the reference to assign the result
* @returns the updated reference
*/
public static FromVector2ToRef(v: Vector2, ref: Polar): Polar{
const theta = Math.sign(v.y) * Math.acos(v.x / v.length());
ref.radius = v.length();
ref.theta = theta;
return ref
}

/**
* Converts a given Vector2 to its polar coordinates
* @param v the Vector2 to convert
* @returns a Polar
*/
public static FromVector2(v: Vector2): Polar{
let polar = new Polar();
return Polar.FromVector2ToRef(v, polar);
}
}

/**
* Class used to store (x,y,z) vector representation
* A Vector3 is the main object used in 3D geometry
Expand Down Expand Up @@ -2953,60 +2895,6 @@ export class Vector3 {
}
}

export class Spherical {
public length: number;
public theta: number;
public phi: number;

constructor(length, theta, phi){
this.length = length;
this.theta = theta;
this.phi = phi;
}

/**
* Assigns the rectangular coordinates of the current Spherical to a Vector3
* @param ref the Vector3 to update
* @returns the updated Vector3
*/
public toVector3ToRef(ref: DeepImmutable<Vector3>): Vector3{
return Vector3.FromSphericalToRef(ref);
}

/**
* Gets a Vector3 from the current spherical coordinates
* @returns the Vector3
*/
public toVector3(): Vector3{
return Vector3.FromSpherical(this);
}

/**
* Assigns the spherical coordinates from a Vector3
* @param vector the vector to convert
* @param ref the Spherical to update
* @returns the updated ref
*/
public static FromVector3ToRef(vector: DeepImmutable<Vector3>, ref: Spherical): Spherical{
let rotation = Vector3.PitchYawRollForDirectionChange(Vector3.Zero(), vector);
ref.radius = vector.length();
ref.theta = rotation.x;
ref.phi = rotation.y;
return ref;
}

/**
* Gets a Spherical from a Vector3
* @param vector defines the vector in (x, y, z) coordinate space
* @returns a new Spherical
*/
public static FromVector3(vector: DeepImmutable<Vector3>): Spherical{
let spherical = new Spherical();
Spherical.FromVector3ToRef(vector, spherical);
return spherical;
}
}

/**
* Vector4 class created for EulerAngle class conversion to Quaternion
*/
Expand Down

0 comments on commit 006be27

Please sign in to comment.