deadsy / sdfx

A simple CAD package using signed distance functions
MIT License
518 stars 52 forks source link

Better STL Generation #6

Open deadsy opened 6 years ago

deadsy commented 6 years ago

Problem: The marching cubes algorithm gives STLs that are large in size and have aliasing defects on sharp edges.

Solution: Have a mesh generation algorithm that is adaptive to the curvature of the object. ie- big triangles on flat surfaces, small triangles for sharper curves. A quality metric could allow trade-offs between file size and mesh accuracy.

deadsy commented 6 years ago

I've been reading this book.

ghost commented 6 years ago

Like a LOD algo ? Level of detail.

It's been used in games for years based on distance. Are you proposing to do it based on curve radius per object ?

It would be very useful to allow the LOD to be driven changed at runtime based off distance too as it would make a 3d viewer very efficient

deadsy commented 6 years ago

Not so much LoD. More like minimizing the number of triangles needed to represent the local detail of the object. Marching cubes samples the bounding box and generates triangles whose size matches the sampling resolution. As a result even planar surfaces end up with lots of little triangles. Those surfaces would be more efficiently represented with a few large triangles. The CGAL project has code that can deal with kind of thing. E.g. https://doc.cgal.org/latest/Surface_mesher/index.html#Chapter_3D_Surface_Mesh_Generation

ghost commented 5 years ago

I have been again playing with this to use with a Milling machine.

Regarding the mesh generation and sharp edges, has there been any movements or thought on this. The reason i ask is not just because of the tessellation problem on sharp edges. The other reason is because i want to build in the ability to manually push and pull the mesh in a GUI. This is basically where you want to do manual adjustments for more organic looking shapes.

deadsy commented 5 years ago

No code written - thinking about it on and off. It's a well recognized problem when it comes to representing objects with meshes. There are solutions out there that just remove triangles selectively from a given mesh. The difference here is that the triangle tuning can be done with on-going knowledge of the SDF, thereby leading to a more accurate mesh. BTW - when it comes to 3d printing the STL file I haven't had any problem with the corner aliasing - it's too small to make a difference- so it's mostly an annoyance in the mesh viewer. I'm sure the slicer would work faster if it wasn't dealing with so many triangles, but it hasn't been an insurmountable problem, and the output gcode will be roughly the same irrespective of the input mesh size. I assume the milling machine experience would be about the same.

deadsy commented 5 years ago

do manual adjustments for more organic looking shapes

That might be easier if you have lot's of triangles on a surface to start off with - which is what you have here.

ghost commented 5 years ago

ok thanks for the advice.

I will look around for examples in golang and other for how to push and pull on 3d stl meshes. Gotta be some prior art. Your code can probably also be used and just add more to move the local vertexes around based on the "brush size" being used.

On Sat, 25 Aug 2018 at 18:37 Jason Harris notifications@github.com wrote:

do manual adjustments for more organic looking shapes

That might be easier if you have lot's of triangles on a surface to start off with - which is what you have here.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/deadsy/sdfx/issues/6#issuecomment-415981316, or mute the thread https://github.com/notifications/unsubscribe-auth/ATuCwotcEkBEuguulBakZEUpJkAFPyroks5uUX1DgaJpZM4SSAUZ .

ghost commented 5 years ago

found this ! https://github.com/g3n/engine

All golang 3D gui viewer. I think i can use this as a canvas and load up the STL's into it to do the pushing and pulling.

Its nice to see this type of 3D stuff in the golang world.

danieltwagner commented 3 years ago

https://github.com/danieltwagner/sdfx/tree/simplify has a WIP mesh simplification algorithm. The idea is to not lose any details, i.e. only reduce coplanar triangles. It's mostly working but I must be generating a few triangles with bogus normals (maybe zero-size triangles?). I'll try to sort out the issue and tidy it all up and then put it up as a PR but would appreciate another pair of eyes.

I attach an example below. You can see that a few wireframe lines are visible in the wrong orientation and as a result the interior cutout doesn't appear smooth.

Before (160160 triangles):

Screen Shot 2020-10-17 at 11 36 45 PM Screen Shot 2020-10-17 at 11 36 40 PM

After (2648 triangles):

Screen Shot 2020-10-17 at 11 37 27 PM Screen Shot 2020-10-17 at 11 37 31 PM
deadsy commented 3 years ago

I haven't had a chance to review your code yet...

I saw this and thought it was of interest:

https://www.reddit.com/r/computervision/comments/jq98ow/shape_aware_mesh_simplification_after_scanning/?utm_source=share&utm_medium=web2x&context=3

Based on: https://www.cs.cmu.edu/~garland/thesis/thesis.html

Also: There are (at least) two approaches to this:

1) Use marching cubes to build a fine triangle mesh and then simplify the mesh. 2) Use something other than marching cubes to perform adaptive sampling that does a better job of the features with high spatial frequencies. ie get rid of the aliasing that is present on corners/edges.

Approach 2 is possible, we have an SDF model that can be sampled wherever we like, it's just a bit beyond my sophisication at the moment. :-)