comparison pkg/mesh/areas.go @ 4827:f4abfd0ee8ad remove-octree-debris

Renamed octree package to mesh as there is no octree any more.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 05 Nov 2019 14:30:22 +0100
parents pkg/octree/areas.go@a2f16bbcc846
children 181c2c05b12a
comparison
equal deleted inserted replaced
4826:ec5afada70ec 4827:f4abfd0ee8ad
1 // This is Free Software under GNU Affero General Public License v >= 3.0
2 // without warranty, see README.md and license for details.
3 //
4 // SPDX-License-Identifier: AGPL-3.0-or-later
5 // License-Filename: LICENSES/AGPL-3.0.txt
6 //
7 // Copyright (C) 2018 by via donau
8 // – Österreichische Wasserstraßen-Gesellschaft mbH
9 // Software engineering by Intevation GmbH
10 //
11 // Author(s):
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de>
13
14 package mesh
15
16 import (
17 "runtime"
18 "sync"
19
20 "gemma.intevation.de/gemma/pkg/common"
21 )
22
23 func GenerateRandomVertices(
24 n int,
25 min, max Vertex,
26 eval func(float64, float64) (float64, bool),
27 callback func([]Vertex),
28 ) {
29 var wg sync.WaitGroup
30
31 jobs := make(chan int)
32 out := make(chan []Vertex)
33 done := make(chan struct{})
34
35 cpus := runtime.NumCPU()
36
37 free := make(chan []Vertex, cpus)
38
39 for i := 0; i < cpus; i++ {
40 wg.Add(1)
41 go func() {
42 defer wg.Done()
43 xRange := common.Random(min.X, max.X)
44 yRange := common.Random(min.Y, max.Y)
45
46 for size := range jobs {
47 var vertices []Vertex
48 select {
49 case vertices = <-free:
50 default:
51 vertices = make([]Vertex, 0, 1000)
52 }
53 for len(vertices) < size {
54 x, y := xRange(), yRange()
55 if z, ok := eval(x, y); ok {
56 vertices = append(vertices, Vertex{X: x, Y: y, Z: z})
57 }
58 }
59 out <- vertices
60 }
61 }()
62 }
63
64 go func() {
65 defer close(done)
66 for vertices := range out {
67 callback(vertices)
68 select {
69 case free <- vertices[:0]:
70 default:
71 }
72 }
73 }()
74
75 for remain := n; remain > 0; {
76 if remain > 1000 {
77 jobs <- 1000
78 remain -= 1000
79 } else {
80 jobs <- remain
81 remain = 0
82 }
83 }
84 close(jobs)
85 wg.Wait()
86 close(out)
87 <-done
88 }