Coverage for pygeodesy/vector2d.py: 98%

1

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

3

4 u'''2- or 3-D vectorial functions L{circin6}, L{circum3}, L{circum4_},

5 L{iscolinearWith}, L{meeus2}, L{radii11}, L{soddy4}, L{triaxum5} and

6 L{trilaterate2d2}.

7

8@note: Functions L{circin6}, L{circum3}, L{circum4_}, L{soddy4} and

9 L{triaxum5} require U{numpyhttps://PyPI.org/project/numpy>}

10 version 1.10 or newer to be installed.

11'''

12

13 from pygeodesy.basics import len2, map2, _xnumpy, typename

14 from pygeodesy.constants import EPS, EPS0, EPS02, EPS4, INF, INT0, \

15 _EPS4e8, isnear0, _0_0, _0_25, _0_5, _N_0_5, \

16 _1_0, _1_0_1T, _N_1_0, _2_0, _N_2_0, _4_0

17 from pygeodesy.errors import _and, _AssertionError, IntersectionError, NumPyError, \

18 PointsError, TriangleError, _xError, _xkwds

19 from pygeodesy.fmath import fabs, fdot, Fdot_, fdot_, hypot, hypot2_, sqrt

20 from pygeodesy.fsums import _Fsumf_, fsumf_, fsum1f_

21# from pygeodesy.internals import typename # from .basics

22 from pygeodesy.interns import NN, _a_, _and_, _b_, _c_, _center_, _coincident_, \

23 _colinear_, _COMMASPACE_, _concentric_, _few_, \

24 _intersection_, _invalid_, _near_, _no_, _of_, \

25 _radius_, _rIn_, _s_, _SPACE_, _too_, _with_

26# from pygeodesy.lazily import _ALL_LAZY # from .named

27 from pygeodesy.named import _ALL_LAZY, _NamedTuple, _Pass

28 from pygeodesy.namedTuples import LatLon3Tuple, Vector2Tuple

29 from pygeodesy.props import deprecated_function, Property_RO

30 from pygeodesy.streprs import Fmt, unstr

31 from pygeodesy.units import Float, Int, Meter, Radius, Radius_

32 from pygeodesy.vector3d import _nearestOn2, _nVc, _otherV3d, trilaterate3d2, \

33 Vector3d, nearestOn as _nearestOn3d # DEPRECATED

34

35 from contextlib import contextmanager

36# from math import fabs, sqrt # from .fmath

37

38 __all__ = _ALL_LAZY.vector2d

39 __version__ = '26.02.27'

40

41 _cA_ = 'cA'

42 _cB_ = 'cB'

43 _cC_ = 'cC'

44 _deltas_ = 'deltas'

45 _outer_ = 'outer'

46 _raise_ = 'raise' # PYCHOK used!

47 _rank_ = 'rank'

48 _residuals_ = 'residuals'

49 _Type_ = 'Type'

50

51

52 class Circin6Tuple(_NamedTuple):

53 '''6-Tuple C{(radius, center, deltas, cA, cB, cC)} with the C{radius}, the

54 trilaterated C{center} and contact points of the I{inscribed} aka I{In-

55 circle} of a triangle. The C{center} is I{un}ambiguous if C{deltas} is

56 C{None}, otherwise C{center} is the mean and C{deltas} the differences of

57 the L{pygeodesy.trilaterate3d2} results. Contact points C{cA}, C{cB} and

58 C{cC} are the points of tangency, aka the corners of the U{Contact Triangle

59 <https://MathWorld.Wolfram.com/ContactTriangle.html>}.

60 '''

61 _Names_ = (_radius_, _center_, _deltas_, _cA_, _cB_, _cC_)

62 _Units_ = ( Radius, _Pass, _Pass, _Pass, _Pass, _Pass)

63

64

65 class Circum3Tuple(_NamedTuple): # in .latlonBase

66 '''3-Tuple C{(radius, center, deltas)} with the C{circumradius} and trilaterated

67 C{circumcenter} of the C{circumcircle} through 3 points (aka {Meeus}' Type II

68 circle) or the C{radius} and C{center} of the smallest I{Meeus}' Type I circle.

69 The C{center} is I{un}ambiguous if C{deltas} is C{None}, otherwise C{center}

70 is the mean and C{deltas} the differences of the L{pygeodesy.trilaterate3d2}

71 results.

72 '''

73 _Names_ = (_radius_, _center_, _deltas_)

74 _Units_ = ( Radius, _Pass, _Pass)

75

76

77 class Circum4Tuple(_NamedTuple):

78 '''4-Tuple C{(radius, center, rank, residuals)} with C{radius} and C{center}

79 of a sphere I{least-squares} fitted through given points and the C{rank}

80 and C{residuals} -if any- from U{numpy.linalg.lstsq

81 <https://NumPy.org/doc/stable/reference/generated/numpy.linalg.lstsq.html>}.

82 '''

83 _Names_ = (_radius_, _center_, _rank_, _residuals_)

84 _Units_ = ( Radius, _Pass, Int, _Pass)

85

86

87 class Meeus2Tuple(_NamedTuple):

88 '''2-Tuple C{(radius, Type)} with C{radius} and I{Meeus}' C{Type} of the smallest

89 circle I{containing} 3 points. C{Type} is C{None} for a I{Meeus}' Type II

90 C{circumcircle} passing through all 3 points. Otherwise C{Type} is the center

91 of a I{Meeus}' Type I circle with 2 points on (a diameter of) and 1 point

92 inside the circle.

93 '''

94 _Names_ = (_radius_, _Type_)

95 _Units_ = ( Radius, _Pass)

96

97

98 class Radii11Tuple(_NamedTuple):

