Coverage for pygeodesy/vector2d.py: 98%
340 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-04-25 13:15 -0400
« prev ^ index » next coverage.py v7.6.1, created at 2025-04-25 13:15 -0400
2# -*- coding: utf-8 -*-
4u'''2- or 3-D vectorial functions L{circin6}, L{circum3}, L{circum4_},
5L{iscolinearWith}, L{meeus2}, L{nearestOn}, L{radii11}, L{soddy4},
6L{triaxum5} and L{trilaterate2d2}.
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'''
13from pygeodesy.basics import len2, map2, _xnumpy, typename
14from 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
17from pygeodesy.errors import _and, _AssertionError, IntersectionError, NumPyError, \
18 PointsError, TriangleError, _xError, _xkwds
19from pygeodesy.fmath import fabs, fdot, fdot_, hypot, hypot2_, sqrt
20from pygeodesy.fsums import _Fsumf_, fsumf_, fsum1f_
21# from pygeodesy.internals import typename # from .basics
22from 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
27from pygeodesy.named import _ALL_LAZY, _NamedTuple, _Pass, Property_RO
28from pygeodesy.namedTuples import LatLon3Tuple, Vector2Tuple
29# from pygeodesy.props import Property_RO # from .named
30from pygeodesy.streprs import Fmt, unstr
31from pygeodesy.units import Float, Int, Meter, Radius, Radius_
32from pygeodesy.vector3d import iscolinearWith, nearestOn, _nearestOn2, _nVc, \
33 _otherV3d, trilaterate3d2, Vector3d # PYCHOK unused
35from contextlib import contextmanager
36# from math import fabs, sqrt # from .fmath
38__all__ = _ALL_LAZY.vector2d
39__version__ = '25.04.16'
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'
52class 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)
65class 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)
77class 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)
87class 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)
98class 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.
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_)
111class 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.
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)
124class 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)
134def 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.
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}).
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.
153 @raise ImportError: Package C{numpy} not found, not installed or older
154 than version 1.10 and C{B{useZ} is True}.
156 @raise IntersectionError: Near-coincident or -colinear points or
157 a trilateration or C{numpy} issue.
159 @raise TypeError: Invalid B{C{point1}}, B{C{point2}} or B{C{point3}}.
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)
171def _circin6(point1, point2, point3, eps=EPS4, useZ=True, dLL3=False, **Vector_kwds):
172 # (INTERNAL) Radius, center, deltas, 3 contact points
174 def _fraction(r, a):
175 return (r / a) if a > EPS0 else _0_5
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
183 t, p1, p2, p3 = _radii11ABC4(point1, point2, point3, useZ=useZ)
184 V, r1, r2, r3 = point1.classof, t.rA, t.rB, t.rC
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_))
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)
196def 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.
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}).
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.
216 @raise ImportError: Package C{numpy} not found, not installed or older
217 than version 1.10 and C{B{useZ} is True}.
219 @raise IntersectionError: Near-coincident or -colinear points or
220 a trilateration or C{numpy} issue.
222 @raise TypeError: Invalid B{C{point1}}, B{C{point2}} or B{C{point3}}.
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)
238def _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)
251def circum4(points, useZ=True, **Vector_and_kwds):
252 '''Best-fit a sphere through three or more (3-D) points.
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.
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.
265 @raise ImportError: Package C{numpy} not found, not installed or older than
266 version 1.10.
268 @raise NumPyError: Some C{numpy} issue.
270 @raise PointsError: Too few B{C{points}}.
272 @raise TypeError: One of the B{C{points}} is invalid.
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_))
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)
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)
295 c = Vector3d(*C[:3], name__=circum4) # .__name__
296 r = Radius(sqrt(fsumf_(C[3], *c.x2y2z2)), name=c.name)
298 c = _nVc(c, **_xkwds(Vector_and_kwds, clas=ps[0].classof, name=c.name))
299 return Circum4Tuple(r, c, rk, R)
302def circum4_(*points, **useZ_Vector_and_kwds):
303 '''Best-fit a sphere through three or more (3-D) positional points.
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}.
310 @see: Function L{circum4} for further details.
311 '''
312 return circum4(points, **useZ_Vector_and_kwds)
315def _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
324def 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.
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}).
338 @return: L{Meeus2Tuple}C{(radius, Type)}, with C{Type} the C{circumcenter}
339 iff C{B{circum}=True}.
341 @raise IntersectionError: Near-coincident or -colinear points, iff C{B{circum}=True}.
343 @raise TypeError: Invalid B{C{point1}}, B{C{point2}} or B{C{point3}}.
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)
358def _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)
365def _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)
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
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
398class _numpy(object): # see also .formy._idllmn6, .geodesicw._wargs, .latlonBase._toCartesian3
399 '''(INTERNAL) Partial C{NumPy} wrapper.
400 '''
401 @contextmanager # <https://www.Python.org/dev/peps/pep-0343/> Examples
402 def __call__(self, where, *args, **kwds):
403 '''(INTERNAL) Yield self with any errors raised as L{NumPyError}
404 embellished with all B{C{args}} and B{C{kwds}}.
405 '''
406 np = self.np
407 try: # <https://NumPy.org/doc/stable/reference/generated/numpy.seterr.html>
408 e = np.seterr(all=_raise_) # throw FloatingPointError for numpy errors
409 yield self
410 except Exception as x: # mostly FloatingPointError?
411 t = unstr(where, *args, **kwds)
412 raise NumPyError(t, cause=x) # _xError2?
413 finally: # restore numpy error handling
414 np.seterr(**e)
416 @Property_RO
417 def array(self):
418 '''Get U{numpy.array<https://NumPy.org/doc/2.2/reference/generated/numpy.array.html#numpy.array>}.
419 '''
420 return self.np.array
422 def least_squares3(self, A, b):
423 '''Linear least-squares function.
424 '''
425 C, R, rk, _ = self.np.linalg.lstsq(A, b, rcond=None) # to silence warning
426 C = map2(float, C)
427 R = map2(float, R) # empty if rk < 4 or n <= 4
428 return C, R, int(rk)
430 @Property_RO
431 def np(self):
432 '''Import numpy 1.10+ once.
433 '''
434 return _xnumpy(self.__class__, 1, 10)
436 def null_space2(self, A, rcond=None):
437 '''Return the C{null_space} and C{rank} of matrix B{C{A}}.
439 @see: U{Source<https://docs.SciPy.org/doc/scipy/reference/generated/scipy.linalg.null_space.html>}
440 U{SciPY Cookbook<https://SciPy-Cookbook.ReadTheDocs.io/items/RankNullspace.html>}, U{here
441 <https://NumPy.org/doc/stable/reference/generated/numpy.linalg.svd.html>}, U{here
442 <https://StackOverflow.com/questions/19820921>}, U{here
443 <https://StackOverflow.com/questions/2992947>} and U{here
444 <https://StackOverflow.com/questions/5889142>}.
445 '''
446 def _Error(**kwds):
447 return _AssertionError(txt__=self.null_space2, **kwds)
449 np = self.np
450 A = np.array(A)
451 m = max(A.shape)
452 if m != 4: # for this usage
453 raise _Error(shape=m)
454 # if needed, square A, pad with zeros
455 A = np.resize(A, m * m).reshape(m, m)
456# try: # no np.linalg.null_space <https://docs.SciPy.org/doc/>
457# Z = scipy.linalg.null_space(A) # XXX no scipy.linalg?
458# return Z, ...
459# except AttributeError:
460# pass
461 U, S, V = np.linalg.svd(A)
462 s = max(EPS, rcond) if rcond else (EPS * max(U.shape[0], V.shape[1]))
463 t = max(EPS, float(np.max(S) * s)) # abs_tol, rel_tol * largest singular
464 r = int(np.sum(S > t)) # rank
465 if r == 3: # get null_space
466 Z = np.transpose(V[r:])
467 s = map2(int, Z.shape)
468 if s != (m, 1): # bad null_space shape
469 raise _Error(shape=s, m=m)
470 D = A.dot(Z) # near-zeros-vector
471 n = float(np.linalg.norm(D, INF)) # INF = max(fabs(D)), 2 = hypot_(*D)
472 if n > t: # largest exceed tol
473 raise _Error(dot=tuple(D.ravel()), norm=n, tol=t)
474 else: # coincident, colinear, concentric centers, ambiguous, etc.
475 Z = None
476 # del A, S, U, V # release numpy
477 return Z, r
479 @Property_RO
480 def pseudo_inverse(self):
481 '''Moore-Penrose pseudo-inverse function.
482 '''
483 return self.np.linalg.pinv
485 def real_roots(self, *coeffs):
486 '''Compute the real, non-complex roots of a polynomial.
487 '''
488 np = self.np
489 rs = np.polynomial.polynomial.polyroots(coeffs)
490 return tuple(float(r) for r in rs if not np.iscomplex(r))
492_numpy = _numpy() # PYCHOK singleton
495def radii11(point1, point2, point3, useZ=True):
496 '''Return the radii of the C{In-}, I{Soddy} and C{Tangent} circles of a
497 (2- or 3-D) triangle.
499 @arg point1: First point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple},
500 C{Vector4Tuple} or C{Vector2Tuple} if C{B{useZ}=False}).
501 @arg point2: Second point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple},
502 C{Vector4Tuple} or C{Vector2Tuple} if C{B{useZ}=False}).
503 @arg point3: Third point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple},
504 C{Vector4Tuple} or C{Vector2Tuple} if C{B{useZ}=False}).
505 @kwarg useZ: If C{True}, use the Z components, otherwise force C{z=INT0} (C{bool}).
507 @return: L{Radii11Tuple}C{(rA, rB, rC, cR, rIn, riS, roS, a, b, c, s)}.
509 @raise TriangleError: Near-coincident or -colinear points.
511 @raise TypeError: Invalid B{C{point1}}, B{C{point2}} or B{C{point3}}.
513 @see: U{Circumradius<https://MathWorld.Wolfram.com/Circumradius.html>},
514 U{Incircle<https://MathWorld.Wolfram.com/Incircle.html>}, U{Soddy
515 Circles<https://MathWorld.Wolfram.com/SoddyCircles.html>} and
516 U{Tangent Circles<https://MathWorld.Wolfram.com/TangentCircles.html>}.
517 '''
518 try:
519 return _radii11ABC4(point1, point2, point3, useZ=useZ)[0]
520 except (TypeError, ValueError) as x:
521 raise _xError(x, point1=point1, point2=point2, point3=point3)
524def _radii11ABC4(point1, point2, point3, useZ=True):
525 # (INTERNAL) Tangent, Circum, Incircle, Soddy radii, sides and semi-perimeter
526 A = _otherV3d(useZ=useZ, point1=point1, NN_OK=False)
527 B = _otherV3d(useZ=useZ, point2=point2, NN_OK=False)
528 C = _otherV3d(useZ=useZ, point3=point3, NN_OK=False)
530 a = B.minus(C).length
531 b = C.minus(A).length
532 c = A.minus(B).length
534 S = _Fsumf_(a, b, c) * _0_5
535 s = float(S) # semi-perimeter
536 if s > EPS0:
537 rs = float(S - a), float(S - b), float(S - c)
538 r3, r2, r1 = sorted(rs) # r3 <= r2 <= r1
539 if r3 > EPS0: # and r2 > EPS0 and r1 > EPS0
540 r3_r1 = r3 / r1
541 r3_r2 = r3 / r2
542 # t = r1 * r2 * r3 * (r1 + r2 + r3)
543 # = r1**2 * r2 * r3 * (1 + r2 / r1 + r3 / r1)
544 # = (r1 * r2)**2 * (r3 / r2) * (1 + r2 / r1 + r3 / r1)
545 t = r3_r2 * fsum1f_(_1_0, r2 / r1, r3_r1) # * (r1 * r2)**2
546 if t > EPS02:
547 t = sqrt(t) * _2_0 # * r1 * r2
548 # d = r1 * r2 + r2 * r3 + r3 * r1
549 # = r1 * (r2 + r2 * r3 / r1 + r3)
550 # = r1 * r2 * (1 + r3 / r1 + r3 / r2)
551 d = fsum1f_(_1_0, r3_r1, r3_r2) # * r1 * r2
552 # si/o = r1 * r2 * r3 / (r1 * r2 * (d +/- t))
553 # = r3 / (d +/- t)
554 si = r3 / (d + t)
555 so = (r3 / (d - t)) if d > t else INF
556 # ci = sqrt(r1 * r2 * r3 / s)
557 # = r1 * sqrt(r2 * r3 / r1 / s)
558 ci = r1 * sqrt(r2 * r3_r1 / s)
559 # co = a * b * c / (4 * ci * s)
560 t = ci * s * _4_0
561 co = (a * b * c / t) if t > EPS0 else INF
562 r1, r2, r3 = rs # original order
563 t = Radii11Tuple(r1, r2, r3, co, ci, si, so, a, b, c, s)
564 return t, A, B, C
566 raise TriangleError(_near_(_coincident_) if min(a, b, c) < EPS0 else (
567 _colinear_ if _iscolinearWith(A, B, C) else _invalid_))
570def soddy4(point1, point2, point3, eps=EPS4, useZ=True):
571 '''Return the radius and center of the C{inner} I{Soddy} circle of a
572 (2- or 3-D) triangle.
574 @arg point1: First point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple},
575 C{Vector4Tuple} or C{Vector2Tuple} if C{B{useZ}=False}).
576 @arg point2: Second point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple},
577 C{Vector4Tuple} or C{Vector2Tuple} if C{B{useZ}=False}).
578 @arg point3: Third point (C{Cartesian}, L{Vector3d}, C{Vector3Tuple},
579 C{Vector4Tuple} or C{Vector2Tuple} if C{B{useZ}=False}).
580 @kwarg eps: Tolerance for function L{pygeodesy.trilaterate3d2} if
581 C{B{useZ} is True} otherwise L{pygeodesy.trilaterate2d2}.
582 @kwarg useZ: If C{True}, use the Z components, otherwise force C{z=INT0} (C{bool}).
584 @return: L{Soddy4Tuple}C{(radius, center, deltas, outer)}. The C{center},
585 an instance of B{C{point1}}'s (sub-)class, is co-planar with the
586 three given points. The C{outer} I{Soddy} radius may be C{INF}.
588 @raise ImportError: Package C{numpy} not found, not installed or older
589 than version 1.10 and C{B{useZ} is True}.
591 @raise IntersectionError: Near-coincident or -colinear points or
592 a trilateration or C{numpy} issue.
594 @raise TypeError: Invalid B{C{point1}}, B{C{point2}} or B{C{point3}}.
596 @see: Functions L{radii11} and L{circum3} and U{Soddy Circles
597 <https://MathWorld.Wolfram.com/SoddyCircles.html>}.
598 '''
599 t, p1, p2, p3 = _radii11ABC4(point1, point2, point3, useZ=useZ)
601 r = t.riS
602 c, d = _tricenter3d2(p1, t.rA + r,
603 p2, t.rB + r,
604 p3, t.rC + r, eps=eps, useZ=useZ,
605 Vector=point1.classof, name=typename(soddy4))
606 return Soddy4Tuple(r, c, d, t.roS)
609def triaxum5(points, useZ=True):
610 '''Best-fit a triaxial ellipsoid through three or more (3-D) points.
612 @arg points: Iterable of points (each a C{Cartesian}, L{Vector3d}, C{Vector3Tuple}
613 or C{Vector4Tuple}).
614 @kwarg useZ: If C{True}, use the points' Z component, otherwise force C{z=INT0}
615 (C{bool}).
617 @return: L{Triaxum5Tuple}C{(a, b, c, rank, residuals)} with the unordered triaxial
618 radii C{a}, C{b} and C{c} in C{meter}, same units as the points' coordinates.
620 @raise ImportError: Package C{numpy} not found, not installed or older than version 1.10.
622 @raise NumPyError: Some C{numpy} issue.
624 @raise PointsError: Too few B{C{points}}.
626 @raise TypeError: One of the B{C{points}} is invalid.
628 @see: I{Charles Jekel}'s U{"Least Squares Ellipsoid Fit"<https://Jekel.me/2020/Least-Squares-Ellipsoid-Fit/>}
629 and U{numpy.linalg.lstsq<https://NumPy.org/doc/stable/reference/generated/numpy.linalg.lstsq.html>}.
630 '''
631 n, ps = len2(points)
632 if n < 3:
633 raise PointsError(points=n, txt=_too_(_few_))
635 A = []
636 for i, p in enumerate(ps):
637 v = _otherV3d(useZ=useZ, i=i, points=p)
638 A.append(v.x2y2z2)
640 with _numpy(triaxum5, n=n) as _np:
641 A = _np.array(A)
642 b = _1_0_1T * n
643 T, R, rk = _np.least_squares3(A, b)
645 def _1_sqrt(x):
646 return sqrt(_1_0 / x) if x else _0_0 # INF
648 a, b, c = map(_1_sqrt, T)
649 return Triaxum5Tuple(a, b, c, rk, R)
652def _tricenter3d2(p1, r1, p2, r2, p3, r3, eps=EPS4, useZ=True, dLL3=False, **kwds):
653 # (INTERNAL) Trilaterate and disambiguate the 3-D center
654 d, kwds = None, _xkwds(kwds, eps=eps, coin=True)
655 if useZ and p1.z != p2.z != p3.z: # ignore z if all match
656 a, b = _trilaterate3d2(p1, r1, p2, r2, p3, r3, **kwds)
657 if a is b: # no unambiguity
658 c = a # == b
659 else:
660 c = a.plus(b).times(_0_5) # mean
661 if not a.isconjugateTo(b, minum=0, eps=eps):
662 if dLL3: # deltas as (lat, lon, height)
663 a = a.toLatLon()
664 b = b.toLatLon()
665 d = LatLon3Tuple(b.lat - a.lat,
666 b.lon - a.lon,
667 b.height - a.height, name=_deltas_)
668 else:
669 d = b.minus(a) # vectorial deltas
670 else:
671 if useZ: # pass z to Vector if given
672 kwds = _xkwds(kwds, z=p1.z)
673 c = _trilaterate2d2(p1.x, p1.y, r1,
674 p2.x, p2.y, r2,
675 p3.x, p3.y, r3, **kwds)
676 return c, d
679def trilaterate2d2(x1, y1, radius1, x2, y2, radius2, x3, y3, radius3,
680 eps=None, **Vector_and_kwds):
681 '''Trilaterate three circles, each given as a (2-D) center and a radius.
683 @arg x1: Center C{x} coordinate of the 1st circle (C{scalar}).
684 @arg y1: Center C{y} coordinate of the 1st circle (C{scalar}).
685 @arg radius1: Radius of the 1st circle (C{scalar}).
686 @arg x2: Center C{x} coordinate of the 2nd circle (C{scalar}).
687 @arg y2: Center C{y} coordinate of the 2nd circle (C{scalar}).
688 @arg radius2: Radius of the 2nd circle (C{scalar}).
689 @arg x3: Center C{x} coordinate of the 3rd circle (C{scalar}).
690 @arg y3: Center C{y} coordinate of the 3rd circle (C{scalar}).
691 @arg radius3: Radius of the 3rd circle (C{scalar}).
692 @kwarg eps: Tolerance to check the trilaterated point I{delta} on
693 all 3 circles (C{scalar}) or C{None} for no checking.
694 @kwarg Vector_and_kwds: Optional class C{B{Vector}=None} to return
695 the trilateration and optionally, additional B{C{Vector}}
696 keyword arguments).
698 @return: Trilaterated point as C{B{Vector}(x, y, **B{Vector_kwds})}
699 or L{Vector2Tuple}C{(x, y)} if C{B{Vector} is None}.
701 @raise IntersectionError: No intersection, near-concentric or -colinear
702 centers, trilateration failed some other way
703 or the trilaterated point is off one circle
704 by more than B{C{eps}}.
706 @raise UnitError: Invalid B{C{radius1}}, B{C{radius2}} or B{C{radius3}}.
708 @see: U{Issue #49<https://GitHub.com/mrJean1/PyGeodesy/issues/49>},
709 U{Find X location using 3 known (X,Y) location using trilateration
710 <https://math.StackExchange.com/questions/884807>} and function
711 L{pygeodesy.trilaterate3d2}.
712 '''
713 return _trilaterate2d2(x1, y1, radius1,
714 x2, y2, radius2,
715 x3, y3, radius3, eps=eps, **Vector_and_kwds)
718def _trilaterate2d2(x1, y1, radius1, x2, y2, radius2, x3, y3, radius3,
719 coin=False, eps=None,
720 Vector=None, **Vector_kwds):
721 # (INTERNAL) Trilaterate three circles, see L{pygeodesy.trilaterate2d2}
723 def _abct4(x1, y1, r1, x2, y2, r2):
724 a = x2 - x1
725 b = y2 - y1
726 t = _tri3near2far(r1, r2, hypot(a, b), coin)
727 c = _0_0 if t else (hypot2_(r1, x2, y2) - hypot2_(r2, x1, y1))
728 return a, b, c, t
730 def _astr(**kwds): # kwds as (name=value, ...) strings
731 return Fmt.PAREN(_COMMASPACE_(*(Fmt.EQUALg(*t) for t in kwds.items())))
733 r1 = Radius_(radius1=radius1)
734 r2 = Radius_(radius2=radius2)
735 r3 = Radius_(radius3=radius3)
737 a, b, c, t = _abct4(x1, y1, r1, x2, y2, r2)
738 if t:
739 raise IntersectionError(_and(_astr(x1=x1, y1=y1, radius1=r1),
740 _astr(x2=x2, y2=y2, radius2=r2)), txt=t)
742 d, e, f, t = _abct4(x2, y2, r2, x3, y3, r3)
743 if t:
744 raise IntersectionError(_and(_astr(x2=x2, y2=y2, radius2=r2),
745 _astr(x3=x3, y3=y3, radius3=r3)), txt=t)
747 _, _, _, t = _abct4(x3, y3, r3, x1, y1, r1)
748 if t:
749 raise IntersectionError(_and(_astr(x3=x3, y3=y3, radius3=r3),
750 _astr(x1=x1, y1=y1, radius1=r1)), txt=t)
752 q = fdot_(a, e, -b, d) * _2_0
753 if isnear0(q):
754 t = _no_(_intersection_)
755 raise IntersectionError(_and(_astr(x1=x1, y1=y1, radius1=r1),
756 _astr(x2=x2, y2=y2, radius2=r2),
757 _astr(x3=x3, y3=y3, radius3=r3)), txt=t)
758 t = Vector2Tuple(fdot_(c, e, -b, f) / q,
759 fdot_(a, f, -c, d) / q, name=typename(trilaterate2d2))
761 if eps and eps > 0: # check distances to center vs radius
762 for x, y, r in ((x1, y1, r1), (x2, y2, r2), (x3, y3, r3)):
763 d = hypot(x - t.x, y - t.y)
764 e = fabs(d - r)
765 if e > eps:
766 t = _and(Float(delta=e).toRepr(), r.toRepr(),
767 Float(distance=d).toRepr(), t.toRepr())
768 raise IntersectionError(t, txt=Fmt.exceeds_eps(eps))
770 if Vector is not None:
771 t = Vector(t.x, t.y, **_xkwds(Vector_kwds, name=t.name))
772 return t
775def _trilaterate3d2(c1, r1, c2, r2, c3, r3, eps=EPS4, coin=False, # MCCABE 13
776 **clas_Vector_and_kwds):
777 # (INTERNAL) Intersect three spheres or circles, see function
778 # L{pygeodesy.trilaterate3d2}, separated to allow callers to
779 # embellish exceptions, like C{FloatingPointError}s from C{numpy}
781 def _Arow4(c):
782 # make a row for matrix A (1, -2x, -2y, -2z)
783 return _1_0_1T + c.times(_N_2_0).xyz3
785 def _F4d3(F):
786 # map numpy 4-vector to floats and xyz3
787 T = map2(float, F)
788 t = T[1:]
789 return T, t, Vector3d(*t)
791 def _N3(t01, x, z):
792 # compute x, y and z and return as B{C{clas}} or B{C{Vector}}
793 v = x.plus(z.times(t01))
794 n = typename(trilaterate3d2)
795 return _nVc(v, **_xkwds(clas_Vector_and_kwds, name=n))
797 c2 = _otherV3d(center2=c2, NN_OK=False)
798 c3 = _otherV3d(center3=c3, NN_OK=False)
799 rs = (r1, Radius_(radius2=r2, low=EPS),
800 Radius_(radius3=r3, low=EPS))
802 # get matrix A[3 x 4], its null_space Z and pseudo-inverse
803 A = [_Arow4(c) for c in (c1, c2, c3)]
804 with _numpy(trilaterate3d2, A=A, eps=eps) as _np:
805 Z, _ = _np.null_space2(A, eps)
806 if Z is not None:
807 Z, _, z = _F4d3(Z) # [4 x 1]
808 z2 = z.length2
809 A = _np.pseudo_inverse(A) # [4 x 3]
810 bs = [c.length2 for c in (c1, c2, c3)]
811 # perturb radii slightly by eps and eps * 4
812 for p in _tri5perturbs(eps, min(rs)):
813 b = [((r + p)**2 - b) for r, b in zip(rs, bs)] # [3 x 1]
814 X, t, x = _F4d3(A.dot(b)) # [4 * 1]
815 # quadratic polynomial, coefficients order (^0, ^1, ^2)
816 t = _np.real_roots(fdot(X, _N_1_0, *t),
817 fdot(Z, _N_0_5, *t) * _2_0, z2)
818 if t:
819 v = _N3(t[0], x, z)
820 if len(t) < 2: # one intersection
821 t = v, v
822 elif fabs(t[0] - t[1]) < eps: # abutting
823 t = v, v
824 else: # "lowest" intersection first (to avoid test failures)
825 u = _N3(t[1], x, z)
826 t = (u, v) if u.x < v.x else (v, u)
827 return t
829 def _no_itersection(coin, Z):
830 t = _no_(_intersection_)
831 if coin:
832 def _reprs(*crs):
833 return _and(*map(repr, crs))
835 r = repr(r1) if r1 == r2 == r3 else _reprs(r1, r2, r3)
836 t = _SPACE_(t, _of_, _reprs(c1, c2, c3), _with_, _radius_, r)
837 elif Z is None:
838 t = _COMMASPACE_(t, _no_(typename(_numpy.null_space2)))
839 return t
841 # coincident, concentric, colinear, too distant, no intersection:
842 # create the explanation and and throw an IntersectionError
843 t = _tri4near2far(c1, r1, c2, r2, coin) or \
844 _tri4near2far(c1, r1, c3, r3, coin) or \
845 _tri4near2far(c2, r2, c3, r3, coin) or (
846 _colinear_ if _iscolinearWith(c1, c2, c3, eps=eps) else
847 _no_itersection(coin, Z))
848 raise IntersectionError(t, txt=None)
851def _tri3near2far(r1, r2, h, coin):
852 # check for near-coincident/-concentric or too distant spheres/circles
853 return _too_(Fmt.distant(h)) if h > (r1 + r2) else (_near_(
854 _coincident_ if coin else _concentric_) if h < fabs(r1 - r2) else NN)
857def _tri4near2far(c1, r1, c2, r2, coin):
858 # check for near-coincident/-concentric or too distant spheres/circles
859 t = _tri3near2far(r1, r2, c1.minus(c2).length, coin)
860 return _SPACE_(c1.name, _and_, c2.name, t) if t else NN
863def _tri5perturbs(eps, r):
864 # perturb the radii to handle this corner case
865 # <https://GitHub.com/mrJean1/PyGeodesy/issues/49>
866 yield _0_0
867 if eps and eps > 0:
868 p = max(eps, EPS)
869 yield p
870 m = min(p, r)
871 yield -m
872 q = max(eps * _4_0, _EPS4e8)
873 if q > p:
874 yield q
875 q = min(q, r)
876 if q > m:
877 yield -q
879# **) MIT License
880#
881# Copyright (C) 2016-2025 -- mrJean1 at Gmail -- All Rights Reserved.
882#
883# Permission is hereby granted, free of charge, to any person obtaining a
884# copy of this software and associated documentation files (the "Software"),
885# to deal in the Software without restriction, including without limitation
886# the rights to use, copy, modify, merge, publish, distribute, sublicense,
887# and/or sell copies of the Software, and to permit persons to whom the
888# Software is furnished to do so, subject to the following conditions:
889#
890# The above copyright notice and this permission notice shall be included
891# in all copies or substantial portions of the Software.
892#
893# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
894# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
895# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
896# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
897# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
898# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
899# OTHER DEALINGS IN THE SOFTWARE.