2
0

test_sprune.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #define PAR_SPRUNE_IMPLEMENTATION
  2. #include "par_sprune.h"
  3. #include "describe.h"
  4. static par_sprune_context* context;
  5. static float boxes20[20 * 4] = {
  6. 0.41, 0.45, 0.57, 0.57,
  7. 0.7, 0.049, 0.74, 0.073,
  8. 0.3, 0.55, 0.46, 0.61,
  9. 0.31, 0.19, 0.62, 0.38,
  10. 0.3, 0.1, 0.34, 0.15,
  11. 0.23, 0.34, 0.23, 0.34,
  12. 0.58, 0.22, 0.82, 0.61, // 6
  13. 0.18, 0.71, 0.27, 0.72,
  14. 0.69, 0.34, 0.81, 0.55,
  15. 0.53, 0.1, 0.56, 0.17,
  16. 0.33, 0.69, 0.39, 0.69,
  17. 0.64, 0.46, 0.65, 0.46,
  18. 0.35, 0.78, 0.4, 0.8,
  19. 0.69, 0.63, 0.79, 0.68,
  20. 0.7, 0.51, 0.77, 0.53,
  21. 0.17, 0.79, 0.79, 0.81,
  22. 0.66, 0.49, 0.67, 0.5, // 16
  23. 0.36, 0.49, 0.41, 0.5,
  24. 0.58, 0.53, 0.58, 0.54,
  25. 0.25, 0.81, 0.51, 0.85,
  26. };
  27. static float boxes10[10 * 4] = {
  28. 0.55, 0.38, 0.55, 0.38,
  29. 0.5, 0.36, 0.51, 0.37,
  30. 0.28, 0.2, 0.4, 0.24,
  31. 0.39, 0.19, 0.4, 0.22,
  32. 0.31, 0.29, 0.7, 0.43,
  33. 0.019, 0.4, 0.02, 0.46,
  34. 0.58, 0.44, 0.64, 0.75,
  35. 0.39, 0.47, 0.48, 0.5,
  36. 0.15, 0.91, 0.21, 0.93,
  37. 0.62, 0.58, 0.63, 0.58,
  38. };
  39. static void export_svg(const char* filename, const char* color, float* boxes,
  40. int nboxes)
  41. {
  42. FILE* svgfile = fopen(filename, "wt");
  43. fprintf(svgfile,
  44. "<svg viewBox='-.1 -.1 1.2 1.2' width='640px' height='640px' "
  45. "version='1.1' "
  46. "xmlns='http://www.w3.org/2000/svg'>\n");
  47. fprintf(svgfile,
  48. "<rect fill-opacity='0.1' stroke='none' fill='%s' x='0' y='0' "
  49. "width='100%%' height='100%%'/>\n", color);
  50. fprintf(svgfile,
  51. "<g stroke-width='0.001' stroke-opacity='0.5' stroke='black' "
  52. "fill-opacity='0.2' fill='#2A8BB6'>\n");
  53. for (int i = 0; i < nboxes; i++) {
  54. float minx = boxes[i * 4 + 0];
  55. float miny = boxes[i * 4 + 1];
  56. float maxx = boxes[i * 4 + 2];
  57. float maxy = boxes[i * 4 + 3];
  58. float w = maxx - minx;
  59. float h = maxy - miny;
  60. fprintf(svgfile,
  61. "<rect x='%f' y='%f' width='%f' height='%f'/>\n", minx, miny, w, h);
  62. fprintf(svgfile, "<text text-anchor='middle' stroke='none' fill-opacity='1.0' "
  63. "x='%f' y='%f' font-size='%f'>%d</text>\n",
  64. 0.5 * (minx + maxx), 0.5 * (miny + maxy), 0.025, i);
  65. }
  66. fputs("</g>\n</svg>", svgfile);
  67. fclose(svgfile);
  68. }
  69. int main()
  70. {
  71. describe("par_sprune_overlap") {
  72. it("should pass a simple smoke test") {
  73. context = par_sprune_overlap(boxes20, 20, 0);
  74. assert_ok(context);
  75. export_svg("build/test_sprune_overlap_20.svg", "#2AB68B",
  76. boxes20, 20);
  77. par_sprune_overlap(boxes10, 10, context);
  78. export_svg("build/test_sprune_overlap_10.svg", "#8B2AB6",
  79. boxes10, 10);
  80. par_sprune_free_context(context);
  81. }
  82. }
  83. return assert_failures();
  84. }