99 '''11-Tuple C{(rA, rB, rC, cR, rIn, riS, roS, a, b, c, s)} with the C{Tangent}

100 circle radii C{rA}, C{rB} and C{rC}, the C{circumradius} C{cR}, the C{Incircle}

101 radius C{rIn} aka C{inradius}, the inner and outer I{Soddy} circle radii C{riS}

102 and C{roS}, the sides C{a}, C{b} and C{c} and semi-perimeter C{s} of a triangle,

103 all in C{meter} conventionally.

104

105 @note: C{Circumradius} C{cR} and outer I{Soddy} radius C{roS} may be C{INF}.

106 '''

107 _Names_ = ('rA', 'rB', 'rC', 'cR', _rIn_, 'riS', 'roS', _a_, _b_, _c_, _s_)

108 _Units_ = ( Meter,) * len(_Names_)

109

110

111 class Soddy4Tuple(_NamedTuple):

112 '''4-Tuple C{(radius, center, deltas, outer)} with C{radius} and (trilaterated)

113 C{center} of the I{inner} I{Soddy} circle and the radius of the C{outer}

114 I{Soddy} circle. The C{center} is I{un}ambiguous if C{deltas} is C{None},

115 otherwise C{center} is the mean and C{deltas} the differences of the

116 L{pygeodesy.trilaterate3d2} results.

117

118 @note: The outer I{Soddy} radius C{outer} may be C{INF}.

119 '''

120 _Names_ = (_radius_, _center_, _deltas_, _outer_)

121 _Units_ = ( Radius, _Pass, _Pass, Radius)

122

123

124 class Triaxum5Tuple(_NamedTuple):

125 '''5-Tuple C{(a, b, c, rank, residuals)} with the (unordered) triaxial radii

126 C{a}, C{b} and C{c} of an ellipsoid I{least-squares} fitted through given

127 points and the C{rank} and C{residuals} -if any- from U{numpy.linalg.lstsq

128 <https://NumPy.org/doc/stable/reference/generated/numpy.linalg.lstsq.html>}.

129 '''

130 _Names_ = (_a_, _b_, _c_, _rank_, _residuals_)

131 _Units_ = ( Radius, Radius, Radius, Int, _Pass)

132

133

134 def circin6(point1, point2, point3, eps=EPS4, useZ=True):

135 '''Return the radius and center of the I{inscribed} aka I{Incircle} of

136 a (2- or 3-D) triangle.

137

138 @arg point1: First point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple},

139 C{Vector4Tuple} or C{Vector2Tuple} if C{B{useZ}=False}).

140 @arg point2: Second point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple},

141 C{Vector4Tuple} or C{Vector2Tuple} if C{B{useZ}=False}).

142 @arg point3: Third point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple},

143 C{Vector4Tuple} or C{Vector2Tuple} if C{B{useZ}=False}).

144 @kwarg eps: Tolerance for function L{pygeodesy.trilaterate3d2} if

145 C{B{useZ} is True} else L{pygeodesy.trilaterate2d2}.

146 @kwarg useZ: If C{True}, use the Z components, otherwise force C{z=INT0} (C{bool}).

147

148 @return: L{Circin6Tuple}C{(radius, center, deltas, cA, cB, cC)}. The

149 C{center} and contact points C{cA}, C{cB} and C{cC}, each an

150 instance of B{C{point1}}'s (sub-)class, are co-planar with

151 the three given points.

152

153 @raise ImportError: Package C{numpy} not found, not installed or older

154 than version 1.10 and C{B{useZ} is True}.

155

156 @raise IntersectionError: Near-coincident or -colinear points or

157 a trilateration or C{numpy} issue.

158

159 @raise TypeError: Invalid B{C{point1}}, B{C{point2}} or B{C{point3}}.

160

161 @see: Functions L{radii11} and L{circum3}, U{Contact Triangle

162 <https://MathWorld.Wolfram.com/ContactTriangle.html>} and

163 U{Incircle<https://MathWorld.Wolfram.com/Incircle.html>}.

164 '''

165 try:

166 return _circin6(point1, point2, point3, eps=eps, useZ=useZ)

167 except (AssertionError, TypeError, ValueError) as x:

168 raise _xError(x, point1=point1, point2=point2, point3=point3)

169

170

171 def _circin6(point1, point2, point3, eps=EPS4, useZ=True, dLL3=False, **Vector_kwds):

172 # (INTERNAL) Radius, center, deltas, 3 contact points

173

174 def _fraction(r, a):

175 return (r / a) if a > EPS0 else _0_5

176

177 def _contact2(a, p2, r2, p3, r3, V, V_kwds):

178 c = p2.intermediateTo(p3, _fraction(r2, a)) if r2 > r3 else \

179 p3.intermediateTo(p2, _fraction(r3, a))

180 C = V(c.x, c.y, c.z, **V_kwds)

181 return c, C

182

183 t, p1, p2, p3 = _radii11ABC4(point1, point2, point3, useZ=useZ)

184 V, r1, r2, r3 = point1.classof, t.rA, t.rB, t.rC

185

186 c1, cA = _contact2(t.a, p2, r2, p3, r3, V, _xkwds(Vector_kwds, name=_cA_))

187 c2, cB = _contact2(t.b, p3, r3, p1, r1, V, _xkwds(Vector_kwds, name=_cB_))

188 c3, cC = _contact2(t.c, p1, r1, p2, r2, V, _xkwds(Vector_kwds, name=_cC_))

189

190 r = t.rIn

191 c, d = _tricenter3d2(c1, r, c2, r, c3, r, eps=eps, useZ=useZ, dLL3=dLL3,

192 **_xkwds(Vector_kwds, Vector=V, name=typename(circin6)))

193 return Circin6Tuple(r, c, d, cA, cB, cC)

194

195

196 def circum3(point1, point2, point3, circum=True, eps=EPS4, useZ=True):

