def testFieldAngleToVector(self): sp00 = SpherePoint(0, 0, degrees) degList = (-90, -89.9, -20, 0, 10, 89.9, 90) for xdeg, ydeg, flipX in itertools.product(degList, degList, (False, True)): with self.subTest(xdeg=xdeg, ydeg=ydeg, flipX=flipX): xrad = xdeg * RAD_PER_DEG signx = -1 if flipX else 1 testOrientation = xdeg != 0 or ydeg != 0 yrad = ydeg * RAD_PER_DEG fieldAngle = (xrad, yrad) vector = coordUtils.fieldAngleToVector(fieldAngle, flipX) self.assertAlmostEqual(np.linalg.norm(vector), 1) if testOrientation: # Orientation should match. orientationFromFieldAngle = math.atan2(yrad, signx*xrad)*radians # Field angle x = vector y, field angle y = vector z. orientationFromVector = math.atan2(vector[2], vector[1])*radians self.assertAnglesAlmostEqual(orientationFromVector, orientationFromFieldAngle) # Now test as spherical geometry. sp = SpherePoint(Vector3d(*vector)) separation = sp00.separation(sp) predictedSeparation = math.hypot(xrad, yrad)*radians self.assertAnglesAlmostEqual(predictedSeparation, separation) if testOrientation: bearing = sp00.bearingTo(sp) self.assertAnglesAlmostEqual(orientationFromFieldAngle, bearing) # Test round trip through vectorToFieldAngle. fieldAngleFromVector = coordUtils.vectorToFieldAngle(vector, flipX) np.testing.assert_allclose(fieldAngleFromVector, fieldAngle, atol=1e-15)
def testVectorToFieldAngle(self): # Note: more sophisticated cases are tested by testFieldAngleToVector. for flipX in (False, True): signx = -1 if flipX else 1 for magMultiplier in (0.001, 1, 1000): for vector, predictedFieldAngleDeg in ( ((1, 0, 0), (0, 0)), ((0, 1, 0), (signx*90, 0)), ((0, 0, 1), (0, 90)), ): predictedFieldAngle = [val*RAD_PER_DEG for val in predictedFieldAngleDeg] scaledVector = np.array(vector) * magMultiplier fieldAngle = coordUtils.vectorToFieldAngle(scaledVector, flipX) np.testing.assert_allclose(predictedFieldAngle, fieldAngle, atol=1e-15)
def testComputeShiftedPlanePos(self): """Test computeShiftedPlanePos for the general case """ # In the general case the increase in x and y equals the shift # times y/x, z/x of a vector equivalent to the field angle. for ratios in ( (0.0, 0.0), (0.5, 0.5), (-0.23, 0.75), (0.3, 0.1), ): vector = (1, ratios[0], ratios[1]) fieldAngle = coordUtils.vectorToFieldAngle(vector, False) for planePos in ( (-1000, -2000), (0, 0), (5000, 4000), ): for shift3 in (-550, 0, 375): predictedShiftedPlanePos = [planePos[i] + shift3*ratios[i] for i in range(2)] shiftedPlanePos = coordUtils.computeShiftedPlanePos(planePos, fieldAngle, shift3) self.assertPairsAlmostEqual(shiftedPlanePos, predictedShiftedPlanePos)