Fixed all stl errors, should now work properly.
This commit is contained in:
111
src/main.py
111
src/main.py
@ -49,7 +49,7 @@ class apply_filters:
|
||||
# if no '=' in filter, it is a new filter
|
||||
self.filters.append(filter)
|
||||
i += 1
|
||||
self.params[i] = {}
|
||||
self.params[i] = {} # create empty dict for params
|
||||
else:
|
||||
# else it's a parameter for current filter
|
||||
key, value = filter.split('=')
|
||||
@ -75,8 +75,8 @@ class apply_filters:
|
||||
self.height = self.img.shape[0]
|
||||
self.print_size(self.img.shape)
|
||||
print(self.dpi)
|
||||
fig = plt.figure(figsize=(self.width, self.height),
|
||||
frameon=False, dpi=1)
|
||||
fig = plt.figure(figsize=(self.width/self.dpi, self.height/self.dpi),
|
||||
frameon=False, dpi=self.dpi)
|
||||
|
||||
ax = plt.Axes(fig, [0., 0., 1., 1.])
|
||||
ax.set_axis_off()
|
||||
@ -189,9 +189,7 @@ class apply_filters:
|
||||
# Apply all filters
|
||||
for i, filter_name in enumerate(self.filters):
|
||||
filter = self.filter_factory(filter_name)
|
||||
#print(self.img.dtype)
|
||||
filter.apply(self, self.params[i+1])
|
||||
#print(self.img.dtype)
|
||||
|
||||
def print_size(self, size):
|
||||
print("Height: " + str(size[0]), file=sys.stderr)
|
||||
@ -229,24 +227,24 @@ class apply_filters:
|
||||
self.img = self.img * 255
|
||||
self.img = self.img.astype(np.uint8)
|
||||
|
||||
# Modify image to make it more suitable depth
|
||||
rescaled = (1 + (1 - self.img/255)/6) * 255 / 10 # for positive forms ?
|
||||
# Make depth map from image
|
||||
# TODO make this depth/height a param
|
||||
self.img = (0.5 + (1 - self.img/255)/6) * \
|
||||
255 / 10 # for positive forms ?
|
||||
if self.mirror is True:
|
||||
rescaled = (2 - (1 - self.img/255)/6) * 255 / 10 # for negative forms
|
||||
self.img = (1 - (1 - self.img/255)/6) * \
|
||||
255 / 10 # for negative forms
|
||||
|
||||
# TODO: i dont know how to make white surrounding be extruded
|
||||
|
||||
# Add zero padding to image
|
||||
# TODO this better be done in the next function to keep dimensions intact
|
||||
self.height = self.img.shape[0] + 2
|
||||
self.width = self.img.shape[1] + 2
|
||||
self.img = np.zeros([self.height, self.width])
|
||||
self.img[1:-1:1, 1:-1:1] = rescaled
|
||||
self.height = self.img.shape[0]
|
||||
self.width = self.img.shape[1]
|
||||
self.print_size(self.img.shape)
|
||||
|
||||
# Create meshgrid for 3D model
|
||||
# This sets the scale of stl model
|
||||
x = np.linspace(0, 25.4 * self.width/self.dpi, self.width)
|
||||
y = np.linspace(0, 25.4 * self.height/self.dpi, self.height)
|
||||
# TODO this is an absolutely random constant that fits the scale...
|
||||
x = np.linspace(0, self.width / 23.6715, self.width)
|
||||
y = np.linspace(0, self.height / 23.6715, self.height)
|
||||
self.meshgrid = np.meshgrid(x, y)
|
||||
|
||||
def make_mesh(self):
|
||||
@ -268,12 +266,12 @@ class apply_filters:
|
||||
'''
|
||||
|
||||
# Add the image matrix to the 2D meshgrid and create 1D array of 3D points
|
||||
vertice_arr = np.vstack(list(map(np.ravel, self.meshgrid))).T
|
||||
vertex_arr = np.vstack(list(map(np.ravel, self.meshgrid))).T
|
||||
z = (self.img / 10).reshape(-1, 1)
|
||||
vertice_arr = np.concatenate((vertice_arr, z), axis=1)
|
||||
vertex_arr = np.concatenate((vertex_arr, z), axis=1)
|
||||
|
||||
# Convert 1D array back to matrix of 3D points
|
||||
vertice_arr = vertice_arr.reshape(self.height, self.width, 3)
|
||||
vertex_arr = vertex_arr.reshape(self.height, self.width, 3)
|
||||
|
||||
count = 0
|
||||
vertices = []
|
||||
@ -283,28 +281,73 @@ class apply_filters:
|
||||
def add_faces(c):
|
||||
faces.append([c, c + 1, c + 2])
|
||||
faces.append([c + 1, c + 3, c + 2])
|
||||
c += 4
|
||||
return c
|
||||
return c + 4
|
||||
|
||||
# TODO: this can be done more efficiently
|
||||
# Iterate over all vertices, create faces
|
||||
for j in range(self.width - 1):
|
||||
for i in range(self.height - 1):
|
||||
for i in range(self.height - 1):
|
||||
for j in range(self.width - 1):
|
||||
|
||||
vertices.append([vertice_arr[i][j]])
|
||||
vertices.append([vertice_arr[i][j+1]])
|
||||
vertices.append([vertice_arr[i+1][j]])
|
||||
vertices.append([vertice_arr[i+1][j+1]])
|
||||
vertices.append([vertex_arr[i][j]])
|
||||
vertices.append([vertex_arr[i][j+1]])
|
||||
vertices.append([vertex_arr[i+1][j]])
|
||||
vertices.append([vertex_arr[i+1][j+1]])
|
||||
|
||||
count = add_faces(count)
|
||||
|
||||
# Add faces for the backside of the lithophane
|
||||
# This makes it closed, so it can be printed
|
||||
vertices.append([vertice_arr[0][0]])
|
||||
vertices.append([vertice_arr[0][self.width - 1]])
|
||||
vertices.append([vertice_arr[self.height - 1][0]])
|
||||
vertices.append([vertice_arr[self.height - 1][self.width - 1]])
|
||||
# TODO: this doesn't work, creates naked edges
|
||||
null_arr = np.copy(vertex_arr)
|
||||
for i in range(self.height):
|
||||
for j in range(self.width):
|
||||
null_arr[i][j][2] = 0
|
||||
|
||||
# Back side faces
|
||||
for i in range(self.height - 1):
|
||||
for j in range(self.width - 1):
|
||||
|
||||
count = add_faces(count)
|
||||
vertices.append([null_arr[i][j]])
|
||||
vertices.append([null_arr[i+1][j]])
|
||||
vertices.append([null_arr[i][j+1]])
|
||||
vertices.append([null_arr[i+1][j+1]])
|
||||
|
||||
count = add_faces(count)
|
||||
|
||||
# Horizontal side faces
|
||||
for j in range(self.height - 1):
|
||||
vertices.append([vertex_arr[j][0]])
|
||||
vertices.append([vertex_arr[j+1][0]])
|
||||
vertices.append([null_arr[j][0]])
|
||||
vertices.append([null_arr[j+1][0]])
|
||||
|
||||
count = add_faces(count)
|
||||
|
||||
max = self.width - 1
|
||||
|
||||
vertices.append([vertex_arr[j+1][max]])
|
||||
vertices.append([vertex_arr[j][max]])
|
||||
vertices.append([null_arr[j+1][max]])
|
||||
vertices.append([null_arr[j][max]])
|
||||
|
||||
count = add_faces(count)
|
||||
|
||||
# Vertical side faces
|
||||
for j in range(self.width - 1):
|
||||
vertices.append([vertex_arr[0][j+1]])
|
||||
vertices.append([vertex_arr[0][j]])
|
||||
vertices.append([null_arr[0][j+1]])
|
||||
vertices.append([null_arr[0][j]])
|
||||
|
||||
count = add_faces(count)
|
||||
|
||||
max = self.height - 1
|
||||
|
||||
vertices.append([vertex_arr[max][j]])
|
||||
vertices.append([vertex_arr[max][j+1]])
|
||||
vertices.append([null_arr[max][j]])
|
||||
vertices.append([null_arr[max][j+1]])
|
||||
|
||||
count = add_faces(count)
|
||||
|
||||
# Convert to numpy arrays
|
||||
faces = np.array(faces)
|
||||
|
Reference in New Issue
Block a user