197 '''Return the radius and center of the smallest circle I{through} or

198 I{containing} three (2- or 3-D) points.

199

200 @arg point1: First point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple} or

201 C{Vector4Tuple}).

202 @arg point2: Second point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple} or

203 C{Vector4Tuple}).

204 @arg point3: Third point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple} or

205 C{Vector4Tuple}).

206 @kwarg circum: If C{True}, return the C{circumradius} and C{circumcenter}

207 always, ignoring the I{Meeus}' Type I case (C{bool}).

208 @kwarg eps: Tolerance for function L{pygeodesy.trilaterate3d2} if C{B{useZ}

209 is True} else L{pygeodesy.trilaterate2d2}.

210 @kwarg useZ: If C{True}, use the Z components, otherwise force C{z=INT0} (C{bool}).

211

212 @return: A L{Circum3Tuple}C{(radius, center, deltas)}. The C{center}, an

213 instance of B{C{point1}}'s (sub-)class, is co-planar with the three

214 given points.

215

216 @raise ImportError: Package C{numpy} not found, not installed or older

217 than version 1.10 and C{B{useZ} is True}.

218

219 @raise IntersectionError: Near-coincident or -colinear points or

220 a trilateration or C{numpy} issue.

221

222 @raise TypeError: Invalid B{C{point1}}, B{C{point2}} or B{C{point3}}.

223

224 @see: Functions L{pygeodesy.circum4_} and L{pygeodesy.meeus2} and Meeus, J.

225 U{I{Astronomical Algorithms}<http://www.Agopax.IT/Libri_astronomia/pdf/

226 Astronomical%20Algorithms.pdf>}, 2nd Ed. 1998, page 127ff, U{circumradius

227 <https://MathWorld.Wolfram.com/Circumradius.html>} and U{circumcircle

228 <https://MathWorld.Wolfram.com/Circumcircle.html>}.

229 '''

230 try:

231 p1 = _otherV3d(useZ=useZ, point1=point1)

232 return _circum3(p1, point2, point3, circum=circum, eps=eps, useZ=useZ,

233 clas=point1.classof)

234 except (AssertionError, TypeError, ValueError) as x:

235 raise _xError(x, point1=point1, point2=point2, point3=point3, circum=circum)

236

237

238 def _circum3(p1, point2, point3, circum=True, eps=EPS4, useZ=True, dLL3=False,

239 clas=Vector3d, **clas_kwds): # in .latlonBase

240 # (INTERNAL) Radius, center, deltas

241 r, d, p2, p3 = _meeus4(p1, point2, point3, circum=circum, useZ=useZ,

242 clas=clas, **clas_kwds)

243 if d is None: # Meeus' Type II or circum=True

244 kwds = _xkwds(clas_kwds, eps=eps, Vector=clas, name=typename(circum3))

245 c, d = _tricenter3d2(p1, r, p2, r, p3, r, useZ=useZ, dLL3=dLL3, **kwds)

246 else: # Meeus' Type I

247 c, d = d, None

248 return Circum3Tuple(r, c, d)

249

250

251 def circum4(points, useZ=True, **Vector_and_kwds):

252 '''Best-fit a sphere through three or more (3-D) points.

253

254 @arg points: Iterable of points (each a C{Cartesian}, L{Vector3d}, C{Vector3Tuple}

255 or C{Vector4Tuple}).

256 @kwarg useZ: If C{True}, use the points' Z component, otherwise force C{z=INT0}

257 (C{bool}).

258 @kwarg Vector_and_kwds: Optional class C{B{Vector}=None} to return the center point

259 and optionally, additional B{C{Vector}} keyword arguments, otherwise

260 the B{C{points}}' (sub-)class.

261

262 @return: L{Circum4Tuple}C{(radius, center, rank, residuals)} with C{center} an

263 instance of C{B{points}[0]}' (sub-)class or B{C{Vector}} if specified.

264

265 @raise ImportError: Package C{numpy} not found, not installed or older than

266 version 1.10.

267

268 @raise NumPyError: Some C{numpy} issue.

269

270 @raise PointsError: Too few B{C{points}}.

271

272 @raise TypeError: One of the B{C{points}} is invalid.

273

274 @see: Functions L{pygeodesy.circum3} and L{pygeodesy.meeus2}, I{Charles Jekel}'s

275 U{"Least Squares Sphere Fit"<https://Jekel.me/2015/Least-Squares-Sphere-Fit/>},

276 U{Appendix A<https://hdl.handle.net/10019.1/98627>}, U{numpy.linalg.lstsq

277 <https://NumPy.org/doc/stable/reference/generated/numpy.linalg.lstsq.html>} and U{Eberly

278 6<https://www.sci.Utah.edu/~balling/FEtools/doc_files/LeastSquaresFitting.pdf>}.

279 '''

280 n, ps = len2(points)

281 if n < 3:

282 raise PointsError(points=n, txt=_too_(_few_))

283

284 A, b = [], []

285 for i, p in enumerate(ps):

286 v = _otherV3d(useZ=useZ, i=i, points=p)

287 A.append(v.times(_2_0).xyz3 + _1_0_1T)

288 b.append(v.length2)

289

290 with _numpy(circum4, n=n) as _np:

291 A = _np.array(A).reshape((n, 4))

292 b = _np.array(b).reshape((n, 1))

293 C, R, rk = _np.least_squares3(A, b)

294

295 c = Vector3d(*C[:3], name__=circum4) # .__name__

296 r = Radius(sqrt(fsumf_(C[3], *c.x2y2z2)), name=c.name)

297

298 c = _nVc(c, **_xkwds(Vector_and_kwds, clas=ps[0].classof, name=c.name))

299 return Circum4Tuple(r, c, rk, R)

300

301

302 def circum4_(*points, **useZ_Vector_and_kwds):

303 '''Best-fit a sphere through three or more (3-D) positional points.

304

305 @arg points: The points (each a C{Cartesian}, L{Vector3d}, C{Vector3Tuple}

306 or C{Vector4Tuple}), all positional.

307 @kwarg useZ_Vector_and_kwds: Keyword arguments C{B{useZ}=True} and

308 C{B{Vector}=None}, see function L{circum4}.

309

310 @see: Function L{circum4} for further details.

311 '''

