Repository navigation
fix(core)!: prevent holes during SVG generation #429
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 28 commits
72a6a7a
aac9407
7e5ac60
37309f0
b37807e
761fd24
7cbded3
51348fc
b4ee348
9efab7c
02cc692
0e973cb
8e35991
680922e
6da4d6f
7aa21d7
b3dc95c
0017754
b728615
8160c24
1cb1f87
b9864ce
c2bc23b
df07f98
b3e33ef
d6f29a1
9360efa
b3d016e
7dcf8aa
b2a8c27
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ static img2num::ImageToSvgConfig to_cpp(const img2num_ImageToSvgConfig& c) { | |
| }, | ||
|
|
||
| .min_cluster_area = c.min_cluster_area, | ||
| .min_thickness = c.min_thickness, | ||
| .color_space = c.color_space | ||
| }; | ||
| // clang-format on | ||
|
|
@@ -38,6 +39,7 @@ static img2num_ImageToSvgConfig to_c(const img2num::ImageToSvgConfig& cpp) { | |
| .max_iter = cpp.kmeans.max_iter | ||
| }, | ||
| .min_cluster_area = cpp.min_cluster_area, | ||
| .min_thickness = cpp.min_thickness, | ||
| .color_space = cpp.color_space | ||
| }; | ||
| // clang-format on | ||
|
|
@@ -90,22 +92,19 @@ void img2num_bilateral_filter( | |
| ); | ||
| } | ||
|
|
||
| char* img2num_labels_to_svg( | ||
| const uint8_t* data, const int32_t* labels, const int width, const int height, | ||
| const int min_area | ||
| ) { | ||
| char* result {nullptr}; | ||
| char *img2num_labels_to_svg(const uint8_t *data, const int32_t *labels, const int width, | ||
| const int height, const int min_area, const int min_thickness) { | ||
| char *result{nullptr}; | ||
| img2num::clear_last_error_and_catch( | ||
| [&](const uint8_t* d, const int32_t* l, const int w, const int h, const int min_a) { | ||
| std::string svg {img2num::labels_to_svg(d, l, w, h, min_a)}; | ||
| result = static_cast<char*>(std::malloc(svg.size() + 1)); | ||
| [&](const uint8_t *d, const int32_t *l, const int w, const int h, const int min_a, const int min_t) { | ||
| std::string svg{img2num::labels_to_svg(d, l, w, h, min_a, min_t)}; | ||
| result = static_cast<char *>(std::malloc(svg.size() + 1)); | ||
| if (!result) { | ||
| return; // Allocation failed | ||
| } | ||
| std::memcpy(result, svg.c_str(), svg.size() + 1); | ||
| }, | ||
| data, labels, width, height, min_area | ||
| ); | ||
| data, labels, width, height, min_area, min_thickness); | ||
| return result; | ||
| } | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What are your thoughts on us removing this function entirely in the future (not now) to make it easier to maintain the library: we only expose one interface for converting to SVGs.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd still keep it... it's not difficult to maintain |
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| #ifndef DOUGLAS_PEUCKER_H | ||
| #define DOUGLAS_PEUCKER_H | ||
|
|
||
| #include <cstdint> | ||
| #include <vector> | ||
|
|
||
| #include "internal/contours.h" | ||
|
|
||
| /** | ||
| * `@brief` Douglas-Peucker contour point reduction with junction locking and retraction bounds. | ||
| * | ||
| * Reduces each contour's point count while preserving junctions and preventing gaps. | ||
| * Fixed junctions marked by `fixed[i][k] != 0` are kept as exact shared endpoints. | ||
| * Simplification is bounded to never retract a boundary inward past the inter-region | ||
| * overlap margin, preventing gaps between neighboring regions. Kept points are emitted | ||
| * as straight-line quadratic Bezier segments for compatibility with existing SVG output. | ||
| * | ||
| * `@param` chains Input polyline chains | ||
| * `@param` fixed Junction mask: fixed[i][k] != 0 marks point k of chain i as a junction to preserve | ||
| * `@param` results Output straight-line QuadBezier segments for each chain | ||
| * `@param` eps Overall deviation tolerance in pixels (overridable via IMG2NUM_DP_EPS env var) | ||
| * | ||
| * `@note` retract_eps (max inward boundary move) defaults to min(eps, 0.5px) and can be | ||
| * overridden via IMG2NUM_DP_RETRACT environment variable. | ||
| */ | ||
| void dp_curve_reduction(const std::vector<std::vector<Point>> &chains, | ||
|
Krasner marked this conversation as resolved.
Resolved
|
||
| const std::vector<std::vector<uint8_t>> &fixed, | ||
| std::vector<std::vector<QuadBezier>> &results, float eps); | ||
|
|
||
| #endif | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,11 +39,37 @@ class Graph { | |
| void hash_node_ids(void); | ||
| void process_overlapping_edges(); | ||
|
|
||
| public: | ||
| inline Graph(std::unique_ptr<std::vector<Node_ptr>>& nodes, int width, int height) | ||
| : m_nodes(std::move(nodes)) | ||
| , m_width(width) | ||
| , m_height(height) { | ||
| /** | ||
| * `@brief` Safely retrieves a pixel value from a binary image with bounds checking. | ||
| * | ||
| * `@param` img Binary image buffer | ||
| * `@param` w Image width | ||
| * `@param` h Image height | ||
| * `@param` x Pixel x-coordinate | ||
| * `@param` y Pixel y-coordinate | ||
| * `@return` Pixel value at (x, y), or 0 if out of bounds | ||
| */ | ||
| inline uint8_t getPixel(const std::vector<uint8_t>& img, int w, int h, int x, int y) { | ||
| if (x < 0 || x >= w || y < 0 || y >= h) return 0; // Boundary check | ||
| return img[y * w + x]; | ||
| } | ||
|
Comment on lines
+42
to
+56
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should probably schedule a refactor to make the library use the image class wherever possible because we repeat this pattern a lot. |
||
|
|
||
| /** | ||
| * `@brief` Analyzes a skeleton image to detect junction points using 8-neighbor crossing-number. | ||
| * | ||
| * Scans the skeleton image and marks pixels as junctions where three or more branches meet, | ||
| * using the crossing-number method (counts 0→1 transitions in the 8-neighbor ring). | ||
| * | ||
| * `@param` skel Binary skeleton image (nonzero = skeleton pixel) | ||
| * `@param` w Image width | ||
| * `@param` h Image height | ||
| * `@return` Junction mask with nonzero entries marking junction pixels | ||
| */ | ||
| std::vector<uint8_t> analyzeJunctions(const std::vector<uint8_t>& skel, int w, int h); | ||
|
Ryan-Millard marked this conversation as resolved.
Resolved
|
||
|
|
||
| public: | ||
| inline Graph(std::unique_ptr<std::vector<Node_ptr>> &nodes, int width, int height) | ||
| : m_nodes(std::move(nodes)), m_width(width), m_height(height) { | ||
| hash_node_ids(); | ||
| } | ||
|
|
||
|
|
@@ -68,10 +94,9 @@ class Graph { | |
| return m_nodes->size(); | ||
| } | ||
|
|
||
| void discover_edges( | ||
| const std::vector<int32_t>& region_labels, const int32_t width, const int32_t height | ||
| ); | ||
| void merge_small_area_nodes(const int32_t min_area); | ||
| void discover_edges(const std::vector<int32_t> ®ion_labels, const int32_t width, | ||
| const int32_t height); | ||
| void merge_small_area_nodes(const int32_t min_area, const int32_t min_thickness = 0); | ||
| void compute_contours(); | ||
| }; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| #ifndef SHARED_CONTOURS_H | ||
| #define SHARED_CONTOURS_H | ||
|
|
||
| #include <cstdint> | ||
| #include <unordered_map> | ||
| #include <vector> | ||
|
|
||
| #include "internal/Point.h" | ||
| #include "internal/contours.h" // QuadBezier | ||
|
|
||
| /** | ||
| * `@brief` Build crack-grid shared boundary loops for each region. | ||
| * | ||
| * Builds region boundaries on the pixel-corner ("crack") grid rather than on | ||
| * pixel centres. Shared edges are extracted once, simplified once, and reused | ||
| * by both adjacent regions so neighbouring loops stay exactly coincident. | ||
| * | ||
| * `@param` labels Per-pixel region ids in row-major order (`w * h` entries). | ||
| * `@param` w Image width in pixels. | ||
| * `@param` h Image height in pixels. | ||
| * `@param` eps Curve-fit tolerance applied to each canonical edge. | ||
| * `@return` Per-region closed boundary loops in corner coordinates. | ||
| */ | ||
| std::unordered_map<int32_t, std::vector<std::vector<QuadBezier>>> build_shared_loops( | ||
| const std::vector<int32_t> &labels, int w, int h, float eps); | ||
|
|
||
| #endif |
Uh oh!
There was an error while loading. Please reload this page.