def __sub__(self, other): """ subtract two vectors or subtract a scalar from each component @param other: other vector or scalar to subtract from vector @type other: (Lgm_Vector, int, long, float) @return: new vector @rtype: Lgm_Vector @author: Brian Larsen @organization: LANL @contact: [email protected] @version: V1: 22-Dec-2010 (BAL) """ if isinstance(other, Lgm_Vector): # an other vector o_vec = Lgm_Vector(0, 0, 0) Lgm_VecSub(pointer(o_vec), pointer(self), pointer(other)) return o_vec elif isinstance(other, (int, float, long)): x = self.x - other y = self.y - other z = self.z - other return Lgm_Vector(x, y, z) else: raise (ArithmeticError("Cannot subtract %s from a Lgm_Vector" % (type(other))))
def __add__(self, other): """ add two vectors together or add a scalar to each component @param other: other vector or scalar to add to vector @type other: (Lgm_Vector, int, long, float) @return: new vector @rtype: Lgm_Vector @author: Brian Larsen @organization: LANL @contact: [email protected] @version: V1: 22-Dec-2010 (BAL) """ if isinstance(other, Lgm_Vector): # an other vector o_vec = Lgm_Vector(0, 0, 0) Lgm_VecAdd(pointer(o_vec), pointer(self), pointer(other)) return o_vec elif isinstance(other, (int, float, long)): x = self.x + other y = self.y + other z = self.z + other return Lgm_Vector(x, y, z) else: raise (ArithmeticError("Cannot add type %s to a Lgm_Vector" % (type(other))))
def crossProduct(self, other): """ compute the cross product of two vectors Parameters ---------- other : other Lgm_Vector to cross prod (on the right) Returns ------- out : the cross product of the 2 vectors (Lgm_Vector) Examples -------- >>> from lgmpy import Lgm_Vector >>> dat = Lgm_Vector.Lgm_Vector(1,2,3) >>> dat.crossProduct(dat) [0.0, 0.0, 0.0] >>> dat2 = Lgm_Vector.Lgm_Vector(3,2,1) >>> dat.crossProduct(dat2) [-4.0, 8.0, -4.0] """ o_vec = Lgm_Vector(0, 0, 0) Lgm_CrossProduct(pointer(self), pointer(other), pointer(o_vec)) return o_vec
def __mul__(self, other): """ compute the cross product of two vectors or multi a scalar to each component @param other: other vector or scalar to cross product or multiply to vector @type other: (Lgm_Vector, int, long, float) @return: new vector @rtype: Lgm_Vector @author: Brian Larsen @organization: LANL @contact: [email protected] @version: V1: 22-Dec-2010 (BAL) """ if isinstance(other, Lgm_Vector): # another vector return self.crossProduct(other) elif isinstance(other, (int, float, long)): x = self.x * other y = self.y * other z = self.z * other return Lgm_Vector(x, y, z) else: raise (ArithmeticError("Cannot subtract %s from a Lgm_Vector" % (type(other))))
def CartToSph(x, y, z): """ takes an input x, y, z and returns Lat, Lon, Rad Parameters ---------- x : x coordinate (Distance unit) y : y coordinate (Distance unit) z : z coordinate (Distance unit Returns ------- lat : output latitude (deg) lon : output longitude (deg) rad : output radius (Distance unit) Examples -------- >>> from lgmpy import Lgm_Vector >>> Lgm_Vector.CartToSph(0,0,1) (90.0, 0.0, 1.0) >>> import math >>> Lgm_Vector.CartToSph(math.sqrt(2),0.,math.sqrt(2)) (45.0, 0.0, 2.0) """ lat, lon, rad = c_double(), c_double(), c_double() try: assert type(x) == type(y) == type(z) lx, ly, lz = len(x), len(y), len(z) assert lx == ly == lz except TypeError: vec1 = Lgm_Vector(x, y, z) Lgm_CartToSphCoords(pointer(vec1), pointer(lat), pointer(lon), pointer(rad)) return lat.value, lon.value, rad.value except AssertionError: raise (ValueError('All input must be the same length and type')) else: ans = [] for v1, v2, v3 in itertools.izip(x, y, z): vec1 = Lgm_Vector(v1, v2, v3) Lgm_CartToSphCoords(pointer(vec1), pointer(lat), pointer(lon), pointer(rad)) ans.append(copy.copy([lat.value, lon.value, rad.value])) return ans
def SphToCart(lat, lon, rad): """ takes an input Lat, Lon, Rad and returns x, y, z Parameters ---------- lat : latitude (deg) lon : longitude (deg) rad : radius (Distance units) Returns ------- out : Lgm_Vector A Lgm_Vector in Cartesian coords same units as the input Examples -------- >>> from lgmpy import Lgm_Vector >>> Lgm_Vector.SphToCart(0,0,1) [1.0, 0.0, 0.0] >>> Lgm_Vector.SphToCart(45., 45., 1.) [0.5000000..., 0.5, 0.70710678...] """ vec1 = Lgm_Vector(0, 0, 0) try: assert type(lat) == type(lon) == type(rad) llat, llon, lrad = len(lat), len(lon), len(rad) assert llat == llon == lrad except TypeError: Lgm_SphToCartCoords(lat, lon, rad, pointer(vec1)) return vec1 except AssertionError: raise (ValueError('All input must be the same length and type')) else: ans = [] for v1, v2, v3 in itertools.izip(lat, lon, rad): Lgm_SphToCartCoords(v1, v2, v3, pointer(vec1)) ans.append(copy.copy(vec1)) return ans
def slerp(self, other, frac): """ In computer graphics, Slerp is shorthand for spherical linear interpolation, introduced by Ken Shoemake in the context of quaternion interpolation for the purpose of animating 3D rotation. It refers to constant-speed motion along a unit-radius great circle arc, given the ends and an interpolation parameter between 0 and 1. Parameters ---------- other : another Lgm_Vector frac : float Fractional distance between the two unit vectors to interpolate Returns ------- out : Lgm_Vector New vector between self and other Examples -------- >>> from lgmpy import Lgm_Vector >>> v1 = Lgm_Vector.Lgm_Vector(1,2,3) >>> v1.normalize() >>> v2 = Lgm_Vector.Lgm_Vector(3,7,1) >>> v2.normalize() >>> print(v1.slerp(v2, 0) [] >>> print(v1.slerp(v2, 1) [] >>> print(v1.slerp(v2, 0.3) [] """ frac = float(frac) out = Lgm_Vector(0, 0, 0) si = Lgm_SlerpInfo() Lgm_InitSlerp(pointer(self), pointer(other), pointer(si)) Lgm_Slerp(pointer(self), pointer(other), pointer(out), c_double(frac), pointer(si)) return out
def __eq__(self, other): """ if the components of a Vector are equal the vectors are equal """ if isinstance(other, Lgm_Vector): if other.x != self.x: return False elif other.y != self.y: return False elif other.z != self.z: return False else: return True elif isinstance(other, list): try: other = Lgm_Vector(other[0], other[1], other[2]) except: raise (TypeError('Bad type: %s in __eq__ comparison' % (type(other)))) else: return self == other else: raise (TypeError('Bad type: %s in __eq__ comparison' % (type(other))))
def __div__(self, other): """ divide a scalar into each component @param other: scalar to divide into vector @type other: (int, long, float) @return: new vector @rtype: Lgm_Vector @author: Brian Larsen @organization: LANL @contact: [email protected] @version: V1: 22-Dec-2010 (BAL) """ if isinstance(other, (int, float, long)): x = self.x / other y = self.y / other z = self.z / other return Lgm_Vector(x, y, z) else: raise (ArithmeticError("Cannot subtract %s from a Lgm_Vector" % (type(other))))
import Lgm_Vector Date = 20100203 UTC = 12.34567 MagInfo = Lgm_MagModelInfo.Lgm_MagModelInfo() Lgm_Set_Coord_Transforms(Date, UTC, MagInfo.c) #MagInfo.Bfield = cast((CFUNCTYPE(c_int))(Lgm_B_OP77), c_void_p) Lgm_Set_Lgm_B_OP77(pointer(MagInfo)) MagInfo.Kp = 5 MagInfo.VerbosityLevel = 5 u = Lgm_Vector.Lgm_Vector(1., 2., 3.) v = Lgm_Vector.Lgm_Vector(0., 0., 0.) u.x = -1.6 u.y = 0.0 u.z = 0.0 # Re u.x = -1.601 u.y = 0.0 u.z = 0.0 # Re u.x = -6.6 u.y = 0.0 u.z = 0.0 # Re Lgm_TraceToEarth(pointer(u), pointer(v), 120.0, -11.0, 1e-7, pointer(MagInfo)) print(u, 'maps to ', v) #