Mercurial > gemma
view pkg/octree/contours.go @ 991:a301d240905f
Decoupled import job creation and job execution with a factory function.
This is needed for persistence purposes.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Mon, 22 Oct 2018 10:45:17 +0200 |
parents | 4a2ca0e20006 |
children | a244b18cb916 |
line wrap: on
line source
package octree import ( "runtime" "sync" ) type ContourResult struct { Height float64 Lines MultiLineStringZ } func DoContours(tree *Tree, step float64, store func(*ContourResult)) { results := make(chan *ContourResult) done := make(chan struct{}) jobs := make(chan float64) go func() { for { select { case r := <-results: store(r) case <-done: return } } }() var wg sync.WaitGroup for i, n := 0, runtime.NumCPU(); i < n; i++ { wg.Add(1) go processLevels(tree, jobs, results, &wg) } for h := tree.Min.Z; h <= tree.Max.Z; h += step { jobs <- h } close(jobs) wg.Wait() done <- struct{}{} } func processLevels( tree *Tree, jobs <-chan float64, results chan<- *ContourResult, wg *sync.WaitGroup, ) { defer wg.Done() for h := range jobs { var lines MultiLineStringZ tree.Horizontal(h, func(t *Triangle) { line := t.IntersectHorizontal(h) if len(line) > 1 { lines = append(lines, line) } }) lines = lines.Merge() results <- &ContourResult{h, lines} } }