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

Interpolation and testing changes

parent 57fe39aa
......@@ -65,9 +65,32 @@ class Line():
self.y = []
def append_coords(self, x, y):
self.x.append(x)
self.y.append(y)
if len(self.x) == 0:
self.x.append(x)
self.y.append(y)
else:
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/0.1))
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])
xs = np.flip(xs)
ys = np.flip(ys)
else:
xs = np.linspace(prev_x, x, num_points)
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
......@@ -175,7 +198,7 @@ class Layer():
def plot_layer(self):
for line in self.lines:
plt.plot(line.x, line.y, 'xk')
plt.plot(line.x, line.y, '-xk')
def to_csv(self, fn):
with open(fn, "w") as f:
......@@ -201,6 +224,20 @@ class Layer():
x_pts = [points['x'][i] for i in sample_pts]
y_pts = [points['y'][i] for i in sample_pts]
self.sample_points = dict([('x', x_pts), ('y', y_pts)])
elif method == "Min-max":
point_array = np.array([points['x'], points['y']])
max_vals = np.argmax(point_array, axis=1)
min_vals = np.argmin(point_array, axis=1)
rot45 = np.array([[0.7071, -0.7071],[0.7071, 0.7071]])
point_array_rot = np.matmul(rot45, point_array)
max_vals_rot = np.argmax(point_array_rot, axis=1)
min_vals_rot = np.argmin(point_array_rot, axis=1)
sample_pts = np.unique(np.concatenate((max_vals, min_vals, max_vals_rot, min_vals_rot))).tolist()
x_pts = [points['x'][i] for i in sample_pts]
y_pts = [points['y'][i] for i in sample_pts]
self.sample_points = dict([('x', x_pts), ('y', y_pts)])
if method == "none":
self.sample_points = dict([('x', []), ('y', [])])
......
This diff is collapsed.
......@@ -6,23 +6,24 @@ import matplotlib.pyplot as plt
import time
import random
import numpy as np
import scipy
import alphashape
sys.path.append(os.path.abspath(".."))
import gcodeparser as gcp
start = time.time()
model = gcp.parse_gcode(os.path.abspath("LM_rocktopus.gcode"), 5, 10)
model = gcp.parse_gcode(os.path.abspath("LM_rocktopus.gcode"), 5, 10, "Min-max")
print("Execution time: {} seconds".format(time.time()-start))
layer = model.layers[10]
#model.layers[10].plot_layer()
layer.to_svg(model.max_y, model.max_x, 'test.svg')
plt.show()
#layer.plot_layer()
#plt.show()
"""
Uncomment this block to save each layer to an SVG image
......@@ -30,3 +31,35 @@ into a folder called "layers"
"""
#for layer_index, layer in enumerate(model.layers):
# layer.to_svg(model.max_y, model.max_x, "layers/{}.svg".format(layer_index))
points = layer.get_points()
point_list = []
[point_list.append([x,y]) for x,y in zip(points['x'], points['y'])]
features = np.array(point_list)
from scipy.spatial import Delaunay
tri = Delaunay(features)
valid_simps = []
bound_simps = []
alpha = 0.8
edges = set()
for i, element in enumerate(tri.simplices):
l1 = np.sqrt(np.sum((features[element[0], :] - features[element[1], :])**2))
l2 = np.sqrt(np.sum((features[element[0], :] - features[element[2], :])**2))
l3 = np.sqrt(np.sum((features[element[1], :] - features[element[2], :])**2))
num_friendos = np.sum(tri.neighbors[i] != -1)
if l1 < alpha and l2 < alpha and l3 < alpha:
valid_simps.append(i)
for i, neighbors in enumerate(tri.neighbors[valid_simps]):
if np.sum(np.isin(valid_simps, neighbors)) < 3:
bound_simps.append(valid_simps[i])
plt.triplot(features[:,0], features[:,1], tri.simplices.copy()[bound_simps,:])
plt.show()
This diff is collapsed.
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