Coverage for pygeodesy/triaxials.py: 96%

590 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2024-06-10 14:08 -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_RO 

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.06.09' 

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}, same units as B{C{a}}), required 

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

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

219 

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

221 ''' 

222 try: 

223 try: 

224 a = a_triaxial 

225 t = a._abc3 

226 except AttributeError: 

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

228 except (TypeError, ValueError) as x: 

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

230 if name: 

231 self.name = name 

232 

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

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

235 s, _, t = sorted(t) 

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

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

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

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

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

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

242 

243 def __str__(self): 

244 return self.toStr() 

245 

246 @Property_RO 

247 def a(self): 

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

249 ''' 

250 a, _, _ = self._abc3 

251 return a 

252 

253 @Property_RO 

254 def _a2b2(self): 

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

256 ''' 

257 a, b, _ = self._abc3 

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

259 

260 @Property_RO 

261 def _a2_b2(self): 

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

263 ''' 

264 a, b, _ = self._abc3 

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

266 

267 @Property_RO 

268 def _a2c2(self): 

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

270 ''' 

271 a, _, c = self._abc3 

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

273 

274 @Property_RO 

275 def area(self): 

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

277 ''' 

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

279 if a > c: 

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

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

282 else: # a == c == b 

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

284 return a 

285 

286 def area_p(self, p=1.6075): 

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

288 

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

290 for "near-flat" triaxials. 

291 

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

293 ''' 

294 a, b, c = self._abc3 

295 if a == b == c: 

296 a *= a 

297 else: 

298 _p = pow 

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

300 return Meter2(area_p=a * PI4) 

301 

302 @Property_RO 

303 def b(self): 

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

305 ''' 

306 _, b, _ = self._abc3 

307 return b 

308 

309 @Property_RO 

310 def _b2c2(self): 

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

312 ''' 

313 _, b, c = self._abc3 

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

315 

316 @Property_RO 

317 def c(self): 

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

319 ''' 

320 _, _, c = self._abc3 

321 return c 

322 

323 @Property_RO 

324 def _c2_b2(self): 

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

326 ''' 

327 _, b, c = self._abc3 

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

329 

330 @Property_RO 

331 def e2ab(self): 

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

333 ''' 

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

335 

336 @Property_RO 

337 def _1e2ab(self): 

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

339 ''' 

340 a, b, _ = self._abc3 

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

342 

343 @Property_RO 

344 def e2ac(self): 

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

346 ''' 

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

348 

349 @Property_RO 

350 def _1e2ac(self): 

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

352 ''' 

353 a, _, c = self._abc3 

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

355 

356 @Property_RO 

357 def e2bc(self): 

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

359 ''' 

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

361 

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

363 

364 @property_RO 

365 def _Elliptic(self): 

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

367 ''' 

368 Triaxial_._Elliptic = E = _MODS.elliptic.Elliptic # overwrite property_RO 

369 return E 

370 

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

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

373 from a Point-Of-View in space. 

374 

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

376 ''' 

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

378 

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

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

381 

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

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

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

385 @kwarg z: Z component (C{scalar}), required if B{C{x_xyz}} if C{scalar}. 

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

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

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

389 value to skip validation. 

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

391 

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

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

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

395 

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

397 root finding or validation failed. 

398 

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

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

401 ''' 

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

403 

404 i, h = None, v.length 

405 if h < EPS0: # EPS 

406 x = y = z = _0_0 

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

408 elif r: # .isSpherical 

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

410 h -= r 

411 else: 

412 x, y, z = v.xyz 

413 try: 

414 if normal: # plumb to surface 

415 x, y, z, h, i = _normalTo5(x, y, z, self, eps=eps) 

416 else: # radial to center 

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

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

419 except Exception as e: 

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

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

422 h = -h # below the surface 

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

424 name=_name__(name, name__=self.height4)) 

425 

426 @Property_RO 

427 def isOrdered(self): 

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

429 ''' 

430 a, b, c = self._abc3 

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

432 

433 @Property_RO 

434 def isSpherical(self): 

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

436 ''' 

437 a, b, c = self._abc3 

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

439 

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

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

442 ''' 

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

444 s, c = norm2(s, c) 

445 if a: 

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

447 return float0_(s, c) 

448 

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

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

451 

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

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

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

455 @kwarg z: Z component (C{scalar}), required if B{C{x_xyz}} if C{scalar}. 

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

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 @kwarg z: Z component (C{scalar}), required if B{C{x_xyz}} if C{scalar}. 

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

564 

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

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

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

568 respectively outside this triaxial. 

569 

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

571 ''' 

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

573 

574 def toEllipsoid(self, **name): 

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

576 

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

578 

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

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

581 

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

583 

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

585 ''' 

