From dbb01513e9a001ea8a8e833566b77c2ab773ef47 Mon Sep 17 00:00:00 2001
From: Paul McCarthy <pauldmccarthy@gmail.com>
Date: Sat, 9 Nov 2019 06:11:29 +0000
Subject: [PATCH] BF: Make mesh.needsFixing a little more robust? To my
 knowledge there's no methoed to do this which is both fast and foolproof

---
 fsl/data/mesh.py | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)

diff --git a/fsl/data/mesh.py b/fsl/data/mesh.py
index c15e49ced..8d05af196 100644
--- a/fsl/data/mesh.py
+++ b/fsl/data/mesh.py
@@ -758,15 +758,19 @@ def needsFixing(vertices, indices, fnormals, loBounds, hiBounds):
     ivert = np.argmin(dists)
     vert  = vertices[ivert]
 
-    # Pick a triangle that
-    # this vertex is in and
-    # ges its face normal
-    itri = np.where(indices == ivert)[0][0]
-    n    = fnormals[itri, :]
+    # Get all the triangles
+    # that this vertex is in
+    # and their face normals
+    itris = np.where(indices == ivert)[0]
+    norms = fnormals[itris, :]
 
-    # Make sure the angle between the
+    # Calculate the angle between each
     # normal, and a vector from the
-    # vertex to the camera is positive
-    # If it isn't, we need to flip the
-    # triangle winding order.
-    return np.dot(n, affine.normalise(camera - vert)) < 0
+    # vertex to the camera. If more than
+    # 50% of the angles are negative
+    # (== more than 90 degrees == the
+    # face is facing away from the
+    # camera), assume that we need to
+    # flip the triangle winding order.
+    angles = np.dot(norms, affine.normalise(camera - vert))
+    return ((angles > 0).sum() / len(itris)) < 0.5
-- 
GitLab