/
test_g3c_tools.py
1381 lines (1089 loc) · 52 KB
/
test_g3c_tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from clifford import Cl, grades_present
import unittest
from clifford.g3c import *
from clifford import general_exp
from clifford.tools.g3c import *
from clifford.tools.g3c.rotor_parameterisation import ga_log, ga_exp, general_logarithm,\
interpolate_rotors
from clifford.tools.g3c.rotor_estimation import *
from clifford.tools.g3c.object_clustering import *
from clifford.tools.g3c.scene_simplification import *
from clifford.tools.g3c.object_fitting import *
from clifford.tools.g3c.model_matching import *
from clifford.tools.g3 import random_euc_mv
from clifford.tools.g3c.GAOnline import draw_objects, GAScene, GanjaScene
import time
import numpy as np
import numpy.testing as npt
from functools import reduce
from numpy import exp, float64, testing
from nose.plugins.skip import SkipTest
import random
class RotorGenerationTests(unittest.TestCase):
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)
np.testing.assert_almost_equal(res.value, res2.value)
class TestFitObjects(unittest.TestCase):
def test_fit_circle(self):
try:
noise = 0.1
trueP = random_circle()
point_list = project_points_to_circle([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)
circle = fit_circle(point_list)
print(circle)
#draw(point_list + [circle], static=False, scale=0.1)
except:
print('FAILED TO FIND CIRCLE')
def test_fit_line(self):
try:
noise = 0.1
trueP = random_line()
point_list = project_points_to_line([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)
line = fit_line(point_list)
print(line)
#draw(point_list + [line], static=False, scale=0.1)
except:
print('FAILED TO FIND LINE')
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')
class TestGeneralLogarithm(unittest.TestCase):
def test_general_logarithm_rotation(self):
# Check we can reverse rotations
for i in range(50):
R = random_rotation_rotor()
biv_2 = general_logarithm(R)
biv_3 = ga_log(R)
np.testing.assert_almost_equal(biv_2.value, biv_3.value, 3)
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_general_logarithm_scaling(self):
# Check we can reverse scaling
for i in range(50):
scale = 0.5 + np.random.rand()
biv = -np.log(scale ) *e45 /2
R = general_exp(biv).normal()
biv_2 = general_logarithm(R)
np.testing.assert_almost_equal(biv.value, biv_2.value)
def test_general_logarithm_RS(self):
for i in range(5):
scale = 0.5 + np.random.rand()
S = generate_dilation_rotor(scale).normal()
R = generate_rotation_rotor(0.5, e1, e2).normal()
V = ( R *S).normal()
biv_test = general_logarithm(R) + general_logarithm(S)
biv = general_logarithm(V)
biv_alt = ga_log(R) + general_logarithm(S)
np.testing.assert_almost_equal(biv.value, biv_test.value, 5)
np.testing.assert_almost_equal(biv.value, biv_alt.value, 5)
def test_general_logarithm_TR(self):
for i in range(5):
# R = generate_rotation_rotor(0.5, e1, e2).normal()
# T = generate_translation_rotor(e3 + 7 * e2 - e1).normal()
# V = (T*R).normal()
biv_true = random_bivector()
V = general_exp(biv_true).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, 2)
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_general_logarithm_TRS(self):
for i in range(5):
scale = 0.5 + np.random.rand()
S = generate_dilation_rotor(scale)
R = generate_rotation_rotor(0.5, e1, e2)
T = generate_translation_rotor(e3 + 7* e2 - e1)
V = (T * R * S).normal()
biv = general_logarithm(V)
V_rebuilt = general_exp(biv).normal()
biv2 = general_logarithm(V)
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)
def test_general_logarithm_conformal(self):
object_generators = [random_point_pair, random_line, random_circle, random_plane]
# object_generators = [random_sphere]
for obj_gen in object_generators:
print(obj_gen.__name__)
for i in range(10000):
X = obj_gen()
Y = obj_gen()
R = rotor_between_objects(X, Y)
biv = general_logarithm(R)
R_recon = general_exp(biv).normal()
np.testing.assert_almost_equal(R.value, R_recon.value, 4)
class VisualisationTests(unittest.TestCase):
def test_draw_objects(self):
scene = ConformalMVArray([random_line() for i in range(100)])
sc_a = str(draw_objects(scene))
scene.save('test.ga')
sc_b = str(draw_objects('test.ga'))
assert sc_a == sc_b
def test_ganja_scene(self):
scene = ConformalMVArray([up(0)^up(e1)^einf,up(0)^up(e2)^einf,up(0)^up(e3)^einf]
+ [random_line() for i in range(2)])
sc = GanjaScene()
sc.add_objects(scene)
sc.save_to_file('test.json')
class ConformalArrayTests(unittest.TestCase):
@classmethod
def setUpClass(self):
self.layout = layout
self.object_generators = [
random_point_pair,
random_line,
random_circle,
random_plane,
random_sphere]
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_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_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_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)
class G3CToolsTests(unittest.TestCase):
@classmethod
def setUpClass(self):
self.object_generators = [
random_point_pair,
random_line,
random_circle,
random_plane,
random_sphere]
def test_factorise(self):
n_repeats = 50
for obj_gen in self.object_generators:
print(obj_gen.__name__)
for i in range(n_repeats):
X1 = obj_gen()
basis, scale = X1.factorise()
for b in basis:
gpres = grades_present(b, 0.0001)
assert len(gpres) == 1
assert gpres[0] == 1
new_blade = (reduce(lambda a, b: a ^ b, basis) * scale)
try:
np.testing.assert_almost_equal(new_blade.value, X1.value, 3)
except:
print(X1)
print(new_blade)
np.testing.assert_almost_equal(new_blade.value, X1.value, 3)
def test_is_blade(self):
a = random_bivector() + random_circle()
assert not a.isBlade()
a = random_translation_rotor()
assert not a.isBlade()
n_repeats = 5
for obj_gen in self.object_generators:
for i in range(n_repeats):
a = obj_gen()
if not a.isBlade():
print(obj_gen.__name__)
raise ValueError('Object is not a blade')
def test_average_objects(self):
n_repeats = 1000
for obj_gen in self.object_generators:
for i in range(n_repeats):
X1 = obj_gen()
X2 = obj_gen()
obj_list = [X1, X2]
try:
average_objects(obj_list, weights=[0.5, 0.5])
except:
print(obj_gen.__name__)
average_objects(obj_list, weights=[0.5, 0.5])
def test_point_beyond_plane(self):
plane = I5 * ((e1 + e2 + e3).normal() + 2 * einf)
P = up((e1 + e2 + e3) * 3)
assert point_beyond_plane(P, plane)
P = up((e1 + e2 + e3) * 1)
assert not point_beyond_plane(P, plane)
def test_join_spheres(self):
for j in range(1000):
s1 = random_sphere()
s2 = random_sphere()
s3 = join_spheres(s1, s2)
assert sphere_in_sphere(s1, s3)
assert sphere_in_sphere(s2, s3)
def test_enclosing_spheres(self):
n_spheres = 10
for j in range(1000):
spheres = [random_sphere() for i in range(n_spheres)]
s4 = normalised(enclosing_sphere(spheres))
for s in spheres:
assert sphere_in_sphere(s, s4)
def test_closest_furthest_circle_points(self):
for _ in range(100):
C1 = random_circle()
C2 = random_circle()
pclose = closest_points_on_circles(C1, C2)
pfar = furthest_points_on_circles(C1, C2)
def test_general_object_interpolation(self):
R_r = generate_rotation_rotor(np.pi / 16, e2, e3) * generate_rotation_rotor(np.pi / 4, e1, e2)
R_d = generate_dilation_rotor(1.5)
R_t = generate_translation_rotor(e3)
R = (R_t * R_r * R_d).normal()
# C1 = (up(0+3*e1)^up(2*e1+3*e1)).normal()
C1 = (up(0 + 3 * e1) ^ up(2 * e1 + 3 * e1) ^ up(e1 + e3 + 3 * e1)).normal()
C2 = (R * C1 * ~R).normal()(3)
C3 = (R * C2 * ~R).normal()(3)
C4 = (R * C3 * ~R).normal()(3)
C5 = (R * C4 * ~R).normal()(3)
object_list = [C1, C2, C3, C4, C5]
object_alpha_array = np.array([0.0, 0.25, 0.5, 0.75, 1.0])
new_alpha_array = np.linspace(0.0, 1.0)
new_object_list = general_object_interpolation(object_alpha_array, object_list, new_alpha_array,
kind='quadratic')
new_object_list = [o(3) for o in new_object_list]
draw_objects(object_list, 'circle', color='rgb(255,0,0)')
draw_objects(new_object_list, 'circle', color='rgb(0,255,0)')
time.sleep(1)
def test_n_th_root(self):
for i in range(200):
a = random_point_pair()
b = random_point_pair()
R = rotor_between_objects(a, b)
for n in [1, 2, 4, 8, 16, 32]:
R_n = n_th_rotor_root(R, n)
np.testing.assert_almost_equal((R_n ** n).value, R.value)
def test_random_point_pair_at_origin(self):
pp_list = [random_point_pair_at_origin() for i in range(10)]
sc = GAScene()
for pp in pp_list:
sc.add_point_pair(pp)
print(sc)
def test_random_line_at_origin(self):
pp_list = [random_line_at_origin() for i in range(10)]
sc = GAScene()
for pp in pp_list:
sc.add_line(pp)
print(sc)
def test_random_circle_at_origin(self):
pp_list = [random_circle_at_origin() for i in range(10)]
sc = GAScene()
for pp in pp_list:
sc.add_circle(pp)
print(sc)
def test_random_sphere_at_origin(self):
pp_list = [random_sphere_at_origin() for i in range(10)]
sc = GAScene()
for pp in pp_list:
sc.add_sphere(pp)
print(sc)
def test_random_plane_at_origin(self):
pp_list = [random_plane_at_origin() for i in range(10)]
sc = GAScene()
for pp in pp_list:
sc.add_plane(pp)
print(sc)
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 test_intersect_line_and_plane_to_point(self):
""" Intersection of a line and a plane """
# First the case that they intersect
line = (up(2*e1) ^ up(2*e1 + e3) ^ ninf).normal()
plane = (up(e3) ^ up(e3 + e1) ^ up(e3 + e2) ^ ninf).normal()
point_result = intersect_line_and_plane_to_point(line, plane)
testing.assert_almost_equal(point_result.value, up(e3 + 2*e1).value)
# Next the case that the do not intersect
line = (up(0) ^ up(e1) ^ ninf).normal()
point_result = intersect_line_and_plane_to_point(line, plane)
assert point_result is None
for i in range(200):
line = random_line()
plane = random_plane()
point_result = intersect_line_and_plane_to_point(line, plane)
# draw_objects([line], mv_type='line')
# draw_objects([plane], mv_type='plane', color='rgb(0,255,0)')
# draw_objects([point_result], mv_type='euc_point', color='rgb(255,0,0)')
def test_normalise_n_minus_1(self):
for i in range(500):
mv = np.random.rand() * random_conformal_point()
mv_normed = normalise_n_minus_1(mv)
testing.assert_almost_equal((mv_normed | ninf)[0], -1.0)
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 test_point_pair_to_end_points(self):
for i in range(100):
point_a = random_conformal_point()
point_b = random_conformal_point()
pp = (point_a ^ point_b).normal()
p_a, p_b = point_pair_to_end_points(pp)
testing.assert_almost_equal(p_a.value, point_a.value)
testing.assert_almost_equal(p_b.value, point_b.value)
def test_euc_distance(self):
for i in range(100):
point_a = random_conformal_point()
point_b = random_conformal_point()
dist = euc_dist(point_a, point_b)
dist_alt = float(abs(down(point_a) - down(point_b)))
testing.assert_almost_equal(dist, dist_alt)
def test_dilation_rotor(self):
for i in range(100):
scale = 2 * np.random.rand()
r = generate_dilation_rotor(scale)
sphere = random_sphere()
radius = get_radius_from_sphere(sphere)
sphere2 = (r * sphere * ~r).normal()
radius2 = get_radius_from_sphere(sphere2)
testing.assert_almost_equal(scale, radius2 / radius)
def test_calculate_S_over_mu_general(self):
# The object generators
object_generators = [random_point_pair,
random_line,
random_circle,
random_plane,
random_sphere]
# Repeats for each fuzz test
n_repeats = 100
# Test the general case
for obj_gen in object_generators:
for i in range(n_repeats):
X1 = obj_gen()
X2 = obj_gen()
S = calculate_S_over_mu(X1, X2)
X3 = -S*(X1 + X2)
X4 = average_objects([X1,X2], [0.5,0.5]).normal()
if sum(np.abs((X3 + X4).value)) < 0.000001:
print(obj_gen.__name__, ' SIGN FLIP')
X4 = -X4
try:
np.testing.assert_almost_equal(X3.value, X4.value, 4)
except:
print(obj_gen.__name__)
print(X3)
print(X4)
X4 = average_objects([X1, X2], [0.5, 0.5]).normal()
np.testing.assert_almost_equal(X3.value, X4.value, 4)
def test_general_rotor_between_objects(self):
# The object generators
object_generators = [random_point_pair,
random_line,
random_circle,
random_plane,
random_sphere]
# Repeats for each fuzz test
n_repeats = 1000
# Test the general case
for obj_gen in object_generators:
print(obj_gen.__name__)
for i in range(n_repeats):
C1 = obj_gen()
C2 = obj_gen()
R = rotor_between_objects(C1, C2)
C3 = (R * C1 * ~R).normal()
if sum(np.abs((C2 + C3).value)) < 0.0001:
print('SIGN FLIP ', obj_gen.__name__)
C3 = -C3
try:
np.testing.assert_almost_equal(C2.value, C3.value, 3)
except:
print(R)
print(C2*C1 + C1*C2)
np.testing.assert_almost_equal(C2.value, C3.value, 3)
def test_motor_between_rounds(self):
# The object generators
object_generators = [random_point_pair, random_circle, random_sphere]
# Repeats for each fuzz test
n_repeats = 1000
# Test the general case
for obj_gen in object_generators:
print(obj_gen.__name__)
for i in range(n_repeats):
C1 = obj_gen()
Rt = random_rotation_translation_rotor()
C2 = (Rt*C1*~Rt).normal()
R = motor_between_rounds(C1, C2)
C3 = (R * C1 * ~R).normal()
if sum(np.abs((C2 + C3).value)) < 0.0001:
print('SIGN FLIP ', obj_gen.__name__)
C3 = -C3
try:
np.testing.assert_almost_equal(C2.value, C3.value, 3)
except:
print(C2.normal())
print(C3.normal())
np.testing.assert_almost_equal(C2.value, C3.value, 3)
#@SkipTest # Skip this because we know that it is a breaking case
def test_general_rotor_between_objects_specific_cases(self):
C1 = -(2.48651^e1234) - (2.48651^e1235) - (1.0^e1245) + (3e-05^e1345) - (0.0^e2345)
C2 = -(25.8135^e1234) - (25.8135^e1235) + (1.0^e1245) - (3e-05^e1345) - (0.0^e2345)
R = rotor_between_objects(C1, C2)
C3 = (R * C1 * ~R).normal()
if sum(np.abs((C2 + C3).value)) < 0.0001:
C3 = -C3
np.testing.assert_almost_equal(C2.value, C3.value, 3)
#@SkipTest # Skip this because we know that it is a breaking case
def test_rotor_between_non_overlapping_spheres(self):
C1 = random_sphere()
rad = get_radius_from_sphere(C1)
t_r = generate_translation_rotor(2.5*rad*e1)
C2 = (t_r * C1 * ~t_r)(4).normal()
rad2 = get_radius_from_sphere(C2)
R = rotor_between_objects(C1, C2)
C3 = (R * C1 * ~R).normal()
if sum(np.abs((C2 + C3).value)) < 0.0001:
print('SIGN FLIP ')
C3 = -C3
np.testing.assert_almost_equal(C2.value, C3.value, 5)
class RotorEstimationTests(unittest.TestCase):
def run_rotor_estimation(self, object_generator, estimation_function,
n_runs=20, 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_de_keninck_twist(self):
X = MVArray([random_conformal_point() for i in range(100)])
R = random_rotation_rotor()
noise_std = 0.0
Y = MVArray([normalise_n_minus_1(apply_rotor(x, random_translation_rotor(noise_std) * R)) for x in X])
res = de_keninck_twist(Y, X)
try:
np.testing.assert_almost_equal(R.value, res.value, 4)
except:
np.testing.assert_almost_equal(R.value, -res.value, 4)
def test_direct_TRS_extraction(self):
X = MVArray([random_conformal_point() for i in range(100)])
R = (random_rotation_translation_rotor(maximum_translation=100) * generate_dilation_rotor(
0.5 + 2 * np.random.rand())).normal()
noise_std = 0.0
Y = MVArray([normalise_n_minus_1(apply_rotor(x, random_translation_rotor(noise_std) * R)) for x in X])
res = direct_TRS_extraction(Y, X)
try:
np.testing.assert_almost_equal(R.value, res.value, 4)
except:
np.testing.assert_almost_equal(R.value, -res.value, 4)
def test_dorst_motor_points(self):
X = MVArray([random_conformal_point() for i in range(100)])
R = random_rotation_translation_rotor(maximum_translation=100)
noise_std = 0.0
Y = MVArray([normalise_n_minus_1(apply_rotor(x, random_translation_rotor(noise_std) * R)) for x in X])
res = dorst_motor_estimate(Y, X)
try:
np.testing.assert_almost_equal(R.value, res.value, 4)
except:
np.testing.assert_almost_equal(R.value, -res.value, 4)
def test_dorst_motor_estimate_lines(self):
self.run_rotor_estimation(random_line, dorst_motor_estimate)
def test_dorst_motor_estimate_circles(self):
self.run_rotor_estimation(random_circle, dorst_motor_estimate)
def test_dorst_motor_estimate_point_pairs(self):
self.run_rotor_estimation(random_point_pair, dorst_motor_estimate)
def test_dorst_motor_estimate_planes(self):
self.run_rotor_estimation(random_plane, dorst_motor_estimate)
def test_dorst_motor_estimate_spheres(self):
self.run_rotor_estimation(random_sphere, dorst_motor_estimate)
def test_estimate_rotor_lines_average_then_opt(self):
def estimation_func(pp_list_a, pp_list_b):
r_start = average_estimator(pp_list_a, pp_list_b)
query_start = [apply_rotor(b,r_start)(3).normal() for b in pp_list_b]
r_est, costs = estimate_rotor_objects(pp_list_a, query_start)
return (r_est*r_start).normal()
self.run_rotor_estimation(random_line, estimation_func)
def test_estimate_motor_lines_optimisation(self):
def estimation_func(pp_list_a, pp_list_b):
r_est, costs = estimate_rotor_objects(pp_list_a, pp_list_b, motor=True)
return r_est
self.run_rotor_estimation(random_line, estimation_func)
def test_estimate_motor_circles_optimisation(self):
def estimation_func(pp_list_a, pp_list_b):
r_est, costs = estimate_rotor_objects(pp_list_a, pp_list_b, motor=True)
return r_est
self.run_rotor_estimation(random_circle, estimation_func)
def test_estimate_motor_point_pairs_optimisation(self):
# """ Skip this one as it seems to take a fairly long time atm """
def estimation_func(pp_list_a, pp_list_b):
r_est, costs = estimate_rotor_objects(pp_list_a, pp_list_b, motor=True)
return r_est
self.run_rotor_estimation(random_point_pair, estimation_func)
def test_estimate_motor_planes_optimisation(self):
def estimation_func(pp_list_a, pp_list_b):
r_est, costs = estimate_rotor_objects(pp_list_a, pp_list_b, motor=True)
return r_est
self.run_rotor_estimation(random_plane, estimation_func)
def test_estimate_motor_spheres_optimisation(self):
def estimation_func(pp_list_a, pp_list_b):
r_est, costs = estimate_rotor_objects(pp_list_a, pp_list_b, motor=True)
return r_est
self.run_rotor_estimation(random_sphere, estimation_func)
def test_estimate_rotor_lines_optimisation(self):
def estimation_func(pp_list_a, pp_list_b):
r_est, costs = estimate_rotor_objects(pp_list_a, pp_list_b)
return r_est
self.run_rotor_estimation(random_line, estimation_func)
def test_estimate_rotor_circles_optimisation(self):
def estimation_func(pp_list_a, pp_list_b):
r_est, costs = estimate_rotor_objects(pp_list_a, pp_list_b)
return r_est
self.run_rotor_estimation(random_circle, estimation_func)
def test_estimate_rotor_point_pairs_optimisation(self):
# """ Skip this one as it seems to take a fairly long time atm """
def estimation_func(pp_list_a, pp_list_b):
r_est, costs = estimate_rotor_objects(pp_list_a, pp_list_b)
return r_est
self.run_rotor_estimation(random_point_pair, estimation_func)
def test_estimate_rotor_planes_optimisation(self):
def estimation_func(pp_list_a, pp_list_b):
r_est, costs = estimate_rotor_objects(pp_list_a, pp_list_b)
return r_est
self.run_rotor_estimation(random_plane, estimation_func)
def test_estimate_rotor_spheres_optimisation(self):
def estimation_func(pp_list_a, pp_list_b):
r_est, costs = estimate_rotor_objects(pp_list_a, pp_list_b)
return r_est
self.run_rotor_estimation(random_sphere, estimation_func)
def test_estimate_rotor_lines_sequential(self):
def estimation_func(pp_list_a, pp_list_b):
r_est, exit_flag = sequential_object_rotor_estimation(pp_list_a, pp_list_b)
print(exit_flag)
return r_est
self.run_rotor_estimation(random_line, estimation_func)
def test_estimate_rotor_circles_sequential(self):
def estimation_func(pp_list_a, pp_list_b):
r_est, exit_flag = sequential_object_rotor_estimation(pp_list_a, pp_list_b)
print(exit_flag)
return r_est
self.run_rotor_estimation(random_circle, estimation_func)
@SkipTest
def test_estimate_rotor_circles_sequential_then_opt(self):
def estimation_func(pp_list_a, pp_list_b):
r_est_1, exit_flag = sequential_object_rotor_estimation(pp_list_a, pp_list_b)
r_est_2 = 1.0
if exit_flag == 1:
object_set_a = [apply_rotor(l, r_est_1).normal() for l in pp_list_a]
r_est_2, costs = estimate_rotor_objects(object_set_a, pp_list_b)
return r_est_2 * r_est_1
self.run_rotor_estimation(random_circle, estimation_func)
@SkipTest
def test_estimate_rotor_point_pairs_sequential(self):
""" Skip this one as it seems to take a fairly long time atm """
def estimation_func(pp_list_a, pp_list_b):
r_est, exit_flag = sequential_object_rotor_estimation(pp_list_a, pp_list_b)
print(exit_flag)
return r_est
self.run_rotor_estimation(random_point_pair, estimation_func)
def test_estimate_rotor_planes_sequential(self):
def estimation_func(pp_list_a, pp_list_b):
r_est, exit_flag = sequential_object_rotor_estimation(pp_list_a, pp_list_b)
print(exit_flag)
return r_est
self.run_rotor_estimation(random_plane, estimation_func)
def test_estimate_rotor_spheres_sequential(self):
def estimation_func(pp_list_a, pp_list_b):
r_est, exit_flag = sequential_object_rotor_estimation(pp_list_a, pp_list_b)
print(exit_flag)
return r_est
self.run_rotor_estimation(random_sphere, estimation_func)
class SceneSimplificationTests(unittest.TestCase):
def test_simplify_recursive(self):
object_generator = random_line
n_clusters = 3
n_objects_per_cluster = 5
threshold = 0.5
all_objects, object_clusters = generate_n_clusters(object_generator,
n_clusters,
n_objects_per_cluster)
all_object_copy = [o for o in all_objects]
all_object_copy = simplify_scene_recursive(all_object_copy, threshold)
print(n_clusters)
# assert len(all_object_copy) == n_clusters
def test_simplify_scene(self):
object_generator = random_line
n_clusters = 3
n_objects_per_cluster = 5
threshold = 2.0
all_objects, object_clusters = generate_n_clusters(object_generator,
n_clusters,
n_objects_per_cluster)
all_object_copy1 = [o for o in all_objects]
all_object_copy1 = simplify_scene(all_object_copy1, threshold)
print(len(all_object_copy1))
# assert len(all_object_copy) == n_clusters
all_object_copy2 = [o for o in all_objects]
all_object_copy2 = simplify_scene(all_object_copy2, threshold)
print(len(all_object_copy2))
draw_objects(all_object_copy1)
draw_objects(all_object_copy2, color='rgb(255,0,0)')
class ObjectClusteringTests(unittest.TestCase):
@classmethod
def setUpClass(self):
self.object_generators = [
random_point_pair,
random_line,
random_circle,
random_plane,
random_sphere]
def run_n_clusters(self, object_generator, n_clusters, n_objects_per_cluster, n_shotgunning):
all_objects, object_clusters = generate_n_clusters(object_generator, n_clusters, n_objects_per_cluster)
[new_labels, centroids, start_labels, start_centroids] = n_clusters_objects(n_clusters, all_objects,
initial_centroids=None,
n_shotgunning=n_shotgunning,
averaging_method='unweighted')
return all_objects, new_labels, centroids
def test_clustering_point_pairs(self):
object_generator = random_point_pair
n_clusters = 3
n_objects_per_cluster = 10
n_shotgunning = 60
all_objects, labels, centroids = self.run_n_clusters(object_generator, n_clusters,
n_objects_per_cluster, n_shotgunning)
sc = visualise_n_clusters(all_objects, centroids, labels, object_type='point_pair',
color_1=np.array([255, 0, 0]), color_2=np.array([0, 255, 0]))
print(sc)
def test_clustering_lines(self):
object_generator = random_line
n_clusters = 3
n_objects_per_cluster = 10
n_shotgunning = 60
all_objects, labels, centroids = self.run_n_clusters(object_generator, n_clusters,
n_objects_per_cluster, n_shotgunning)
sc = visualise_n_clusters(all_objects, centroids, labels, object_type='line',
color_1=np.array([255, 0, 0]), color_2=np.array([0, 255, 0]))
print(sc)
def test_clustering_circles(self):
object_generator = random_circle
n_clusters = 3
n_objects_per_cluster = 10
n_shotgunning = 60
all_objects, labels, centroids = self.run_n_clusters(object_generator, n_clusters,
n_objects_per_cluster, n_shotgunning)
sc = visualise_n_clusters(all_objects, centroids, labels, object_type='circle',
color_1=np.array([255, 0, 0]), color_2=np.array([0, 255, 0]))
print(sc)
def test_clustering_spheres(self):