586 a, b, c = self._abc3 

587 if a == b: 

588 b = c # N = c-Z 

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

590 a, b = b, a 

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

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

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

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

595 

596 def toStr(self, prec=9, name=NN, **unused): # PYCHOK signature 

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

598 

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

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

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

602 

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

604 ''' 

605 T = Triaxial_ 

606 t = T.a, 

607 J = JacobiConformalSpherical 

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

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

610 J = JacobiConformal 

611 if isinstance(self, J): 

612 t += J.xyQ2, 

613 t += T.volume, T.area 

614 return self._instr(name, prec, props=t, area_p=self.area_p()) # __name__ 

615 

616 @Property_RO 

617 def volume(self): 

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

619 ''' 

620 a, b, c = self._abc3 

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

622 

623 

624class Triaxial(Triaxial_): 

625 '''I{Ordered} triaxial ellipsoid. 

626 

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

628 ''' 

629 _unordered = False 

630 

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

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

633 

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

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

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

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

638 @kwarg c: Smallest semi-axis (C{meter}, same units as B{C{a}}), required 

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

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 not ordered, 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 @kwarg z: Z component (C{scalar}), required if B{C{x_xyz}} if C{scalar}. 

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

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

747 see method L{Triaxial.height4}. 

748 

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

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

751 ''' 

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

753 

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

755 '''Convert I{geodetic} lat-, longitude and heigth to cartesian. 

756 

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

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

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

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

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

762 

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

764 

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

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

767 ''' 

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

769 

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

771 '''Convert I{geodetic} lat-, longitude and heigth to cartesian. 

772 

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

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

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

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

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

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

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

780 

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

782 

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

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

785 ''' 

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

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

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

789 

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

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

792 ''' 

793 ca_x_sb = ca * sb 

794 h = self._Height(height) 

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

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

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

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

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

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

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

802 

803 def _Height(self, height): 

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

805 ''' 

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

807 

808 @Property_RO 

809 def _k2_kp2(self): 

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

811 ''' 

812 # k2 = a2b2 / a2c2 * c2_b2 

813 # kp2 = b2c2 / a2c2 * a2_b2 

814 # b2 = b**2 

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

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

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

818 return (self._a2b2_a2c2 * self._c2_b2, 

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

820 

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

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

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

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

825 ''' 

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

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

828 

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

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

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

832 

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

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

835 

836 x, y, z = self._abc3 

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

838 y *= ca * sb 

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

840 return x, y, z 

841 

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

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

844 and height. 

845 

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 @kwarg name: Optional C{B{name}=NN} (C{str}). 

851 

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

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

854 units as this triaxial's axes. 

855 

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

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

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

859 ''' 

860 v = _otherV3d_(x_xyz, y, z) 

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

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

863 

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

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

866 

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

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

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

870 @kwarg z: Z component (C{scalar}), required if B{C{x_xyz}} if C{scalar}. 

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

872 as the axes). 

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

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

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

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

877 

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

879 

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

881 

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

883 ''' 

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

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

886 if s: # PYCHOK no cover 

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

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

889 

890 if h: 

891 if normal: 

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

893 elif v.length > EPS0: 

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

895 return v.xyz # Vector3Tuple 

896 

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

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

899 

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

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

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

903 @kwarg z: Z component (C{scalar}), required if B{C{x_xyz}} if C{scalar}. 

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

905 

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

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

908 as this triaxial's axes. 

909 

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

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

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

913 ''' 

914 v = _otherV3d_(x_xyz, y, z) 

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

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

917 _1_0) 

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

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

920 

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

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

923 ''' 

924 x, y, z = s.xyz 

925 d = hypot( x, y) 

926 a = atan2_(z, d) 

927 b = atan2_(y, x) 

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

929 return a, b, h 

930 

931 

932class JacobiConformal(Triaxial): 

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

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

935 

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

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

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

939 returned in the case of an ellipsoid of revolution. 

940 

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

942 licensed under the MIT/X11 License. 

943 

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

945 L{JacobiConformalSpherical}. 

946 

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

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

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

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

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

952 ''' 

953 

954 @Property_RO 

955 def _xE(self): 

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

957 ''' 

958 k2, kp2 = self._k2_kp2 

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

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

961 

962 def xR(self, omega): 

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

964 

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

966 

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

968 ''' 

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

970 

971 def xR_(self, somega, comega): 

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

973 

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

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

976 

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

978 ''' 

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

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

981 

982 @Property_RO 

983 def xyQ2(self): 

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

985 ''' 

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

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

988 name=JacobiConformal.xyQ2.name) 

