Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Project: New Julia
Views: 239
Kernel: Python 2 (SageMath)
import numpy as np import cPickle as pickle from numpy2stl import writeSTL with open('../data/10^2.pkl') as f: data=pickle.load(f)["iterations"] writer = STL_Writer('test.stl', ascii=False) WIDTH = 1 num_y, num_x = data.shape x_edge = (num_x//2)*WIDTH + WIDTH/2 y_edge = (num_y//2)*WIDTH + WIDTH/2 x_grid = np.linspace(-x_edge, x_edge, num_x+1) y_grid = np.linspace(y_edge, -y_edge, num_y+1) left_x = np.column_stack( (x_grid[0:-1],)*num_x ).transpose() right_x = np.column_stack( (x_grid[1:], )*num_x ).transpose() top_y = np.column_stack( (y_grid[0:-1],)*num_y ) bot_y = np.column_stack( (y_grid[1:], )*num_y ) #top faces points #All have norm (0,0,1) p4 = np.dstack( (left_x, bot_y, data) ) p3 = np.dstack( (left_x, top_y, data) ) p2 = np.dstack( (right_x, top_y, data) ) p1 = np.dstack( (right_x, bot_y, data) ) norm = np.array( [0,0,1]*(p1.shape[0]*p1.shape[1]) ).reshape( p1.shape ) #Top face triangles (defined clockwise) #P1,P2,P3 #P1,P3,P4 triangles_1 = np.dstack( (norm, p1,p2,p3) ) triangles_2 = np.dstack( (norm, p1,p3,p4) ) triangles = np.vstack( (triangles_1, triangles_2) ) #Export top triangles triangles = triangles.reshape( ( triangles.shape[0]*triangles.shape[1],12) ) writer.add_faces(triangles) #------------------------------------------------------------- #front face points (Not including botttom row) p4 = np.dstack( (left_x[:-1,:], bot_y[:-1,:], data[1:,:]) )#use next row down for h p3 = np.dstack( (left_x[:-1,:], bot_y[:-1,:], data[:-1,:]) ) p2 = np.dstack( (right_x[:-1,:], bot_y[:-1,:], data[:-1,:]) ) p1 = np.dstack( (right_x[:-1,:], bot_y[:-1,:], data[1:,:]) ) norm = np.array( [0,-1,0]*(p1.shape[0]*p1.shape[1]) ).reshape( p1.shape ) #front triangles (all but bottom row) triangles_1 = np.dstack( (norm, p1,p2,p3) ) triangles_2 = np.dstack( (norm, p1,p3,p4) ) triangles = np.vstack( (triangles_1, triangles_2) ) #Export front triangles #first remove triangles which are lines (first and second z values are the same) triangles = np.delete( triangles, np.where(triangles[:,:,5]==triangles[:,:,8] ), axis=1 ) triangles = triangles.reshape( ( triangles.shape[0]*triangles.shape[1],12) ) writer.add_faces(triangles) #------------------------------------------------------------- #front face points (Botttom row) p1 = np.dstack( (left_x[-1,:], bot_y[-1,:], np.zeros( (1,num_x) )) ) p2 = np.dstack( (left_x[-1,:], bot_y[-1,:], data[-1,:]) ) p3 = np.dstack( (right_x[-1,:], bot_y[-1,:], data[-1,:]) ) p4 = np.dstack( (right_x[-1,:], bot_y[-1,:], np.zeros( (1,num_x) )) ) norm = np.array( [0,-1,0]*(p1.shape[0]*p1.shape[1]) ).reshape( p1.shape ) #front triangles (bottom row) triangles_1 = np.dstack( (norm, p1,p2,p3) ) triangles_2 = np.dstack( (norm, p1,p3,p4) ) triangles = np.vstack( (triangles_1, triangles_2) ) #Export front triangles triangles = triangles.reshape( ( triangles.shape[0]*triangles.shape[1],12) ) writer.add_faces(triangles) #------------------------------------------------ #left hand face (all but first column) p1 = np.dstack( (left_x[:,1:], top_y[:,1:], data[:,:-1] ) )#all but last column p2 = np.dstack( (left_x[:,1:], top_y[:,1:], data[:,1:] ) ) p3 = np.dstack( (left_x[:,1:], bot_y[:,1:], data[:,1:] ) ) p4 = np.dstack( (left_x[:,1:], bot_y[:,1:], data[:,:-1] ) ) norm = np.array( [-1,0,0]*(p1.shape[0]*p1.shape[1]) ).reshape( p1.shape ) #left side middle triangles triangles_1 = np.dstack( (norm, p1,p2,p3) ) triangles_2 = np.dstack( (norm, p1,p3,p4) ) triangles = np.vstack( (triangles_1, triangles_2) ) #export left side middle triangles #first remove triangles which are lines (first and second z values are the same) triangles = np.delete( triangles, np.where(triangles[:,:,5]==triangles[:,:,8] ), axis=1 ) triangles = triangles.reshape( ( triangles.shape[0]*triangles.shape[1],12) ) writer.add_faces(triangles) #------------------------------------------------ #left hand face (first column) p1 = np.dstack( (left_x[:,1], top_y[:,1], np.zeros( (num_y,) ) ) ) p2 = np.dstack( (left_x[:,1], top_y[:,1], data[:,1]) ) p3 = np.dstack( (left_x[:,1], bot_y[:,1], data[:,1]) ) p4 = np.dstack( (left_x[:,1], bot_y[:,1], np.zeros( (num_y,) ) ) ) norm = np.array( [-1,0,0]*(p1.shape[0]*p1.shape[1]) ).reshape( p1.shape ) #left side first column triangles triangles_1 = np.dstack( (norm, p1,p2,p3) ) triangles_2 = np.dstack( (norm, p1,p3,p4) ) triangles = np.vstack( (triangles_1, triangles_2) ) #export left side first column triangles triangles = triangles.reshape( ( triangles.shape[0]*triangles.shape[1],12) ) writer.add_faces(triangles) #----------------------------------------------- #Top face (not including first row) p1 = np.dstack( (right_x[1:,:], top_y[1:,:], data[:-1,:]) )#use row above for h p2 = np.dstack( (right_x[1:,:], top_y[1:,:], data[1:,:]) ) p3 = np.dstack( (left_x[1:,:], top_y[1:,:], data[1:,:]) ) p4 = np.dstack( (left_x[1:,:], top_y[1:,:], data[:-1,:]) ) norm = np.array( [0,1,0]*(p1.shape[0]*p1.shape[1]) ).reshape( p1.shape ) #top face triangles (not including first row) triangles_1 = np.dstack( (norm, p1,p2,p3) ) triangles_2 = np.dstack( (norm, p1,p3,p4) ) triangles = np.vstack( (triangles_1, triangles_2) ) #export top face (not first row) triangles #first remove triangles which are lines (first and second z values are the same) triangles = np.delete( triangles, np.where(triangles[:,:,5]==triangles[:,:,8] ), axis=1 ) triangles = triangles.reshape( ( triangles.shape[0]*triangles.shape[1],12) ) writer.add_faces(triangles) #-------------------------------------------- #Top face (first row) p1 = np.dstack( (right_x[1,:], top_y[1,:], np.zeros( (1,num_x) ) ) ) p2 = np.dstack( (right_x[1,:], top_y[1,:], data[1,:]) ) p3 = np.dstack( (left_x[1,:], top_y[1,:], data[1,:]) ) p4 = np.dstack( (left_x[1,:], top_y[1,:], np.zeros( (1,num_x) ) ) ) norm = np.array( [0,1,0]*(p1.shape[0]*p1.shape[1]) ).reshape( p1.shape ) #top face triangles (first row) triangles_1 = np.dstack( (norm, p1,p2,p3) ) triangles_2 = np.dstack( (norm, p1,p3,p4) ) triangles = np.vstack( (triangles_1, triangles_2) ) #export top face (first row) triangles triangles = triangles.reshape( ( triangles.shape[0]*triangles.shape[1],12) ) writer.add_faces(triangles) #------------------------------------------------ #Right side face (not includeing last colum) p1 = np.dstack( (right_x[:,:-1], bot_y[:,:-1], data[:,1:]) )#use next column to right for h p2 = np.dstack( (right_x[:,:-1], bot_y[:,:-1], data[:,:-1]) ) p3 = np.dstack( (right_x[:,:-1], top_y[:,:-1], data[:,:-1]) ) p4 = np.dstack( (right_x[:,:-1], top_y[:,:-1], data[:,1:]) ) norm = np.array( [1,0,0]*(p1.shape[0]*p1.shape[1]) ).reshape( p1.shape ) #Right face triangles (last column not included) triangles_1 = np.dstack( (norm, p1,p2,p3) ) triangles_2 = np.dstack( (norm, p1,p3,p4) ) triangles = np.vstack( (triangles_1, triangles_2) ) #export right face (not including last column) #first remove triangles which are lines (first and second z values are the same) triangles = np.delete( triangles, np.where(triangles[:,:,5]==triangles[:,:,8] ), axis=1 ) triangles = triangles.reshape( ( triangles.shape[0]*triangles.shape[1],12) ) writer.add_faces(triangles) #--------------------------------------------------- #Right side face (last column) p1 = np.dstack( (right_x[:,-1], bot_y[:,-1], np.zeros( (num_y,) ) ) ) p2 = np.dstack( (right_x[:,-1], bot_y[:,-1], data[:,-1]) ) p3 = np.dstack( (right_x[:,-1], top_y[:,-1], data[:,-1]) ) p4 = np.dstack( (right_x[:,-1], top_y[:,-1], np.zeros( (num_y,) ) ) ) norm = np.array( [1,0,0]*(p1.shape[0]*p1.shape[1]) ).reshape( p1.shape ) #Right face triangles (last column) triangles_1 = np.dstack( (norm, p1,p2,p3) ) triangles_2 = np.dstack( (norm, p1,p3,p4) ) triangles = np.vstack( (triangles_1, triangles_2) ) #export right face (ast column) triangles = triangles.reshape( ( triangles.shape[0]*triangles.shape[1],12) ) writer.add_faces(triangles) writer.close()
import numpy as np import cPickle as pickle data = ((np.random.random( (4,4) )*5).astype(int) ).astype(float) print(data) WIDTH = 1 num_y, num_x = data.shape x_edge = (num_x//2)*WIDTH + WIDTH/2 y_edge = (num_y//2)*WIDTH + WIDTH/2 x_grid = np.linspace(-x_edge, x_edge, num_x+1) y_grid = np.linspace(y_edge, -y_edge, num_y+1) left_x = np.column_stack( (x_grid[0:-1],)*num_x ).transpose() right_x = np.column_stack( (x_grid[1:], )*num_x ).transpose() top_y = np.column_stack( (y_grid[0:-1],)*num_y ) bot_y = np.column_stack( (y_grid[1:], )*num_y ) #------------------------------------------------------------- #front face points (Not including botttom row) p4 = np.dstack( (left_x[:-1,:], bot_y[:-1,:], data[1:,:]) )#use next row down for h p3 = np.dstack( (left_x[:-1,:], bot_y[:-1,:], data[:-1,:]) ) p2 = np.dstack( (right_x[:-1,:], bot_y[:-1,:], data[:-1,:]) ) p1 = np.dstack( (right_x[:-1,:], bot_y[:-1,:], data[1:,:]) ) norm = np.array( [0,-1,0]*(p1.shape[0]*p1.shape[1]) ).reshape( p1.shape ) #front triangles (all but bottom row) triangles_1 = np.dstack( (norm, p1,p2,p3) ) triangles_2 = np.dstack( (norm, p1,p3,p4) ) triangles = np.vstack( (triangles_1, triangles_2) ) #Export front triangles #first remove triangles which are lines (first and second z values are the same) triangles = triangles.reshape( (triangles.shape[0]*triangles.shape[1],12) ) print(triangles.shape) print(triangles) d = np.where( triangles[:,5]==triangles[:,8] ) print(d) print(triangles[ d ]) triangles = np.delete(triangles,d, axis=0) print(triangles) print(triangles.shape)
[[ 2. 0. 4. 2.] [ 2. 4. 4. 4.] [ 1. 2. 0. 0.] [ 2. 4. 3. 0.]] (24, 12) [[ 0. -1. 0. -1. 1. 2. -1. 1. 2. -2. 1. 2.] [ 0. -1. 0. 0. 1. 4. 0. 1. 0. -1. 1. 0.] [ 0. -1. 0. 1. 1. 4. 1. 1. 4. 0. 1. 4.] [ 0.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. 0. 1. 4.] [ 0. -1. 0. 2. -1. 0. 1. -1. 0. 1. -1. 0.]] [[ 0. -1. 0. 0. 1. 4. 0. 1. 0. -1. 1. 0.] [ 0. -1. 0. 2. 1. 4. 2. 1. 2. 1. 1. 2.] [ 0. -1. 0. -1. 0. 1. -1. 0. 2. -2. 0. 2.] [ 0. -1. 0. 0. 0. 2. 0. 0. 4. -1. 0. 4.] [ 0. -1. 0. 1. 0. 0. 1. 0. 4. 0. 0. 4.] [ 0. -1. 0. 2. 0. 0. 2. 0. 4. 1. 0. 4.] [ 0. -1. 0. -1. -1. 2. -1. -1. 1. -2. -1. 1.] [ 0. -1. 0. 0. -1. 4. 0. -1. 2. -1. -1. 2.] [ 0. -1. 0. 1. -1. 3. 1. -1. 0. 0. -1. 0.] [ 0. -1. 0. 0. 1. 4. -1. 1. 0. -1. 1. 4.] [ 0. -1. 0. 2. 1. 4. 1. 1. 2. 1. 1. 4.] [ 0. -1. 0. -1. 0. 1. -2. 0. 2. -2. 0. 1.] [ 0. -1. 0. 0. 0. 2. -1. 0. 4. -1. 0. 2.] [ 0. -1. 0. 1. 0. 0. 0. 0. 4. 0. 0. 0.] [ 0. -1. 0. 2. 0. 0. 1. 0. 4. 1. 0. 0.] [ 0. -1. 0. -1. -1. 2. -2. -1. 1. -2. -1. 2.] [ 0. -1. 0. 0. -1. 4. -1. -1. 2. -1. -1. 4.] [ 0. -1. 0. 1. -1. 3. 0. -1. 0. 0. -1. 3.]] (18, 12)
np.version.version()
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-20-3d709334ddbb> in <module>() ----> 1 np.version.version() TypeError: 'str' object is not callable