Coverage for pygeodesy/vector3d.py : 92%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# -*- coding: utf-8 -*-
Function L{intersection3d3}, L{intersections2}, L{iscolinearWith}, L{nearestOn}, L{parse3d}, L{sumOf}, L{trilaterate2d2} and L{trilaterate3d2}. '''
NumPyError, _TypeError, _ValueError, \ VectorError, _xError, _xkwds_popitem _and_, _colinear_, _COMMA_, _COMMASPACE_, \ _datum_, _h_, _height_, _intersection_, \ _invalid_, _name_, _near_concentric_, \ _no_, _SPACE_, _too_, _xyz_, _y_, _z_, \ _0_0, _1_0, _2_0 Vector3Tuple # Vector4Tuple
'''Extended 3-D vector.
In a geodesy context, these may be used to represent: - n-vector representing a normal to point on earth's surface - earth-centered, earth-fixed cartesian (= spherical n-vector) - great circle normal to vector - motion vector on earth's surface - etc. '''
'''Check whether this and two other points are colinear.
@arg point1: One point (L{Vector3d}, C{Vector3Tuple} or C{Vector4Tuple}). @arg point2: Another point (L{Vector3d}, C{Vector3Tuple} or C{Vector4Tuple}). @kwarg eps: Tolerance (C{scalar}), same units as C{x}, C{y}, and C{z}.
@return: C{True} if this point is colinear with B{C{point1}} and B{C{point2}}, C{False} otherwise.
@raise TypeError: Invalid B{C{point1}} or B{C{point2}}.
@see: Method L{nearestOn}. ''' v = self if self.name else _otherV3d(this=self) return _iscolinearWith(v, point1, point2, eps=eps)
'''Locate the point between two points closest to this point.
@arg other1: Start point (L{Vector3d}). @arg other2: End point (L{Vector3d}). @kwarg within: If C{True} return the closest point between the given points, otherwise the closest point on the extended line through both points (C{bool}).
@return: Closest point (L{Vector3d}).
@raise TypeError: If B{C{other1}} or B{C{other2}} is not L{Vector3d}.
@see: Method L{sphericalTrigonometry.LatLon.nearestOn3} and U{3-D Point-Line distance<https://MathWorld.Wolfram.com/ Point-LineDistance3-Dimensional.html>}. ''' _otherV3d(other2=other2), within=within)
'''Parse an C{"x, y, z"} string to a L{Vector3d} instance.
@arg str3d: X, y and z string (C{str}), see function L{parse3d}. @kwarg sep: Optional separator (C{str}). @kwarg name: Optional instance name (C{str}), overriding this name.
@return: The instance (L{Vector3d}).
@raise VectorError: Invalid B{C{str3d}}. ''' name=name or self.name)
'''Trilaterate this and two other spheres, each given as a (3-D) center and a radius.
@arg radius: Radius of this sphere (same C{units} as this C{x}, C{y} and C{z}). @arg center2: Center of the 2nd sphere (L{Vector3d}, C{Vector3Tuple} or C{Vector4Tuple}). @arg radius2: Radius of this sphere (same C{units} as this C{x}, C{y} and C{z}). @arg center3: Center of the 3rd sphere (L{Vector3d}, C{Vector3Tuple} or C{Vector4Tuple}). @arg radius3: Radius of the 3rd sphere (same C{units} as this C{x}, C{y} and C{z}). @kwarg eps: Tolerance (C{scalar}), same units as C{x}, C{y}, and C{z}.
@return: 2-Tuple with two trilaterated points, each an instance of this L{Vector3d} (sub-)class. Both points are the same instance if all three spheres intersect or abut in a single point.
@raise ImportError: Package C{numpy} not found, not installed or older than version 1.15.
@raise IntersectionError: Near-concentric, colinear, too distant or non-intersecting spheres or C{numpy} issue.
@raise TypeError: Invalid B{C{center2}} or B{C{center3}}.
@raise UnitError: Invalid B{C{radius}}, B{C{radius2}} or B{C{radius3}}.
@note: Package U{numpy<https://pypi.org/project/numpy>} is required, version 1.15 or later.
@see: Norrdine, A. U{I{An Algebraic Solution to the Multilateration Problem}<https://www.ResearchGate.net/publication/ 275027725_An_Algebraic_Solution_to_the_Multilateration_Problem>} and U{I{implementation}<https://www.ResearchGate.net/publication/ 288825016_Trilateration_Matlab_Code>}. ''' Radius_(radius, low=eps), center2, radius2, center3, radius3, eps=eps, Vector=self.classof) raise _xError(x, center=self, radius=radius, center2=center2, radius2=radius2, center3=center3, radius3=radius3)
Vector=None, **Vector_kwds): '''Compute the intersection point of two lines, each defined by or through a start and end point.
@arg start1: Start point of the first line (L{Vector3d}, C{Vector3Tuple} or C{Vector4Tuple}). @arg end1: End point of the first line (L{Vector3d}, C{Vector3Tuple} or C{Vector4Tuple}). @arg start2: Start point of the second line (L{Vector3d}, C{Vector3Tuple} or C{Vector4Tuple}). @arg end2: End point of the second line (L{Vector3d}, C{Vector3Tuple} or C{Vector4Tuple}). @kwarg eps: Tolerance for skew line distance and length (C{EPS}). @kwarg useZ: If C{True} use the Z component, if C{False} force C{z=0} (C{bool}). @kwarg Vector: Class to return intersections (L{Vector3d} or C{Vector3Tuple}) or C{None} for L{Vector3d}. @kwarg Vector_kwds: Optional, additional B{C{Vector}} keyword arguments, ignored if C{B{Vector}=None}.
@return: An L{Intersection3Tuple}C{(point, outside1, outside2)} with C{point} a C{Vector3d} or B{C{Vector}}.
@raise IntersectionError: Invalid, skew, non-coplanar or otherwise non-intersecting lines.
@see: U{Line-line intersection<https://MathWorld.Wolfram.com/Line-LineIntersection.html>} and U{line-line distance<https://MathWorld.Wolfram.com/Line-LineDistance.html>}, U{skew lines<https://MathWorld.Wolfram.com/SkewLines.html>} and U{point-line distance<https://MathWorld.Wolfram.com/Point-LineDistance3-Dimensional.html>}. ''' raise _xError(x, start1=start1, end1=end1, start2=start2, end2=end2)
# (INTERNAL) Intersect two lines, see L{intersection} above, # separated to allow callers to embellish any exceptions
d = d / ab.length if d > e: # argonic, skew lines distance raise IntersectionError(skew_d=d, txt=_no_(_intersection_))
# co-planar, non-skew lines if c.length < e: return s1, 0, 0 elif e2.minus(e1).length < e: return e1, 0, 0 # like _nearestOn(s1, s2, e2, within=False, eps=e) raise IntersectionError(length2=ab2, txt=_no_(_intersection_))
Vector=None, **Vector_kwds): '''Compute the intersection of two spheres or circles, each defined by a center point and a radius.
@arg center1: Center of the first sphere or circle (L{Vector3d}, C{Vector3Tuple} or C{Vector4Tuple}). @arg radius1: Radius of the first sphere or circle (same units as the B{C{center1}} coordinates). @arg center2: Center of the second sphere or circle (L{Vector3d}, C{Vector3Tuple} or C{Vector4Tuple}). @arg radius2: Radius of the second sphere or circle (same units as the B{C{center1}} and B{C{center2}} coordinates). @kwarg sphere: If C{True} compute the center and radius of the intersection of two spheres. If C{False}, ignore the C{z}-component and compute the intersection of two circles (C{bool}). @kwarg Vector: Class to return intersections (L{Vector3d} or C{Vector3Tuple}) or C{None} for L{Vector3d}. @kwarg Vector_kwds: Optional, additional B{C{Vector}} keyword arguments, ignored if C{B{Vector}=None}.
@return: If B{C{sphere}} is C{True}, a 2-Tuple of the C{center} and C{radius} of the intersection of the spheres. The C{radius} is C{0.0} for abutting spheres.
If B{C{sphere}} is C{False}, a 2-tuple of the intersection points of two circles. For abutting circles, both points are the same B{C{Vector}} instance.
@raise IntersectionError: Concentric, invalid or non-intersecting spheres or circles.
@raise UnitError: Invalid B{C{radius1}} or B{C{radius2}}.
@see: U{Sphere-Sphere<https://MathWorld.Wolfram.com/Sphere- SphereIntersection.html>} and U{circle-circle <https://MathWorld.Wolfram.com/Circle-CircleIntersection.html>} intersections. ''' center2, Radius_(radius2=radius2), sphere=sphere, Vector=Vector, **Vector_kwds) except (TypeError, ValueError) as x: raise _xError(x, center1=center1, radius1=radius1, center2=center2, radius2=radius2)
Vector=None, **Vector_kwds): # (INTERNAL) Intersect two spheres or circles, see L{intersections2} # above, separated to allow callers to embellish any exceptions
fdot(xy1, u.y, u.x, c1.y), _0_0)
raise IntersectionError(_near_concentric_)
# compute intersections with c1 at (0, 0) and c2 at (d, 0), like # <https://MathWorld.Wolfram.com/Circle-CircleIntersection.html> elif y < 0: raise IntersectionError(_invalid_) else: # abutting y = _0_0 raise IntersectionError(_too_(Fmt.distant(t))) else: # abutting
c2 if x > EPS1 else c1.plus(u.times(x)))
else: # abutting circles
'''Check whether a point is colinear with two other points.
@arg point: The point (L{Vector3d}, C{Vector3Tuple} or C{Vector4Tuple}). @arg point1: First point (L{Vector3d}, C{Vector3Tuple} or C{Vector4Tuple}). @arg point2: Second point (L{Vector3d}, C{Vector3Tuple} or C{Vector4Tuple}). @kwarg eps: Tolerance (C{scalar}), same units as C{x}, C{y} and C{z}.
@return: C{True} if B{C{point}} is colinear B{C{point1}} and B{C{point2}}, C{False} otherwise.
@raise TypeError: Invalid B{C{point}}, B{C{point1}} or B{C{point2}}.
@see: Function L{nearestOn}. ''' point1, point2, eps=eps)
# (INTERNAL) Check colinear, see L{isColinear} above, # separated to allow callers to embellish any exceptions
Vector=None, **Vector_kwds): '''Locate the point between two points closest to a reference.
@arg point: Reference point (L{Vector3d}, C{Vector3Tuple} or C{Vector4Tuple}). @arg point1: Start point (L{Vector3d}, C{Vector3Tuple} or C{Vector4Tuple}). @arg point2: End point (L{Vector3d}, C{Vector3Tuple} or C{Vector4Tuple}). @kwarg within: If C{True} return the closest point between both given points, otherwise the closest point on the extended line through both points (C{bool}). @kwarg Vector: Class to return closest point (L{Vector3d} or C{Vector3Tuple}) or C{None} for L{Vector3d}. @kwarg Vector_kwds: Optional, additional B{C{Vector}} keyword arguments, ignored if C{B{Vector}=None}.
@return: Closest point (L{Vector3d} or C{Vector}).
@raise TypeError: Invalid B{C{point}}, B{C{point1}} or B{C{point2}}.
@see: Methods L{sphericalTrigonometry.LatLon.nearestOn3} and L{sphericalTrigonometry.LatLon.nearestOn3} U{3-D Point-Line distance<https://MathWorld.Wolfram.com/ Point-LineDistance3-Dimensional.html>}. ''' _otherV3d(point1=point1), _otherV3d(point2=point2), within=within)
# (INTERNAL) Get closest point, see L{nearestOn} above, # separated to allow callers to embellish any exceptions else: p2 if (within and t > (_1_0 - eps)) else p1.plus(p21.times(t)))
# (INTERNAL) Return the nullspace and rank of matrix A # @see: <https://SciPy-Cookbook.ReadTheDocs.io/items/RankNullspace.html>, # <https://NumPy.org/doc/stable/reference/generated/numpy.linalg.svd.html>, # <https://StackOverflow.com/questions/19820921>, # <https://StackOverflow.com/questions/2992947> and # <https://StackOverflow.com/questions/5889142> raise _AssertionError(shape=m, txt=modulename(_null_space2, True)) # if needed, square A, pad with zeros # try: # no numpy.linalg.null_space <https://docs.SciPy.org/doc/> # return scipy.linalg.null_space(A) # XXX no scipy.linalg? # except AttributeError: # pass raise _AssertionError(shape=s, txt=modulename(_null_space2, True)) raise _AssertionError(eps=e, txt=modulename(_null_space2, True)) else: # coincident, colinear, concentric centers, ambiguous, etc. # del A, s, vh # release numpy
# check B{C{name#}} vector instance, return Vector3d raise _AssertionError(name_v=MISSING)
except AttributeError: # no _x_ or _y_ attr pass raise _xotherError(Vector3d(0, 0, 0), v, name=name, up=2)
'''Parse an C{"x, y, z"} string.
@arg str3d: X, y and z values (C{str}). @kwarg sep: Optional separator (C{str}). @kwarg name: Optional instance name (C{str}). @kwarg Vector: Optional class (L{Vector3d}). @kwarg Vector_kwds: Optional B{C{Vector}} keyword arguments, ignored if C{B{Vector}=None}.
@return: New B{C{Vector}} or if B{C{Vector}} is C{None}, a L{Vector3Tuple}C{(x, y, z)}.
@raise VectorError: Invalid B{C{str3d}}. ''' raise _ValueError(len=n) except (TypeError, ValueError) as x: raise VectorError(str3d=str3d, txt=str(x))
'''Compute the vectorial sum of several vectors.
@arg vectors: Vectors to be added (L{Vector3d}[]). @kwarg Vector: Optional class for the vectorial sum (L{Vector3d}). @kwarg Vector_kwds: Optional B{C{Vector}} keyword arguments, ignored if C{B{Vector}=None}.
@return: Vectorial sum as B{C{Vector}} or if B{C{Vector}} is C{None}, a L{Vector3Tuple}C{(x, y, z)}.
@raise VectorError: No B{C{vectors}}. ''' raise VectorError(vectors=n, txt=MISSING)
fsum(v.y for v in vectors), fsum(v.z for v in vectors))
'''Trilaterate three circles, each given as a (2d) center and a radius.
@arg x1: Center C{x} coordinate of the 1st circle (C{scalar}). @arg y1: Center C{y} coordinate of the 1st circle (C{scalar}). @arg radius1: Radius of the 1st circle (C{scalar}). @arg x2: Center C{x} coordinate of the 2nd circle (C{scalar}). @arg y2: Center C{y} coordinate of the 2nd circle (C{scalar}). @arg radius2: Radius of the 2nd circle (C{scalar}). @arg x3: Center C{x} coordinate of the 3rd circle (C{scalar}). @arg y3: Center C{y} coordinate of the 3rd circle (C{scalar}). @arg radius3: Radius of the 3rd circle (C{scalar}). @kwarg eps: Check the trilaterated point I{delta} on all 3 circles (C{scalar}) or C{None}.
@return: Trilaterated point as L{Vector2Tuple}C{(x, y)}.
@raise IntersectionError: No intersection, colinear or near-concentric centers, trilateration failed some other way or the trilaterated point is off one circle by more than B{C{eps}}.
@raise UnitError: Invalid B{C{radius1}}, B{C{radius2}} or B{C{radius3}}.
@see: U{Issue #49<https://GitHub.com/mrJean1/PyGeodesy/issues/49>}, U{Find X location using 3 known (X,Y) location using trilateration <https://math.StackExchange.com/questions/884807>} and L{trilaterate3d2} '''
return Fmt.PAREN(_COMMASPACE_(*(Fmt.EQUAL(*t) for t in kwds.items())))
raise IntersectionError(_and(_astr(x1=x1, y1=y1, radius1=r1), _astr(x2=x2, y2=y2, radius2=r2)), txt=t)
raise IntersectionError(_and(_astr(x2=x2, y2=y2, radius2=r2), _astr(x3=x3, y3=y3, radius3=r3)), txt=t)
raise IntersectionError(_and(_astr(x3=x3, y3=y3, radius3=r3), _astr(x1=x1, y1=y1, radius1=r1)), txt=t)
t = _no_(_intersection_) raise IntersectionError(_and(_astr(x1=x1, y1=y1, radius1=r1), _astr(x2=x2, y2=y2, radius2=r2), _astr(x3=x3, y3=y3, radius3=r3)), txt=t) (a * f - c * d) / q, name=trilaterate2d2.__name__)
Float(distance=d).toRepr()) raise IntersectionError(t, txt=Fmt.exceeds_eps(eps))
eps=EPS, Vector=None, **Vector_kwds): '''Trilaterate three spheres, each given as a (3-D) center and a radius.
@arg center1: Center of the 1st sphere (L{Vector3d}, C{Vector3Tuple} or C{Vector4Tuple}). @arg radius1: Radius of the 1st sphere (same C{units} as C{x}, C{y} and C{z}). @arg center2: Center of the 2nd sphere (L{Vector3d}, C{Vector3Tuple} or C{Vector4Tuple}). @arg radius2: Radius of this sphere (same C{units} as C{x}, C{y} and C{z}). @arg center3: Center of the 3rd sphere (L{Vector3d}, C{Vector3Tuple} or C{Vector4Tuple}). @arg radius3: Radius of the 3rd sphere (same C{units} as C{x}, C{y} and C{z}). @kwarg eps: Tolerance (C{scalar}), same units as C{x}, C{y}, and C{z}. @kwarg Vector: Class to return intersections (L{Vector3d} or C{Vector3Tuple}) or C{None} for L{Vector3d}. @kwarg Vector_kwds: Optional, additional B{C{Vector}} keyword arguments, ignored if C{B{Vector}=None}.
@return: 2-Tuple with two trilaterated points, each a B{C{Vector}} instance. Both points are the same instance if all three spheres abut/intersect in a single point.
@raise ImportError: Package C{numpy} not found, not installed or older than version 1.15.
@raise IntersectionError: Near-concentric, colinear, too distant or non-intersecting spheres.
@raise NumPyError: Some C{numpy} issue.
@raise TypeError: Invalid B{C{center1}}, B{C{center2}} or B{C{center3}}.
@raise UnitError: Invalid B{C{radius1}}, B{C{radius2}} or B{C{radius3}}.
@note: Package U{numpy<https://pypi.org/project/numpy>} is required, version 1.15 or later.
@see: Norrdine, A. U{I{An Algebraic Solution to the Multilateration Problem}<https://www.ResearchGate.net/publication/ 275027725_An_Algebraic_Solution_to_the_Multilateration_Problem>} and U{I{implementation}<https://www.ResearchGate.net/publication/ 288825016_Trilateration_Matlab_Code>} and L{trilaterate2d2}. ''' Radius_(radius1=radius1, low=eps), center2, radius2, center3, radius3, eps=eps, Vector=Vector, **Vector_kwds) raise _xError(x, center1=center1, radius1=radius1, center2=center2, radius2=radius2, center3=center3, radius3=radius3)
# (INTERNAL) Intersect three spheres or circles, see L{trilaterate3d2} # above, separated to allow callers to embellish any exceptions, like # C{FloatingPointError}s from C{numpy}
# map numpy 4-vector to floats and split
# compute x, y and z and return as Vector
# non-complex roots of a polynomial
Radius_(radius3=r3, low=eps)]
# get null_space Z and pseudo-inverse A, once except Exception as X: # mostly FloatingPointError? raise NumPyError(X.__class__.__name__, txt=str(X)) finally: # restore numpy error handling raise _trilaterror(c1, r1, c2, r2, c3, r3, eps)
# perturbe radii to handle corner cases like this # <https://GitHub.com/mrJean1/PyGeodesy/issues/49> else: ps = _0_0, p1, -p1 else: # t = () # quadratic polynomial coefficients, ordered (^0, ^1, ^2) z.dot(x) * _2_0 - Z0, # fdot(Z, -_0_5, *x.xyz) * 2 z.length2) # fdot(Z, _0_0, *z.xyz) except Exception as X: # mostly FloatingPointError? raise NumPyError(X.__class__.__name__, txt=str(X)) finally: # restore numpy error handling else: # coincident, concentric, colinear, too distant, no intersection, etc. raise _trilaterror(c1, r1, c2, r2, c3, r3, eps)
t = v, v t = v, v else: # "lowest" intersection first (to avoid test failures)
# return FloatingPointError with the cause of the error
_txt(c1, r1, c3, r3) or \ _txt(c2, r2, c3, r3) or (_colinear_ if _iscolinearWith(c1, c2, c3, eps=eps) else _no_(_intersection_))
# check for near-concentric or too distant spheres/circles _near_concentric_ if h < abs(r1 - r2) else NN)
# return a named Vector instance
'''(INTERNAL) Get an C{(x, y, z, name)} 4-tuple. ''' except (TypeError, ValueError) as x: d = dict(zip((_xyz_, _y_, _z_), t)) raise Error(txt=str(x), **d)
'''(INTERNAL) Get an C{(x, y, z, h, d, name)} 6-tuple. '''
or getattr(xyz, _h_, None) \ or getattr(ll, _height_, None)
or getattr(ll, _datum_, None)
# **) MIT License # # Copyright (C) 2016-2021 -- mrJean1 at Gmail -- All Rights Reserved. # # Permission is hereby granted, free of charge, to any person obtaining a # copy of this software and associated documentation files (the "Software"), # to deal in the Software without restriction, including without limitation # the rights to use, copy, modify, merge, publish, distribute, sublicense, # and/or sell copies of the Software, and to permit persons to whom the # Software is furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included # in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR # OTHER DEALINGS IN THE SOFTWARE. |