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

Commit

Permalink
refactor SDF interface to completely use gonum types
Browse files Browse the repository at this point in the history
  • Loading branch information
soypat committed May 12, 2022
1 parent 64fa18b commit 9da0a15
Show file tree
Hide file tree
Showing 14 changed files with 143 additions and 145 deletions.
6 changes: 3 additions & 3 deletions form2/must2/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() r2.Box {
func (s *circle) Bounds() r2.Box {
return s.bb
}

Expand Down Expand Up @@ -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() r2.Box {
func (s *box) Bounds() r2.Box {
return s.bb
}

Expand Down Expand Up @@ -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() r2.Box {
func (s *line) Bounds() r2.Box {
return s.bb
}
2 changes: 1 addition & 1 deletion form2/must2/poly.go
Original file line number Diff line number Diff line change
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() r2.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
12 changes: 6 additions & 6 deletions form3/must3/cylinders.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() r3.Box {
func (s *box) Bounds() r3.Box {
return s.bb
}

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

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

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

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

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() r3.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
2 changes: 1 addition & 1 deletion form3/must3/screw.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,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() r3.Box {
func (s *screw) Bounds() r3.Box {
return s.bb
}
44 changes: 44 additions & 0 deletions matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,3 +510,47 @@ func (a m22) Inverse() m22 {
m.x11 = a.x00 * d
return m
}

// rotateToVector returns the rotation matrix that transforms a onto the same direction as b.
func rotateToVec(a, b r3.Vec) m44 {
// is either vector == 0?
if d3.EqualWithin(a, r3.Vec{}, epsilon) || d3.EqualWithin(b, r3.Vec{}, epsilon) {
return Identity3d()
}
// normalize both vectors
a = r3.Unit(a)
b = r3.Unit(b)
// are the vectors the same?
if d3.EqualWithin(a, b, epsilon) {
return Identity3d()
}

// are the vectors opposite (180 degrees apart)?
if d3.EqualWithin(r3.Scale(-1, a), b, epsilon) {
return m44{
-1, 0, 0, 0,
0, -1, 0, 0,
0, 0, -1, 0,
0, 0, 0, 1,
}
}
// general case
// See: https://math.stackexchange.com/questions/180418/calculate-rotation-matrix-to-align-vector-a-to-vector-b-in-3d
v := r3.Cross(a, b)
vx := r3.Skew(v)

k := 1 / (1 + r3.Dot(a, b))
vx2 := r3.NewMat(nil)
vx2.Mul(vx, vx)
vx2.Scale(k, vx2)

// Calculate sum of matrices.
vx.Add(vx, r3.Eye())
vx.Add(vx, vx2)
return m44{
vx.At(0, 0), vx.At(0, 1), vx.At(0, 2), 0,
vx.At(1, 0), vx.At(1, 1), vx.At(1, 2), 0,
vx.At(2, 0), vx.At(2, 1), vx.At(2, 2), 0,
0, 0, 0, 1,
}
}
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(d3.Box(s0.BoundingBox()).Size())
size := r3.Norm(d3.Box(s0.Bounds()).Size())
// calculate relative tolerance
rtol := tol * size / quality
input, err := RenderAll(NewOctreeRenderer(s0, quality))
Expand Down
2 changes: 1 addition & 1 deletion render/kdrender.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (s kdSDF) Nearest(v r3.Vec) kdTriangle {
return got.(kdTriangle)
}

func (s kdSDF) BoundingBox() r3.Box {
func (s kdSDF) Bounds() r3.Box {
bb := s.tree.Root.Bounding
if bb == nil {
panic("got nil bounding box?")
Expand Down
2 changes: 1 addition & 1 deletion render/kdrender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestKDSDF(t *testing.T) {
}
t.Error(len(model), "triangles")
kdf := render.NewKDSDF(model)
t.Error(kdf.BoundingBox())
t.Error(kdf.Bounds())
start := time.Now()
outside := kdf.Evaluate(r3.Vec{2, 0, 0}) // evaluate point outside bounds
inside := kdf.Evaluate(r3.Vec{0, 0, 0}) // evaluate point inside bounds
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 := d3.Box(s.BoundingBox())
bb := d3.Box(s.Bounds())
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 9da0a15

Please sign in to comment.