Wednesday, February 22, 2017

Going from 2D drawings to 3D printing

I wanted make a 3D-printable Valentine's day card for my wife, with an inflated red heart on a white background, so I spent perhaps too much time playing with algorithms to "inflate" a 2D drawing of a heart into a 3D polyhedron that I can load into OpenSCAD. Here's the card I ended up with.

After Valentine's, I still worked on refining the Python code that did the inflation. The code is now an easy to use Inkscape extension, which adds the ability to save to an inflated SCAD or STL file. The official repository is here.

The final algorithm I settled on is a non-linear scheme that sets the height of the inflation of a 2D image at a given point in the image x by approximating the Lp norm (E(Txp))1/p of the exit time Tx of a random walk started at x. For further adjustment, you can replace Tx with min(Tx,K) where K is exponentially distributed, which flattens the inflation in inner regions. The code could use a lot of optimization (using pypy instead of cpython improves runtimes by a factor of 10, but Inkscape only bundles python) as on my laptop the code takes about 45 seconds with default settings on one of my simple test images.

While my original Valentine's day card used p=1, I have since found that p=2 produces more nicely rounded output.

No comments: