correct ms to microsec in cern muon graph
[light-and-matter.git] / check_for_colliding_figures.rb
blobb63cc2c43002e158534496e4dab4a80eda0831dd
1 #!/usr/bin/ruby
3 $tex_points_to_mm = (25.4)/(65536.*72.27)
5 # If the interval (a,b) overlaps the interval (c,d) then return the amount by which they overlap.
6 # Otherwise return false.
7 def overlap(a,b,c,d)
8   if a>c then return overlap(c,d,a,b) end
9   # ab should lie strictly underneath cd
10   if b<c then return false end
11   return b-c
12 end
14 def mm(x)
15   return sprintf((x+0.5).to_i.to_s,"%d")
16 end
18 found_some = false
19 Dir["*.pos"].each { |filename|
20   found_some = true
21   filename=~/(.*)\.pos/
22   ch = $1 # interpret as string, not integer, e.g., in NP 001 is not the same as 01
23   #print "ch. #{ch}\n"
24   lo_y = {}
25   hi_y = {}
26   lo_x = {}
27   hi_x = {}
28   page = {}
29   index_by_page = []
30   File.open(filename,'r') do |f|
31     f.each_line { |line|
32       if line=~/^fig,label=fig:(.*),page=(.*),x=(.*),y=(.*),at=(.*)/ then
33         fig,pg,x,y = $1,$2.to_i,$3.to_i,$4.to_i
34         x = x*$tex_points_to_mm
35         y = y*$tex_points_to_mm
36         if page.has_key?(fig) then
37           if page[fig]!=pg then print "figure #{fig} occurs on both page #{page[fig]} and page #{pg} -- maybe the second one needs a suffix\n" end
38           if x<lo_x[fig] then lo_x[fig]=x end
39           if x>hi_x[fig] then hi_x[fig]=x end
40           if y<lo_y[fig] then lo_y[fig]=y end
41           if y>hi_y[fig] then hi_y[fig]=y end
42         else
43           page[fig]=pg
44           lo_x[fig]=x
45           hi_x[fig]=x
46           lo_y[fig]=y
47           hi_y[fig]=y
48         end
49         if index_by_page[pg]==nil then
50           index_by_page[pg] = {fig=>'1'}
51         else
52           index_by_page[pg][fig] = 1
53         end
54       end
55     }
56     index_by_page.each_index { |pg|
57       figs = index_by_page[pg]
58       if figs!=nil then
59         figs.keys.each { |f|
60           figs.keys.each { |g|
61             if f<g and overlap(lo_y[f],hi_y[f],lo_y[g],hi_y[g]) and overlap(lo_x[f],hi_x[f],lo_x[g],hi_x[g]) then
62               print "***** colliding figs, ch. #{ch}, p. #{pg}, #{f} and #{g}, overlapping by #{mm(overlap(lo_x[f],hi_x[f],lo_x[g],hi_x[g]))} mm horiz, #{mm(overlap(lo_y[f],hi_y[f],lo_y[g],hi_y[g]))} mm vert\n"
63               #print "    #{f} extends from #{mm(lo_y[f])} to #{mm(hi_y[f])} mm, #{g} from #{mm(lo_y[g])} to #{mm(hi_y[g])} mm \n"
64             end
65           }
66         }
67       end
68     }  
69   end
72 if !found_some then
73   print "no .pos files found\n"
74 end