def arc_between_two_points(coordinate_system, point1, point2, radius=1, right=True): global_point1 = coordinate_system.to_parent(point1) global_point2 = coordinate_system.to_parent(point2) direction = point2 - point1 distance = np.sqrt(np.dot(direction, direction)) arc_coordinate_system = Cartesian(basis=np.copy(coordinate_system.basis), origin=np.copy(global_point1), name='Arc coordinate_system') r_theta_phi = transforms.cartesian_to_spherical(direction) arc_coordinate_system.rotate_axis_angle([0, 0, 1], r_theta_phi[2]) arc_coordinate_system.rotate_axis_angle([0, 1, 0], r_theta_phi[1] + np.pi/2) x_offset = -distance / 2 y_offset = np.sqrt(radius**2 - x_offset**2) if right: y_offset *= -1 arc_coordinate_system.origin = arc_coordinate_system.to_parent([x_offset, y_offset, 0]) local_point1 = arc_coordinate_system.to_local(global_point1) local_point2 = arc_coordinate_system.to_local(global_point2) start = transforms.cartesian_to_spherical(local_point1)[2] stop = transforms.cartesian_to_spherical(local_point2)[2] if not right: start = 2 * np.pi - start stop = 2 * np.pi - stop path = Arc(coordinate_system=arc_coordinate_system, a=radius, b=radius, start=start, stop=stop, right=right) return path
def test_to_parent_to_local(self): origin = (np.random.random(3) - 0.5) * 100 other_coordinate_system = Cartesian(origin=origin) axis = (np.random.random(3) - 0.5) * 100 angle = (np.random.random() - 0.5) * 100 other_coordinate_system.rotate_axis_angle(axis, angle) point_global = (np.random.random(3) - 0.5) * 100 point_local = other_coordinate_system.to_local(point_global) point_global2 = other_coordinate_system.to_parent(point_local) np.testing.assert_allclose(point_global2, point_global, atol=np.finfo(float).eps) point_local = (np.random.random(3) - 0.5) * 100 point_global = other_coordinate_system.to_parent(point_local) point_local_2 = other_coordinate_system.to_local(point_global) np.testing.assert_allclose(point_local_2, point_local, atol=np.finfo(float).eps)
def helix_between_two_points(coordinate_system, point1, point2, radius=1, loops=1, right=True): direction = point2 - point1 distance = np.sqrt(np.dot(direction, direction)) origin = coordinate_system.to_parent(point1) helix_coordinate_system = Cartesian(basis=np.copy(coordinate_system.basis), origin=np.copy(origin), name='Helix coordinate system') r_theta_phi = transforms.cartesian_to_spherical(direction) helix_coordinate_system.rotate_axis_angle([0, 0, 1], r_theta_phi[2]) helix_coordinate_system.rotate_axis_angle([0, 1, 0], r_theta_phi[1]) pitch = distance / int(loops) name = 'Right Helix' if right else 'Left Helix' path = Helix(name=name, coordinate_system=helix_coordinate_system, radius=radius, pitch=pitch, start=0, stop=np.pi * 2 * int(loops), right=right) return path
def test_rotation_axis_angle(self): other_coordinate_system = Cartesian() order = 3 axis = [1, 1, 2] steps = 10**order # per turn step = 2 * np.pi / steps for k in range(steps): other_coordinate_system.rotate_axis_angle(axis, step) self.assertEqual(self.coordinate_system, other_coordinate_system) np.testing.assert_allclose(self.coordinate_system.basis, other_coordinate_system.basis, atol=np.finfo(float).eps*steps) axis = [1, 0, 0] self.coordinate_system.rotate_axis_angle(axis, np.pi) np.testing.assert_allclose(self.coordinate_system.basis, np.array([[1, 0, 0], [0, -1, 0], [0, 0, -1]]), atol=np.finfo(float).eps) self.coordinate_system.rotate_axis_angle(axis, np.pi) np.testing.assert_allclose(self.coordinate_system.basis, np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]), atol=2*np.finfo(float).eps)
cs_2 = Cartesian(origin=np.array([0, 0, 0]), euler_angles_convention='Bunge') print(cs_1) turns = 10 axis = np.array([1, 1, 1]) steps = 1 # per turn max_steps_order = 4 errors = [] for order in range(max_steps_order + 1): steps = 10**order # per turn print("Processing %g steps per turn" % steps) step = 2 * np.pi / steps print(" Angle increment is %g rad (%g deg)" % (step, np.rad2deg(step))) laps_errors = [] print(" Lap:",) for i in range(turns): print(i+1,) for k in range(steps): cs_2.rotate_axis_angle(axis, step) laps_errors.append(error(cs_1, cs_2)) print("done.\n") errors.append(np.mean(laps_errors)) print(errors) if use_mpl: plt.semilogy(np.arange(max_steps_order + 1), errors, 'r-o') plt.show()
from __future__ import division, print_function import numpy as np from mayavi import mlab from Space.Coordinates import Cartesian from Space.Curve.Parametric import Helix from Space.Pathfinder import line_between_two_points, helix_between_two_points, arc_between_two_points import Space_visualization as Visual coordinate_system = Cartesian() coordinate_system.rotate_axis_angle(np.ones(3), np.deg2rad(45)) fig = mlab.figure('CS demo', bgcolor=(0, 0, 0)) Visual.draw_coordinate_system_axes(fig, coordinate_system) right_helix = Helix(name='Right Helix', coordinate_system=coordinate_system, radius=2, pitch=0.5, start=0, stop=np.pi * 4, right=True) left_helix = Helix(name='Left Helix', coordinate_system=coordinate_system, radius=2, pitch=0.5, start=0, stop=np.pi * 2, right=False) print('Helix length:', left_helix.length()) right_helix_view = Visual.CurveView(fig=fig, curve=right_helix) right_helix_view.draw() left_helix_view = Visual.CurveView(fig=fig, curve=left_helix) left_helix_view.draw() point1 = np.array([1, 1, 0]) point2 = np.array([2, 2, 0]) points = np.vstack((coordinate_system.to_parent(point1), coordinate_system.to_parent(point2)))
class TestCoordinates(unittest.TestCase): def setUp(self): self.coordinate_system = Cartesian() def test_equality(self): other_coordinate_system = Cartesian() self.assertEqual(self.coordinate_system, other_coordinate_system) other_coordinate_system.origin = [1, 0, 0] self.assertNotEqual(self.coordinate_system, other_coordinate_system) def test_rotation_axis_angle(self): other_coordinate_system = Cartesian() order = 3 axis = [1, 1, 2] steps = 10**order # per turn step = 2 * np.pi / steps for k in range(steps): other_coordinate_system.rotate_axis_angle(axis, step) self.assertEqual(self.coordinate_system, other_coordinate_system) np.testing.assert_allclose(self.coordinate_system.basis, other_coordinate_system.basis, atol=np.finfo(float).eps*steps) axis = [1, 0, 0] self.coordinate_system.rotate_axis_angle(axis, np.pi) np.testing.assert_allclose(self.coordinate_system.basis, np.array([[1, 0, 0], [0, -1, 0], [0, 0, -1]]), atol=np.finfo(float).eps) self.coordinate_system.rotate_axis_angle(axis, np.pi) np.testing.assert_allclose(self.coordinate_system.basis, np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]), atol=2*np.finfo(float).eps) def test_euler_angles(self): self.coordinate_system.euler_angles_convention = 'Bunge' axis = [1, 0, 0] self.coordinate_system.rotate_axis_angle(axis, np.pi) np.testing.assert_allclose(self.coordinate_system.euler_angles, np.array([0, np.pi, 0]), atol=np.finfo(float).eps) self.coordinate_system.rotate_axis_angle(axis, np.pi) np.testing.assert_allclose(self.coordinate_system.euler_angles, np.array([0, 0, 0]), atol=np.finfo(float).eps * 2) self.coordinate_system.rotate_axis_angle(axis, np.pi / 2) #np.testing.assert_allclose(self.coordinate_system.euler_angles, # np.array([0, np.pi / 2, 0]), atol=np.finfo(float).eps * 2) self.coordinate_system.rotate_axis_angle(axis, np.pi / 2) np.testing.assert_allclose(self.coordinate_system.euler_angles, np.array([0, np.pi, 0]), atol=np.finfo(float).eps) self.coordinate_system.rotate_axis_angle(axis, np.pi / 2) #np.testing.assert_allclose(self.coordinate_system.euler_angles, # np.array([np.pi, np.pi / 2, np.pi]), atol=np.finfo(float).eps) self.coordinate_system.rotate_axis_angle(axis, np.pi / 2) np.testing.assert_allclose(self.coordinate_system.euler_angles, np.array([0, 0, 0]), atol=np.finfo(float).eps * 4) def test_to_parent_to_local(self): origin = (np.random.random(3) - 0.5) * 100 other_coordinate_system = Cartesian(origin=origin) axis = (np.random.random(3) - 0.5) * 100 angle = (np.random.random() - 0.5) * 100 other_coordinate_system.rotate_axis_angle(axis, angle) point_global = (np.random.random(3) - 0.5) * 100 point_local = other_coordinate_system.to_local(point_global) point_global2 = other_coordinate_system.to_parent(point_local) np.testing.assert_allclose(point_global2, point_global, atol=np.finfo(float).eps) point_local = (np.random.random(3) - 0.5) * 100 point_global = other_coordinate_system.to_parent(point_local) point_local_2 = other_coordinate_system.to_local(point_global) np.testing.assert_allclose(point_local_2, point_local, atol=np.finfo(float).eps)