aboutsummaryrefslogtreecommitdiff
path: root/svg-flatten/src
diff options
context:
space:
mode:
Diffstat (limited to 'svg-flatten/src')
-rw-r--r--svg-flatten/src/nopencv.cpp26
-rw-r--r--svg-flatten/src/nopencv_test.cpp19
2 files changed, 25 insertions, 20 deletions
diff --git a/svg-flatten/src/nopencv.cpp b/svg-flatten/src/nopencv.cpp
index c222734..9fd2b2f 100644
--- a/svg-flatten/src/nopencv.cpp
+++ b/svg-flatten/src/nopencv.cpp
@@ -10,6 +10,8 @@
using namespace gerbolyze;
using namespace gerbolyze::nopencv;
+static constexpr bool debug = false;
+
/* directions:
* 0
* 7 1
@@ -33,7 +35,7 @@ enum Direction {
D_NW
};
-//const char * const dir_str[8] = { "N", "NE", "E", "SE", "S", "SW", "W", "NW" };
+const char * const dir_str[8] = { "N", "NE", "E", "SE", "S", "SW", "W", "NW" };
static struct {
int x;
@@ -53,7 +55,10 @@ static Direction flip_direction[8] = {
static void follow(gerbolyze::nopencv::Image32 &img, int start_x, int start_y, Direction initial_direction, int nbd, int connectivity, Polygon_i &poly) {
- //cerr << "follow " << start_x << " " << start_y << " | dir=" << dir_str[initial_direction] << " nbd=" << nbd << " conn=" << connectivity << endl;
+ if (debug) {
+ cerr << "follow " << start_x << " " << start_y << " | dir=" << dir_str[initial_direction] << " nbd=" << nbd << " conn=" << connectivity << endl;
+ }
+
int dir_inc = (connectivity == 4) ? 2 : 1;
int probe_x, probe_y;
@@ -73,10 +78,11 @@ static void follow(gerbolyze::nopencv::Image32 &img, int start_x, int start_y, D
if (!found) { /* No nonzero pixels found. This is a single-pixel contour */
img.at(start_x, start_y) = nbd;
- poly.emplace_back(i2p{start_x, start_y});
- poly.emplace_back(i2p{start_x+1, start_y});
- poly.emplace_back(i2p{start_x+1, start_y+1});
+ /* We must return these vertices counter-clockwise! */
poly.emplace_back(i2p{start_x, start_y+1});
+ poly.emplace_back(i2p{start_x+1, start_y+1});
+ poly.emplace_back(i2p{start_x+1, start_y});
+ poly.emplace_back(i2p{start_x, start_y});
return;
}
@@ -85,7 +91,10 @@ static void follow(gerbolyze::nopencv::Image32 &img, int start_x, int start_y, D
int current_direction = k % 8;
int start_direction = current_direction;
int center_x = start_x, center_y = start_y;
- //cerr << " init: " << center_x << " " << center_y << " / " << dir_str[current_direction] << endl;
+
+ if (debug) {
+ cerr << " init: " << center_x << " " << center_y << " / " << dir_str[current_direction] << endl;
+ }
do {
bool flag = false;
@@ -122,7 +131,9 @@ static void follow(gerbolyze::nopencv::Image32 &img, int start_x, int start_y, D
center_y = probe_y;
current_direction = flip_direction[k % 8];
- //cerr << " " << center_x << " " << center_y << " / " << dir_str[current_direction] << " -> " << set_val << endl;
+ if (debug) {
+ cerr << " " << center_x << " " << center_y << " / " << dir_str[current_direction] << " -> " << set_val << endl;
+ }
} while (center_x != start_x || center_y != start_y || current_direction != start_direction);
}
@@ -250,7 +261,6 @@ double k_cos(const Polygon_i &poly, size_t i, size_t k) {
return dp / (sqrt(sq_a)*sqrt(sq_b));
}
-constexpr bool debug = false;
ContourCallback gerbolyze::nopencv::simplify_contours_teh_chin(ContourCallback cb) {
return [&cb](Polygon_i &poly, ContourPolarity cpol) {
size_t sz = poly.size();
diff --git a/svg-flatten/src/nopencv_test.cpp b/svg-flatten/src/nopencv_test.cpp
index 77fbe30..5eedfe1 100644
--- a/svg-flatten/src/nopencv_test.cpp
+++ b/svg-flatten/src/nopencv_test.cpp
@@ -227,7 +227,7 @@ MU_TEST(test_round_trip_two_px) { testdata_roundtrip("testdata/two-p
MU_TEST(test_round_trip_two_px_inv) { testdata_roundtrip("testdata/two-px-inv.png"); }
static void test_polygon_area(const char *fn) {
- cerr << endl << "poly area test " << fn << endl;
+ //cerr << endl << "poly area test " << fn << endl;
Image32 ref_img;
mu_assert(ref_img.load(fn), "Input image failed to load");
ref_img.binarize();
@@ -245,23 +245,20 @@ static void test_polygon_area(const char *fn) {
}
double pos_sum = 0.0;
- double neg_sum = 0.0;
- gerbolyze::nopencv::find_blobs(ref_img, [fn, white_px_count, black_px_count, &pos_sum, &neg_sum](Polygon_i& poly, ContourPolarity pol) {
+ double neg_sum = black_px_count;
+ gerbolyze::nopencv::find_blobs(ref_img, [&pos_sum, &neg_sum](Polygon_i& poly, ContourPolarity pol) {
double area = polygon_area(poly);
- cerr << endl << fn << ": " << area << pos_sum << " / " << neg_sum << " -- " << white_px_count << " / " << black_px_count << endl;
+ //cerr << endl << fn << ": " << area << " " << pos_sum << " / " << neg_sum << " -- " << white_px_count << " / " << black_px_count << " GOT: " << poly.size() << " w/ " << pol << endl;
mu_assert(fabs(area) > 0.99, "Polygon smaller than a single pixel");
mu_assert((pol == CP_CONTOUR) == (area >= 0), "Polygon area has incorrect sign");
- if (area > 0) {
- pos_sum += area;
- } else {
- neg_sum -= area;
- }
+ pos_sum += area;
+ neg_sum -= area;
});
mu_assert(pos_sum - white_px_count < 0.01, "Calculated area outside tolerance");
mu_assert(neg_sum - black_px_count < 0.01, "Calculated area outside tolerance");
- cerr << endl << "poly area test " << fn << " done" << endl;
+ //cerr << endl << "poly area test " << fn << " done" << endl;
}
MU_TEST(test_polygon_area_blank) { test_polygon_area("testdata/blank.png"); }
@@ -346,7 +343,6 @@ MU_TEST(chain_approx_test_two_px_inv) { chain_approx_test("testdata/two-
MU_TEST_SUITE(nopencv_contours_suite) {
- /*
MU_RUN_TEST(test_complex_example_from_paper);
MU_RUN_TEST(test_round_trip_blank);
MU_RUN_TEST(test_round_trip_white);
@@ -379,7 +375,6 @@ MU_TEST_SUITE(nopencv_contours_suite) {
MU_RUN_TEST(chain_approx_test_two_blobs);
MU_RUN_TEST(chain_approx_test_two_px);
MU_RUN_TEST(chain_approx_test_two_px_inv);
- */
MU_RUN_TEST(test_polygon_area_blank);
MU_RUN_TEST(test_polygon_area_white);
MU_RUN_TEST(test_polygon_area_blob_border_w);