Skip to content

Commit

Permalink
Builder mega refactor: wrap all sdf generation with Builder type for …
Browse files Browse the repository at this point in the history
…error handling strategies
  • Loading branch information
soypat committed Nov 16, 2024
1 parent 37890cc commit 67294ad
Show file tree
Hide file tree
Showing 30 changed files with 468 additions and 459 deletions.
11 changes: 6 additions & 5 deletions examples/bolt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ func init() {
}

// scene generates the 3D object for rendering.
func scene() (glbuild.Shader3D, error) {
func scene(bld *gsdf.Builder) (glbuild.Shader3D, error) {
const L, shank = 8, 3
threader := threads.ISO{D: 3, P: 0.5, Ext: true}
M3, err := threads.Bolt(threads.BoltParams{
M3, err := threads.Bolt(bld, threads.BoltParams{
Thread: threader,
Style: threads.NutHex,
TotalLength: L + shank,
Expand All @@ -35,8 +35,8 @@ func scene() (glbuild.Shader3D, error) {
if err != nil {
return nil, err
}
M3, _ = gsdf.Rotate(M3, 2.5*math.Pi/2, ms3.Vec{X: 1, Z: 0.1})
return M3, nil
M3 = bld.Rotate(M3, 2.5*math.Pi/2, ms3.Vec{X: 1, Z: 0.1})
return M3, bld.Err()
}

func run() error {
Expand All @@ -49,7 +49,8 @@ func run() error {
flag.Float64Var(&resolution, "res", 0, "Set resolution in shape units. Useful for setting the minimum level of detail to a fixed amount for final result. If not set resdiv used [mm/in]")
flag.UintVar(&flagResDiv, "resdiv", 200, "Set resolution in bounding box diagonal divisions. Useful for prototyping when constant speed of rendering is desired.")
flag.Parse()
object, err := scene()
var bld gsdf.Builder
object, err := scene(&bld)
if err != nil {
return err
}
Expand Down
31 changes: 14 additions & 17 deletions examples/fibonacci-showerhead/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func init() {
}

// scene returns the showerhead object.
func scene() (glbuild.Shader3D, error) {
func scene(bld *gsdf.Builder) (glbuild.Shader3D, error) {
// Showerhead parameters as defined by showerhead geometry.
const (
threadExtDiameter = 65.
Expand All @@ -49,7 +49,7 @@ func scene() (glbuild.Shader3D, error) {
P: threadPitch,
}
)
t2d, err := showerThread.Thread()
t2d, err := showerThread.Thread(bld)
if err != nil {
return nil, err
}
Expand All @@ -60,35 +60,31 @@ func scene() (glbuild.Shader3D, error) {
var object glbuild.Shader3D

// startblock := must3.Cylinder(10, threadExtDiameter/2+showerheadWall, 0)
knurled, err := threads.KnurledHead(threadExtDiameter/2+showerheadWall, threadheight, 1)
knurled, err := threads.KnurledHead(bld, threadExtDiameter/2+showerheadWall, threadheight, 1)
if err != nil {
return nil, err
}

threads, err := threads.Screw(threadheight+.5, showerThread)
threads, err := threads.Screw(bld, threadheight+.5, showerThread)
if err != nil {
return nil, err
}
object = gsdf.Difference(knurled, threads)

base, err := gsdf.NewCylinder(threadExtDiameter/2+showerheadWall, showerheadBaseThick, 0)
if err != nil {
return nil, err
}
base = gsdf.Translate(base, 0, 0, -(threadedLength/2 + showerheadBaseThick/2 - 1))
object = bld.Difference(knurled, threads)
base := bld.NewCylinder(threadExtDiameter/2+showerheadWall, showerheadBaseThick, 0)
base = bld.Translate(base, 0, 0, -(threadedLength/2 + showerheadBaseThick/2 - 1))

// Make showerhead holesSlice with fibonacci spacing.)
hole, _ := gsdf.NewCylinder(0.8, showerheadBaseThick*10, 0)
hole := bld.NewCylinder(0.8, showerheadBaseThick*10, 0)
// Declare Hole accumulator.
holes := hole
for i := 0; i < 130; i++ {
v := fibonacci(i)
holes = gsdf.Union(holes, gsdf.Translate(hole, v.X, v.Y, 0))
holes = bld.Union(holes, bld.Translate(hole, v.X, v.Y, 0))
}
base = gsdf.Difference(base, holes)
base = bld.Difference(base, holes)

object = gsdf.Union(object, base)
return object, nil
object = bld.Union(object, base)
return object, bld.Err()
}

func run() error {
Expand All @@ -101,7 +97,8 @@ func run() error {
flag.Float64Var(&resolution, "res", 0, "Set resolution in shape units. Useful for setting the minimum level of detail to a fixed amount for final result. If not set resdiv used [mm/in]")
flag.UintVar(&flagResDiv, "resdiv", 200, "Set resolution in bounding box diagonal divisions. Useful for prototyping when constant speed of rendering is desired.")
flag.Parse()
object, err := scene()
var bld gsdf.Builder
object, err := scene(&bld)
if err != nil {
return err
}
Expand Down
16 changes: 7 additions & 9 deletions examples/gasket/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func init() {
}

// scene returns the gasket object.
func scene() (glbuild.Shader3D, error) {
func scene(bld *gsdf.Builder) (glbuild.Shader3D, error) {
// Sistema Food Storage Container geometry definitions.
// The problem we are trying to solve is how the container is not airtight
// due to the o-ring not sealing against the lid. We can aid the o-ring
Expand Down Expand Up @@ -65,12 +65,9 @@ func scene() (glbuild.Shader3D, error) {
if err != nil {
return nil, err
}
poly2, err := gsdf.NewPolygon(verts)
if err != nil {
return nil, err
}
poly2 = gsdf.Symmetry2D(poly2, true, true)
poly2 = gsdf.Offset2D(poly2, tol)
poly2 := bld.NewPolygon(verts)
poly2 = bld.Symmetry2D(poly2, true, true)
poly2 = bld.Offset2D(poly2, tol)
if visualization2D != "" {
start := time.Now()
sdf, err := gleval.NewCPUSDF2(poly2)
Expand All @@ -83,7 +80,7 @@ func scene() (glbuild.Shader3D, error) {
}
fmt.Println("wrote 2D visualization to", visualization2D, "in", time.Since(start))
}
return gsdf.Extrude(poly2, gasketHeight)
return bld.Extrude(poly2, gasketHeight), bld.Err()
}

func run() error {
Expand All @@ -96,7 +93,8 @@ func run() error {
flag.Float64Var(&resolution, "res", 0, "Set resolution in shape units. Useful for setting the minimum level of detail to a fixed amount for final result. If not set resdiv used [mm/in]")
flag.UintVar(&flagResDiv, "resdiv", 350, "Set resolution in bounding box diagonal divisions. Useful for prototyping when constant speed of rendering is desired.")
flag.Parse()
object, err := scene()
var bld gsdf.Builder
object, err := scene(&bld)
if err != nil {
return err
}
Expand Down
19 changes: 7 additions & 12 deletions examples/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,21 @@ import (
const dim = 20
const filename = "image-example.png"

func scene() (glbuild.Shader2D, error) {
s, err := gsdf.NewCircle(dim)
if err != nil {
return nil, err
}
poly, err := gsdf.NewPolygon([]ms2.Vec{
func scene(bld *gsdf.Builder) (glbuild.Shader2D, error) {
s := bld.NewCircle(dim)
poly := bld.NewPolygon([]ms2.Vec{
{X: dim, Y: 0},
{X: 3 * dim, Y: dim},
{X: 3 * dim, Y: -dim},
})
if err != nil {
return nil, err
}
s = gsdf.Union2D(s, poly)
return s, nil
s = bld.Union2D(s, poly)
return s, bld.Err()
}

func main() {
useGPU := false
s, err := scene()
var bld gsdf.Builder
s, err := scene(&bld)
if err != nil {
log.Fatal(err)
}
Expand Down
21 changes: 11 additions & 10 deletions examples/npt-flange/flange.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func init() {
runtime.LockOSThread() // For when using GPU this is required.
}

func scene() (glbuild.Shader3D, error) {
func scene(bld *gsdf.Builder) (glbuild.Shader3D, error) {
const (
tlen = 18. / 25.4
internalDiameter = 1.5 / 2.
Expand All @@ -37,25 +37,25 @@ func scene() (glbuild.Shader3D, error) {
return nil, err
}

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

// Base plate which goes bolted to joint.
flange, _ = gsdf.NewCylinder(flangeD/2, flangeH, flangeH/8)
flange = bld.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)
flange = bld.Translate(flange, 0, 0, -tlen/2)
union := bld.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)
hole := bld.NewCylinder(internalDiameter/2, 4*flangeH, 0)
union = bld.Difference(union, hole)
// Convert from imperial inches units to millimeter:
union = gsdf.Scale(union, 25.4)
return union, nil
union = bld.Scale(union, 25.4)
return union, bld.Err()
}

func run() error {
Expand All @@ -68,7 +68,8 @@ func run() error {
flag.Float64Var(&resolution, "res", 0, "Set resolution in shape units. Useful for setting the minimum level of detail to a fixed amount for final result. If not set resdiv used [mm/in]")
flag.UintVar(&flagResDiv, "resdiv", 200, "Set resolution in bounding box diagonal divisions. Useful for prototyping when constant speed of rendering is desired.")
flag.Parse()
object, err := scene()
var bld gsdf.Builder
object, err := scene(&bld)
if err != nil {
return err
}
Expand Down
13 changes: 6 additions & 7 deletions examples/plantpot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func init() {
}

// scenePotBase returns the plant pot base object.
func scenePotBase() (glbuild.Shader3D, error) {
func scenePotBase(bld *gsdf.Builder) (glbuild.Shader3D, error) {
const (
baseHeight = 10.
baseInclinationDeg = 45.
Expand All @@ -51,10 +51,8 @@ func scenePotBase() (glbuild.Shader3D, error) {
if err != nil {
return nil, err
}
poly2, err := gsdf.NewPolygon(verts)
if err != nil {
return nil, err
}
poly2 := bld.NewPolygon(verts)

sdf, err := gleval.NewCPUSDF2(poly2)
if err != nil {
return nil, err
Expand All @@ -63,7 +61,7 @@ func scenePotBase() (glbuild.Shader3D, error) {
if err != nil {
return nil, err
}
return gsdf.Revolve(poly2, 0)
return bld.Revolve(poly2, 0), bld.Err()
}

func run() error {
Expand All @@ -76,7 +74,8 @@ func run() error {
flag.Float64Var(&resolution, "res", 0, "Set resolution in shape units. Useful for setting the minimum level of detail to a fixed amount for final result. If not set resdiv used [mm/in]")
flag.UintVar(&flagResDiv, "resdiv", 350, "Set resolution in bounding box diagonal divisions. Useful for prototyping when constant speed of rendering is desired.")
flag.Parse()
object, err := scenePotBase()
var bld gsdf.Builder
object, err := scenePotBase(&bld)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 67294ad

Please sign in to comment.