changeset 689:614135d69823 octree

octree: prepare storing the lines to file in contouring tool.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 20 Sep 2018 13:19:49 +0200
parents be90ab542aa7
children a9783d8f74ed
files cmd/octree2contour/main.go cmd/octree2contour/vertex.go
diffstat 2 files changed, 76 insertions(+), 53 deletions(-) [+]
line wrap: on
line diff
--- a/cmd/octree2contour/main.go	Thu Sep 20 11:32:03 2018 +0200
+++ b/cmd/octree2contour/main.go	Thu Sep 20 13:19:49 2018 +0200
@@ -16,8 +16,8 @@
 )
 
 type result struct {
-	h         float64
-	numPoints int
+	h     float64
+	lines [][]vertex
 }
 
 func processLevels(
@@ -28,72 +28,80 @@
 ) {
 	defer wg.Done()
 	for h := range jobs {
-		var points []vertex
+		var lines [][]vertex
 		tree.horizontal(h, func(t *triangle) {
-			points = t.intersectH(h, points)
+			line := t.intersectH(h)
+			if len(line) > 1 {
+				lines = append(lines, line)
+			}
 		})
-		results <- result{h, len(points)}
+		results <- result{h, lines}
 	}
 }
 
-func process(tree *octree) {
-	var numPoints int
-	start := time.Now()
-	if *one {
-		var points []vertex
-		tree.horizontal(*step, func(t *triangle) {
-			points = t.intersectH(*step, points)
-		})
-		numPoints = len(points)
-	} else {
+func process(tree *octree) []result {
 
-		results := make(chan result)
-		done := make(chan struct{})
-
-		var all []result
+	if *one {
+		var lines [][]vertex
+		tree.horizontal(*step, func(t *triangle) {
+			line := t.intersectH(*step)
+			if len(line) > 0 {
+				lines = append(lines, line)
+			}
+		})
+		return []result{{*step, lines}}
+	}
 
-		go func() {
-			for {
-				select {
-				case r := <-results:
-					all = append(all, r)
-				case <-done:
-					return
-				}
+	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{}{}
+	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
-		})
+	sort.Slice(all, func(i, j int) bool {
+		return all[i].h < all[j].h
+	})
+
+	return all
+}
 
-		for i := range all {
-			a := &all[i]
-			numPoints += a.numPoints
-			log.Printf("level %f: %d\n", a.h, a.numPoints)
-		}
+func store(all []result, fname string) error {
+	for i := range all {
+		a := &all[i]
+		log.Printf("level %f: %d\n", a.h, len(a.lines))
 	}
-	log.Printf("num points: %d\n", numPoints)
-	log.Printf("processing took: %s\n", time.Since(start))
+	return nil
 }
 
 func main() {
 	flag.Parse()
 
-	for _, fname := range flag.Args() {
+	files := flag.Args()
+
+	for i := 0; i < len(files); i += 2 {
+		fname := files[i]
 		log.Printf("processing %s\n", fname)
 		start := time.Now()
 		tree, err := loadOctree(fname)
@@ -102,6 +110,19 @@
 			continue
 		}
 		log.Printf("loading took: %v\n", time.Since(start))
-		process(tree)
+		start = time.Now()
+		all := process(tree)
+		log.Printf("processing took: %v\n", time.Since(start))
+		var outname string
+		if i+1 < len(files) {
+			outname = files[i+1]
+		} else {
+			outname = "out.shp"
+		}
+		start = time.Now()
+		if err = store(all, outname); err != nil {
+			log.Printf("error: %v\n", err)
+		}
+		log.Printf("storing took: %v\n", time.Since(start))
 	}
 }
--- a/cmd/octree2contour/vertex.go	Thu Sep 20 11:32:03 2018 +0200
+++ b/cmd/octree2contour/vertex.go	Thu Sep 20 13:19:49 2018 +0200
@@ -99,13 +99,15 @@
 	return 0
 }
 
-func (t *triangle) intersectH(h float64, points []vertex) []vertex {
+func (t *triangle) intersectH(h float64) []vertex {
 	sides := [3]int{
 		side(t[0].z, h),
 		side(t[1].z, h),
 		side(t[2].z, h),
 	}
 
+	var points []vertex
+
 	for i := 0; i < 3; i++ {
 		j := (i + 1) % 3
 		si := sides[i]