Skip to content

soypat/gsdf

Repository files navigation

gsdf

gsdf is a CAD 3D design library for Go that uses SDFs for shape definition. Rendering can be done on GPU or CPU for visualization or 3D printing file outputs.

All images and shapes in readme were generated using this library.

circle bolt-example

Features

  • Heapless algorithms for everything. No usage of GC in happy path.
  • Generate visualization for your parts as shaders.
  • Heapless Octree triangle renderer. Is stupid fast.
  • GPU and CPU implementations for all shapes and operations. CPU implementations are actually faster for simple parts.
    • Design your part using one API, switch between CPU and GPU after design.
  • Extremely coherent API design.
  • TinyGo supported for CPU evaluation :)

Package layout/structure

  • gsdf: Top level package defines exact SDFs primitives and operations for use on CPU or GPU workloads. Consumes glbuild interfaces and logic to build shaders.
  • glbuild: Automatic shader generation interfaces and logic.
  • gleval: SDF evaluation interfaces and facilities, both CPU and GPU bound.
  • glrender: Triangle rendering logic which consumes gleval. STL generation.
  • forge: Composed shape generation such as threads package for generating screw threads. Engineering applications.
  • gsdfaux: High level helper functions to get users started up with gsdf. See examples.

Part design - NPT Flange example.

This was converted from the original example. See README for images.

See working example under examples directory. Run on GPU with -gpu flag: go run ./examples/npt-flange -gpu

Output and timings for

  • CPU: 12th Gen Intel i5-12400F (12) @ 4.400GHz
  • GPU: AMD ATI Radeon RX 6800
$ time go run ./examples/npt-flange/ -gpu
enabled GPU usage
SDF created in  5.253108ms evaluated sdf 13808829 times, rendered 219992 triangles in 847.606426ms wrote file in 29.16306ms

real    0m1,307s
user    0m0,753s
sys     0m0,284s

$ time go run ./examples/npt-flange/ 
SDF created in  229.895µs evaluated sdf 13808405 times, rendered 220064 triangles in 2.411541291s wrote file in 28.265793ms

real    0m2,744s
user    0m2,826s
sys     0m0,280s

The result of running the example are two files:

  • nptflange.glsl: Visualization shader that can be copy pasted into shadertoy to visualize the part, or rendered within your editor with an extension such as the Shader Toy Visual Studio Code extension.
  • nptflange.stl: Triangle model file used in 3D printing software such as Cura. Can be visualized online in sites such as View STL.

Below is the 3D scene code. Omits rendering pipeline.

	const (
		tlen             = 18. / 25.4
		internalDiameter = 1.5 / 2.
		flangeH          = 7. / 25.4
		flangeD          = 60. / 25.4
	)
	var (
		npt    threads.NPT
		flange glbuild.Shader3D
		err    error
	)
	err = npt.SetFromNominal(1.0 / 2.0)
	if err != nil {
		return nil, err
	}

	pipe, _ := threads.Nut(threads.NutParms{
		Thread: npt,
		Style:  threads.NutCircular,
	})

	// Base plate which goes bolted to joint.
	flange, _ = gsdf.NewCylinder(flangeD/2, flangeH, flangeH/8)

	// Join threaded section with flange.
	flange = gsdf.Translate(flange, 0, 0, -tlen/2)
	union := gsdf.SmoothUnion(pipe, flange, 0.2)

	// Make through-hole in flange bottom. Holes usually done at the end
	// to avoid smoothing effects covering up desired negative space.
	hole, _ := gsdf.NewCylinder(internalDiameter/2, 4*flangeH, 0)
	union = gsdf.Difference(union, hole)
	// Convert from imperial inches units to millimeter:
	union = gsdf.Scale(union, 25.4)

	stl, _ := os.Create("for3dprinting.stl")
	err = gsdfaux.Render(union, gsdfaux.RenderConfig{
		STLOutput:    stl,
		Resolution:   union.Bounds().Diagonal() / 200, 
		UseGPU:       true,
	})

npt-flange-example

More examples

fibonacci-showerhead iso-screw array-triangles

About

3D/2D CAD design package written in Go.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Languages