deadsy / sdfx

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

Gyroid shape #57

Closed Megidd closed 1 year ago

Megidd commented 1 year ago

Gyroid cube

There is an example code to create Gyroid cubes:

https://github.com/deadsy/sdfx/blob/c456ed660b0c0c654daf98d618fa455aec51724a/examples/gyroid/main.go#L79

I tried the example but with smaller marching cube input of 50:

render.ToSTL(s0, "gyroid_cube.stl", render.NewMarchingCubesUniform(50))

The result is a gyroid with the shape of a cube;

Screen Shot 2022-12-15 at 13 00 17

Gyroid in any shape?

Now, I have a question. How to create Gyroid in any shape? For example, assume I have a surface mesh represented by an STL file. Assume the shape of the surface mesh is a teapot:

Screen Shot 2022-12-15 at 13 11 02

I can import and covert the surface mesh into SDF by:

    inputPth := "input.stl"

    // read the stl file.
    file, err := os.OpenFile(inputPth, os.O_RDONLY, 0400)

    // create the SDF from the mesh
    imported, err := obj.ImportSTL(file, 20, 3, 5)

    // TODO: How to create a gyroid with the imported shape?

I cannot figure out how to create a gyroid that has the imported shape. I mean, the final gyroid must have the shape of the teapot. How can I do that?

deadsy commented 1 year ago

A gyroid is defined over all of 3d space, and in the example we turn it into a finite object by intersecting with a cube. Likewise you can intersect the teapot with the gyroid and get a teapot shaped gyroid region.