Skip to content

Commit

Permalink
fix CPU cylinder for round=0
Browse files Browse the repository at this point in the history
  • Loading branch information
soypat committed Aug 17, 2024
1 parent b8a3480 commit c2dc60a
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cpu_evaluators.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (c *cylinder) Evaluate(pos []ms3.Vec, dist []float32, userData any) error {
if round == 0 {
for i, p := range pos {
p = ms3.Vec{X: p.X, Y: p.Z, Z: p.Y}
dx := math32.Hypot(p.X, p.Y) - r
dx := math32.Hypot(p.X, p.Z) - r
dy := math32.Abs(p.Y) - h
dist[i] = minf(0, maxf(dx, dy)) + math32.Hypot(math32.Max(0, dx), math32.Max(0, dy))
}
Expand Down
3 changes: 1 addition & 2 deletions examples/npt-flange/flange.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ func scene() (gleval.SDF3, error) {
Thread: threads.ISO{D: npt.D, P: 1.0 / npt.TPI},
Style: threads.NutHex,
})

return makeSDF(pipe)
if err != nil {
return nil, err
}
Expand All @@ -113,6 +111,7 @@ func scene() (gleval.SDF3, error) {
if err != nil {
return nil, err
}
// return makeSDF(hole)
flange = gsdf.Difference(flange, hole) // Make through-hole in flange bottom
flange = gsdf.Scale(flange, 25.4) // convert to millimeters
return makeSDF(flange)
Expand Down
1 change: 1 addition & 0 deletions examples/test/glsdf3test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ var PremadePrimitives = []glbuild.Shader3D{
mustShader(gsdf.NewTorus(3, .5)), // Negative normal?
mustShader(gsdf.NewTriangularPrism(1, 2)),
mustShader(gsdf.NewCylinder(1, 3, .1)),
mustShader(gsdf.NewCylinder(1, 3, 0)), // Cylinder with no rounding.
mustShader(threads.Screw(5, threads.ISO{ // Negative normal.
D: 1,
P: 0.1,
Expand Down
9 changes: 5 additions & 4 deletions primitives.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,14 @@ func (s *box) Bounds() ms3.Box {
}

func NewCylinder(r, h, rounding float32) (glbuild.Shader3D, error) {
if rounding < 0 || rounding >= r || rounding > h/2 {
okRounding := rounding >= 0 && rounding < r && rounding < h/2
if !okRounding {
return nil, errors.New("invalid cylinder rounding")
}
if r <= 0 || h <= 0 {
return nil, errors.New("zero or negative cylinder dimension")
if r > 0 && h > 0 {
return &cylinder{r: r, h: h, round: rounding}, nil
}
return &cylinder{r: r, h: h, round: rounding}, nil
return nil, errors.New("bad cylinder dimension")
}

type cylinder struct {
Expand Down

0 comments on commit c2dc60a

Please sign in to comment.