312 return circum4(points, **useZ_Vector_and_kwds)

313

314

315 def _iscolinearWith(p, point1, point2, eps=EPS, useZ=True):

316 # (INTERNAL) Check colinear, see L{iscolinearWith} above,

317 # separated to allow callers to embellish any exceptions

318 p1 = _otherV3d(useZ=useZ, point1=point1)

319 p2 = _otherV3d(useZ=useZ, point2=point2)

320 n, _ = _nearestOn2(p, p1, p2, within=False, eps=eps)

321 return n is p1 or n.minus(p).length2 < eps

322

323

324 def meeus2(point1, point2, point3, circum=False, useZ=True):

325 '''Return the radius and I{Meeus}' Type of the smallest circle I{through}

326 or I{containing} three (2- or 3-D) points.

327

328 @arg point1: First point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple},

329 C{Vector4Tuple} or C{Vector2Tuple} if C{B{useZ}=False}).

330 @arg point2: Second point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple},

331 C{Vector4Tuple} or C{Vector2Tuple} if C{B{useZ}=False}).

332 @arg point3: Third point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple},

333 C{Vector4Tuple} or C{Vector2Tuple} if C{B{useZ}=False}).

334 @kwarg circum: If C{True}, return the C{circumradius} and C{circumcenter}

335 always, overriding I{Meeus}' Type II case (C{bool}).

336 @kwarg useZ: If C{True}, use the Z components, otherwise force C{z=INT0} (C{bool}).

337

338 @return: L{Meeus2Tuple}C{(radius, Type)}, with C{Type} the C{circumcenter}

339 iff C{B{circum}=True}.

340

341 @raise IntersectionError: Near-coincident or -colinear points, iff C{B{circum}=True}.

342

343 @raise TypeError: Invalid B{C{point1}}, B{C{point2}} or B{C{point3}}.

344

345 @see: Functions L{pygeodesy.circum3} and L{pygeodesy.circum4_} and Meeus, J.

346 U{I{Astronomical Algorithms}<http://www.Agopax.IT/Libri_astronomia/pdf/

347 Astronomical%20Algorithms.pdf>}, 2nd Ed. 1998, page 127ff, U{circumradius

348 <https://MathWorld.Wolfram.com/Circumradius.html>} and U{circumcircle

349 <https://MathWorld.Wolfram.com/Circumcircle.html>}.

350 '''

351 try:

352 A = _otherV3d(useZ=useZ, point1=point1)

353 return _meeus2(A, point2, point3, circum, useZ=useZ, clas=point1.classof)

354 except (TypeError, ValueError) as x:

355 raise _xError(x, point1=point1, point2=point2, point3=point3, circum=circum)

356

357

358 def _meeus2(A, point2, point3, circum, useZ=True, **clas_and_kwds): # in .vector3d

359 # (INTERNAL) Radius and center or Meeus' Type

360 f = _circum3 if circum else _meeus4

361 t = f(A, point2, point3, circum=circum, useZ=useZ, **clas_and_kwds)[:2]

362 return Meeus2Tuple(t)

363

364

365 def _meeus4(A, point2, point3, circum=False, useZ=True, clas=None, **clas_kwds):

366 # (INTERNAL) Radius and Meeus' Type

367 B = p2 = _otherV3d(useZ=useZ, point2=point2)

368 C = p3 = _otherV3d(useZ=useZ, point3=point3)

369

370 a = B.minus(C).length2

371 b = C.minus(A).length2

372 c = A.minus(B).length2

373 if a < b:

374 a, b, A, B = b, a, B, A

375 if a < c:

376 a, c, A, C = c, a, C, A

377

378 if a > EPS02 and (circum or a < (b + c)): # circumradius

379 b = sqrt(b / a)

380 c = sqrt(c / a)

381 R = _Fsumf_(_1_0, b, c) * _Fsumf_(_1_0, b, -c) * \

382 _Fsumf_(_1_0, -b, c) * _Fsumf_(_N_1_0, b, c)

383 r = R.fover(a)

384 if r < EPS02:

385 t = _coincident_ if b < EPS0 or c < EPS0 else (

386 _colinear_ if _iscolinearWith(A, B, C) else _invalid_)

387 raise IntersectionError(t)

388 r = b * c / sqrt(r)

389 t = None # Meeus' Type II

390 else: # obtuse or right angle at A

391 r = sqrt(a * _0_25) if a > EPS02 else INT0

392 t = B.plus(C).times(_0_5) # Meeus' Type I

393 if clas is not None:

394 t = clas(t.x, t.y, t.z, **_xkwds(clas_kwds, name=typename(meeus2)))

395 return r, t, p2, p3

396

397

398@deprecated_function

399 def nearestOn(point, point1, point2, **within_useZ_Vector_and_kwds):

400 '''DEPRECATED on 2025.05.06, use L{pygeodesy.nearestOn<pygeodesy.vector3d.nearestOn>}.'''

401 return _nearestOn3d(point, point1, point2, **within_useZ_Vector_and_kwds)

402

403

404 class _numpy(object): # see also .formy._idllmn6, .geodesicw._wargs, .latlonBase._toCartesian3

405 '''(INTERNAL) Partial C{NumPy} wrapper.

406 '''

407 @contextmanager # <https://www.Python.org/dev/peps/pep-0343/> Examples

408 def __call__(self, where, *args, **kwds):

409 '''(INTERNAL) Yield self with any errors raised as L{NumPyError}

410 embellished with all B{C{args}} and B{C{kwds}}.

411 '''

412 np = self.np

413 try: # <https://NumPy.org/doc/stable/reference/generated/numpy.seterr.html>

414 e = np.seterr(all=_raise_) # throw FloatingPointError for numpy errors

415 yield self

416 except Exception as x: # mostly FloatingPointError?

417 t = unstr(where, *args, **kwds)

418 raise NumPyError(t, cause=x) # _xError2?

419 finally: # restore numpy error handling

