Coverage for pygeodesy/triaxials.py: 95%
586 statements
« prev ^ index » next coverage.py v7.2.2, created at 2024-05-25 12:04 -0400
« prev ^ index » next coverage.py v7.2.2, created at 2024-05-25 12:04 -0400
2# -*- coding: utf-8 -*-
4u'''Triaxal ellipsoid classes I{ordered} L{Triaxial} and I{unordered} L{Triaxial_} and Jacobi
5conformal projections L{JacobiConformal} and L{JacobiConformalSpherical}, transcoded from
6I{Charles Karney}'s C++ class U{JacobiConformal<https://GeographicLib.SourceForge.io/C++/doc/
7classGeographicLib_1_1JacobiConformal.html#details>} to pure Python and miscellaneous classes
8L{BetaOmega2Tuple}, L{BetaOmega3Tuple}, L{Jacobi2Tuple} and L{TriaxialError}.
10Copyright (C) U{Charles Karney<mailto:Karney@Alum.MIT.edu>} (2008-2023). For more information,
11see the U{GeographicLib<https://GeographicLib.SourceForge.io>} documentation.
13@see: U{Geodesics on a triaxial ellipsoid<https://WikiPedia.org/wiki/Geodesics_on_an_ellipsoid#
14 Geodesics_on_a_triaxial_ellipsoid>} and U{Triaxial coordinate systems and their geometrical
15 interpretation<https://www.Topo.Auth.GR/wp-content/uploads/sites/111/2021/12/09_Panou.pdf>}.
17@var Triaxials.Amalthea: Triaxial(name='Amalthea', a=125000, b=73000, c=64000, e2ab=0.658944, e2bc=0.231375493, e2ac=0.737856, volume=2446253479595252, area=93239507787.490371704, area_p=93212299402.670425415)
18@var Triaxials.Ariel: Triaxial(name='Ariel', a=581100, b=577900, c=577700, e2ab=0.01098327, e2bc=0.000692042, e2ac=0.011667711, volume=812633172614203904, area=4211301462766.580078125, area_p=4211301574065.829589844)
19@var Triaxials.Earth: Triaxial(name='Earth', a=6378173.435, b=6378103.9, c=6356754.399999999, e2ab=0.000021804, e2bc=0.006683418, e2ac=0.006705077, volume=1083208241574987694080, area=510065911057441.0625, area_p=510065915922713.6875)
20@var Triaxials.Enceladus: Triaxial(name='Enceladus', a=256600, b=251400, c=248300, e2ab=0.040119337, e2bc=0.024509841, e2ac=0.06364586, volume=67094551514082248, area=798618496278.596679688, area_p=798619018175.109863281)
21@var Triaxials.Europa: Triaxial(name='Europa', a=1564130, b=1561230, c=1560930, e2ab=0.003704694, e2bc=0.000384275, e2ac=0.004087546, volume=15966575194402123776, area=30663773697323.51953125, area_p=30663773794562.45703125)
22@var Triaxials.Io: Triaxial(name='Io', a=1829400, b=1819300, c=1815700, e2ab=0.011011391, e2bc=0.003953651, e2ac=0.014921506, volume=25313121117889765376, area=41691875849096.7421875, area_p=41691877397441.2109375)
23@var Triaxials.Mars: Triaxial(name='Mars', a=3394600, b=3393300, c=3376300, e2ab=0.000765776, e2bc=0.009994646, e2ac=0.010752768, volume=162907283585817247744, area=144249140795107.4375, area_p=144249144150662.15625)
24@var Triaxials.Mimas: Triaxial(name='Mimas', a=207400, b=196800, c=190600, e2ab=0.09960581, e2bc=0.062015624, e2ac=0.155444317, volume=32587072869017956, area=493855762247.691894531, area_p=493857714107.9375)
25@var Triaxials.Miranda: Triaxial(name='Miranda', a=240400, b=234200, c=232900, e2ab=0.050915557, e2bc=0.011070811, e2ac=0.061422691, volume=54926187094835456, area=698880863325.756958008, area_p=698881306767.950317383)
26@var Triaxials.Moon: Triaxial(name='Moon', a=1735550, b=1735324, c=1734898, e2ab=0.000260419, e2bc=0.000490914, e2ac=0.000751206, volume=21886698675223740416, area=37838824729886.09375, area_p=37838824733332.2265625)
27@var Triaxials.Tethys: Triaxial(name='Tethys', a=535600, b=528200, c=525800, e2ab=0.027441672, e2bc=0.009066821, e2ac=0.036259685, volume=623086233855821440, area=3528073490771.394042969, area_p=3528074261832.738769531)
28@var Triaxials.WGS84_35: Triaxial(name='WGS84_35', a=6378172, b=6378102, c=6356752.314245179, e2ab=0.00002195, e2bc=0.006683478, e2ac=0.006705281, volume=1083207319768789942272, area=510065621722018.125, area_p=510065626587483.3125)
29'''
30# make sure int/int division yields float quotient, see .basics
31from __future__ import division as _; del _ # PYCHOK semicolon
33from pygeodesy.basics import isLatLon, isscalar, _zip, _ValueError
34from pygeodesy.constants import EPS, EPS0, EPS02, EPS4, INT0, PI2, PI_3, PI4, \
35 _EPS2e4, float0_, isfinite, isnear1, _0_0, _0_5, \
36 _1_0, _N_1_0, _N_2_0, _4_0 # PYCHOK used!
37from pygeodesy.datums import Datum, _spherical_datum, _WGS84, Ellipsoid, _EWGS84, Fmt
38# from pygeodesy.dms import toDMS # _MODS
39# from pygeodesy.ellipsoids import Ellipsoid, _EWGS84 # from .datums
40# from pygeodesy.elliptic import Elliptic # _MODS
41# from pygeodesy.errors import _ValueError # from .basics
42from pygeodesy.fmath import Fdot, fdot, fmean_, hypot, hypot_, norm2, sqrt0
43from pygeodesy.fsums import _Fsumf_, fsumf_, fsum1f_
44from pygeodesy.interns import NN, _a_, _b_, _beta_, _c_, _distant_, _finite_, \
45 _height_, _inside_, _near_, _negative_, _not_, \
46 _NOTEQUAL_, _null_, _opposite_, _outside_, _SPACE_, \
47 _spherical_, _too_, _x_, _y_
48# from pygeodesy.lazily import _ALL_LAZY, _ALL_MODS as _MODS # from .vector3d
49from pygeodesy.named import _lazyNamedEnumItem as _lazy, _name__, _NamedEnum, \
50 _NamedEnumItem, _NamedTuple, _Pass
51from pygeodesy.namedTuples import LatLon3Tuple, Vector3Tuple, Vector4Tuple
52from pygeodesy.props import Property_RO, property_RO
53# from pygeodesy.streprs import Fmt # from .datums
54from pygeodesy.units import Float, Height_, Meter, Meter2, Meter3, Radians, \
55 Radius, Scalar_, _toDegrees, _toRadians
56from pygeodesy.utily import asin1, atan2d, km2m, m2km, SinCos2, sincos2d_
57from pygeodesy.vector3d import _otherV3d, Vector3d, _ALL_LAZY, _MODS
59from math import atan2, fabs, sqrt
61__all__ = _ALL_LAZY.triaxials
62__version__ = '24.05.21'
64_not_ordered_ = _not_('ordered')
65_omega_ = 'omega'
66_TRIPS = 269 # 48-55, Eberly 1074?
69class _NamedTupleTo(_NamedTuple): # in .testNamedTuples, like .cartesianBase.RadiusThetaPhi3Tuple
70 '''(INTERNAL) Base for C{-.toDegrees}, C{-.toRadians}.
71 '''
72 def _toDegrees(self, a, b, *c, **toDMS_kwds):
73 a, b, _ = _toDegrees(self, a, b, **toDMS_kwds)
74 return _ or self.classof(a, b, *c, name=self.name)
76 def _toRadians(self, a, b, *c):
77 a, b, _ = _toRadians(self, a, b)
78 return _ or self.classof(a, b, *c, name=self.name)
81class BetaOmega2Tuple(_NamedTupleTo):
82 '''2-Tuple C{(beta, omega)} with I{ellipsoidal} lat- and
83 longitude C{beta} and C{omega} both in L{Radians} (or
84 L{Degrees}).
85 '''
86 _Names_ = (_beta_, _omega_)
87 _Units_ = (_Pass, _Pass)
89 def toDegrees(self, **toDMS_kwds):
90 '''Convert this L{BetaOmega2Tuple} to L{Degrees} or C{toDMS}.
92 @return: L{BetaOmega2Tuple}C{(beta, omega)} with
93 C{beta} and C{omega} both in L{Degrees}
94 or as a L{toDMS} string provided some
95 B{C{toDMS_kwds}} keyword arguments are
96 specified.
97 '''
98 return _NamedTupleTo._toDegrees(self, *self, **toDMS_kwds)
100 def toRadians(self):
101 '''Convert this L{BetaOmega2Tuple} to L{Radians}.
103 @return: L{BetaOmega2Tuple}C{(beta, omega)} with
104 C{beta} and C{omega} both in L{Radians}.
105 '''
106 return _NamedTupleTo._toRadians(self, *self)
109class BetaOmega3Tuple(_NamedTupleTo):
110 '''3-Tuple C{(beta, omega, height)} with I{ellipsoidal} lat- and
111 longitude C{beta} and C{omega} both in L{Radians} (or L{Degrees})
112 and the C{height}, rather the (signed) I{distance} to the triaxial's
113 surface (measured along the radial line to the triaxial's center)
114 in C{meter}, conventionally.
115 '''
116 _Names_ = BetaOmega2Tuple._Names_ + (_height_,)
117 _Units_ = BetaOmega2Tuple._Units_ + ( Meter,)
119 def toDegrees(self, **toDMS_kwds):
120 '''Convert this L{BetaOmega3Tuple} to L{Degrees} or C{toDMS}.
122 @return: L{BetaOmega3Tuple}C{(beta, omega, height)} with
123 C{beta} and C{omega} both in L{Degrees} or as a
124 L{toDMS} string provided some B{C{toDMS_kwds}}
125 keyword arguments are specified.
126 '''
127 return _NamedTupleTo._toDegrees(self, *self, **toDMS_kwds)
129 def toRadians(self):
130 '''Convert this L{BetaOmega3Tuple} to L{Radians}.
132 @return: L{BetaOmega3Tuple}C{(beta, omega, height)} with
133 C{beta} and C{omega} both in L{Radians}.
134 '''
135 return _NamedTupleTo._toRadians(self, *self)
137 def to2Tuple(self):
138 '''Reduce this L{BetaOmega3Tuple} to a L{BetaOmega2Tuple}.
139 '''
140 return BetaOmega2Tuple(*self[:2])
143class Jacobi2Tuple(_NamedTupleTo):
144 '''2-Tuple C{(x, y)} with a Jacobi Conformal C{x} and C{y}
145 projection, both in L{Radians} (or L{Degrees}).
146 '''
147 _Names_ = (_x_, _y_)
148 _Units_ = (_Pass, _Pass)
150 def toDegrees(self, **toDMS_kwds):
151 '''Convert this L{Jacobi2Tuple} to L{Degrees} or C{toDMS}.
153 @return: L{Jacobi2Tuple}C{(x, y)} with C{x} and C{y}
154 both in L{Degrees} or as a L{toDMS} string
155 provided some B{C{toDMS_kwds}} keyword
156 arguments are specified.
157 '''
158 return _NamedTupleTo._toDegrees(self, *self, **toDMS_kwds)
160 def toRadians(self):
161 '''Convert this L{Jacobi2Tuple} to L{Radians}.
163 @return: L{Jacobi2Tuple}C{(x, y)} with C{x}
164 and C{y} both in L{Radians}.
165 '''
166 return _NamedTupleTo._toRadians(self, *self)
169class Triaxial_(_NamedEnumItem):
170 '''I{Unordered} triaxial ellipsoid and base class.
172 Triaxial ellipsoids with right-handed semi-axes C{a}, C{b} and C{c}, oriented
173 such that the large principal ellipse C{ab} is the equator I{Z}=0, I{beta}=0,
174 while the small principal ellipse C{ac} is the prime meridian, plane I{Y}=0,
175 I{omega}=0.
177 The four umbilic points, C{abs}(I{omega}) = C{abs}(I{beta}) = C{PI/2}, lie on
178 the middle principal ellipse C{bc} in plane I{X}=0, I{omega}=C{PI/2}.
180 @note: I{Geodetic} C{lat}- and C{lon}gitudes are in C{degrees}, I{geodetic}
181 C{phi} and C{lam}bda are in C{radians}, but I{ellipsoidal} lat- and
182 longitude C{beta} and C{omega} are in L{Radians} by default (or in
183 L{Degrees} if converted).
184 '''
185 _ijk = _kji = None
186 _unordered = True
188 def __init__(self, a_triaxial, b=None, c=None, **name):
189 '''New I{unordered} L{Triaxial_}.
191 @arg a_triaxial: Large, C{X} semi-axis (C{scalar}, conventionally in
192 C{meter}) or an other L{Triaxial} or L{Triaxial_} instance.
193 @kwarg b: Middle, C{Y} semi-axis (C{meter}, same units as B{C{a}}), required
194 if C{B{a_triaxial} is scalar}, ignored otherwise.
195 @kwarg c: Small, C{Z} semi-axis (C{meter}, same units as B{C{a}}), required
196 if C{B{a_triaxial} is scalar}, ignored otherwise.
197 @kwarg name: Optional C{B{name}=NN} (C{str}).
199 @raise TriaxialError: Invalid semi-axis or -axes.
200 '''
201 try:
202 try:
203 a = a_triaxial
204 t = a._abc3
205 except AttributeError:
206 t = Radius(a=a), Radius(b=b), Radius(c=c)
207 except (TypeError, ValueError) as x:
208 raise TriaxialError(a=a, b=b, c=c, cause=x)
209 if name:
210 self.name = name
212 a, b, c = self._abc3 = t
213 if self._unordered: # == not isinstance(self, Triaxial)
214 s, _, t = sorted(t)
215 if not (isfinite(t) and s > 0):
216 raise TriaxialError(a=a, b=b, c=c) # txt=_invalid_
217 elif not (isfinite(a) and a >= b >= c > 0):
218 raise TriaxialError(a=a, b=b, c=c, txt=_not_ordered_)
219 elif not (a > c and self._a2c2 > 0 and self.e2ac > 0):
220 raise TriaxialError(a=a, c=c, e2ac=self.e2ac, txt=_spherical_)
222 def __str__(self):
223 return self.toStr()
225 @Property_RO
226 def a(self):
227 '''Get the (largest) C{x} semi-axis (C{meter}, conventionally).
228 '''
229 a, _, _ = self._abc3
230 return a
232 @Property_RO
233 def _a2b2(self):
234 '''(INTERNAL) Get C{a**2 - b**2} == E_sub_e**2.
235 '''
236 a, b, _ = self._abc3
237 return ((a - b) * (a + b)) if a != b else _0_0
239 @Property_RO
240 def _a2_b2(self):
241 '''(INTERNAL) Get C{(a/b)**2}.
242 '''
243 a, b, _ = self._abc3
244 return (a / b)**2 if a != b else _1_0
246 @Property_RO
247 def _a2c2(self):
248 '''(INTERNAL) Get C{a**2 - c**2} == E_sub_x**2.
249 '''
250 a, _, c = self._abc3
251 return ((a - c) * (a + c)) if a != c else _0_0
253 @Property_RO
254 def area(self):
255 '''Get the surface area (C{meter} I{squared}).
256 '''
257 c, b, a = sorted(self._abc3)
258 if a > c:
259 a = Triaxial(a, b, c).area if a > b else \
260 Ellipsoid(a, b=c).areax # a == b
261 else: # a == c == b
262 a = Meter2(area=a**2 * PI4)
263 return a
265 def area_p(self, p=1.6075):
266 '''I{Approximate} the surface area (C{meter} I{squared}).
268 @kwarg p: Exponent (C{scalar} > 0), 1.6 for near-spherical or 1.5849625007
269 for "near-flat" triaxials.
271 @see: U{Surface area<https://WikiPedia.org/wiki/Ellipsoid#Approximate_formula>}.
272 '''
273 a, b, c = self._abc3
274 if a == b == c:
275 a *= a
276 else:
277 _p = pow
278 a = _p(fmean_(_p(a * b, p), _p(a * c, p), _p(b * c, p)), _1_0 / p)
279 return Meter2(area_p=a * PI4)
281 @Property_RO
282 def b(self):
283 '''Get the (middle) C{y} semi-axis (C{meter}, same units as B{C{a}}).
284 '''
285 _, b, _ = self._abc3
286 return b
288 @Property_RO
289 def _b2c2(self):
290 '''(INTERNAL) Get C{b**2 - c**2} == E_sub_y**2.
291 '''
292 _, b, c = self._abc3
293 return ((b - c) * (b + c)) if b != c else _0_0
295 @Property_RO
296 def c(self):
297 '''Get the (smallest) C{z} semi-axis (C{meter}, same units as B{C{a}}).
298 '''
299 _, _, c = self._abc3
300 return c
302 @Property_RO
303 def _c2_b2(self):
304 '''(INTERNAL) Get C{(c/b)**2}.
305 '''
306 _, b, c = self._abc3
307 return (c / b)**2 if b != c else _1_0
309 @Property_RO
310 def e2ab(self):
311 '''Get the C{ab} ellipse' I{(1st) eccentricity squared} (C{scalar}), M{1 - (b/a)**2}.
312 '''
313 return Float(e2ab=(_1_0 - self._1e2ab) or _0_0)
315 @Property_RO
316 def _1e2ab(self):
317 '''(INTERNAL) Get C{1 - e2ab} == C{(b/a)**2}.
318 '''
319 a, b, _ = self._abc3
320 return (b / a)**2 if a != b else _1_0
322 @Property_RO
323 def e2ac(self):
324 '''Get the C{ac} ellipse' I{(1st) eccentricity squared} (C{scalar}), M{1 - (c/a)**2}.
325 '''
326 return Float(e2ac=(_1_0 - self._1e2ac) or _0_0)
328 @Property_RO
329 def _1e2ac(self):
330 '''(INTERNAL) Get C{1 - e2ac} == C{(c/a)**2}.
331 '''
332 a, _, c = self._abc3
333 return (c / a)**2 if a != c else _1_0
335 @Property_RO
336 def e2bc(self):
337 '''Get the C{bc} ellipse' I{(1st) eccentricity squared} (C{scalar}), M{1 - (c/b)**2}.
338 '''
339 return Float(e2bc=(_1_0 - self._1e2bc) or _0_0)
341 _1e2bc = _c2_b2 # C{1 - e2bc} == C{(c/b)**2}
343 @property_RO
344 def _Elliptic(self):
345 '''(INTERNAL) Get class L{Elliptic}, I{once}.
346 '''
347 Triaxial_._Elliptic = E = _MODS.elliptic.Elliptic # overwrite property_RO
348 return E
350 def hartzell4(self, pov, los=False, **name):
351 '''Compute the intersection of this triaxial's surface with a Line-Of-Sight
352 from a Point-Of-View in space.
354 @see: Function L{hartzell4<triaxials.hartzell4>} for further details.
355 '''
356 return hartzell4(pov, los=los, tri_biax=self, **name)
358 def height4(self, x_xyz, y=None, z=None, normal=True, eps=EPS, **name):
359 '''Compute the projection on and the height above or below this triaxial's surface.
361 @arg x_xyz: X component (C{scalar}) or a cartesian (C{Cartesian}, L{Ecef9Tuple},
362 L{Vector3d}, L{Vector3Tuple} or L{Vector4Tuple}).
363 @kwarg y: Y component (C{scalar}), required if B{C{x_xyz}} if C{scalar}.
364 @kwarg z: Z component (C{scalar}), required if B{C{x_xyz}} if C{scalar}.
365 @kwarg normal: If C{True} the projection is the I{normal, plumb} to the surface of,
366 otherwise the C{radial} line to the center of this triaxial (C{bool}).
367 @kwarg eps: Tolerance for root finding and validation (C{scalar}), use a negative
368 value to skip validation.
369 @kwarg name: Optional, overriding C{B{name}="heigh4"} (C{str}).
371 @return: L{Vector4Tuple}C{(x, y, z, h)} with the cartesian coordinates C{x}, C{y}
372 and C{z} of the projection on or the intersection with and with the height
373 C{h} above or below the triaxial's surface in C{meter}, conventionally.
375 @raise TriaxialError: Non-cartesian B{C{xyz}}, invalid B{C{eps}}, no convergence in
376 root finding or validation failed.
378 @see: Method L{Ellipsoid.height4} and I{Eberly}'s U{Distance from a Point to ...
379 <https://www.GeometricTools.com/Documentation/DistancePointEllipseEllipsoid.pdf>}.
380 '''
381 v, r = _otherV3d_(x_xyz, y, z), self.isSpherical
383 i, h = None, v.length
384 if h < EPS0: # EPS
385 x = y = z = _0_0
386 h -= min(self._abc3) # nearest
387 elif r: # .isSpherical
388 x, y, z = v.times(r / h).xyz
389 h -= r
390 else:
391 x, y, z = v.xyz
392 try:
393 if normal: # plumb to surface
394 x, y, z, h, i = _normalTo5(x, y, z, self, eps=eps)
395 else: # radial to center
396 x, y, z = self._radialTo3(z, hypot(x, y), y, x)
397 h = v.minus_(x, y, z).length
398 except Exception as e:
399 raise TriaxialError(x=x, y=y, z=z, cause=e)
400 if h > 0 and self.sideOf(v, eps=EPS0) < 0:
401 h = -h # below the surface
402 return Vector4Tuple(x, y, z, h, iteration=i,
403 name=_name__(name, name__=self.height4))
405 @Property_RO
406 def isOrdered(self):
407 '''Is this triaxial I{ordered} and I{not spherical} (C{bool})?
408 '''
409 a, b, c = self._abc3
410 return bool(a >= b > c) # b > c!
412 @Property_RO
413 def isSpherical(self):
414 '''Is this triaxial I{spherical} (C{Radius} or INT0)?
415 '''
416 a, b, c = self._abc3
417 return a if a == b == c else INT0
419 def _norm2(self, s, c, *a):
420 '''(INTERNAL) Normalize C{s} and C{c} iff not already.
421 '''
422 if fabs(_hypot21(s, c)) > EPS02:
423 s, c = norm2(s, c)
424 if a:
425 s, c = norm2(s * self.b, c * a[0])
426 return float0_(s, c)
428 def normal3d(self, x_xyz, y=None, z=None, length=_1_0):
429 '''Get a 3-D vector at a cartesian on and perpendicular to this triaxial's surface.
431 @arg x_xyz: X component (C{scalar}) or a cartesian (C{Cartesian},
432 L{Ecef9Tuple}, L{Vector3d}, L{Vector3Tuple} or L{Vector4Tuple}).
433 @kwarg y: Y component (C{scalar}), required if B{C{x_xyz}} if C{scalar}.
434 @kwarg z: Z component (C{scalar}), required if B{C{x_xyz}} if C{scalar}.
435 @kwarg length: Optional length and in-/outward direction (C{scalar}).
437 @return: A C{Vector3d(x_, y_, z_)} normalized to B{C{length}}, pointing
438 in- or outward for neg- respectively positive B{C{length}}.
440 @raise TriaxialError: Zero length cartesian or vector.
442 @note: Cartesian location C{(B{x}, B{y}, B{z})} must be on this triaxial's
443 surface, use method L{Triaxial.sideOf} to validate.
444 '''
445 # n = 2 * (x / a2, y / b2, z / c2)
446 # == 2 * (x, y * a2 / b2, z * a2 / c2) / a2 # iff ordered
447 # == 2 * (x, y / _1e2ab, z / _1e2ac) / a2
448 # == unit(x, y / _1e2ab, z / _1e2ac).times(length)
449 n = self._normal3d.times_(*_otherV3d_(x_xyz, y, z).xyz)
450 if n.length < EPS0:
451 raise TriaxialError(x=x_xyz, y=y, z=z, txt=_null_)
452 return n.times(length / n.length)
454 @Property_RO
455 def _normal3d(self):
456 '''(INTERNAL) Get M{Vector3d((d/a)**2, (d/b)**2, (d/c)**2)}, M{d = max(a, b, c)}.
457 '''
458 d = max(self._abc3)
459 t = tuple(((d / x)**2 if x != d else _1_0) for x in self._abc3)
460 return Vector3d(*t, name__=self.normal3d)
462 def _order3(self, *abc, **reverse): # reverse=False
463 '''(INTERNAL) Un-/Order C{a}, C{b} and C{c}.
465 @return: 3-Tuple C{(a, b, c)} ordered by or un-ordered
466 (reverse-ordered) C{ijk} if C{B{reverse}=True}.
467 '''
468 ijk = self._order_ijk(**reverse)
469 return _getitems(abc, *ijk) if ijk else abc
471 def _order3d(self, v, **reverse): # reverse=False
472 '''(INTERNAL) Un-/Order a C{Vector3d}.
474 @return: Vector3d(x, y, z) un-/ordered.
475 '''
476 ijk = self._order_ijk(**reverse)
477 return v.classof(*_getitems(v.xyz, *ijk)) if ijk else v
479 @Property_RO
480 def _ordered4(self):
481 '''(INTERNAL) Helper for C{_hartzell3} and C{_normalTo5}.
482 '''
483 def _order2(reverse, a, b, c):
484 '''(INTERNAL) Un-Order C{a}, C{b} and C{c}.
486 @return: 2-Tuple C{((a, b, c), ijk)} with C{a} >= C{b} >= C{c}
487 and C{ijk} a 3-tuple with the initial indices.
488 '''
489 i, j, k = 0, 1, 2 # range(3)
490 if a < b:
491 a, b, i, j = b, a, j, i
492 if a < c:
493 a, c, i, k = c, a, k, i
494 if b < c:
495 b, c, j, k = c, b, k, j
496 # reverse (k, j, i) since (a, b, c) is reversed-sorted
497 ijk = (k, j, i) if reverse else (None if i < j < k else (i, j, k))
498 return (a, b, c), ijk
500 abc, T = self._abc3, self
501 if not self.isOrdered:
502 abc, ijk = _order2(False, *abc)
503 if ijk:
504 _, kji = _order2(True, *ijk)
505 T = Triaxial_(*abc)
506 T._ijk, T._kji = ijk, kji
507 return abc + (T,)
509 def _order_ijk(self, reverse=False):
510 '''(INTERNAL) Get the un-/order indices.
511 '''
512 return self._kji if reverse else self._ijk
514 def _radialTo3(self, sbeta, cbeta, somega, comega):
515 '''(INTERNAL) I{Unordered} helper for C{.height4}.
516 '''
517 def _rphi(a, b, sphi, cphi):
518 # <https://WikiPedia.org/wiki/Ellipse#Polar_form_relative_to_focus>
519 # polar form: radius(phi) = a * b / hypot(a * sphi, b * cphi)
520 return (b / hypot(sphi, b / a * cphi)) if a > b else (
521 (a / hypot(cphi, a / b * sphi)) if a < b else a)
523 sa, ca = self._norm2(sbeta, cbeta)
524 sb, cb = self._norm2(somega, comega)
526 a, b, c = self._abc3
527 if a != b:
528 a = _rphi(a, b, sb, cb)
529 if a != c:
530 c = _rphi(a, c, sa, ca)
531 z, r = c * sa, c * ca
532 x, y = r * cb, r * sb
533 return x, y, z
535 def sideOf(self, x_xyz, y=None, z=None, eps=EPS4):
536 '''Is a cartesian above, below or on the surface of this triaxial?
538 @arg x_xyz: X component (C{scalar}) or a cartesian (C{Cartesian},
539 L{Ecef9Tuple}, L{Vector3d}, L{Vector3Tuple} or L{Vector4Tuple}).
540 @kwarg y: Y component (C{scalar}), required if B{C{x_xyz}} if C{scalar}.
541 @kwarg z: Z component (C{scalar}), required if B{C{x_xyz}} if C{scalar}.
542 @kwarg eps: Near-surface tolerance (C{scalar}, distance I{squared}).
544 @return: C{INT0} if C{(B{x}, B{y}, B{z})} is near this triaxial's surface
545 within tolerance B{C{eps}}, otherwise a signed, radial, normalized
546 distance I{squared} (C{float}), negative or positive for in-
547 respectively outside this triaxial.
549 @see: Methods L{Triaxial.height4} and L{Triaxial.normal3d}.
550 '''
551 return _sideOf(_otherV3d_(x_xyz, y, z).xyz, self._abc3, eps=eps)
553 def toEllipsoid(self, **name):
554 '''Convert this triaxial to an L{Ellipsoid}, provided 2 axes match.
556 @kwarg name: Optional, overriding C{B{name}=NN} (C{str})=.
558 @return: An L{Ellipsoid} with north along this C{Z} axis if C{a == b},
559 this C{Y} axis if C{a == c} or this C{X} axis if C{b == c}.
561 @raise TriaxialError: This C{a != b}, C{b != c} and C{c != a}.
563 @see: Method L{Ellipsoid.toTriaxial}.
564 '''
565 a, b, c = self._abc3
566 if a == b:
567 b = c # N = c-Z
568 elif b == c: # N = a-X
569 a, b = b, a
570 elif a != c: # N = b-Y
571 t = _SPACE_(_a_, _NOTEQUAL_, _b_, _NOTEQUAL_, _c_)
572 raise TriaxialError(a=a, b=b, c=c, txt=t)
573 return Ellipsoid(a, b=b, name=self._name__(name))
575 def toStr(self, prec=9, name=NN, **unused): # PYCHOK signature
576 '''Return this C{Triaxial} as a string.
578 @kwarg prec: Precision, number of decimal digits (0..9).
579 @kwarg name: Optional, overriding C{B{name}=NN} (C{str})
580 or C{None} to exclude this triaxial's name.
582 @return: This C{Triaxial}'s attributes (C{str}).
583 '''
584 T = Triaxial_
585 t = T.a,
586 J = JacobiConformalSpherical
587 t += (J.ab, J.bc) if isinstance(self, J) else (T.b, T.c)
588 t += T.e2ab, T.e2bc, T.e2ac
589 J = JacobiConformal
590 if isinstance(self, J):
591 t += J.xyQ2,
592 t += T.volume, T.area
593 return self._instr(name, prec, props=t, area_p=self.area_p()) # __name__
595 @Property_RO
596 def volume(self):
597 '''Get the volume (C{meter**3}), M{4 / 3 * PI * a * b * c}.
598 '''
599 a, b, c = self._abc3
600 return Meter3(volume=a * b * c * PI_3 * _4_0)
603class Triaxial(Triaxial_):
604 '''I{Ordered} triaxial ellipsoid.
606 @see: L{Triaxial_} for more information.
607 '''
608 _unordered = False
610 def __init__(self, a_triaxial, b=None, c=None, **name):
611 '''New I{ordered} L{Triaxial}.
613 @arg a_triaxial: Largest semi-axis (C{scalar}, conventionally in C{meter})
614 or an other L{Triaxial} or L{Triaxial_} instance.
615 @kwarg b: Middle semi-axis (C{meter}, same units as B{C{a}}), required
616 if C{B{a_triaxial} is scalar}, ignored otherwise.
617 @kwarg c: Smallest semi-axis (C{meter}, same units as B{C{a}}), required
618 if C{B{a_triaxial} is scalar}, ignored otherwise.
619 @kwarg name: Optional C{B{name}=NN} (C{str}).
621 @note: The semi-axes must be ordered as C{B{a} >= B{b} >= B{c} > 0} and
622 must be ellipsoidal, C{B{a} > B{c}}.
624 @raise TriaxialError: Semi-axes not ordered, spherical or invalid.
625 '''
626 Triaxial_.__init__(self, a_triaxial, b=b, c=c, **name)
628 @Property_RO
629 def _a2b2_a2c2(self):
630 '''@see: Methods C{.forwardBetaOmega} and C{._k2_kp2}.
631 '''
632 return self._a2b2 / self._a2c2
634 @Property_RO
635 def area(self):
636 '''Get the surface area (C{meter} I{squared}).
638 @see: U{Surface area<https://WikiPedia.org/wiki/Ellipsoid#Surface_area>}.
639 '''
640 a, b, c = self._abc3
641 if a != b:
642 kp2, k2 = self._k2_kp2 # swapped!
643 aE = self._Elliptic(k2, _0_0, kp2, _1_0)
644 c2 = self._1e2ac # cos(phi)**2 = (c/a)**2
645 s = sqrt(self.e2ac) # sin(phi)**2 = 1 - c2
646 r = asin1(s) # phi = atan2(sqrt(c2), s)
647 b *= fsum1f_(aE.fE(r) * s, c / a * c / b,
648 aE.fF(r) * c2 / s)
649 a = Meter2(area=a * b * PI2)
650 else: # a == b > c
651 a = Ellipsoid(a, b=c).areax
652 return a
654 def _exyz3(self, u):
655 '''(INTERNAL) Helper for C{.forwardBetOmg}.
656 '''
657 if u > 0:
658 u2 = u**2
659 x = u * sqrt0(_1_0 + self._a2c2 / u2, Error=TriaxialError)
660 y = u * sqrt0(_1_0 + self._b2c2 / u2, Error=TriaxialError)
661 else:
662 x = y = u = _0_0
663 return x, y, u
665 def forwardBetaOmega(self, beta, omega, height=0, **name):
666 '''Convert I{ellipsoidal} lat- and longitude C{beta}, C{omega}
667 and height to cartesian.
669 @arg beta: Ellipsoidal latitude (C{radians} or L{Degrees}).
670 @arg omega: Ellipsoidal longitude (C{radians} or L{Degrees}).
671 @arg height: Height above or below the ellipsoid's surface (C{meter}, same
672 units as this triaxial's C{a}, C{b} and C{c} semi-axes).
673 @kwarg name: Optional C{B{name}=NN} (C{str}).
675 @return: A L{Vector3Tuple}C{(x, y, z)}.
677 @see: Method L{Triaxial.reverseBetaOmega} and U{Expressions (23-25)<https://
678 www.Topo.Auth.GR/wp-content/uploads/sites/111/2021/12/09_Panou.pdf>}.
679 '''
680 if height:
681 h = self._Height(height)
682 x, y, z = self._exyz3(h + self.c)
683 else:
684 x, y, z = self._abc3 # == self._exyz3(self.c)
685 if z: # and x and y:
686 sa, ca = SinCos2(beta)
687 sb, cb = SinCos2(omega)
689 r = self._a2b2_a2c2
690 x *= cb * sqrt0(ca**2 + r * sa**2, Error=TriaxialError)
691 y *= ca * sb
692 z *= sa * sqrt0(_1_0 - r * cb**2, Error=TriaxialError)
693 return Vector3Tuple(x, y, z, **name)
695 def forwardBetaOmega_(self, sbeta, cbeta, somega, comega, **name):
696 '''Convert I{ellipsoidal} lat- and longitude C{beta} and C{omega}
697 to cartesian coordinates I{on the triaxial's surface}.
699 @arg sbeta: Ellipsoidal latitude C{sin(beta)} (C{scalar}).
700 @arg cbeta: Ellipsoidal latitude C{cos(beta)} (C{scalar}).
701 @arg somega: Ellipsoidal longitude C{sin(omega)} (C{scalar}).
702 @arg comega: Ellipsoidal longitude C{cos(omega)} (C{scalar}).
703 @kwarg name: Optional C{B{name}=NN} (C{str}).
705 @return: A L{Vector3Tuple}C{(x, y, z)} on the surface.
707 @raise TriaxialError: This triaxial is near-spherical.
709 @see: Method L{Triaxial.reverseBetaOmega}, U{Triaxial ellipsoid coordinate
710 system<https://WikiPedia.org/wiki/Geodesics_on_an_ellipsoid#
711 Triaxial_ellipsoid_coordinate_system>} and U{expressions (23-25)<https://
712 www.Topo.Auth.GR/wp-content/uploads/sites/111/2021/12/09_Panou.pdf>}.
713 '''
714 t = self._radialTo3(sbeta, cbeta, somega, comega)
715 return Vector3Tuple(*t, **name)
717 def forwardCartesian(self, x_xyz, y=None, z=None, **normal_eps_name):
718 '''Project a cartesian on this triaxial.
720 @arg x_xyz: X component (C{scalar}) or a cartesian (C{Cartesian},
721 L{Ecef9Tuple}, L{Vector3d}, L{Vector3Tuple} or L{Vector4Tuple}).
722 @kwarg y: Y component (C{scalar}), required if B{C{x_xyz}} if C{scalar}.
723 @kwarg z: Z component (C{scalar}), required if B{C{x_xyz}} if C{scalar}.
724 @kwarg normal_eps_name: Optional keyword arguments C{B{normal}=True},
725 C{B{eps}=EPS} and overriding C{B{name}="height4"} (C{str}),
726 see method L{Triaxial.height4}.
728 @see: Method L{Triaxial.height4} for further information and method
729 L{Triaxial.reverseCartesian} to reverse the projection.
730 '''
731 return self.height4(x_xyz, y, z, **normal_eps_name)
733 def forwardLatLon(self, lat, lon, height=0, **name):
734 '''Convert I{geodetic} lat-, longitude and heigth to cartesian.
736 @arg lat: Geodetic latitude (C{degrees}).
737 @arg lon: Geodetic longitude (C{degrees}).
738 @arg height: Height above the ellipsoid (C{meter}, same units
739 as this triaxial's C{a}, C{b} and C{c} axes).
740 @kwarg name: Optional C{B{name}=NN} (C{str}).
742 @return: A L{Vector3Tuple}C{(x, y, z)}.
744 @see: Method L{Triaxial.reverseLatLon} and U{Expressions (9-11)<https://
745 www.Topo.Auth.GR/wp-content/uploads/sites/111/2021/12/09_Panou.pdf>}.
746 '''
747 return self._forwardLatLon3(height, name, *sincos2d_(lat, lon))
749 def forwardLatLon_(self, slat, clat, slon, clon, height=0, **name):
750 '''Convert I{geodetic} lat-, longitude and heigth to cartesian.
752 @arg slat: Geodetic latitude C{sin(lat)} (C{scalar}).
753 @arg clat: Geodetic latitude C{cos(lat)} (C{scalar}).
754 @arg slon: Geodetic longitude C{sin(lon)} (C{scalar}).
755 @arg clon: Geodetic longitude C{cos(lon)} (C{scalar}).
756 @arg height: Height above the ellipsoid (C{meter}, same units
757 as this triaxial's axes C{a}, C{b} and C{c}).
758 @kwarg name: Optional C{B{name}=NN} (C{str}).
760 @return: A L{Vector3Tuple}C{(x, y, z)}.
762 @see: Method L{Triaxial.reverseLatLon} and U{Expressions (9-11)<https://
763 www.Topo.Auth.GR/wp-content/uploads/sites/111/2021/12/09_Panou.pdf>}.
764 '''
765 sa, ca = self._norm2(slat, clat)
766 sb, cb = self._norm2(slon, clon)
767 return self._forwardLatLon3(height, name, sa, ca, sb, cb)
769 def _forwardLatLon3(self, height, name, sa, ca, sb, cb): # name always **name
770 '''(INTERNAL) Helper for C{.forwardLatLon} and C{.forwardLatLon_}.
771 '''
772 ca_x_sb = ca * sb
773 h = self._Height(height)
774 # 1 - (1 - (c/a)**2) * sa**2 - (1 - (b/a)**2) * ca**2 * sb**2
775 t = fsumf_(_1_0, -self.e2ac * sa**2, -self.e2ab * ca_x_sb**2)
776 n = self.a / sqrt0(t, Error=TriaxialError) # prime vertical
777 x = (h + n) * ca * cb
778 y = (h + n * self._1e2ab) * ca_x_sb
779 z = (h + n * self._1e2ac) * sa
780 return Vector3Tuple(x, y, z, **name)
782 def _Height(self, height):
783 '''(INTERNAL) Validate a C{height}.
784 '''
785 return Height_(height=height, low=-self.c, Error=TriaxialError)
787 @Property_RO
788 def _k2_kp2(self):
789 '''(INTERNAL) Get C{k2} and C{kp2} for C{._xE}, C{._yE} and C{.area}.
790 '''
791 # k2 = a2b2 / a2c2 * c2_b2
792 # kp2 = b2c2 / a2c2 * a2_b2
793 # b2 = b**2
794 # xE = Elliptic(k2, -a2b2 / b2, kp2, a2_b2)
795 # yE = Elliptic(kp2, +b2c2 / b2, k2, c2_b2)
796 # aE = Elliptic(kp2, 0, k2, 1)
797 return (self._a2b2_a2c2 * self._c2_b2,
798 self._b2c2 / self._a2c2 * self._a2_b2)
800 def _radialTo3(self, sbeta, cbeta, somega, comega):
801 '''(INTERNAL) Convert I{ellipsoidal} lat- and longitude C{beta} and
802 C{omega} to cartesian coordinates I{on the triaxial's surface},
803 also I{ordered} helper for C{.height4}.
804 '''
805 sa, ca = self._norm2(sbeta, cbeta)
806 sb, cb = self._norm2(somega, comega)
808 b2_a2 = self._1e2ab # == (b/a)**2
809 c2_a2 = -self._1e2ac # == -(c/a)**2
810 a2c2_a2 = self. e2ac # (a**2 - c**2) / a**2 == 1 - (c/a)**2
812 x2 = _Fsumf_(_1_0, -b2_a2 * sa**2, c2_a2 * ca**2).fover(a2c2_a2)
813 z2 = _Fsumf_(c2_a2, sb**2, b2_a2 * cb**2).fover(a2c2_a2)
815 x, y, z = self._abc3
816 x *= cb * sqrt0(x2, Error=TriaxialError)
817 y *= ca * sb
818 z *= sa * sqrt0(z2, Error=TriaxialError)
819 return x, y, z
821 def reverseBetaOmega(self, x_xyz, y=None, z=None, **name):
822 '''Convert cartesian to I{ellipsoidal} lat- and longitude, C{beta}, C{omega}
823 and height.
825 @arg x_xyz: X component (C{scalar}) or a cartesian (C{Cartesian},
826 L{Ecef9Tuple}, L{Vector3d}, L{Vector3Tuple} or L{Vector4Tuple}).
827 @kwarg y: Y component (C{scalar}), required if B{C{x_xyz}} if C{scalar}.
828 @kwarg z: Z component (C{scalar}), required if B{C{x_xyz}} if C{scalar}.
829 @kwarg name: Optional C{B{name}=NN} (C{str}).
831 @return: A L{BetaOmega3Tuple}C{(beta, omega, height)} with C{beta} and
832 C{omega} in L{Radians} and (radial) C{height} in C{meter}, same
833 units as this triaxial's axes.
835 @see: Methods L{Triaxial.forwardBetaOmega} and L{Triaxial.forwardBetaOmega_}
836 and U{Expressions (21-22)<https://www.Topo.Auth.GR/wp-content/uploads/
837 sites/111/2021/12/09_Panou.pdf>}.
838 '''
839 v = _otherV3d_(x_xyz, y, z)
840 a, b, h = self._reverseLatLon3(v, atan2, v, self.forwardBetaOmega_)
841 return BetaOmega3Tuple(Radians(beta=a), Radians(omega=b), h, **name)
843 def reverseCartesian(self, x_xyz, y=None, z=None, h=0, normal=True, eps=_EPS2e4, **name):
844 '''"Unproject" a cartesian on to a cartesion I{off} this triaxial's surface.
846 @arg x_xyz: X component (C{scalar}) or a cartesian (C{Cartesian},
847 L{Ecef9Tuple}, L{Vector3d}, L{Vector3Tuple} or L{Vector4Tuple}).
848 @kwarg y: Y component (C{scalar}), required if B{C{x_xyz}} if C{scalar}.
849 @kwarg z: Z component (C{scalar}), required if B{C{x_xyz}} if C{scalar}.
850 @arg h: Height above or below this triaxial's surface (C{meter}, same units
851 as the axes).
852 @kwarg normal: If C{True} the height is C{normal} to the surface, otherwise
853 C{radially} to the center of this triaxial (C{bool}).
854 @kwarg eps: Tolerance for surface test (C{scalar}).
855 @kwarg name: Optional C{B{name}=NN} (C{str}).
857 @return: A L{Vector3Tuple}C{(x, y, z)}.
859 @raise TrialError: Cartesian C{(x, y, z)} not on this triaxial's surface.
861 @see: Methods L{Triaxial.forwardCartesian} and L{Triaxial.height4}.
862 '''
863 v = _otherV3d_(x_xyz, y, z, **name)
864 s = _sideOf(v.xyz, self._abc3, eps=eps)
865 if s: # PYCHOK no cover
866 t = _SPACE_((_inside_ if s < 0 else _outside_), self.toRepr())
867 raise TriaxialError(eps=eps, sideOf=s, x=v.x, y=v.y, z=v.z, txt=t)
869 if h:
870 if normal:
871 v = v.plus(self.normal3d(*v.xyz, length=h))
872 elif v.length > EPS0:
873 v = v.times(_1_0 + (h / v.length))
874 return v.xyz # Vector3Tuple
876 def reverseLatLon(self, x_xyz, y=None, z=None, **name):
877 '''Convert cartesian to I{geodetic} lat-, longitude and height.
879 @arg x_xyz: X component (C{scalar}) or a cartesian (C{Cartesian},
880 L{Ecef9Tuple}, L{Vector3d}, L{Vector3Tuple} or L{Vector4Tuple}).
881 @kwarg y: Y component (C{scalar}), required if B{C{x_xyz}} if C{scalar}.
882 @kwarg z: Z component (C{scalar}), required if B{C{x_xyz}} if C{scalar}.
883 @kwarg name: Optional C{B{name}=NN} (C{str}).
885 @return: A L{LatLon3Tuple}C{(lat, lon, height)} with C{lat} and C{lon}
886 in C{degrees} and (radial) C{height} in C{meter}, same units
887 as this triaxial's axes.
889 @see: Methods L{Triaxial.forwardLatLon} and L{Triaxial.forwardLatLon_}
890 and U{Expressions (4-5)<https://www.Topo.Auth.GR/wp-content/uploads/
891 sites/111/2021/12/09_Panou.pdf>}.
892 '''
893 v = _otherV3d_(x_xyz, y, z)
894 s = v.times_(self._1e2ac, # == 1 - e_sub_x**2
895 self._1e2bc, # == 1 - e_sub_y**2
896 _1_0)
897 t = self._reverseLatLon3(s, atan2d, v, self.forwardLatLon_)
898 return LatLon3Tuple(*t, **name)
900 def _reverseLatLon3(self, s, atan2_, v, forward_):
901 '''(INTERNAL) Helper for C{.reverseBetOmg} and C{.reverseLatLon}.
902 '''
903 x, y, z = s.xyz
904 d = hypot( x, y)
905 a = atan2_(z, d)
906 b = atan2_(y, x)
907 h = v.minus_(*forward_(z, d, y, x)).length
908 return a, b, h
911class JacobiConformal(Triaxial):
912 '''This is a conformal projection of a triaxial ellipsoid to a plane in which the
913 C{X} and C{Y} grid lines are straight.
915 Ellipsoidal coordinates I{beta} and I{omega} are converted to Jacobi Conformal
916 I{y} respectively I{x} separately. Jacobi's coordinates have been multiplied
917 by C{sqrt(B{a}**2 - B{c}**2) / (2 * B{b})} so that the customary results are
918 returned in the case of an ellipsoid of revolution.
920 Copyright (C) U{Charles Karney<mailto:Karney@Alum.MIT.edu>} (2014-2023) and
921 licensed under the MIT/X11 License.
923 @note: This constructor can I{not be used to specify a sphere}, see alternate
924 L{JacobiConformalSpherical}.
926 @see: L{Triaxial}, C++ class U{JacobiConformal<https://GeographicLib.SourceForge.io/
927 C++/doc/classGeographicLib_1_1JacobiConformal.html#details>}, U{Jacobi's conformal
928 projection<https://GeographicLib.SourceForge.io/C++/doc/jacobi.html>} and Jacobi,
929 C. G. J. I{U{Vorlesungen über Dynamik<https://Books.Google.com/books?
930 id=ryEOAAAAQAAJ&pg=PA212>}}, page 212ff.
931 '''
933 @Property_RO
934 def _xE(self):
935 '''(INTERNAL) Get the x-elliptic function.
936 '''
937 k2, kp2 = self._k2_kp2
938 # -a2b2 / b2 == (b2 - a2) / b2 == 1 - a2 / b2 == 1 - a2_b2
939 return self._Elliptic(k2, _1_0 - self._a2_b2, kp2, self._a2_b2)
941 def xR(self, omega):
942 '''Compute a Jacobi Conformal C{x} projection.
944 @arg omega: Ellipsoidal longitude (C{radians} or L{Degrees}).
946 @return: The C{x} projection (L{Radians}).
947 '''
948 return self.xR_(*SinCos2(omega))
950 def xR_(self, somega, comega):
951 '''Compute a Jacobi Conformal C{x} projection.
953 @arg somega: Ellipsoidal longitude C{sin(omega)} (C{scalar}).
954 @arg comega: Ellipsoidal longitude C{cos(omega)} (C{scalar}).
956 @return: The C{x} projection (L{Radians}).
957 '''
958 s, c = self._norm2(somega, comega, self.a)
959 return Radians(x=self._xE.fPi(s, c) * self._a2_b2)
961 @Property_RO
962 def xyQ2(self):
963 '''Get the Jacobi Conformal quadrant size (L{Jacobi2Tuple}C{(x, y)}).
964 '''
965 return Jacobi2Tuple(Radians(x=self._a2_b2 * self._xE.cPi),
966 Radians(y=self._c2_b2 * self._yE.cPi),
967 name=JacobiConformal.xyQ2.name)
969 def xyR2(self, beta, omega, **name):
970 '''Compute a Jacobi Conformal C{x} and C{y} projection.
972 @arg beta: Ellipsoidal latitude (C{radians} or L{Degrees}).
973 @arg omega: Ellipsoidal longitude (C{radians} or L{Degrees}).
974 @kwarg name: Optional, overriding C{B{name}="xyR2"} (C{str}).
976 @return: A L{Jacobi2Tuple}C{(x, y)}.
977 '''
978 return self.xyR2_(*(SinCos2(beta) + SinCos2(omega)),
979 name=_name__(name, name__=self.xyR2))
981 def xyR2_(self, sbeta, cbeta, somega, comega, **name):
982 '''Compute a Jacobi Conformal C{x} and C{y} projection.
984 @arg sbeta: Ellipsoidal latitude C{sin(beta)} (C{scalar}).
985 @arg cbeta: Ellipsoidal latitude C{cos(beta)} (C{scalar}).
986 @arg somega: Ellipsoidal longitude C{sin(omega)} (C{scalar}).
987 @arg comega: Ellipsoidal longitude C{cos(omega)} (C{scalar}).
988 @kwarg name: Optional, overriding C{B{name}="xyR2_"} (C{str}).
990 @return: A L{Jacobi2Tuple}C{(x, y)}.
991 '''
992 return Jacobi2Tuple(self.xR_(somega, comega),
993 self.yR_(sbeta, cbeta),
994 name=_name__(name, name__=self.xyR2_))
996 @Property_RO
997 def _yE(self):
998 '''(INTERNAL) Get the x-elliptic function.
999 '''
1000 kp2, k2 = self._k2_kp2 # swapped!
1001 # b2c2 / b2 == (b2 - c2) / b2 == 1 - c2 / b2 == e2bc
1002 return self._Elliptic(k2, self.e2bc, kp2, self._c2_b2)
1004 def yR(self, beta):
1005 '''Compute a Jacobi Conformal C{y} projection.
1007 @arg beta: Ellipsoidal latitude (C{radians} or L{Degrees}).
1009 @return: The C{y} projection (L{Radians}).
1010 '''
1011 return self.yR_(*SinCos2(beta))
1013 def yR_(self, sbeta, cbeta):
1014 '''Compute a Jacobi Conformal C{y} projection.
1016 @arg sbeta: Ellipsoidal latitude C{sin(beta)} (C{scalar}).
1017 @arg cbeta: Ellipsoidal latitude C{cos(beta)} (C{scalar}).
1019 @return: The C{y} projection (L{Radians}).
1020 '''
1021 s, c = self._norm2(sbeta, cbeta, self.c)
1022 return Radians(y=self._yE.fPi(s, c) * self._c2_b2)
1025class JacobiConformalSpherical(JacobiConformal):
1026 '''An alternate, I{spherical} L{JacobiConformal} projection.
1028 @see: L{JacobiConformal} for other and more details.
1029 '''
1030 _ab = _bc = 0
1032 def __init__(self, radius_triaxial, ab=0, bc=0, **name):
1033 '''New L{JacobiConformalSpherical}.
1035 @arg radius_triaxial: Radius (C{scalar}, conventionally in
1036 C{meter}) or an other L{JacobiConformalSpherical},
1037 L{JacobiConformal} or ordered L{Triaxial}.
1038 @kwarg ab: Relative magnitude of C{B{a} - B{b}} (C{meter},
1039 same units as C{scalar B{radius}}.
1040 @kwarg bc: Relative magnitude of C{B{b} - B{c}} (C{meter},
1041 same units as C{scalar B{radius}}.
1042 @kwarg name: Optional C{B{name}=NN} (C{str}).
1044 @raise TriaxialError: Invalid B{C{radius_triaxial}}, negative
1045 B{C{ab}}, negative B{C{bc}} or C{(B{ab}
1046 + B{bc})} not positive.
1048 @note: If B{C{radius_triaxial}} is a L{JacobiConformalSpherical}
1049 and if B{C{ab}} and B{C{bc}} are both zero or C{None},
1050 the B{C{radius_triaxial}}'s C{ab}, C{bc}, C{a}, C{b}
1051 and C{c} are copied.
1052 '''
1053 try:
1054 r = radius_triaxial
1055 if isinstance(r, Triaxial): # ordered only
1056 t = r._abc3
1057 j = isinstance(r, JacobiConformalSpherical) and not bool(ab or bc)
1058 else:
1059 t = (Radius(radius=r),) * 3
1060 j = False
1061 self._ab = r.ab if j else Scalar_(ab=ab) # low=0
1062 self._bc = r.bc if j else Scalar_(bc=bc) # low=0
1063 if (self.ab + self.bc) <= 0:
1064 raise ValueError(_negative_)
1065 a, _, c = self._abc3 = t
1066 if not (a >= c and isfinite(self._a2b2)
1067 and isfinite(self._a2c2)):
1068 raise ValueError(_not_(_finite_))
1069 except (TypeError, ValueError) as x:
1070 raise TriaxialError(radius_triaxial=r, ab=ab, bc=bc, cause=x)
1071 if name:
1072 self.name = name
1074 @Property_RO
1075 def ab(self):
1076 '''Get relative magnitude C{ab} (C{meter}, same units as B{C{a}}).
1077 '''
1078 return self._ab
1080 @Property_RO
1081 def _a2b2(self):
1082 '''(INTERNAL) Get C{a**2 - b**2} == ab * (a + b).
1083 '''
1084 a, b, _ = self._abc3
1085 return self.ab * (a + b)
1087 @Property_RO
1088 def _a2c2(self):
1089 '''(INTERNAL) Get C{a**2 - c**2} == a2b2 + b2c2.
1090 '''
1091 return self._a2b2 + self._b2c2
1093 @Property_RO
1094 def bc(self):
1095 '''Get relative magnitude C{bc} (C{meter}, same units as B{C{a}}).
1096 '''
1097 return self._bc
1099 @Property_RO
1100 def _b2c2(self):
1101 '''(INTERNAL) Get C{b**2 - c**2} == bc * (b + c).
1102 '''
1103 _, b, c = self._abc3
1104 return self.bc * (b + c)
1106 @Property_RO
1107 def radius(self):
1108 '''Get radius (C{meter}, conventionally).
1109 '''
1110 return self.a
1113class TriaxialError(_ValueError):
1114 '''Raised for L{Triaxial} issues.
1115 '''
1116 pass # ...
1119class Triaxials(_NamedEnum):
1120 '''(INTERNAL) L{Triaxial} registry, I{must} be a sub-class
1121 to accommodate the L{_LazyNamedEnumItem} properties.
1122 '''
1123 def _Lazy(self, *abc, **name):
1124 '''(INTERNAL) Instantiate the C{Triaxial}.
1125 '''
1126 a, b, c = map(km2m, abc)
1127 return Triaxial(a, b, c, **name)
1129Triaxials = Triaxials(Triaxial, Triaxial_) # PYCHOK singleton
1130'''Some pre-defined L{Triaxial}s, all I{lazily} instantiated.'''
1131# <https://ArxIV.org/pdf/1909.06452.pdf> Table 1 Semi-axes in Km
1132# <https://www.JPS.NASA.gov/education/images/pdf/ss-moons.pdf>
1133# <https://link.Springer.com/article/10.1007/s00190-022-01650-9>
1134_abc84_35 = (_EWGS84.a + 35), (_EWGS84.a - 35), _EWGS84.b
1135Triaxials._assert( # a (Km) b (Km) c (Km) planet
1136 Amalthea = _lazy('Amalthea', 125.0, 73.0, 64), # Jupiter
1137 Ariel = _lazy('Ariel', 581.1, 577.9, 577.7), # Uranus
1138 Earth = _lazy('Earth', 6378.173435, 6378.1039, 6356.7544),
1139 Enceladus = _lazy('Enceladus', 256.6, 251.4, 248.3), # Saturn
1140 Europa = _lazy('Europa', 1564.13, 1561.23, 1560.93), # Jupiter
1141 Io = _lazy('Io', 1829.4, 1819.3, 1815.7), # Jupiter
1142 Mars = _lazy('Mars', 3394.6, 3393.3, 3376.3),
1143 Mimas = _lazy('Mimas', 207.4, 196.8, 190.6), # Saturn
1144 Miranda = _lazy('Miranda', 240.4, 234.2, 232.9), # Uranus
1145 Moon = _lazy('Moon', 1735.55, 1735.324, 1734.898), # Earth
1146 Tethys = _lazy('Tethys', 535.6, 528.2, 525.8), # Saturn
1147 WGS84_35 = _lazy('WGS84_35', *map(m2km, _abc84_35)))
1148del _abc84_35, _EWGS84
1151def _getitems(items, *indices):
1152 '''(INTERNAL) Get the C{items} at the given I{indices}.
1154 @return: C{Type(items[i] for i in indices)} with
1155 C{Type = type(items)}, any C{type} having
1156 the special method C{__getitem__}.
1157 '''
1158 return type(items)(map(items.__getitem__, indices))
1161def _hartzell3(pov, los, Tun): # in .ellipsoids.hartzell4, .formy.hartzell
1162 '''(INTERNAL) Hartzell's "Satellite Line-of-Sight Intersection ...",
1163 formula from a Point-Of-View to an I{un-/ordered} Triaxial.
1164 '''
1165 def _toUvwV3d(los, pov):
1166 try: # pov must be LatLon or Cartesian if los is a Los
1167 v = los.toUvw(pov)
1168 except (AttributeError, TypeError):
1169 v = _otherV3d(los=los)
1170 return v
1172 p3 = _otherV3d(pov=pov.toCartesian() if isLatLon(pov) else pov)
1173 if los is True: # normal
1174 a, b, c, d, i = _normalTo5(p3.x, p3.y, p3.z, Tun)
1175 return type(p3)(a, b, c), d, i
1177 u3 = p3.negate() if los is False or los is None else _toUvwV3d(los, pov)
1179 a, b, c, T = Tun._ordered4
1181 a2 = a**2 # largest, factored out
1182 b2, p2 = (b**2, T._1e2ab) if b != a else (a2, _1_0)
1183 c2, q2 = (c**2, T._1e2ac) if c != a else (a2, _1_0)
1185 p3 = T._order3d(p3)
1186 u3 = T._order3d(u3).unit() # unit vector, opposing signs
1188 x2, y2, z2 = p3.x2y2z2 # p3.times_(p3).xyz
1189 ux, vy, wz = u3.times_(p3).xyz
1190 u2, v2, w2 = u3.x2y2z2 # u3.times_(u3).xyz
1192 t = (p2 * c2), c2, b2
1193 m = fdot(t, u2, v2, w2) # a2 factored out
1194 if m < EPS0: # zero or near-null LOS vector
1195 raise _ValueError(_near_(_null_))
1197 r = fsumf_(b2 * w2, c2 * v2, -v2 * z2, vy * wz * 2,
1198 -w2 * y2, -u2 * y2 * q2, -u2 * z2 * p2, ux * wz * 2 * p2,
1199 -w2 * x2 * p2, b2 * u2 * q2, -v2 * x2 * q2, ux * vy * 2 * q2)
1200 if r > 0: # a2 factored out
1201 r = sqrt(r) * b * c # == a * a * b * c / a2
1202 elif r < 0: # LOS pointing away from or missing the triaxial
1203 raise _ValueError(_opposite_ if max(ux, vy, wz) > 0 else _outside_)
1205 d = Fdot(t, ux, vy, wz).fadd_(r).fover(m) # -r for antipode, a2 factored out
1206 if d > 0: # POV inside or LOS outside or missing the triaxial
1207 s = fsumf_(_N_1_0, x2 / a2, y2 / b2, z2 / c2) # like _sideOf
1208 raise _ValueError(_outside_ if s > 0 else _inside_)
1209 elif fsum1f_(x2, y2, z2) < d**2: # d past triaxial's center
1210 raise _ValueError(_too_(_distant_))
1212 v = p3.minus(u3.times(d)) # cartesian type(pov) or Vector3d
1213 h = p3.minus(v).length # distance to pov == -d
1214 return T._order3d(v, reverse=True), h, None
1217def hartzell4(pov, los=False, tri_biax=_WGS84, **name):
1218 '''Compute the intersection of a tri-/biaxial ellipsoid and a Line-Of-Sight
1219 from a Point-Of-View outside.
1221 @arg pov: Point-Of-View outside the tri-/biaxial (C{Cartesian}, L{Ecef9Tuple}
1222 C{LatLon} or L{Vector3d}).
1223 @kwarg los: Line-Of-Sight, I{direction} to the tri-/biaxial (L{Los}, L{Vector3d}),
1224 C{True} for the I{normal, plumb} onto the surface or C{False} or
1225 C{None} to point to the center of the tri-/biaxial.
1226 @kwarg tri_biax: A triaxial (L{Triaxial}, L{Triaxial_}, L{JacobiConformal} or
1227 L{JacobiConformalSpherical}) or biaxial ellipsoid (L{Datum},
1228 L{Ellipsoid}, L{Ellipsoid2}, L{a_f2Tuple} or C{scalar} radius,
1229 conventionally in C{meter}).
1230 @kwarg name: Optional, overriding C{B{name}="hartzell4"} (C{str}).
1232 @return: L{Vector4Tuple}C{(x, y, z, h)} on the tri-/biaxial's surface, with C{h}
1233 the distance from B{C{pov}} to C{(x, y, z)} I{along the} B{C{los}}, all
1234 in C{meter}, conventionally.
1236 @raise TriaxialError: Invalid B{C{pov}} or B{C{pov}} inside the tri-/biaxial or
1237 invalid B{C{los}} or B{C{los}} points outside or away from
1238 the tri-/biaxial.
1240 @raise TypeError: Invalid B{C{tri_biax}}, C{ellipsoid} or C{datum}.
1242 @see: Class L{pygeodesy3.Los}, functions L{pygeodesy.tyr3d} and L{pygeodesy.hartzell}
1243 and U{lookAtSpheroid<https://PyPI.org/project/pymap3d>} and U{"Satellite
1244 Line-of-Sight Intersection with Earth"<https://StephenHartzell.Medium.com/
1245 satellite-line-of-sight-intersection-with-earth-d786b4a6a9b6>}.
1246 '''
1247 if isinstance(tri_biax, Triaxial_):
1248 T = tri_biax
1249 else:
1250 D = tri_biax if isinstance(tri_biax, Datum) else \
1251 _spherical_datum(tri_biax, name__=hartzell4) # _dunder_nameof
1252 T = D.ellipsoid._triaxial
1253 try:
1254 v, h, i = _hartzell3(pov, los, T)
1255 except Exception as x:
1256 raise TriaxialError(pov=pov, los=los, tri_biax=tri_biax, cause=x)
1257 return Vector4Tuple(v.x, v.y, v.z, h, iteration=i, # _dunder_nameof
1258 name=_name__(name, name__=hartzell4))
1261def _hypot21(x, y, z=0):
1262 '''(INTERNAL) Compute M{x**2 + y**2 + z**2 - 1} with C{max(fabs(x),
1263 fabs(y), fabs(z))} rarely greater than 1.0.
1264 '''
1265 return fsumf_(_1_0, x**2, y**2, (z**2 if z else _0_0), _N_2_0)
1268def _normalTo4(x, y, a, b, eps=EPS):
1269 '''(INTERNAL) Nearest point on and distance to a 2-D ellipse, I{unordered}.
1271 @see: Function C{pygeodesy.ellipsoids._normalTo3} and I{Eberly}'s U{Distance
1272 from a Point to ... an Ellipse ...<https://www.GeometricTools.com/
1273 Documentation/DistancePointEllipseEllipsoid.pdf>}.
1274 '''
1275 if b > a:
1276 b, a, d, i = _normalTo4(y, x, b, a, eps=eps)
1277 return a, b, d, i
1279 if not (b > 0 and isfinite(a)):
1280 raise _ValueError(a=a, b=b)
1282 i, _a = None, fabs
1283 if y:
1284 if x:
1285 u = _a(x / a)
1286 v = _a(y / b)
1287 g = _hypot21(u, v)
1288 if _a(g) < EPS02: # on the ellipse
1289 a, b, d = x, y, _0_0
1290 else:
1291 r = (b / a)**2
1292 t, i = _rootXd(_1_0 / r, 0, u, 0, v, g, eps)
1293 a = x / (t * r + _1_0)
1294 b = y / (t + _1_0)
1295 d = hypot(x - a, y - b)
1296 else: # x == 0
1297 if y < 0:
1298 b = -b
1299 a, d = x, _a(y - b)
1301 else: # y == 0
1302 n = a * x
1303 d = (a + b) * (a - b)
1304 if d > _a(n): # PYCHOK no cover
1305 r = n / d
1306 a *= r
1307 r = _1_0 - r**2
1308 if r > EPS02:
1309 b *= sqrt(r)
1310 d = hypot(x - a, b)
1311 else:
1312 b = _0_0
1313 d = _a(x - a)
1314 else:
1315 if x < 0:
1316 a = -a
1317 b, d = y, _a(x - a)
1318 return a, b, d, i
1321def _normalTo5(x, y, z, Tun, eps=EPS): # MCCABE 19
1322 '''(INTERNAL) Nearest point on and distance to an I{un-/ordered} triaxial.
1324 @see: I{Eberly}'s U{Distance from a Point to ... an Ellipsoid ...<https://
1325 www.GeometricTools.com/Documentation/DistancePointEllipseEllipsoid.pdf>}.
1326 '''
1327 a, b, c, T = Tun._ordered4
1328 if Tun is not T: # T is ordered, Tun isn't
1329 t = T._order3(x, y, z) + (T,)
1330 a, b, c, d, i = _normalTo5(*t, eps=eps)
1331 return T._order3(a, b, c, reverse=True) + (d, i)
1333 if not (c > 0 and isfinite(a)):
1334 raise _ValueError(a=a, b=b, c=c)
1336 if eps > 0:
1337 val = max(eps * 1e8, EPS)
1338 else: # no validation
1339 val, eps = 0, max(EPS0, -eps)
1341 i, _a = None, fabs
1342 if z:
1343 if y:
1344 if x:
1345 u = _a(x / a)
1346 v = _a(y / b)
1347 w = _a(z / c)
1348 g = _hypot21(u, v, w)
1349 if _a(g) < EPS02: # on the ellipsoid
1350 a, b, c, d = x, y, z, _0_0
1351 else:
1352 r = T._1e2ac # (c / a)**2
1353 s = T._1e2bc # (c / b)**2
1354 t, i = _rootXd(_1_0 / r, _1_0 / s, u, v, w, g, eps)
1355 a = x / (t * r + _1_0)
1356 b = y / (t * s + _1_0)
1357 c = z / (t + _1_0)
1358 d = hypot_(x - a, y - b, z - c)
1359 else: # x == 0
1360 a = x # 0
1361 b, c, d, i = _normalTo4(y, z, b, c, eps=eps)
1362 elif x: # y == 0
1363 b = y # 0
1364 a, c, d, i = _normalTo4(x, z, a, c, eps=eps)
1365 else: # x == y == 0
1366 if z < 0:
1367 c = -c
1368 a, b, d = x, y, _a(z - c)
1370 else: # z == 0
1371 t = True
1372 d = T._a2c2 # (a + c) * (a - c)
1373 n = a * x
1374 if d > _a(n):
1375 u = n / d
1376 d = T._b2c2 # (b + c) * (b - c)
1377 n = b * y
1378 if d > _a(n):
1379 v = n / d
1380 n = _hypot21(u, v)
1381 if n < 0:
1382 a *= u
1383 b *= v
1384 c *= sqrt(-n)
1385 d = hypot_(x - a, y - b, c)
1386 t = False
1387 if t:
1388 c = z # signed-0
1389 a, b, d, i = _normalTo4(x, y, a, b, eps=eps)
1391 if val > 0: # validate
1392 e = T.sideOf(a, b, c, eps=val)
1393 if e: # not near the ellipsoid's surface
1394 raise _ValueError(a=a, b=b, c=c, d=d,
1395 sideOf=e, eps=val)
1396 if d: # angle of delta and normal vector
1397 m = Vector3d(x, y, z).minus_(a, b, c)
1398 if m.euclid > val:
1399 m = m.unit()
1400 n = T.normal3d(a, b, c)
1401 e = n.dot(m) # n.negate().dot(m)
1402 if not isnear1(_a(e), eps1=val):
1403 raise _ValueError(n=n, m=m,
1404 dot=e, eps=val)
1405 return a, b, c, d, i
1408def _otherV3d_(x_xyz, y, z, **name):
1409 '''(INTERNAL) Get a Vector3d from C{x_xyz}, C{y} and C{z}.
1410 '''
1411 return Vector3d(x_xyz, y, z, **name) if isscalar(x_xyz) else \
1412 _otherV3d(x_xyz=x_xyz)
1415def _rootXd(r, s, u, v, w, g, eps):
1416 '''(INTERNAL) Robust 2d- or 3d-root finder: 2d- if C{s == v == 0} else 3d-root.
1418 @see: I{Eberly}'s U{Robust Root Finders ...<https://www.GeometricTools.com/
1419 Documentation/DistancePointEllipseEllipsoid.pdf>}.
1420 '''
1421 _1, __2 = _1_0, _0_5
1422 _a, _h21 = fabs, _hypot21
1424 u *= r
1425 v *= s # 0 for 2d-root
1426 t0 = w - _1
1427 t1 = _0_0 if g < 0 else (hypot_(u, w, v) - _1)
1428 # assert t0 <= t1
1429 for i in range(1, _TRIPS): # 48-55
1430 e = _a(t0 - t1)
1431 if e < eps:
1432 break
1433 t = (t0 + t1) * __2
1434 if t in (t0, t1):
1435 break
1436 g = _h21(u / (t + r), w / (t + _1),
1437 (v / (t + s)) if v else 0)
1438 if g > 0:
1439 t0 = t
1440 elif g < 0:
1441 t1 = t
1442 else:
1443 break
1444 else: # PYCHOK no cover
1445 t = Fmt.no_convergence(e, eps)
1446 raise _ValueError(t, txt__=_rootXd)
1447 return t, i
1450def _sideOf(xyz, abc, eps=EPS):
1451 '''(INTERNAL) Helper for C{_hartzell3}, M{.sideOf} and M{.reverseCartesian}.
1453 @return: M{sum((x / a)**2 for x, a in zip(xyz, abc)) - 1} or C{INT0}.
1454 '''
1455 s = fsumf_(_N_1_0, *((x / a)**2 for x, a in _zip(xyz, abc) if a)) # strict=True
1456 return INT0 if fabs(s) < eps else s
1459if __name__ == '__main__':
1461 from pygeodesy import printf
1462 from pygeodesy.interns import _COMMA_, _NL_, _NLATvar_
1464 # __doc__ of this file, force all into registery
1465 t = [NN] + Triaxials.toRepr(all=True, asorted=True).split(_NL_)
1466 printf(_NLATvar_.join(i.strip(_COMMA_) for i in t))
1468# **) MIT License
1469#
1470# Copyright (C) 2022-2024 -- mrJean1 at Gmail -- All Rights Reserved.
1471#
1472# Permission is hereby granted, free of charge, to any person obtaining a
1473# copy of this software and associated documentation files (the "Software"),
1474# to deal in the Software without restriction, including without limitation
1475# the rights to use, copy, modify, merge, publish, distribute, sublicense,
1476# and/or sell copies of the Software, and to permit persons to whom the
1477# Software is furnished to do so, subject to the following conditions:
1478#
1479# The above copyright notice and this permission notice shall be included
1480# in all copies or substantial portions of the Software.
1481#
1482# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
1483# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1484# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
1485# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
1486# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1487# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
1488# OTHER DEALINGS IN THE SOFTWARE.