math.polar.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. import { Vector2, Vector3 } from "./math.vector.js";
  2. /**
  3. * Class used to store (r, theta) vector representation
  4. */
  5. export class Polar {
  6. /**
  7. * Creates a new Polar object
  8. * @param radius the radius of the vector
  9. * @param theta the angle of the vector
  10. */
  11. constructor(radius, theta) {
  12. this.radius = radius;
  13. this.theta = theta;
  14. this.radius = radius;
  15. this.theta = theta;
  16. }
  17. /**
  18. * Gets the class name
  19. * @returns the string "Polar"
  20. */
  21. getClassName() {
  22. return "Polar";
  23. }
  24. /**
  25. * Converts the current polar to a string
  26. * @returns the current polar as a string
  27. */
  28. toString() {
  29. return JSON.stringify(this);
  30. }
  31. /**
  32. * Converts the current polar to an array
  33. * @returns the current polar as an array
  34. */
  35. asArray() {
  36. return [this.radius, this.theta];
  37. }
  38. /**
  39. * Adds the current Polar and the given Polar and stores the result
  40. * @param polar the polar to add
  41. * @param ref the polar to store the result in
  42. * @returns the updated ref
  43. */
  44. addToRef(polar, ref) {
  45. ref.radius = this.radius + polar.radius;
  46. ref.theta = this.theta + polar.theta;
  47. return ref;
  48. }
  49. /**
  50. * Adds the current Polar and the given Polar
  51. * @param polar the polar to add
  52. * @returns the sum polar
  53. */
  54. add(polar) {
  55. const ref = new Polar(0, 0);
  56. this.addToRef(polar, ref);
  57. return ref;
  58. }
  59. /**
  60. * Adds the given polar to the current polar
  61. * @param polar the polar to add
  62. * @returns the current polar
  63. */
  64. addInPlace(polar) {
  65. this.addToRef(polar, this);
  66. return this;
  67. }
  68. /**
  69. * Adds the provided values to the current polar
  70. * @param radius the amount to add to the radius
  71. * @param theta the amount to add to the theta
  72. * @returns the current polar
  73. */
  74. addInPlaceFromFloats(radius, theta) {
  75. this.radius += radius;
  76. this.theta += theta;
  77. return this;
  78. }
  79. /**
  80. * Subtracts the given Polar from the current Polar and stores the result
  81. * @param polar the polar to subtract
  82. * @param ref the polar to store the result in
  83. * @returns the updated ref
  84. */
  85. subtractToRef(polar, ref) {
  86. ref.radius = this.radius - polar.radius;
  87. ref.theta = this.theta - polar.theta;
  88. return ref;
  89. }
  90. /**
  91. * Subtracts the given Polar from the current Polar
  92. * @param polar the polar to subtract
  93. * @returns the difference polar
  94. */
  95. subtract(polar) {
  96. const ref = new Polar(0, 0);
  97. this.subtractToRef(polar, ref);
  98. return ref;
  99. }
  100. /**
  101. * Subtracts the given Polar from the current Polar
  102. * @param polar the polar to subtract
  103. * @returns the current polar
  104. */
  105. subtractInPlace(polar) {
  106. this.subtractToRef(polar, this);
  107. return this;
  108. }
  109. /**
  110. * Subtracts the given floats from the current polar
  111. * @param radius the amount to subtract from the radius
  112. * @param theta the amount to subtract from the theta
  113. * @param ref the polar to store the result in
  114. * @returns the updated ref
  115. */
  116. subtractFromFloatsToRef(radius, theta, ref) {
  117. ref.radius = this.radius - radius;
  118. ref.theta = this.theta - theta;
  119. return ref;
  120. }
  121. /**
  122. * Subtracts the given floats from the current polar
  123. * @param radius the amount to subtract from the radius
  124. * @param theta the amount to subtract from the theta
  125. * @returns the difference polar
  126. */
  127. subtractFromFloats(radius, theta) {
  128. const ref = new Polar(0, 0);
  129. this.subtractFromFloatsToRef(radius, theta, ref);
  130. return ref;
  131. }
  132. /**
  133. * Multiplies the given Polar with the current Polar and stores the result
  134. * @param polar the polar to multiply
  135. * @param ref the polar to store the result in
  136. * @returns the updated ref
  137. */
  138. multiplyToRef(polar, ref) {
  139. ref.radius = this.radius * polar.radius;
  140. ref.theta = this.theta * polar.theta;
  141. return ref;
  142. }
  143. /**
  144. * Multiplies the given Polar with the current Polar
  145. * @param polar the polar to multiply
  146. * @returns the product polar
  147. */
  148. multiply(polar) {
  149. const ref = new Polar(0, 0);
  150. this.multiplyToRef(polar, ref);
  151. return ref;
  152. }
  153. /**
  154. * Multiplies the given Polar with the current Polar
  155. * @param polar the polar to multiply
  156. * @returns the current polar
  157. */
  158. multiplyInPlace(polar) {
  159. this.multiplyToRef(polar, this);
  160. return this;
  161. }
  162. /**
  163. * Divides the current Polar by the given Polar and stores the result
  164. * @param polar the polar to divide
  165. * @param ref the polar to store the result in
  166. * @returns the updated ref
  167. */
  168. divideToRef(polar, ref) {
  169. ref.radius = this.radius / polar.radius;
  170. ref.theta = this.theta / polar.theta;
  171. return ref;
  172. }
  173. /**
  174. * Divides the current Polar by the given Polar
  175. * @param polar the polar to divide
  176. * @returns the quotient polar
  177. */
  178. divide(polar) {
  179. const ref = new Polar(0, 0);
  180. this.divideToRef(polar, ref);
  181. return ref;
  182. }
  183. /**
  184. * Divides the current Polar by the given Polar
  185. * @param polar the polar to divide
  186. * @returns the current polar
  187. */
  188. divideInPlace(polar) {
  189. this.divideToRef(polar, this);
  190. return this;
  191. }
  192. /**
  193. * Clones the current polar
  194. * @returns a clone of the current polar
  195. */
  196. clone() {
  197. return new Polar(this.radius, this.theta);
  198. }
  199. /**
  200. * Copies the source polar into the current polar
  201. * @param source the polar to copy from
  202. * @returns the current polar
  203. */
  204. copyFrom(source) {
  205. this.radius = source.radius;
  206. this.theta = source.theta;
  207. return this;
  208. }
  209. /**
  210. * Copies the given values into the current polar
  211. * @param radius the radius to use
  212. * @param theta the theta to use
  213. * @returns the current polar
  214. */
  215. copyFromFloats(radius, theta) {
  216. this.radius = radius;
  217. this.theta = theta;
  218. return this;
  219. }
  220. /**
  221. * Scales the current polar and stores the result
  222. * @param scale defines the multiplication factor
  223. * @param ref where to store the result
  224. * @returns the updated ref
  225. */
  226. scaleToRef(scale, ref) {
  227. ref.radius = this.radius * scale;
  228. ref.theta = this.theta * scale;
  229. return ref;
  230. }
  231. /**
  232. * Scales the current polar and returns a new polar with the scaled coordinates
  233. * @param scale defines the multiplication factor
  234. * @returns the scaled polar
  235. */
  236. scale(scale) {
  237. const ref = new Polar(0, 0);
  238. this.scaleToRef(scale, ref);
  239. return ref;
  240. }
  241. /**
  242. * Scales the current polar
  243. * @param scale defines the multiplication factor
  244. * @returns the current polar
  245. */
  246. scaleInPlace(scale) {
  247. this.scaleToRef(scale, this);
  248. return this;
  249. }
  250. /**
  251. * Sets the values of the current polar
  252. * @param radius the new radius
  253. * @param theta the new theta
  254. * @returns the current polar
  255. */
  256. set(radius, theta) {
  257. this.radius = radius;
  258. this.theta = theta;
  259. return this;
  260. }
  261. /**
  262. * Sets the values of the current polar
  263. * @param value the new values
  264. * @returns the current polar
  265. */
  266. setAll(value) {
  267. this.set(value, value);
  268. return this;
  269. }
  270. /**
  271. * Gets the rectangular coordinates of the current Polar
  272. * @param ref the reference to assign the result
  273. * @returns the updated reference
  274. */
  275. toVector2ToRef(ref) {
  276. const x = this.radius * Math.cos(this.theta);
  277. const y = this.radius * Math.sin(this.theta);
  278. ref.set(x, y);
  279. return ref;
  280. }
  281. /**
  282. * Gets the rectangular coordinates of the current Polar
  283. * @returns the rectangular coordinates
  284. */
  285. toVector2() {
  286. const ref = new Vector2(0, 0);
  287. return this.toVector2ToRef(ref);
  288. }
  289. /**
  290. * Converts a given Vector2 to its polar coordinates
  291. * @param v the Vector2 to convert
  292. * @param ref the reference to assign the result
  293. * @returns the updated reference
  294. */
  295. static FromVector2ToRef(v, ref) {
  296. const theta = Math.sign(v.y) * Math.acos(v.x / v.length());
  297. ref.radius = v.length();
  298. ref.theta = theta;
  299. return ref;
  300. }
  301. /**
  302. * Converts a given Vector2 to its polar coordinates
  303. * @param v the Vector2 to convert
  304. * @returns a Polar
  305. */
  306. static FromVector2(v) {
  307. const polar = new Polar(0, 0);
  308. Polar.FromVector2ToRef(v, polar);
  309. return polar;
  310. }
  311. /**
  312. * Converts an array of floats to a polar
  313. * @param array the array to convert
  314. * @returns the converted polar
  315. */
  316. static FromArray(array) {
  317. return new Polar(array[0], array[1]);
  318. }
  319. }
  320. /**
  321. * Class used for (radius, theta, phi) vector representation.
  322. */
  323. export class Spherical {
  324. /**
  325. * Creates a new Spherical object from the given spherical coordinates
  326. * @param radius spherical radius
  327. * @param theta angle from positive y axis to radial line from 0 to PI (vertical)
  328. * @param phi angle from positive x axis measured anticlockwise from -PI to PI (horizontal)
  329. */
  330. constructor(radius, theta, phi) {
  331. this.radius = radius;
  332. this.theta = theta;
  333. this.phi = phi;
  334. this.radius = radius;
  335. this.theta = theta;
  336. this.phi = phi;
  337. }
  338. /**
  339. * Gets the class name
  340. * @returns the string "Spherical"
  341. */
  342. getClassName() {
  343. return "Spherical";
  344. }
  345. /**
  346. * Converts the current spherical to a string
  347. * @returns the current spherical as a string
  348. */
  349. toString() {
  350. return JSON.stringify(this);
  351. }
  352. /**
  353. * Converts the current spherical to an array
  354. * @returns the current spherical as an array
  355. */
  356. asArray() {
  357. return [this.radius, this.theta, this.phi];
  358. }
  359. /**
  360. * Adds the current Spherical and the given Spherical and stores the result
  361. * @param spherical the spherical to add
  362. * @param ref the spherical to store the result in
  363. * @returns the updated ref
  364. */
  365. addToRef(spherical, ref) {
  366. ref.radius = this.radius + spherical.radius;
  367. ref.theta = this.theta + spherical.theta;
  368. ref.phi = this.phi + spherical.phi;
  369. return ref;
  370. }
  371. /**
  372. * Adds the current Spherical and the given Spherical
  373. * @param spherical the spherical to add
  374. * @returns the sum spherical
  375. */
  376. add(spherical) {
  377. const ref = new Spherical(0, 0, 0);
  378. this.addToRef(spherical, ref);
  379. return ref;
  380. }
  381. /**
  382. * Adds the given spherical to the current spherical
  383. * @param spherical the spherical to add
  384. * @returns the current spherical
  385. */
  386. addInPlace(spherical) {
  387. this.addToRef(spherical, this);
  388. return this;
  389. }
  390. /**
  391. * Adds the provided values to the current spherical
  392. * @param radius the amount to add to the radius
  393. * @param theta the amount to add to the theta
  394. * @param phi the amount to add to the phi
  395. * @returns the current spherical
  396. */
  397. addInPlaceFromFloats(radius, theta, phi) {
  398. this.radius += radius;
  399. this.theta += theta;
  400. this.phi += phi;
  401. return this;
  402. }
  403. /**
  404. * Subtracts the given Spherical from the current Spherical and stores the result
  405. * @param spherical the spherical to subtract
  406. * @param ref the spherical to store the result in
  407. * @returns the updated ref
  408. */
  409. subtractToRef(spherical, ref) {
  410. ref.radius = this.radius - spherical.radius;
  411. ref.theta = this.theta - spherical.theta;
  412. ref.phi = this.phi - spherical.phi;
  413. return ref;
  414. }
  415. /**
  416. * Subtracts the given Spherical from the current Spherical
  417. * @param spherical the spherical to subtract
  418. * @returns the difference spherical
  419. */
  420. subtract(spherical) {
  421. const ref = new Spherical(0, 0, 0);
  422. this.subtractToRef(spherical, ref);
  423. return ref;
  424. }
  425. /**
  426. * Subtracts the given Spherical from the current Spherical
  427. * @param spherical the spherical to subtract
  428. * @returns the current spherical
  429. */
  430. subtractInPlace(spherical) {
  431. this.subtractToRef(spherical, this);
  432. return this;
  433. }
  434. /**
  435. * Subtracts the given floats from the current spherical
  436. * @param radius the amount to subtract from the radius
  437. * @param theta the amount to subtract from the theta
  438. * @param phi the amount to subtract from the phi
  439. * @param ref the spherical to store the result in
  440. * @returns the updated ref
  441. */
  442. subtractFromFloatsToRef(radius, theta, phi, ref) {
  443. ref.radius = this.radius - radius;
  444. ref.theta = this.theta - theta;
  445. ref.phi = this.phi - phi;
  446. return ref;
  447. }
  448. /**
  449. * Subtracts the given floats from the current spherical
  450. * @param radius the amount to subtract from the radius
  451. * @param theta the amount to subtract from the theta
  452. * @param phi the amount to subtract from the phi
  453. * @returns the difference spherical
  454. */
  455. subtractFromFloats(radius, theta, phi) {
  456. const ref = new Spherical(0, 0, 0);
  457. this.subtractFromFloatsToRef(radius, theta, phi, ref);
  458. return ref;
  459. }
  460. /**
  461. * Multiplies the given Spherical with the current Spherical and stores the result
  462. * @param spherical the spherical to multiply
  463. * @param ref the spherical to store the result in
  464. * @returns the updated ref
  465. */
  466. multiplyToRef(spherical, ref) {
  467. ref.radius = this.radius * spherical.radius;
  468. ref.theta = this.theta * spherical.theta;
  469. ref.phi = this.phi * spherical.phi;
  470. return ref;
  471. }
  472. /**
  473. * Multiplies the given Spherical with the current Spherical
  474. * @param spherical the spherical to multiply
  475. * @returns the product spherical
  476. */
  477. multiply(spherical) {
  478. const ref = new Spherical(0, 0, 0);
  479. this.multiplyToRef(spherical, ref);
  480. return ref;
  481. }
  482. /**
  483. * Multiplies the given Spherical with the current Spherical
  484. * @param spherical the spherical to multiply
  485. * @returns the current spherical
  486. */
  487. multiplyInPlace(spherical) {
  488. this.multiplyToRef(spherical, this);
  489. return this;
  490. }
  491. /**
  492. * Divides the current Spherical by the given Spherical and stores the result
  493. * @param spherical the spherical to divide
  494. * @param ref the spherical to store the result in
  495. * @returns the updated ref
  496. */
  497. divideToRef(spherical, ref) {
  498. ref.radius = this.radius / spherical.radius;
  499. ref.theta = this.theta / spherical.theta;
  500. ref.phi = this.phi / spherical.phi;
  501. return ref;
  502. }
  503. /**
  504. * Divides the current Spherical by the given Spherical
  505. * @param spherical the spherical to divide
  506. * @returns the quotient spherical
  507. */
  508. divide(spherical) {
  509. const ref = new Spherical(0, 0, 0);
  510. this.divideToRef(spherical, ref);
  511. return ref;
  512. }
  513. /**
  514. * Divides the current Spherical by the given Spherical
  515. * @param spherical the spherical to divide
  516. * @returns the current spherical
  517. */
  518. divideInPlace(spherical) {
  519. this.divideToRef(spherical, this);
  520. return this;
  521. }
  522. /**
  523. * Clones the current spherical
  524. * @returns a clone of the current spherical
  525. */
  526. clone() {
  527. return new Spherical(this.radius, this.theta, this.phi);
  528. }
  529. /**
  530. * Copies the source spherical into the current spherical
  531. * @param source the spherical to copy from
  532. * @returns the current spherical
  533. */
  534. copyFrom(source) {
  535. this.radius = source.radius;
  536. this.theta = source.theta;
  537. this.phi = source.phi;
  538. return this;
  539. }
  540. /**
  541. * Copies the given values into the current spherical
  542. * @param radius the radius to use
  543. * @param theta the theta to use
  544. * @param phi the phi to use
  545. * @returns the current spherical
  546. */
  547. copyFromFloats(radius, theta, phi) {
  548. this.radius = radius;
  549. this.theta = theta;
  550. this.phi = phi;
  551. return this;
  552. }
  553. /**
  554. * Scales the current spherical and stores the result
  555. * @param scale defines the multiplication factor
  556. * @param ref where to store the result
  557. * @returns the updated ref
  558. */
  559. scaleToRef(scale, ref) {
  560. ref.radius = this.radius * scale;
  561. ref.theta = this.theta * scale;
  562. ref.phi = this.phi * scale;
  563. return ref;
  564. }
  565. /**
  566. * Scales the current spherical and returns a new spherical with the scaled coordinates
  567. * @param scale defines the multiplication factor
  568. * @returns the scaled spherical
  569. */
  570. scale(scale) {
  571. const ref = new Spherical(0, 0, 0);
  572. this.scaleToRef(scale, ref);
  573. return ref;
  574. }
  575. /**
  576. * Scales the current spherical
  577. * @param scale defines the multiplication factor
  578. * @returns the current spherical
  579. */
  580. scaleInPlace(scale) {
  581. this.scaleToRef(scale, this);
  582. return this;
  583. }
  584. /**
  585. * Sets the values of the current spherical
  586. * @param radius the new radius
  587. * @param theta the new theta
  588. * @param phi the new phi
  589. * @returns the current spherical
  590. */
  591. set(radius, theta, phi) {
  592. this.radius = radius;
  593. this.theta = theta;
  594. this.phi = phi;
  595. return this;
  596. }
  597. /**
  598. * Sets the values of the current spherical
  599. * @param value the new values
  600. * @returns the current spherical
  601. */
  602. setAll(value) {
  603. this.set(value, value, value);
  604. return this;
  605. }
  606. /**
  607. * Assigns the rectangular coordinates of the current Spherical to a Vector3
  608. * @param ref the Vector3 to update
  609. * @returns the updated Vector3
  610. */
  611. toVector3ToRef(ref) {
  612. const x = this.radius * Math.sin(this.theta) * Math.cos(this.phi);
  613. const y = this.radius * Math.cos(this.theta);
  614. const z = this.radius * Math.sin(this.theta) * Math.sin(this.phi);
  615. ref.set(x, y, z);
  616. return ref;
  617. }
  618. /**
  619. * Gets a Vector3 from the current spherical coordinates
  620. * @returns the (x, y,z) form of the current Spherical
  621. */
  622. toVector3() {
  623. const ref = new Vector3(0, 0, 0);
  624. return this.toVector3ToRef(ref);
  625. }
  626. /**
  627. * Assigns the spherical coordinates from a Vector3
  628. * @param vector the vector to convert
  629. * @param ref the Spherical to update
  630. * @returns the updated ref
  631. */
  632. static FromVector3ToRef(vector, ref) {
  633. ref.radius = vector.length();
  634. ref.theta = Math.acos(vector.y / ref.radius);
  635. ref.phi = Math.atan2(vector.z, vector.x);
  636. return ref;
  637. }
  638. /**
  639. * Gets a Spherical from a Vector3
  640. * @param vector defines the vector in (x, y, z) coordinate space
  641. * @returns a new Spherical
  642. */
  643. static FromVector3(vector) {
  644. const spherical = new Spherical(0, 0, 0);
  645. Spherical.FromVector3ToRef(vector, spherical);
  646. return spherical;
  647. }
  648. /**
  649. * Converts an array of floats to a spherical
  650. * @param array the array to convert
  651. * @returns the converted spherical
  652. */
  653. static FromArray(array) {
  654. return new Spherical(array[0], array[1], array[2]);
  655. }
  656. }
  657. //# sourceMappingURL=math.polar.js.map