420 np.seterr(**e)

421

422 @Property_RO

423 def array(self):

424 '''Get U{numpy.array<https://NumPy.org/doc/2.2/reference/generated/numpy.array.html#numpy.array>}.

425 '''

426 return self.np.array

427

428 def least_squares3(self, A, b):

429 '''Linear least-squares function.

430 '''

431 C, R, rk, _ = self.np.linalg.lstsq(A, b, rcond=None) # to silence warning

432 C = map2(float, C)

433 R = map2(float, R) # empty if rk < 4 or n <= 4

434 return C, R, int(rk)

435

436 @Property_RO

437 def np(self):

438 '''Import numpy 1.10+ once.

439 '''

440 return _xnumpy(type(self), 1, 10)

441

442 def null_space2(self, A, rcond=None):

443 '''Return the C{null_space} and C{rank} of matrix B{C{A}}.

444

445 @see: U{Source<https://docs.SciPy.org/doc/scipy/reference/generated/scipy.linalg.null_space.html>}

446 U{SciPY Cookbook<https://SciPy-Cookbook.ReadTheDocs.io/items/RankNullspace.html>}, U{here

447 <https://NumPy.org/doc/stable/reference/generated/numpy.linalg.svd.html>}, U{here

448 <https://StackOverflow.com/questions/19820921>}, U{here

449 <https://StackOverflow.com/questions/2992947>} and U{here

450 <https://StackOverflow.com/questions/5889142>}.

451 '''

452 def _Error(**kwds):

453 return _AssertionError(txt__=self.null_space2, **kwds)

454

455 np = self.np

456 A = np.array(A)

457 m = max(A.shape)

458 if m != 4: # for this usage

459 raise _Error(shape=m)

460 # if needed, square A, pad with zeros

461 A = np.resize(A, m * m).reshape(m, m)

462# try: # no np.linalg.null_space <https://docs.SciPy.org/doc/>

463# Z = scipy.linalg.null_space(A) # XXX no scipy.linalg?

464# return Z, ...

465# except AttributeError:

466# pass

467 U, S, V = np.linalg.svd(A)

468 s = max(EPS, rcond) if rcond else (EPS * max(U.shape[0], V.shape[1]))

469 t = max(EPS, float(np.max(S) * s)) # abs_tol, rel_tol * largest singular

470 r = int(np.sum(S > t)) # rank

471 if r == 3: # get null_space

472 Z = np.transpose(V[r:])

473 s = map2(int, Z.shape)

474 if s != (m, 1): # bad null_space shape

475 raise _Error(shape=s, m=m)

476 D = A.dot(Z) # near-zeros-vector

477 n = float(np.linalg.norm(D, INF)) # INF = max(fabs(D)), 2 = hypot_(*D)

478 if n > t: # largest exceed tol

479 raise _Error(dot=tuple(D.ravel()), norm=n, tol=t)

480 else: # coincident, colinear, concentric centers, ambiguous, etc.

481 Z = None

482 # del A, S, U, V # release numpy

483 return Z, r

484

485 @Property_RO

486 def pseudo_inverse(self):

487 '''Moore-Penrose pseudo-inverse function.

488 '''

489 return self.np.linalg.pinv

490

491 def real_roots(self, *coeffs):

492 '''Compute the real, non-complex roots of a polynomial.

493 '''

494 np = self.np

495 rs = np.polynomial.polynomial.polyroots(coeffs)

496 return tuple(float(r) for r in rs if not np.iscomplex(r))

497

498 _numpy = _numpy() # PYCHOK singleton

499

500

501 def radii11(point1, point2, point3, useZ=True):

502 '''Return the radii of the C{In-}, I{Soddy} and C{Tangent} circles of a

503 (2- or 3-D) triangle.

504

505 @arg point1: First point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple},

506 C{Vector4Tuple} or C{Vector2Tuple} if C{B{useZ}=False}).

507 @arg point2: Second point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple},

508 C{Vector4Tuple} or C{Vector2Tuple} if C{B{useZ}=False}).

509 @arg point3: Third point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple},

510 C{Vector4Tuple} or C{Vector2Tuple} if C{B{useZ}=False}).

511 @kwarg useZ: If C{True}, use the Z components, otherwise force C{z=INT0} (C{bool}).

512

513 @return: L{Radii11Tuple}C{(rA, rB, rC, cR, rIn, riS, roS, a, b, c, s)}.

514

515 @raise TriangleError: Near-coincident or -colinear points.

516

517 @raise TypeError: Invalid B{C{point1}}, B{C{point2}} or B{C{point3}}.

518

519 @see: U{Circumradius<https://MathWorld.Wolfram.com/Circumradius.html>},

520 U{Incircle<https://MathWorld.Wolfram.com/Incircle.html>}, U{Soddy

521 Circles<https://MathWorld.Wolfram.com/SoddyCircles.html>} and

522 U{Tangent Circles<https://MathWorld.Wolfram.com/TangentCircles.html>}.

523 '''

524 try:

525 return _radii11ABC4(point1, point2, point3, useZ=useZ)[0]

526 except (TypeError, ValueError) as x:

527 raise _xError(x, point1=point1, point2=point2, point3=point3)

528

529

530 def _radii11ABC4(point1, point2, point3, useZ=True):

531 # (INTERNAL) Tangent, Circum, Incircle, Soddy radii, sides and semi-perimeter

532 A = _otherV3d(useZ=useZ, point1=point1, NN_OK=False)

533 B = _otherV3d(useZ=useZ, point2=point2, NN_OK=False)

534 C = _otherV3d(useZ=useZ, point3=point3, NN_OK=False)

535

536 a = B.minus(C).length

537 b = C.minus(A).length

538 c = A.minus(B).length

539

540 S = _Fsumf_(a, b, c) * _0_5

541 s = float(S) # semi-perimeter

542 if s > EPS0:

543 rs = float(S - a), float(S - b), float(S - c)

