Concave hulls aka poly-from-sprite

Monkey Archive Forums/Monkey Projects/Concave hulls aka poly-from-sprite

Raph(Posted 2014) [#1]


http://www.raphkoster.com/monkey/concavehull/MonkeyGame.html

Still WIP.

- Hit SPACE to cycle through the image test cases.
- tap the left and right arrows to adjust epsilon, aka the level-of-detail of the generated polygon.

Algorithm:
- Convert the image to a pixel data array
- add a border around the entire image
- flood fill everything under 0.5 alpha
- create an outline from the border of the floodfill
- identify all corners in the outline
- build a list of edges from the corners
- build a polygon by welding all the edges
- run Douglas-Peucker on the resulting poly

Edit: here is the code.



You need a vector class. I used this one: http://www.monkey-x.com/Community/posts.php?topic=568

This looks like I started to change it slightly from the web demo... but it should be a decent starting point. That said, I never finished it -- it uses color flood fill on the image.


dragon(Posted 2014) [#2]
wonderful!


Raph(Posted 2014) [#3]
Thanks. I still need to do the floodfill/edgefinding via a method other than using color to mark pixels -- currently it breaks on images with (255,0,255) or (255, 255, 0) on the edges. Small fix though! The hard parts were actually the edge detection/poly welding given extreme concave angles...


CopperCircle(Posted 2014) [#4]
Looks good, I did something similar awhile back, does it deal with islands? (parts of an image that are not connected to the main image)


Raph(Posted 2014) [#5]
Right now the welding algo intentionally throws away the excess edges, but it would be easy to simply start a new poly when it hits that case. But the resulting array won't cleanly plug into other uses, of course.


Supertino(Posted 2014) [#6]
Now this is handy, I got caught out a while back with poly in Monkey when shapes ( X ) on some targets did look right.


Raph(Posted 2015) [#7]
Source code for this project is now in the first post. It does seem to work but I haven't touched it in months.