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

Commit 615e52e

Browse files
committed
add a preliminary threaded octreee implementation
1 parent 6bdb6a5 commit 615e52e

File tree

4 files changed

+95
-18
lines changed

4 files changed

+95
-18
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
*.prof
99
*.pprof
1010

11+
*bin
12+
1113
*.png
1214
# Exclude all png files except for test files
1315
!render/testdata/defacto*.png

render/internal_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ package render
33
import (
44
"bytes"
55
"errors"
6+
"io"
7+
"os"
68
"testing"
79

10+
"github.com/soypat/sdf/form3/must3"
811
"github.com/soypat/sdf/form3/obj3/thread"
912
"github.com/soypat/sdf/internal/d3"
1013
"gonum.org/v1/gonum/spatial/r3"
@@ -71,3 +74,22 @@ func TestSTLWriteReadback(t *testing.T) {
7174
}
7275
}
7376
}
77+
78+
func TestOctreeMultithread(t *testing.T) {
79+
oct := NewOctreeRenderer(must3.Sphere(1), 8)
80+
oct.concurrent = 2
81+
buf := make([]Triangle3, oct.concurrent*10)
82+
var err error
83+
var nt int
84+
var model []Triangle3
85+
for err == nil {
86+
nt, err = oct.ReadTriangles(buf)
87+
model = append(model, buf[nt:]...)
88+
}
89+
if err != io.EOF {
90+
t.Fatal(err)
91+
}
92+
fp, _ := os.Create("mt.stl")
93+
defer fp.Close()
94+
WriteSTL(fp, model)
95+
}

render/marchingcubes.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import (
77
)
88

99
const (
10-
marchingCubesEpsilon = 1e-12
10+
marchingCubesEpsilon = 1e-12
11+
// max number of triangles that can be formed from a single cube.
1112
marchingCubesMaxTriangles = 5
1213
)
1314

render/octree_renderer.go

Lines changed: 69 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
// MarchingCubesOctree renders using marching cubes with octree space sampling.
1414
type octree struct {
1515
dc dc3
16+
mu sync.Mutex
1617
todo []cube
1718
unwritten triangle3Buffer
1819
// concurrent goroutine processing.
@@ -56,6 +57,7 @@ func NewOctreeRenderer(s sdf.SDF3, meshCells int) *octree {
5657
dc: *newDc3(s, bb.Min, resolution, levels),
5758
unwritten: triangle3Buffer{buf: make([]Triangle3, 0, 1024)},
5859
todo: cubes, //[]cube{{sdf.V3i{0, 0, 0}, levels - 1}}, // process the octree, start at the top level
60+
// concurrent: 2,
5961
}
6062
}
6163

@@ -75,50 +77,100 @@ func (oc *octree) ReadTriangles(dst []Triangle3) (n int, err error) {
7577
// Done rendering model.
7678
return n, io.EOF
7779
}
80+
// Number of additional triangles proccessed.
7881
var nt int
79-
if oc.concurrent <= 1 {
80-
nt = oc.readTriangles(dst[n:])
82+
if oc.concurrent <= 1 || len(oc.todo) < oc.concurrent || n < oc.concurrent {
83+
tproc, nc, newCubes := oc.readTriangles(dst[n:], oc.todo)
84+
oc.todo = append(oc.todo, newCubes...)
85+
oc.todo = oc.todo[nc:] // this leaks, luckily this is a short lived function?
86+
// oc.todo = append(newCubes, oc.todo[cubesProcessed:]...) // Non leaking slow implementation
87+
nt = tproc
8188
} else {
82-
// multi core processing
83-
panic("no concurrency yet")
89+
nt = oc.readTrianglesThreaded(dst[n:])
8490
}
8591
n += nt
8692
return n, err
8793
}
8894

