Coverage for pygeodesy/triaxials.py: 95%

589 statements  

« prev     ^ index     » next       coverage.py v7.6.0, created at 2024-08-02 18:24 -0400

1 

2# -*- coding: utf-8 -*- 

3 

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}. 

9 

10Copyright (C) U{Charles Karney<mailto:Karney@Alum.MIT.edu>} (2008-2023). For more information, 

11see the U{GeographicLib<https://GeographicLib.SourceForge.io>} documentation. 

12 

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>}. 

16 

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 

32 

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.ellipsoids import Ellipsoid, _EWGS84 # from .datums 

39# from pygeodesy.elliptic import Elliptic # _MODS 

40# from pygeodesy.errors import _ValueError # from .basics 

41from pygeodesy.fmath import Fdot, fdot, fmean_, hypot, hypot_, norm2, sqrt0 

42from pygeodesy.fsums import _Fsumf_, fsumf_, fsum1f_ 

43from pygeodesy.interns import NN, _a_, _b_, _beta_, _c_, _distant_, _finite_, \ 

44 _height_, _inside_, _near_, _negative_, _not_, \ 

45 _NOTEQUAL_, _null_, _opposite_, _outside_, _SPACE_, \ 

46 _spherical_, _too_, _x_, _y_ 

47# from pygeodesy.lazily import _ALL_LAZY, _ALL_MODS as _MODS # from .vector3d 

48from pygeodesy.named import _lazyNamedEnumItem as _lazy, _name__, _NamedEnum, \ 

49 _NamedEnumItem, _Pass 

50from pygeodesy.namedTuples import LatLon3Tuple, _NamedTupleTo, Vector3Tuple, \ 

51 Vector4Tuple 

52from pygeodesy.props import Property_RO, property_ROver 

53# from pygeodesy.streprs import Fmt # from .datums 

54from pygeodesy.units import Degrees, Float, Height_, Meter, Meter2, Meter3, \ 

55 Radians, Radius, Scalar_ 

56from pygeodesy.utily import asin1, atan2d, km2m, m2km, SinCos2, sincos2d_ 

57from pygeodesy.vector3d import _otherV3d, Vector3d, _ALL_LAZY, _MODS 

58 

59from math import atan2, fabs, sqrt 

60 

61__all__ = _ALL_LAZY.triaxials 

62__version__ = '24.07.29' 

63 

64_not_ordered_ = _not_('ordered') 

65_omega_ = 'omega' 

66_TRIPS = 269 # 48-55, Eberly 1074? 

67 

68 

69class _NamedTupleToX(_NamedTupleTo): # in .testNamedTuples 

70 '''(INTERNAL) Base class for L{BetaOmega2Tuple}, 

71 L{BetaOmega3Tuple} and L{Jacobi2Tuple}. 

72 ''' 

73 def _toDegrees(self, name, **toDMS_kwds): 

74 '''(INTERNAL) Convert C{self[0:2]} to L{Degrees} or C{toDMS}. 

75 ''' 

76 return self._toX3U(_NamedTupleTo._Degrees3, Degrees, name, *self, **toDMS_kwds) 

77 

78 def _toRadians(self, name): 

79 '''(INTERNAL) Convert C{self[0:2]} to L{Radians}. 

80 ''' 

81 return self._toX3U(_NamedTupleTo._Radians3, Radians, name, *self) 

82 

83 def _toX3U(self, _X3, U, name, a, b, *c, **kwds): 

84 a, b, s = _X3(self, a, b, **kwds) 

85 if s is None or name: 

86 n = self._name__(name) 

87 s = self.classof(a, b, *c, name=n).reUnit(U, U).toUnits() 

88 return s 

89 

90 

91class BetaOmega2Tuple(_NamedTupleToX): 

92 '''2-Tuple C{(beta, omega)} with I{ellipsoidal} lat- and 

93 longitude C{beta} and C{omega} both in L{Radians} (or 

94 L{Degrees}). 

95 ''' 

96 _Names_ = (_beta_, _omega_) 

97 _Units_ = (_Pass, _Pass) 

98 

99 def toDegrees(self, name=NN, **toDMS_kwds): 

100 '''Convert this L{BetaOmega2Tuple} to L{Degrees} or C{toDMS}. 

101 

102 @kwarg name: Optional name (C{str}), overriding this name. 

103 

104 @return: L{BetaOmega2Tuple}C{(beta, omega)} with C{beta} and 

105 C{omega} both in L{Degrees} or as L{toDMS} strings 

106 provided some B{C{toDMS_kwds}} keyword arguments are 

107 specified. 

108 ''' 

109 return self._toDegrees(name, **toDMS_kwds) 

110 

111 def toRadians(self, **name): 

112 '''Convert this L{BetaOmega2Tuple} to L{Radians}. 

113 

114 @kwarg name: Optional C{B{name}=NN} (C{str}), overriding this name. 

115 

116 @return: L{BetaOmega2Tuple}C{(beta, omega)} with C{beta} and C{omega} 

117 both in L{Radians}. 

118 ''' 

119 return self._toRadians(name) 

120 

121 

122class BetaOmega3Tuple(_NamedTupleToX): 

123 '''3-Tuple C{(beta, omega, height)} with I{ellipsoidal} lat- and 

124 longitude C{beta} and C{omega} both in L{Radians} (or L{Degrees}) 

125 and the C{height}, rather the (signed) I{distance} to the triaxial's 

126 surface (measured along the radial line to the triaxial's center) 

127 in C{meter}, conventionally. 

128 ''' 

129 _Names_ = BetaOmega2Tuple._Names_ + (_height_,) 

130 _Units_ = BetaOmega2Tuple._Units_ + ( Meter,) 

131 

132 def toDegrees(self, name=NN, **toDMS_kwds): 

133 '''Convert this L{BetaOmega3Tuple} to L{Degrees} or C{toDMS}. 

134 

135 @kwarg name: Optional name (C{str}), overriding this name. 

136 

137 @return: L{BetaOmega3Tuple}C{(beta, omega, height)} with 

138 C{beta} and C{omega} both in L{Degrees} or as 

139 L{toDMS} strings provided some B{C{toDMS_kwds}} 

140 keyword arguments are specified. 

141 ''' 

142 return self._toDegrees(name, **toDMS_kwds) 

143 

144 def toRadians(self, **name): 

145 '''Convert this L{BetaOmega3Tuple} to L{Radians}. 

146 

147 @kwarg name: Optional C{B{name}=NN} (C{str}), overriding this name. 

148 

149 @return: L{BetaOmega3Tuple}C{(beta, omega, height)} with C{beta} 

150 and C{omega} both in L{Radians}. 

151 ''' 

152 return self._toRadians(name) 

153 

154 def to2Tuple(self, **name): 

155 '''Reduce this L{BetaOmega3Tuple} to a L{BetaOmega2Tuple}. 

156 

157 @kwarg name: Optional C{B{name}=NN} (C{str}), overriding this name. 

158 ''' 

159 return BetaOmega2Tuple(*self[:2], name=self._name__(name)) 

160 

161 

162class Jacobi2Tuple(_NamedTupleToX): 

163 '''2-Tuple C{(x, y)} with a Jacobi Conformal C{x} and C{y} 

164 projection, both in L{Radians} (or L{Degrees}). 

165 ''' 

166 _Names_ = (_x_, _y_) 

167 _Units_ = (_Pass, _Pass) 

168 

169 def toDegrees(self, name=NN, **toDMS_kwds): 

170 '''Convert this L{Jacobi2Tuple} to L{Degrees} or C{toDMS}. 

171 

172 @kwarg name: Optional name (C{str}), overriding this name. 

173 

174 @return: L{Jacobi2Tuple}C{(x, y)} with C{x} and C{y} both 

175 in L{Degrees} or as L{toDMS} strings provided some 

176 B{C{toDMS_kwds}} keyword arguments are specified. 

177 ''' 

178 return self._toDegrees(name, **toDMS_kwds) 

179 

180 def toRadians(self, **name): 

181 '''Convert this L{Jacobi2Tuple} to L{Radians}. 

182 

183 @kwarg name: Optional C{B{name}=NN} (C{str}), overriding this name. 

184 

185 @return: L{Jacobi2Tuple}C{(x, y)} with C{x} and C{y} both in L{Radians}. 

186 ''' 

187 return self._toRadians(name) 

188 

189 

190class Triaxial_(_NamedEnumItem): 

191 '''I{Unordered} triaxial ellipsoid and base class. 

192 

193 Triaxial ellipsoids with right-handed semi-axes C{a}, C{b} and C{c}, oriented 

194 such that the large principal ellipse C{ab} is the equator I{Z}=0, I{beta}=0, 

195 while the small principal ellipse C{ac} is the prime meridian, plane I{Y}=0, 

196 I{omega}=0. 

197 

198 The four umbilic points, C{abs}(I{omega}) = C{abs}(I{beta}) = C{PI/2}, lie on 

199 the middle principal ellipse C{bc} in plane I{X}=0, I{omega}=C{PI/2}. 

200 

201 @note: I{Geodetic} C{lat}- and C{lon}gitudes are in C{degrees}, I{geodetic} 

202 C{phi} and C{lam}bda are in C{radians}, but I{ellipsoidal} lat- and 

203 longitude C{beta} and C{omega} are in L{Radians} by default (or in 

204 L{Degrees} if converted). 

205 ''' 

206 _ijk = _kji = None 

207 _unordered = True 

208 

209 def __init__(self, a_triaxial, b=None, c=None, **name): 

