Skip to content
This repository has been archived by the owner on Aug 12, 2024. It is now read-only.

Commit

Permalink
fix form3.Cylinder. refactor threads
Browse files Browse the repository at this point in the history
  • Loading branch information
soypat committed May 14, 2022
1 parent a502593 commit 0341adb
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 34 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 0 additions & 5 deletions form2/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
31 changes: 16 additions & 15 deletions form2/must2/thread.go → form2/obj2/thread.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package must2
package obj2

import (
"fmt"
"log"
"math"

"github.com/soypat/sdf"
"github.com/soypat/sdf/form2/must2"
)

// Screws
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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)
Expand All @@ -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.
Expand All @@ -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)
Expand All @@ -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. }
2 changes: 1 addition & 1 deletion form3/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 0 additions & 5 deletions form3/must3/cylinders.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
6 changes: 3 additions & 3 deletions form3/obj3/bolt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions form3/obj3/nut.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
}
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions form3/screw.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 0341adb

Please sign in to comment.