annotate cmd/octree2contour/main.go @ 718:c0bba602b60e octree

octree: define a type for LineStringZ.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 21 Sep 2018 14:47:44 +0200
parents a9783d8f74ed
children 5af9ab39e715
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
674
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 package main
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
3 import (
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
4 "flag"
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
5 "log"
686
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
6 "runtime"
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
7 "sort"
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
8 "sync"
674
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9 "time"
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10 )
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
11
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
12 var (
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13 one = flag.Bool("o", false, "create only a single contour")
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14 step = flag.Float64("s", 0.5, "step with")
680
c79c7be29a7a octree: Small fix in horizontal traversal.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 678
diff changeset
15 max = flag.Float64("m", 10, "max height from lowest point")
674
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16 )
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
17
686
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
18 func processLevels(
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
19 tree *octree,
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
20 jobs <-chan float64,
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
21 results chan<- result,
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
22 wg *sync.WaitGroup,
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
23 ) {
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
24 defer wg.Done()
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
25 for h := range jobs {
694
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 689
diff changeset
26 var lines multiLineStringZ
687
be90ab542aa7 octree: contouring: Do the math to calculate the intersection points of the triangles and the planes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 686
diff changeset
27 tree.horizontal(h, func(t *triangle) {
689
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
28 line := t.intersectH(h)
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
29 if len(line) > 1 {
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
30 lines = append(lines, line)
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
31 }
686
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
32 })
689
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
33 results <- result{h, lines}
686
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
34 }
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
35 }
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
36
689
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
37 func process(tree *octree) []result {
686
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
38
689
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
39 if *one {
694
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 689
diff changeset
40 var lines multiLineStringZ
689
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
41 tree.horizontal(*step, func(t *triangle) {
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
42 line := t.intersectH(*step)
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
43 if len(line) > 0 {
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
44 lines = append(lines, line)
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
45 }
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
46 })
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
47 return []result{{*step, lines}}
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
48 }
686
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
49
689
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
50 results := make(chan result)
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
51 done := make(chan struct{})
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
52
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
53 var all []result
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
54 go func() {
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
55 for {
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
56 select {
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
57 case r := <-results:
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
58 all = append(all, r)
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
59 case <-done:
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
60 return
686
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
61 }
689
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
62 }
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
63 }()
686
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
64
689
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
65 var wg sync.WaitGroup
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
66 jobs := make(chan float64)
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
67 for i, n := 0, runtime.NumCPU(); i < n; i++ {
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
68 wg.Add(1)
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
69 go processLevels(tree, jobs, results, &wg)
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
70 }
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
71 for h := tree.min.z; h <= tree.max.z; h += *step {
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
72 jobs <- h
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
73 }
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
74 close(jobs)
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
75 wg.Wait()
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
76 done <- struct{}{}
686
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
77
689
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
78 sort.Slice(all, func(i, j int) bool {
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
79 return all[i].h < all[j].h
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
80 })
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
81
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
82 return all
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
83 }
686
a2f107f1e4e7 octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 681
diff changeset
84
674
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
85 func main() {
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
86 flag.Parse()
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
87
694
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 689
diff changeset
88 for _, fname := range flag.Args() {
674
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
89 log.Printf("processing %s\n", fname)
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
90 start := time.Now()
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
91 tree, err := loadOctree(fname)
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
92 if err != nil {
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
93 log.Printf("error: %v\n", err)
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
94 continue
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
95 }
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
96 log.Printf("loading took: %v\n", time.Since(start))
689
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
97 start = time.Now()
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
98 all := process(tree)
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
99 log.Printf("processing took: %v\n", time.Since(start))
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
100 start = time.Now()
694
a9783d8f74ed octree: Store contour lines into postgres/postgis.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 689
diff changeset
101 if err = store(all, tree.epsg); err != nil {
689
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
102 log.Printf("error: %v\n", err)
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
103 }
614135d69823 octree: prepare storing the lines to file in contouring tool.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 687
diff changeset
104 log.Printf("storing took: %v\n", time.Since(start))
674
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
105 }
120a82bd9953 octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
106 }