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

Gonum box #5

Merged
merged 2 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions examples/atx-bench-supply/atx.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ func main() {

// Begin working on regulated step-down block
regOut = sdf.Array2D(bananaPlugSmall, sdf.V2i{2, 1}, r2.Vec{bananaSpacing, bananaSpacing})
bplugX := regOut.BoundingBox().Size().X
bplugX := bbSize(regBlock.Bounds()).X
vDisp := sdf.Transform2D(voltageDisplay, sdf.Translate2d(r2.Vec{bplugX / 2, vDispH/2 + bananaSpacing/2}))
regOut = sdf.Union2D(regOut, vDisp)
regOut = sdf.Transform2D(regOut, sdf.Translate2d(r2.Vec{-atxW/2 - bplugX/2 + vDispW/2 + 12, atxH/2 - 12 - vDispH/2 - bananaSpacing}))
// Create mound for step up outputs.
regSz := regOut.BoundingBox().Size()
regSz := bbSize(regBlock.Bounds())
regBlock = form2.Box(r2.Vec{regSz.X + regBlockMargin, regSz.Y + regBlockMargin}, regBlockMargin/2)
regBlock = sdf.Transform2D(regBlock, sdf.Translate2d(regOut.BoundingBox().Center()))
regBlock = sdf.Transform2D(regBlock, sdf.Translate2d(bbCenter(regOut.Bounds())))
regBlock = sdf.Difference2D(regBlock, regOut)
regBlock3 := sdf.Extrude3D(regBlock, panelThickness+regBlockDepth) // extrude does it both ways.
regBlock3 = sdf.Transform3D(regBlock3, sdf.Translate3D(r3.Vec{0, 0, regBlockDepth / 2}))
Expand Down Expand Up @@ -96,3 +96,11 @@ func must(err error) {
panic(err)
}
}

func bbSize(bb r2.Box) r2.Vec {
return r2.Sub(bb.Max, bb.Min)
}

func bbCenter(bb r2.Box) r2.Vec {
return r2.Add(bb.Min, r2.Scale(0.5, bbSize(bb)))
}
18 changes: 9 additions & 9 deletions form2/must2/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// circle is the 2d signed distance object for a circle.
type circle struct {
radius float64
bb d2.Box
bb r2.Box
}

// Circle returns the SDF2 for a 2d circle.
Expand All @@ -21,7 +21,7 @@ func Circle(radius float64) *circle {
s := circle{}
s.radius = radius
d := r2.Vec{radius, radius}
s.bb = d2.Box{r2.Scale(-1, d), d}
s.bb = r2.Box{r2.Scale(-1, d), d}
return &s
}

Expand All @@ -31,7 +31,7 @@ func (s *circle) Evaluate(p r2.Vec) float64 {
}