210 '''New I{unordered} L{Triaxial_}. 

211 

212 @arg a_triaxial: Large, C{X} semi-axis (C{scalar}, conventionally in 

213 C{meter}) or an other L{Triaxial} or L{Triaxial_} instance. 

214 @kwarg b: Middle, C{Y} semi-axis (C{meter}, same units as B{C{a}}), required 

215 if C{B{a_triaxial} is scalar}, ignored otherwise. 

216 @kwarg c: Small, C{Z} semi-axis (C{meter}, B{C{b}}). 

217 @kwarg name: Optional C{B{name}=NN} (C{str}). 

218 

219 @raise TriaxialError: Invalid semi-axis or -axes. 

220 ''' 

221 try: 

222 try: 

223 a = a_triaxial 

224 t = a._abc3 

225 except AttributeError: 

226 t = Radius(a=a), Radius(b=b), Radius(c=c) 

227 except (TypeError, ValueError) as x: 

228 raise TriaxialError(a=a, b=b, c=c, cause=x) 

229 if name: 

230 self.name = name 

231 

232 a, b, c = self._abc3 = t 

233 if self._unordered: # == not isinstance(self, Triaxial) 

234 s, _, t = sorted(t) 

235 if not (isfinite(t) and s > 0): 

236 raise TriaxialError(a=a, b=b, c=c) # txt=_invalid_ 

237 elif not (isfinite(a) and a >= b >= c > 0): 

238 raise TriaxialError(a=a, b=b, c=c, txt=_not_ordered_) 

239 elif not (a > c and self._a2c2 > 0 and self.e2ac > 0): 

240 raise TriaxialError(a=a, c=c, e2ac=self.e2ac, txt=_spherical_) 

241 

242 def __str__(self): 

243 return self.toStr() 

244 

245 @Property_RO 

246 def a(self): 

247 '''Get the (largest) C{x} semi-axis (C{meter}, conventionally). 

248 ''' 

249 a, _, _ = self._abc3 

250 return a 

251 

252 @Property_RO 

253 def _a2b2(self): 

254 '''(INTERNAL) Get C{a**2 - b**2} == E_sub_e**2. 

255 ''' 

256 a, b, _ = self._abc3 

257 return ((a - b) * (a + b)) if a != b else _0_0 

258 

259 @Property_RO 

260 def _a2_b2(self): 

261 '''(INTERNAL) Get C{(a/b)**2}. 

262 ''' 

263 a, b, _ = self._abc3 

264 return (a / b)**2 if a != b else _1_0 

265 

266 @Property_RO 

267 def _a2c2(self): 

268 '''(INTERNAL) Get C{a**2 - c**2} == E_sub_x**2. 

269 ''' 

270 a, _, c = self._abc3 

271 return ((a - c) * (a + c)) if a != c else _0_0 

272 

273 @Property_RO 

274 def area(self): 

275 '''Get the surface area (C{meter} I{squared}). 

276 ''' 

277 c, b, a = sorted(self._abc3) 

278 if a > c: 

279 a = Triaxial(a, b, c).area if a > b else \ 

280 Ellipsoid(a, b=c).areax # a == b 

281 else: # a == c == b 

282 a = Meter2(area=a**2 * PI4) 

283 return a 

284 

285 def area_p(self, p=1.6075): 

286 '''I{Approximate} the surface area (C{meter} I{squared}). 

287 

288 @kwarg p: Exponent (C{scalar} > 0), 1.6 for near-spherical or 1.5849625007 

289 for "near-flat" triaxials. 

290 

291 @see: U{Surface area<https://WikiPedia.org/wiki/Ellipsoid#Approximate_formula>}. 

292 ''' 

293 a, b, c = self._abc3 

294 if a == b == c: 

295 a *= a 

296 else: 

297 _p = pow 

298 a = _p(fmean_(_p(a * b, p), _p(a * c, p), _p(b * c, p)), _1_0 / p) 

299 return Meter2(area_p=a * PI4) 

300 

301 @Property_RO 

302 def b(self): 

303 '''Get the (middle) C{y} semi-axis (C{meter}, same units as B{C{a}}). 

304 ''' 

305 _, b, _ = self._abc3 

306 return b 

307 

308 @Property_RO 

309 def _b2c2(self): 

310 '''(INTERNAL) Get C{b**2 - c**2} == E_sub_y**2. 

311 ''' 

312 _, b, c = self._abc3 

313 return ((b - c) * (b + c)) if b != c else _0_0 

314 

315 @Property_RO 

316 def c(self): 

317 '''Get the (smallest) C{z} semi-axis (C{meter}, same units as B{C{a}}). 

318 ''' 

319 _, _, c = self._abc3 

320 return c 

321 

322 @Property_RO 

323 def _c2_b2(self): 

324 '''(INTERNAL) Get C{(c/b)**2}. 

325 ''' 

326 _, b, c = self._abc3 

327 return (c / b)**2 if b != c else _1_0 

328 

329 @Property_RO 

330 def e2ab(self): 

331 '''Get the C{ab} ellipse' I{(1st) eccentricity squared} (C{scalar}), M{1 - (b/a)**2}. 

332 ''' 

333 return Float(e2ab=(_1_0 - self._1e2ab) or _0_0) 

334 

335 @Property_RO 

336 def _1e2ab(self): 

337 '''(INTERNAL) Get C{1 - e2ab} == C{(b/a)**2}. 

338 ''' 

339 a, b, _ = self._abc3 

340 return (b / a)**2 if a != b else _1_0 

341 

342 @Property_RO 

343 def e2ac(self): 

344 '''Get the C{ac} ellipse' I{(1st) eccentricity squared} (C{scalar}), M{1 - (c/a)**2}. 

345 ''' 

346 return Float(e2ac=(_1_0 - self._1e2ac) or _0_0) 

347 

348 @Property_RO 

349 def _1e2ac(self): 

350 '''(INTERNAL) Get C{1 - e2ac} == C{(c/a)**2}. 

351 ''' 

352 a, _, c = self._abc3 

353 return (c / a)**2 if a != c else _1_0 

354 

355 @Property_RO 

356 def e2bc(self): 

357 '''Get the C{bc} ellipse' I{(1st) eccentricity squared} (C{scalar}), M{1 - (c/b)**2}. 

358 ''' 

359 return Float(e2bc=(_1_0 - self._1e2bc) or _0_0) 

360 

361 _1e2bc = _c2_b2 # C{1 - e2bc} == C{(c/b)**2} 

362 

363 @property_ROver 

364 def _Elliptic(self): 

365 '''(INTERNAL) Get class L{Elliptic}, I{once}. 

366 ''' 

367 return _MODS.elliptic.Elliptic # overwrite property_ROver 

368 

369 def hartzell4(self, pov, los=False, **name): 

370 '''Compute the intersection of this triaxial's surface with a Line-Of-Sight 

371 from a Point-Of-View in space. 

372 

373 @see: Function L{hartzell4<triaxials.hartzell4>} for further details. 

374 ''' 

375 return hartzell4(pov, los=los, tri_biax=self, **name) 

376 

377 def height4(self, x_xyz, y=None, z=None, normal=True, eps=EPS, **name): 

378 '''Compute the projection on and the height above or below this triaxial's surface. 

379 

380 @arg x_xyz: X component (C{scalar}) or a cartesian (C{Cartesian}, L{Ecef9Tuple}, 

381 L{Vector3d}, L{Vector3Tuple} or L{Vector4Tuple}). 

382 @kwarg y: Y component (C{scalar}), required if B{C{x_xyz}} if C{scalar}, ignored 

383 otherwise. 

384 @kwarg z: Z component (C{scalar}), like B{C{y}}. 

385 @kwarg normal: If C{True} the projection is the I{normal, plumb} to the surface of, 

386 otherwise the C{radial} line to the center of this triaxial (C{bool}). 

387 @kwarg eps: Tolerance for root finding and validation (C{scalar}), use a negative 

388 value to skip validation. 

389 @kwarg name: Optional C{B{name}="heigh4"} (C{str}). 

390 

391 @return: L{Vector4Tuple}C{(x, y, z, h)} with the cartesian coordinates C{x}, C{y} 

392 and C{z} of the projection on or the intersection with and with the height 

393 C{h} above or below the triaxial's surface in C{meter}, conventionally. 

394 

395 @raise TriaxialError: Non-cartesian B{C{xyz}}, invalid B{C{eps}}, no convergence in 

396 root finding or validation failed. 

397 

398 @see: Method L{Ellipsoid.height4} and I{Eberly}'s U{Distance from a Point to ... 

399 <https://www.GeometricTools.com/Documentation/DistancePointEllipseEllipsoid.pdf>}. 

400 ''' 

401 v, r = _otherV3d_(x_xyz, y, z), self.isSpherical 

402 

403 i, h = None, v.length 

404 if h < EPS0: # EPS 

405 x = y = z = _0_0 

406 h -= min(self._abc3) # nearest 

407 elif r: # .isSpherical 

408 x, y, z = v.times(r / h).xyz 

409 h -= r 

410 else: 

411 x, y, z = v.xyz 

412 try: 

413 if normal: # plumb to surface 

414 x, y, z, h, i = _plumbTo5(x, y, z, self, eps=eps) 

415 else: # radial to center 

416 x, y, z = self._radialTo3(z, hypot(x, y), y, x) 

417 h = v.minus_(x, y, z).length 

418 except Exception as e: 

419 raise TriaxialError(x=x, y=y, z=z, cause=e) 

420 if h > 0 and self.sideOf(v, eps=EPS0) < 0: 

