arpruss / inflatemesh

Inflate 2D svg files to 3D STL or OpenSCAD files
Other
11 stars 2 forks source link

Brilliant project abandoned! #2

Open keto33 opened 3 years ago

keto33 commented 3 years ago

This is a fabulous piece of code. The approach to the entire problem is brilliant, and the code has been well written. It is a shame that such a powerful and useful project is abandoned, and no longer developed.

The TODO comments throughout the code can massively push the project forward.

arpruss commented 3 years ago

The code works well enough for me. I use it from time to time. The TODO comments are minor optimizations and not really needed.

keto33 commented 3 years ago

You're absolutely right! The code does its job, but I believe there are rooms for adding new (essential features). By the TODO comments, I was referring to your forward perspective.

There are two aspects, I hope to improve.

1. The speed:

2. The shape accuracy Although the mesh is standard and accurate, the visibility at the edge is much higher than on the (curved) surface. Increasing the resolution makes the process painfully slow.

In general, your algorithm beautifully works to create the curvature in the third dimension. However, the problem is the sharp edges, which cannot be smoothened as restricted by the speed.

arpruss commented 3 years ago

You definitely should run the code in pypy or some other JIT-compiling python system for speed. This is an order of magnitude improvement.

In the linear case (exponent=1), we're solving the heat equation, and it should be possible to find some off-the-shelf code to do it more efficiently. In the non-linear case, I haven't bothered to figure out what PDE, if any, we are solving. It might be possible to use some solver for it if we can figure it out. It's been decades since I've worked with PDEs, though.

One could make a mesh that is finer around the edges and coarser in the middle. It would have a lot of narrow triangles as the two meshes join. But I worry that this would throw off the math of the algorithm.

One optimization would be parallel processing. However, pypy doesn't seem to do that well: https://ao.gl/when-your-python-code-is-much-faster-with-pypy/

Here is an optimization that just occurred to me. First, run the computation on a coarse grid. Then use that to generate the initial values for the iteration on a finer grid. My intuition is that we would need very few iterations on the finer grid to smooth things out given the approximation.

What would also really help would be to rewrite the main code in C.

But is all the optimization worth the effort? As far as I know, you and I are the only users of the code, and I use it maybe twice a year, and so I suspect that the person-hour savings from having faster code could be negative once one takes into account the amount of time it takes to rewrite and test the code.

arpruss commented 3 years ago

There is also a much simpler technique which simply uses the distance to the edge for inflation. I do that here: https://www.thingiverse.com/thing:4416566

keto33 commented 3 years ago

This conversation is getting fruitful!

I confirm PyPy can speed up the inflating process by one order of magnitude. However, it has no impact on the step of fixing the outer faces, which is the bottleneck if increasing the resolution.

I think your algorithm is perfect, as it works perfectly to compute the inflated curvature. I think a practical solution is to offset both sides positively and negatively. Then, adding vertices for the ribbon added around the shape on the SVG paths. A slight curvature of this ribbon can merge into the curvatures on the SVG surface. Therefore, the sharp edges disappear.

In fact, my ultimate plan is to write the whole code in C, but I try to find the optimised and simple code to be translated into C.

It is critical to find bottlenecks. A while ago, I wrote an unusual script to create mesh from images. I gathered the pixel map by converting the image via ImageMagick, and processed the pixel coordinates by awk. Awkward, but fast. However, here, rasterising is not a critical step.

I believe, this code is very useful, but people are not aware of it and its potential. Blender, for example, has the option of importing SVG and using inflation; and it is indeed popular. It is much slower and less efficient than your code (except for the physics engine adding physical reality to it). I myself was searching for an efficient (fast and accurate) to transform 2D paths to 3D mesh, and just came across your code, accidentally.

Your the other code looks very promising, but it takes a while for me to delve into it, and I have never used OpenSCAD. I use POV-RAY (generally, I prefer to stay in the terminal all the time). The idea of the distance to the edge is brilliant. This is exactly what I tried to compute efficiently for months (occasionally, of course).

Do you have any similar library? They can be inspiring!