def random_point_pair(): """ Creates a random point pair bivector object """ mv_a = random_euc_mv() mv_b = random_euc_mv() pp = (up(mv_a) ^ up(mv_b)).normal() return pp
def random_line(): """ Creates a random line """ mv_a = random_euc_mv() mv_b = random_euc_mv() line_a = ((up(mv_a) ^ up(mv_b) ^ ninf)).normal() return line_a
def random_line(): """ Creates a random line """ mv_a = random_euc_mv() mv_b = random_euc_mv() line_a = ((up(mv_a) ^ up(mv_b) ^ ninf)).normal() return line_a
def random_point_pair(): """ Creates a random point pair bivector object """ mv_a = random_euc_mv() mv_b = random_euc_mv() pp = (up(mv_a) ^ up(mv_b)).normal() return pp
def random_circle(): """ Creates a random circle """ mv_a = random_euc_mv() mv_b = random_euc_mv() mv_c = random_euc_mv() line_a = ((up(mv_a) ^ up(mv_b) ^ up(mv_c))).normal() return line_a
def random_bivector(): """ Creates a random bivector on the form described by R. Wareham in Mesh Vertex Pose and Position Interpolation using Geometric Algebra. $$ B = ab + c*n_{\inf}$$ where $a, b, c \in \mathcal(R)^3$ """ a = random_euc_mv() c = random_euc_mv() return a * I3 + c * ninf
def random_bivector(): """ Creates a random bivector on the form described by R. Wareham in Mesh Vertex Pose and Position Interpolation using Geometric Algebra. $$ B = ab + c*n_{\inf}$$ where $a, b, c \in \mathcal(R)^3$ """ a = random_euc_mv() c = random_euc_mv() return a * I3 + c * ninf
def random_bivector(): """ Creates a random bivector on the form described by R. Wareham in TODO: Dig out the name of the interpolation paper $$ B = ab + c*n_{\inf}$$ where $a, b, c \in \mathcal(R)^3$ """ a = random_euc_mv() c = random_euc_mv() return a * I3 + c * ninf
def random_sphere(): """ Creates a random sphere """ mv_a = random_euc_mv() mv_b = random_euc_mv() mv_c = random_euc_mv() mv_d = random_euc_mv() sphere = ((up(mv_a) ^ up(mv_b) ^ up(mv_c) ^ up(mv_d))).normal() return sphere
def test_generate_translation_rotor(self): """ Tests translation rotor generation """ for i in range(100): rand = random_euc_mv() starting_point = up(random_euc_mv()) r_trans = generate_translation_rotor(rand) end_point = r_trans * starting_point * ~r_trans translation_vec = down(end_point) - down(starting_point) npt.assert_almost_equal(translation_vec.value, rand.value)
def random_sphere(): """ Creates a random sphere """ mv_a = random_euc_mv() mv_b = random_euc_mv() mv_c = random_euc_mv() mv_d = random_euc_mv() sphere = ((up(mv_a) ^ up(mv_b) ^ up(mv_c) ^ up(mv_d))).normal() return sphere
def test_generate_translation_rotor(self): """ Tests translation rotor generation """ for i in range(100): rand = random_euc_mv() starting_point = up(random_euc_mv()) r_trans = generate_translation_rotor(rand) end_point = r_trans * starting_point * ~r_trans translation_vec = down(end_point) - down(starting_point) testing.assert_almost_equal(translation_vec.value, rand.value)
def random_circle_at_origin(): """ Creates a random circle at the origin """ mv_a = random_euc_mv() mv_r = random_euc_mv() r = generate_rotation_rotor(np.pi / 2, mv_a, mv_r) mv_b = r * mv_a * ~r mv_c = r * mv_b * ~r pp = (up(mv_a) ^ up(mv_b) ^ up(mv_c)).normal() return pp
def random_circle_at_origin(): """ Creates a random circle at the origin """ mv_a = random_euc_mv() mv_r = random_euc_mv() r = generate_rotation_rotor(np.pi/2, mv_a, mv_r) mv_b = r*mv_a*~r mv_c = r * mv_b * ~r pp = (up(mv_a) ^ up(mv_b) ^ up(mv_c) ).normal() return pp
def run_rotor_estimation(self, object_generator, estimation_function, n_runs=5, n_objects_per_run=10): error_count = 0 for i in range(n_runs): query_model = [object_generator().normal() for i in range(n_objects_per_run)] r = (generate_translation_rotor(random_euc_mv(l_max=0.01)) * generate_rotation_rotor(np.random.randn() / 10, random_euc_mv().normal(), random_euc_mv().normal())).normal() reference_model = [(r * l * ~r).normal() for l in query_model] r_est = estimation_function(reference_model, query_model) error_flag = False for a, b in zip([(r_est * l * ~r_est).normal() for l in query_model], reference_model): if abs(a + b) < 0.0001: c = -b print('SIGN FLIP') else: c = b if np.any(np.abs(a.value - c.value) > 0.01): error_flag = True if error_flag: error_count += 1 print(i, error_count) print('\n\nESTIMATION SUMMARY') print('OBJECTS ', n_objects_per_run) print('RUNS ', n_runs) print('ERRORS ', error_count) print('ERROR percentage ', 100 * error_count / float(n_runs), '%')
def run_rotor_estimation(self, object_generator, estimation_function, n_runs=5, n_objects_per_run=10): error_count = 0 for i in range(n_runs): query_model = [object_generator().normal() for i in range(n_objects_per_run)] r = (generate_translation_rotor(random_euc_mv(l_max=0.01)) * generate_rotation_rotor(np.random.randn() / 10, random_euc_mv().normal(), random_euc_mv().normal())).normal() reference_model = [(r * l * ~r).normal() for l in query_model] r_est = estimation_function(reference_model, query_model) error_flag = False for a, b in zip([(r_est * l * ~r_est).normal() for l in query_model], reference_model): if abs(a + b) < 0.0001: c = -b print('SIGN FLIP') else: c = b if np.any(np.abs(a.value - c.value) > 0.01): error_flag = True if error_flag: error_count += 1 print(i, error_count) print('\n\nESTIMATION SUMMARY') print('OBJECTS ', n_objects_per_run) print('RUNS ', n_runs) print('ERRORS ', error_count) print('ERROR percentage ', 100 * error_count / float(n_runs), '%')
def test_draw_points(self): from clifford.tools.g3 import random_euc_mv from clifford.g3 import layout gs = GanjaScene() gs.add_objects([random_euc_mv().value[0:8] for i in range(10)], static=False) with open('test_file.html', 'w') as test_file: print(generate_full_html(str(gs), sig=layout.sig, grid=True, scale=1.0, gl=False), file=test_file) render_cef_script(str(gs), sig=layout.sig, grid=True, scale=1.0, gl=False)
def test_general_logarithm_translation(self): # Check we can reverse translation for i in range(50): t = random_euc_mv() biv = ninf * t / 2 R = general_exp(biv).normal() biv_2 = general_logarithm(R) npt.assert_almost_equal(biv.value, biv_2.value)
def test_general_logarithm_translation(self): # Check we can reverse translation for i in range(50): t = random_euc_mv() biv = ninf * t /2 R = general_exp(biv).normal() biv_2 = general_logarithm(R) np.testing.assert_almost_equal(biv.value, biv_2.value)
def test_from_value_array(self): mv = [] for i in range(100): p = random_euc_mv() mv.append(p) test_array = ConformalMVArray(mv) up_array = test_array.up() new_mv_array = ConformalMVArray.from_value_array(up_array.value) npt.assert_almost_equal(new_mv_array.value, up_array.value)
def test_from_value_array(self): mv = [] for i in range(100): p = random_euc_mv() mv.append(p) test_array = ConformalMVArray(mv) up_array = test_array.up() new_mv_array = ConformalMVArray.from_value_array(up_array.value) np.testing.assert_almost_equal(new_mv_array.value, up_array.value)
def random_point_pair_at_origin(): """ Creates a random point pair bivector object at the origin """ mv_a = random_euc_mv() plane_a = (mv_a*I3).normal() mv_b = plane_a*mv_a*plane_a pp = (up(mv_a) ^ up(mv_b)).normal() return pp
def random_point_pair_at_origin(): """ Creates a random point pair bivector object at the origin """ mv_a = random_euc_mv() plane_a = (mv_a * I3).normal() mv_b = plane_a * mv_a * plane_a pp = (up(mv_a) ^ up(mv_b)).normal() return pp
def test_dual(self): mv = [] for i in range(100): p = random_euc_mv() mv.append(p) test_array = ConformalMVArray(mv) up_array = test_array.up() I5 = self.layout.blades['e12345'] np.testing.assert_almost_equal((up_array * ConformalMVArray([I5])).value, ConformalMVArray([i * I5 for i in up_array]).value)
def test_dual(self): mv = [] for i in range(100): p = random_euc_mv() mv.append(p) test_array = ConformalMVArray(mv) up_array = test_array.up() I5 = self.layout.blades['e12345'] np.testing.assert_almost_equal((up_array * ConformalMVArray([I5])).value, ConformalMVArray([i * I5 for i in up_array]).value)
def test_fit_plane(self): noise = 0.1 trueP = random_plane() point_list = project_points_to_plane( [random_conformal_point() for i in range(100)], trueP) point_list = [ up(down(P) + noise * random_euc_mv()) for P in point_list ] print(trueP) plane = fit_plane(point_list) print(plane)
def test_fit_sphere(self): try: noise = 0.1 trueP = random_sphere() point_list = project_points_to_sphere([random_conformal_point() for i in range(100)], trueP) point_list = [up(down(P) + noise * random_euc_mv()) for P in point_list] print(trueP) sphere = fit_sphere(point_list) print(sphere) #draw([sphere] + point_list, static=False, scale=0.1) except: print('FAILED TO FIND SPHERE')
def test_fit_plane(self): try: noise = 0.1 trueP = random_plane() point_list = project_points_to_plane([random_conformal_point() for i in range(100)], trueP) point_list = [up(down(P) + noise * random_euc_mv()) for P in point_list] print(trueP) plane = fit_plane(point_list) print(plane) #draw(point_list + [plane], static=False, scale=0.1) except: print('FAILED TO FIND PLANE')
def test_fit_sphere(self): try: noise = 0.1 trueP = random_sphere() point_list = project_points_to_sphere([random_conformal_point() for i in range(100)], trueP) point_list = [up(down(P) + noise * random_euc_mv()) for P in point_list] print(trueP) sphere = fit_sphere(point_list) print(sphere) #draw([sphere] + point_list, static=False, scale=0.1) except: print('FAILED TO FIND SPHERE')
def test_fit_plane(self): try: noise = 0.1 trueP = random_plane() point_list = project_points_to_plane([random_conformal_point() for i in range(100)], trueP) point_list = [up(down(P) + noise * random_euc_mv()) for P in point_list] print(trueP) plane = fit_plane(point_list) print(plane) #draw(point_list + [plane], static=False, scale=0.1) except: print('FAILED TO FIND PLANE')
def test_general_logarithm_TS(self): for i in range(5): scale = 0.5 +np.random.rand() t = random_euc_mv() S = generate_dilation_rotor(scale) T = generate_translation_rotor(t) V = ( T *S).normal() biv = general_logarithm(V) V_rebuilt = (general_exp(biv)).normal() C1 = random_point_pair() C2 = ( V *C1 *~V).normal() C3 = (V_rebuilt *C1 *~V_rebuilt).normal() np.testing.assert_almost_equal(C2.value, C3.value, 5)
def test_apply_rotor(self): mv = [] for i in range(100): p = random_euc_mv() mv.append(p) test_array = ConformalMVArray(mv) up_array = test_array.up() # Test apply rotor for i in range(100): R = ConformalMVArray([self.layout.randomRotor()]) rotated_array = up_array.apply_rotor(R) for i, v in enumerate(rotated_array): np.testing.assert_almost_equal(v.value, apply_rotor(up_array[i], R[0]).value)
def test_general_logarithm_TS(self): for i in range(5): scale = 0.5 + np.random.rand() t = random_euc_mv() S = generate_dilation_rotor(scale) T = generate_translation_rotor(t) V = (T * S).normal() biv = general_logarithm(V) V_rebuilt = (general_exp(biv)).normal() C1 = random_point_pair() C2 = (V * C1 * ~V).normal() C3 = (V_rebuilt * C1 * ~V_rebuilt).normal() npt.assert_almost_equal(C2.value, C3.value, 5)
def test_apply_rotor(self): mv = [] for i in range(100): p = random_euc_mv() mv.append(p) test_array = ConformalMVArray(mv) up_array = test_array.up() # Test apply rotor for i in range(100): R = ConformalMVArray([self.layout.randomRotor()]) rotated_array = up_array.apply_rotor(R) for i, v in enumerate(rotated_array): np.testing.assert_almost_equal(v.value, apply_rotor(up_array[i], R[0]).value)
def test_up_down(self): mv = [] up_mv = [] for i in range(100): p = random_euc_mv() mv.append(p) up_mv.append(up(p)) test_array = ConformalMVArray(mv) up_array = test_array.up() down_array = up_array.down() for a, b in zip(up_array, up_mv): np.testing.assert_almost_equal(a.value, b.value) np.testing.assert_almost_equal(a.value, b.value) for a, b in zip(down_array, mv): np.testing.assert_almost_equal(a.value, b.value)
def test_up_down(self): mv = [] up_mv = [] for i in range(100): p = random_euc_mv() mv.append(p) up_mv.append(up(p)) test_array = ConformalMVArray(mv) up_array = test_array.up() down_array = up_array.down() for a, b in zip(up_array, up_mv): npt.assert_almost_equal(a.value, b.value) npt.assert_almost_equal(a.value, b.value) for a, b in zip(down_array, mv): npt.assert_almost_equal(a.value, b.value)
def test_find_rotor_aligning_vectors(self, rng): # noqa: F811 """ Currently fails, needs to be dug into """ from clifford.g3c import layout e1 = layout.blades['e1'] e2 = layout.blades['e2'] from clifford.tools.g3 import random_euc_mv, random_rotation_rotor, rotor_align_vecs u_list = [random_euc_mv(rng=rng) for i in range(50)] for i in range(100): r = random_rotation_rotor(rng=rng) v_list = [r * u * ~r for u in u_list] r_2 = rotor_align_vecs(u_list, v_list) print(r_2) print(r) testing.assert_almost_equal(r.value, r_2.value)
def test_find_rotor_aligning_vectors(self): """ Currently fails, needs to be dug into """ from clifford.g3c import layout e1 = layout.blades['e1'] e2 = layout.blades['e2'] from clifford.tools.g3 import random_euc_mv, random_rotation_rotor, rotor_align_vecs u_list = [random_euc_mv() for i in range(50)] for i in range(100): r = random_rotation_rotor() v_list = [r*u*~r for u in u_list] r_2 = rotor_align_vecs(u_list, v_list) print(r_2) print(r) testing.assert_almost_equal(r.value, r_2.value)
def test_get_properties_of_sphere(self): for i in range(100): # Make a sphere scale_factor = np.random.rand() sphere = (up(scale_factor * e1) ^ up(-scale_factor * e1) ^ up(scale_factor * e3) ^ up(scale_factor * e2)).normal() # Translate it rand_trans = random_euc_mv() trans_rot = generate_translation_rotor(rand_trans) sphere = (trans_rot * sphere * ~trans_rot).normal() center = get_center_from_sphere(sphere) radius = get_radius_from_sphere(sphere) npt.assert_almost_equal(down(center).value, rand_trans.value) npt.assert_almost_equal(radius, scale_factor)
def test_get_properties_of_sphere(self): for i in range(100): # Make a sphere scale_factor = np.random.rand() sphere = (up(scale_factor * e1) ^ up(-scale_factor * e1) ^ up(scale_factor * e3) ^ up( scale_factor * e2)).normal() # Translate it rand_trans = random_euc_mv() trans_rot = generate_translation_rotor(rand_trans) sphere = (trans_rot * sphere * ~trans_rot).normal() center = get_center_from_sphere(sphere) radius = get_radius_from_sphere(sphere) testing.assert_almost_equal(down(center).value, rand_trans.value) testing.assert_almost_equal(radius, scale_factor)
def random_translation_rotor(maximum_translation=10.0): """ generate a random translation rotor """ return generate_translation_rotor(random_euc_mv(maximum_translation))
def test_generate_translation_rotor(self): for i in range(10000): euc_vector_a = random_euc_mv() res = generate_translation_rotor(euc_vector_a) res2 = (1 + ninf * euc_vector_a / 2) npt.assert_almost_equal(res.value, res2.value)
def random_conformal_point(l_max=10): """ Creates a random conformal point """ return up(random_euc_mv(l_max=l_max))
def random_translation_rotor(maximum_translation=10.0): """ generate a random translation rotor """ return generate_translation_rotor(random_euc_mv(maximum_translation))
def random_conformal_point(l_max=10): """ Creates a random conformal point """ return up(random_euc_mv(l_max=l_max))