421 h = -h # below the surface 

422 n = _name__(name, name__=self.height4) 

423 return Vector4Tuple(x, y, z, h, iteration=i, name=n) 

424 

425 @Property_RO 

426 def isOrdered(self): 

427 '''Is this triaxial I{ordered} and I{not spherical} (C{bool})? 

428 ''' 

429 a, b, c = self._abc3 

430 return bool(a >= b > c) # b > c! 

431 

432 @Property_RO 

433 def isSpherical(self): 

434 '''Is this triaxial I{spherical} (C{Radius} or INT0)? 

435 ''' 

436 a, b, c = self._abc3 

437 return a if a == b == c else INT0 

438 

439 def _norm2(self, s, c, *a): 

440 '''(INTERNAL) Normalize C{s} and C{c} iff not already. 

441 ''' 

442 if fabs(_hypot21(s, c)) > EPS02: 

443 s, c = norm2(s, c) 

444 if a: 

445 s, c = norm2(s * self.b, c * a[0]) 

446 return float0_(s, c) 

447 

448 def normal3d(self, x_xyz, y=None, z=None, length=_1_0): 

449 '''Get a 3-D vector at a cartesian on and perpendicular to this triaxial's surface. 

450 

451 @arg x_xyz: X component (C{scalar}) or a cartesian (C{Cartesian}, 

452 L{Ecef9Tuple}, L{Vector3d}, L{Vector3Tuple} or L{Vector4Tuple}). 

453 @kwarg y: Y component (C{scalar}), required if B{C{x_xyz}} if C{scalar}, ignored 

454 otherwise. 

455 @kwarg z: Z component (C{scalar}), like B{C{y}}. 

456 @kwarg length: Optional length and in-/outward direction (C{scalar}). 

457 

458 @return: A C{Vector3d(x_, y_, z_)} normalized to B{C{length}}, pointing 

459 in- or outward for neg- respectively positive B{C{length}}. 

460 

461 @raise TriaxialError: Zero length cartesian or vector. 

462 

463 @note: Cartesian location C{(B{x}, B{y}, B{z})} must be on this triaxial's 

464 surface, use method L{Triaxial.sideOf} to validate. 

465 ''' 

466 # n = 2 * (x / a2, y / b2, z / c2) 

467 # == 2 * (x, y * a2 / b2, z * a2 / c2) / a2 # iff ordered 

468 # == 2 * (x, y / _1e2ab, z / _1e2ac) / a2 

469 # == unit(x, y / _1e2ab, z / _1e2ac).times(length) 

470 n = self._normal3d.times_(*_otherV3d_(x_xyz, y, z).xyz) 

471 if n.length < EPS0: 

472 raise TriaxialError(x=x_xyz, y=y, z=z, txt=_null_) 

473 return n.times(length / n.length) 

474 

475 @Property_RO 

476 def _normal3d(self): 

477 '''(INTERNAL) Get M{Vector3d((d/a)**2, (d/b)**2, (d/c)**2)}, M{d = max(a, b, c)}. 

478 ''' 

479 d = max(self._abc3) 

480 t = tuple(((d / x)**2 if x != d else _1_0) for x in self._abc3) 

481 return Vector3d(*t, name__=self.normal3d) 

482 

483 def _order3(self, *abc, **reverse): # reverse=False 

484 '''(INTERNAL) Un-/Order C{a}, C{b} and C{c}. 

485 

486 @return: 3-Tuple C{(a, b, c)} ordered by or un-ordered 

487 (reverse-ordered) C{ijk} if C{B{reverse}=True}. 

488 ''' 

489 ijk = self._order_ijk(**reverse) 

490 return _getitems(abc, *ijk) if ijk else abc 

491 

492 def _order3d(self, v, **reverse): # reverse=False 

493 '''(INTERNAL) Un-/Order a C{Vector3d}. 

494 

495 @return: Vector3d(x, y, z) un-/ordered. 

496 ''' 

497 ijk = self._order_ijk(**reverse) 

498 return v.classof(*_getitems(v.xyz, *ijk)) if ijk else v 

499 

500 @Property_RO 

501 def _ordered4(self): 

502 '''(INTERNAL) Helper for C{_hartzell3} and C{_plumbTo5}. 

503 ''' 

504 def _order2(reverse, a, b, c): 

505 '''(INTERNAL) Un-Order C{a}, C{b} and C{c}. 

506 

507 @return: 2-Tuple C{((a, b, c), ijk)} with C{a} >= C{b} >= C{c} 

508 and C{ijk} a 3-tuple with the initial indices. 

509 ''' 

510 i, j, k = 0, 1, 2 # range(3) 

511 if a < b: 

512 a, b, i, j = b, a, j, i 

513 if a < c: 

514 a, c, i, k = c, a, k, i 

515 if b < c: 

516 b, c, j, k = c, b, k, j 

517 # reverse (k, j, i) since (a, b, c) is reversed-sorted 

518 ijk = (k, j, i) if reverse else (None if i < j < k else (i, j, k)) 

519 return (a, b, c), ijk 

520 

521 abc, T = self._abc3, self 

522 if not self.isOrdered: 

523 abc, ijk = _order2(False, *abc) 

524 if ijk: 

525 _, kji = _order2(True, *ijk) 

526 T = Triaxial_(*abc) 

527 T._ijk, T._kji = ijk, kji 

528 return abc + (T,) 

529 

530 def _order_ijk(self, reverse=False): 

531 '''(INTERNAL) Get the un-/order indices. 

532 ''' 

533 return self._kji if reverse else self._ijk 

534 

535 def _radialTo3(self, sbeta, cbeta, somega, comega): 

536 '''(INTERNAL) I{Unordered} helper for C{.height4}. 

537 ''' 

538 def _rphi(a, b, sphi, cphi): 

539 # <https://WikiPedia.org/wiki/Ellipse#Polar_form_relative_to_focus> 

540 # polar form: radius(phi) = a * b / hypot(a * sphi, b * cphi) 

541 return (b / hypot(sphi, b / a * cphi)) if a > b else ( 

542 (a / hypot(cphi, a / b * sphi)) if a < b else a) 

543 

544 sa, ca = self._norm2(sbeta, cbeta) 

545 sb, cb = self._norm2(somega, comega) 

546 

547 a, b, c = self._abc3 

548 if a != b: 

549 a = _rphi(a, b, sb, cb) 

550 if a != c: 

551 c = _rphi(a, c, sa, ca) 

552 z, r = c * sa, c * ca 

553 x, y = r * cb, r * sb 

554 return x, y, z 

555 

556 def sideOf(self, x_xyz, y=None, z=None, eps=EPS4): 

557 '''Is a cartesian above, below or on the surface of this triaxial? 

558 

559 @arg x_xyz: X component (C{scalar}) or a cartesian (C{Cartesian}, 

560 L{Ecef9Tuple}, L{Vector3d}, L{Vector3Tuple} or L{Vector4Tuple}). 

561 @kwarg y: Y component (C{scalar}), required if B{C{x_xyz}} if C{scalar}, 

562 ignored otherwise. 

563 @kwarg z: Z component (C{scalar}), like B{C{y}}. 

564 @kwarg eps: Near-surface tolerance (C{scalar}, distance I{squared}). 

565 

566 @return: C{INT0} if C{(B{x}, B{y}, B{z})} is near this triaxial's surface 

567 within tolerance B{C{eps}}, otherwise a signed, radial, normalized 

568 distance I{squared} (C{float}), negative or positive for in- 

569 respectively outside this triaxial. 

570 

571 @see: Methods L{Triaxial.height4} and L{Triaxial.normal3d}. 

572 ''' 

573 return _sideOf(_otherV3d_(x_xyz, y, z).xyz, self._abc3, eps=eps) 

574 

575 def toEllipsoid(self, **name): 

576 '''Convert this triaxial to an L{Ellipsoid}, provided 2 axes match. 

577 

578 @kwarg name: Optional, overriding C{B{name}=NN} (C{str})=. 

579 

580 @return: An L{Ellipsoid} with north along this C{Z} axis if C{a == b}, 

581 this C{Y} axis if C{a == c} or this C{X} axis if C{b == c}. 

582 

583 @raise TriaxialError: This C{a != b}, C{b != c} and C{c != a}. 

584 

585 @see: Method L{Ellipsoid.toTriaxial}. 

586 ''' 

587 a, b, c = self._abc3 

588 if a == b: 

589 b = c # N = c-Z 

590 elif b == c: # N = a-X 

591 a, b = b, a 

592 elif a != c: # N = b-Y 

593 t = _SPACE_(_a_, _NOTEQUAL_, _b_, _NOTEQUAL_, _c_) 

594 raise TriaxialError(a=a, b=b, c=c, txt=t) 

595 return Ellipsoid(a, b=b, name=self._name__(name)) 

596 

597 def toStr(self, prec=9, **name): # PYCHOK signature 

598 '''Return this C{Triaxial} as a string. 

599 

600 @kwarg prec: Precision, number of decimal digits (0..9). 

601 @kwarg name: Optional, overriding C{B{name}=NN} (C{str}) 

602 or C{None} to exclude this triaxial's name. 

603 

604 @return: This C{Triaxial}'s attributes (C{str}). 

605 ''' 

606 T = Triaxial_ 

607 t = T.a, 

608 J = JacobiConformalSpherical 

609 t += (J.ab, J.bc) if isinstance(self, J) else (T.b, T.c) 

610 t += T.e2ab, T.e2bc, T.e2ac 

611 J = JacobiConformal 