989 

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

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

992 

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

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

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

996 

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

998 ''' 

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

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

1001 

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

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

1004 

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

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

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

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

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

1010 

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

1012 ''' 

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

1014 self.yR_(sbeta, cbeta), 

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

1016 

1017 @Property_RO 

1018 def _yE(self): 

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

1020 ''' 

1021 kp2, k2 = self._k2_kp2 # swapped! 

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

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

1024 

1025 def yR(self, beta): 

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

1027 

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

1029 

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

1031 ''' 

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

1033 

1034 def yR_(self, sbeta, cbeta): 

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

1036 

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

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

1039 

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

1041 ''' 

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

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

1044 

1045 

1046class JacobiConformalSpherical(JacobiConformal): 

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

1048 

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

1050 ''' 

1051 _ab = _bc = 0 

1052 

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

1054 '''New L{JacobiConformalSpherical}. 

1055 

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

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

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

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

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

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

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

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

1064 

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

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

1067 + B{bc})} not positive. 

1068 

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

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

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

1072 and C{c} are copied. 

1073 ''' 

1074 try: 

1075 r = radius_triaxial 

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

1077 t = r._abc3 

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

1079 else: 

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

1081 j = False 

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

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

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

1085 raise ValueError(_negative_) 

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

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

1088 and isfinite(self._a2c2)): 

1089 raise ValueError(_not_(_finite_)) 

1090 except (TypeError, ValueError) as x: 

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

1092 if name: 

1093 self.name = name 

1094 

1095 @Property_RO 

1096 def ab(self): 

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

1098 ''' 

1099 return self._ab 

1100 

1101 @Property_RO 

1102 def _a2b2(self): 

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

1104 ''' 

1105 a, b, _ = self._abc3 

1106 return self.ab * (a + b) 

1107 

1108 @Property_RO 

1109 def _a2c2(self): 

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

1111 ''' 

1112 return self._a2b2 + self._b2c2 

1113 

1114 @Property_RO 

1115 def bc(self): 

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

1117 ''' 

1118 return self._bc 

1119 

1120 @Property_RO 

1121 def _b2c2(self): 

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

1123 ''' 

1124 _, b, c = self._abc3 

1125 return self.bc * (b + c) 

1126 

1127 @Property_RO 

1128 def radius(self): 

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

1130 ''' 

1131 return self.a 

1132 

1133 

1134class TriaxialError(_ValueError): 

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

1136 ''' 

1137 pass # ... 

1138 

1139 

1140class Triaxials(_NamedEnum): 

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

1142 to accommodate the L{_LazyNamedEnumItem} properties. 

1143 ''' 

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

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

1146 ''' 

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

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

1149 

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1169del _abc84_35, _EWGS84 

1170 

1171 

1172def _getitems(items, *indices): 

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

1174 

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

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

1177 the special method C{__getitem__}. 

1178 ''' 

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

1180 

1181 

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

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

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

1185 ''' 

1186 def _toUvwV3d(los, pov): 

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

1188 v = los.toUvw(pov) 

1189 except (AttributeError, TypeError): 

1190 v = _otherV3d(los=los) 

1191 return v 

1192 

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

1194 if los is True: # normal 

1195 a, b, c, d, i = _normalTo5(p3.x, p3.y, p3.z, Tun) 

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

1197 

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

1199 

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

1201 

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

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

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

1205 

1206 p3 = T._order3d(p3) 

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

1208 

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

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

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

1212 

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

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

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

1216 raise _ValueError(_near_(_null_)) 

1217 

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

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

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

1221 if r > 0: # a2 factored out 

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

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

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

1225 

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

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

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

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

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

1231 raise _ValueError(_too_(_distant_)) 

1232 

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

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

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

1236 

1237 

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

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

1240 from a Point-Of-View outside. 

1241 

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

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

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

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

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

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

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

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

1250 conventionally in C{meter}). 

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

1252 

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

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

1255 in C{meter}, conventionally. 

1256 

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

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

1259 the tri-/biaxial. 

1260 

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

1262 

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

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

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

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

1267 ''' 

1268 if isinstance(tri_biax, Triaxial_): 

1269 T = tri_biax 

1270 else: 

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

1272 _spherical_datum(tri_biax, name__=hartzell4) # _dunder_nameof 

1273 T = D.ellipsoid._triaxial 

1274 try: 

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

1276 except Exception as x: 

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

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

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

1280 

1281 

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

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

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

1285 ''' 

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

1287 

1288 

1289def _normalTo4(x, y, a, b, eps=EPS): 

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

1291 

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

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

1294 Documentation/DistancePointEllipseEllipsoid.pdf>}. 

1295 ''' 

