changeset 3775:33fa76994b8a

Fixed simplification a bit.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 02 Jul 2019 11:43:12 +0200
parents bb62c98fcf05
children 6521c962a7b6
files pkg/octree/simplify.go
diffstat 1 files changed, 76 insertions(+), 63 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/octree/simplify.go	Mon Jul 01 11:10:47 2019 +0200
+++ b/pkg/octree/simplify.go	Tue Jul 02 11:43:12 2019 +0200
@@ -17,6 +17,74 @@
 	"math"
 )
 
+func handleTriangle(
+	t *Triangle,
+	maxDist, tolerance float64,
+	maxIdx int,
+	points MultiPointZ,
+	result *MultiPointZ,
+) bool {
+	if maxDist <= tolerance {
+		return false
+	}
+
+	if len(points) == 1 {
+		*result = append(*result, points[0])
+		return true
+	}
+
+	var (
+		tris     [3]Triangle
+		planes   [3]Plane3D
+		maxDists [3]float64
+		maxIdxs  [3]int
+		parts    [3]MultiPointZ
+	)
+
+	top := points[maxIdx]
+	for i := 0; i < 3; i++ {
+		tris[i] = Triangle{t[i], t[(i+1)%3], top}
+		planes[i] = tris[i].Plane3D()
+	}
+
+nextPoint:
+	for i, v := range points {
+		if i == maxIdx {
+			continue
+		}
+
+		for j := range tris {
+			if tris[j].Contains(v.X, v.Y) {
+				if dist := math.Abs(planes[j].Eval(v)); dist > maxDists[j] {
+					maxDists[j] = dist
+					maxIdxs[j] = len(parts[j])
+				}
+				parts[j] = append(parts[j], v)
+				continue nextPoint
+			}
+		}
+	}
+
+	var found bool
+	for i, part := range parts {
+		if len(part) > 0 && handleTriangle(
+			&tris[i],
+			maxDists[i], tolerance,
+			maxIdxs[i],
+			part,
+			result,
+		) {
+			found = true
+		}
+	}
+
+	if found {
+		*result = append(*result, top)
+	}
+
+	return found
+}
+
 func (points MultiPointZ) Simplify(tolerance float64) MultiPointZ {
 
 	if len(points) < 2 {
@@ -111,72 +179,17 @@
 		}
 	}
 
-	result := make([]Vertex, 0, len(points))
-
-	var handleTriangle func(*Triangle, float64, int, []Vertex) bool
-
-	handleTriangle = func(
-		t *Triangle,
-		maxDist float64, maxIdx int,
-		points []Vertex,
-	) bool {
-		if maxDist <= tolerance {
-			return false
-		}
-
-		if len(points) == 1 {
-			result = append(result, points[0])
-		}
-
-		var (
-			tris     [3]Triangle
-			planes   [3]Plane3D
-			maxDists [3]float64
-			maxIdxs  [3]int
-			parts    [3][]Vertex
-		)
-
-		top := points[maxIdx]
-		for i := 0; i < 3; i++ {
-			tris[i] = Triangle{t[i], t[(i+1)%3], top}
-			planes[i] = tris[i].Plane3D()
-		}
-
-	nextPoint:
-		for i, v := range points {
-			if i == maxIdx {
-				continue
-			}
-
-			for j := range tris {
-				if tris[j].Contains(v.X, v.Y) {
-					if dist := math.Abs(planes[j].Eval(v)); dist > maxDists[j] {
-						maxDists[j] = dist
-						maxIdxs[j] = len(parts[j])
-					}
-					parts[j] = append(parts[j], v)
-					continue nextPoint
-				}
-			}
-		}
-
-		var found bool
-		for i, part := range parts {
-			if len(part) > 0 && handleTriangle(&tris[i], maxDists[i], maxIdxs[i], part) {
-				found = true
-			}
-		}
-
-		if found {
-			result = append(result, top)
-		}
-
-		return found
-	}
+	result := make(MultiPointZ, 0, len(points))
 
 	var found bool
 	for i, part := range parts {
-		if len(part) > 0 && handleTriangle(&tris[i], maxDists[i], maxIdxs[i], part) {
+		if len(part) > 0 && handleTriangle(
+			&tris[i],
+			maxDists[i], tolerance,
+			maxIdxs[i],
+			part,
+			&result,
+		) {
 			found = true
 		}
 	}