mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-04-06 00:32:05 +02:00
Optimize and simplify MarchingSquares.hpp. #2180
Closed
opened 2026-04-06 01:11:49 +02:00 by MrUnknownDE
·
0 comments
No Branch/Tag Specified
main
release/v2.3
dependabot/github_actions/geekyeggo/delete-artifact-6
dependabot/github_actions/microsoft/setup-msbuild-3
feature/fix-build-error-for-OrcaSlicer_profile_validator
feature/fix-regression-bug-of-extruder-clearance
feature/auto-update
release/v2.3.1
release/v1.9
release/v1.8
nightly-builds
v2.3.2
v2.3.2-rc2
v2.3.2-rc
v2.3.2-beta2
v2.3.2-beta
v2.3.1
v2.3.1-beta
v2.3.1-alpha
v2.3.0
v2.3.0-rc
v2.3.0-beta2
v2.3.0-beta
v2.2.0
v2.2.0-rc
v2.2.0-beta2
v2.2.0-beta
v2.1.1
v2.1.0
v2.1.0-rc
v2.1.0-beta
v2.0.0
v2.0.0-rc
v2.0.0-beta
v1.9.1
v1.9.0
v1.9.0-beta
v1.9.0-alpha
v1.8.1
v1.8.0
v1.8.0-rc2
v1.8.0-rc
v1.8.0-beta2
v1.8.0-beta
v1.7.0
v1.7.0-beta
v1.6.6
v1.6.5
v1.6.4
v1.6.4-beta3
v1.6.4-beta2
v1.6.4-beta
v1.6.3
v1.6.3-beta
v1.6.2
v1.6.2-beta
v1.6.1
v1.6.0
v1.5.0
v1.4.5
v1.4.4
v1.4.3
v1.4.2
v1.4.1
v1.4.0
v1.4.0_beta3
v1.4.0_beta2
v1.4.0_beta1
v1.3.4
v1.3.3
v1.3.3-sf-beta3
v1.3.3-sf-beta
v1.3.2-sf
v1.3.1-sf
v1.3.0-sf
v1.2.5.3-sf
v1.2.5-sf
v1.2.4-sf
v1.2-sf
v1.1
v1.1.1.1-sf
v1.1.0.14-sf
v1.0.10-sf2.1
v1.0.10-sf2
V1.0.10-sf
v1.0.10
v1.0.10.05
Labels
Clear labels
2.3.2-beta2
2.3.2-beta2
2.3.2-beta2
2.3.2-beta2
2.3.2-beta2
2.3.2-beta2
2.3.2-beta2
2.3.2-beta2
2.3.2-beta2
2.3.2-beta2
2.3.2-beta2
2.3.2-beta2
2.3.2-beta2
2.3.2-beta2
2.3.2-hotfix
2.3.2-hotfix
2.3.2-hotfix
2.3.2-hotfix
2.3.2-hotfix
2.4.0
Community testers wanted
Community testers wanted
Community testers wanted
Community testers wanted
Community testers wanted
Community testers wanted
Community testers wanted
Community testers wanted
Community testers wanted
Community testers wanted
Community testers wanted
Community testers wanted
Community testers wanted
Community testers wanted
Community testers wanted
Community testers wanted
Community testers wanted
Community testers wanted
Community testers wanted
Community testers wanted
Community testers wanted
Community testers wanted
Community testers wanted
Community testers wanted
Community testers wanted
Community testers wanted
Community testers wanted
Community testers wanted
Community testers wanted
Community testers wanted
Community testers wanted
Community testers wanted
Community testers wanted
Crazy Orca Community
Crazy Orca Community
Crazy Orca Community
Linux
Linux
Localization
Localization
Localization
Localization
Localization
Localization
Localization
Localization
Localization
Localization
Localization
Localization
Localization
Localization
Localization
Localization
Need more information
Need more information
Need more information
QoL
QoL
QoL
QoL
QoL
QoL
QoL
QoL
QoL
QoL
QoL
QoL
QoL
QoL
QoL
QoL
QoL
UI/UX
UI/UX
UI/UX
UI/UX
UI/UX
UI/UX
UI/UX
UI/UX
UI/UX
UI/UX
UI/UX
Wait for CI/CD test result
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
bug-fix
dependencies
dependencies
dependencies
dependencies
dependencies
dependencies
dependencies
dependencies
dependencies
dependencies
dependencies
dependencies
dependencies
dependencies
documentation
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
duplicate
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
enhancement
github_actions
github_actions
github_actions
github_actions
github_actions
github_actions
github_actions
github_actions
github_actions
github_actions
github_actions
github_actions
profile
profile
profile
profile
profile
profile
profile
profile
profile
profile
profile
profile
profile
profile
profile
profile
profile
profile
profile
profile
profile
profile
profile
profile
profile
profile
profile
profile
profile
profile
profile
profile
profile
profile
profile
profile
profile
profile
profile
profile
profile
regression
regression
wait for response
wait for response
wait for response
wait for response
wait for response
wait for response
wait for response
wait for response
wait for response
wait for response
wait for response
wait for response
wait for response
wontfix
📍 Assigned
📍 Assigned
📍 Assigned
📍 Assigned
📍 Assigned
📍 Assigned
📍 Assigned
📍 Assigned
📍 Assigned
📍 Assigned
📍 Assigned
📍 Assigned
📍 Assigned
📍 Assigned
📍 Assigned
📍 Assigned
📍 Assigned
🔔 reminder-sent
🔔 reminder-sent
🔔 reminder-sent
🔔 reminder-sent
No Label
Milestone
No items
No Milestone
Projects
Clear projects
No project
Assignees
MrUnknownDE
Clear assignees
No Assignees
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: github/OrcaSlicer#2180
Reference in New Issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @dbaarda on 9/15/2025
Optimize and Simplify MarchingSquares.hpp
This slightly improves the programming interface, optimizes MarchingSquares.hpp pretty significantly, and improves the output quality. It also fixes and improves the tests.
The programming interface improvement was removing the default 1 pixel "overlap" that caused some strange scaling artefacts and confused the meaning of the "window" argument. The old code required you ask for a minimum 2x2 window, which minus the overlap meant it actually used a 1x1 window. Now the window you ask for is the window it uses, and you can use a 1x1 window.
The optimizations include reducing memory requirements, improving memory locality for caching, and significantly improving the algorithm performance. The removal of the "overlap" enabled some of the performance improvements, because it means that cell corner tags can be shared between all 4 adjacent cells because they are not slightly offset by the overlap. The algorithm has been generally improved to take advantage of sharing the corner tags, packing them tightly into bitmapped blocks, and can skip over multiple "empty" cells at a time when scanning through tags to get cell exit directions, and when scanning through cell exit directions to find the start of a new ring.
Removing the "overlap" also removed some small scaling artifacts, and SLA/RasterToPolygons.cpp included some compensation scaling (with limited success) that can now be removed. Additionally the edge interpolation includes tweaks to improve the results for the bottom and right edges of extracted polygons. This has resulted in improving the output quality, and the extracted ExPolygons more closely track the raster inputs.
A more detailed breakdown by file;
tests/libslic3r/alltests compile.RasterBasenamespace fromsla::RasterBase::PixelDimandsla::RasterBase::Resolution.test_expolys()function for more consistent tests and concise definitions.test_expolys()function to be more comprehensive and optionally more tolerant when extracted rings that touch get merged together byunion_ex()insidesrc/libslic3r/SLA/RasterToPolygons.cpp.clang-format.m_tagsvector containing auint8_tfor each cell with that cell's tags and direction flags into anm_tagsvector ofuint32_tcontaining tags for each cell corner packed in row/column order, and anm_dirsvector ofuint32_tcontaining 8 packed cells 4 direction flags. The direction flags indicate the direction (left/down/right/up) a yet-to-be-processed line that passing through will exit the cell.isoval()once per corner instead of 4x per cell. Next it scans through all the tags to set the directions inm_dirs. Then it scans through them_dirsto build the rings, using the directions flags to get the edges the rings cross into the next cell and clearing them as it goes. Finally it interpolates the edges in the rings into points on the ring. Both the scan of tags to set directions flags, and the scan of directions flags to find rings, process the packed data in blocks and can skip over multiple "empty" blocks of tags/cells at a time.clang-format.Note when looking at diffs there were a lot of trailing whitespace that got stripped by my editor, so use
git diff -wto see it more clearly. You can also turn on "ignore whitespace" in the github "changed files" view under the gear icon.Screenshots/Recordings/Graphs
It was a bit hard to get comparative data because the old tests were so broken, so I've managed to do tests using the new tests and the old implementation for comparison. This required some minor tweaks to the tests to use the old interface window arguments equivalent to the new window arguments.
The images compare the reference polygons (thick red lines), the raster they generated (green areas), and the generated output (thin blue lines);
Old implementation, Old RasterToPolygons, New Test.
Note that these show that the extracted polygons are a bit offset. This is very visible in the smaller tests, and less obvious for larger images. I believe this is an artefact caused by RasterToPolygons attempts to compensate for the MarchingSquares output being a bit under-sized.
Old Implementation, New RasterToPolygons, New Test.
With the RasterToPolygons scaling compensation removed the extracted polygons are no longer shifted, but they are noticeably smaller. Note that the undersizing appears to be entirely because it doesn't compensate for the top and right side interpolation going one pixel too far.
New Implementation, New RasterToPolygons, New Test.
Note the extracted polygons are significantly better at matching the raster image. There is a little bit of "corner cutting" on some corners, mainly bottom right, which are an almost inevitable artefact of the way marching squares works. One of the problems I encountered was for the "ambiguous case" tests the extracted polygons are so close they touch, and
ex_union()when converting these to ExPolygons joined them together, but the old test was looking for multiple polygons.Also note that using very small windows exactly matching raster images is not the only use-case. Many other use-cases would use large windows, in which case the corner cutting problem goes away because the lines transition through the middle of edges, and nearly never through cell corners.
Speed comparisons;
The best speed comparison I've got is the SL1Import test in
test_marchingsquares.cpp, which seems to load a frog mesh model, slice it, output the slices to rasters, convert the rasters to polygons and then use the polygons to regenerate a mesh. This test is completely unchanged in the old and new versions. This is running on my intel laptop;Old implementation;
New implementation;
So it went from 21.6 seconds down to 7.9 seconds... nearly 3x as fast. And a fair chunk of this test is not even MarchingSquares, but all the loading/slicing/rastering/etc stuff, so the marching squares part must have an even higher relative speed improvement.
Tests
I've been running the exiting unittests. It took me a bit of effort to figure out how, but FTR my general setup started with following the instructions at https://github.com/SoftFever/OrcaSlicer/wiki/How-to-build, but despite the
-targument claiming to build tests it didn't seem to so I had to find the relevant cmake target to build it myself. Also important is setting up ccache and using-pNotes
The reason I tackled this is I'm planning to migrate FillTpmsFK.cpp to use it (I'm pretty sure it will be faster than the marching squares they have), and turn it into a generic surface equation fill pattern generator, and then use it to implement variable density fill patterns like gyroid,but where the "period" density is a function of how close to an external surface you are. This will make the
isoval()surface equation function calls by marching-squares much more expensive, so optimising this from calling it 4x per cell to 1x per cell when building the tags should be a nearly 4x win. Note that the tests/benches etc just doing raster lookups barely show this benefit because theirisoval()is so cheap, but there was a bunch of other low-hanging fruit to optimize so I got a bunch of other wins.I'm also not really a C++ coder, so I've probably got some stuff that'll make C++ guys cringe. I'm more old-school C and quick-n-dirty Python these days.