view pkg/octree/contours.go @ 977:4a2ca0e20006

Fixed build error. Copied file to the wrong place and said 'go build' to another wrong place. Argh.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 18 Oct 2018 17:30:53 +0200
parents c397fdd8c327
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}
	}
}