From 8601eb82d486b508e946810b186567ae68000d0f Mon Sep 17 00:00:00 2001
From: Joshua Steer <joshua.w.steer@gmail.com>
Date: Mon, 10 Sep 2018 00:23:14 +0100
Subject: [PATCH] Functionality to fix normals through flipping

---
 AmpScan/core.py | 30 +++++++++++++++++++++++++++++-
 1 file changed, 29 insertions(+), 1 deletion(-)

diff --git a/AmpScan/core.py b/AmpScan/core.py
index 0a628c8..0b20423 100644
--- a/AmpScan/core.py
+++ b/AmpScan/core.py
@@ -100,6 +100,7 @@ class AmpObject(trimMixin, smoothMixin, analyseMixin, visMixin):
         if unify is True:
             self.unifyVert()
         # Call function to calculate the edges array
+#        self.fixNorm()
         if struc is True:
             self.calcStruct()
         self.values = np.zeros([len(self.vert)])
@@ -242,6 +243,18 @@ class AmpObject(trimMixin, smoothMixin, analyseMixin, visMixin):
                          self.vert[self.faces[:,0]])
         mag = np.linalg.norm(norms, axis=1)
         self.norm = np.divide(norms, mag[:,None])
+    
+    def fixNorm(self):
+        r"""
+        Fix normals of faces so they all face outwards 
+        """
+        fC = self.vert[self.faces].mean(axis=1)
+        cent = self.vert.mean(axis=0)
+        polarity = np.sum(self.norm * (fC-cent), axis=1) < 0
+        if polarity.mean() > 0.5:
+            self.faces[:, [1,2]] = self.faces[:, [2,1]]
+            self.calcNorm()
+            if hasattr(self, 'vNorm'): self.calcVNorm()
         
     def calcVNorm(self):
         """
@@ -372,7 +385,6 @@ class AmpObject(trimMixin, smoothMixin, analyseMixin, visMixin):
             self.translate(T)
         
 
-        
     @staticmethod
     def rotMatrix(rot, ang='rad'):
         r"""
@@ -406,3 +418,19 @@ class AmpObject(trimMixin, smoothMixin, analyseMixin, visMixin):
                        [0, 0, 1]])
         R = np.dot(np.dot(Rz, Ry), Rx)
         return R
+    
+    def flip(self, axis=1):
+        r"""
+        Flip the mesh in a plane
+        
+        Parameters
+        ----------
+        axis: int, default 1
+            The axis in which to flip the mesh
+
+        """
+        self.vert[:, axis] *= -1.0
+        # Switch face order to normals face same direction
+        self.faces[:, [1, 2]] = self.faces[:, [2, 1]]
+        self.calcNorm()
+        self.calcVNorm()
-- 
GitLab