612 if isinstance(self, J): 

613 t += J.xyQ2, 

614 t += T.volume, T.area 

615 return self._instr(area_p=self.area_p(), prec=prec, props=t, **name) 

616 

617 @Property_RO 

618 def volume(self): 

619 '''Get the volume (C{meter**3}), M{4 / 3 * PI * a * b * c}. 

620 ''' 

621 a, b, c = self._abc3 

622 return Meter3(volume=a * b * c * PI_3 * _4_0) 

623 

624 

625class Triaxial(Triaxial_): 

626 '''I{Ordered} triaxial ellipsoid. 

627 

628 @see: L{Triaxial_} for more information. 

629 ''' 

630 _unordered = False 

631 

632 def __init__(self, a_triaxial, b=None, c=None, **name): 

633 '''New I{ordered} L{Triaxial}. 

634 

635 @arg a_triaxial: Largest semi-axis (C{scalar}, conventionally in C{meter}) 

636 or an other L{Triaxial} or L{Triaxial_} instance. 

637 @kwarg b: Middle semi-axis (C{meter}, same units as B{C{a}}), required 

638 if C{B{a_triaxial} is scalar}, ignored otherwise. 

639 @kwarg c: Smallest semi-axis (C{meter}, like B{C{b}}). 

640 @kwarg name: Optional C{B{name}=NN} (C{str}). 

641 

642 @note: The semi-axes must be ordered as C{B{a} >= B{b} >= B{c} > 0} and 

643 must be ellipsoidal, C{B{a} > B{c}}. 

644 

645 @raise TriaxialError: Semi-axes unordered, spherical or invalid. 

646 ''' 

647 Triaxial_.__init__(self, a_triaxial, b=b, c=c, **name) 

648 

649 @Property_RO 

650 def _a2b2_a2c2(self): 

651 '''@see: Methods C{.forwardBetaOmega} and C{._k2_kp2}. 

652 ''' 

653 return self._a2b2 / self._a2c2 

654 

655 @Property_RO 

656 def area(self): 

657 '''Get the surface area (C{meter} I{squared}). 

658 

659 @see: U{Surface area<https://WikiPedia.org/wiki/Ellipsoid#Surface_area>}. 

660 ''' 

661 a, b, c = self._abc3 

662 if a != b: 

663 kp2, k2 = self._k2_kp2 # swapped! 

664 aE = self._Elliptic(k2, _0_0, kp2, _1_0) 

665 c2 = self._1e2ac # cos(phi)**2 = (c/a)**2 

666 s = sqrt(self.e2ac) # sin(phi)**2 = 1 - c2 

667 r = asin1(s) # phi = atan2(sqrt(c2), s) 

668 b *= fsum1f_(aE.fE(r) * s, c / a * c / b, 

669 aE.fF(r) * c2 / s) 

670 a = Meter2(area=a * b * PI2) 

671 else: # a == b > c 

672 a = Ellipsoid(a, b=c).areax 

673 return a 

674 

675 def _exyz3(self, u): 

676 '''(INTERNAL) Helper for C{.forwardBetOmg}. 

677 ''' 

678 if u > 0: 

679 u2 = u**2 

680 x = u * sqrt0(_1_0 + self._a2c2 / u2, Error=TriaxialError) 

681 y = u * sqrt0(_1_0 + self._b2c2 / u2, Error=TriaxialError) 

682 else: 

683 x = y = u = _0_0 

684 return x, y, u 

685 

686 def forwardBetaOmega(self, beta, omega, height=0, **name): 

687 '''Convert I{ellipsoidal} lat- and longitude C{beta}, C{omega} 

688 and height to cartesian. 

689 

690 @arg beta: Ellipsoidal latitude (C{radians} or L{Degrees}). 

691 @arg omega: Ellipsoidal longitude (C{radians} or L{Degrees}). 

692 @arg height: Height above or below the ellipsoid's surface (C{meter}, same 

693 units as this triaxial's C{a}, C{b} and C{c} semi-axes). 

694 @kwarg name: Optional C{B{name}=NN} (C{str}). 

695 

696 @return: A L{Vector3Tuple}C{(x, y, z)}. 

697 

698 @see: Method L{Triaxial.reverseBetaOmega} and U{Expressions (23-25)<https:// 

699 www.Topo.Auth.GR/wp-content/uploads/sites/111/2021/12/09_Panou.pdf>}. 

700 ''' 

701 if height: 

702 h = self._Height(height) 

703 x, y, z = self._exyz3(h + self.c) 

704 else: 

705 x, y, z = self._abc3 # == self._exyz3(self.c) 

706 if z: # and x and y: 

707 sa, ca = SinCos2(beta) 

708 sb, cb = SinCos2(omega) 

709 

710 r = self._a2b2_a2c2 

711 x *= cb * sqrt0(ca**2 + r * sa**2, Error=TriaxialError) 

712 y *= ca * sb 

713 z *= sa * sqrt0(_1_0 - r * cb**2, Error=TriaxialError) 

714 return Vector3Tuple(x, y, z, **name) 

715 

716 def forwardBetaOmega_(self, sbeta, cbeta, somega, comega, **name): 

717 '''Convert I{ellipsoidal} lat- and longitude C{beta} and C{omega} 

718 to cartesian coordinates I{on the triaxial's surface}. 

719 

720 @arg sbeta: Ellipsoidal latitude C{sin(beta)} (C{scalar}). 

721 @arg cbeta: Ellipsoidal latitude C{cos(beta)} (C{scalar}). 

722 @arg somega: Ellipsoidal longitude C{sin(omega)} (C{scalar}). 

723 @arg comega: Ellipsoidal longitude C{cos(omega)} (C{scalar}). 

724 @kwarg name: Optional C{B{name}=NN} (C{str}). 

725 

726 @return: A L{Vector3Tuple}C{(x, y, z)} on the surface. 

727 

728 @raise TriaxialError: This triaxial is near-spherical. 

729 

730 @see: Method L{Triaxial.reverseBetaOmega}, U{Triaxial ellipsoid coordinate 

731 system<https://WikiPedia.org/wiki/Geodesics_on_an_ellipsoid# 

732 Triaxial_ellipsoid_coordinate_system>} and U{expressions (23-25)<https:// 

733 www.Topo.Auth.GR/wp-content/uploads/sites/111/2021/12/09_Panou.pdf>}. 

734 ''' 

735 t = self._radialTo3(sbeta, cbeta, somega, comega) 

736 return Vector3Tuple(*t, **name) 

737 

738 def forwardCartesian(self, x_xyz, y=None, z=None, **normal_eps_name): 

739 '''Project a cartesian on this triaxial. 

740 

741 @arg x_xyz: X component (C{scalar}) or a cartesian (C{Cartesian}, 

742 L{Ecef9Tuple}, L{Vector3d}, L{Vector3Tuple} or L{Vector4Tuple}). 

743 @kwarg y: Y component (C{scalar}), required if B{C{x_xyz}} if C{scalar}, 

744 ignored otherwise. 

745 @kwarg z: Z component (C{scalar}), like B{C{y}}. 

746 @kwarg normal_eps_name: Optional keyword arguments C{B{normal}=True}, 

747 C{B{eps}=EPS} and overriding C{B{name}="height4"} (C{str}), 

748 see method L{Triaxial.height4}. 

749 

750 @see: Method L{Triaxial.height4} for further information and method 

751 L{Triaxial.reverseCartesian} to reverse the projection. 

752 ''' 

753 return self.height4(x_xyz, y, z, **normal_eps_name) 

754 

755 def forwardLatLon(self, lat, lon, height=0, **name): 

756 '''Convert I{geodetic} lat-, longitude and height to cartesian. 

757 

758 @arg lat: Geodetic latitude (C{degrees}). 

759 @arg lon: Geodetic longitude (C{degrees}). 

760 @arg height: Height above the ellipsoid (C{meter}, same units 

761 as this triaxial's C{a}, C{b} and C{c} axes). 

762 @kwarg name: Optional C{B{name}=NN} (C{str}). 

763 

764 @return: A L{Vector3Tuple}C{(x, y, z)}. 

765 

766 @see: Method L{Triaxial.reverseLatLon} and U{Expressions (9-11)<https:// 

767 www.Topo.Auth.GR/wp-content/uploads/sites/111/2021/12/09_Panou.pdf>}. 

768 ''' 

769 return self._forwardLatLon3(height, name, *sincos2d_(lat, lon)) 

770 

771 def forwardLatLon_(self, slat, clat, slon, clon, height=0, **name): 

772 '''Convert I{geodetic} lat-, longitude and height to cartesian. 

773 

774 @arg slat: Geodetic latitude C{sin(lat)} (C{scalar}). 

775 @arg clat: Geodetic latitude C{cos(lat)} (C{scalar}). 

776 @arg slon: Geodetic longitude C{sin(lon)} (C{scalar}). 

777 @arg clon: Geodetic longitude C{cos(lon)} (C{scalar}). 

778 @arg height: Height above the ellipsoid (C{meter}, same units 

779 as this triaxial's axes C{a}, C{b} and C{c}). 

780 @kwarg name: Optional C{B{name}=NN} (C{str}). 

781 

782 @return: A L{Vector3Tuple}C{(x, y, z)}. 

783 

784 @see: Method L{Triaxial.reverseLatLon} and U{Expressions (9-11)<https:// 

785 www.Topo.Auth.GR/wp-content/uploads/sites/111/2021/12/09_Panou.pdf>}. 

786 ''' 

787 sa, ca = self._norm2(slat, clat) 