89-
// readTriangles is single threaded implementation of ReadTriangles and only returns
90-
// number of triangles written.
91-
func (oc *octree) readTriangles(dst []Triangle3) (n int) {
92-
cubesProcessed := 0
93-
var newCubes []cube
94-
for _, cube := range oc.todo {
95+
// readTriangles is single threaded implementation of ReadTriangles.
96+
// todo is the slice of cubes that shall be proccessed. n is the number of triangles written to dst.
97+
// Returned cubesProcessed is the number of cubes of todo that were completely processed.
98+
// Returned newCubes are non-empty cubes that should be processed in future calls to readTriangles.
99+
// Triangles that were not succesfully written to dst are stored in octree unwritten buffer.
100+
// This function is safe to call concurrently.
101+
func (oc *octree) readTriangles(dst []Triangle3, todo []cube) (n, cubesProcessed int, newCubes []cube) {
102+
for _, cube := range todo {
95103
if n == len(dst) {
96104
// Finished writing all the buffer
97105
break
98106
}
99107
if n+marchingCubesMaxTriangles > len(dst) {
100108
// Not enough room in buffer to write all triangles that could be found by marching cubes.
101-
tmp := make([]Triangle3, 5)
109+
tmp := make([]Triangle3, marchingCubesMaxTriangles)
102110
tri, cubes := oc.processCube(tmp, cube)
111+
oc.mu.Lock()
103112
oc.unwritten.Write(tmp[:tri])
113+
oc.mu.Unlock()
104114
newCubes = append(newCubes, cubes...)
105115
cubesProcessed++
106116
break
107117
}
108118
tri, cubes := oc.processCube(dst[n:], cube)
109-
110119
newCubes = append(newCubes, cubes...)
111-
112120
cubesProcessed++
113121
n += tri
114122
}
115-
oc.todo = append(oc.todo, newCubes...)
116-
oc.todo = oc.todo[cubesProcessed:] // this leaks, luckily this is a short lived function?
117-
// oc.todo = append(newCubes, oc.todo[cubesProcessed:]...) // Non leaking slow implementation
118-
return n
123+
return n, cubesProcessed, newCubes
124+
}
125+
126+
// readTrianglesThreaded is a multithreaded triangle reader implementation for octree.
127+
func (oc *octree) readTrianglesThreaded(dst []Triangle3) (nt int) {
128+
var wg sync.WaitGroup
129+
div := len(dst) / oc.concurrent
130+
work := make([][]Triangle3, oc.concurrent)
131+
cubeWork := make([][]cube, oc.concurrent)
132+
newCubesC := make([][]cube, oc.concurrent)
133+
divC := len(oc.todo) / oc.concurrent
134+
for i := 0; i < oc.concurrent; i++ {
135+
i := i // Escape loop variable.
136+
wg.Add(1)
137+
go func() {
138+
start := div * i
139+
work[i] = dst[start : start+div]
140+
cubeWork[i] = oc.todo[i*divC : (i+1)*divC]
141+
ntc, nc, newC := oc.readTriangles(work[i], cubeWork[i])
142+
newCubesC[i] = newC
143+
work[i] = work[i][:ntc]
144+
cubeWork[i] = cubeWork[i][nc:]
145+
wg.Done()
146+
}()
147+
}
148+
wg.Wait()
149+
// Consolidate work done.
150+
offset := 0
151+
oc.todo = oc.todo[len(oc.todo):]
152+
for i := 0; i < oc.concurrent; i++ {
153+
// Triangles written.
154+
start := div*i - offset
155+
if i != oc.concurrent-1 && len(work[i]) != div {
156+
offset += div - len(work[i])
157+
copy(dst[start+len(work[i]):], dst[start+div:])
158+
}
159+
nt += len(work[i])
160+
// Cubes unprocessed.
161+
if len(cubeWork[i]) != 0 {
162+
oc.todo = append(oc.todo, cubeWork[i]...)
163+
}
164+
// New Cubes
165+
if len(newCubesC[i]) != 0 {
166+
oc.todo = append(oc.todo, newCubesC[i]...)
167+
}
168+
}
169+
return nt
119170
}
120171

121172
// Process a cube. Generate triangles, or more cubes.
173+
// Safe to call concurrently.
122174
func (oc *octree) processCube(dst []Triangle3, c cube) (writtenTriangles int, newCubes []cube) {
123175
if c.n == 1 {
124176
// this cube is at the required resolution

0 commit comments

Comments
 (0)