-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlotFinderTests.cpp
More file actions
175 lines (149 loc) · 6.03 KB
/
Copy pathPlotFinderTests.cpp
File metadata and controls
175 lines (149 loc) · 6.03 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
#if !defined(OFX_IMGUI_ENABLE_TEST_ENGINE)
#error PlotFinderTests requires OFX_IMGUI_ENABLE_TEST_ENGINE - see example-test/config.make
#endif
#include "PlotFinderTests.h"
#include "PlotFinderTestHelpers.h"
#include "FinderCapabilities.h"
#include "FinderProgress.h"
#include "FinderRegistry.h"
#include "PlotFinderTypes.h"
#include "RegionHelpers.h"
#include "RegionTypes.h"
#include "imgui_te_context.h"
#include "imgui_te_engine.h"
#include <algorithm>
#include <cfloat>
namespace {
struct GoldenCase {
const char* name;
PlotFinderType type;
int minPaths;
int maxPaths;
float minLength;
float maxLength;
};
void registerGoldenCase(ImGuiTestEngine* e, const GoldenCase& golden) {
ImGuiTest* t = IM_REGISTER_TEST(e, "plotfind/golden", golden.name);
t->UserData = const_cast<GoldenCase*>(&golden);
t->TestFunc = [](ImGuiTestContext* ctx) {
const GoldenCase& g = *static_cast<const GoldenCase*>(ctx->Test->UserData);
const ofPixels checker = plotfind::test::makeCheckerboard(64, 8);
entt::registry reg;
PlotDoc doc(reg);
plotfind::test::setupGoldenLayer(doc, reg, g.type, checker);
doc.generateLayer(doc.activeLayer);
const plotfind::test::GenStats stats = plotfind::test::statsFromLayer(reg, doc.activeLayer);
IM_CHECK_GE(stats.pathCount, g.minPaths);
IM_CHECK_LE(stats.pathCount, g.maxPaths);
IM_CHECK_GE(stats.totalLength, g.minLength);
IM_CHECK_LE(stats.totalLength, g.maxLength);
const glm::vec2 paper = doc.getPaperSizeMM();
IM_CHECK_LE(stats.bounds.getWidth(), paper.x + 1.f);
IM_CHECK_LE(stats.bounds.getHeight(), paper.y + 1.f);
};
}
} // namespace
void RegisterPlotFinderTests(ImGuiTestEngine* e) {
ImGuiTest* t = nullptr;
t = IM_REGISTER_TEST(e, "plotfind/unit", "registry_count");
t->TestFunc = [](ImGuiTestContext* ctx) {
(void)ctx;
IM_CHECK_EQ(plotfind::FinderRegistry::instance().listIds().size(), 20u);
};
t = IM_REGISTER_TEST(e, "plotfind/unit", "plot_finder_id_roundtrip");
t->TestFunc = [](ImGuiTestContext* ctx) {
(void)ctx;
for (const auto type : plotfind::test::kAllFinders) {
const auto id = plotfind::PlotFinderId::fromType(type);
const auto back = plotfind::PlotFinderId::toType(id);
IM_CHECK_EQ(back, type);
}
};
t = IM_REGISTER_TEST(e, "plotfind/unit", "capabilities_table");
t->TestFunc = [](ImGuiTestContext* ctx) {
(void)ctx;
for (const auto type : plotfind::test::kAllFinders) {
const auto table = plotfind::capabilitiesFor(type);
const auto live = plotfind::FinderRegistry::instance().capabilities(type);
IM_CHECK_EQ(live.partition, table.partition);
IM_CHECK_EQ(live.strokeRole, table.strokeRole);
IM_CHECK_EQ(live.declaresFillIntent, table.declaresFillIntent);
IM_CHECK_EQ(live.canExportRegions, table.canExportRegions);
IM_CHECK_EQ(live.canBeCompositeSubFinder, table.canBeCompositeSubFinder);
}
};
t = IM_REGISTER_TEST(e, "plotfind/unit", "composite_orchestrator");
t->TestFunc = [](ImGuiTestContext* ctx) {
(void)ctx;
const auto caps = plotfind::capabilitiesFor(PlotFinderType::CompositeFinder);
IM_CHECK_EQ(caps.canBeCompositeSubFinder, false);
IM_CHECK_EQ(caps.canExportRegions, true);
};
t = IM_REGISTER_TEST(e, "plotfind/unit", "rect_mask");
t->TestFunc = [](ImGuiTestContext* ctx) {
(void)ctx;
const ofRectangle rect(10, 10, 20, 15);
const auto mask = plotfind::rectMask(64, 64, rect);
const int inside =
(int)std::count_if(mask.begin(), mask.end(), [](unsigned char v) { return v == 255; });
IM_CHECK_EQ(inside, 20 * 15);
};
t = IM_REGISTER_TEST(e, "plotfind/unit", "masked_grey_pixels");
t->TestFunc = [](ImGuiTestContext* ctx) {
(void)ctx;
std::vector<unsigned char> grey(64 * 64, 0);
std::vector<unsigned char> halfMask(64 * 64, 0);
for (int y = 0; y < 64; ++y)
for (int x = 0; x < 32; ++x)
halfMask[y * 64 + x] = 255;
const auto masked = plotfind::maskedGreyPixels(grey.data(), 64, 64, halfMask);
for (int i = 0; i < 64 * 64; ++i) {
if (halfMask[i] == 0) IM_CHECK_EQ(masked[i], (unsigned char)255);
}
};
t = IM_REGISTER_TEST(e, "plotfind/unit", "average_brightness_in_mask");
t->TestFunc = [](ImGuiTestContext* ctx) {
(void)ctx;
std::vector<unsigned char> grey(64 * 64, 0);
std::vector<unsigned char> halfMask(64 * 64, 0);
for (int y = 0; y < 64; ++y)
for (int x = 0; x < 32; ++x)
halfMask[y * 64 + x] = 255;
const float avg = plotfind::averageBrightnessInMask(grey.data(), 64, 64, halfMask);
IM_CHECK_FLOAT_EQ(avg, 0.f);
};
t = IM_REGISTER_TEST(e, "plotfind/unit", "progress_clamp");
t->TestFunc = [](ImGuiTestContext* ctx) {
(void)ctx;
float last = -1.f;
plotfind::reportProgress(
[&](float p, const std::string&) { last = p; }, 1.5f, "high");
IM_CHECK_LE(last, 1.f);
plotfind::reportProgress(
[&](float p, const std::string&) { last = p; }, -0.5f, "low");
IM_CHECK_GE(last, 0.f);
};
// Bounds are for a 64×64 checkerboard on default A4 paper (see PlotFinderTestHelpers).
static const GoldenCase goldenCases[] = {
{"checkerboard_structure_grid", PlotFinderType::GridFinder, 4, 800, 10.f, 25000.f},
{"checkerboard_hatch_parallel", PlotFinderType::ParallelHatch, 1, 5000, 1.f, 50000.f},
{"checkerboard_contour_edges", PlotFinderType::Contours, 1, 1200, 1.f, 50000.f},
{"checkerboard_stipple_dots", PlotFinderType::Stippling, 1, 100000, 1.f, 100000.f},
{"checkerboard_potrace_regions", PlotFinderType::Potrace, 1, 4000, 1.f, 50000.f},
{"checkerboard_structure_maze", PlotFinderType::MazeFinder, 1, 1500, 1.f, 12000.f},
};
for (const auto& golden : goldenCases) registerGoldenCase(e, golden);
t = IM_REGISTER_TEST(e, "plotfind/golden", "generate_all_finders");
t->TestFunc = [](ImGuiTestContext* ctx) {
(void)ctx;
const ofPixels checker = plotfind::test::makeCheckerboard(64, 8);
for (const auto type : plotfind::test::kAllFinders) {
entt::registry reg;
PlotDoc doc(reg);
plotfind::test::setupGoldenLayer(doc, reg, type, checker);
doc.generateLayer(doc.activeLayer);
const plotfind::test::GenStats stats = plotfind::test::statsFromLayer(reg, doc.activeLayer);
IM_CHECK_GE(stats.pathCount, 0);
}
};
}