def test_extend_to_y(self): """ extend a line given by two points to desired y-coordinate """ p1 = f.vector(0, 0, 0) p2 = f.vector(1, 1, 0) y = 5 self.assert_np_equal(f.extend_to_y(p1, p2, y), f.vector(y, y, 0))
def test_arbitrary_rotation_axis(self): """ rotation of a point around arbitrary axis """ point = f.vector(1, 0, 0) origin = f.vector(0, 0, 0) axis = f.vector(1, 1, 0) self.assert_np_almost_equal( f.arbitrary_rotation(point, axis, np.pi, origin), f.vector(0, 1, 0))
def test_arbitrary_rotation_point(self): """ rotation of a point from another origin """ point = f.vector(0, 2, 0) origin = f.vector(0, 1, 0) axis = f.vector(0, 0, 1) self.assert_np_equal( f.arbitrary_rotation(point, axis, -np.pi / 2, origin), f.vector(1, 1, 0))
def test_angle_between(self): """ angle between two vectors """ v1 = f.vector(0, 0, 1) v2 = f.vector(0, 2, 0) self.assertEqual(f.angle_between(v1, v2), np.pi / 2) self.assertEqual(f.angle_between(v1, v1), 0) v1 = f.vector(1, 0, 0) v2 = f.vector(1, 1, 0) self.assertAlmostEqual(f.angle_between(v1, v2), np.pi / 4)
def test_curve_length(self): """ length of a curve given by a list of points """ curve = np.array([ f.vector(0, 0, 0), f.vector(1, 0, 0), f.vector(1, 1, 0), f.vector(1, 2, 0), f.vector(0, 2, 0), ]) self.assertAlmostEqual(c.curve_length(curve), 4)
def test_xy_line_intersection(self): """ intersection of two lines in xy-plane """ # line 1 p1 = f.vector(0, 0, 0) p2 = f.vector(1, 1, 0) # line 2 p3 = f.vector(1, 0, 0) p4 = f.vector(0, 1, 0) self.assert_np_equal(f.xy_line_intersection(p1, p2, p3, p4), f.vector(0.5, 0.5, 0))
def test_to_cartesian_point(self): """ polar point to xyz """ # polar point p = np.array([1, np.pi / 2, 2]) # cartesian versions # axis: z self.assert_np_almost_equal(f.to_cartesian(p, axis='z'), f.vector(0, 1, 2)) self.assert_np_almost_equal(f.to_cartesian(p, axis='x'), f.vector(2, 0, 1)) self.assert_np_almost_equal(f.to_cartesian(p, axis='x', direction=-1), f.vector(2, 0, -1))
def test_rotate(self): """ simple rotation around a coordinate system axis """ v = f.vector(1, 0, 0) r = f.rotate(v, np.pi / 2, axis='x') self.assertAlmostEqual(f.angle_between(r, v), 0) r = f.rotate(v, np.pi / 4, axis='y') self.assertAlmostEqual(f.angle_between(r, v), np.pi / 4)
def test_to_cartesian_curve(self): """ f.to_cartesian() for a list of points """ # polar points points = [ f.vector(1, 0, 0), f.vector(1, np.pi / 2, 1), f.vector(2, 0, 1) ] self.assert_np_almost_equal( c.to_cartesian(points), np.array([f.vector(1, 0, 0), f.vector(0, 1, 1), f.vector(2, 0, 1)]))
def test_to_polar_x_axis(self): """ cartesian coordinate system to polar c.s., rotation around x-axis """ cartesian = f.vector(5, 2, 2) polar = f.vector(8**0.5, np.pi / 4, 5) self.assert_np_almost_equal(polar, f.to_polar(cartesian, axis='x'))
def test_rotate_exception(self): """ an exception must be raised if axis is anything but x, y, or z """ with self.assertRaises(ValueError): f.rotate(f.vector(0, 0, 1), np.pi / 2, 'a')
def test_unit_vector(self): """ scale vector to magnitude 1 """ vector = f.vector(3, 5, 7) unit_vector = vector / np.linalg.norm(vector) self.assert_np_equal(unit_vector, f.unit_vector(vector))