Mercurial > gemma
annotate cmd/octree2contour/main.go @ 687:be90ab542aa7 octree
octree: contouring: Do the math to calculate the intersection points of the triangles and the planes.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Thu, 20 Sep 2018 11:32:03 +0200 |
parents | a2f107f1e4e7 |
children | 614135d69823 |
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 type result struct { |
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
19 h float64 |
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
|
20 numPoints int |
686
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
21 } |
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
22 |
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
23 func processLevels( |
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
24 tree *octree, |
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
25 jobs <-chan float64, |
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
26 results chan<- result, |
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
27 wg *sync.WaitGroup, |
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
28 ) { |
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
29 defer wg.Done() |
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
30 for h := range jobs { |
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
|
31 var points []vertex |
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
|
32 tree.horizontal(h, func(t *triangle) { |
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
|
33 points = t.intersectH(h, points) |
686
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
34 }) |
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
|
35 results <- result{h, len(points)} |
686
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
36 } |
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
37 } |
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
38 |
674
120a82bd9953
octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
39 func process(tree *octree) { |
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
|
40 var numPoints int |
680
c79c7be29a7a
octree: Small fix in horizontal traversal.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
678
diff
changeset
|
41 start := time.Now() |
678
7bb961d750b6
octree: traverse horizontally over tree to find out which triangles to process.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
674
diff
changeset
|
42 if *one { |
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
|
43 var points []vertex |
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
|
44 tree.horizontal(*step, func(t *triangle) { |
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
|
45 points = t.intersectH(*step, points) |
678
7bb961d750b6
octree: traverse horizontally over tree to find out which triangles to process.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
674
diff
changeset
|
46 }) |
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
|
47 numPoints = len(points) |
678
7bb961d750b6
octree: traverse horizontally over tree to find out which triangles to process.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
674
diff
changeset
|
48 } else { |
686
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
49 |
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
50 results := make(chan result) |
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
51 done := make(chan struct{}) |
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
52 |
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
53 var all []result |
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
54 |
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
55 go func() { |
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
56 for { |
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
57 select { |
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
58 case r := <-results: |
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
59 all = append(all, r) |
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
60 case <-done: |
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
61 return |
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
62 } |
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
63 } |
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
64 }() |
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
65 |
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
66 var wg sync.WaitGroup |
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
67 jobs := make(chan float64) |
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
68 for i, n := 0, runtime.NumCPU(); i < n; i++ { |
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
69 wg.Add(1) |
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
70 go processLevels(tree, jobs, results, &wg) |
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
71 } |
680
c79c7be29a7a
octree: Small fix in horizontal traversal.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
678
diff
changeset
|
72 for h := tree.min.z; h <= tree.max.z; h += *step { |
686
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
73 jobs <- h |
680
c79c7be29a7a
octree: Small fix in horizontal traversal.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
678
diff
changeset
|
74 } |
686
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
75 close(jobs) |
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
76 wg.Wait() |
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
77 done <- struct{}{} |
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
78 |
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
79 sort.Slice(all, func(i, j int) bool { |
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
80 return all[i].h < all[j].h |
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
81 }) |
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
82 |
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
83 for i := range all { |
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
84 a := &all[i] |
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
|
85 numPoints += a.numPoints |
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
|
86 log.Printf("level %f: %d\n", a.h, a.numPoints) |
686
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
87 } |
678
7bb961d750b6
octree: traverse horizontally over tree to find out which triangles to process.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
674
diff
changeset
|
88 } |
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
|
89 log.Printf("num points: %d\n", numPoints) |
686
a2f107f1e4e7
octree: run each slice in contour tool concurrently.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
681
diff
changeset
|
90 log.Printf("processing took: %s\n", time.Since(start)) |
674
120a82bd9953
octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
91 } |
120a82bd9953
octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
92 |
120a82bd9953
octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
93 func main() { |
120a82bd9953
octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
94 flag.Parse() |
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 for _, fname := range flag.Args() { |
120a82bd9953
octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
97 log.Printf("processing %s\n", fname) |
120a82bd9953
octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
98 start := time.Now() |
120a82bd9953
octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
99 tree, err := loadOctree(fname) |
120a82bd9953
octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
100 if err != nil { |
120a82bd9953
octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
101 log.Printf("error: %v\n", err) |
120a82bd9953
octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
102 continue |
120a82bd9953
octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
103 } |
120a82bd9953
octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
104 log.Printf("loading took: %v\n", time.Since(start)) |
120a82bd9953
octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
105 process(tree) |
120a82bd9953
octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
106 } |
120a82bd9953
octree: Added loading frame for contour generation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
107 } |