classify_rrect_clips.lua 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. function sk_scrape_startcanvas(c, fileName) end
  2. function sk_scrape_endcanvas(c, fileName) end
  3. function classify_rrect(rrect)
  4. if (rrect:type() == "simple") then
  5. local x, y = rrect:radii(0)
  6. if (x == y) then
  7. return "simple_circle"
  8. else
  9. return "simple_oval"
  10. end
  11. elseif (rrect:type() == "complex") then
  12. local numNotSquare = 0
  13. local rx, ry
  14. local same = true;
  15. local first_not_square_corner
  16. local last_not_square_corner
  17. for i = 1, 4 do
  18. local x, y = rrect:radii(i-1)
  19. if (x ~= 0 and y ~= 0) then
  20. if (numNotSquare == 0) then
  21. rx = x
  22. ry = y
  23. first_not_square_corner = i
  24. else
  25. last_not_square_corner = i
  26. if (rx ~= x or ry ~=y) then
  27. same = false
  28. end
  29. end
  30. numNotSquare = numNotSquare + 1
  31. end
  32. end
  33. local numSquare = 4 - numNotSquare
  34. if (numSquare > 0 and same) then
  35. local corners = "corners"
  36. if (numSquare == 2) then
  37. if ((last_not_square_corner - 1 == first_not_square_corner) or
  38. (1 == first_not_square_corner and 4 == last_not_square_corner )) then
  39. corners = "adjacent_" .. corners
  40. else
  41. corners = "opposite_" .. corners
  42. end
  43. elseif (1 == numSquare) then
  44. corners = "corner"
  45. end
  46. if (rx == ry) then
  47. return "circles_with_" .. numSquare .. "_square_" .. corners
  48. else
  49. return "ovals_with_" .. numSquare .. "_square_" .. corners
  50. end
  51. end
  52. return "complex_unclassified"
  53. elseif (rrect:type() == "rect") then
  54. return "rect"
  55. elseif (rrect:type() == "oval") then
  56. local x, y = rrect:radii(0)
  57. if (x == y) then
  58. return "circle"
  59. else
  60. return "oval"
  61. end
  62. elseif (rrect:type() == "empty") then
  63. return "empty"
  64. else
  65. return "unknown"
  66. end
  67. end
  68. function print_classes(class_table)
  69. function sort_classes(a, b)
  70. return a.count > b.count
  71. end
  72. array = {}
  73. for k, v in pairs(class_table) do
  74. if (type(v) == "number") then
  75. array[#array + 1] = {class = k, count = v};
  76. end
  77. end
  78. table.sort(array, sort_classes)
  79. local i
  80. for i = 1, #array do
  81. io.write(array[i].class, ": ", array[i].count, " (", array[i].count/class_table["total"] * 100, "%)\n");
  82. end
  83. end
  84. function sk_scrape_accumulate(t)
  85. if (t.verb == "clipRRect") then
  86. local rrect = t.rrect
  87. table["total"] = (table["total"] or 0) + 1
  88. local class = classify_rrect(rrect)
  89. table[class] = (table[class] or 0) + 1
  90. end
  91. end
  92. function sk_scrape_summarize()
  93. print_classes(table)
  94. --[[ To use the web scraper comment out the above call to print_classes, run the code below,
  95. and in the aggregator pass agg_table to print_classes.
  96. for k, v in pairs(table) do
  97. if (type(v) == "number") then
  98. local t = "agg_table[\"" .. k .. "\"]"
  99. io.write(t, " = (", t, " or 0) + ", table[k], "\n" );
  100. end
  101. end
  102. --]]
  103. end