diff pkg/mesh/vertex.go @ 5706:148abae1fcd0 sr-v2

Quantize xyz in points in X/Y to 1/QuantScale.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 20 Feb 2024 12:52:09 +0100
parents d2ccf6bb6940
children 61eff98a40ae
line wrap: on
line diff
--- a/pkg/mesh/vertex.go	Tue Feb 20 09:51:20 2024 +0100
+++ b/pkg/mesh/vertex.go	Tue Feb 20 12:52:09 2024 +0100
@@ -1161,6 +1161,39 @@
 	return out
 }
 
+// QuantizeXY quantize the X/Y values to scale and
+// removes duplicates in place. The z value of
+// duplicates will be averaged pairwise.
+func (mpz MultiPointZ) QuantizeXY(scale float64) MultiPointZ {
+	type qpoint struct {
+		x int64
+		y int64
+	}
+	m := make(map[qpoint]float64, len(mpz))
+	for _, p := range mpz {
+		k := qpoint{
+			x: int64(math.Round(p.X * scale)),
+			y: int64(math.Round(p.Y * scale)),
+		}
+		if z, ok := m[k]; ok {
+			m[k] = (z + p.Z) * 0.5
+		} else {
+			m[k] = p.Z
+		}
+	}
+	i := 0
+	invScale := 1 / scale
+	for k, z := range m {
+		mpz[i] = Vertex{
+			X: float64(k.x) * invScale,
+			Y: float64(k.y) * invScale,
+			Z: z,
+		}
+		i++
+	}
+	return mpz[:i]
+}
+
 // Filter returns a copy removed the vertices which
 // don't pass the filter test.
 func (mpz MultiPointZ) Filter(filter func(Vertex) bool) MultiPointZ {