|
|
@ -205,14 +205,11 @@ class app:
|
|
|
|
self.input_file, cv.IMREAD_GRAYSCALE).astype(np.uint8)
|
|
|
|
self.input_file, cv.IMREAD_GRAYSCALE).astype(np.uint8)
|
|
|
|
|
|
|
|
|
|
|
|
self.height, self.width = self.img.shape
|
|
|
|
self.height, self.width = self.img.shape
|
|
|
|
|
|
|
|
# gets empty figure and ax with dimensions of input image
|
|
|
|
|
|
|
|
fig, ax = self.get_empty_figure()
|
|
|
|
|
|
|
|
|
|
|
|
print("Height: " + str(self.height) + " px and width: "
|
|
|
|
print("Height: " + str(self.height) + " px and width: "
|
|
|
|
+ str(self.width) + " px", file=sys.stderr)
|
|
|
|
+ str(self.width) + " px", file=sys.stderr)
|
|
|
|
size = (self.width/self.dpi, self.height/self.dpi)
|
|
|
|
|
|
|
|
fig = plt.figure(figsize=size, frameon=False, dpi=self.dpi)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ax = plt.Axes(fig, [0., 0., 1., 1.])
|
|
|
|
|
|
|
|
ax.set_axis_off()
|
|
|
|
|
|
|
|
fig.add_axes(ax)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if self.mirror is True:
|
|
|
|
if self.mirror is True:
|
|
|
|
self.mirror_image()
|
|
|
|
self.mirror_image()
|
|
|
@ -222,6 +219,16 @@ class app:
|
|
|
|
self.save_image(fig, ax)
|
|
|
|
self.save_image(fig, ax)
|
|
|
|
plt.close()
|
|
|
|
plt.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_empty_figure(self):
|
|
|
|
|
|
|
|
'''Return empty figure with one ax of dimensions of input image.
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
size = (self.width/self.dpi, self.height/self.dpi)
|
|
|
|
|
|
|
|
fig = plt.figure(figsize=size, frameon=False, dpi=self.dpi)
|
|
|
|
|
|
|
|
ax = plt.Axes(fig, [0., 0., 1., 1.])
|
|
|
|
|
|
|
|
ax.set_axis_off()
|
|
|
|
|
|
|
|
fig.add_axes(ax)
|
|
|
|
|
|
|
|
return fig, ax
|
|
|
|
|
|
|
|
|
|
|
|
def mirror_image(self):
|
|
|
|
def mirror_image(self):
|
|
|
|
'''Mirror image using opencv, should be used if we want a positive model.
|
|
|
|
'''Mirror image using opencv, should be used if we want a positive model.
|
|
|
|
'''
|
|
|
|
'''
|
|
|
@ -259,6 +266,7 @@ class app:
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
self.prepare_heightmap()
|
|
|
|
self.prepare_heightmap()
|
|
|
|
|
|
|
|
self.get_ID()
|
|
|
|
|
|
|
|
|
|
|
|
if self.mode == "2d":
|
|
|
|
if self.mode == "2d":
|
|
|
|
self.make_stl_planar()
|
|
|
|
self.make_stl_planar()
|
|
|
@ -270,12 +278,13 @@ class app:
|
|
|
|
self.error_exit("Mode not supported")
|
|
|
|
self.error_exit("Mode not supported")
|
|
|
|
|
|
|
|
|
|
|
|
plt.show()
|
|
|
|
plt.show()
|
|
|
|
print(f"Saving model to ", self.stl_file, file=sys.stderr)
|
|
|
|
|
|
|
|
self.save_stl()
|
|
|
|
self.save_stl()
|
|
|
|
|
|
|
|
print(f"Saving model to ", self.stl_file, file=sys.stderr)
|
|
|
|
|
|
|
|
|
|
|
|
def prepare_heightmap(self):
|
|
|
|
def prepare_heightmap(self):
|
|
|
|
'''Modify image values to get usable height/depth values.
|
|
|
|
'''Modify image values to get usable height/depth values.
|
|
|
|
Check validity of dimension parameters.
|
|
|
|
Check validity of dimension parameters.
|
|
|
|
|
|
|
|
Prepare meshgrid.
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
# TODO: redo, too complicated, add extra params, redo checks
|
|
|
|
# TODO: redo, too complicated, add extra params, redo checks
|
|
|
@ -303,12 +312,84 @@ class app:
|
|
|
|
# Transform image values to get a heightmap
|
|
|
|
# Transform image values to get a heightmap
|
|
|
|
self.img = (1 - self.img/255) * self.height_line
|
|
|
|
self.img = (1 - self.img/255) * self.height_line
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# This sets the size of stl model and number of subdivisions / triangles
|
|
|
|
|
|
|
|
x = np.linspace(0, self.width * 25.4 / self.dpi, self.width)
|
|
|
|
|
|
|
|
y = np.linspace(0, self.height * 25.4 / self.dpi, self.height)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.meshgrid = np.meshgrid(x, y)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_ID(self):
|
|
|
|
|
|
|
|
'''Get unique ID for the model.
|
|
|
|
|
|
|
|
Consists of pair input_file + preset_name.
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
# TODO: somehow compress this to fit it onto the model, maybe zlib
|
|
|
|
|
|
|
|
self.id = self.input_file.split(
|
|
|
|
|
|
|
|
"/")[-1].split(".")[0] + "_" + self.preset_name
|
|
|
|
|
|
|
|
print(self.id)
|
|
|
|
|
|
|
|
|
|
|
|
def append_faces(self, faces, c):
|
|
|
|
def append_faces(self, faces, c):
|
|
|
|
# Function to add faces to the list
|
|
|
|
# Function to add faces to the list
|
|
|
|
faces.append([c, c + 1, c + 2])
|
|
|
|
faces.append([c, c + 1, c + 2])
|
|
|
|
faces.append([c + 1, c + 3, c + 2])
|
|
|
|
faces.append([c + 1, c + 3, c + 2])
|
|
|
|
return c + 4
|
|
|
|
return c + 4
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def engrave_text(self, bottom_vert_arr):
|
|
|
|
|
|
|
|
'''Engrave text on the back of the model.
|
|
|
|
|
|
|
|
Create an empty image, fill it with color and draw text on it.
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fig, ax = self.get_empty_figure()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# paint the background black
|
|
|
|
|
|
|
|
ax.plot([0, 1], [0, 1], c="black", lw=self.width)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# extract text from filename
|
|
|
|
|
|
|
|
text = self.stl_file.split("/")[-1].split(".")[0] + self.id
|
|
|
|
|
|
|
|
fontsize = 20
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# create text object, paint it white
|
|
|
|
|
|
|
|
t = ax.text(0.5, 0.5, text, ha="center", va="center",
|
|
|
|
|
|
|
|
fontsize=30, c="white", rotation=90, wrap=True, clip_on=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# adjust fontsize to fit text in the image
|
|
|
|
|
|
|
|
# matplotlib does not support multiline text, wrapping is broken
|
|
|
|
|
|
|
|
rend = fig.canvas.get_renderer()
|
|
|
|
|
|
|
|
while (t.get_window_extent(rend).width > self.width or t.get_window_extent(rend).height > self.height):
|
|
|
|
|
|
|
|
fontsize -= 0.3
|
|
|
|
|
|
|
|
t.set_fontsize(fontsize)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# update figure, read pixels and reshape to 3d array
|
|
|
|
|
|
|
|
fig.canvas.draw()
|
|
|
|
|
|
|
|
data = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
|
|
|
|
|
|
|
|
data = data.reshape(fig.canvas.get_width_height()[::-1] + (3,))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# scale inscription layer to suitable height
|
|
|
|
|
|
|
|
data = (data/255)/10
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
plt.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# TODO: maybe don't use nested for loops, use numpy?
|
|
|
|
|
|
|
|
for i in range(self.height):
|
|
|
|
|
|
|
|
for j in range(self.width):
|
|
|
|
|
|
|
|
bottom_vert_arr[i][j][2] = data[i][j][0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return bottom_vert_arr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_stl_mesh(self, faces, vertices):
|
|
|
|
|
|
|
|
'''Create mesh from faces and vertices.
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Convert lists to numpy arrays
|
|
|
|
|
|
|
|
faces = np.array(faces)
|
|
|
|
|
|
|
|
vertices = np.array(vertices)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Create the mesh - vertices.shape (no_faces, 3, 3)
|
|
|
|
|
|
|
|
self.stl_model = mesh.Mesh(
|
|
|
|
|
|
|
|
np.zeros(faces.shape[0], dtype=mesh.Mesh.dtype))
|
|
|
|
|
|
|
|
for i, face in enumerate(faces):
|
|
|
|
|
|
|
|
for j in range(3):
|
|
|
|
|
|
|
|
self.stl_model.vectors[i][j] = vertices[face[j], :]
|
|
|
|
|
|
|
|
|
|
|
|
def make_stl_planar(self):
|
|
|
|
def make_stl_planar(self):
|
|
|
|
'''Create mesh from meshgrid.
|
|
|
|
'''Create mesh from meshgrid.
|
|
|
|
Create vertices from meshgrid, add depth values from image.
|
|
|
|
Create vertices from meshgrid, add depth values from image.
|
|
|
@ -326,123 +407,87 @@ class app:
|
|
|
|
endfacet
|
|
|
|
endfacet
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
# This sets the size of stl model and number of subdivisions / triangles
|
|
|
|
# Add the image matrix to the 2D meshgrid and create 1D array of 3D points
|
|
|
|
x = np.linspace(0, self.width * 25.4 / self.dpi, self.width)
|
|
|
|
top_vert_arr = np.vstack(list(map(np.ravel, self.meshgrid))).T
|
|
|
|
y = np.linspace(0, self.height * 25.4 / self.dpi, self.height)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.meshgrid_2d = np.meshgrid(x, y)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Add the image matrix to the 2D meshgrid and create 1D array of 3D pointsd
|
|
|
|
|
|
|
|
vertex_arr = np.vstack(list(map(np.ravel, self.meshgrid_2d))).T
|
|
|
|
|
|
|
|
z = (self.img / 10).reshape(-1, 1)
|
|
|
|
z = (self.img / 10).reshape(-1, 1)
|
|
|
|
vertex_arr = np.concatenate((vertex_arr, z), axis=1)
|
|
|
|
top_vert_arr = np.concatenate((top_vert_arr, z), axis=1)
|
|
|
|
|
|
|
|
|
|
|
|
# Convert 1D array back to matrix of 3D points
|
|
|
|
# Convert 1D array back to matrix of 3D points
|
|
|
|
vertex_arr = vertex_arr.reshape(self.height, self.width, 3)
|
|
|
|
top_vert_arr = top_vert_arr.reshape(self.height, self.width, 3)
|
|
|
|
|
|
|
|
|
|
|
|
count = 0
|
|
|
|
count = 0
|
|
|
|
vertices = []
|
|
|
|
vertices = []
|
|
|
|
faces = []
|
|
|
|
faces = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# TODO: don't like this, could be done using numpy vectorisation?
|
|
|
|
# Iterate over all vertices, create faces
|
|
|
|
# Iterate over all vertices, create faces
|
|
|
|
for i in range(self.height - 1):
|
|
|
|
for i in range(self.height - 1):
|
|
|
|
for j in range(self.width - 1):
|
|
|
|
for j in range(self.width - 1):
|
|
|
|
|
|
|
|
|
|
|
|
vertices.append([vertex_arr[i][j]])
|
|
|
|
vertices.append([top_vert_arr[i][j]])
|
|
|
|
vertices.append([vertex_arr[i][j+1]])
|
|
|
|
vertices.append([top_vert_arr[i][j+1]])
|
|
|
|
vertices.append([vertex_arr[i+1][j]])
|
|
|
|
vertices.append([top_vert_arr[i+1][j]])
|
|
|
|
vertices.append([vertex_arr[i+1][j+1]])
|
|
|
|
vertices.append([top_vert_arr[i+1][j+1]])
|
|
|
|
|
|
|
|
|
|
|
|
count = self.append_faces(faces, count)
|
|
|
|
count = self.append_faces(faces, count)
|
|
|
|
|
|
|
|
|
|
|
|
# Add faces for the backside of the lithophane
|
|
|
|
# Prepare image with plotted text for the backside of the lithophane
|
|
|
|
null_arr = np.copy(vertex_arr)
|
|
|
|
bottom_vert_arr = np.copy(top_vert_arr)
|
|
|
|
for i in range(self.height):
|
|
|
|
self.engrave_text(bottom_vert_arr)
|
|
|
|
for j in range(self.width):
|
|
|
|
|
|
|
|
null_arr[i][j][2] = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Back side faces
|
|
|
|
# Back side faces
|
|
|
|
for i in range(self.height - 1):
|
|
|
|
for i in range(self.height - 1):
|
|
|
|
for j in range(self.width - 1):
|
|
|
|
for j in range(self.width - 1):
|
|
|
|
|
|
|
|
|
|
|
|
vertices.append([null_arr[i][j]])
|
|
|
|
vertices.append([bottom_vert_arr[i][j]])
|
|
|
|
vertices.append([null_arr[i+1][j]])
|
|
|
|
vertices.append([bottom_vert_arr[i+1][j]])
|
|
|
|
vertices.append([null_arr[i][j+1]])
|
|
|
|
vertices.append([bottom_vert_arr[i][j+1]])
|
|
|
|
vertices.append([null_arr[i+1][j+1]])
|
|
|
|
vertices.append([bottom_vert_arr[i+1][j+1]])
|
|
|
|
|
|
|
|
|
|
|
|
count = self.append_faces(faces, count)
|
|
|
|
count = self.append_faces(faces, count)
|
|
|
|
|
|
|
|
|
|
|
|
# Horizontal side faces
|
|
|
|
# Horizontal side faces
|
|
|
|
for i in range(self.height - 1):
|
|
|
|
for i in range(self.height - 1):
|
|
|
|
vertices.append([vertex_arr[i][0]])
|
|
|
|
vertices.append([top_vert_arr[i][0]])
|
|
|
|
vertices.append([vertex_arr[i+1][0]])
|
|
|
|
vertices.append([top_vert_arr[i+1][0]])
|
|
|
|
vertices.append([null_arr[i][0]])
|
|
|
|
vertices.append([bottom_vert_arr[i][0]])
|
|
|
|
vertices.append([null_arr[i+1][0]])
|
|
|
|
vertices.append([bottom_vert_arr[i+1][0]])
|
|
|
|
|
|
|
|
|
|
|
|
count = self.append_faces(faces, count)
|
|
|
|
count = self.append_faces(faces, count)
|
|
|
|
|
|
|
|
|
|
|
|
max = self.width - 1
|
|
|
|
max = self.width - 1
|
|
|
|
|
|
|
|
|
|
|
|
vertices.append([vertex_arr[i+1][max]])
|
|
|
|
vertices.append([top_vert_arr[i+1][max]])
|
|
|
|
vertices.append([vertex_arr[i][max]])
|
|
|
|
vertices.append([top_vert_arr[i][max]])
|
|
|
|
vertices.append([null_arr[i+1][max]])
|
|
|
|
vertices.append([bottom_vert_arr[i+1][max]])
|
|
|
|
vertices.append([null_arr[i][max]])
|
|
|
|
vertices.append([bottom_vert_arr[i][max]])
|
|
|
|
|
|
|
|
|
|
|
|
count = self.append_faces(faces, count)
|
|
|
|
count = self.append_faces(faces, count)
|
|
|
|
|
|
|
|
|
|
|
|
# Vertical side faces
|
|
|
|
# Vertical side faces
|
|
|
|
for j in range(self.width - 1):
|
|
|
|
for j in range(self.width - 1):
|
|
|
|
vertices.append([vertex_arr[0][j+1]])
|
|
|
|
vertices.append([top_vert_arr[0][j+1]])
|
|
|
|
vertices.append([vertex_arr[0][j]])
|
|
|
|
vertices.append([top_vert_arr[0][j]])
|
|
|
|
vertices.append([null_arr[0][j+1]])
|
|
|
|
vertices.append([bottom_vert_arr[0][j+1]])
|
|
|
|
vertices.append([null_arr[0][j]])
|
|
|
|
vertices.append([bottom_vert_arr[0][j]])
|
|
|
|
|
|
|
|
|
|
|
|
count = self.append_faces(faces, count)
|
|
|
|
count = self.append_faces(faces, count)
|
|
|
|
|
|
|
|
|
|
|
|
max = self.height - 1
|
|
|
|
max = self.height - 1
|
|
|
|
|
|
|
|
|
|
|
|
vertices.append([vertex_arr[max][j]])
|
|
|
|
vertices.append([top_vert_arr[max][j]])
|
|
|
|
vertices.append([vertex_arr[max][j+1]])
|
|
|
|
vertices.append([top_vert_arr[max][j+1]])
|
|
|
|
vertices.append([null_arr[max][j]])
|
|
|
|
vertices.append([bottom_vert_arr[max][j]])
|
|
|
|
vertices.append([null_arr[max][j+1]])
|
|
|
|
vertices.append([bottom_vert_arr[max][j+1]])
|
|
|
|
|
|
|
|
|
|
|
|
count = self.append_faces(faces, count)
|
|
|
|
count = self.append_faces(faces, count)
|
|
|
|
|
|
|
|
|
|
|
|
# Convert to numpy arrays
|
|
|
|
self.create_stl_mesh(faces, vertices)
|
|
|
|
faces = np.array(faces)
|
|
|
|
|
|
|
|
vertices = np.array(vertices)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Create the mesh - vertices.shape (no_faces, 3, 3)
|
|
|
|
|
|
|
|
self.stl_lithophane = mesh.Mesh(
|
|
|
|
|
|
|
|
np.zeros(faces.shape[0], dtype=mesh.Mesh.dtype))
|
|
|
|
|
|
|
|
for i, face in enumerate(faces):
|
|
|
|
|
|
|
|
for j in range(3):
|
|
|
|
|
|
|
|
self.stl_lithophane.vectors[i][j] = vertices[face[j], :]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def make_stl_curved(self):
|
|
|
|
def make_stl_curved(self):
|
|
|
|
'''Map fingerprint to finger model.
|
|
|
|
'''Map fingerprint to finger model.
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
# TODO: if this is the same as 2D, move to heightmap to reduce duplicate code
|
|
|
|
|
|
|
|
x = np.linspace(0, self.width * 25.4 / self.dpi, self.width)
|
|
|
|
|
|
|
|
y = np.linspace(0, self.height * 25.4 / self.dpi, self.height)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.meshgrid_3d = np.meshgrid(x, y)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Method 1 - logspace and logarithmic curve
|
|
|
|
|
|
|
|
'''z1 = np.logspace(0, 10, int(np.ceil(self.width / 2)), base=0.7)
|
|
|
|
|
|
|
|
z2 = np.logspace(10, 0, int(np.floor(self.width / 2)), base=0.7)
|
|
|
|
|
|
|
|
ztemp = 5*np.concatenate((z1, z2))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
z = np.array([])
|
|
|
|
|
|
|
|
for i in range(self.height):
|
|
|
|
|
|
|
|
z = np.concatenate((z, ztemp + 25*(((i+50)/20)**(-1/2))))
|
|
|
|
|
|
|
|
z = z.reshape(-1, 1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.img = (self.img / 10).reshape(-1, 1)
|
|
|
|
|
|
|
|
z += self.img'''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Method 2 - 2 ellipses
|
|
|
|
|
|
|
|
z = np.array([])
|
|
|
|
z = np.array([])
|
|
|
|
for x in range(self.width):
|
|
|
|
for x in range(self.width):
|
|
|
|
z = np.append(z, np.sqrt(1 - (2*x/self.width - 1)**2)
|
|
|
|
z = np.append(z, np.sqrt(1 - (2*x/self.width - 1)**2)
|
|
|
@ -453,133 +498,118 @@ class app:
|
|
|
|
* (self.curv_rate_y**2))
|
|
|
|
* (self.curv_rate_y**2))
|
|
|
|
z[y] = z[y] + new
|
|
|
|
z[y] = z[y] + new
|
|
|
|
|
|
|
|
|
|
|
|
# TODO: clip responsivelly
|
|
|
|
# TODO: clip responsivelly to save material used to print the model
|
|
|
|
bottom = z[0][math.floor(self.width/2)]
|
|
|
|
#bottom = z[0][math.floor(self.width/2)]
|
|
|
|
#top = self.curv_rate_x**2 + self.curv_rate_y
|
|
|
|
|
|
|
|
#np.clip(z, bottom, top, out=z)
|
|
|
|
|
|
|
|
z = z.reshape(-1, 1)
|
|
|
|
z = z.reshape(-1, 1)
|
|
|
|
self.img = (self.img / 10).reshape(-1, 1)
|
|
|
|
self.img = (self.img / 10).reshape(-1, 1)
|
|
|
|
z += self.img
|
|
|
|
z += self.img
|
|
|
|
|
|
|
|
|
|
|
|
vertex_arr = np.vstack(list(map(np.ravel, self.meshgrid_3d))).T
|
|
|
|
top_vert_arr = np.vstack(list(map(np.ravel, self.meshgrid))).T
|
|
|
|
vertex_arr = np.concatenate((vertex_arr, z), axis=1)
|
|
|
|
top_vert_arr = np.concatenate((top_vert_arr, z), axis=1)
|
|
|
|
vertex_arr = vertex_arr.reshape(self.height, self.width, 3)
|
|
|
|
top_vert_arr = top_vert_arr.reshape(self.height, self.width, 3)
|
|
|
|
|
|
|
|
|
|
|
|
count = 0
|
|
|
|
count = 0
|
|
|
|
vertices = []
|
|
|
|
vertices = []
|
|
|
|
faces = []
|
|
|
|
faces = []
|
|
|
|
min_point = 0
|
|
|
|
|
|
|
|
for i in range(self.height - 1):
|
|
|
|
#min_point = 0
|
|
|
|
if vertex_arr[i][0][2] <= bottom:
|
|
|
|
#for i in range(self.height - 1):
|
|
|
|
min_point = i
|
|
|
|
# if top_vert_arr[i][0][2] <= bottom:
|
|
|
|
|
|
|
|
# min_point = i
|
|
|
|
|
|
|
|
|
|
|
|
# Add faces for the backside of the lithophane
|
|
|
|
# Add faces for the backside of the lithophane
|
|
|
|
vec_side = (vertex_arr[self.height-1][0][2] -
|
|
|
|
#vec_side = (top_vert_arr[self.height-1][0][2] -
|
|
|
|
vertex_arr[min_point][0][2]) / (self.height - min_point)
|
|
|
|
# top_vert_arr[min_point][0][2]) / (self.height - min_point)
|
|
|
|
null_arr = np.copy(vertex_arr)
|
|
|
|
bottom_vert_arr = np.copy(top_vert_arr)
|
|
|
|
for i in range(self.height):
|
|
|
|
self.engrave_text(bottom_vert_arr)
|
|
|
|
for j in range(self.width):
|
|
|
|
|
|
|
|
null_arr[i][j][2] = 0
|
|
|
|
# TODO: code bellow is duplicate of the code in planar generation
|
|
|
|
#null_arr[i][j][2] = bottom + vec_side * i
|
|
|
|
# if not changed move to a separate function and simplify
|
|
|
|
# for smaller mesh
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Iterate over all vertices, create faces
|
|
|
|
# Iterate over all vertices, create faces
|
|
|
|
for i in range(self.height - 1):
|
|
|
|
for i in range(self.height - 1):
|
|
|
|
for j in range(self.width - 1):
|
|
|
|
for j in range(self.width - 1):
|
|
|
|
if (vertex_arr[i][j][2] <= null_arr[i][j][2]
|
|
|
|
if (top_vert_arr[i][j][2] <= bottom_vert_arr[i][j][2]
|
|
|
|
or vertex_arr[i+1][j][2] <= null_arr[i+1][j][2]
|
|
|
|
or top_vert_arr[i+1][j][2] <= bottom_vert_arr[i+1][j][2]
|
|
|
|
or vertex_arr[i][j+1][2] <= null_arr[i][j+1][2]
|
|
|
|
or top_vert_arr[i][j+1][2] <= bottom_vert_arr[i][j+1][2]
|
|
|
|
or vertex_arr[i+1][j+1][2] <= null_arr[i+1][j+1][2]):
|
|
|
|
or top_vert_arr[i+1][j+1][2] <= bottom_vert_arr[i+1][j+1][2]):
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
vertices.append([vertex_arr[i][j]])
|
|
|
|
vertices.append([top_vert_arr[i][j]])
|
|
|
|
vertices.append([vertex_arr[i][j+1]])
|
|
|
|
vertices.append([top_vert_arr[i][j+1]])
|
|
|
|
vertices.append([vertex_arr[i+1][j]])
|
|
|
|
vertices.append([top_vert_arr[i+1][j]])
|
|
|
|
vertices.append([vertex_arr[i+1][j+1]])
|
|
|
|
vertices.append([top_vert_arr[i+1][j+1]])
|
|
|
|
|
|
|
|
|
|
|
|
count = self.append_faces(faces, count)
|
|
|
|
count = self.append_faces(faces, count)
|
|
|
|
|
|
|
|
|
|
|
|
# Rotated back side faces
|
|
|
|
# Rotated back side faces
|
|
|
|
for i in range(self.height - 1):
|
|
|
|
for i in range(self.height - 1):
|
|
|
|
for j in range(self.width - 1):
|
|
|
|
for j in range(self.width - 1):
|
|
|
|
if (vertex_arr[i][j][2] <= null_arr[i][j][2]):
|
|
|
|
if (top_vert_arr[i][j][2] <= bottom_vert_arr[i][j][2]):
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
|
|
vertices.append([null_arr[i][j]])
|
|
|
|
vertices.append([bottom_vert_arr[i][j]])
|
|
|
|
vertices.append([null_arr[i+1][j]])
|
|
|
|
vertices.append([bottom_vert_arr[i+1][j]])
|
|
|
|
vertices.append([null_arr[i][j+1]])
|
|
|
|
vertices.append([bottom_vert_arr[i][j+1]])
|
|
|
|
vertices.append([null_arr[i+1][j+1]])
|
|
|
|
vertices.append([bottom_vert_arr[i+1][j+1]])
|
|
|
|
|
|
|
|
|
|
|
|
count = self.append_faces(faces, count)
|
|
|
|
count = self.append_faces(faces, count)
|
|
|
|
|
|
|
|
|
|
|
|
# Horizontal side faces
|
|
|
|
# Horizontal side faces
|
|
|
|
for i in range(self.height - 1): # right
|
|
|
|
for i in range(self.height - 1): # right
|
|
|
|
#if (vertex_arr[i][0][2] < null_arr[i][0][2]):
|
|
|
|
#if (top_vert_arr[i][0][2] < bottom_vert_arr[i][0][2]):
|
|
|
|
# continue
|
|
|
|
# continue
|
|
|
|
|
|
|
|
|
|
|
|
vertices.append([vertex_arr[i][0]])
|
|
|
|
vertices.append([top_vert_arr[i][0]])
|
|
|
|
vertices.append([vertex_arr[i+1][0]])
|
|
|
|
vertices.append([top_vert_arr[i+1][0]])
|
|
|
|
vertices.append([null_arr[i][0]])
|
|
|
|
vertices.append([bottom_vert_arr[i][0]])
|
|
|
|
vertices.append([null_arr[i+1][0]])
|
|
|
|
vertices.append([bottom_vert_arr[i+1][0]])
|
|
|
|
|
|
|
|
|
|
|
|
count = self.append_faces(faces, count)
|
|
|
|
count = self.append_faces(faces, count)
|
|
|
|
|
|
|
|
|
|
|
|
for i in range(self.height - 1): # left
|
|
|
|
for i in range(self.height - 1): # left
|
|
|
|
max = self.width - 1
|
|
|
|
max = self.width - 1
|
|
|
|
#if (vertex_arr[i][max][2] < null_arr[i][max][2]):
|
|
|
|
#if (top_vert_arr[i][max][2] < bottom_vert_arr[i][max][2]):
|
|
|
|
# continue
|
|
|
|
# continue
|
|
|
|
|
|
|
|
|
|
|
|
vertices.append([vertex_arr[i+1][max]])
|
|
|
|
vertices.append([top_vert_arr[i+1][max]])
|
|
|
|
vertices.append([vertex_arr[i][max]])
|
|
|
|
vertices.append([top_vert_arr[i][max]])
|
|
|
|
vertices.append([null_arr[i+1][max]])
|
|
|
|
vertices.append([bottom_vert_arr[i+1][max]])
|
|
|
|
vertices.append([null_arr[i][max]])
|
|
|
|
vertices.append([bottom_vert_arr[i][max]])
|
|
|
|
|
|
|
|
|
|
|
|
count = self.append_faces(faces, count)
|
|
|
|
count = self.append_faces(faces, count)
|
|
|
|
|
|
|
|
|
|
|
|
# Vertical side faces
|
|
|
|
# Vertical side faces
|
|
|
|
for j in range(self.width - 1): # top
|
|
|
|
for j in range(self.width - 1): # top
|
|
|
|
#if (vertex_arr[0][j][2] < null_arr[0][j][2]):
|
|
|
|
#if (top_vert_arr[0][j][2] < bottom_vert_arr[0][j][2]):
|
|
|
|
# continue
|
|
|
|
# continue
|
|
|
|
|
|
|
|
|
|
|
|
vertices.append([vertex_arr[0][j+1]])
|
|
|
|
vertices.append([top_vert_arr[0][j+1]])
|
|
|
|
vertices.append([vertex_arr[0][j]])
|
|
|
|
vertices.append([top_vert_arr[0][j]])
|
|
|
|
vertices.append([null_arr[0][j+1]])
|
|
|
|
vertices.append([bottom_vert_arr[0][j+1]])
|
|
|
|
vertices.append([null_arr[0][j]])
|
|
|
|
vertices.append([bottom_vert_arr[0][j]])
|
|
|
|
|
|
|
|
|
|
|
|
count = self.append_faces(faces, count)
|
|
|
|
count = self.append_faces(faces, count)
|
|
|
|
|
|
|
|
|
|
|
|
for j in range(self.width - 1): # bottom
|
|
|
|
for j in range(self.width - 1): # bottom
|
|
|
|
max = self.height - 1
|
|
|
|
max = self.height - 1
|
|
|
|
#if (vertex_arr[max][j][2] < null_arr[max][j][2]):
|
|
|
|
#if (top_vert_arr[max][j][2] < bottom_vert_arr[max][j][2]):
|
|
|
|
# continue
|
|
|
|
# continue
|
|
|
|
|
|
|
|
|
|
|
|
vertices.append([vertex_arr[max][j]])
|
|
|
|
vertices.append([top_vert_arr[max][j]])
|
|
|
|
vertices.append([vertex_arr[max][j+1]])
|
|
|
|
vertices.append([top_vert_arr[max][j+1]])
|
|
|
|
vertices.append([null_arr[max][j]])
|
|
|
|
vertices.append([bottom_vert_arr[max][j]])
|
|
|
|
vertices.append([null_arr[max][j+1]])
|
|
|
|
vertices.append([bottom_vert_arr[max][j+1]])
|
|
|
|
|
|
|
|
|
|
|
|
count = self.append_faces(faces, count)
|
|
|
|
count = self.append_faces(faces, count)
|
|
|
|
|
|
|
|
|
|
|
|
# Convert to numpy arrays
|
|
|
|
self.create_stl_mesh(faces, vertices)
|
|
|
|
faces = np.array(faces)
|
|
|
|
|
|
|
|
vertices = np.array(vertices)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Create the mesh - vertices.shape (no_faces, 3, 3)
|
|
|
|
|
|
|
|
self.mesh_finger = mesh.Mesh(
|
|
|
|
|
|
|
|
np.zeros(faces.shape[0], dtype=mesh.Mesh.dtype))
|
|
|
|
|
|
|
|
for i, face in enumerate(faces):
|
|
|
|
|
|
|
|
for j in range(3):
|
|
|
|
|
|
|
|
self.mesh_finger.vectors[i][j] = vertices[face[j], :]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# print(self.mesh_finger.normals)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def save_stl(self):
|
|
|
|
def save_stl(self):
|
|
|
|
'''Save final mesh to stl file.
|
|
|
|
'''Save final mesh to stl file.
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
# TODO: add a hash function to create filename specific to input image and preset
|
|
|
|
# TODO: add a hash function to create filename specific to input image and preset
|
|
|
|
if self.mode == "3d":
|
|
|
|
self.stl_file = self.stl_file.split(".")[0] + "_" + self.id + "." + self.stl_file.split(".")[1]
|
|
|
|
self.mesh_finger.save(self.stl_file)
|
|
|
|
self.stl_model.save(self.stl_file)
|
|
|
|
else:
|
|
|
|
|
|
|
|
self.stl_lithophane.save(self.stl_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# run the application
|
|
|
|
# run the application
|
|
|
|