544 r3, r2, r1 = sorted(rs) # r3 <= r2 <= r1

545 if r3 > EPS0: # and r2 > EPS0 and r1 > EPS0

546 r3_r1 = r3 / r1

547 r3_r2 = r3 / r2

548 # t = r1 * r2 * r3 * (r1 + r2 + r3)

549 # = r1**2 * r2 * r3 * (1 + r2 / r1 + r3 / r1)

550 # = (r1 * r2)**2 * (r3 / r2) * (1 + r2 / r1 + r3 / r1)

551 t = r3_r2 * fsum1f_(_1_0, r2 / r1, r3_r1) # * (r1 * r2)**2

552 if t > EPS02:

553 t = sqrt(t) * _2_0 # * r1 * r2

554 # d = r1 * r2 + r2 * r3 + r3 * r1

555 # = r1 * (r2 + r2 * r3 / r1 + r3)

556 # = r1 * r2 * (1 + r3 / r1 + r3 / r2)

557 d = fsum1f_(_1_0, r3_r1, r3_r2) # * r1 * r2

558 # si/o = r1 * r2 * r3 / (r1 * r2 * (d +/- t))

559 # = r3 / (d +/- t)

560 si = r3 / (d + t)

561 so = (r3 / (d - t)) if d > t else INF

562 # ci = sqrt(r1 * r2 * r3 / s)

563 # = r1 * sqrt(r2 * r3 / r1 / s)

564 ci = r1 * sqrt(r2 * r3_r1 / s)

565 # co = a * b * c / (4 * ci * s)

566 t = ci * s * _4_0

567 co = (a * b * c / t) if t > EPS0 else INF

568 r1, r2, r3 = rs # original order

569 t = Radii11Tuple(r1, r2, r3, co, ci, si, so, a, b, c, s)

570 return t, A, B, C

571

572 raise TriangleError(_near_(_coincident_) if min(a, b, c) < EPS0 else (

573 _colinear_ if _iscolinearWith(A, B, C) else _invalid_))

574

575

576 def soddy4(point1, point2, point3, eps=EPS4, useZ=True):

577 '''Return the radius and center of the C{inner} I{Soddy} circle of a

578 (2- or 3-D) triangle.

579

580 @arg point1: First point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple},

581 C{Vector4Tuple} or C{Vector2Tuple} if C{B{useZ}=False}).

582 @arg point2: Second point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple},

583 C{Vector4Tuple} or C{Vector2Tuple} if C{B{useZ}=False}).

584 @arg point3: Third point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple},

585 C{Vector4Tuple} or C{Vector2Tuple} if C{B{useZ}=False}).

586 @kwarg eps: Tolerance for function L{pygeodesy.trilaterate3d2} if

587 C{B{useZ} is True} otherwise L{pygeodesy.trilaterate2d2}.

588 @kwarg useZ: If C{True}, use the Z components, otherwise force C{z=INT0} (C{bool}).

589

590 @return: L{Soddy4Tuple}C{(radius, center, deltas, outer)}. The C{center},

591 an instance of B{C{point1}}'s (sub-)class, is co-planar with the

592 three given points. The C{outer} I{Soddy} radius may be C{INF}.

593

594 @raise ImportError: Package C{numpy} not found, not installed or older

595 than version 1.10 and C{B{useZ} is True}.

596

597 @raise IntersectionError: Near-coincident or -colinear points or

598 a trilateration or C{numpy} issue.

599

600 @raise TypeError: Invalid B{C{point1}}, B{C{point2}} or B{C{point3}}.

601

602 @see: Functions L{radii11} and L{circum3} and U{Soddy Circles

603 <https://MathWorld.Wolfram.com/SoddyCircles.html>}.

604 '''

605 t, p1, p2, p3 = _radii11ABC4(point1, point2, point3, useZ=useZ)

606

607 r = t.riS

608 c, d = _tricenter3d2(p1, t.rA + r,

609 p2, t.rB + r,

610 p3, t.rC + r, eps=eps, useZ=useZ,

611 Vector=point1.classof, name=typename(soddy4))

612 return Soddy4Tuple(r, c, d, t.roS)

613

614

615 def triaxum5(points, useZ=True):

616 '''Best-fit a triaxial ellipsoid through three or more (3-D) points.

617

618 @arg points: Iterable of points (each a C{Cartesian}, L{Vector3d}, C{Vector3Tuple}

619 or C{Vector4Tuple}).

620 @kwarg useZ: If C{True}, use the points' Z component, otherwise force C{z=INT0}

621 (C{bool}).

622

623 @return: L{Triaxum5Tuple}C{(a, b, c, rank, residuals)} with the unordered triaxial

624 radii C{a}, C{b} and C{c} in C{meter}, same units as the points' coordinates.

625

626 @raise ImportError: Package C{numpy} not found, not installed or older than version 1.10.

627

628 @raise NumPyError: Some C{numpy} issue.

629

630 @raise PointsError: Too few B{C{points}}.

631

632 @raise TypeError: One of the B{C{points}} is invalid.

633

634 @see: I{Charles Jekel}'s U{"Least Squares Ellipsoid Fit"<https://Jekel.me/2020/Least-Squares-Ellipsoid-Fit/>}

635 and U{numpy.linalg.lstsq<https://NumPy.org/doc/stable/reference/generated/numpy.linalg.lstsq.html>}.

636 '''

637 n, ps = len2(points)

638 if n < 3:

639 raise PointsError(points=n, txt=_too_(_few_))

640

641 A = []

642 for i, p in enumerate(ps):

643 v = _otherV3d(useZ=useZ, i=i, points=p)

644 A.append(v.x2y2z23)

645

646 with _numpy(triaxum5, n=n) as _np:

647 A = _np.array(A)

648 b = _1_0_1T * n

649 T, R, rk = _np.least_squares3(A, b)

650

651 def _1_sqrt(x):

652 return sqrt(_1_0 / x) if x else _0_0 # INF

653