// BoundingBox returns the bounding box of a 2d circle.
func (s *circle) BoundingBox() d2.Box {
func (s *circle) Bounds() r2.Box {
return s.bb
}

Expand All @@ -41,7 +41,7 @@ func (s *circle) BoundingBox() d2.Box {
type box struct {
size r2.Vec
round float64
bb d2.Box
bb r2.Box
}

// Box returns a 2d box.
Expand All @@ -50,7 +50,7 @@ func Box(size r2.Vec, round float64) *box {
s := box{}
s.size = r2.Sub(size, d2.Elem(round))
s.round = round
s.bb = d2.Box{r2.Scale(-1, size), size}
s.bb = r2.Box{r2.Scale(-1, size), size}
return &s
}

Expand All @@ -60,7 +60,7 @@ func (s *box) Evaluate(p r2.Vec) float64 {
}

// BoundingBox returns the bounding box for a 2d box.
func (s *box) BoundingBox() d2.Box {
func (s *box) Bounds() r2.Box {
return s.bb
}

Expand All @@ -70,15 +70,15 @@ func (s *box) BoundingBox() d2.Box {
type line struct {
l float64 // line length
round float64 // rounding
bb d2.Box // bounding box
bb r2.Box // bounding box
}

// Line returns a line from (-l/2,0) to (l/2,0).
func Line(l, round float64) *line {
s := line{}
s.l = l / 2
s.round = round
s.bb = d2.Box{r2.Vec{-s.l - round, -round}, r2.Vec{s.l + round, round}}
s.bb = r2.Box{r2.Vec{-s.l - round, -round}, r2.Vec{s.l + round, round}}
return &s
}

Expand All @@ -92,6 +92,6 @@ func (s *line) Evaluate(p r2.Vec) float64 {
}

// BoundingBox returns the bounding box for a 2d line.
func (s *line) BoundingBox() d2.Box {
func (s *line) Bounds() r2.Box {
return s.bb
}
6 changes: 3 additions & 3 deletions form2/must2/poly.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type polygon struct {
vertex []r2.Vec // vertices
vector []r2.Vec // unit line vectors
length []float64 // line lengths
bb d2.Box // bounding box
bb r2.Box // bounding box
}

// Polygon returns an SDF2 made from a closed set of line segments.
Expand Down Expand Up @@ -48,7 +48,7 @@ func Polygon(vertex []r2.Vec) sdf.SDF2 {
vmax = d2.MaxElem(vmax, s.vertex[i])
}

s.bb = d2.Box{r2.Vec{vmin.X, vmin.Y}, r2.Vec{vmax.X, vmax.Y}}
s.bb = r2.Box{r2.Vec{vmin.X, vmin.Y}, r2.Vec{vmax.X, vmax.Y}}
return &s
}

Expand Down Expand Up @@ -107,7 +107,7 @@ func (s *polygon) Evaluate(p r2.Vec) float64 {
}

// BoundingBox returns the bounding box of a 2d polygon.
func (s *polygon) BoundingBox() d2.Box {
func (s *polygon) Bounds() r2.Box {
return s.bb
}

Expand Down
4 changes: 2 additions & 2 deletions form3/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ func Cone(height, r0, r1, round float64) (s sdf.SDF3, err error) {
// ChamferedCylinder intersects a chamfered cylinder with an SDF3.
func ChamferedCylinder(s sdf.SDF3, kb, kt float64) (sdf.SDF3, error) {
// get the length and radius from the bounding box
l := s.BoundingBox().Max.Z
r := s.BoundingBox().Max.X
l := s.Bounds().Max.Z
r := s.Bounds().Max.X
p := form2.NewPolygon()
p.Add(0, -l)
p.Add(r, -l).Chamfer(r * kb)
Expand Down
28 changes: 14 additions & 14 deletions form3/must3/cylinders.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
type box struct {
size r3.Vec
round float64
bb d3.Box
bb r3.Box
}

// Box return an SDF3 for a 3d box (rounded corners with round > 0).
Expand All @@ -30,7 +30,7 @@ func Box(size r3.Vec, round float64) *box {
s := box{
size: r3.Sub(size, d3.Elem(round)),
round: round,
bb: d3.Box{Min: r3.Scale(-1, size), Max: size},
bb: r3.Box{Min: r3.Scale(-1, size), Max: size},
}
return &s
}
Expand All @@ -41,7 +41,7 @@ func (s *box) Evaluate(p r3.Vec) float64 {
}

// BoundingBox returns the bounding box for a 3d box.
func (s *box) BoundingBox() d3.Box {
func (s *box) Bounds() r3.Box {
return s.bb
}

Expand All @@ -50,7 +50,7 @@ func (s *box) BoundingBox() d3.Box {
// sphere is a sphere.
type sphere struct {
radius float64
bb d3.Box
bb r3.Box
}

// Sphere return an SDF3 for a sphere.
Expand All @@ -61,7 +61,7 @@ func Sphere(radius float64) *sphere {
d := r3.Vec{radius, radius, radius}
s := sphere{
radius: radius,
bb: d3.Box{Min: r3.Scale(-1, d), Max: d},
bb: r3.Box{Min: r3.Scale(-1, d), Max: d},
}
return &s
}
Expand All @@ -72,7 +72,7 @@ func (s *sphere) Evaluate(p r3.Vec) float64 {
}

// BoundingBox returns the bounding box for a sphere.
func (s *sphere) BoundingBox() d3.Box {
func (s *sphere) Bounds() r3.Box {
return s.bb
}

Expand All @@ -83,7 +83,7 @@ type cylinder struct {
height float64
radius float64
round float64
bb d3.Box
bb r3.Box
}

// Cylinder return an SDF3 for a cylinder (rounded edges with round > 0).
Expand All @@ -105,7 +105,7 @@ func Cylinder(height, radius, round float64) *cylinder {
s.radius = radius - round
s.round = round
d := r3.Vec{radius, radius, height / 2}
s.bb = d3.Box{r3.Scale(-1, d), d}
s.bb = r3.Box{r3.Scale(-1, d), d}
return &s
}

Expand All @@ -121,7 +121,7 @@ func (s *cylinder) Evaluate(p r3.Vec) float64 {
}

// BoundingBox returns the bounding box for a cylinder.
func (s *cylinder) BoundingBox() d3.Box {
func (s *cylinder) Bounds() r3.Box {
return s.bb
}

Expand All @@ -136,7 +136,7 @@ type cone struct {
u r2.Vec // normalized cone slope vector
n r2.Vec // normal to cone slope (points outward)
l float64 // length of cone slope
bb d3.Box // bounding box
bb r3.Box // bounding box
}

// Cone returns the SDF3 for a trucated cone (round > 0 gives rounded edges).
Expand Down Expand Up @@ -164,7 +164,7 @@ func Cone(height, r0, r1, round float64) *cone {
s.l = r2.Norm(r2.Vec{s.r1, s.height}.Sub(r2.Vec{s.r0, -s.height}))
// work out the bounding box
r := math.Max(s.r0+round, s.r1+round)
s.bb = d3.Box{r3.Vec{-r, -r, -height / 2}, r3.Vec{r, r, height / 2}}
s.bb = r3.Box{r3.Vec{-r, -r, -height / 2}, r3.Vec{r, r, height / 2}}
return &s
}

Expand Down Expand Up @@ -201,7 +201,7 @@ func (s *cone) Evaluate(p r3.Vec) float64 {
}

// BoundingBox return the bounding box for the trucated cone..
func (s *cone) BoundingBox() d3.Box {
func (s *cone) Bounds() r3.Box {
return s.bb
}

Expand Down Expand Up @@ -234,8 +234,8 @@ func sdfBox3d(p, s r3.Vec) float64 {
// ChamferedCylinder intersects a chamfered cylinder with an SDF3.
func ChamferedCylinder(s sdf.SDF3, kb, kt float64) sdf.SDF3 {
// get the length and radius from the bounding box
l := s.BoundingBox().Max.Z
r := s.BoundingBox().Max.X
l := s.Bounds().Max.Z
r := s.Bounds().Max.X
p := form2.NewPolygon()
p.Add(0, -l)
p.Add(r, -l).Chamfer(r * kb)
Expand Down
9 changes: 4 additions & 5 deletions form3/must3/screw.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"math"

"github.com/soypat/sdf"
"github.com/soypat/sdf/internal/d3"
"gonum.org/v1/gonum/spatial/r2"
"gonum.org/v1/gonum/spatial/r3"
)
Expand All @@ -17,7 +16,7 @@ type screw struct {
length float64 // total length of screw
taper float64 // thread taper angle
// starts int // number of thread starts
bb d3.Box // bounding box
bb r3.Box // bounding box
}

// Screw returns a screw SDF3.
Expand Down Expand Up @@ -49,11 +48,11 @@ func Screw(thread sdf.SDF2, length float64, taper float64, pitch float64, starts
s.lead = -pitch * float64(starts)
// Work out the bounding box.
// The max-y axis of the sdf2 bounding box is the radius of the thread.
bb := s.thread.BoundingBox()
bb := s.thread.Bounds()
r := bb.Max.Y
// add the taper increment
r += s.length * math.Tan(taper)
s.bb = d3.Box{r3.Vec{X: -r, Y: -r, Z: -s.length}, r3.Vec{X: r, Y: r, Z: s.length}}
s.bb = r3.Box{r3.Vec{X: -r, Y: -r, Z: -s.length}, r3.Vec{X: r, Y: r, Z: s.length}}
return &s
}

Expand All @@ -80,6 +79,6 @@ func (s *screw) Evaluate(p r3.Vec) float64 {
}

// BoundingBox returns the bounding box for a 3d screw form.
func (s *screw) BoundingBox() d3.Box {
func (s *screw) Bounds() r3.Box {
return s.bb
}
4 changes: 1 addition & 3 deletions internal/d2/box.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import (
)

// Box is a 2d bounding box.
type Box struct {
Min, Max r2.Vec
}
type Box r2.Box

// NewBox2 creates a 2d box with a given center and size.
func NewBox2(center, size r2.Vec) Box {
Expand Down
4 changes: 1 addition & 3 deletions internal/d3/box.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import (
)

// d3.Box is a 3d bounding box.
type Box struct {
Min, Max r3.Vec
}
type Box r3.Box

// Newd3.Box creates a 3d box with a given center and size.
func NewBox(center, size r3.Vec) Box {
Expand Down
Loading