1296 if b > a: 

1297 b, a, d, i = _normalTo4(y, x, b, a, eps=eps) 

1298 return a, b, d, i 

1299 

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

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

1302 

1303 i, _a = None, fabs 

1304 if y: 

1305 if x: 

1306 u = _a(x / a) 

1307 v = _a(y / b) 

1308 g = _hypot21(u, v) 

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

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

1311 else: 

1312 r = (b / a)**2 

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

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

1315 b = y / (t + _1_0) 

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

1317 else: # x == 0 

1318 if y < 0: 

1319 b = -b 

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

1321 

1322 else: # y == 0 

1323 n = a * x 

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

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

1326 r = n / d 

1327 a *= r 

1328 r = _1_0 - r**2 

1329 if r > EPS02: 

1330 b *= sqrt(r) 

1331 d = hypot(x - a, b) 

1332 else: 

1333 b = _0_0 

1334 d = _a(x - a) 

1335 else: 

1336 if x < 0: 

1337 a = -a 

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

1339 return a, b, d, i 

1340 

1341 

1342def _normalTo5(x, y, z, Tun, eps=EPS): # MCCABE 19 

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

1344 

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

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

1347 ''' 

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

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

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

1351 a, b, c, d, i = _normalTo5(*t, eps=eps) 

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

1353 

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

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

1356 

1357 if eps > 0: 

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

1359 else: # no validation 

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

1361 

1362 i, _a = None, fabs 

1363 if z: 

1364 if y: 

1365 if x: 

1366 u = _a(x / a) 

1367 v = _a(y / b) 

1368 w = _a(z / c) 

1369 g = _hypot21(u, v, w) 

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

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

1372 else: 

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

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

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

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

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

1378 c = z / (t + _1_0) 

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

1380 else: # x == 0 

1381 a = x # 0 

1382 b, c, d, i = _normalTo4(y, z, b, c, eps=eps) 

1383 elif x: # y == 0 

1384 b = y # 0 

1385 a, c, d, i = _normalTo4(x, z, a, c, eps=eps) 

1386 else: # x == y == 0 

1387 if z < 0: 

1388 c = -c 

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

1390 

1391 else: # z == 0 

1392 t = True 

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

1394 n = a * x 

1395 if d > _a(n): 

1396 u = n / d 

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

1398 n = b * y 

1399 if d > _a(n): 

1400 v = n / d 

1401 n = _hypot21(u, v) 

1402 if n < 0: 

1403 a *= u 

1404 b *= v 

1405 c *= sqrt(-n) 

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

1407 t = False 

1408 if t: 

1409 c = z # signed-0 

1410 a, b, d, i = _normalTo4(x, y, a, b, eps=eps) 

1411 

1412 if val > 0: # validate 

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

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

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

1416 sideOf=e, eps=val) 

1417 if d: # angle of delta and normal vector 

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

1419 if m.euclid > val: 

1420 m = m.unit() 

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

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

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

1424 raise _ValueError(n=n, m=m, 

1425 dot=e, eps=val) 

1426 return a, b, c, d, i 

1427 

1428 

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

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

1431 ''' 

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

1433 _otherV3d(x_xyz=x_xyz) 

1434 

1435 

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

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

1438 

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

1440 Documentation/DistancePointEllipseEllipsoid.pdf>}. 

1441 ''' 

1442 _1, __2 = _1_0, _0_5 

1443 _a, _h21 = fabs, _hypot21 

1444 

1445 u *= r 

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

1447 t0 = w - _1 

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

1449 # assert t0 <= t1 

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

1451 e = _a(t0 - t1) 

1452 if e < eps: 

1453 break 

1454 t = (t0 + t1) * __2 

1455 if t in (t0, t1): 

1456 break 

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

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

1459 if g > 0: 

1460 t0 = t 

1461 elif g < 0: 

1462 t1 = t 

1463 else: 

1464 break 

1465 else: # PYCHOK no cover 

1466 t = Fmt.no_convergence(e, eps) 

1467 raise _ValueError(t, txt__=_rootXd) 

1468 return t, i 

1469 

1470 

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

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

1473 

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

1475 ''' 

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

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

1478 

1479 

1480if __name__ == '__main__': 

1481 

1482 from pygeodesy import printf 

1483 from pygeodesy.interns import _COMMA_, _NL_, _NLATvar_ 

1484 

1485 # __doc__ of this file, force all into registery 

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

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

1488 

1489# **) MIT License 

1490# 

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

1492# 

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

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

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

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

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

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

1499# 

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

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

1502# 

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

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

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

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

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

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

1509# OTHER DEALINGS IN THE SOFTWARE.