From 6bdb6a5ea11167c20408416a46e556000c51a180 Mon Sep 17 00:00:00 2001 From: soypat Date: Sun, 15 May 2022 16:36:30 -0300 Subject: [PATCH] refactor sdf package- hide api footprint --- examples/npt-flange/flange.go | 2 +- matrix.go | 52 +++++++++++++---------------------- sdf2.go | 6 ++-- sdf3.go | 4 +-- 4 files changed, 25 insertions(+), 39 deletions(-) diff --git a/examples/npt-flange/flange.go b/examples/npt-flange/flange.go index 2328a9f..e5b8a1c 100644 --- a/examples/npt-flange/flange.go +++ b/examples/npt-flange/flange.go @@ -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) diff --git a/matrix.go b/matrix.go index f17b1f9..3bb1566 100644 --- a/matrix.go +++ b/matrix.go @@ -81,8 +81,8 @@ 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, @@ -90,16 +90,16 @@ func Identity3d() m44 { 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} @@ -122,9 +122,9 @@ 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, @@ -132,17 +132,17 @@ func Scale3d(v r3.Vec) m44 { 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 @@ -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. @@ -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{ @@ -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{} @@ -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)? diff --git a/sdf2.go b/sdf2.go index a8795a7..b43c39b 100644 --- a/sdf2.go +++ b/sdf2.go @@ -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, @@ -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 @@ -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)) diff --git a/sdf3.go b/sdf3.go index 5cacef9..e4638dc 100644 --- a/sdf3.go +++ b/sdf3.go @@ -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, @@ -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))