Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert remaining panics #27

Merged
merged 9 commits into from
Nov 24, 2020
11 changes: 9 additions & 2 deletions examples/benchmark/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package main

import . "github.com/deadsy/sdfx/sdf"
import (
"log"

. "github.com/deadsy/sdfx/sdf"
)

func main() {
s2d := Circle2D(5)
Expand All @@ -9,7 +13,10 @@ func main() {
s2d, _ = FlatFlankCam2D(30, 20, 5)
BenchmarkSDF2("cam1 SDF2", s2d)

s2d, _ = ThreeArcCam2D(30, 20, 5, 200)
s2d, err := ThreeArcCam2D(30, 20, 5, 200)
if err != nil {
log.Fatal(err)
}
BenchmarkSDF2("cam2 SDF2", s2d)

s2d = Polygon2D(Nagon(6, 10.0))
Expand Down
5 changes: 4 additions & 1 deletion examples/bezier/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,10 @@ func shape() error {
s5 := sdf.Polygon2D(p.Vertices())

s2d := sdf.Difference2D(s0, sdf.Union2D(s1, s2, s3, s4, s5))
s3d, _ := sdf.ExtrudeRounded3D(s2d, 200, 20)
s3d, err := sdf.ExtrudeRounded3D(s2d, 200, 20)
if err != nil {
return err
}
sdf.RenderSTL(s3d, 300, "shape.stl")
return nil
}
Expand Down
16 changes: 12 additions & 4 deletions examples/devo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ package main

import (
"fmt"
"log"

"github.com/deadsy/sdfx/sdf"
)

//-----------------------------------------------------------------------------

func dome(r, h, w float64) sdf.SDF3 {
func dome(r, h, w float64) (sdf.SDF3, error) {

fillet := w

Expand Down Expand Up @@ -77,10 +78,13 @@ func dome(r, h, w float64) sdf.SDF3 {

b.Close()

p, _ = b.Polygon()
p, err := b.Polygon()
if err != nil {
return nil, err
}
inner := sdf.Revolve3D(sdf.Polygon2D(p.Vertices()))

return sdf.Difference3D(outer, inner)
return sdf.Difference3D(outer, inner), nil
}

//-----------------------------------------------------------------------------
Expand All @@ -90,7 +94,11 @@ func main() {
h0 := 2.05 * sdf.MillimetresPerInch
wall := 4.0

s := dome(radius, h0, wall)
s, err := dome(radius, h0, wall)
if err != nil {
log.Fatal(err)
}

//s = sdf.Cut3D(s, V3{0, 0, 0}, sdf.V3{0, 1, 0})
sdf.RenderSTL(s, 150, "energy_dome.stl")
}
Expand Down
49 changes: 38 additions & 11 deletions examples/fidget/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Fidget Spinners
package main

import (
"log"

"github.com/deadsy/sdfx/sdf"
)

Expand All @@ -33,7 +35,7 @@ const bbSmallD = (5.0 / 16.0) * sdf.MillimetresPerInch
//-----------------------------------------------------------------------------

// Return an N petal bezier flower.
func flower(n int, r0, r1, r2 float64) sdf.SDF2 {
func flower(n int, r0, r1, r2 float64) (sdf.SDF2, error) {

theta := sdf.Tau / float64(n)
b := sdf.NewBezier()
Expand Down Expand Up @@ -63,11 +65,15 @@ func flower(n int, r0, r1, r2 float64) sdf.SDF2 {
}

b.Close()
p, _ := b.Polygon()
return sdf.Polygon2D(p.Vertices())
p, err := b.Polygon()
if err != nil {
return nil, err
}

return sdf.Polygon2D(p.Vertices()), nil
}

func body1() sdf.SDF3 {
func body1() (sdf.SDF3, error) {

n := 3
t := bearingThickness
Expand All @@ -78,13 +84,21 @@ func body1() sdf.SDF3 {
r2 := r + 4.0

// body
s1, _ := sdf.ExtrudeRounded3D(flower(n, r0, r1, r2), t, t/4.0)
f, err := flower(n, r0, r1, r2)
if err != nil {
log.Fatal(err)
}
s1, err := sdf.ExtrudeRounded3D(f, t, t/4.0)
if err != nil {
return nil, err
}

// periphery holes
s2 := sdf.MakeBoltCircle3D(t, r+clearance, r1, n)
// center hole
s3 := sdf.Cylinder3D(t, r+clearance, 0)

return sdf.Difference3D(s1, sdf.Union3D(s2, s3))
return sdf.Difference3D(s1, sdf.Union3D(s2, s3)), nil
}

//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -177,25 +191,38 @@ func spincapDouble(male bool) sdf.SDF3 {
}

// Inner washer for double spinner.
func spincapWasher() sdf.SDF3 {
func spincapWasher() (sdf.SDF3, error) {
k := sdf.WasherParms{
Thickness: 1.0,
InnerRadius: (bearingInnerID / 2) * 1.05,
OuterRadius: (bearingOuterOD + bearingInnerID) / 4,
}
s, _ := sdf.Washer3D(&k)
return s
s, err := sdf.Washer3D(&k)
if err != nil {
return nil, err
}
return s, nil
}

//-----------------------------------------------------------------------------

func main() {
sdf.RenderSTL(body1(), 300, "body1.stl")
b1, err := body1()
if err != nil {
log.Fatal(err)
}
sdf.RenderSTL(b1, 300, "body1.stl")

sdf.RenderSTL(body2(), 300, "body2.stl")
sdf.RenderSTL(spincapSingle(), 150, "cap_single.stl")
sdf.RenderSTL(spincapWasher(), 150, "washer.stl")
sdf.RenderSTL(spincapDouble(true), 150, "cap_double_male.stl")
sdf.RenderSTL(spincapDouble(false), 150, "cap_double_female.stl")

scw, err := spincapWasher()
if err != nil {
log.Fatal(err)
}
sdf.RenderSTL(scw, 150, "washer.stl")
}

//-----------------------------------------------------------------------------
16 changes: 12 additions & 4 deletions examples/finial/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func square2(l float64) SDF2 {

//-----------------------------------------------------------------------------

func finial2() {
func finial2() error {
base := 100.0
base_height := 20.0
column_radius := 15.0
Expand All @@ -52,7 +52,10 @@ func finial2() {
s0 := square2(base)
s1 := Circle2D(column_radius)

column_3d, _ := Loft3D(s0, s1, column_height, 0)
column_3d, err := Loft3D(s0, s1, column_height, 0)
if err != nil {
return err
}
column_3d = Transform3D(column_3d, Translate3d(V3{0, 0, column_ofs}))

ball_3d := Sphere3D(ball_radius)
Expand All @@ -64,11 +67,12 @@ func finial2() {
bc_3d.(*UnionSDF3).SetMin(PolyMin(round))

RenderSTLSlow(Union3D(bc_3d, base_3d), 300, "f2.stl")
return err
}

//-----------------------------------------------------------------------------

func finial1() {
func finial1() error {
base := 100.0
base_height := 20.0
column_radius := 15.0
Expand All @@ -81,7 +85,10 @@ func finial1() {
s0 := Polygon2D(Nagon(4, base*math.Sqrt(0.5)))
s1 := Circle2D(column_radius)

column_3d, _ := Loft3D(s0, s1, column_height, 0)
column_3d, err := Loft3D(s0, s1, column_height, 0)
if err != nil {
return err
}
column_3d = Transform3D(column_3d, Translate3d(V3{0, 0, column_ofs}))

ball_3d := Sphere3D(ball_radius)
Expand All @@ -93,6 +100,7 @@ func finial1() {
bc_3d.(*UnionSDF3).SetMin(PolyMin(round))

RenderSTLSlow(Union3D(bc_3d, base_3d), 300, "f1.stl")
return err
}

//-----------------------------------------------------------------------------
Expand Down
30 changes: 22 additions & 8 deletions examples/maixgo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ https://www.seeedstudio.com/Sipeed-MAix-GO-Suit-for-RISC-V-AI-IoT-p-2874.html

package main

import . "github.com/deadsy/sdfx/sdf"
import (
"log"

. "github.com/deadsy/sdfx/sdf"
)

//-----------------------------------------------------------------------------

Expand Down Expand Up @@ -87,7 +91,7 @@ func speakerHoles(d float64, ofs V2) SDF2 {
return Transform2D(Union2D(s0, s1), Translate2d(ofs))
}

func speakerHolder(d float64, ofs V2) SDF3 {
func speakerHolder(d float64, ofs V2) (SDF3, error) {
thickness := 3.0
zOfs := 0.5 * (thickness + baseThickness)
k := WasherParms{
Expand All @@ -96,14 +100,17 @@ func speakerHolder(d float64, ofs V2) SDF3 {
OuterRadius: 0.5 * (d + 4.0),
Remove: 0.3,
}
s, _ := Washer3D(&k)
s, err := Washer3D(&k)
if err != nil {
return nil, err
}
s = Transform3D(s, RotateZ(Pi))
return Transform3D(s, Translate3d(V3{ofs.X, ofs.Y, zOfs}))
return Transform3D(s, Translate3d(V3{ofs.X, ofs.Y, zOfs})), nil
}

//-----------------------------------------------------------------------------

func bezel() SDF3 {
func bezel() (SDF3, error) {

speakerOfs := V2{60, 14}
speakerDiameter := 20.3
Expand Down Expand Up @@ -138,15 +145,22 @@ func bezel() SDF3 {
s1.(*UnionSDF3).SetMin(PolyMin(3.0))

// speaker holder
s3 := speakerHolder(speakerDiameter, speakerOfs)
s3, err := speakerHolder(speakerDiameter, speakerOfs)
if err != nil {
return nil, err
}

return Union3D(s1, s3)
return Union3D(s1, s3), nil
}

//-----------------------------------------------------------------------------

func main() {
RenderSTL(ScaleUniform3D(bezel(), shrink), 330, "bezel.stl")
b, err := bezel()
if err != nil {
log.Fatal(err)
}
RenderSTL(ScaleUniform3D(b, shrink), 330, "bezel.stl")
}

//-----------------------------------------------------------------------------
32 changes: 24 additions & 8 deletions examples/test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,15 @@ func test32() {
RenderSTL(s1, 200, "test.stl")
}

func test33() {
s0, _ := ThreeArcCam2D(30, 20, 5, 50000)
func test33() error {
s0, err := ThreeArcCam2D(30, 20, 5, 50000)
if err != nil {
return err
}
fmt.Printf("%+v\n", s0)
s1 := Extrude3D(s0, 4)
RenderSTL(s1, 200, "test.stl")
return nil
}

func test34() {
Expand Down Expand Up @@ -398,19 +402,27 @@ func test42() {
p.Render("test.dxf")
}

func test43() {
func test43() error {
s0 := Line2D(10, 3)
s0 = Cut2D(s0, V2{4, 0}, V2{1, 1})
s1, _ := ExtrudeRounded3D(s0, 4, 1)
s1, err := ExtrudeRounded3D(s0, 4, 1)
if err != nil {
return err
}
RenderSTL(s1, 300, "test.stl")
return nil
}

func test44() {
func test44() error {
r := 100.0
s0 := Polygon2D(Nagon(5, r))
s1 := Circle2D(r / 2)
s2, _ := Loft3D(s1, s0, 200.0, 20.0)
s2, err := Loft3D(s1, s0, 200.0, 20.0)
if err != nil {
return err
}
RenderSTL(s2, 300, "test.stl")
return err
}

func test45() {
Expand Down Expand Up @@ -518,15 +530,19 @@ func test49() {
RenderDXF(s0, 50, "test.dxf")
}

func test50() {
func test50() error {
k := WasherParms{
Thickness: 10,
InnerRadius: 40,
OuterRadius: 50,
Remove: 0.3,
}
s, _ := Washer3D(&k)
s, err := Washer3D(&k)
if err != nil {
return err
}
RenderSTL(s, 300, "test.stl")
return nil
}

func main() {
Expand Down
5 changes: 4 additions & 1 deletion examples/text/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ func main() {
sdf.RenderDXF(s2d, 600, "shape.dxf")
sdf.RenderSVG(s2d, 600, "shape.svg", "fill:none;stroke:black;stroke-width:0.1")

s3d, _ := sdf.ExtrudeRounded3D(s2d, 1.0, 0.2)
s3d, err := sdf.ExtrudeRounded3D(s2d, 1.0, 0.2)
if err != nil {
log.Fatal(err)
}
sdf.RenderSTL(s3d, 600, "shape.stl")
}

Expand Down
Loading