788 sb, cb = self._norm2(slon, clon) 

789 return self._forwardLatLon3(height, name, sa, ca, sb, cb) 

790 

791 def _forwardLatLon3(self, height, name, sa, ca, sb, cb): # name always **name 

792 '''(INTERNAL) Helper for C{.forwardLatLon} and C{.forwardLatLon_}. 

793 ''' 

794 ca_x_sb = ca * sb 

795 h = self._Height(height) 

796 # 1 - (1 - (c/a)**2) * sa**2 - (1 - (b/a)**2) * ca**2 * sb**2 

797 t = fsumf_(_1_0, -self.e2ac * sa**2, -self.e2ab * ca_x_sb**2) 

798 n = self.a / sqrt0(t, Error=TriaxialError) # prime vertical 

799 x = (h + n) * ca * cb 

800 y = (h + n * self._1e2ab) * ca_x_sb 

801 z = (h + n * self._1e2ac) * sa 

802 return Vector3Tuple(x, y, z, **name) 

803 

804 def _Height(self, height): 

805 '''(INTERNAL) Validate a C{height}. 

806 ''' 

807 return Height_(height=height, low=-self.c, Error=TriaxialError) 

808 

809 @Property_RO 

810 def _k2_kp2(self): 

811 '''(INTERNAL) Get C{k2} and C{kp2} for C{._xE}, C{._yE} and C{.area}. 

812 ''' 

813 # k2 = a2b2 / a2c2 * c2_b2 

814 # kp2 = b2c2 / a2c2 * a2_b2 

815 # b2 = b**2 

816 # xE = Elliptic(k2, -a2b2 / b2, kp2, a2_b2) 

817 # yE = Elliptic(kp2, +b2c2 / b2, k2, c2_b2) 

818 # aE = Elliptic(kp2, 0, k2, 1) 

819 return (self._a2b2_a2c2 * self._c2_b2, 

820 self._b2c2 / self._a2c2 * self._a2_b2) 

821 

822 def _radialTo3(self, sbeta, cbeta, somega, comega): 

823 '''(INTERNAL) Convert I{ellipsoidal} lat- and longitude C{beta} and 

824 C{omega} to cartesian coordinates I{on the triaxial's surface}, 

825 also I{ordered} helper for C{.height4}. 

826 ''' 

827 sa, ca = self._norm2(sbeta, cbeta) 

828 sb, cb = self._norm2(somega, comega) 

829 

830 b2_a2 = self._1e2ab # == (b/a)**2 

831 c2_a2 = -self._1e2ac # == -(c/a)**2 

832 a2c2_a2 = self. e2ac # (a**2 - c**2) / a**2 == 1 - (c/a)**2 

833 

834 x2 = _Fsumf_(_1_0, -b2_a2 * sa**2, c2_a2 * ca**2).fover(a2c2_a2) 

835 z2 = _Fsumf_(c2_a2, sb**2, b2_a2 * cb**2).fover(a2c2_a2) 

836 

837 x, y, z = self._abc3 

838 x *= cb * sqrt0(x2, Error=TriaxialError) 

839 y *= ca * sb 

840 z *= sa * sqrt0(z2, Error=TriaxialError) 

841 return x, y, z 

842 

843 def reverseBetaOmega(self, x_xyz, y=None, z=None, **name): 

844 '''Convert cartesian to I{ellipsoidal} lat- and longitude, C{beta}, C{omega} 

845 and height. 

846 

847 @arg x_xyz: X component (C{scalar}) or a cartesian (C{Cartesian}, 

848 L{Ecef9Tuple}, L{Vector3d}, L{Vector3Tuple} or L{Vector4Tuple}). 

849 @kwarg y: Y component (C{scalar}), required if B{C{x_xyz}} if C{scalar}, 

850 ignored otherwise. 

851 @kwarg z: Z component (C{scalar}), like B{C{y}}. 

852 @kwarg name: Optional C{B{name}=NN} (C{str}). 

853 

854 @return: A L{BetaOmega3Tuple}C{(beta, omega, height)} with C{beta} and 

855 C{omega} in L{Radians} and (radial) C{height} in C{meter}, same 

856 units as this triaxial's axes. 

857 

858 @see: Methods L{Triaxial.forwardBetaOmega} and L{Triaxial.forwardBetaOmega_} 

859 and U{Expressions (21-22)<https://www.Topo.Auth.GR/wp-content/uploads/ 

860 sites/111/2021/12/09_Panou.pdf>}. 

861 ''' 

862 v = _otherV3d_(x_xyz, y, z) 

863 a, b, h = self._reverseLatLon3(v, atan2, v, self.forwardBetaOmega_) 

864 return BetaOmega3Tuple(Radians(beta=a), Radians(omega=b), h, **name) 

865 

866 def reverseCartesian(self, x_xyz, y=None, z=None, h=0, normal=True, eps=_EPS2e4, **name): 

867 '''"Unproject" a cartesian on to a cartesion I{off} this triaxial's surface. 

868 

869 @arg x_xyz: X component (C{scalar}) or a cartesian (C{Cartesian}, 

870 L{Ecef9Tuple}, L{Vector3d}, L{Vector3Tuple} or L{Vector4Tuple}). 

871 @kwarg y: Y component (C{scalar}), required if B{C{x_xyz}} if C{scalar}, 

872 ignored otherwise. 

873 @kwarg z: Z component (C{scalar}), like B{C{y}}. 

874 @arg h: Height above or below this triaxial's surface (C{meter}, same units 

875 as the axes). 

876 @kwarg normal: If C{True} the height is C{normal} to the surface, otherwise 

877 C{radially} to the center of this triaxial (C{bool}). 

878 @kwarg eps: Tolerance for surface test (C{scalar}). 

879 @kwarg name: Optional C{B{name}=NN} (C{str}). 

880 

881 @return: A L{Vector3Tuple}C{(x, y, z)}. 

882 

883 @raise TrialError: Cartesian C{(x, y, z)} not on this triaxial's surface. 

884 

885 @see: Methods L{Triaxial.forwardCartesian} and L{Triaxial.height4}. 

886 ''' 

887 v = _otherV3d_(x_xyz, y, z, **name) 

888 s = _sideOf(v.xyz, self._abc3, eps=eps) 

889 if s: # PYCHOK no cover 

890 t = _SPACE_((_inside_ if s < 0 else _outside_), self.toRepr()) 

891 raise TriaxialError(eps=eps, sideOf=s, x=v.x, y=v.y, z=v.z, txt=t) 

892 

893 if h: 

894 if normal: 

895 v = v.plus(self.normal3d(*v.xyz, length=h)) 

896 elif v.length > EPS0: 

897 v = v.times(_1_0 + (h / v.length)) 

898 return v.xyz # Vector3Tuple 

899 

900 def reverseLatLon(self, x_xyz, y=None, z=None, **name): 

901 '''Convert cartesian to I{geodetic} lat-, longitude and height. 

902 

903 @arg x_xyz: X component (C{scalar}) or a cartesian (C{Cartesian}, 

904 L{Ecef9Tuple}, L{Vector3d}, L{Vector3Tuple} or L{Vector4Tuple}). 

905 @kwarg y: Y component (C{scalar}), required if B{C{x_xyz}} if C{scalar}, 

906 ignored otherwise. 

907 @kwarg z: Z component (C{scalar}), like B{C{y}}. 

908 @kwarg name: Optional C{B{name}=NN} (C{str}). 

909 

910 @return: A L{LatLon3Tuple}C{(lat, lon, height)} with C{lat} and C{lon} 

911 in C{degrees} and (radial) C{height} in C{meter}, same units 

912 as this triaxial's axes. 

913 

914 @see: Methods L{Triaxial.forwardLatLon} and L{Triaxial.forwardLatLon_} 

915 and U{Expressions (4-5)<https://www.Topo.Auth.GR/wp-content/uploads/ 

916 sites/111/2021/12/09_Panou.pdf>}. 

917 ''' 

918 v = _otherV3d_(x_xyz, y, z) 

919 s = v.times_(self._1e2ac, # == 1 - e_sub_x**2 

920 self._1e2bc, # == 1 - e_sub_y**2 

921 _1_0) 

922 a, b, h = self._reverseLatLon3(s, atan2d, v, self.forwardLatLon_) 

923 return LatLon3Tuple(Degrees(lat=a), Degrees(lon=b), h, **name) 

924 

925 def _reverseLatLon3(self, s, atan2_, v, forward_): 

926 '''(INTERNAL) Helper for C{.reverseBetOmg} and C{.reverseLatLon}. 

927 ''' 

928 x, y, z = s.xyz 

929 d = hypot( x, y) 

930 a = atan2_(z, d) 

931 b = atan2_(y, x) 

932 h = v.minus_(*forward_(z, d, y, x)).length 

933 return a, b, h 

934 

935 

936class JacobiConformal(Triaxial): 

937 '''This is a conformal projection of a triaxial ellipsoid to a plane in which the 

938 C{X} and C{Y} grid lines are straight. 

939 

940 Ellipsoidal coordinates I{beta} and I{omega} are converted to Jacobi Conformal 

941 I{y} respectively I{x} separately. Jacobi's coordinates have been multiplied 

942 by C{sqrt(B{a}**2 - B{c}**2) / (2 * B{b})} so that the customary results are 

943 returned in the case of an ellipsoid of revolution. 

944 

945 Copyright (C) U{Charles Karney<mailto:Karney@Alum.MIT.edu>} (2014-2023) and 

946 licensed under the MIT/X11 License. 

947 

948 @note: This constructor can I{not be used to specify a sphere}, see alternate 

949 L{JacobiConformalSpherical}. 

950 

951 @see: L{Triaxial}, C++ class U{JacobiConformal<https://GeographicLib.SourceForge.io/ 

952 C++/doc/classGeographicLib_1_1JacobiConformal.html#details>}, U{Jacobi's conformal 

953 projection<https://GeographicLib.SourceForge.io/C++/doc/jacobi.html>} and Jacobi, 

954 C. G. J. I{U{Vorlesungen über Dynamik<https://Books.Google.com/books? 

955 id=ryEOAAAAQAAJ&pg=PA212>}}, page 212ff. 

956 ''' 

