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

Commit

Permalink
gofmt -w -r dn.Box -> rn.Box
Browse files Browse the repository at this point in the history
  • Loading branch information
soypat committed May 12, 2022
1 parent cd28906 commit 64fa18b
Show file tree
Hide file tree
Showing 14 changed files with 166 additions and 161 deletions.
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) BoundingBox() 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) BoundingBox() 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) BoundingBox() 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) BoundingBox() r2.Box {
return s.bb
}

Expand Down
24 changes: 12 additions & 12 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) BoundingBox() 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) BoundingBox() 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) BoundingBox() 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) BoundingBox() r3.Box {
return s.bb
}

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) BoundingBox() 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
8 changes: 4 additions & 4 deletions matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ func (a m44) MulPosition(b r3.Vec) r3.Vec {
}

// MulBox rotates/translates a 3d bounding box and resizes for axis-alignment.
func (a m44) MulBox(box d3.Box) d3.Box {
func (a m44) MulBox(box r3.Box) r3.Box {
r := r3.Vec{X: a.x00, Y: a.x10, Z: a.x20}
u := r3.Vec{X: a.x01, Y: a.x11, Z: a.x21}
b := r3.Vec{X: a.x02, Y: a.x12, Z: a.x22}
Expand All @@ -414,11 +414,11 @@ func (a m44) MulBox(box d3.Box) d3.Box {
za, zb = d3.MinElem(za, zb), d3.MaxElem(za, zb)
min := xa.Add(ya).Add(za).Add(t)
max := xb.Add(yb).Add(zb).Add(t)
return d3.Box{min, max}
return r3.Box{min, max}
}

// MulBox rotates/translates a 2d bounding box and resizes for axis-alignment.
func (a m33) MulBox(box d2.Box) d2.Box {
func (a m33) MulBox(box r2.Box) r2.Box {
r := r2.Vec{a.x00, a.x10}
u := r2.Vec{a.x01, a.x11}
t := r2.Vec{a.x02, a.x12}
Expand All @@ -430,7 +430,7 @@ func (a m33) MulBox(box d2.Box) d2.Box {
ya, yb = d2.MinElem(ya, yb), d2.MaxElem(ya, yb)
min := xa.Add(ya).Add(t)
max := xb.Add(yb).Add(t)
return d2.Box{min, max}
return r2.Box{min, max}
}

// Determinant returns the determinant of a 4x4 matrix.
Expand Down
2 changes: 1 addition & 1 deletion render/internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestSTLWriteReadback(t *testing.T) {
TotalLength: 40.,
ShankLength: 10.0,
})
size := r3.Norm(s0.BoundingBox().Size())
size := r3.Norm(d3.Box(s0.BoundingBox()).Size())
// calculate relative tolerance
rtol := tol * size / quality
input, err := RenderAll(NewOctreeRenderer(s0, quality))
Expand Down
4 changes: 2 additions & 2 deletions render/kdrender.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ func (s kdSDF) Nearest(v r3.Vec) kdTriangle {
return got.(kdTriangle)
}

func (s kdSDF) BoundingBox() d3.Box {
func (s kdSDF) BoundingBox() r3.Box {
bb := s.tree.Root.Bounding
if bb == nil {
panic("got nil bounding box?")
}
tMin := bb.Min.(kdTriangle)
tMax := bb.Max.(kdTriangle)
return d3.Box{
return r3.Box{
Min: d3.MinElem(tMin.V[2], d3.MinElem(tMin.V[0], tMin.V[1])),
Max: d3.MaxElem(tMax.V[2], d3.MaxElem(tMax.V[0], tMax.V[1])),
}
Expand Down
2 changes: 1 addition & 1 deletion render/octree_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func NewOctreeRenderer(s sdf.SDF3, meshCells int) *octree {
}
// Scale the bounding box about the center to make sure the boundaries
// aren't on the object surface.
bb := s.BoundingBox()
bb := d3.Box(s.BoundingBox())
bb = bb.ScaleAboutCenter(1.01)
longAxis := d3.Max(bb.Size())
// We want to test the smallest cube (side == resolution) for emptiness
Expand Down
Loading

0 comments on commit 64fa18b

Please sign in to comment.