@itowns/geographic 2.44.2 → 2.44.3-next.42

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/Ellipsoid.js DELETED
@@ -1,186 +0,0 @@
1
- import * as THREE from 'three';
2
- import proj4 from 'proj4';
3
- import Coordinates from "./Coordinates.js";
4
-
5
- /**
6
- * Length of the semi-axes of the WGS84 ellipsoid.
7
- * @internal
8
- */
9
- export const ellipsoidSizes = new THREE.Vector3(proj4.WGS84.a, proj4.WGS84.a, proj4.WGS84.b);
10
- const normal = new THREE.Vector3();
11
- class Ellipsoid {
12
- /**
13
- * Length of the semi-axes of the ellipsoid.
14
- */
15
-
16
- /**
17
- * Eccentricity of the ellipsoid.
18
- */
19
-
20
- /**
21
- * @param size - Length of the semi-axes of the ellipsoid. Defaults to those
22
- * defined by the WGS84 ellipsoid.
23
- */
24
- constructor() {
25
- let size = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ellipsoidSizes;
26
- this.size = new THREE.Vector3();
27
- this._radiiSquared = new THREE.Vector3();
28
- this._invRadiiSquared = new THREE.Vector3();
29
- this.eccentricity = 0;
30
- this.setSize(size);
31
- }
32
-
33
- /**
34
- * Computes the normal vector to an ellipsoid at the given cartesian
35
- * coordinate `(x, y, z)`.
36
- *
37
- * @param cartesian - The given cartesian coordinate.
38
- * @param target - An object to store this vector to. If this is not
39
- * specified, a new vector will be created.
40
- */
41
- geodeticSurfaceNormal(cartesian) {
42
- let target = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new THREE.Vector3();
43
- return cartesian.toVector3(target).multiply(this._invRadiiSquared).normalize();
44
- }
45
-
46
- /**
47
- * Computes the normal vector to an ellipsoid at the given geographic
48
- * coordinate `(longitude, latitude, altitude)`.
49
- *
50
- * @param coordCarto - The given geographic coordinate.
51
- * @param target - An object to store this vector to. If this is not
52
- * specified, a new vector will be created.
53
- */
54
- geodeticSurfaceNormalCartographic(coordCarto) {
55
- let target = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new THREE.Vector3();
56
- const longitude = THREE.MathUtils.degToRad(coordCarto.longitude);
57
- const latitude = THREE.MathUtils.degToRad(coordCarto.latitude);
58
- const cosLatitude = Math.cos(latitude);
59
- return target.set(cosLatitude * Math.cos(longitude), cosLatitude * Math.sin(longitude), Math.sin(latitude));
60
- }
61
-
62
- /**
63
- * Sets the length of the semi-axes of this ellipsoid from a 3-dimensional
64
- * vector-like object. The object shall have both `x`, `y` and `z`
65
- * properties.
66
- *
67
- * @param size - The source vector.
68
- */
69
- setSize(size) {
70
- this.size.set(size.x, size.y, size.z);
71
- this._radiiSquared.multiplyVectors(size, size);
72
- this._invRadiiSquared.x = size.x === 0 ? 0 : 1 / this._radiiSquared.x;
73
- this._invRadiiSquared.y = size.y === 0 ? 0 : 1 / this._radiiSquared.y;
74
- this._invRadiiSquared.z = size.z === 0 ? 0 : 1 / this._radiiSquared.z;
75
- this.eccentricity = Math.sqrt(this._radiiSquared.x - this._radiiSquared.z) / this.size.x;
76
- return this;
77
- }
78
- cartographicToCartesian(coordCarto) {
79
- let target = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new THREE.Vector3();
80
- normal.copy(coordCarto.geodesicNormal);
81
- target.multiplyVectors(this._radiiSquared, normal);
82
- const gamma = Math.sqrt(normal.dot(target));
83
- target.divideScalar(gamma);
84
- normal.multiplyScalar(coordCarto.altitude);
85
- return target.add(normal);
86
- }
87
-
88
- /**
89
- * Convert cartesian coordinates to geographic according to the current
90
- * ellipsoid of revolution.
91
- * @param position - The coordinate to convert
92
- * @param target - coordinate to copy result
93
- * @returns an object describing the coordinates on the reference ellipsoid,
94
- * angles are in degree
95
- */
96
- cartesianToCartographic(position) {
97
- let target = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new Coordinates('EPSG:4326', 0, 0, 0);
98
- // for details, see for example http://www.linz.govt.nz/data/geodetic-system/coordinate-conversion/geodetic-datum-conversions/equations-used-datum
99
- // TODO the following is only valable for oblate ellipsoid of
100
- // revolution. do we want to support triaxial ellipsoid?
101
- const R = Math.sqrt(position.x * position.x + position.y * position.y + position.z * position.z);
102
- const a = this.size.x; // x
103
- const b = this.size.z; // z
104
- const e = Math.abs((a * a - b * b) / (a * a));
105
- const f = 1 - Math.sqrt(1 - e);
106
- const rsqXY = Math.sqrt(position.x * position.x + position.y * position.y);
107
- const theta = Math.atan2(position.y, position.x);
108
- const nu = Math.atan(position.z / rsqXY * (1 - f + e * a / R));
109
- const sinu = Math.sin(nu);
110
- const cosu = Math.cos(nu);
111
- const phi = Math.atan((position.z * (1 - f) + e * a * sinu * sinu * sinu) / ((1 - f) * (rsqXY - e * a * cosu * cosu * cosu)));
112
- const h = rsqXY * Math.cos(phi) + position.z * Math.sin(phi) - a * Math.sqrt(1 - e * Math.sin(phi) * Math.sin(phi));
113
- return target.setFromValues(THREE.MathUtils.radToDeg(theta), THREE.MathUtils.radToDeg(phi), h);
114
- }
115
- cartographicToCartesianArray(coordCartoArray) {
116
- const cartesianArray = [];
117
- for (let i = 0; i < coordCartoArray.length; i++) {
118
- cartesianArray.push(this.cartographicToCartesian(coordCartoArray[i]));
119
- }
120
- return cartesianArray;
121
- }
122
- intersection(ray) {
123
- const EPSILON = 0.0001;
124
- const O_C = ray.origin;
125
- const dir = ray.direction;
126
- // normalizeVector( dir );
127
-
128
- const a = dir.x * dir.x * this._invRadiiSquared.x + dir.y * dir.y * this._invRadiiSquared.y + dir.z * dir.z * this._invRadiiSquared.z;
129
- const b = 2 * O_C.x * dir.x * this._invRadiiSquared.x + 2 * O_C.y * dir.y * this._invRadiiSquared.y + 2 * O_C.z * dir.z * this._invRadiiSquared.z;
130
- const c = O_C.x * O_C.x * this._invRadiiSquared.x + O_C.y * O_C.y * this._invRadiiSquared.y + O_C.z * O_C.z * this._invRadiiSquared.z - 1;
131
- let d = b * b - 4 * a * c;
132
- if (d < 0 || a === 0 || b === 0 || c === 0) {
133
- return false;
134
- }
135
- d = Math.sqrt(d);
136
- const t1 = (-b + d) / (2 * a);
137
- const t2 = (-b - d) / (2 * a);
138
- if (t1 <= EPSILON && t2 <= EPSILON) {
139
- // both intersections are behind the ray origin
140
- return false;
141
- }
142
- let t = 0;
143
- if (t1 <= EPSILON) {
144
- t = t2;
145
- } else if (t2 <= EPSILON) {
146
- t = t1;
147
- } else {
148
- t = t1 < t2 ? t1 : t2;
149
- }
150
- if (t < EPSILON) {
151
- return false;
152
- } // Too close to intersection
153
-
154
- const inter = new THREE.Vector3();
155
- inter.addVectors(ray.origin, dir.clone().setLength(t));
156
- return inter;
157
- }
158
-
159
- /**
160
- * Calculate the geodesic distance, between coordCarto1 and coordCarto2.
161
- * It's most short distance on ellipsoid surface between coordCarto1 and
162
- * coordCarto2.
163
- * It's called orthodromy.
164
- *
165
- * @param coordCarto1 - The coordinate carto 1
166
- * @param coordCarto2 - The coordinate carto 2
167
- * @returns The orthodromic distance between the two given coordinates.
168
- */
169
- geodesicDistance(coordCarto1, coordCarto2) {
170
- // The formula uses the distance on approximated sphere,
171
- // with the nearest local radius of curvature of the ellipsoid
172
- // https://geodesie.ign.fr/contenu/fichiers/Distance_longitude_latitude.pdf
173
- const longitude1 = THREE.MathUtils.degToRad(coordCarto1.longitude);
174
- const latitude1 = THREE.MathUtils.degToRad(coordCarto1.latitude);
175
- const longitude2 = THREE.MathUtils.degToRad(coordCarto2.longitude);
176
- const latitude2 = THREE.MathUtils.degToRad(coordCarto2.latitude);
177
- const distRad = Math.acos(Math.sin(latitude1) * Math.sin(latitude2) + Math.cos(latitude1) * Math.cos(latitude2) * Math.cos(longitude2 - longitude1));
178
- const e = this.eccentricity;
179
- const latMoy = (latitude1 + latitude2) * 0.5;
180
- const es = (e * Math.sin(latMoy)) ** 2;
181
- const rho = this.size.x * (1 - e ** 2) / (1 - es) ** (3 / 2);
182
- const N = this.size.x / Math.sqrt(1 - es);
183
- return distRad * Math.sqrt(rho * N);
184
- }
185
- }
186
- export default Ellipsoid;