Commits

Nathan Collier committed abe2a02

added subdivision

  • Participants
  • Parent commits 7eb20c6

Comments (0)

Files changed (1)

+import numpy as np
+
+__all__ = ['CatmullClark']
+
+sten = np.zeros((3,3))
+sten[0,0] = 1./64.; sten[0,1] = 3./32.; sten[0,2] = 1./64.
+sten[1,0] = 3./32.; sten[1,1] = 9./16.; sten[1,2] = 3./32.
+sten[2,0] = 1./64.; sten[2,1] = 3./32.; sten[2,2] = 1./64.
+
+def CatmullClark(S):
+    """
+    Uses Catmull-Clark subdivision to refine a surface
+
+    Parameters:
+    -----------
+    S : ndarray (n,m,:)
+        a numpy array which is n by m
+
+    Returns:
+    --------
+    R : ndarray (2n-1,2m-1,:)
+        the refined array
+    """
+    R = np.zeros((2*S.shape[0]-1,2*S.shape[1]-1,S.shape[2]))
+    for i in range(S.shape[0]):
+        for j in range(S.shape[1]):
+            R[2*i,2*j,:] = S[i,j,:]
+    # locate face points
+    for i in range(S.shape[0]-1):
+        for j in range(S.shape[1]-1):
+            R[2*i+1,2*j+1,:] = 0.25*(S[i,j,:]+S[i+1,j,:]+S[i+1,j+1,:]+S[i,j+1,:])
+    # locate edge points
+    for i in range(S.shape[0]-1):
+        R[2*i+1,0,:] = 1./2.*(S[i,0,:]+S[i+1,0,:])
+        R[2*i+1,-1,:] = 1./2.*(S[i,-1,:]+S[i+1,-1,:])
+        for j in range(1,S.shape[1]-1):
+            R[2*i+1,2*j,:] = 0.25*(R[2*i+1,2*j-1,:]+R[2*i+1,2*j+1,:]+S[i,j,:]+S[i+1,j,:])
+    for j in range(S.shape[1]-1):
+        R[0,2*j+1,:] = 1./2.*(S[0,j,:]+S[0,j+1,:])
+        R[-1,2*j+1,:] = 1./2.*(S[-1,j,:]+S[-1,j+1,:])
+        for i in range(1,S.shape[0]-1):
+            R[2*i,2*j+1,:] = 0.25*(R[2*i-1,2*j+1,:]+R[2*i+1,2*j+1,:]+S[i,j,:]+S[i,j+1,:])
+    # relocate original points
+    for i in range(1,S.shape[0]-1):
+        for j in range(1,S.shape[1]-1):
+            for k in range(R.shape[2]):
+                R[2*i,2*j,k] = (sten*R[2*i-1:2*i+2,2*j-1:2*j+2,k]).sum()
+    return R