654 a, b, c = map(_1_sqrt, T)

655 return Triaxum5Tuple(a, b, c, rk, R)

656

657

658 def _tricenter3d2(p1, r1, p2, r2, p3, r3, eps=EPS4, useZ=True, dLL3=False, **kwds):

659 # (INTERNAL) Trilaterate and disambiguate the 3-D center

660 d, kwds = None, _xkwds(kwds, eps=eps, coin=True)

661 if useZ and p1.z != p2.z != p3.z: # ignore z if all match

662 a, b = _trilaterate3d2(p1, r1, p2, r2, p3, r3, **kwds)

663 if a is b: # no unambiguity

664 c = a # == b

665 else:

666 c = a.plus(b).times(_0_5) # mean

667 if not a.isconjugateTo(b, minum=0, eps=eps):

668 if dLL3: # deltas as (lat, lon, height)

669 a = a.toLatLon()

670 b = b.toLatLon()

671 d = LatLon3Tuple(b.lat - a.lat,

672 b.lon - a.lon,

673 b.height - a.height, name=_deltas_)

674 else:

675 d = b.minus(a) # vectorial deltas

676 else:

677 if useZ: # pass z to Vector if given

678 kwds = _xkwds(kwds, z=p1.z)

679 c = _trilaterate2d2(p1.x, p1.y, r1,

680 p2.x, p2.y, r2,

681 p3.x, p3.y, r3, **kwds)

682 return c, d

683

684

685 def trilaterate2d2(x1, y1, radius1, x2, y2, radius2, x3, y3, radius3,

686 eps=None, **Vector_and_kwds):

687 '''Trilaterate three circles, each given as a (2-D) center and a radius.

688

689 @arg x1: Center C{x} coordinate of the 1st circle (C{scalar}).

690 @arg y1: Center C{y} coordinate of the 1st circle (C{scalar}).

691 @arg radius1: Radius of the 1st circle (C{scalar}).

692 @arg x2: Center C{x} coordinate of the 2nd circle (C{scalar}).

693 @arg y2: Center C{y} coordinate of the 2nd circle (C{scalar}).

694 @arg radius2: Radius of the 2nd circle (C{scalar}).

695 @arg x3: Center C{x} coordinate of the 3rd circle (C{scalar}).

696 @arg y3: Center C{y} coordinate of the 3rd circle (C{scalar}).

697 @arg radius3: Radius of the 3rd circle (C{scalar}).

698 @kwarg eps: Tolerance to check the trilaterated point I{delta} on

699 all 3 circles (C{scalar}) or C{None} for no checking.

700 @kwarg Vector_and_kwds: Optional class C{B{Vector}=None} to return

701 the trilateration and optionally, additional B{C{Vector}}

702 keyword arguments).

703

704 @return: Trilaterated point as C{B{Vector}(x, y, **B{Vector_kwds})}

705 or L{Vector2Tuple}C{(x, y)} if C{B{Vector} is None}.

706

707 @raise IntersectionError: No intersection, near-concentric or -colinear

708 centers, trilateration failed some other way

709 or the trilaterated point is off one circle

710 by more than B{C{eps}}.

711

712 @raise UnitError: Invalid B{C{radius1}}, B{C{radius2}} or B{C{radius3}}.

713

714 @see: U{Issue #49<https://GitHub.com/mrJean1/PyGeodesy/issues/49>},

715 U{Find X location using 3 known (X,Y) location using trilateration

716 <https://math.StackExchange.com/questions/884807>} and function

717 L{pygeodesy.trilaterate3d2}.

718 '''

719 return _trilaterate2d2(x1, y1, radius1,

720 x2, y2, radius2,

721 x3, y3, radius3, eps=eps, **Vector_and_kwds)

722

723

724 def _trilaterate2d2(x1, y1, radius1, x2, y2, radius2, x3, y3, radius3,

725 coin=False, eps=None,

726 Vector=None, **Vector_kwds):

727 # (INTERNAL) Trilaterate three circles, see L{pygeodesy.trilaterate2d2}

728

729 def _abct4(x1, y1, r1, x2, y2, r2):

730 a = x2 - x1

731 b = y2 - y1

732 t = _tri3near2far(r1, r2, hypot(a, b), coin)

733 c = _0_0 if t else (hypot2_(r1, x2, y2) - hypot2_(r2, x1, y1))

734 return a, b, c, t

735

736 def _astr(**kwds): # kwds as (name=value, ...) strings

737 return Fmt.PAREN(_COMMASPACE_(*(Fmt.EQUALg(*t) for t in kwds.items())))

738

739 r1 = Radius_(radius1=radius1)

740 r2 = Radius_(radius2=radius2)

741 r3 = Radius_(radius3=radius3)

742

743 a, b, c, t = _abct4(x1, y1, r1, x2, y2, r2)

744 if t:

745 raise IntersectionError(_and(_astr(x1=x1, y1=y1, radius1=r1),

746 _astr(x2=x2, y2=y2, radius2=r2)), txt=t)

747

748 d, e, f, t = _abct4(x2, y2, r2, x3, y3, r3)

749 if t:

750 raise IntersectionError(_and(_astr(x2=x2, y2=y2, radius2=r2),

751 _astr(x3=x3, y3=y3, radius3=r3)), txt=t)

752

753 _, _, _, t = _abct4(x3, y3, r3, x1, y1, r1)

754 if t:

755 raise IntersectionError(_and(_astr(x3=x3, y3=y3, radius3=r3),

756 _astr(x1=x1, y1=y1, radius1=r1)), txt=t)

757

758 q = fdot_(a, e, -b, d) * _2_0

759 if isnear0(q):

760 t = _no_(_intersection_)

761 raise IntersectionError(_and(_astr(x1=x1, y1=y1, radius1=r1),

762 _astr(x2=x2, y2=y2, radius2=r2),

763 _astr(x3=x3, y3=y3, radius3=r3)), txt=t)

