changeset 5703:d2ccf6bb6940 sr-v2

Make plane eval for z-values of triangles numerial more robust.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 19 Feb 2024 17:48:13 +0100
parents fe83406fe7ed
children a68e8eae7273
files pkg/mesh/raster.go pkg/mesh/vertex.go
diffstat 2 files changed, 22 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/mesh/raster.go	Wed Feb 14 22:38:14 2024 +0100
+++ b/pkg/mesh/raster.go	Mon Feb 19 17:48:13 2024 +0100
@@ -104,8 +104,10 @@
 					n++
 				}
 
-				if n > 0 {
+				if n > 0 && !math.IsNaN(sum) {
 					row[j] = sum / float64(n)
+				} else {
+					row[j] = noData
 				}
 				px += r.CellSize
 			}
@@ -168,7 +170,7 @@
 					n++
 				}
 
-				if n > 0 {
+				if n > 0 && !math.IsNaN(sum) {
 					row[j] -= sum / float64(n)
 				} else {
 					row[j] = noData
--- a/pkg/mesh/vertex.go	Wed Feb 14 22:38:14 2024 +0100
+++ b/pkg/mesh/vertex.go	Mon Feb 19 17:48:13 2024 +0100
@@ -88,7 +88,24 @@
 
 	v0 := t[1].Sub(t[0])
 	v1 := t[2].Sub(t[0])
-	n := v0.Cross(v1).Normalize()
+	n := v0.Cross(v1)
+
+	// If the length of normal is near zero assume we have
+	// a plane constant in z with an average z value of
+	// the three vertices.
+	// This should protect us from the effects of collinear
+	// geometries.
+	l := n.Length()
+	if l < 1e-7 {
+		sum := t[0].Z + t[1].Z + t[2].Z
+		return Plane3D{
+			A: 0,
+			B: 0,
+			C: 1,
+			D: -sum / 3,
+		}
+	}
+	n = n.Scale(1 / l)
 
 	// x*nx+ y*ny+ z*nz + d = 0
 	// d = - (x*nx+ y*ny + z*nz)