Skip to content

Commit

Permalink
README update for more readability
Browse files Browse the repository at this point in the history
  • Loading branch information
soypat committed Nov 16, 2024
1 parent 9b2a9f7 commit 37890cc
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 107 deletions.
101 changes: 19 additions & 82 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,34 @@
[![sourcegraph](https://sourcegraph.com/github.com/soypat/gsdf/-/badge.svg)](https://sourcegraph.com/github.com/soypat/gsdf?badge)

`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.
for visualization or 3D printing file outputs. Quick jump to usage: [bolt example](./examples/bolt/main.go).

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

![circle](https://github.com/user-attachments/assets/91c99f47-0c52-4cb1-83e7-452b03b69dff)
![bolt-example](https://github.com/user-attachments/assets/8da50871-2415-423f-beb3-0d78ad67c79e)

## Requirements
- [Go](https://go.dev/)
- **Optional**: See latest requirements on [go-glfw](https://github.com/go-gl/glfw) if using GPU

## Features

- Extremely coherent API design.

- UI for visualizing parts, rendered directly from shaders. See [UI example](./examples/ui-mandala) by running `go run ./examples/ui-mandala`

- Generate visualization for your parts as shaders.

- GPU and CPU implementations for all shapes and operations. CPU implementations are actually faster for simple parts.

- Include arbitrary buffers into GPU calculation. See [`Shader` interface](./glbuild/glbuild.go).

- 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.
- 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
Expand All @@ -43,15 +46,22 @@ All images and shapes in readme were generated using this library.
- `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](./examples).

## Part design - NPT Flange example - 9× GPU speedup
This was converted from the [original sdf library example](https://github.com/soypat/sdf/blob/main/examples/npt-flange/flange.go).

See working example under [examples](./examples/) directory. Run on GPU with `-gpu` flag: `go run ./examples/npt-flange -gpu`
# Examples
Find examples under [examples](./examples/) directory. Run on GPU with: `-gpu` flag.

Most 3D examples output two files:
- `example-name.glsl`: Visualization shader that can be copy pasted into [shadertoy](https://www.shadertoy.com/new) to visualize the part, or rendered within your editor with an extension such as the [Shader Toy Visual Studio Code extension](https://marketplace.visualstudio.com/items?itemName=stevensona.shader-toy).
- `example-name.stl`: Triangle model file used in 3D printing software such as [Cura](https://ultimaker.com/software/ultimaker-cura/). Can be visualized online in sites such as [View STL](https://www.viewstl.com/).


Output and timings for
- CPU: 12th Gen Intel i5-12400F (12) @ 4.400GHz
- GPU: AMD ATI Radeon RX 6800

## npt-flange - 9× GPU speedup
This was converted from the [original sdf library example](https://github.com/soypat/sdf/blob/main/examples/npt-flange/flange.go).

#### GPU rendering in 1 second. 0.4M triangles
```sh
time go run ./examples/npt-flange -resdiv 400 -gpu
Expand All @@ -77,83 +87,10 @@ finished npt-flange example
go run ./examples/npt-flange -resdiv 400 9,01s user 0,82s system 103% cpu 9,481 total
```

GPU rendering is ~40 times faster for the fibonacci-showerhead example.

The result of running the example are two files:
- `nptflange.glsl`: Visualization shader that can be copy pasted into [shadertoy](https://www.shadertoy.com/new) to visualize the part, or rendered within your editor with an extension such as the [Shader Toy Visual Studio Code extension](https://marketplace.visualstudio.com/items?itemName=stevensona.shader-toy).
- `nptflange.stl`: Triangle model file used in 3D printing software such as [Cura](https://ultimaker.com/software/ultimaker-cura/). Can be visualized online in sites such as [View STL](https://www.viewstl.com/).

Below is the 3D scene code. Omits rendering pipeline.
```go
package main

import (
"os"
"runtime"

"github.com/soypat/gsdf"
"github.com/soypat/gsdf/forge/threads"
"github.com/soypat/gsdf/glbuild"
"github.com/soypat/gsdf/gsdfaux"
)

func init() {
runtime.LockOSThread()
}

func main() {
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 {
panic(err)
}

pipe, _ := threads.Nut(threads.NutParams{
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(0.2, pipe, flange)

// 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.RenderShader3D(union, gsdfaux.RenderConfig{
STLOutput: stl,
Resolution: union.Bounds().Diagonal() / 200,
UseGPU: true,
})
if err != nil {
panic(err)
}
}
```

![npt-flange-example](https://github.com/user-attachments/assets/32a00926-0a1e-47f0-8b6c-dda940240265)


### Fibonacci Showerhead example - 40× GPU speedup
### fibonacci-showerhead - 40× GPU speedup

Note that the amount of triangles is very similar to the NPT flange example, but the speedup is much more notable due to the complexity of the part.

Expand Down
4 changes: 4 additions & 0 deletions forge/textsdf/glyph.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package textsdf

type glyph struct {
}
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ go 1.22.1
require (
github.com/chewxy/math32 v1.11.1
github.com/go-gl/gl v0.0.0-20231021071112-07e5d0ea2e71
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
github.com/soypat/glgl v0.0.0-20241019203012-a2ad2ed164c2
)

require (
github.com/go-gl/glfw v0.0.0-20221017161538-93cebf72946b // indirect
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b // indirect
golang.org/x/exp v0.0.0-20221230185412-738e83a70c30 // indirect
golang.org/x/image v0.22.0 // indirect
)
28 changes: 4 additions & 24 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,11 @@ github.com/go-gl/glfw v0.0.0-20221017161538-93cebf72946b h1:2hdUMUOJuLQkhaPAwoyO
github.com/go-gl/glfw v0.0.0-20221017161538-93cebf72946b/go.mod h1:wyvWpaEu9B/VQiV1jsPs7Mha9I7yto/HqIBw197ZAzk=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b h1:GgabKamyOYguHqHjSkDACcgoPIz3w0Dis/zJ1wyHHHU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/soypat/glgl v0.0.0-20240919181848-3ab2abb89c6d h1:ZGFeC6bqQoyQXdYCFitm+4y1FuS1JY6zh5zzBG0OoDQ=
github.com/soypat/glgl v0.0.0-20240919181848-3ab2abb89c6d/go.mod h1:1LcEp6XHSMCI91WlJHzl/aW4Bp5v6yQOiYFyjrlk350=
github.com/soypat/glgl v0.0.0-20240919182556-c8a0c61170b5 h1:1XUEnlRfPTa5DEfZRl9Mhr2UzxQ23cBwNNw0FS1xjXY=
github.com/soypat/glgl v0.0.0-20240919182556-c8a0c61170b5/go.mod h1:1LcEp6XHSMCI91WlJHzl/aW4Bp5v6yQOiYFyjrlk350=
github.com/soypat/glgl v0.0.0-20241002211926-dd918c50e92c h1:AlK/fJ26jQn2fh7vFwWPMMGYYWH6U02P9FpgVG7mXLA=
github.com/soypat/glgl v0.0.0-20241002211926-dd918c50e92c/go.mod h1:1LcEp6XHSMCI91WlJHzl/aW4Bp5v6yQOiYFyjrlk350=
github.com/soypat/glgl v0.0.0-20241002235249-2e94328dcb41 h1:18vgZOd/q87AajjwwFYYIvdjAIqI7wbGDzryhU4DmlU=
github.com/soypat/glgl v0.0.0-20241002235249-2e94328dcb41/go.mod h1:1LcEp6XHSMCI91WlJHzl/aW4Bp5v6yQOiYFyjrlk350=
github.com/soypat/glgl v0.0.0-20241005144040-7a738f969ec0 h1:WaYMUDa2xgr9JASiK8I5SC3Va/z/Bl2Q2ZS4Wav9ytI=
github.com/soypat/glgl v0.0.0-20241005144040-7a738f969ec0/go.mod h1:1LcEp6XHSMCI91WlJHzl/aW4Bp5v6yQOiYFyjrlk350=
github.com/soypat/glgl v0.0.0-20241005144144-20e590a649e7 h1:mBevCWAgR+poTuSPqmyZqzQrBLvUu3NuV4y0sgLHw4Y=
github.com/soypat/glgl v0.0.0-20241005144144-20e590a649e7/go.mod h1:1LcEp6XHSMCI91WlJHzl/aW4Bp5v6yQOiYFyjrlk350=
github.com/soypat/glgl v0.0.0-20241007020858-78b69f528d70 h1:oAG7pXMAOS2v9nBaTp8qhmofwt/LlPKXPV7lxPJh60Y=
github.com/soypat/glgl v0.0.0-20241007020858-78b69f528d70/go.mod h1:1LcEp6XHSMCI91WlJHzl/aW4Bp5v6yQOiYFyjrlk350=
github.com/soypat/glgl v0.0.0-20241007044135-f3d594c19467 h1:I3OXt51Iddvx24TmQrh8mRl91KgTNKZwnKote6YHBac=
github.com/soypat/glgl v0.0.0-20241007044135-f3d594c19467/go.mod h1:1LcEp6XHSMCI91WlJHzl/aW4Bp5v6yQOiYFyjrlk350=
github.com/soypat/glgl v0.0.0-20241008221808-d7a058908ab3 h1:XQ/ITfV8+nbgFXL+tyjihgwppmxF2hyjDtRNCClwalY=
github.com/soypat/glgl v0.0.0-20241008221808-d7a058908ab3/go.mod h1:1LcEp6XHSMCI91WlJHzl/aW4Bp5v6yQOiYFyjrlk350=
github.com/soypat/glgl v0.0.0-20241011195847-825a50ecf4c9 h1:JRjtUKePHuHzACGYTEPNupCeRPm4acZcbGLyERFkXNw=
github.com/soypat/glgl v0.0.0-20241011195847-825a50ecf4c9/go.mod h1:1LcEp6XHSMCI91WlJHzl/aW4Bp5v6yQOiYFyjrlk350=
github.com/soypat/glgl v0.0.0-20241014232203-86da6094be91 h1:etXBCgx7mI2TCq6X3qIzih/r6SmFR0x2b3tBZqPOKm8=
github.com/soypat/glgl v0.0.0-20241014232203-86da6094be91/go.mod h1:1LcEp6XHSMCI91WlJHzl/aW4Bp5v6yQOiYFyjrlk350=
github.com/soypat/glgl v0.0.0-20241019115710-19459b43f961 h1:FsT4mIwdIwrmnhNcw0mlZrgF/yaE/Kc6qPb+2tlMa+I=
github.com/soypat/glgl v0.0.0-20241019115710-19459b43f961/go.mod h1:1LcEp6XHSMCI91WlJHzl/aW4Bp5v6yQOiYFyjrlk350=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/soypat/glgl v0.0.0-20241019203012-a2ad2ed164c2 h1:N/GkdilItOR9bBrGXIj+53DbxUzRnG1SF/aOkBQzlxs=
github.com/soypat/glgl v0.0.0-20241019203012-a2ad2ed164c2/go.mod h1:1LcEp6XHSMCI91WlJHzl/aW4Bp5v6yQOiYFyjrlk350=
golang.org/x/exp v0.0.0-20221230185412-738e83a70c30 h1:m9O6OTJ627iFnN2JIWfdqlZCzneRO6EEBsHXI25P8ws=
golang.org/x/exp v0.0.0-20221230185412-738e83a70c30/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/image v0.22.0 h1:UtK5yLUzilVrkjMAZAZ34DXGpASN8i8pj8g+O+yd10g=
golang.org/x/image v0.22.0/go.mod h1:9hPFhljd4zZ1GNSIZJ49sqbp45GKK9t6w+iXvGqZUz4=

0 comments on commit 37890cc

Please sign in to comment.