764 t = Vector2Tuple(Fdot_(c, e, -b, f).fover(q),

765 Fdot_(a, f, -c, d).fover(q), name=typename(trilaterate2d2))

766

767 if eps and eps > 0: # check distances to center vs radius

768 for x, y, r in ((x1, y1, r1), (x2, y2, r2), (x3, y3, r3)):

769 d = hypot(x - t.x, y - t.y)

770 e = fabs(d - r)

771 if e > eps:

772 t = _and(Float(delta=e).toRepr(), r.toRepr(),

773 Float(distance=d).toRepr(), t.toRepr())

774 raise IntersectionError(t, txt=Fmt.exceeds_eps(eps))

775

776 if Vector is not None:

777 t = Vector(t.x, t.y, **_xkwds(Vector_kwds, name=t.name))

778 return t

779

780

781 def _trilaterate3d2(c1, r1, c2, r2, c3, r3, eps=EPS4, coin=False, # MCCABE 13

782 **clas_Vector_and_kwds):

783 # (INTERNAL) Intersect three spheres or circles, see function

784 # L{pygeodesy.trilaterate3d2}, separated to allow callers to

785 # embellish exceptions, like C{FloatingPointError}s from C{numpy}

786

787 def _Arow4(c):

788 # make a row for matrix A (1, -2x, -2y, -2z)

789 return _1_0_1T + c.times(_N_2_0).xyz3

790

791 def _F4d3(F):

792 # map numpy 4-vector to floats and xyz3

793 T = map2(float, F)

794 t = T[1:]

795 return T, t, Vector3d(*t)

796

797 def _N3(t01, x, z):

798 # compute x, y and z and return as B{C{clas}} or B{C{Vector}}

799 v = x.plus(z.times(t01))

800 n = typename(trilaterate3d2)

801 return _nVc(v, **_xkwds(clas_Vector_and_kwds, name=n))

802

803 c2 = _otherV3d(center2=c2, NN_OK=False)

804 c3 = _otherV3d(center3=c3, NN_OK=False)

805 rs = (r1, Radius_(radius2=r2, low=EPS),

806 Radius_(radius3=r3, low=EPS))

807

808 # get matrix A[3 x 4], its null_space Z and pseudo-inverse

809 A = [_Arow4(c) for c in (c1, c2, c3)]

810 with _numpy(trilaterate3d2, A=A, eps=eps) as _np:

811 Z, _ = _np.null_space2(A, eps)

812 if Z is not None:

813 Z, _, z = _F4d3(Z) # [4 x 1]

814 z2 = z.length2

815 A = _np.pseudo_inverse(A) # [4 x 3]

816 bs = [c.length2 for c in (c1, c2, c3)]

817 # perturb radii slightly by eps and eps * 4

818 for p in _tri5perturbs(eps, min(rs)):

819 b = [((r + p)**2 - b) for r, b in zip(rs, bs)] # [3 x 1]

820 X, t, x = _F4d3(A.dot(b)) # [4 * 1]

821 # quadratic polynomial, coefficients order (^0, ^1, ^2)

822 t = _np.real_roots(fdot(X, _N_1_0, *t),

823 fdot(Z, _N_0_5, *t) * _2_0, z2)

824 if t:

825 v = _N3(t[0], x, z)

826 if len(t) < 2: # one intersection

827 t = v, v

828 elif fabs(t[0] - t[1]) < eps: # abutting

829 t = v, v

830 else: # "lowest" intersection first (to avoid test failures)

831 u = _N3(t[1], x, z)

832 t = (u, v) if u.x < v.x else (v, u)

833 return t

834

835 def _no_itersection(coin, Z):

836 t = _no_(_intersection_)

837 if coin:

838 def _reprs(*crs):

839 return _and(*map(repr, crs))

840

841 r = repr(r1) if r1 == r2 == r3 else _reprs(r1, r2, r3)

842 t = _SPACE_(t, _of_, _reprs(c1, c2, c3), _with_, _radius_, r)

843 elif Z is None:

844 t = _COMMASPACE_(t, _no_(typename(_numpy.null_space2)))

845 return t

846

847 # coincident, concentric, colinear, too distant, no intersection:

848 # create the explanation and and throw an IntersectionError

849 t = _tri4near2far(c1, r1, c2, r2, coin) or \

850 _tri4near2far(c1, r1, c3, r3, coin) or \

851 _tri4near2far(c2, r2, c3, r3, coin) or (

852 _colinear_ if _iscolinearWith(c1, c2, c3, eps=eps) else

853 _no_itersection(coin, Z))

854 raise IntersectionError(t, txt=None)

855

856

857 def _tri3near2far(r1, r2, h, coin):

858 # check for near-coincident/-concentric or too distant spheres/circles

859 return _too_(Fmt.distant(h)) if h > (r1 + r2) else (_near_(

860 _coincident_ if coin else _concentric_) if h < fabs(r1 - r2) else NN)

861

862

863 def _tri4near2far(c1, r1, c2, r2, coin):

864 # check for near-coincident/-concentric or too distant spheres/circles

865 t = _tri3near2far(r1, r2, c1.minus(c2).length, coin)

866 return _SPACE_(c1.name, _and_, c2.name, t) if t else NN

867

868

869 def _tri5perturbs(eps, r):

870 # perturb the radii to handle this corner case

871 # <https://GitHub.com/mrJean1/PyGeodesy/issues/49>

872 yield _0_0

873 if eps and eps > 0:

874 p = max(eps, EPS)

875 yield p

876 m = min(p, r)

877 yield -m

878 q = max(eps * _4_0, _EPS4e8)

879 if q > p:

880 yield q

881 q = min(q, r)

882 if q > m:

883 yield -q

884

885# **) MIT License

886#

887# Copyright (C) 2016-2026 -- mrJean1 at Gmail -- All Rights Reserved.

888#

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

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

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

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

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

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

895#

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

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

898#

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

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

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

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

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

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

905# OTHER DEALINGS IN THE SOFTWARE.