957 

958 @Property_RO 

959 def _xE(self): 

960 '''(INTERNAL) Get the x-elliptic function. 

961 ''' 

962 k2, kp2 = self._k2_kp2 

963 # -a2b2 / b2 == (b2 - a2) / b2 == 1 - a2 / b2 == 1 - a2_b2 

964 return self._Elliptic(k2, _1_0 - self._a2_b2, kp2, self._a2_b2) 

965 

966 def xR(self, omega): 

967 '''Compute a Jacobi Conformal C{x} projection. 

968 

969 @arg omega: Ellipsoidal longitude (C{radians} or L{Degrees}). 

970 

971 @return: The C{x} projection (L{Radians}). 

972 ''' 

973 return self.xR_(*SinCos2(omega)) 

974 

975 def xR_(self, somega, comega): 

976 '''Compute a Jacobi Conformal C{x} projection. 

977 

978 @arg somega: Ellipsoidal longitude C{sin(omega)} (C{scalar}). 

979 @arg comega: Ellipsoidal longitude C{cos(omega)} (C{scalar}). 

980 

981 @return: The C{x} projection (L{Radians}). 

982 ''' 

983 s, c = self._norm2(somega, comega, self.a) 

984 return Radians(x=self._xE.fPi(s, c) * self._a2_b2) 

985 

986 @Property_RO 

987 def xyQ2(self): 

988 '''Get the Jacobi Conformal quadrant size (L{Jacobi2Tuple}C{(x, y)}). 

989 ''' 

990 return Jacobi2Tuple(Radians(x=self._a2_b2 * self._xE.cPi), 

991 Radians(y=self._c2_b2 * self._yE.cPi), 

992 name=JacobiConformal.xyQ2.name) 

993 

994 def xyR2(self, beta, omega, **name): 

995 '''Compute a Jacobi Conformal C{x} and C{y} projection. 

996 

997 @arg beta: Ellipsoidal latitude (C{radians} or L{Degrees}). 

998 @arg omega: Ellipsoidal longitude (C{radians} or L{Degrees}). 

999 @kwarg name: Optional, overriding C{B{name}="xyR2"} (C{str}). 

1000 

1001 @return: A L{Jacobi2Tuple}C{(x, y)}. 

1002 ''' 

1003 return self.xyR2_(*(SinCos2(beta) + SinCos2(omega)), 

1004 name=_name__(name, name__=self.xyR2)) 

1005 

1006 def xyR2_(self, sbeta, cbeta, somega, comega, **name): 

1007 '''Compute a Jacobi Conformal C{x} and C{y} projection. 

1008 

1009 @arg sbeta: Ellipsoidal latitude C{sin(beta)} (C{scalar}). 

1010 @arg cbeta: Ellipsoidal latitude C{cos(beta)} (C{scalar}). 

1011 @arg somega: Ellipsoidal longitude C{sin(omega)} (C{scalar}). 

1012 @arg comega: Ellipsoidal longitude C{cos(omega)} (C{scalar}). 

1013 @kwarg name: Optional, overriding C{B{name}="xyR2_"} (C{str}). 

1014 

1015 @return: A L{Jacobi2Tuple}C{(x, y)}. 

1016 ''' 

1017 return Jacobi2Tuple(self.xR_(somega, comega), 

1018 self.yR_(sbeta, cbeta), 

1019 name=_name__(name, name__=self.xyR2_)) 

1020 

1021 @Property_RO 

1022 def _yE(self): 

1023 '''(INTERNAL) Get the x-elliptic function. 

1024 ''' 

1025 kp2, k2 = self._k2_kp2 # swapped! 

1026 # b2c2 / b2 == (b2 - c2) / b2 == 1 - c2 / b2 == e2bc 

1027 return self._Elliptic(k2, self.e2bc, kp2, self._c2_b2) 

1028 

1029 def yR(self, beta): 

1030 '''Compute a Jacobi Conformal C{y} projection. 

1031 

1032 @arg beta: Ellipsoidal latitude (C{radians} or L{Degrees}). 

1033 

1034 @return: The C{y} projection (L{Radians}). 

1035 ''' 

1036 return self.yR_(*SinCos2(beta)) 

1037 

1038 def yR_(self, sbeta, cbeta): 

1039 '''Compute a Jacobi Conformal C{y} projection. 

1040 

1041 @arg sbeta: Ellipsoidal latitude C{sin(beta)} (C{scalar}). 

1042 @arg cbeta: Ellipsoidal latitude C{cos(beta)} (C{scalar}). 

1043 

1044 @return: The C{y} projection (L{Radians}). 

1045 ''' 

1046 s, c = self._norm2(sbeta, cbeta, self.c) 

1047 return Radians(y=self._yE.fPi(s, c) * self._c2_b2) 

1048 

1049 

1050class JacobiConformalSpherical(JacobiConformal): 

1051 '''An alternate, I{spherical} L{JacobiConformal} projection. 

1052 

1053 @see: L{JacobiConformal} for other and more details. 

1054 ''' 

1055 _ab = _bc = 0 

1056 

1057 def __init__(self, radius_triaxial, ab=0, bc=0, **name): 

1058 '''New L{JacobiConformalSpherical}. 

1059 

1060 @arg radius_triaxial: Radius (C{scalar}, conventionally in 

1061 C{meter}) or an other L{JacobiConformalSpherical}, 

1062 L{JacobiConformal} or ordered L{Triaxial}. 

1063 @kwarg ab: Relative magnitude of C{B{a} - B{b}} (C{meter}, 

1064 same units as C{scalar B{radius}}. 

1065 @kwarg bc: Relative magnitude of C{B{b} - B{c}} (C{meter}, 

1066 same units as C{scalar B{radius}}. 

1067 @kwarg name: Optional C{B{name}=NN} (C{str}). 

1068 

1069 @raise TriaxialError: Invalid B{C{radius_triaxial}}, negative 

1070 B{C{ab}}, negative B{C{bc}} or C{(B{ab} 

1071 + B{bc})} not positive. 

1072 

1073 @note: If B{C{radius_triaxial}} is a L{JacobiConformalSpherical} 

1074 and if B{C{ab}} and B{C{bc}} are both zero or C{None}, 

1075 the B{C{radius_triaxial}}'s C{ab}, C{bc}, C{a}, C{b} 

1076 and C{c} are copied. 

1077 ''' 

1078 try: 

1079 r = radius_triaxial 

1080 if isinstance(r, Triaxial): # ordered only 

1081 t = r._abc3 

1082 j = isinstance(r, JacobiConformalSpherical) and not bool(ab or bc) 

1083 else: 

1084 t = (Radius(radius=r),) * 3 

1085 j = False 

1086 self._ab = r.ab if j else Scalar_(ab=ab) # low=0 

1087 self._bc = r.bc if j else Scalar_(bc=bc) # low=0 

1088 if (self.ab + self.bc) <= 0: 

1089 raise ValueError(_negative_) 

1090 a, _, c = self._abc3 = t 

1091 if not (a >= c and isfinite(self._a2b2) 

1092 and isfinite(self._a2c2)): 

1093 raise ValueError(_not_(_finite_)) 

1094 except (TypeError, ValueError) as x: 

1095 raise TriaxialError(radius_triaxial=r, ab=ab, bc=bc, cause=x) 

1096 if name: 

1097 self.name = name 

1098 

1099 @Property_RO 

1100 def ab(self): 

1101 '''Get relative magnitude C{ab} (C{meter}, same units as B{C{a}}). 

1102 ''' 

1103 return self._ab 

1104 

1105 @Property_RO 

1106 def _a2b2(self): 

1107 '''(INTERNAL) Get C{a**2 - b**2} == ab * (a + b). 

1108 ''' 

1109 a, b, _ = self._abc3 

1110 return self.ab * (a + b) 

1111 

1112 @Property_RO 

1113 def _a2c2(self): 

1114 '''(INTERNAL) Get C{a**2 - c**2} == a2b2 + b2c2. 

1115 ''' 

1116 return self._a2b2 + self._b2c2 

1117 

1118 @Property_RO 

1119 def bc(self): 

1120 '''Get relative magnitude C{bc} (C{meter}, same units as B{C{a}}). 

1121 ''' 

1122 return self._bc 

1123 

1124 @Property_RO 

1125 def _b2c2(self): 

1126 '''(INTERNAL) Get C{b**2 - c**2} == bc * (b + c). 

1127 ''' 

1128 _, b, c = self._abc3 

1129 return self.bc * (b + c) 

1130 

1131 @Property_RO 

1132 def radius(self): 

1133 '''Get radius (C{meter}, conventionally). 

1134 ''' 

1135 return self.a 

1136 

1137 

1138class TriaxialError(_ValueError): 

1139 '''Raised for L{Triaxial} issues. 

1140 ''' 

1141 pass # ... 

1142 

1143 

1144class Triaxials(_NamedEnum): 

