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

Commit

Permalink
refactor sdf package- hide api footprint
Browse files Browse the repository at this point in the history
  • Loading branch information
soypat committed May 15, 2022
1 parent eec0546 commit 6bdb6a5
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 39 deletions.
2 changes: 1 addition & 1 deletion examples/npt-flange/flange.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func main() {
panic(err)
}
// PLA scaling to thread
pipe = sdf.Transform3D(pipe, sdf.Scale3d(r3.Vec{plaScale, plaScale, 1}))
pipe = sdf.Transform3D(pipe, sdf.Scale3D(r3.Vec{plaScale, plaScale, 1}))
flange = form3.Cylinder(flangeH, flangeD/2, flangeH/8)
flange = sdf.Transform3D(flange, sdf.Translate3D(r3.Vec{0, 0, -tlen / 2}))
union := sdf.Union3D(pipe, flange)
Expand Down
52 changes: 19 additions & 33 deletions matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,25 +81,25 @@ func randomM44(a, b float64) m44 {
return m
}

// Identity3d returns a 4x4 identity matrix.
func Identity3d() m44 {
// identity3d returns a 4x4 identity matrix.
func identity3d() m44 {
return m44{
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1}
}

// Identity2d returns a 3x3 identity matrix.
func Identity2d() m33 {
// identity2d returns a 3x3 identity matrix.
func identity2d() m33 {
return m33{
1, 0, 0,
0, 1, 0,
0, 0, 1}
}

// Identity returns a 2x2 identity matrix.
func Identity() m22 {
// identity returns a 2x2 identity matrix.
func identity() m22 {
return m22{
1, 0,
0, 1}
Expand All @@ -122,27 +122,27 @@ func Translate2D(v r2.Vec) m33 {
0, 0, 1}
}

// Scale3d returns a 4x4 scaling matrix.
// Scale3D returns a 4x4 scaling matrix.
// Scaling does not preserve distance. See: ScaleUniform3D()
func Scale3d(v r3.Vec) m44 {
func Scale3D(v r3.Vec) m44 {
return m44{
v.X, 0, 0, 0,
0, v.Y, 0, 0,
0, 0, v.Z, 0,
0, 0, 0, 1}
}

// Scale2d returns a 3x3 scaling matrix.
// Scale2D returns a 3x3 scaling matrix.
// Scaling does not preserve distance. See: ScaleUniform2D().
func Scale2d(v r2.Vec) m33 {
func Scale2D(v r2.Vec) m33 {
return m33{
v.X, 0, 0,
0, v.Y, 0,
0, 0, 1}
}

// Rotate3d returns an orthographic 4x4 rotation matrix (right hand rule).
func Rotate3d(v r3.Vec, a float64) m44 {
// Rotate3D returns an orthographic 4x4 rotation matrix (right hand rule).
func Rotate3D(v r3.Vec, a float64) m44 {
v = r3.Unit(v)
s, c := math.Sincos(a)
m := 1 - c
Expand All @@ -156,17 +156,17 @@ func Rotate3d(v r3.Vec, a float64) m44 {

// RotateX returns a 4x4 matrix with rotation about the X axis.
func RotateX(a float64) m44 {
return Rotate3d(r3.Vec{X: 1, Y: 0, Z: 0}, a)
return Rotate3D(r3.Vec{X: 1, Y: 0, Z: 0}, a)
}

// RotateY returns a 4x4 matrix with rotation about the Y axis.
func RotateY(a float64) m44 {
return Rotate3d(r3.Vec{X: 0, Y: 1, Z: 0}, a)
return Rotate3D(r3.Vec{X: 0, Y: 1, Z: 0}, a)
}

// RotateZ returns a 4x4 matrix with rotation about the Z axis.
func RotateZ(a float64) m44 {
return Rotate3d(r3.Vec{X: 0, Y: 0, Z: 1}, a)
return Rotate3D(r3.Vec{X: 0, Y: 0, Z: 1}, a)
}

// MirrorXY returns a 4x4 matrix with mirroring across the XY plane.
Expand Down Expand Up @@ -221,8 +221,8 @@ func MirrorY() m33 {
0, 0, 1}
}

// Rotate2d returns an orthographic 3x3 rotation matrix (right hand rule).
func Rotate2d(a float64) m33 {
// Rotate2D returns an orthographic 3x3 rotation matrix (right hand rule).
func Rotate2D(a float64) m33 {
s := math.Sin(a)
c := math.Cos(a)
return m33{
Expand Down Expand Up @@ -294,20 +294,6 @@ func (a m22) MulPosition(b r2.Vec) r2.Vec {
Y: a.x10*b.X + a.x11*b.Y}
}

// MulVertices multiples a set of V2 vertices by a rotate/translate matrix.
func MulVertices2(v d2.Set, a m33) {
for i := range v {
v[i] = a.MulPosition(v[i])
}
}

// MulVertices multiples a set of r3.Vec vertices by a rotate/translate matrix.
func MulVertices3(v d3.Set, a m44) {
for i := range v {
v[i] = a.MulPosition(v[i])
}
}

// Mul multiplies 4x4 matrices.
func (a m44) Mul(b m44) m44 {
m := m44{}
Expand Down Expand Up @@ -515,14 +501,14 @@ func (a m22) Inverse() m22 {
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()
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()
return identity3d()
}

// are the vectors opposite (180 degrees apart)?
Expand Down
6 changes: 3 additions & 3 deletions sdf2.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ type ScaleUniformSDF2 struct {
// ScaleUniform2D scales an SDF2 by k on each axis.
// Distance is correct with scaling.
func ScaleUniform2D(sdf SDF2, k float64) SDF2 {
m := Scale2d(r2.Vec{X: k, Y: k})
m := Scale2D(r2.Vec{X: k, Y: k})
return &ScaleUniformSDF2{
sdf: sdf,
k: k,
Expand Down Expand Up @@ -241,7 +241,7 @@ func RotateUnion2D(sdf SDF2, num int, step m33) SDF2 {
bbMin = d2.MinElem(bbMin, vset.Min())
bbMin = d2.MinElem(bbMin, vset.Min())
bbMax = d2.MaxElem(bbMax, vset.Max())
MulVertices2(vset, step)
mulVertices2(vset, step)
}
s.bb = r2.Box{Min: bbMin, Max: bbMax}
return &s
Expand All @@ -250,7 +250,7 @@ func RotateUnion2D(sdf SDF2, num int, step m33) SDF2 {
// Evaluate returns the minimum distance to a union of rotated SDF2s.
func (s *rotateUnion2) Evaluate(p r2.Vec) float64 {
d := math.MaxFloat64
rot := Identity2d()
rot := identity2d()
for i := 0; i < s.num; i++ {
x := rot.MulPosition(p)
d = s.min(d, s.sdf.Evaluate(x))
Expand Down
4 changes: 2 additions & 2 deletions sdf3.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ type scaleUniform3 struct {

// ScaleUniform3D uniformly scales an SDF3 on all axes.
func ScaleUniform3D(sdf SDF3, k float64) SDF3 {
m := Scale3d(r3.Vec{X: k, Y: k, Z: k})
m := Scale3D(r3.Vec{X: k, Y: k, Z: k})
return &scaleUniform3{
sdf: sdf,
k: k,
Expand Down Expand Up @@ -688,7 +688,7 @@ func RotateUnion3D(sdf SDF3, num int, step m44) SDF3Union {
// Evaluate returns the minimum distance to a rotate/union object.
func (s *rotateUnion) Evaluate(p r3.Vec) float64 {
d := math.MaxFloat64
rot := Identity3d()
rot := identity3d()
for i := 0; i < s.num; i++ {
x := rot.MulPosition(p)
d = s.min(d, s.sdf.Evaluate(x))
Expand Down

0 comments on commit 6bdb6a5

Please sign in to comment.