Commit c8500283 authored by Nicholas Gar Hei Chan's avatar Nicholas Gar Hei Chan
Browse files

Added line type detection

parent 5bc9b2e9
......@@ -13,6 +13,7 @@ try:
import matplotlib.pyplot as plt
except:
print("Matplotlib not installed")
import time
class Model():
"""Model class for storing layer and max/min data"""
......@@ -63,6 +64,7 @@ class Line():
def __init__(self):
self.x = []
self.y = []
self.line_type = "regular"
def append_coords(self, x, y):
if len(self.x) == 0:
......@@ -72,12 +74,16 @@ class Line():
prev_x = self.x[-1]
prev_y = self.y[-1]
segment_length = np.sqrt((x-prev_x)**2+(y-prev_y)**2)
num_points = int(np.floor(segment_length/1))
num_points = int(np.floor(segment_length/0.1))
if segment_length > 2.5:
if len(self.x) > 5:
self.line_type = "regular"
else:
self.line_type = "infill"
if num_points < 2:
self.x.append(x)
self.y.append(y)
else:
if x < prev_x:
xs = np.linspace(x, prev_x, num_points)
ys = np.interp(xs, [x, prev_x], [y, prev_y])
......@@ -88,9 +94,7 @@ class Line():
ys = np.interp(xs, [prev_x, x], [prev_y, y])
self.x.extend(xs[1:])
self.y.extend(ys[1:])
#print('-----')
#print(xs)
#print(ys)
def len(self):
return len(self.x) - 1
......@@ -196,9 +200,12 @@ class Layer():
out += '</svg>'
return out
def plot_layer(self):
def plot_layer(self, col_reg, col_infill):
for line in self.lines:
plt.plot(line.x, line.y, '-xk')
if line.line_type == "regular":
plt.plot(line.x, line.y, col_reg) #self.z_height,
elif line.line_type == "infill":
plt.plot(line.x, line.y, col_infill)
def to_csv(self, fn):
with open(fn, "w") as f:
......@@ -208,12 +215,12 @@ class Layer():
def len(self):
return len([line for line in self.lines if line.len() > 0])
def get_points(self):
def get_points(self, ignore_infill = False):
x_pts = []
y_pts = []
[x_pts.extend(line.x) for line in self.lines]
[y_pts.extend(line.y) for line in self.lines]
[x_pts.extend(line.x) for line in self.lines if ignore_infill is not True or line.line_type is "regular"]
[y_pts.extend(line.y) for line in self.lines if ignore_infill is not True or line.line_type is "regular"]
return dict([('x', x_pts), ('y', y_pts), ('num_pts', len(x_pts))])
......@@ -243,16 +250,16 @@ class Layer():
[point_list.append([x,y]) for x,y in zip(points['x'], points['y'])]
features = np.array(point_list)
print(features.shape)
if features.shape[0] < 4:
self.sample_points = dict([('x', []), ('y', [])])
else:
hull = ConvexHull(features)
outside_indx = np.random.choice(hull.vertices, 3)
outside_indx = np.random.choice(hull.vertices, 10)
all_inside_indx = np.arange(1, len(point_list))
all_inside_indx = all_inside_indx[np.isin(all_inside_indx, hull.vertices, invert=True)]
inside_indx = np.random.choice(all_inside_indx, 3)
inside_indx = np.random.choice(all_inside_indx, 10)
all_points = np.concatenate((inside_indx, outside_indx));
x_pts = [feature[0] for feature in features[all_points]]
......@@ -276,6 +283,7 @@ def parse_gcode(filename, sample_spacing, samples_per_layer, method):
prev_z = 0
prev_e = 0
start = time.time()
with open(filename) as f: # Open GCODE file
file = f.readlines()
......@@ -324,7 +332,7 @@ def parse_gcode(filename, sample_spacing, samples_per_layer, method):
es.append(e_loc)
else:
es.append(prev_e)
print("Import time: {} seconds".format(time.time()-start))
[layer_heights.append(height) for height in set(zs)] # Find the unique Z heights in the GCODE file
layer_heights.sort() # Make it a sorted list
......@@ -336,13 +344,13 @@ def parse_gcode(filename, sample_spacing, samples_per_layer, method):
for i in range(len(lines)): # Iterate through each line
layers[index[zs[i]]].append_coords(xs[i],ys[i],es[i],Gs[i]) # Append each command into the model list
layers = [layer for layer in layers if layer.len() > 0]
layers = [layer for layer in layers if layer.len() > 0]
print("Regen time: {} seconds".format(time.time()-start))
for i in range(len(layers)):
if (i % sample_spacing) == 0 and (i != 0):
layers[i].gen_sample_points(method, samples_per_layer)
else:
layers[i].gen_sample_points("none", 0)
print("Total time: {} seconds".format(time.time()-start))
model = Model(layers, max(xs), max(ys), max(zs), min(xs), min(ys), min(zs))
return model
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment