123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657 |
- import { Vector2, Vector3 } from "./math.vector.js";
- /**
- * Class used to store (r, theta) vector representation
- */
- export class Polar {
- /**
- * Creates a new Polar object
- * @param radius the radius of the vector
- * @param theta the angle of the vector
- */
- constructor(radius, theta) {
- this.radius = radius;
- this.theta = theta;
- this.radius = radius;
- this.theta = theta;
- }
- /**
- * Gets the class name
- * @returns the string "Polar"
- */
- getClassName() {
- return "Polar";
- }
- /**
- * Converts the current polar to a string
- * @returns the current polar as a string
- */
- toString() {
- return JSON.stringify(this);
- }
- /**
- * Converts the current polar to an array
- * @returns the current polar as an array
- */
- asArray() {
- return [this.radius, this.theta];
- }
- /**
- * Adds the current Polar and the given Polar and stores the result
- * @param polar the polar to add
- * @param ref the polar to store the result in
- * @returns the updated ref
- */
- addToRef(polar, ref) {
- ref.radius = this.radius + polar.radius;
- ref.theta = this.theta + polar.theta;
- return ref;
- }
- /**
- * Adds the current Polar and the given Polar
- * @param polar the polar to add
- * @returns the sum polar
- */
- add(polar) {
- const ref = new Polar(0, 0);
- this.addToRef(polar, ref);
- return ref;
- }
- /**
- * Adds the given polar to the current polar
- * @param polar the polar to add
- * @returns the current polar
- */
- addInPlace(polar) {
- this.addToRef(polar, this);
- return this;
- }
- /**
- * Adds the provided values to the current polar
- * @param radius the amount to add to the radius
- * @param theta the amount to add to the theta
- * @returns the current polar
- */
- addInPlaceFromFloats(radius, theta) {
- this.radius += radius;
- this.theta += theta;
- return this;
- }
- /**
- * Subtracts the given Polar from the current Polar and stores the result
- * @param polar the polar to subtract
- * @param ref the polar to store the result in
- * @returns the updated ref
- */
- subtractToRef(polar, ref) {
- ref.radius = this.radius - polar.radius;
- ref.theta = this.theta - polar.theta;
- return ref;
- }
- /**
- * Subtracts the given Polar from the current Polar
- * @param polar the polar to subtract
- * @returns the difference polar
- */
- subtract(polar) {
- const ref = new Polar(0, 0);
- this.subtractToRef(polar, ref);
- return ref;
- }
- /**
- * Subtracts the given Polar from the current Polar
- * @param polar the polar to subtract
- * @returns the current polar
- */
- subtractInPlace(polar) {
- this.subtractToRef(polar, this);
- return this;
- }
- /**
- * Subtracts the given floats from the current polar
- * @param radius the amount to subtract from the radius
- * @param theta the amount to subtract from the theta
- * @param ref the polar to store the result in
- * @returns the updated ref
- */
- subtractFromFloatsToRef(radius, theta, ref) {
- ref.radius = this.radius - radius;
- ref.theta = this.theta - theta;
- return ref;
- }
- /**
- * Subtracts the given floats from the current polar
- * @param radius the amount to subtract from the radius
- * @param theta the amount to subtract from the theta
- * @returns the difference polar
- */
- subtractFromFloats(radius, theta) {
- const ref = new Polar(0, 0);
- this.subtractFromFloatsToRef(radius, theta, ref);
- return ref;
- }
- /**
- * Multiplies the given Polar with the current Polar and stores the result
- * @param polar the polar to multiply
- * @param ref the polar to store the result in
- * @returns the updated ref
- */
- multiplyToRef(polar, ref) {
- ref.radius = this.radius * polar.radius;
- ref.theta = this.theta * polar.theta;
- return ref;
- }
- /**
- * Multiplies the given Polar with the current Polar
- * @param polar the polar to multiply
- * @returns the product polar
- */
- multiply(polar) {
- const ref = new Polar(0, 0);
- this.multiplyToRef(polar, ref);
- return ref;
- }
- /**
- * Multiplies the given Polar with the current Polar
- * @param polar the polar to multiply
- * @returns the current polar
- */
- multiplyInPlace(polar) {
- this.multiplyToRef(polar, this);
- return this;
- }
- /**
- * Divides the current Polar by the given Polar and stores the result
- * @param polar the polar to divide
- * @param ref the polar to store the result in
- * @returns the updated ref
- */
- divideToRef(polar, ref) {
- ref.radius = this.radius / polar.radius;
- ref.theta = this.theta / polar.theta;
- return ref;
- }
- /**
- * Divides the current Polar by the given Polar
- * @param polar the polar to divide
- * @returns the quotient polar
- */
- divide(polar) {
- const ref = new Polar(0, 0);
- this.divideToRef(polar, ref);
- return ref;
- }
- /**
- * Divides the current Polar by the given Polar
- * @param polar the polar to divide
- * @returns the current polar
- */
- divideInPlace(polar) {
- this.divideToRef(polar, this);
- return this;
- }
- /**
- * Clones the current polar
- * @returns a clone of the current polar
- */
- clone() {
- return new Polar(this.radius, this.theta);
- }
- /**
- * Copies the source polar into the current polar
- * @param source the polar to copy from
- * @returns the current polar
- */
- copyFrom(source) {
- this.radius = source.radius;
- this.theta = source.theta;
- return this;
- }
- /**
- * Copies the given values into the current polar
- * @param radius the radius to use
- * @param theta the theta to use
- * @returns the current polar
- */
- copyFromFloats(radius, theta) {
- this.radius = radius;
- this.theta = theta;
- return this;
- }
- /**
- * Scales the current polar and stores the result
- * @param scale defines the multiplication factor
- * @param ref where to store the result
- * @returns the updated ref
- */
- scaleToRef(scale, ref) {
- ref.radius = this.radius * scale;
- ref.theta = this.theta * scale;
- return ref;
- }
- /**
- * Scales the current polar and returns a new polar with the scaled coordinates
- * @param scale defines the multiplication factor
- * @returns the scaled polar
- */
- scale(scale) {
- const ref = new Polar(0, 0);
- this.scaleToRef(scale, ref);
- return ref;
- }
- /**
- * Scales the current polar
- * @param scale defines the multiplication factor
- * @returns the current polar
- */
- scaleInPlace(scale) {
- this.scaleToRef(scale, this);
- return this;
- }
- /**
- * Sets the values of the current polar
- * @param radius the new radius
- * @param theta the new theta
- * @returns the current polar
- */
- set(radius, theta) {
- this.radius = radius;
- this.theta = theta;
- return this;
- }
- /**
- * Sets the values of the current polar
- * @param value the new values
- * @returns the current polar
- */
- setAll(value) {
- this.set(value, value);
- return this;
- }
- /**
- * Gets the rectangular coordinates of the current Polar
- * @param ref the reference to assign the result
- * @returns the updated reference
- */
- toVector2ToRef(ref) {
- 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
- */
- toVector2() {
- const ref = new Vector2(0, 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
- */
- static FromVector2ToRef(v, ref) {
- 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
- */
- static FromVector2(v) {
- const polar = new Polar(0, 0);
- Polar.FromVector2ToRef(v, polar);
- return polar;
- }
- /**
- * Converts an array of floats to a polar
- * @param array the array to convert
- * @returns the converted polar
- */
- static FromArray(array) {
- return new Polar(array[0], array[1]);
- }
- }
- /**
- * Class used for (radius, theta, phi) vector representation.
- */
- export class Spherical {
- /**
- * Creates a new Spherical object from the given spherical coordinates
- * @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, theta, phi) {
- this.radius = radius;
- this.theta = theta;
- this.phi = phi;
- this.radius = radius;
- this.theta = theta;
- this.phi = phi;
- }
- /**
- * Gets the class name
- * @returns the string "Spherical"
- */
- getClassName() {
- return "Spherical";
- }
- /**
- * Converts the current spherical to a string
- * @returns the current spherical as a string
- */
- toString() {
- return JSON.stringify(this);
- }
- /**
- * Converts the current spherical to an array
- * @returns the current spherical as an array
- */
- asArray() {
- return [this.radius, this.theta, this.phi];
- }
- /**
- * Adds the current Spherical and the given Spherical and stores the result
- * @param spherical the spherical to add
- * @param ref the spherical to store the result in
- * @returns the updated ref
- */
- addToRef(spherical, ref) {
- ref.radius = this.radius + spherical.radius;
- ref.theta = this.theta + spherical.theta;
- ref.phi = this.phi + spherical.phi;
- return ref;
- }
- /**
- * Adds the current Spherical and the given Spherical
- * @param spherical the spherical to add
- * @returns the sum spherical
- */
- add(spherical) {
- const ref = new Spherical(0, 0, 0);
- this.addToRef(spherical, ref);
- return ref;
- }
- /**
- * Adds the given spherical to the current spherical
- * @param spherical the spherical to add
- * @returns the current spherical
- */
- addInPlace(spherical) {
- this.addToRef(spherical, this);
- return this;
- }
- /**
- * Adds the provided values to the current spherical
- * @param radius the amount to add to the radius
- * @param theta the amount to add to the theta
- * @param phi the amount to add to the phi
- * @returns the current spherical
- */
- addInPlaceFromFloats(radius, theta, phi) {
- this.radius += radius;
- this.theta += theta;
- this.phi += phi;
- return this;
- }
- /**
- * Subtracts the given Spherical from the current Spherical and stores the result
- * @param spherical the spherical to subtract
- * @param ref the spherical to store the result in
- * @returns the updated ref
- */
- subtractToRef(spherical, ref) {
- ref.radius = this.radius - spherical.radius;
- ref.theta = this.theta - spherical.theta;
- ref.phi = this.phi - spherical.phi;
- return ref;
- }
- /**
- * Subtracts the given Spherical from the current Spherical
- * @param spherical the spherical to subtract
- * @returns the difference spherical
- */
- subtract(spherical) {
- const ref = new Spherical(0, 0, 0);
- this.subtractToRef(spherical, ref);
- return ref;
- }
- /**
- * Subtracts the given Spherical from the current Spherical
- * @param spherical the spherical to subtract
- * @returns the current spherical
- */
- subtractInPlace(spherical) {
- this.subtractToRef(spherical, this);
- return this;
- }
- /**
- * Subtracts the given floats from the current spherical
- * @param radius the amount to subtract from the radius
- * @param theta the amount to subtract from the theta
- * @param phi the amount to subtract from the phi
- * @param ref the spherical to store the result in
- * @returns the updated ref
- */
- subtractFromFloatsToRef(radius, theta, phi, ref) {
- ref.radius = this.radius - radius;
- ref.theta = this.theta - theta;
- ref.phi = this.phi - phi;
- return ref;
- }
- /**
- * Subtracts the given floats from the current spherical
- * @param radius the amount to subtract from the radius
- * @param theta the amount to subtract from the theta
- * @param phi the amount to subtract from the phi
- * @returns the difference spherical
- */
- subtractFromFloats(radius, theta, phi) {
- const ref = new Spherical(0, 0, 0);
- this.subtractFromFloatsToRef(radius, theta, phi, ref);
- return ref;
- }
- /**
- * Multiplies the given Spherical with the current Spherical and stores the result
- * @param spherical the spherical to multiply
- * @param ref the spherical to store the result in
- * @returns the updated ref
- */
- multiplyToRef(spherical, ref) {
- ref.radius = this.radius * spherical.radius;
- ref.theta = this.theta * spherical.theta;
- ref.phi = this.phi * spherical.phi;
- return ref;
- }
- /**
- * Multiplies the given Spherical with the current Spherical
- * @param spherical the spherical to multiply
- * @returns the product spherical
- */
- multiply(spherical) {
- const ref = new Spherical(0, 0, 0);
- this.multiplyToRef(spherical, ref);
- return ref;
- }
- /**
- * Multiplies the given Spherical with the current Spherical
- * @param spherical the spherical to multiply
- * @returns the current spherical
- */
- multiplyInPlace(spherical) {
- this.multiplyToRef(spherical, this);
- return this;
- }
- /**
- * Divides the current Spherical by the given Spherical and stores the result
- * @param spherical the spherical to divide
- * @param ref the spherical to store the result in
- * @returns the updated ref
- */
- divideToRef(spherical, ref) {
- ref.radius = this.radius / spherical.radius;
- ref.theta = this.theta / spherical.theta;
- ref.phi = this.phi / spherical.phi;
- return ref;
- }
- /**
- * Divides the current Spherical by the given Spherical
- * @param spherical the spherical to divide
- * @returns the quotient spherical
- */
- divide(spherical) {
- const ref = new Spherical(0, 0, 0);
- this.divideToRef(spherical, ref);
- return ref;
- }
- /**
- * Divides the current Spherical by the given Spherical
- * @param spherical the spherical to divide
- * @returns the current spherical
- */
- divideInPlace(spherical) {
- this.divideToRef(spherical, this);
- return this;
- }
- /**
- * Clones the current spherical
- * @returns a clone of the current spherical
- */
- clone() {
- return new Spherical(this.radius, this.theta, this.phi);
- }
- /**
- * Copies the source spherical into the current spherical
- * @param source the spherical to copy from
- * @returns the current spherical
- */
- copyFrom(source) {
- this.radius = source.radius;
- this.theta = source.theta;
- this.phi = source.phi;
- return this;
- }
- /**
- * Copies the given values into the current spherical
- * @param radius the radius to use
- * @param theta the theta to use
- * @param phi the phi to use
- * @returns the current spherical
- */
- copyFromFloats(radius, theta, phi) {
- this.radius = radius;
- this.theta = theta;
- this.phi = phi;
- return this;
- }
- /**
- * Scales the current spherical and stores the result
- * @param scale defines the multiplication factor
- * @param ref where to store the result
- * @returns the updated ref
- */
- scaleToRef(scale, ref) {
- ref.radius = this.radius * scale;
- ref.theta = this.theta * scale;
- ref.phi = this.phi * scale;
- return ref;
- }
- /**
- * Scales the current spherical and returns a new spherical with the scaled coordinates
- * @param scale defines the multiplication factor
- * @returns the scaled spherical
- */
- scale(scale) {
- const ref = new Spherical(0, 0, 0);
- this.scaleToRef(scale, ref);
- return ref;
- }
- /**
- * Scales the current spherical
- * @param scale defines the multiplication factor
- * @returns the current spherical
- */
- scaleInPlace(scale) {
- this.scaleToRef(scale, this);
- return this;
- }
- /**
- * Sets the values of the current spherical
- * @param radius the new radius
- * @param theta the new theta
- * @param phi the new phi
- * @returns the current spherical
- */
- set(radius, theta, phi) {
- this.radius = radius;
- this.theta = theta;
- this.phi = phi;
- return this;
- }
- /**
- * Sets the values of the current spherical
- * @param value the new values
- * @returns the current spherical
- */
- setAll(value) {
- this.set(value, value, value);
- return this;
- }
- /**
- * Assigns the rectangular coordinates of the current Spherical to a Vector3
- * @param ref the Vector3 to update
- * @returns the updated Vector3
- */
- toVector3ToRef(ref) {
- 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 (x, y,z) form of the current Spherical
- */
- toVector3() {
- const ref = new Vector3(0, 0, 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
- */
- static FromVector3ToRef(vector, ref) {
- 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
- */
- static FromVector3(vector) {
- const spherical = new Spherical(0, 0, 0);
- Spherical.FromVector3ToRef(vector, spherical);
- return spherical;
- }
- /**
- * Converts an array of floats to a spherical
- * @param array the array to convert
- * @returns the converted spherical
- */
- static FromArray(array) {
- return new Spherical(array[0], array[1], array[2]);
- }
- }
- //# sourceMappingURL=math.polar.js.map
|