-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rb
More file actions
1259 lines (1117 loc) · 50 KB
/
main.rb
File metadata and controls
1259 lines (1117 loc) · 50 KB
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
# file: main.rb
# The main program of the 2D objects in context protypeing system.
# It contains a class Field that abstracts the canvas of the main window that displays the s-reps.
# It contains a class InterpolationControl that abstracts a subwindow for user
# to select which s-rep's spokes to interpolate.
# The last part: Shoes.app runs the program.
#
# Author: Chong Shao (cshao@cs.unc.edu)
# ----------------------------------------------------------------
require 'nokogiri'
load 'lib/srep_toolbox.rb'
load 'lib/color.rb'
load 'view/interpolate_control.rb'
load 'view/srep_info.rb'
load 'view/file_loader_view.rb'
load 'lib/io_toolbox.rb'
#$mosrepindex = 3
$mosrepindex = ARGV[1].to_s
$noise_index = ARGV[2].to_s
$noise_file_name = 'noise_data_' + $noise_index + '.txt'
# change the path will effect all these things
$points_file_path = "data/mosrep"+$mosrepindex.to_s+ "/noise_" + $noise_index + "/interpolated_points_"
$radius_file_path = "data/mosrep"+$mosrepindex.to_s + "/noise_" + $noise_index +"/interpolated_rs_"
$logrk_file_path = "data/mosrep"+$mosrepindex.to_s + "/noise_" + $noise_index + "/interpolated_logrkm1s_"
#$logrk_file_path2 = "data2/interpolated_logrkm1s_"
#$logrk_file_path3 = "data3/interpolated_logrkm1s_"
#$radius_file_path2 = "data2/interpolated_rs_"
#$points_file_path2 = "data2/interpolated_points_"
$saved_linking_data_path = "data/saved_data/mosrep" + $mosrepindex.to_s + '/noise_' + $noise_index.to_s + '/linking'
$saved_mapping_data_path = "data/saved_data/mosrep" + $mosrepindex.to_s + '/noise_' + $noise_index.to_s + '/mapping'
$saved_data_path = 'data/saved_data/mosrep' + $mosrepindex.to_s + '/noise_' + $noise_index.to_s + '/'
$dilate_ratio = 1.05
$a_big_number = 100
$end_disk_spoke_number = 20
$show_subsetof_extended_spokes = true
# 0 to 277
$subset_index = [7, 25, 51, 75, 93, 113, 131, 151, 171]
$refine_linking_structure = false
$refine_window_r = 20
$refine_window_dir_lst = [90,90,90]
$refine_window_center_pos = [[380,410],[420,423], [500,440]]
$refined_linkingPts = []
$display_fake_linking = true
$fake_linkingPts = []
$fake_linkingPts2 = []
$fake_linkingPts3 = []
$fake_linkingPts4 = []
$fake_indices = [0, 55, 70, 155, 200, 235, 270, 305, 320, 355, 380, 415, 478, 510]
$render_from_base = true
def transform_interp_spoke_index(n)
k = n - 1
i = -1
if k <= 98
i = k * 2
elsif k <= 138
i = k - 98 + 237
elsif k <= 237
i = (k-139) * 2 + 1
else
i = (k-238) + 198
end
return i
end
#$colored_spoke_indices_origin = [35, 40, 41, 42, 44, 45, 47, 50]
$colored_spoke_indices_origin = []
$colored_spoke_indices = []
$colored_spoke_indices_origin.each do |n|
$colored_spoke_indices << transform_interp_spoke_index(n)
end
class Field
# this is the Field class that draws everything about s-rep on the main UI
attr_accessor :sreps, :shifts
def initialize(app, points, sreps, shifts)
@app = app
# set window size
@width, @height= 800, 600
@sreps = sreps
@shifts = shifts
end
def setSrep(srep, index)
@sreps[index] = srep
end
def addSrep(srep)
@sreps << srep
end
# methods for rendering points
def render_point_big(x, y, color)
@app.stroke color
@app.strokewidth 2
@app.nofill
@app.oval x-1,y-1, 2
end
def render_point_small(x, y, color, bg_color)
@app.stroke color
@app.line x, y, x, y+1
@app.stroke bg_color
@app.line x, y+1, x+1, y+1
end
# methods for rendering disks
def render_circle(cx, cy, d, color)
@app.stroke color
@app.nofill
@app.oval cx, cy, d
end
# methods for rendering spokes
def render_spokes(cx, cy, type, spoke_length, spoke_direction, color)
@app.stroke color
u_p1 = spoke_direction[0]
u_m1 = spoke_direction[1]
@app.stroke color
spoke_end_x_up1 = cx + spoke_length[0] * u_p1[0]
spoke_end_y_up1 = cy - spoke_length[0] * u_p1[1]
@app.line(cx, cy, spoke_end_x_up1, spoke_end_y_up1)
spoke_end_x_up2 = cx + spoke_length[0] * u_m1[0]
spoke_end_y_up2 = cy - spoke_length[0] * u_m1[1]
@app.line(cx, cy, spoke_end_x_up2, spoke_end_y_up2)
if type == 'end'
u_0 = spoke_direction[2]
spoke_end_x_u0 = cx + spoke_length[0] * u_0[0]
spoke_end_y_u0 = cy - spoke_length[0] * u_0[1]
@app.line(cx, cy, spoke_end_x_u0, spoke_end_y_u0)
end
end
# the method for rendering s-reps
def render_srep(*args)
srep = args[0]
shiftx = args[1]
shifty = args[2]
scale = args[3]
show_sphere = args[4]
show_sphere = false
srep.atoms.each_with_index do |atom, i|
render_atom(atom.x + shiftx, atom.y + shifty, atom.color)
if show_sphere
center_x = atom.x + shiftx - atom.spoke_length[0]
center_y = atom.y + shifty - atom.spoke_length[0]
d = atom.spoke_length[0] * 2
# if (srep.index != 1 or i != 2)
render_circle(center_x, center_y, d, srep.color)
# else
# render_circle(center_x+10, center_y+10, 80, srep.color)
# end
end
if srep.show_extend_disk
center_x = atom.x + shiftx - atom.expand_spoke_length[0]
center_y = atom.y + shifty - atom.expand_spoke_length[0]
d = atom.expand_spoke_length[0] * 2
render_circle(center_x, center_y, d, srep.color)
end
atom_x = atom.x+shiftx
atom_y = atom.y+shifty
#render_spokes(atom_x, atom_y, atom.type, atom.spoke_length, atom.spoke_direction, srep.color)
end
if srep.interpolated_spokes_begin.length > 0 and srep.show_interpolated_spokes
spoke_begin = srep.interpolated_spokes_begin
spoke_end = srep.interpolated_spokes_end
render_interp_spokes(shiftx, shifty, Color.white, spoke_begin, spoke_end, srep.index)
end
if srep.show_curve
# display the interpolated curve points
render_curve($sreps, srep.index, srep, shiftx, shifty)
end
end
def render_curve(sreps, index, srep, shiftx, shifty)
file_name = $points_file_path + index.to_s
if File::exists?(file_name)
gamma_file = File.open(file_name, "r")
else
alert('file does not exist, interpolate it now')
xt = srep.atoms.collect{|atom| atom.x}
yt = srep.atoms.collect{|atom| atom.y}
step_size = 0.01
interpolateSkeletalCurveGamma(xt,yt,step_size,srep.index)
gamma_file = File.open(file_name, "r")
end
xs = gamma_file.gets.split(" ").collect{|x| x.to_f}
ys = gamma_file.gets.split(" ").collect{|y| y.to_f}
if (srep.interpolate_finished)
xs.each_with_index do |x,i|
spoke_ind = i*2
atom_type = srep.getExtendInterpolatedSpokesEnd()[spoke_ind][4]
back_atom_type = srep.getExtendInterpolatedSpokesEnd()[spoke_ind+1][4]
if atom_type != 'end' and back_atom_type != 'end'
linkingIndex = srep.getExtendInterpolatedSpokesEnd()[spoke_ind][2]
if linkingIndex == -1
color1 = srep.color
else
color1 = sreps[linkingIndex].color
end
linkingIndex = srep.getExtendInterpolatedSpokesEnd()[spoke_ind+1][2]
if linkingIndex == -1
color2 = srep.color
else
color2 = sreps[linkingIndex].color
end
else
color1 = srep.color
color2 = srep.color
end
render_atom(x+shiftx,ys[i]+shifty, color1)
render_atom(x+shiftx+3*srep.orientation[0], ys[i]+shifty+3*srep.orientation[1], color2)
end
else
xs.each_with_index do |x,i|
render_atom(x+shiftx,ys[i]+shifty, srep.color)
render_atom(x+shiftx+3*srep.orientation[0], ys[i]+shifty+3*srep.orientation[1], srep.color)
end
end
end
# method for rendering interpolated spokes
def render_interp_spokes(shiftx, shifty, color, ibegin, iend, srep_index)
ibegin.each_with_index do |p, i|
if ($colored_spoke_indices.include? i and srep_index == 0)
@app.stroke Color.blue
else
@app.stroke color
end
@app.line(p[0]+shiftx, p[1]+shifty, iend[i][0]+shiftx, iend[i][1]+shifty)
end
end
# method for rendering extended part of spokes
def render_extend_interp_spokes(shiftx, shifty, color, ibegin, iend)
iend.each_with_index do |p, i|
# if p.size >= 4 and (p[3].is_a? Integer) and p[3] >= 0 and p[3] < 3
# @app.stroke $sreps[p[3]].color
if p.size >=3 and (p[2].is_a? Integer) and p[2] >= 0 and p[2] < 3
@app.stroke $sreps[p[2]].color
else
@app.stroke color
end
@app.line(ibegin[i][0]+shiftx, ibegin[i][1]+shifty, p[0]+shiftx, p[1]+shifty)
end
end
def refine_fake_linking_structure()
=begin
if $linkingPts.size > 0
$linkingPts.sort_by! {|p| p[0]}
$linkingPts.each_with_index do |p, i|
# alert(p)
if $fake_indices.include? i and p[1] < 150
$fake_linkingPts << p
end
end
end
=end
$fake_linkingPts = []
$subset_index.each do |i|
pt = $sreps[0].getExtendInterpolatedSpokesEnd()[i]
$fake_linkingPts << [ pt[0], pt[1] ]
end
$fake_linkingPts << [270-300, 320-300]
$fake_linkingPts << [770-300, 370-300]
# $fake_linkingPts << [301-300, 350-300]
# $fake_linkingPts << [760-300, 375-300]
# $fake_linkingPts << [430-300, 600-300]
$fake_linkingPts.sort_by! {|p| p[0]}
$fake_linkingPts3 << $fake_linkingPts[0]
$fake_linkingPts3 << $fake_linkingPts[1]
$fake_linkingPts3 << $fake_linkingPts[2]
$fake_linkingPts3 << $fake_linkingPts[3]
$fake_linkingPts3 << $fake_linkingPts[4]
$fake_linkingPts3 << [$fake_linkingPts[5][0], $fake_linkingPts[5][1] ]
$fake_linkingPts4 << [$fake_linkingPts[5][0], $fake_linkingPts[5][1] ]
# $fake_linkingPts4 << $fake_linkingPts[5]
$fake_linkingPts4 << $fake_linkingPts[6]
$fake_linkingPts4 << $fake_linkingPts[7]
$fake_linkingPts4 << $fake_linkingPts[8]
$fake_linkingPts4 << $fake_linkingPts[9]
$fake_linkingPts4 << $fake_linkingPts[10]
$fake_linkingPts2 << [450-300-5, 500-300]
$fake_linkingPts2 << [440-300-5, 550-300]
# $fake_linkingPts2 << [430-300, 600-300]
# $fake_linkingPts2 << [440-300, 550-300]
$fake_linkingPts2 << [$fake_linkingPts[5][0], $fake_linkingPts[5][1]]
# alert($fake_linkingPts2)
$fake_linkingPts2.sort_by! {|p| p[0]}
end
def render_fake_linkingPts(shifts)
@app.stroke Color.black
shift = shifts[0]
system('python interpolate_fake_curce.py ' + "'" + $fake_linkingPts3.to_s + "'")
interpolated_fake_linkingPts_reader = File.open('interpolate_fake_linkingPts.txt','r')
interpolated_fake_linkingPts = []
while line = interpolated_fake_linkingPts_reader.gets
interpolated_fake_linkingPts << [line.strip.split(" ")[0].to_f, line.strip.split(" ")[1].to_f ]
end
interpolated_fake_linkingPts_reader.close
interpolated_fake_linkingPts.each_with_index do |p, i|
if i < interpolated_fake_linkingPts.size - 1
@app.line(p[0]+shift, p[1]+shift, interpolated_fake_linkingPts[i+1][0]+shift, interpolated_fake_linkingPts[i+1][1]+shift)
end
# @app.stroke rgb(i *255 / 12, 0 ,0 )
# @app.oval p[0]+300, p[1]+300,3
# @app.line p
end
system('python interpolate_fake_curce.py ' + "'" + $fake_linkingPts2.to_s + "'")
interpolated_fake_linkingPts_reader = File.open('interpolate_fake_linkingPts.txt', 'r')
interpolated_fake_linkingPts2 = []
while line = interpolated_fake_linkingPts_reader.gets
interpolated_fake_linkingPts2 << [line.strip.split(" ")[0].to_f, line.strip.split(" ")[1].to_f]
end
interpolated_fake_linkingPts_reader.close
interpolated_fake_linkingPts2.each_with_index do |p, i|
if i < interpolated_fake_linkingPts2.size - 1
@app.line(p[0]+shift, p[1]+shift, interpolated_fake_linkingPts2[i+1][0]+shift, interpolated_fake_linkingPts2[i+1][1]+shift)
end
end
system('python interpolate_fake_curce.py ' + "'" + $fake_linkingPts4.to_s + "'")
interpolated_fake_linkingPts_reader = File.open('interpolate_fake_linkingPts.txt', 'r')
interpolated_fake_linkingPts2 = []
while line = interpolated_fake_linkingPts_reader.gets
interpolated_fake_linkingPts2 << [line.strip.split(" ")[0].to_f, line.strip.split(" ")[1].to_f]
end
interpolated_fake_linkingPts_reader.close
interpolated_fake_linkingPts2.each_with_index do |p, i|
if i < interpolated_fake_linkingPts2.size - 1
@app.line(p[0]+shift, p[1]+shift, interpolated_fake_linkingPts2[i+1][0]+shift, interpolated_fake_linkingPts2[i+1][1]+shift)
end
end
# @app.line $fake_linkingPts[0][0]+shift, $fake_linkingPts[0][1]+shift, 270, 320
# @app.line $fake_linkingPts[8][0]+shift, $fake_linkingPts[8][1]+shift, 770, 370
# @app.line $fake_linkingPts[2][0]+shift, $fake_linkingPts[2][1]+shift, 430, 600
end
#dispalys refine window
def render_refine_windows()
@app.stroke Color.red
r = $refine_window_r
$refine_window_center_pos.each_with_index do |p, i|
@app.oval(p[0]-r,p[1]-r,r*2)
# render the dir for each window
angle = $refine_window_dir_lst[i].to_f / 180 * Math::PI
@app.line(p[0]-r*Math.cos(angle), p[1]+r*Math.sin(angle),p[0]+r*Math.cos(angle), p[1]-r*Math.sin(angle))
@app.oval(p[0]+r*Math.cos(angle)-3, p[1]-r*Math.sin(angle)-3,3)
end
end
def refine_linking_structure()
refined_linkingPts = $linkingPts.dup
# alert($linkingPts)
ret = []
refined_linkingPts.each_with_index do |dot, i|
# if dot is in window
# has_new_dots = false
$refine_window_center_pos.each_with_index do |pp, j|
p = [pp[0] - 300, pp[1] - 300]
if ( dot[0] - p[1] )**2 + (dot[1] - p[1])**2 < $refine_window_r ** 2
#alert("hi")
=begin
dot_vec = [ dot[0] - p[0] , (dot[1] - p[1]) ]
dot_vec_len = Math.sqrt(dot_vec[0]**2 + dot_vec[1]**2)
dot_vec = [dot_vec[0]/dot_vec_len, dot_vec[1]/dot_vec_len]
# get the dot product between it and the angle
theta = $refine_window_dir_lst[i].to_f / 180 * Math::PI
dir_vec = [ Math.cos(theta), -1*Math.sin(theta) ]
cos_angle = dir_vec[0] * dot_vec[0] + dir_vec[1] * dot_vec[1]
if (cos_angle < 0) # other side
new_dot = [dot[0]+dir_vec[0]*r*cos_angle,dot[1]+dir_vec[1]*r*cos_angle]
else # same side
new_dot = [dot[0]-dir_vec[0]*cos_angle,dot[1]-dir_vec[1]**cos_angle]
end
=end ret << [p[0],p[1]]
break
# new_dot = [p[0], p[1]]
# $refined_linkingPts << new_dot
# $has_new_dots = true
# break
else
ret << dot
end
# $refined_linkingPts << dot
end
end
alert(ret)
# alert($linkingPts)
return ret
end
def color_one_spoke(shiftx, shifty, color, ibegin, iend)
@app.stroke color
@app.line(ibegin[0]+shiftx, ibegin[1]+shifty, iend[0]+shiftx, iend[0]+shifty)
end
# method for rendering subset of extended part of spokes
def render_subset_extend_interp_spokes(shiftx, shifty, color, ibegin, iend, srep_index)
iend.each_with_index do |p, i|
# if p.size >= 4 and (p[3].is_a? Integer) and p[3] >= 0 and p[3] < 3
# @app.stroke $sreps[p[3]].color
if srep_index == 0
if $subset_index.include? i
if p.size >=3 and (p[2].is_a? Integer) and p[2] >= 0 and p[2] < 3
@app.stroke $sreps[p[2]].color
other_srep_index = p[2]
other_srep_spoke_index = p[3]
other_srep_spoke_begin = $sreps[other_srep_index].interpolated_spokes_begin[other_srep_spoke_index]
@app.line(other_srep_spoke_begin[0]+shiftx, other_srep_spoke_begin[1]+shifty, p[0]+shiftx, p[1]+shifty)
else
@app.stroke color
end
@app.line(ibegin[i][0]+shiftx, ibegin[i][1]+shifty, p[0]+shiftx, p[1]+shifty)
end
end
end
end
def color_one_spoke(shiftx, shifty, color, ibegin, iend)
@app.stroke color
@app.line(ibegin[0]+shiftx, ibegin[1]+shifty, iend[0]+shiftx, iend[0]+shifty)
end
# method for rendering atoms
def render_atom(x, y, color)
render_point_big(x, y, color)
end
# method for rendering linking structure
def render_linking_structure(shifts)
shift = shifts[0]
if not $refine_linking_structure
$linkingPts.each do |pt|
if pt != []
render_atom(pt[0]+shift, pt[1]+shift, Color.black)
end
end
else
$refined_linkingPts.each do |pt|
if pt != []
render_atom(pt[0]+shift, pt[1]+shift, Color.black)
end
end
end
end
# this method calls the render_srep() and render_linking_structure() method
def paint
if $render_from_base
@app.nostroke
checkSrepIntersection
$sreps.each.with_index do |srep, i|
render_srep(srep, @shifts[i] , @shifts[i] , 1.5, true)
end
#render extended spokes
$sreps.each.with_index do |srep, i|
if (srep.getExtendInterpolatedSpokesEnd()).length > 0 and srep.show_extend_spokes
spoke_begin = srep.interpolated_spokes_begin
spoke_end = srep.getExtendInterpolatedSpokesEnd()
if ($show_subsetof_extended_spokes)
render_subset_extend_interp_spokes(@shifts[i],@shifts[i], srep.color, spoke_begin, spoke_end, srep.index)
else
render_extend_interp_spokes(@shifts[i],@shifts[i], srep.color, spoke_begin, spoke_end)
end
end
#render_extended_interp_spokes(srep, @shifts[i] , @shifts[i] , 1.5, true)
end
if $show_linking_structure
if $dilateCount != 22
render_linking_structure(@shifts)
else
refine_fake_linking_structure()
render_fake_linkingPts(@shifts)
end
end
# render refining windows
if $refine_linking_structure
render_refine_windows()
end
else
@app.stroke Color.black
# read stdev 2 data and render
shift = 300
data_vector_reader = File.open('data_vector.txt','r')
data_vector = []
while line = data_vector_reader.gets
data_vector << line.strip.to_f
end
data_vector_reader.close
base_pts = data_vector[0..101*2*3-1]
base_pts_xy = []
(base_pts.size / 2).times do |i|
base_pts_xy << [base_pts[i*2], base_pts[i*2+1]]
end
base_pts_xy.each do |p|
@app.oval(p[0]+shift,p[1]+shift,3)
end
offset1 = 101*2*3
spoke_dir = data_vector[offset1 .. 278*2*3+offset1-1]
spoke_dir_xy = []
(spoke_dir.size/2).times do |i|
spoke_dir_xy << [ spoke_dir[i*2], spoke_dir[i*2+1] ]
end
offset2 = 278*2*3+offset1
spoke_len = data_vector[offset2 .. 278*3+offset2-1]
offset3 = 278*3+offset2
extended_lens = data_vector[offset3 .. 278*3+offset3-1]
# alert(offset3)
99.times do |i|
@app.stroke Color.black
extended_len = extended_lens[i]
spoke = [ spoke_dir_xy[i][0], spoke_dir_xy[i][1] ]
spoke_len = Math.sqrt(spoke[0]**2 + spoke[1]**2)
extended = [ spoke[0].to_f * extended_len / spoke_len, spoke[1].to_f * extended_len / spoke_len ]
if i != 0
@app.line base_pts_xy[i+1][0]+shift, base_pts_xy[i+1][1]+shift, base_pts_xy[i+1][0] +shift+ spoke_dir_xy[i][0] + extended[0], base_pts_xy[i+1][1]+shift + spoke_dir_xy[i][1] + extended[1]
end
extended_len = extended_lens[i+139]
spoke = [ spoke_dir_xy[i+139][0], spoke_dir_xy[i+139][1] ]
spoke_len = Math.sqrt(spoke[0]**2 + spoke[1]**2)
extended = [ spoke[0].to_f * extended_len / spoke_len, spoke[1].to_f * extended_len / spoke_len ]
if i != 48 and i != 73 and i != 24
@app.line base_pts_xy[i+1][0]+shift, base_pts_xy[i+1][1]+shift, base_pts_xy[i+1][0] +shift+ spoke_dir_xy[i+139][0]+extended[0], base_pts_xy[i+1][1]+shift + spoke_dir_xy[i+139][1] + extended[1]
end
# @app.stroke Color.green
@app.stroke rgb(0,51,0,1)
@app.line base_pts_xy[i+1+101][0]+shift, base_pts_xy[i+1+101][1]+shift, base_pts_xy[i+1+101][0] +shift+ spoke_dir_xy[i+278][0], base_pts_xy[i+1+101][1]+shift + spoke_dir_xy[i+278][1]
@app.line base_pts_xy[i+1+101][0]+shift, base_pts_xy[i+1+101][1]+shift, base_pts_xy[i+1+101][0] +shift+ spoke_dir_xy[i+139+278][0], base_pts_xy[i+1+101][1]+shift + spoke_dir_xy[i+139+278][1]
@app.stroke Color.red
@app.line base_pts_xy[i+1+202][0]+shift, base_pts_xy[i+1+202][1]+shift, base_pts_xy[i+1+202][0] +shift+ spoke_dir_xy[i+556][0] , base_pts_xy[i+1+202][1]+shift + spoke_dir_xy[i+556][1]
@app.line base_pts_xy[i+1+202][0]+shift, base_pts_xy[i+1+202][1]+shift, base_pts_xy[i+1+202][0] +shift+ spoke_dir_xy[i+139+556][0], base_pts_xy[i+1+202][1]+shift + spoke_dir_xy[i+139+556][1]
end
40.times do |i|
@app.stroke Color.black
if i != 0
@app.line base_pts_xy[1][0]+shift, base_pts_xy[1][1]+shift, base_pts_xy[1][0] +shift+ spoke_dir_xy[i+238][0], base_pts_xy[1][1]+shift + spoke_dir_xy[i+238][1]
end
@app.line base_pts_xy[99][0]+shift, base_pts_xy[99][1]+shift, base_pts_xy[99][0] +shift+ spoke_dir_xy[i+99][0], base_pts_xy[99][1]+shift + spoke_dir_xy[i+99][1]
# @app.stroke Color.green
@app.stroke rgb(0,51,0,1)
@app.line base_pts_xy[1+101][0]+shift, base_pts_xy[1+101][1]+shift, base_pts_xy[1+101][0] +shift+ spoke_dir_xy[i+238+278][0], base_pts_xy[1+101][1]+shift + spoke_dir_xy[i+238+278][1]
@app.line base_pts_xy[99+101][0]+shift, base_pts_xy[99+101][1]+shift, base_pts_xy[99+101][0] +shift+ spoke_dir_xy[i+99+278][0], base_pts_xy[99+101][1]+shift + spoke_dir_xy[i+99+278][1]
@app.stroke Color.red
@app.line base_pts_xy[1+202][0]+shift, base_pts_xy[1+202][1]+shift, base_pts_xy[1+202][0] +shift+ spoke_dir_xy[i+238+556][0], base_pts_xy[1+202][1]+shift + spoke_dir_xy[i+238+556][1]
@app.line base_pts_xy[99+202][0]+shift, base_pts_xy[99+202][1]+shift, base_pts_xy[99+202][0] +shift+ spoke_dir_xy[i+99+556][0], base_pts_xy[99+202][1]+shift + spoke_dir_xy[i+99+556][1]
end
end
end
# this method interates the sreps and let them check the spoke intersection
def checkSrepIntersection
(0..$sreps.length-1).each do |j|
(0..$sreps.length-1).each do |i|
if i != j
$sreps[j].checkIntersection($sreps[i])
end
end
end
end
def [](*args)
x, y = args
raise "Cell #{x}:#{y} does not exist!" unless cell_exists?(x, y)
@field[y][x]
end
def []=(*args)
x, y, v = args
cell_exists?(x, y) ? @field[y][x] = v : false
end
end
# this is the driver for the main program
Shoes.app :width => 1000, :height => 800, :title => '2d multi object' do
def render_field
clear do
#background rgb(192, 192, 192, 0.7) # light gray
background rgb(205,205,0,0.7) # yellow
# html_doc = Nokogiri::HTML("<html><body><h1>Loaded with XML</h1></body></html>")
# para html_doc
# para " \n\n\n\n"
para $info
flow :margin => 6 do
# code for buttons
button("Dilate") {
$sreps.each do |srep|
srep.atoms.each do |atom|
atom.dilate($dilate_ratio)
end
# dilate interpolated spokes
# and check intersection
# user can specify first serval count to speed up dilate
if $dilateCount > 6
sublinkingPts = srep.extendInterpolatedSpokes($dilate_ratio, $sreps, true)
sublinkingPts.each do |p|
$linkingPts << p
end
else
srep.extendInterpolatedSpokes($dilate_ratio, $sreps, false)
end
end
$linkingPts = $linkingPts.uniq
if $refine_linging_structure
$refined_linkingPts.replace( @field.refine_linking_structure() )
end
$dilateCount = $dilateCount + 1
refresh @points, $sreps, @shifts
}
button("Reset") {
@dontGrowLst= []
initialConfig
}
button("Check File") {
window :title => "draw srep", :width => 402, :height => 375 do
ip = InterpolateControl.new(self) # change dp to ip
end
}
button("Load") {
window :title => "choose file", :width => 250, :height => 100 do
flv = FileLoaderView.new(self)
end
}
button("Interpolate Spokes") {
# make this correct!!
# the k is the change of the swings .
# ... which can be approximated by the change of the swings of 3 base points .....
# i guesss...
# need to look at my report!
#
$sreps.each_with_index do |srep, srep_index|
file1 = File.open($points_file_path + srep_index.to_s, 'r')
test = "here!!"
xt = file1.gets.split(' ').collect{|x| x.to_f}
yt = file1.gets.split(' ').collect{|y| y.to_f}
file2 = File.open($radius_file_path + srep_index.to_s, 'r')
rt = file2.gets.split(' ').collect{|r| r.to_f} #=> get the r files
# if srep_index == 2
# alert(rt.to_s)
# end
file3 = File.open($logrk_file_path + srep_index.to_s, 'r')
logrkm1 = file3.gets.split(' ').collect{|logrkm1| logrkm1.to_f}
puts "logrkm1 length: " + logrkm1.length.to_s
$a_big_number.times do # default: 100
# interpolate one side
indices = srep.base_index
base_index = $current_base_index
distance_to_next_base = ( indices[base_index+1] - indices[base_index] ) - $step_go_so_far
if distance_to_next_base == 0 # <= reached another base point
$step_go_so_far = 0
$current_base_index = $current_base_index +1
distance_to_next_base = indices[base_index+1] - indices[base_index]
spoke_index = $current_base_index
end
curr_index = indices[base_index] + $step_go_so_far + 1
# this is wrong... . <= why?
d1t = 0.01 * $step_go_so_far
d2t = distance_to_next_base * 0.01
# calculate parameters
# read all points, rs, logrkm1s from the file
# file = File.open($points_file_path + srep_index.to_s, 'r')
# xt/ yt: interpolatd x and y locus
# xt = file1.gets.split(' ').collect{|x| x.to_f}
# yt = file1.gets.split(' ').collect{|y| y.to_f}
# file2 = File.open($radius_file_path + srep_index.to_s, 'r')
# rt: interpolated radius r
# rt = file2.gets.split(' ').collect{|r| r.to_f}
# logrk: interpolated logrk <= needs to be fixed.
# here it uses the large difference to produce v, which is not good.
if curr_index < xt.length-1
v1t = [xt[curr_index] - xt[indices[base_index]], yt[curr_index] - yt[indices[base_index]]]
# it checks whether it reaches the next base index
if curr_index == indices[base_index+1]
v2t = [xt[indices[base_index+1]+1] - xt[curr_index], yt[indices[base_index+1]+1] - yt[curr_index]]
else
v2t = [xt[indices[base_index+1]] - xt[curr_index], yt[indices[base_index+1]] - yt[curr_index]]
end
# puts "v1t: " + v1t.to_s
# yes it normalizes the size of v vector
size_v1t = v1t[0]**2 + v1t[1]**2
norm_v1t = v1t.collect{|v| v / size_v1t}
size_v2t = v2t[0]**2 + v2t[1]**2
# puts "size_v2t: " + size_v2t.to_s
norm_v2t = v2t.collect{|v| v / size_v2t}
# these k's are calculated using the stored value of log(1-rk)
puts "curr logrkm1: " + logrkm1[indices[base_index]].to_s
puts "additional logrkm1: " + logrkm1[indices[base_index+1]].to_s
k1t = ( 1 + ( -1 * Math.exp(logrkm1[indices[base_index]] ) ) ) / rt[indices[base_index]]
if logrkm1[indices[base_index+1]]
k2t = ( 1 + ( -1 * Math.exp(logrkm1[indices[base_index+1]] ) ) ) / rt[indices[base_index+1]]
else
k2t = ( 1 + ( -1 * Math.exp(logrkm1[-1] ) ) ) / rt[indices[-1]]
end
u1t = srep.atoms[base_index].spoke_direction[0]
u2t = srep.atoms[base_index+1].spoke_direction[0]
# if (srep_index == 2)
# ui = interpolateSpokeAtPos2(u1t,norm_v1t,k1t,d1t)
# else
ui = interpolateSpokeAtPos(u1t, norm_v1t, k1t, d1t, u2t, norm_v2t, k2t, d2t)
# end
# if srep_index == 2
# alert ui
# end
# ui should be normalized
# ui_size = Math.sqrt(ui[0] ** 2 + ui[1] ** 2)
# ui = [ui[0]/ui_size, ui[1]/ui_size]
# puts "ui: " + ui.to_s
srep.interpolated_spokes_begin << [xt[curr_index],yt[curr_index],-1]
# puts "rt: " + rt[curr_index-1].to_s
srep.interpolated_spokes_end << [xt[curr_index]+ui[0]*rt[curr_index],yt[curr_index]-ui[1]*rt[curr_index],-1,[],'regular']
# interpolate another side
u1t = $sreps[srep_index].atoms[base_index].spoke_direction[1]
u2t = $sreps[srep_index].atoms[base_index+1].spoke_direction[1]
# if (srep_index == 2)
# ui = interpolateSpokeAtPos2(u1t,norm_v1t,k1t,d1t)
# else
ui = interpolateSpokeAtPos(u1t, norm_v1t, k1t, d1t, u2t, norm_v2t, k2t, d2t)
# end
# puts "ui: " + ui.to_s
spoke_index = indices[base_index]+$step_go_so_far+1
spoke_begin_x = xt[spoke_index]
spoke_begin_y = yt[spoke_index ]
$sreps[srep_index].interpolated_spokes_begin << [spoke_begin_x,spoke_begin_y,-1,[],'regular']
# puts "rt: " + rt[indices[base_index]+$step_go_so_far].to_s
spoke_end_x = spoke_begin_x + ui[0]*rt[spoke_index]
spoke_end_y = spoke_begin_y - ui[1]*rt[spoke_index]
$sreps[srep_index].interpolated_spokes_end << [spoke_end_x,spoke_end_y,-1,[],'regular']
else
# add spoke interpolation for end disks
# get atom for end disks
end_atom_one = srep.atoms[0]
end_atom_two = srep.atoms[-1]
end_atoms = [end_atom_one, end_atom_two]
# get upper and lower and middle spokes
end_atoms.each_with_index do |atom, i|
atom_spoke_dir_plus1 = atom.spoke_direction[0]
atom_spoke_dir_minus1 = atom.spoke_direction[1]
atom_spoke_dir_zero = atom.spoke_direction[2]
x_diff_1 = atom_spoke_dir_zero[0] - atom_spoke_dir_plus1[0]
x_diff_2 = atom_spoke_dir_zero[0] - atom_spoke_dir_minus1[0]
y_diff_1 = atom_spoke_dir_zero[1] - atom_spoke_dir_plus1[1]
y_diff_2 = atom_spoke_dir_zero[1] - atom_spoke_dir_minus1[1]
x_step_size_1 = x_diff_1.to_f / 15
y_step_size_1 = y_diff_1.to_f / 15
x_step_size_2 = x_diff_2.to_f / 15
y_step_size_2 = y_diff_2.to_f / 15
previous_x = atom_spoke_dir_plus1[0]
previous_y = atom_spoke_dir_plus1[1]
$end_disk_spoke_number.times do
new_spoke_dir_x = previous_x + x_step_size_1
new_spoke_dir_y = previous_y + y_step_size_1
# normalize
length_new_spoke = Math.sqrt(new_spoke_dir_x**2 + new_spoke_dir_y**2)
new_spoke_dir_x = new_spoke_dir_x / length_new_spoke
new_spoke_dir_y = new_spoke_dir_y / length_new_spoke
previous_x = new_spoke_dir_x
previous_y = new_spoke_dir_y
# calculate interpolated spoke end
new_spoke_vector_x = atom.spoke_length[0]*new_spoke_dir_x
new_spoke_vector_y = atom.spoke_length[0]*new_spoke_dir_y
new_spoke_end = [atom.x + new_spoke_vector_x, atom.y - new_spoke_vector_y,-1,[],'end']
$sreps[srep_index].interpolated_spokes_begin << [atom.x, atom.y]
$sreps[srep_index].interpolated_spokes_end << new_spoke_end
end
previous_x = atom_spoke_dir_minus1[0]
previous_y = atom_spoke_dir_minus1[1]
$end_disk_spoke_number.times do
new_spoke_dir_x = previous_x + x_step_size_2
new_spoke_dir_y = previous_y + y_step_size_2
# normalize
length_new_spoke = Math.sqrt(new_spoke_dir_x**2 + new_spoke_dir_y**2)
new_spoke_dir_x = new_spoke_dir_x / length_new_spoke
new_spoke_dir_y = new_spoke_dir_y / length_new_spoke
previous_x = new_spoke_dir_x
previous_y = new_spoke_dir_y
# calculate interpolated spoke end
new_spoke_vector_x = atom.spoke_length[0]*new_spoke_dir_x
new_spoke_vector_y = atom.spoke_length[0]*new_spoke_dir_y
new_spoke_end = [atom.x + new_spoke_vector_x, atom.y - new_spoke_vector_y,-1,[],'end']
$sreps[srep_index].interpolated_spokes_begin << [atom.x, atom.y]
$sreps[srep_index].interpolated_spokes_end << new_spoke_end
$sreps[srep_index].interpolate_finished = true
end
end
$info = "interpolation finished"
end
$step_go_so_far = $step_go_so_far + 1
# puts "count: "+ $step_go_so_far.to_s
refresh @points, $sreps, @shifts
end
$step_go_so_far = 1
$current_base_index = 0
end
}
button("Info") {
window :title => "s-rep info", :width => 402, :height => 375 do
# si = sreps info
si = SrepInfo.new(self)
end
}
# ---------------------------------------------------
# EDIT
button("orthogonalization") {
orthogonalized_middle_spokes = computeOrthogonalizedSpokes($sreps[0])
3.times do |i|
orthogonalized_middle_spokes[i][1] = orthogonalized_middle_spokes[i][1]
$sreps[0].atoms[i+1].spoke_direction[0] = orthogonalized_middle_spokes[i]
end
$info = orthogonalized_middle_spokes.to_s
$sreps[0].interpolated_spokes_begin = []
$sreps[0].interpolated_spokes_end = []
$sreps[0].extend_interpolated_spokes_end = []
refresh @points, $sreps, @shifts
}
#
button("save") {
# save all the linking infomation with assocaited mosrep index as file name postfix
# things to save:
# linking structure index
# mapping: medial curve pts color, where it links to
# save as txt
# save linking
link_save = File.open($saved_linking_data_path,'w')
link_save.write($linkingPts.to_s)
link_save.close
# save mapping information
mapping_save = File.open($saved_mapping_data_path, 'w')
mapping_info = $sreps[0].get_linking_info # TODO: fix this get_linking_info method
mapping_save.write(mapping_info.to_s)
mapping_save.close
}
button("save2") {
# according to the notes, this save2 button will save the follows:
# 1. array for all the interpolated spokes for the reference obj, parameterized by a
# number from 0 to 1 ( currently it has 100 spokes? )
# 2. index for the neighbor ( 0 if not intersected ) <- how about self-linking?
# 3. array for linking length
# 4. a link position array, indicates which spoke on that object this spoke links to
# ^^^^^^^^^^^^^^^
# data1: base pts positions for 3 s-reps, in 3 files
# data2: interpolated spokes end for 3 sreps, in 3 files
# data3: extended_spokes for srep0, in one file
# from the 3 kinds of data above, we should be able to generate all the data for stats
number_of_sreps = $sreps.size
number_of_sreps.times do |i|
data1_save = File.open($saved_data_path + "interpolated_pts_" + i.to_s, 'w')
pts_info = $sreps[i].skeletal_curve
data1_save.write(pts_info.to_s)
data1_save.close
end
number_of_sreps.times do |i|
data2_save = File.open($saved_data_path + "interpolated_spokes_" + i.to_s,'w')
interp_info_begin = $sreps[i].interpolated_spokes_begin
data2_save.write(interp_info_begin.to_s)
data2_save.write("\n========\n")
interp_info_end = $sreps[i].interpolated_spokes_end
data2_save.write(interp_info_end.to_s)
data2_save.close
end
data3_save = File.open($saved_data_path + 'ref_obj_linking','w')
ref_obj_linking_info = $sreps[0].extend_interpolated_spokes_end
data3_save.write(ref_obj_linking_info.to_s)
data3_save.close
alert('save completed.')
}
button("save as") {
# currently save as config_1
saveSrepDataAsXML('srep_data/config_1.xml',$sreps,3)
}
button("label spokes") {
indices = [49]
indices.each do |k|
i = k / 2 - 1
# get the position for that spoke with index i
shiftx = @shifts[0]
shifty = @shifts[1]
srep_0 = $sreps[0]
ibegin = [srep_0.interpolated_spokes_begin[i][0],srep_0.interpolated_spokes_begin[i][1]]
iend = [srep_0.interpolated_spokes_end[i][0],srep_0.interpolated_spokes_end[i][1]]
# color it
@field.color_one_spoke(shiftx, shifty, Color.blue, ibegin, iend)
end
}
# button("Interpolate Spokes 2") {
# srep = $sreps[0]
# srep_index = 0
# curr_index = 0
# if not $xt
# file = File.open($points_file_path + srep_index.to_s, 'r')
# $xt = file.gets.split(' ').collect{|x| x.to_f}
# $yt = file.gets.split(' ').collect{|y| y.to_f}
# file = File.open($radius_file_path2 + srep_index.to_s, 'r')
# # rt: interpolated radius r
# $rt = file.gets.split(' ').collect{|r| r.to_f}
# # logrk: interpolated logrk
# file = File.open($logrk_file_path3 + srep_index.to_s, 'r')
# $logrkm1 = file.gets.split(' ').collect{|logrkm1| logrkm1.to_f}
# $indices = srep.base_index
# puts "read all data needed >"
# end
# # initially it is zero
# base_index = $current_base_index
# distance_to_next_base = ( $indices[base_index+1] - $indices[base_index] ) - $step_go_so_far
# puts "dis to next base: " + distance_to_next_base.to_s
# curr_index = $indices[base_index] + $step_go_so_far + 1
# if distance_to_next_base == 0 # <= reached another base point
# puts "############################################################"
# puts "############################################################"
# puts " at base end! "
# puts "############################################################"
# puts "############################################################"
# $step_go_so_far = 0
# # update current_base_index
# $current_base_index = $current_base_index + 1
# $ui1 = srep.atoms[$current_base_index].spoke_direction[0]
# $ui2 = srep.atoms[$current_base_index].spoke_direction[1]
# distance_to_next_base = $indices[base_index+1] - $indices[base_index]