1 | import {asin, atan2, cos, degrees, epsilon2, radians, sin, sqrt} from "./math.js";
|
2 |
|
3 | export function spherical(cartesian) {
|
4 | return [atan2(cartesian[1], cartesian[0]), asin(cartesian[2])];
|
5 | }
|
6 |
|
7 | export function sphericalDegrees(cartesian) {
|
8 | var c = spherical(cartesian);
|
9 | return [c[0] * degrees, c[1] * degrees];
|
10 | }
|
11 |
|
12 | export function cartesian(spherical) {
|
13 | var lambda = spherical[0], phi = spherical[1], cosPhi = cos(phi);
|
14 | return [cosPhi * cos(lambda), cosPhi * sin(lambda), sin(phi)];
|
15 | }
|
16 |
|
17 | export function cartesianDegrees(spherical) {
|
18 | return cartesian([spherical[0] * radians, spherical[1] * radians]);
|
19 | }
|
20 |
|
21 | export function cartesianDot(a, b) {
|
22 | return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
|
23 | }
|
24 |
|
25 | export function cartesianCross(a, b) {
|
26 | return [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];
|
27 | }
|
28 |
|
29 |
|
30 | export function cartesianAddInPlace(a, b) {
|
31 | a[0] += b[0], a[1] += b[1], a[2] += b[2];
|
32 | }
|
33 |
|
34 | export function cartesianScale(vector, k) {
|
35 | return [vector[0] * k, vector[1] * k, vector[2] * k];
|
36 | }
|
37 |
|
38 |
|
39 | export function cartesianNormalizeInPlace(d) {
|
40 | var l = sqrt(d[0] * d[0] + d[1] * d[1] + d[2] * d[2]);
|
41 | d[0] /= l, d[1] /= l, d[2] /= l;
|
42 | }
|
43 |
|
44 | export function cartesianEqual(a, b) {
|
45 | var dx = b[0] - a[0],
|
46 | dy = b[1] - a[1],
|
47 | dz = b[2] - a[2];
|
48 | return dx * dx + dy * dy + dz * dz < epsilon2 * epsilon2;
|
49 | }
|