Coverage for pygeodesy/vector3dBase.py : 97%

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 -*-
A pure Python implementation of vector-based functions by I{(C) Chris Veness 2011-2015} published under the same MIT Licence**, see U{Vector-based geodesy <https://www.Movable-Type.co.UK/scripts/latlong-vectors.html>}. '''
isnear0, isnear1, _1_0 # from pygeodesy.fsums import fsum1_ # from .fmath _y_, _z_ property_doc_, _update_all # from pygeodesy.utily import sincos2 # in Vector3dBase.rotate below
'''(INTERNAL) Generic 3-D vector base class.
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. '''
'''New L{Vector3d} or C{Vector3dBase} instance.
The vector may be normalised or use x, y, z for position and distance from earth centre or height relative to the surface of the earth' sphere or ellipsoid.
@arg x_xyz: X component of vector (C{scalar}) or a (3-D) vector (C{Cartesian}, L{Ecef9Tuple}, C{Nvector}, L{Vector3d}, L{Vector3Tuple}, L{Vector4Tuple} or a C{tuple} or C{list} of 3+ C{scalar} values). @kwarg y: Y component of vector (C{scalar}), ignored if B{C{x_xyz}} is not C{scalar}, otherwise same units as B{C{x_xyz}}. @kwarg z: Z component of vector (C{scalar}), ignored if B{C{x_xyz}} is not C{scalar}, otherwise same units as B{C{x_xyz}}. @kwarg ll: Optional latlon reference (C{LatLon}). @kwarg name: Optional name (C{str}).
@raise VectorError: Invalid B{C{x_xyz}}. ''' else:
'''Return the norm of this vector.
@return: Norm, unit length (C{float}); '''
'''Add this to an other vector (L{Vector3d}).
@return: Vectorial sum (L{Vector3d}).
@raise TypeError: Incompatible B{C{other}} C{type}. '''
'''Is this vector non-zero? '''
'''Compare this and an other vector
@arg other: The other vector (L{Vector3d}).
@return: -1, 0 or +1 (C{int}).
@raise TypeError: Incompatible B{C{other}} C{type}. ''' +1 if self.length > n else 0)
def __divmod__(self, other): # PYCHOK no cover '''Not implemented.''' return _NotImplemented(self, other)
'''Is this vector equal to an other vector?
@arg other: The other vector (L{Vector3d}).
@return: C{True} if equal, C{False} otherwise.
@raise TypeError: Incompatible B{C{other}} C{type}. '''
def __floordiv__(self, other): # PYCHOK no cover '''Not implemented.''' return _NotImplemented(self, other)
def __format__(self, *other): # PYCHOK no cover '''Not implemented.''' return _NotImplemented(self, *other)
'''Is this vector longer than or equal to an other vector?
@arg other: The other vector (L{Vector3d}).
@return: C{True} if so, C{False} otherwise.
@raise TypeError: Incompatible B{C{other}} C{type}. '''
# def __getitem__(self, key): # '''Return C{item} at index or slice C{[B{key}]}. # ''' # return self.xyz[key]
'''Is this vector longer than an other vector?
@arg other: The other vector (L{Vector3d}).
@return: C{True} if so, C{False} otherwise.
@raise TypeError: Incompatible B{C{other}} C{type}. '''
'''Add this and an other vector I{in-place}, C{this += B{other}}.
@arg other: The other vector (L{Vector3d}).
@raise TypeError: Incompatible B{C{other}} C{type}. '''
'''Cross multiply this and an other vector I{in-place}, C{this @= B{other}}.
@arg other: The other vector (L{Vector3d}).
@raise TypeError: Incompatible B{C{other}} C{type}.
@see: Luciano Ramalho, "Fluent Python", page 397-398, O'Reilly 2016. ''' return self._xyz(self.cross(other))
'''Multiply this vector by a scalar I{in-place}, C{this *= B{scalar}}.
@arg scalar: Factor (C{scalar}).
@raise TypeError: Non-scalar B{C{scalar}}. '''
def __int__(self): # PYCHOK no cover '''Not implemented.''' return _NotImplemented(self)
'''Subtract an other vector from this one I{in-place}, C{this -= B{other}}.
@arg other: The other vector (L{Vector3d}).
@raise TypeError: Incompatible B{C{other}} C{type}. '''
'''Divide this vector by a scalar I{in-place}, C{this /= B{scalar}}.
@arg scalar: The divisor (C{scalar}).
@raise TypeError: Non-scalar B{C{scalar}}. '''
'''Is this vector shorter than or equal to an other vector?
@arg other: The other vector (L{Vector3d}).
@return: C{True} if so, C{False} otherwise.
@raise TypeError: Incompatible B{C{other}} C{type}. '''
# def __len__(self): # '''Return C{3}, always. # ''' # return len(self.xyz)
'''Is this vector shorter than an other vector?
@arg other: The other vector (L{Vector3d}).
@return: C{True} if so, C{False} otherwise.
@raise TypeError: Incompatible B{C{other}} C{type}. '''
'''Compute the cross product of this and an other vector, C{this @ B{other}}.
@arg other: The other vector (L{Vector3d}).
@return: Cross product (L{Vector3d}).
@raise TypeError: Incompatible B{C{other}} C{type}. '''
def __mod__(self, other): # PYCHOK no cover '''Not implemented.''' return _NotImplemented(self, other)
'''Multiply this vector by a scalar, C{this * B{scalar}}.
@arg scalar: Factor (C{scalar}).
@return: Product (L{Vector3d}). '''
'''Is this vector not equal to an other vector?
@arg other: The other vector (L{Vector3d}).
@return: C{True} if so, C{False} otherwise.
@raise TypeError: Incompatible B{C{other}} C{type}. '''
def __pos__(self): # PYCHOK no cover '''Return this vector I{as-is}.
@return: This instance (L{Vector3d}) ''' return self # XXX self.copy()
def __pow__(self, other, *mod): # PYCHOK no cover '''Not implemented.''' return _NotImplemented(self, other, *mod)
__radd__ = __add__ # PYCHOK no cover
def __rdivmod__ (self, other): # PYCHOK no cover '''Not implemented.''' return _NotImplemented(self, other)
def __rfloordiv__(self, other): # PYCHOK no cover '''Not implemented.''' return _NotImplemented(self, other)
'''Compute the cross product of an other and this vector, C{B{other} @ this}.
@arg other: The other vector (L{Vector3d}).
@return: Cross product (L{Vector3d}).
@raise TypeError: Incompatible B{C{other}} C{type}. '''
def __rmod__(self, other): # PYCHOK no cover '''Not implemented.''' return _NotImplemented(self, other)
def __round__(self, ndigits=None): # PYCHOK no cover '''Not implemented.''' return _NotImplemented(self, ndigits=ndigits)
def __rpow__(self, other, *mod): # PYCHOK no cover '''Not implemented.''' return _NotImplemented(self, other, *mod)
def __rsub__(self, other): # PYCHOK no cover '''Subtract this vector from an other vector, C{B{other} - this}.
@arg other: The other vector (L{Vector3d}).
@return: Difference (L{Vector3d}).
@raise TypeError: Incompatible B{C{other}} C{type}. ''' return self.others(other).minus(self)
def __rtruediv__(self, scalar): # PYCHOK no cover '''Not implemented.''' return _NotImplemented(self, scalar)
'''Subtract an other vector from this vector, C{this - B{other}}.
@arg other: The other vector (L{Vector3d}).
@return: Difference (L{Vector3d}).
@raise TypeError: Incompatible B{C{other}} C{type}. '''
'''Divide this vector by a scalar, C{this / B{scalar}}.
@arg scalar: The divisor (C{scalar}).
@return: Quotient (L{Vector3d}).
@raise TypeError: Non-scalar B{C{scalar}}. '''
if _sys_version_info2 < (3, 0): # PYCHOK no cover # <https://docs.Python.org/2/library/operator.html#mapping-operators-to-functions> __div__ = __truediv__ __idiv__ = __itruediv__ __long__ = __int__ __nonzero__ = __bool__ __rdiv__ = __rtruediv__
'''Compute the angle between this and an other vector.
@arg other: The other vector (L{Vector3d}). @kwarg vSign: Optional vector, if supplied (and out of the plane of this and the other), angle is signed positive if this->other is clockwise looking along vSign or negative in opposite direction, otherwise angle is unsigned. @kwarg wrap: Wrap/unroll the angle to +/-PI (C{bool}).
@return: Angle (C{radians}).
@raise TypeError: If B{C{other}} or B{C{vSign}} not a L{Vector3d}. ''' # use vSign as reference to set sign of s
a -= copysign0(PI2, a)
'''Apply a 2-argument function component-pairwise to this and an other vector.
@arg fun2: 2-Argument callable (C{any(scalar, scalar}), return a C{scalar} or L{INT0} result. @arg other_x: Other X component (C{scalar}) or a vector with X, Y and Z components (C{Cartesian}, L{Ecef9Tuple}, C{Nvector}, L{Vector3d}, L{Vector3Tuple} or L{Vector4Tuple}). @arg y_z: Other Y and Z components, positional (C{scalar}, C{scalar}). @kwarg fun2_kwds: Optional keyword arguments for B{C{fun2}}.
@return: New, applied vector (L{Vector3d}).
@raise ValueError: Invalid B{C{other_x}} or B{C{y_z}}. ''' raise _IsnotError(callable.__name__, fun2=fun2)
def _f2(a, b): return fun2(a, b, **fun2_kwds) else:
'''Compute the cross product of this and an other vector.
@arg other: The other vector (L{Vector3d}). @kwarg raiser: Optional, L{CrossError} label if raised (C{str}, non-L{NN}). @kwarg eps0: Near-zero tolerance (C{scalar}), same units as C{x}, C{y}, and C{z}.
@return: Cross product (L{Vector3d}).
@raise CrossError: Zero or near-zero cross product and both B{C{raiser}} and L{pygeodesy.crosserrors} set.
@raise TypeError: Incompatible B{C{other}} C{type}. '''
and max(map1(abs, x, y, z)) < eps0: raise CrossError(raiser, r, txt=t)
'''Get L{CrossError} exceptions (C{bool}). '''
'''Raise L{CrossError} exceptions (C{bool}). '''
'''Divide this vector by a scalar.
@arg divisor: The divisor (C{scalar}).
@return: New, scaled vector (L{Vector3d}).
@raise TypeError: Non-scalar B{C{divisor}}.
@raise VectorError: Invalid or zero B{C{divisor}}. ''' except (ValueError, ZeroDivisionError) as x: raise VectorError(divisor=divisor, txt=str(x))
'''Compute the dot (scalar) product of this and an other vector.
@arg other: The other vector (L{Vector3d}).
@return: Dot product (C{float}).
@raise TypeError: Incompatible B{C{other}} C{type}. ''' fdot(self.xyz, *self.others(other).xyz)
def equals(self, other, units=False): # PYCHOK no cover '''DEPRECATED, use method C{isequalTo}. ''' return self.isequalTo(other, units=units)
'''I{Approximate} the length (norm, magnitude) of this vector (C{Float}).
@see: Properties C{length} and C{length2} and function L{pygeodesy.euclid_}. '''
'''I{Approximate} the different between this and an other vector.
@arg other: Vector to subtract (C{Vector3dBase}).
@return: The lenght I{squared} of the difference (C{Float}).
@raise TypeError: Incompatible B{C{other}} C{type}.
@see: Property C{length2}. '''
'''(INTERNAL) Get the latlon reference (C{LatLon}) or C{None}. '''
'''(INTERNAL) Set the latlon reference (C{LatLon}) or C{None}. '''
'''Locate the vector at a given fraction between (or along) this and an other vector.
@arg other: The other vector (L{Vector3d}). @arg fraction: Fraction between both vectors (C{scalar}, 0.0 for this and 1.0 for the other vector).
@return: Intermediate vector (L{Vector3d}).
@raise TypeError: Incompatible B{C{other}} C{type}. ''' if isnear0(f): # PYCHOK no cover r = self else:
'''Determine whether this and an other vector are conjugates.
@arg other: The other vector (C{Cartesian}, L{Ecef9Tuple}, L{Vector3d}, C{Vector3Tuple} or C{Vector4Tuple}). @kwarg minum: Minimal number of conjugates required (C{int}, 0..3). @kwarg eps: Tolerance for equality and conjugation (C{scalar}), same units as C{x}, C{y}, and C{z}.
@return: C{True} if both vector's components either match or at least C{B{minum}} have opposite signs.
@raise TypeError: Incompatible B{C{other}} C{type}.
@see: Method C{isequalTo}. ''' (a > 0 and b < 0)):
'''Check if this and an other vector are equal or equivalent.
@arg other: The other vector (L{Vector3d}). @kwarg units: Optionally, compare the normalized, unit version of both vectors. @kwarg eps: Tolerance for equality (C{scalar}), same units as C{x}, C{y}, and C{z}.
@return: C{True} if vectors are identical, C{False} otherwise.
@raise TypeError: Incompatible B{C{other}} C{type}.
@see: Method C{isconjugateTo}. ''' else:
'''Get the length (norm, magnitude) of this vector (C{Float}).
@see: Properties L{length2} and L{euclid}. '''
'''Get the length I{squared} of this vector (C{Float}).
@see: Property L{length} and method C{equirectangular}. '''
'''Subtract an other vector from this vector.
@arg other: The other vector (L{Vector3d}).
@return: New vector difference (L{Vector3d}).
@raise TypeError: Incompatible B{C{other}} C{type}. ''' self.y - other.y, self.z - other.z)
'''Return this vector in opposite direction.
@return: New, opposite vector (L{Vector3d}). '''
__neg__ = negate # PYCHOK no cover
'''(INTERNAL) Get the (C{nvectorBase._N_vector_}) '''
'''Refined class comparison.
@arg other: The other vector (L{Vector3d}). @kwarg name_other_up: Overriding C{name=other} and C{up=1} keyword arguments.
@return: The B{C{other}} if compatible.
@raise TypeError: Incompatible B{C{other}} C{type}. ''' _NamedBase.others(self, other, name=name, up=up + 1)
'''Add this vector and an other vector.
@arg other: The other vector (L{Vector3d}).
@return: Vectorial sum (L{Vector3d}).
@raise TypeError: Incompatible B{C{other}} C{type}. ''' self.y + other.y, self.z + other.z)
'''Rotate this vector around an axis by a specified angle.
See U{Rotation matrix from axis and angle<https://WikiPedia.org/wiki/ Rotation_matrix#Rotation_matrix_from_axis_and_angle>} and U{Quaternion-derived rotation matrix<https://WikiPedia.org/wiki/ Quaternions_and_spatial_rotation#Quaternion-derived_rotation_matrix>}.
@arg axis: The axis being rotated around (L{Vector3d}). @arg theta: The angle of rotation (C{radians}).
@return: New, rotated vector (L{Vector3d}). '''
def rotateAround(self, axis, theta): # PYCHOK no cover '''DEPRECATED, use method C{rotate}.''' return self.rotate(axis, theta)
'''Multiply this vector by a scalar.
@arg factor: Scale factor (C{scalar}).
@return: New, scaled vector (L{Vector3d}).
@raise TypeError: Non-scalar B{C{factor}}. '''
'''(INTERNAL) Helper for C{.dividedBy} and C{.times}. '''
'''Multiply this vector's components by separate X, Y and Z factors.
@arg other_x: X scale factor (C{scalar}) or a vector's X, Y, and Z components as scale factors (C{Cartesian}, L{Ecef9Tuple}, C{Nvector}, L{Vector3d}, L{Vector3Tuple}, L{Vector4Tuple}). @arg y_z: Y and Z scale factors (C{scalar}, C{scalar}), ignored if B{C{other_x}} is not C{scalar}.
@return: New, scaled vector (L{Vector3d}).
@raise ValueError: Invalid B{C{other_x}} or B{C{y_z}}. '''
# @deprecated_method # def to2ab(self): # PYCHOK no cover # '''DEPRECATED, use property C{Nvector.philam}. # # @return: A L{PhiLam2Tuple}C{(phi, lam)}. # ''' # return _MODS.formy.n_xyz2philam(self.x, self.y, self.z)
# @deprecated_method # def to2ll(self): # PYCHOK no cover # '''DEPRECATED, use property C{Nvector.latlon}. # # @return: A L{LatLon2Tuple}C{(lat, lon)}. # ''' # return _MODS.formy.n_xyz2latlon(self.x, self.y, self.z)
def to3xyz(self): # PYCHOK no cover '''DEPRECATED, use property L{xyz}. ''' return self.xyz
'''Return a string representation of this vector.
@kwarg prec: Number of decimal places (C{int}). @kwarg fmt: Enclosing format to use (C{str}). @kwarg sep: Separator between components (C{str}).
@return: Vector as "(x, y, z)" (C{str}). '''
'''Normalize this vector to unit length.
@kwarg ll: Optional, original location (C{LatLon}).
@return: Normalized vector (L{Vector3d}). ''' u._fromll = ll
'''(INTERNAL) Get normalized vector (L{Vector3d}). ''' else:
'''Get the X component (C{float}). '''
'''Set the X component, if different (C{float}). '''
'''Get the X, Y and Z components (L{Vector3Tuple}C{(x, y, z)}). '''
'''Set the X, Y and Z components (C{Cartesian}, L{Ecef9Tuple}, C{Nvector}, L{Vector3d}, L{Vector3Tuple}, L{Vector4Tuple} or a C{tuple} or C{list} of 3+ C{scalar} values). ''' self._xyz(*xyz[:3]) else:
'''(INTERNAL) Set the C{_x}, C{_y} and C{_z} attributes. ''' self._y, \ self._z = map1(_float0, x_xyz, *y_z) if y_z else x_xyz.xyz except (AttributeError, TypeError, ValueError) as x: raise VectorError(txt=str(x), **_xyzkwds(y_z, x_xyz=x_xyz))
'''Get the X, Y and Z components I{squared} (3-tuple C{(x**2, y**2, z**2)}). '''
'''Get the Y component (C{float}). '''
'''Set the Y component, if different (C{float}). '''
'''Get the Z component (C{float}). '''
'''Set the Z component, if different (C{float}). '''
'''(INTERNAL) Helper for C{Vector3dBase.apply} and C{Vector3dBase.times_}. ''' (other_x.x, other_x.y, other_x.z) # not .xyz! except (AttributeError, TypeError, ValueError) as x: raise _InvalidError(txt=str(x), **_xyzkwds(y_z, other_x=other_x))
def _xyzkwds(y_z, **xyz): # PYCHOK no cover '''(INTERANL) Helper for C{_other_x_y_z3} and C{Vector3dBase._xyz}. ''' if y_z: d = dict(_zip((_y_, _z_), y_z)) # if y_z else {}, strict=True for x in xyz.values(): d.update(x=x) return d return xyz
# **) MIT License # # Copyright (C) 2016-2022 -- 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. |