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

Commit

Permalink
Merge pull request #6 from soypat/gonum-box
Browse files Browse the repository at this point in the history
refactor Triangle3 data type
  • Loading branch information
soypat authored May 12, 2022
2 parents 6370cb4 + 6e886af commit 5df8829
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 58 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ require (
golang.org/x/image v0.0.0-20220302094943-723b81ca9867 // indirect
rsc.io/pdf v0.1.1 // indirect
)

2 changes: 1 addition & 1 deletion render/3mf.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (m *model) AddObject(name string, src []Triangle3) error {
vertices := make([]mf3Vertex, 0, len(src)*3)
for it, t := range src {
for i := 0; i < 3; i++ {
vertex := t.V[i]
vertex := t[i]
entry, ok := vertexMap[vertex]
if !ok {
entry = vertexEntry{
Expand Down
6 changes: 3 additions & 3 deletions render/internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ func TestSTLWriteReadback(t *testing.T) {
if got.Degenerate(1e-12) {
t.Fatalf("triangle degenerate: %+v", got)
}
for i := range expect.V {
if !d3.EqualWithin(got.V[i], expect.V[i], rtol) {
for i := range expect {
if !d3.EqualWithin(got[i], expect[i], rtol) {
mismatches++
t.Errorf("%dth triangle equality out of tolerance. got vertex %0.5g, want %0.5g", iface, got.V[i], expect.V[i])
t.Errorf("%dth triangle equality out of tolerance. got vertex %0.5g, want %0.5g", iface, got[i], expect[i])
}
}
if mismatches > 10 {
Expand Down
40 changes: 19 additions & 21 deletions render/kdrender.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ func (s kdSDF) Evaluate(v r3.Vec) float64 {
// Find closest vertex
closest := r3.Vec{}
for i := 0; i < 3; i++ {
vDist := r3.Norm(r3.Sub(v, triangle.V[i]))
vDist := r3.Norm(r3.Sub(v, triangle[i]))
if vDist < minDist {
closest = triangle.V[i]
closest = triangle[i]
minDist = vDist
}
}
Expand All @@ -65,9 +65,7 @@ func (s kdSDF) Evaluate(v r3.Vec) float64 {

// Get nearest triangle to point.
func (s kdSDF) Nearest(v r3.Vec) kdTriangle {
got, _ := s.tree.Nearest(kdTriangle{
V: [3]r3.Vec{v, v, v},
})
got, _ := s.tree.Nearest(kdTriangle{v, v, v})
// do some ad-hoc math with the triangle normal ????
return got.(kdTriangle)
}
Expand All @@ -80,8 +78,8 @@ func (s kdSDF) Bounds() r3.Box {
tMin := bb.Min.(kdTriangle)
tMax := bb.Max.(kdTriangle)
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])),
Min: d3.MinElem(tMin[2], d3.MinElem(tMin[0], tMin[1])),
Max: d3.MaxElem(tMax[2], d3.MaxElem(tMax[0], tMax[1])),
}
}

Expand Down Expand Up @@ -116,12 +114,12 @@ func (k kdTriangles) Bounds() *kdtree.Bounding {
tbounds := tri.Bounds()
tmin := tbounds.Min.(kdTriangle)
tmax := tbounds.Max.(kdTriangle)
min = d3.MinElem(min, tmin.V[0])
max = d3.MaxElem(max, tmax.V[0])
min = d3.MinElem(min, tmin[0])
max = d3.MaxElem(max, tmax[0])
}
return &kdtree.Bounding{
Min: kdTriangle{V: [3]r3.Vec{min, min, min}},
Max: kdTriangle{V: [3]r3.Vec{max, max, max}},
Min: kdTriangle{min, min, min},
Max: kdTriangle{max, max, max},
}
}

Expand All @@ -146,11 +144,11 @@ func (a kdTriangle) Distance(b kdtree.Comparable) float64 {
}

func (a kdTriangle) Bounds() *kdtree.Bounding {
min := d3.MinElem(a.V[2], d3.MinElem(a.V[0], a.V[1]))
max := d3.MaxElem(a.V[2], d3.MaxElem(a.V[0], a.V[1]))
min := d3.MinElem(a[2], d3.MinElem(a[0], a[1]))
max := d3.MaxElem(a[2], d3.MaxElem(a[0], a[1]))
return &kdtree.Bounding{
Min: kdTriangle{V: [3]r3.Vec{min, min, min}},
Max: kdTriangle{V: [3]r3.Vec{max, max, max}},
Min: kdTriangle{min, min, min},
Max: kdTriangle{max, max, max},
}
}

Expand All @@ -163,11 +161,11 @@ func (a kdTriangle) Normal() r3.Vec {
func kdComp(a, b kdTriangle, dim int) (c float64) {
switch dim {
case 0:
c = (a.V[0].X + a.V[1].X + a.V[2].X) - (b.V[0].X + b.V[1].X + b.V[2].X)
c = (a[0].X + a[1].X + a[2].X) - (b[0].X + b[1].X + b[2].X)
case 1:
c = (a.V[0].Y + a.V[1].Y + a.V[2].Y) - (b.V[0].Y + b.V[1].Y + b.V[2].Y)
c = (a[0].Y + a[1].Y + a[2].Y) - (b[0].Y + b[1].Y + b[2].Y)
case 2:
c = (a.V[0].Z + a.V[1].Z + a.V[2].Z) - (b.V[0].Z + b.V[1].Z + b.V[2].Z)
c = (a[0].Z + a[1].Z + a[2].Z) - (b[0].Z + b[1].Z + b[2].Z)
}
return c / 3
}
Expand All @@ -181,9 +179,9 @@ func kdDist(a, b kdTriangle) (c float64) {

func kdCentroid(a kdTriangle) r3.Vec {
v := r3.Vec{
X: a.V[0].X + a.V[1].X + a.V[2].X,
Y: a.V[0].Y + a.V[1].Y + a.V[2].Y,
Z: a.V[0].Z + a.V[1].Z + a.V[2].Z,
X: a[0].X + a[1].X + a[2].X,
Y: a[0].Y + a[1].Y + a[2].Y,
Z: a[0].Z + a[1].Z + a[2].Z,
}
return r3.Scale(1./3., v)
}
Expand Down
8 changes: 3 additions & 5 deletions render/marchingcubes.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,9 @@ func mcToTriangles(dst []Triangle3, p [8]r3.Vec, v [8]float64, x float64) (n int
count := len(table) / 3 // max count is 5, a.k.a marchingCubesMaxTriangles
for i := 0; i < count; i++ {
t := Triangle3{
V: [3]r3.Vec{
points[table[i*3+2]],
points[table[i*3+1]],
points[table[i*3+0]],
},
points[table[i*3+2]],
points[table[i*3+1]],
points[table[i*3+0]],
}
if !t.Degenerate(1e-12) {
dst[n] = t
Expand Down
14 changes: 6 additions & 8 deletions render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ type Renderer interface {
type Triangle2 [3]r2.Vec

// Triangle3 is a 3D triangle
type Triangle3 struct {
V [3]r3.Vec
}
type Triangle3 [3]r3.Vec

// Normal returns the normal vector to the plane defined by the 3D triangle.
func (t *Triangle3) Normal() r3.Vec {
e1 := t.V[1].Sub(t.V[0])
e2 := t.V[2].Sub(t.V[0])
e1 := t[1].Sub(t[0])
e2 := t[2].Sub(t[0])

return r3.Unit(r3.Cross(e1, e2))
}
Expand All @@ -30,7 +28,7 @@ func (t *Triangle3) Normal() r3.Vec {
func (t *Triangle3) Degenerate(tolerance float64) bool {
// check for identical vertices.
// TODO more tests needed.
return d3.EqualWithin(t.V[0], t.V[1], tolerance) ||
d3.EqualWithin(t.V[1], t.V[2], tolerance) ||
d3.EqualWithin(t.V[2], t.V[0], tolerance)
return d3.EqualWithin(t[0], t[1], tolerance) ||
d3.EqualWithin(t[1], t[2], tolerance) ||
d3.EqualWithin(t[2], t[0], tolerance)
}
40 changes: 20 additions & 20 deletions render/stl.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ func WriteSTL(w io.Writer, model []Triangle3) error {
d.Normal[0] = float32(n.X)
d.Normal[1] = float32(n.Y)
d.Normal[2] = float32(n.Z)
d.Vertex1[0] = float32(triangle.V[0].X)
d.Vertex1[1] = float32(triangle.V[0].Y)
d.Vertex1[2] = float32(triangle.V[0].Z)
d.Vertex2[0] = float32(triangle.V[1].X)
d.Vertex2[1] = float32(triangle.V[1].Y)
d.Vertex2[2] = float32(triangle.V[1].Z)
d.Vertex3[0] = float32(triangle.V[2].X)
d.Vertex3[1] = float32(triangle.V[2].Y)
d.Vertex3[2] = float32(triangle.V[2].Z)
d.Vertex1[0] = float32(triangle[0].X)
d.Vertex1[1] = float32(triangle[0].Y)
d.Vertex1[2] = float32(triangle[0].Z)
d.Vertex2[0] = float32(triangle[1].X)
d.Vertex2[1] = float32(triangle[1].Y)
d.Vertex2[2] = float32(triangle[1].Z)
d.Vertex3[0] = float32(triangle[2].X)
d.Vertex3[1] = float32(triangle[2].Y)
d.Vertex3[2] = float32(triangle[2].Z)
d.put(b[:])
_, err := io.Copy(w, bytes.NewReader(b[:]))
if err != nil {
Expand Down Expand Up @@ -112,15 +112,15 @@ func (w *stlReader) Read(b []byte) (int, error) {
d.Normal[0] = float32(n.X)
d.Normal[1] = float32(n.Y)
d.Normal[2] = float32(n.Z)
d.Vertex1[0] = float32(triangle.V[0].X)
d.Vertex1[1] = float32(triangle.V[0].Y)
d.Vertex1[2] = float32(triangle.V[0].Z)
d.Vertex2[0] = float32(triangle.V[1].X)
d.Vertex2[1] = float32(triangle.V[1].Y)
d.Vertex2[2] = float32(triangle.V[1].Z)
d.Vertex3[0] = float32(triangle.V[2].X)
d.Vertex3[1] = float32(triangle.V[2].Y)
d.Vertex3[2] = float32(triangle.V[2].Z)
d.Vertex1[0] = float32(triangle[0].X)
d.Vertex1[1] = float32(triangle[0].Y)
d.Vertex1[2] = float32(triangle[0].Z)
d.Vertex2[0] = float32(triangle[1].X)
d.Vertex2[1] = float32(triangle[1].Y)
d.Vertex2[2] = float32(triangle[1].Z)
d.Vertex3[0] = float32(triangle[2].X)
d.Vertex3[1] = float32(triangle[2].Y)
d.Vertex3[2] = float32(triangle[2].Z)
d.put(b[it*stlTriangleSize:])
it++
}
Expand Down Expand Up @@ -327,9 +327,9 @@ func equalWithin3F32(a, b [3]float32, tol float32) bool {
}

func (d stlTriangle) toTriangle3() Triangle3 {
return Triangle3{V: [3]r3.Vec{
return Triangle3{
r3From3F32(d.Vertex1),
r3From3F32(d.Vertex2),
r3From3F32(d.Vertex3),
}}
}
}

0 comments on commit 5df8829

Please sign in to comment.