aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/lua/classify_rrect_clips.lua
blob: 792d169ab3985342019a0dbc94e707aed769c294 (plain)
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

function sk_scrape_startcanvas(c, fileName) end

function sk_scrape_endcanvas(c, fileName) end

function classify_rrect(rrect)
    if (rrect:type() == "simple") then
        local x, y = rrect:radii(0)
        if (x == y) then
            return "simple_circle"
        else
            return "simple_oval"
        end
    elseif (rrect:type() == "complex") then
        local numNotSquare = 0
        local rx, ry
        local same = true;
        local first_not_square_corner
        local last_not_square_corner
        for i = 1, 4 do
            local x, y = rrect:radii(i-1)
            if (x ~= 0 and y ~= 0) then
                if (numNotSquare == 0) then
                    rx = x
                    ry = y
                    first_not_square_corner = i
                else
                   last_not_square_corner = i
                   if (rx ~= x or ry ~=y) then
                       same = false
                   end
                end
                numNotSquare = numNotSquare + 1
            end
        end
        local numSquare = 4 - numNotSquare
        if (numSquare > 0 and same) then
            local corners = "corners"
            if (numSquare == 2) then
                if ((last_not_square_corner - 1 == first_not_square_corner) or
                    (1 == first_not_square_corner and 4 == last_not_square_corner )) then
                    corners = "adjacent_" .. corners
                else
                    corners = "opposite_" .. corners
                end
            elseif (1 == numSquare) then
                corners = "corner"
            end
            if (rx == ry) then
                return "circles_with_" .. numSquare .. "_square_" .. corners
            else
                return "ovals_with_" .. numSquare .. "_square_" .. corners
            end
        end
        return "complex_unclassified"
    elseif (rrect:type() == "rect") then
        return "rect"
    elseif (rrect:type() == "oval") then
        local x, y = rrect:radii(0)
        if (x == y) then
            return "circle"
        else
            return "oval"
        end
    elseif (rrect:type() == "empty") then
        return "empty"
    else
        return "unknown"
    end
end

function print_classes(class_table)
  function sort_classes(a, b)
     return a.count > b.count
  end
  array = {}
  for k, v in pairs(class_table) do
      if (type(v) == "number") then
          array[#array + 1] = {class = k, count = v};
      end
  end
  table.sort(array, sort_classes)
  local i
  for i = 1, #array do
      io.write(array[i].class, ": ", array[i].count, " (", array[i].count/class_table["total"] * 100, "%)\n");
  end
end

function sk_scrape_accumulate(t)
    if (t.verb == "clipRRect") then
        local rrect = t.rrect
        table["total"] = (table["total"] or 0) + 1
        local class = classify_rrect(rrect)
        table[class] = (table[class] or 0) + 1
    end
end

function sk_scrape_summarize()
  print_classes(table)
  --[[ To use the web scraper comment out the above call to print_classes, run the code below,
       and in the aggregator pass agg_table to print_classes.
  for k, v in pairs(table) do
      if (type(v) == "number") then
          local t = "agg_table[\"" .. k .. "\"]"
          io.write(t, " = (", t, " or 0) + ", table[k], "\n" );
      end
  end
  --]]
end