Mercurial > gemma
view cmd/octree2contour/main.go @ 726:5af9ab39e715
Renamed a few types to uppercase names to prepare the move to the octree package.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Sat, 22 Sep 2018 21:34:12 +0200 |
parents | a9783d8f74ed |
children | 41c8dc61f38f |
line wrap: on
line source
package main import ( "flag" "log" "runtime" "sort" "sync" "time" ) var ( one = flag.Bool("o", false, "create only a single contour") step = flag.Float64("s", 0.5, "step with") max = flag.Float64("m", 10, "max height from lowest point") ) func processLevels( tree *Octree, jobs <-chan float64, results chan<- result, 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) } }) results <- result{h, lines} } } func process(tree *Octree) []result { if *one { var lines MultiLineStringZ tree.Horizontal(*step, func(t *Triangle) { line := t.IntersectHorizontal(*step) if len(line) > 0 { lines = append(lines, line) } }) return []result{{*step, lines}} } results := make(chan result) done := make(chan struct{}) var all []result go func() { for { select { case r := <-results: all = append(all, r) case <-done: return } } }() var wg sync.WaitGroup jobs := make(chan float64) 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{}{} sort.Slice(all, func(i, j int) bool { return all[i].h < all[j].h }) return all } func main() { flag.Parse() for _, fname := range flag.Args() { log.Printf("processing %s\n", fname) start := time.Now() tree, err := LoadOctree(fname) if err != nil { log.Printf("error: %v\n", err) continue } log.Printf("loading took: %v\n", time.Since(start)) start = time.Now() all := process(tree) log.Printf("processing took: %v\n", time.Since(start)) start = time.Now() if err = store(all, tree.EPSG); err != nil { log.Printf("error: %v\n", err) } log.Printf("storing took: %v\n", time.Since(start)) } }