Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add polar and spherical coordinate system support #12942

Merged
merged 34 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
a53cbca
Add polar coordinate conversions to Vector2 and Vector3
james-pre Sep 2, 2022
72610af
Fixed Vector3.toPolar to return length
james-pre Sep 2, 2022
a461644
Merge pull request #1 from BabylonJS/master
james-pre Sep 3, 2022
119dc22
Added seperate class for Polar and Spherical coordinate systems
james-pre Sep 4, 2022
9826cd0
Fixed pascal case, terminology, and theta issue
james-pre Sep 4, 2022
fb54ef6
Added toRef for polar and spherical related functions.
james-pre Sep 4, 2022
55372dd
Merge pull request #2 from BabylonJS/master
james-pre Sep 7, 2022
226b012
Upates
james-pre Sep 7, 2022
006be27
Moved Spherical and Polar to math.polar.ts
james-pre Sep 7, 2022
e9e5c8f
Merge pull request #3 from BabylonJS/master
james-pre Sep 7, 2022
31dbcbf
Removed FromPolar/FromSpherical
james-pre Sep 7, 2022
0ae0d37
Whitespace
james-pre Sep 7, 2022
b69853f
Change Spherical length to radius, fixed PitchYawRollForDirectionChan…
james-pre Sep 8, 2022
239a961
Removed PitchYawRollForDirectionChangeToRef offset (unneeded)
james-pre Sep 8, 2022
0e19159
Fixed variable order
james-pre Sep 8, 2022
8f1dcd9
Fixed theta/phi equations
james-pre Sep 8, 2022
18bccec
Removed need for PitchYawRollForDirectionChange in math.polar.ts
james-pre Sep 8, 2022
932d6a8
Added Spherical typedoc comment
james-pre Sep 8, 2022
ae157ab
Merge pull request #4 from BabylonJS/master
james-pre Sep 8, 2022
62f1539
atan -> atan2
james-pre Sep 8, 2022
9e3885d
Merge pull request #5 from BabylonJS/master
james-pre Sep 8, 2022
aa43fb4
Remove Vector.GetAngleBetweenVectors, changed Spherical.FromVector3To…
james-pre Sep 9, 2022
c1f6209
Merge pull request #6 from BabylonJS/master
james-pre Sep 9, 2022
7de4f7a
Fixed Spherical.FromVector3ToRef
james-pre Sep 9, 2022
a520e90
PitchYawRollForDirectionChangeToRef
james-pre Sep 9, 2022
16a0155
atan2 for both theta and phi
james-pre Sep 12, 2022
435ec60
Removed uneeded line
james-pre Sep 12, 2022
382a50c
Fixed toVector3toRef, Removed pitchYawRoll
james-pre Sep 13, 2022
0119e2c
Fixed issues Sebavan pointed out
james-pre Sep 13, 2022
a1e768f
Formatting
james-pre Sep 13, 2022
908bc30
Formatting (spaces)
james-pre Sep 13, 2022
9d9d81a
Formatting (let -> const)
james-pre Sep 14, 2022
5b33ab7
Formatting (literally 1 extra line at the end)
james-pre Sep 14, 2022
22c429d
The extra line, again?
james-pre Sep 14, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 133 additions & 0 deletions packages/dev/core/src/Maths/math.polar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/* eslint-disable @typescript-eslint/naming-convention */
import type { DeepImmutable } from "../types";
import type { Vector2, Vector3 } from "./math.vector";
import { TmpVectors } 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 ref the reference to assign the result
* @returns the updated reference
*/
public toVector2ToRef(ref: Vector2): Vector2 {
const x = this.radius * Math.cos(this.theta);
const y = this.radius * Math.sin(this.theta);
ref.set(x, y);
return ref;
}

/**
* Gets the rectangular coordinates of the current Polar
* @returns the rectangular coordinates
*/
public toVector2(): Vector2 {
const ref = TmpVectors.Vector2[0];
return this.toVector2ToRef(ref);
}

/**
* 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 {
const polar = new Polar(0, 0);
Polar.FromVector2ToRef(v, polar);
return polar;
}
}

/**
* Class used for (radius, theta, phi) vector representation.
*/
export class Spherical {
public radius: number;
public theta: number;
public phi: number;

/**
* @param radius spherical radius
* @param theta angle from positive y axis to radial line from 0 to PI (vertical)
* @param phi angle from positive x axis measured anticlockwise from -PI to PI (horizontal)
*/
constructor(radius: number, theta: number, phi: number) {
this.radius = radius;
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 {
const x = this.radius * Math.sin(this.theta) * Math.cos(this.phi);
const y = this.radius * Math.cos(this.theta);
const z = this.radius * Math.sin(this.theta) * Math.sin(this.phi);
ref.set(x, y, z);
return ref;
}

/**
* Gets a Vector3 from the current spherical coordinates
* @returns the Vector3
*/
public toVector3(): Vector3 {
const ref = TmpVectors.Vector3[0];
return this.toVector3ToRef(ref);
}

/**
* 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 {
ref.radius = vector.length();
ref.theta = Math.acos(vector.y / ref.radius);
ref.phi = Math.atan2(vector.z, vector.x);
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 {
const spherical = new Spherical(0, 0, 0);
Spherical.FromVector3ToRef(vector, spherical);
return spherical;
}
}
4 changes: 2 additions & 2 deletions packages/dev/core/src/Maths/math.vector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1664,8 +1664,8 @@ export class Vector3 {
/**
* Get angle between two vectors
* Example Playground https://playground.babylonjs.com/#R1F8YU#86
* @param vector0 angle between vector0 and vector1
* @param vector1 angle between vector0 and vector1
* @param vector0 the starting point
* @param vector1 the ending point
* @param normal direction of the normal
* @return the angle between vector0 and vector1
*/
Expand Down