1145 '''(INTERNAL) L{Triaxial} registry, I{must} be a sub-class 

1146 to accommodate the L{_LazyNamedEnumItem} properties. 

1147 ''' 

1148 def _Lazy(self, *abc, **name): 

1149 '''(INTERNAL) Instantiate the C{Triaxial}. 

1150 ''' 

1151 a, b, c = map(km2m, abc) 

1152 return Triaxial(a, b, c, **name) 

1153 

1154Triaxials = Triaxials(Triaxial, Triaxial_) # PYCHOK singleton 

1155'''Some pre-defined L{Triaxial}s, all I{lazily} instantiated.''' 

1156# <https://ArxIV.org/pdf/1909.06452.pdf> Table 1 Semi-axes in Km 

1157# <https://www.JPS.NASA.gov/education/images/pdf/ss-moons.pdf> 

1158# <https://link.Springer.com/article/10.1007/s00190-022-01650-9> 

1159_abc84_35 = (_EWGS84.a + 35), (_EWGS84.a - 35), _EWGS84.b 

1160Triaxials._assert( # a (Km) b (Km) c (Km) planet 

1161 Amalthea = _lazy('Amalthea', 125.0, 73.0, 64), # Jupiter 

1162 Ariel = _lazy('Ariel', 581.1, 577.9, 577.7), # Uranus 

1163 Earth = _lazy('Earth', 6378.173435, 6378.1039, 6356.7544), 

1164 Enceladus = _lazy('Enceladus', 256.6, 251.4, 248.3), # Saturn 

1165 Europa = _lazy('Europa', 1564.13, 1561.23, 1560.93), # Jupiter 

1166 Io = _lazy('Io', 1829.4, 1819.3, 1815.7), # Jupiter 

1167 Mars = _lazy('Mars', 3394.6, 3393.3, 3376.3), 

1168 Mimas = _lazy('Mimas', 207.4, 196.8, 190.6), # Saturn 

1169 Miranda = _lazy('Miranda', 240.4, 234.2, 232.9), # Uranus 

1170 Moon = _lazy('Moon', 1735.55, 1735.324, 1734.898), # Earth 

1171 Tethys = _lazy('Tethys', 535.6, 528.2, 525.8), # Saturn 

1172 WGS84_35 = _lazy('WGS84_35', *map(m2km, _abc84_35))) 

1173del _abc84_35, _EWGS84 

1174 

1175 

1176def _getitems(items, *indices): 

1177 '''(INTERNAL) Get the C{items} at the given I{indices}. 

1178 

1179 @return: C{Type(items[i] for i in indices)} with 

1180 C{Type = type(items)}, any C{type} having 

1181 the special method C{__getitem__}. 

1182 ''' 

1183 return type(items)(map(items.__getitem__, indices)) 

1184 

1185 

1186def _hartzell3(pov, los, Tun): # in .Ellipsoid.hartzell4, .formy.hartzell 

1187 '''(INTERNAL) Hartzell's "Satellite Line-of-Sight Intersection ...", 

1188 formula from a Point-Of-View to an I{un-/ordered} Triaxial. 

1189 ''' 

1190 def _toUvwV3d(los, pov): 

1191 try: # pov must be LatLon or Cartesian if los is a Los 

1192 v = los.toUvw(pov) 

1193 except (AttributeError, TypeError): 

1194 v = _otherV3d(los=los) 

1195 return v 

1196 

1197 p3 = _otherV3d(pov=pov.toCartesian() if isLatLon(pov) else pov) 

1198 if los is True: # normal 

1199 a, b, c, d, i = _plumbTo5(p3.x, p3.y, p3.z, Tun) 

1200 return type(p3)(a, b, c), d, i 

1201 

1202 u3 = p3.negate() if los is False or los is None else _toUvwV3d(los, pov) 

1203 

1204 a, b, c, T = Tun._ordered4 

1205 

1206 a2 = a**2 # largest, factored out 

1207 b2, p2 = (b**2, T._1e2ab) if b != a else (a2, _1_0) 

1208 c2, q2 = (c**2, T._1e2ac) if c != a else (a2, _1_0) 

1209 

1210 p3 = T._order3d(p3) 

1211 u3 = T._order3d(u3).unit() # unit vector, opposing signs 

1212 

1213 x2, y2, z2 = p3.x2y2z2 # p3.times_(p3).xyz 

1214 ux, vy, wz = u3.times_(p3).xyz 

1215 u2, v2, w2 = u3.x2y2z2 # u3.times_(u3).xyz 

1216 

1217 t = (p2 * c2), c2, b2 

1218 m = fdot(t, u2, v2, w2) # a2 factored out 

1219 if m < EPS0: # zero or near-null LOS vector 

1220 raise _ValueError(_near_(_null_)) 

1221 

1222 r = fsumf_(b2 * w2, c2 * v2, -v2 * z2, vy * wz * 2, 

1223 -w2 * y2, -u2 * y2 * q2, -u2 * z2 * p2, ux * wz * 2 * p2, 

1224 -w2 * x2 * p2, b2 * u2 * q2, -v2 * x2 * q2, ux * vy * 2 * q2) 

1225 if r > 0: # a2 factored out 

1226 r = sqrt(r) * b * c # == a * a * b * c / a2 

1227 elif r < 0: # LOS pointing away from or missing the triaxial 

1228 raise _ValueError(_opposite_ if max(ux, vy, wz) > 0 else _outside_) 

1229 

1230 d = Fdot(t, ux, vy, wz).fadd_(r).fover(m) # -r for antipode, a2 factored out 

1231 if d > 0: # POV inside or LOS outside or missing the triaxial 

1232 s = fsumf_(_N_1_0, x2 / a2, y2 / b2, z2 / c2) # like _sideOf 

1233 raise _ValueError(_outside_ if s > 0 else _inside_) 

1234 elif fsum1f_(x2, y2, z2) < d**2: # d past triaxial's center 

1235 raise _ValueError(_too_(_distant_)) 

1236 

1237 v = p3.minus(u3.times(d)) # cartesian type(pov) or Vector3d 

1238 h = p3.minus(v).length # distance to pov == -d 

1239 return T._order3d(v, reverse=True), h, None 

1240 

1241 

1242def hartzell4(pov, los=False, tri_biax=_WGS84, **name): 

1243 '''Compute the intersection of a tri-/biaxial ellipsoid and a Line-Of-Sight 

1244 from a Point-Of-View outside. 

1245 

1246 @arg pov: Point-Of-View outside the tri-/biaxial (C{Cartesian}, L{Ecef9Tuple} 

1247 C{LatLon} or L{Vector3d}). 

1248 @kwarg los: Line-Of-Sight, I{direction} to the tri-/biaxial (L{Los}, L{Vector3d}), 

1249 C{True} for the I{normal, plumb} onto the surface or C{False} or 

1250 C{None} to point to the center of the tri-/biaxial. 

1251 @kwarg tri_biax: A triaxial (L{Triaxial}, L{Triaxial_}, L{JacobiConformal} or 

1252 L{JacobiConformalSpherical}) or biaxial ellipsoid (L{Datum}, 

1253 L{Ellipsoid}, L{Ellipsoid2}, L{a_f2Tuple} or C{scalar} radius, 

1254 conventionally in C{meter}). 

1255 @kwarg name: Optional, overriding C{B{name}="hartzell4"} (C{str}). 

1256 

1257 @return: L{Vector4Tuple}C{(x, y, z, h)} on the tri-/biaxial's surface, with C{h} 

1258 the distance from B{C{pov}} to C{(x, y, z)} I{along the} B{C{los}}, all 

1259 in C{meter}, conventionally. 

1260 

1261 @raise TriaxialError: Invalid B{C{pov}} or B{C{pov}} inside the tri-/biaxial or 

1262 invalid B{C{los}} or B{C{los}} points outside or away from 

1263 the tri-/biaxial. 

1264 

1265 @raise TypeError: Invalid B{C{tri_biax}}, C{ellipsoid} or C{datum}. 

1266 

1267 @see: Class L{pygeodesy3.Los}, functions L{pygeodesy.tyr3d} and L{pygeodesy.hartzell} 

1268 and U{lookAtSpheroid<https://PyPI.org/project/pymap3d>} and U{"Satellite 

1269 Line-of-Sight Intersection with Earth"<https://StephenHartzell.Medium.com/ 

1270 satellite-line-of-sight-intersection-with-earth-d786b4a6a9b6>}. 

1271 ''' 

1272 if isinstance(tri_biax, Triaxial_): 

1273 T = tri_biax 

1274 else: 

1275 D = tri_biax if isinstance(tri_biax, Datum) else \ 

1276 _spherical_datum(tri_biax, name__=hartzell4) # _dunder_nameof 

1277 T = D.ellipsoid._triaxial 

1278 try: 

1279 v, h, i = _hartzell3(pov, los, T) 

1280 except Exception as x: 

1281 raise TriaxialError(pov=pov, los=los, tri_biax=tri_biax, cause=x) 

1282 return Vector4Tuple(v.x, v.y, v.z, h, iteration=i, # _dunder_nameof 

1283 name=_name__(name, name__=hartzell4)) 

1284 

1285 

1286def _hypot21(x, y, z=0): 

1287 '''(INTERNAL) Compute M{x**2 + y**2 + z**2 - 1} with C{max(fabs(x), 

1288 fabs(y), fabs(z))} rarely greater than 1.0. 

1289 ''' 

1290 return fsumf_(_1_0, x**2, y**2, (z**2 if z else _0_0), _N_2_0) 

1291 

