diff pkg/mesh/vertex.go @ 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 1222b777f51f
children 148abae1fcd0
line wrap: on
line diff
--- 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)