Mercurial > gemma
comparison 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 |
comparison
equal
deleted
inserted
replaced
5702:fe83406fe7ed | 5703:d2ccf6bb6940 |
---|---|
86 // the three points of the triangles are in. | 86 // the three points of the triangles are in. |
87 func (t *Triangle) Plane3D() Plane3D { | 87 func (t *Triangle) Plane3D() Plane3D { |
88 | 88 |
89 v0 := t[1].Sub(t[0]) | 89 v0 := t[1].Sub(t[0]) |
90 v1 := t[2].Sub(t[0]) | 90 v1 := t[2].Sub(t[0]) |
91 n := v0.Cross(v1).Normalize() | 91 n := v0.Cross(v1) |
92 | |
93 // If the length of normal is near zero assume we have | |
94 // a plane constant in z with an average z value of | |
95 // the three vertices. | |
96 // This should protect us from the effects of collinear | |
97 // geometries. | |
98 l := n.Length() | |
99 if l < 1e-7 { | |
100 sum := t[0].Z + t[1].Z + t[2].Z | |
101 return Plane3D{ | |
102 A: 0, | |
103 B: 0, | |
104 C: 1, | |
105 D: -sum / 3, | |
106 } | |
107 } | |
108 n = n.Scale(1 / l) | |
92 | 109 |
93 // x*nx+ y*ny+ z*nz + d = 0 | 110 // x*nx+ y*ny+ z*nz + d = 0 |
94 // d = - (x*nx+ y*ny + z*nz) | 111 // d = - (x*nx+ y*ny + z*nz) |
95 d := -t[0].Dot(n) | 112 d := -t[0].Dot(n) |
96 return Plane3D{ | 113 return Plane3D{ |