1292 

1293def _otherV3d_(x_xyz, y, z, **name): 

1294 '''(INTERNAL) Get a Vector3d from C{x_xyz}, C{y} and C{z}. 

1295 ''' 

1296 return Vector3d(x_xyz, y, z, **name) if isscalar(x_xyz) else \ 

1297 _otherV3d(x_xyz=x_xyz, **name) 

1298 

1299 

1300def _plumbTo4(x, y, a, b, eps=EPS): 

1301 '''(INTERNAL) Nearest point on and distance to a 2-D ellipse, I{unordered}. 

1302 

1303 @see: Function C{pygeodesy.ellipsoids._plumbTo3} and I{Eberly}'s U{Distance 

1304 from a Point to ... an Ellipse ...<https://www.GeometricTools.com/ 

1305 Documentation/DistancePointEllipseEllipsoid.pdf>}. 

1306 ''' 

1307 if b > a: 

1308 b, a, d, i = _plumbTo4(y, x, b, a, eps=eps) 

1309 return a, b, d, i 

1310 

1311 if not (b > 0 and isfinite(a)): 

1312 raise _ValueError(a=a, b=b) 

1313 

1314 i, _a = None, fabs 

1315 if y: 

1316 if x: 

1317 u = _a(x / a) 

1318 v = _a(y / b) 

1319 g = _hypot21(u, v) 

1320 if _a(g) < EPS02: # on the ellipse 

1321 a, b, d = x, y, _0_0 

1322 else: 

1323 r = (b / a)**2 

1324 t, i = _rootXd(_1_0 / r, 0, u, 0, v, g, eps) 

1325 a = x / (t * r + _1_0) 

1326 b = y / (t + _1_0) 

1327 d = hypot(x - a, y - b) 

1328 else: # x == 0 

1329 if y < 0: 

1330 b = -b 

1331 a, d = x, _a(y - b) 

1332 

1333 else: # y == 0 

1334 n = a * x 

1335 d = (a + b) * (a - b) 

1336 if d > _a(n): # PYCHOK no cover 

1337 r = n / d 

1338 a *= r 

1339 r = _1_0 - r**2 

1340 if r > EPS02: 

1341 b *= sqrt(r) 

1342 d = hypot(x - a, b) 

1343 else: 

1344 b = _0_0 

1345 d = _a(x - a) 

1346 else: 

1347 if x < 0: 

1348 a = -a 

1349 b, d = y, _a(x - a) 

1350 return a, b, d, i 

1351 

1352 

1353def _plumbTo5(x, y, z, Tun, eps=EPS): # MCCABE 19 in .testTriaxials 

1354 '''(INTERNAL) Nearest point on and distance to an I{un-/ordered} triaxial. 

1355 

1356 @see: I{Eberly}'s U{Distance from a Point to ... an Ellipsoid ...<https:// 

1357 www.GeometricTools.com/Documentation/DistancePointEllipseEllipsoid.pdf>}. 

1358 ''' 

1359 a, b, c, T = Tun._ordered4 

1360 if Tun is not T: # T is ordered, Tun isn't 

1361 t = T._order3(x, y, z) + (T,) 

1362 a, b, c, d, i = _plumbTo5(*t, eps=eps) 

1363 return T._order3(a, b, c, reverse=True) + (d, i) 

1364 

1365 if not (c > 0 and isfinite(a)): 

1366 raise _ValueError(a=a, b=b, c=c) 

1367 

1368 if eps > 0: 

1369 val = max(eps * 1e8, EPS) 

1370 else: # no validation 

1371 val, eps = 0, max(EPS0, -eps) 

1372 

1373 i, _a = None, fabs 

1374 if z: 

1375 if y: 

1376 if x: 

1377 u = _a(x / a) 

1378 v = _a(y / b) 

1379 w = _a(z / c) 

1380 g = _hypot21(u, v, w) 

1381 if _a(g) < EPS02: # on the ellipsoid 

1382 a, b, c, d = x, y, z, _0_0 

1383 else: 

1384 r = T._1e2ac # (c / a)**2 

1385 s = T._1e2bc # (c / b)**2 

1386 t, i = _rootXd(_1_0 / r, _1_0 / s, u, v, w, g, eps) 

1387 a = x / (t * r + _1_0) 

1388 b = y / (t * s + _1_0) 

1389 c = z / (t + _1_0) 

1390 d = hypot_(x - a, y - b, z - c) 

1391 else: # x == 0 

1392 a = x # 0 

1393 b, c, d, i = _plumbTo4(y, z, b, c, eps=eps) 

1394 elif x: # y == 0 

1395 b = y # 0 

1396 a, c, d, i = _plumbTo4(x, z, a, c, eps=eps) 

1397 else: # x == y == 0 

1398 if z < 0: 

1399 c = -c 

1400 a, b, d = x, y, _a(z - c) 

1401 

1402 else: # z == 0 

1403 t = True 

1404 d = T._a2c2 # (a + c) * (a - c) 

1405 n = a * x 

1406 if d > _a(n): 

1407 u = n / d 

1408 d = T._b2c2 # (b + c) * (b - c) 

1409 n = b * y 

1410 if d > _a(n): 

1411 v = n / d 

1412 n = _hypot21(u, v) 

1413 if n < 0: 

1414 a *= u 

1415 b *= v 

1416 c *= sqrt(-n) 

1417 d = hypot_(x - a, y - b, c) 

1418 t = False 

1419 if t: 

1420 c = z # signed-0 

1421 a, b, d, i = _plumbTo4(x, y, a, b, eps=eps) 

1422 

1423 if val > 0: # validate 

1424 e = T.sideOf(a, b, c, eps=val) 

1425 if e: # not near the ellipsoid's surface 

1426 raise _ValueError(a=a, b=b, c=c, d=d, 

1427 sideOf=e, eps=val) 

1428 if d: # angle of delta and normal vector 

1429 m = Vector3d(x, y, z).minus_(a, b, c) 

1430 if m.euclid > val: 

1431 m = m.unit() 

1432 n = T.normal3d(a, b, c) 

1433 e = n.dot(m) # n.negate().dot(m) 

1434 if not isnear1(_a(e), eps1=val): 

1435 raise _ValueError(n=n, m=m, 

1436 dot=e, eps=val) 

1437 return a, b, c, d, i 

1438 

1439 

1440def _rootXd(r, s, u, v, w, g, eps): 

1441 '''(INTERNAL) Robust 2d- or 3d-root finder: 2d- if C{s == v == 0} else 3d-root. 

1442 

1443 @see: I{Eberly}'s U{Robust Root Finders ...<https://www.GeometricTools.com/ 

1444 Documentation/DistancePointEllipseEllipsoid.pdf>}. 

1445 ''' 

1446 _1, __2 = _1_0, _0_5 

1447 _a, _h21 = fabs, _hypot21 

1448 

1449 u *= r 

1450 v *= s # 0 for 2d-root 

1451 t0 = w - _1 

1452 t1 = _0_0 if g < 0 else (hypot_(u, w, v) - _1) 

1453 # assert t0 <= t1 

1454 for i in range(1, _TRIPS): # 48-55 

1455 e = _a(t0 - t1) 

1456 if e < eps: 

1457 break 

1458 t = (t0 + t1) * __2 

1459 if t in (t0, t1): 

1460 break 

1461 g = _h21(u / (t + r), w / (t + _1), 

1462 (v / (t + s)) if v else 0) 

1463 if g > 0: 

1464 t0 = t 

1465 elif g < 0: 

1466 t1 = t 

1467 else: 

1468 break 

1469 else: # PYCHOK no cover 

1470 t = Fmt.no_convergence(e, eps) 

1471 raise _ValueError(t, txt__=_rootXd) 

1472 return t, i 

1473 

1474 

1475def _sideOf(xyz, abc, eps=EPS): 

1476 '''(INTERNAL) Helper for C{_hartzell3}, M{.sideOf} and M{.reverseCartesian}. 

1477 

1478 @return: M{sum((x / a)**2 for x, a in zip(xyz, abc)) - 1} or C{INT0}. 

1479 ''' 

1480 s = fsumf_(_N_1_0, *((x / a)**2 for x, a in _zip(xyz, abc) if a)) # strict=True 

1481 return INT0 if fabs(s) < eps else s 

1482 

1483 

1484if __name__ == '__main__': 

1485 

1486 from pygeodesy import printf 

1487 from pygeodesy.interns import _COMMA_, _NL_, _NLATvar_ 

1488 

1489 # __doc__ of this file, force all into registery 

1490 t = [NN] + Triaxials.toRepr(all=True, asorted=True).split(_NL_) 

1491 printf(_NLATvar_.join(i.strip(_COMMA_) for i in t)) 

1492 

1493# **) MIT License 

1494# 

1495# Copyright (C) 2022-2024 -- mrJean1 at Gmail -- All Rights Reserved. 

1496# 

1497# Permission is hereby granted, free of charge, to any person obtaining a 

1498# copy of this software and associated documentation files (the "Software"), 

1499# to deal in the Software without restriction, including without limitation 

1500# the rights to use, copy, modify, merge, publish, distribute, sublicense, 

1501# and/or sell copies of the Software, and to permit persons to whom the 

1502# Software is furnished to do so, subject to the following conditions: 

1503# 

1504# The above copyright notice and this permission notice shall be included 

1505# in all copies or substantial portions of the Software. 

1506# 

1507# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 

1508# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 

1509# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 

1510# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 

1511# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 

1512# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 

1513# OTHER DEALINGS IN THE SOFTWARE.