From 0341adb87ec5933f9a9f84d3269dcd3fb415e739 Mon Sep 17 00:00:00 2001 From: soypat Date: Sat, 14 May 2022 14:20:49 -0300 Subject: [PATCH] fix form3.Cylinder. refactor threads --- README.md | 1 + form2/basic.go | 5 ----- form2/{must2 => obj2}/thread.go | 31 ++++++++++++++++--------------- form3/basic.go | 2 +- form3/must3/cylinders.go | 5 ----- form3/obj3/bolt.go | 6 +++--- form3/obj3/nut.go | 6 +++--- form3/screw.go | 4 ++-- 8 files changed, 26 insertions(+), 34 deletions(-) rename form2/{must2 => obj2}/thread.go (93%) diff --git a/README.md b/README.md index 001740a..1a3631c 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ Here is a rendered bolt from one of the unit tests under [form3_test.go](./rende ## Roadmap - [ ] Add a 2D renderer and it's respective `Renderer2` interface. - [ ] Make 3D renderer multicore +- [ ] Clean up thread API mess ## Comparison diff --git a/form2/basic.go b/form2/basic.go index f93e510..60fee85 100644 --- a/form2/basic.go +++ b/form2/basic.go @@ -56,8 +56,3 @@ func Line(l, round float64) (s sdf.SDF2, err error) { }() return must2.Line(l, round), err } - -// ThreadLookup lookups the parameters for a thread by name. -func ThreadLookup(name string) (must2.ThreadParameters, error) { - return must2.ThreadLookup(name) -} diff --git a/form2/must2/thread.go b/form2/obj2/thread.go similarity index 93% rename from form2/must2/thread.go rename to form2/obj2/thread.go index 391643b..e7d1d3b 100644 --- a/form2/must2/thread.go +++ b/form2/obj2/thread.go @@ -1,4 +1,4 @@ -package must2 +package obj2 import ( "fmt" @@ -6,6 +6,7 @@ import ( "math" "github.com/soypat/sdf" + "github.com/soypat/sdf/form2/must2" ) // Screws @@ -38,12 +39,10 @@ type threadDatabase map[string]ThreadParameters var threadDB = initThreadLookup() // UTSAdd adds a Unified Thread Standard to the thread database. -func (m threadDatabase) UTSAdd( - name string, // thread name - diameter float64, // screw major diameter - tpi float64, // threads per inch - ftof float64, // hex head flat to flat distance -) { +// diameter is screw major diameter. +// tpi is threads per inch. +// ftof is hex head flat to flat distance. +func (m threadDatabase) UTSAdd(name string, diameter float64, tpi float64, ftof float64) { if ftof <= 0 { log.Panicf("bad flat to flat distance for thread \"%s\"", name) } @@ -212,7 +211,7 @@ func AcmeThread(radius float64, pitch float64) sdf.SDF2 { xOfs0 := 0.25*pitch - delta xOfs1 := 0.25*pitch + delta - acme := NewPolygon() + acme := must2.NewPolygon() acme.Add(radius, 0) acme.Add(radius, h) acme.Add(xOfs1, h) @@ -222,7 +221,7 @@ func AcmeThread(radius float64, pitch float64) sdf.SDF2 { acme.Add(-radius, h) acme.Add(-radius, 0) - return Polygon(acme.Vertices()) + return must2.Polygon(acme.Vertices()) } // ISOThread returns the 2d profile for an ISO/UTS thread. @@ -236,7 +235,7 @@ func ISOThread(radius float64, pitch float64, external bool) sdf.SDF2 { rMajor := radius r0 := rMajor - (7.0/8.0)*h - iso := NewPolygon() + iso := must2.NewPolygon() if external { rRoot := (pitch / 8.0) / math.Cos(theta) xOfs := (1.0 / 16.0) * pitch @@ -260,7 +259,7 @@ func ISOThread(radius float64, pitch float64, external bool) sdf.SDF2 { iso.Add(-pitch, rMinor) iso.Add(-pitch, 0) } - return Polygon(iso.Vertices()) + return must2.Polygon(iso.Vertices()) } // ANSIButtressThread returns the 2d profile for an ANSI 45/7 buttress thread. @@ -276,7 +275,7 @@ func ANSIButtressThread(radius float64, pitch float64) sdf.SDF2 { h1 := ((b / 2.0) * pitch) + (0.5 * h0) hp := pitch / 2.0 - tp := NewPolygon() + tp := must2.NewPolygon() tp.Add(pitch, 0) tp.Add(pitch, radius) tp.Add(hp-((h0-h1)*t1), radius) @@ -285,7 +284,7 @@ func ANSIButtressThread(radius float64, pitch float64) sdf.SDF2 { tp.Add(-pitch, radius) tp.Add(-pitch, 0) - return Polygon(tp.Vertices()) + return must2.Polygon(tp.Vertices()) } // PlasticButtressThread returns the 2d profile for a screw top style plastic buttress thread. @@ -300,7 +299,7 @@ func PlasticButtressThread(radius float64, pitch float64) sdf.SDF2 { h1 := ((b / 2.0) * pitch) + (0.5 * h0) hp := pitch / 2.0 - tp := NewPolygon() + tp := must2.NewPolygon() tp.Add(pitch, 0) tp.Add(pitch, radius) tp.Add(hp-((h0-h1)*t1), radius).Smooth(0.05*pitch, 5) @@ -309,5 +308,7 @@ func PlasticButtressThread(radius float64, pitch float64) sdf.SDF2 { tp.Add(-pitch, radius) tp.Add(-pitch, 0) - return Polygon(tp.Vertices()) + return must2.Polygon(tp.Vertices()) } +func d2r(degrees float64) float64 { return degrees * math.Pi / 180. } +func r2d(radians float64) float64 { return radians / math.Pi * 180. } diff --git a/form3/basic.go b/form3/basic.go index ed37617..6f7b901 100644 --- a/form3/basic.go +++ b/form3/basic.go @@ -56,7 +56,7 @@ func Cylinder(height, radius, round float64) (s sdf.SDF3, err error) { } } }() - return must3.Sphere(radius), err + return must3.Cylinder(height, radius, round), err } // Capsule3D return an SDF3 for a capsule. diff --git a/form3/must3/cylinders.go b/form3/must3/cylinders.go index 1f5bd29..f80a810 100644 --- a/form3/must3/cylinders.go +++ b/form3/must3/cylinders.go @@ -109,11 +109,6 @@ func Cylinder(height, radius, round float64) *cylinder { return &s } -// Capsule3D return an SDF3 for a capsule. -func Capsule(height, radius float64) *cylinder { - return Cylinder(height, radius, radius) -} - // Evaluate returns the minimum distance to a cylinder. func (s *cylinder) Evaluate(p r3.Vec) float64 { d := sdfBox2d(r2.Vec{math.Hypot(p.X, p.Y), p.Z}, r2.Vec{s.radius, s.height}) diff --git a/form3/obj3/bolt.go b/form3/obj3/bolt.go index 07af990..439482c 100644 --- a/form3/obj3/bolt.go +++ b/form3/obj3/bolt.go @@ -2,7 +2,7 @@ package obj3 import ( "github.com/soypat/sdf" - form2 "github.com/soypat/sdf/form2/must2" + "github.com/soypat/sdf/form2/obj2" form3 "github.com/soypat/sdf/form3/must3" "gonum.org/v1/gonum/spatial/r3" ) @@ -21,7 +21,7 @@ type BoltParms struct { // Bolt returns a simple bolt suitable for 3d printing. func Bolt(k BoltParms) (s sdf.SDF3, err error) { // validate parameters - t, err := form2.ThreadLookup(k.Thread) + t, err := obj2.ThreadLookup(k.Thread) if err != nil { panic(err) } @@ -63,7 +63,7 @@ func Bolt(k BoltParms) (s sdf.SDF3, err error) { if threadLength != 0 { r := t.Radius - k.Tolerance threadOffset := threadLength/2 + shankLength - isoThread := form2.ISOThread(r, t.Pitch, true) + isoThread := obj2.ISOThread(r, t.Pitch, true) thread = form3.Screw(isoThread, threadLength, t.Taper, t.Pitch, 1) // chamfer the thread thread = form3.ChamferedCylinder(thread, 0, 0.5) diff --git a/form3/obj3/nut.go b/form3/obj3/nut.go index c6c5c0d..39b6654 100644 --- a/form3/obj3/nut.go +++ b/form3/obj3/nut.go @@ -2,7 +2,7 @@ package obj3 import ( "github.com/soypat/sdf" - form2 "github.com/soypat/sdf/form2/must2" + "github.com/soypat/sdf/form2/obj2" form3 "github.com/soypat/sdf/form3/must3" ) @@ -19,7 +19,7 @@ func Nut(k NutParms) (s sdf.SDF3, err error) { panic("Tolerance < 0") } // validate parameters - t, err := form2.ThreadLookup(k.Thread) + t, err := obj2.ThreadLookup(k.Thread) if err != nil { panic(err) } @@ -40,7 +40,7 @@ func Nut(k NutParms) (s sdf.SDF3, err error) { } // internal thread - isoThread := form2.ISOThread(t.Radius+k.Tolerance, t.Pitch, false) + isoThread := obj2.ISOThread(t.Radius+k.Tolerance, t.Pitch, false) thread := form3.Screw(isoThread, nh, t.Taper, t.Pitch, 1) return sdf.Difference3D(nut, thread), err // TODO error handling diff --git a/form3/screw.go b/form3/screw.go index 4ea902b..75a0a2f 100644 --- a/form3/screw.go +++ b/form3/screw.go @@ -7,12 +7,12 @@ import ( "github.com/soypat/sdf/form3/must3" ) -// Screw returns a screw SDF3. +// screw returns a screw SDF3. // - length of screw // - thread taper angle (radians) // - pitch thread to thread distance // - number of thread starts (< 0 for left hand threads) -func Screw(thread sdf.SDF2, length, taper, pitch float64, starts int) (s sdf.SDF3, err error) { +func screw(thread sdf.SDF2, length, taper, pitch float64, starts int) (s sdf.SDF3, err error) { defer func() { if a := recover(); a != nil { err = &shapeErr{