From 129696749e707d5a3126ac40ef57a76ac506b868 Mon Sep 17 00:00:00 2001 From: Joshua Steer <Joshua.Steer@soton.ac.uk> Date: Thu, 30 Aug 2018 12:10:01 +0100 Subject: [PATCH] pca, registration and smooth updates --- AmpScan/pca.py | 20 ++++++++++++++++---- AmpScan/registration.py | 14 +++----------- AmpScan/smooth.py | 11 +++++++++-- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/AmpScan/pca.py b/AmpScan/pca.py index 3ee7d5f..4db4e9a 100644 --- a/AmpScan/pca.py +++ b/AmpScan/pca.py @@ -25,8 +25,10 @@ class pca(object): r""" Function to import multiple stl files from folder """ - self.shapes = [AmpObject(path + f, 'limb', unify=unify) for f in - os.listdir(path) if f.endswith('.stl')] + self.fnames = [f for f in os.listdir(path) if f.endswith('.stl')] + self.shapes = [AmpObject(path + f, 'limb', unify=unify) for f in self.fnames] + for s in self.shapes: + s.lp_smooth(3, brim=True) def sliceFiles(self, height): r""" @@ -35,12 +37,22 @@ class pca(object): for s in self.shapes: s.planarTrim(height) - def register(self, scale=None): + def register(self, scale=None, save=None, baseline=True): r""" Function to register all the shapes to a baseline """ - self.registered = [registration(self.baseline, t, fixBrim=True, steps=10).reg for t in self.shapes] + self.registered = [] + for t in self.shapes: + r = registration(self.baseline, t, fixBrim=True, steps=5, scale=scale, smooth=1, neigh=50).reg + r.lp_smooth() + self.registered.append(r) + if save is not None: + for f, r in zip(self.fnames, self.registered): + r.save(save + f) self.X = np.array([r.vert.flatten() for r in self.registered]).T + if baseline is True: + self.X = np.c_[self.X, self.baseline.vert.flatten()] + def pca(self): r""" diff --git a/AmpScan/registration.py b/AmpScan/registration.py index b89fc6a..dde6a09 100644 --- a/AmpScan/registration.py +++ b/AmpScan/registration.py @@ -48,7 +48,7 @@ class registration(object): def point2plane(self, steps = 1, neigh = 10, inside = True, subset = None, - scale=False, smooth=1, fixBrim=False): + scale=None, smooth=1, fixBrim=False): r""" Point to Plane method for registration between the two meshes @@ -70,9 +70,6 @@ class registration(object): If True, the nodes on the brim line will not be included in the smooth """ - if fixBrim is True: - eidx = (self.b.faceEdges == -99999).sum(axis=1).astype(bool) - vBrim = np.unique(self.b.edges[eidx, :]) # Calc FaceCentroids fC = self.t.vert[self.t.faces].mean(axis=1) # Construct knn tree @@ -84,7 +81,7 @@ class registration(object): if scale is not None: tmin = self.t.vert.min(axis=0)[2] rmin = self.reg.vert.min(axis=0)[2] - SF = 1 - ((tmin-scale)/(rmin-scale)) + SF = ((tmin-scale)/(rmin-scale)) - 1 logic = self.reg.vert[:, 2] < scale d = (self.reg.vert[logic, 2] - scale) * SF self.reg.vert[logic, 2] += d @@ -122,14 +119,9 @@ class registration(object): D = G[np.arange(len(G)), GInd, :] rVert += D/step if smooth > 0 and step > 1: - if fixBrim is True: - bPoints = rVert[vBrim, :].copy() # v = self.reg.vert[~subset] - self.reg.lp_smooth(smooth) - self.reg.vert[vBrim, :] = bPoints + self.reg.lp_smooth(smooth, brim = fixBrim) # self.reg.vert[~subset] = v - else: - self.reg.lp_smooth(smooth) else: self.reg.calcNorm() diff --git a/AmpScan/smooth.py b/AmpScan/smooth.py index 62878d1..ff6bb37 100644 --- a/AmpScan/smooth.py +++ b/AmpScan/smooth.py @@ -9,7 +9,7 @@ import copy class smoothMixin(object): - def lp_smooth(self, n=1): + def lp_smooth(self, n=1, brim = True): r""" Function to apply a laplacian smooth to the mesh. This method replaces each vertex with the mean of its connected neighbours @@ -21,6 +21,10 @@ class smoothMixin(object): number of iterations of smoothing """ + if brim is True: + eidx = (self.faceEdges == -99999).sum(axis=1).astype(bool) + vBrim = np.unique(self.edges[eidx, :]) + else: vBrim = [] # Flatten the edges array to 1D e = self.edges.flatten() # Get the indicies to sort edges @@ -36,7 +40,10 @@ class smoothMixin(object): # List all vertices vert = copy.deepcopy(self.vert) neighVerts = vert[self.edges[row, 1-col], :] - for j in np.arange(self.vert.shape[0]): + vRange = np.arange(self.vert.shape[0]) + log = np.isin(vRange, vBrim) + vRange = vRange[~log] + for j in vRange: # Calculate the mean of the vertex set self.vert[j, :] = neighVerts[ndx[j]:ndx[j+1]].mean(axis=0) self.calcNorm() -- GitLab