- fixed the Voronoi generation in the Autolevelling Tool (removed the Foronoi package due of issues and now using the embedded functionality from Shapely)

This commit is contained in:
Marius Stanciu
2023-01-16 23:52:30 +02:00
parent a1b6f78d78
commit 853b273e79
2 changed files with 67 additions and 58 deletions

View File

@@ -21,16 +21,22 @@ from io import StringIO
from matplotlib.backend_bases import KeyEvent as mpl_key_event from matplotlib.backend_bases import KeyEvent as mpl_key_event
# try:
# from foronoi import Voronoi
# from foronoi import Polygon as voronoi_poly
# VORONOI_ENABLED = True
# except Exception:
# try:
# from shapely.ops import voronoi_diagram
# VORONOI_ENABLED = True
# # from appCommon.Common import voronoi_diagram
# except Exception:
# VORONOI_ENABLED = False
try: try:
from foronoi import Voronoi
from foronoi import Polygon as voronoi_poly
VORONOI_ENABLED = True
except Exception:
try:
from shapely.ops import voronoi_diagram from shapely.ops import voronoi_diagram
VORONOI_ENABLED = True VORONOI_ENABLED = True
# from appCommon.Common import voronoi_diagram # from appCommon.Common import voronoi_diagram
except Exception: except Exception:
VORONOI_ENABLED = False VORONOI_ENABLED = False
fcTranslate.apply_language('strings') fcTranslate.apply_language('strings')
@@ -548,7 +554,7 @@ class ToolLevelling(AppTool, CNCjob):
al_method = self.ui.al_method_radio.get_value() al_method = self.ui.al_method_radio.get_value()
if al_method == 'v': if al_method == 'v':
if VORONOI_ENABLED is True: if VORONOI_ENABLED is True:
self.generate_voronoi_geometry_2(pts=vor_pts_list) self.generate_voronoi_geometry(pts=vor_pts_list)
# generate Probing GCode # generate Probing GCode
self.probing_gcode_text = self.probing_gcode(storage=self.al_voronoi_geo_storage) self.probing_gcode_text = self.probing_gcode(storage=self.al_voronoi_geo_storage)
else: else:
@@ -736,7 +742,7 @@ class ToolLevelling(AppTool, CNCjob):
return return
new_voronoi = [] new_voronoi = []
for p in voronoi_union: for p in voronoi_union.geoms:
new_voronoi.append(p.intersection(env)) new_voronoi.append(p.intersection(env))
for pt_key in list(self.al_voronoi_geo_storage.keys()): for pt_key in list(self.al_voronoi_geo_storage.keys()):
@@ -744,52 +750,55 @@ class ToolLevelling(AppTool, CNCjob):
if self.al_voronoi_geo_storage[pt_key]['point'].within(poly): if self.al_voronoi_geo_storage[pt_key]['point'].within(poly):
self.al_voronoi_geo_storage[pt_key]['geo'] = poly self.al_voronoi_geo_storage[pt_key]['geo'] = poly
def generate_voronoi_geometry_2(self, pts): # def generate_voronoi_geometry_2(self, pts):
env = self.solid_geo.envelope # env = self.solid_geo.envelope
fact = 1 if self.units == 'MM' else 0.039 # fact = 1 if self.units == 'MM' else 0.039
env = env.buffer(fact) # env = env.buffer(fact)
env_poly = voronoi_poly(list(env.exterior.coords)) # env_poly = voronoi_poly(tuple(env.exterior.coords))
new_pts = [[pt.x, pt.y] for pt in pts]
# Initialize the algorithm
v = Voronoi(env_poly)
# calculate the Voronoi diagram
try:
v.create_diagram(new_pts)
except AttributeError as e:
self.app.log.error("CNCJobObject.generate_voronoi_geometry_2() --> %s" % str(e))
new_pts_2 = []
for pt_index in range(len(new_pts)):
new_pts_2.append([
new_pts[pt_index][0] + random.random() * 1e-03,
new_pts[pt_index][1] + random.random() * 1e-03
])
try:
v.create_diagram(new_pts_2)
except Exception:
print("Didn't work.")
return
new_voronoi = []
for p in v.points:
p_coords = [(coord.x, coord.y) for coord in p.get_coordinates()]
new_pol = Polygon(p_coords)
new_voronoi.append(new_pol)
new_voronoi = MultiPolygon(new_voronoi)
# new_voronoi = []
# for p in voronoi_union:
# new_voronoi.append(p.intersection(env))
# #
for pt_key in list(self.al_voronoi_geo_storage.keys()): # new_pts = [[pt.x, pt.y] for pt in pts]
for poly in new_voronoi: # print(new_pts)
if self.al_voronoi_geo_storage[pt_key]['point'].within(poly) or \ # print(env_poly)
self.al_voronoi_geo_storage[pt_key]['point'].intersects(poly): #
self.al_voronoi_geo_storage[pt_key]['geo'] = poly # # Initialize the algorithm
# v = Voronoi(env_poly)
#
# # calculate the Voronoi diagram
# try:
# v.create_diagram(new_pts)
# except AttributeError as e:
# self.app.log.error("CNCJobObject.generate_voronoi_geometry_2() --> %s" % str(e))
# new_pts_2 = []
# for pt_index in range(len(new_pts)):
# new_pts_2.append([
# new_pts[pt_index][0] + random.random() * 1e-03,
# new_pts[pt_index][1] + random.random() * 1e-03
# ])
#
# try:
# v.create_diagram(new_pts_2)
# except Exception:
# print("Didn't work.")
# return
#
# new_voronoi = []
# for p in v.sites:
# # p_coords = [(coord.x, coord.y) for coord in p.get_coordinates()]
# p_coords = [(p.x, p.y)]
# new_pol = Polygon(p_coords)
# new_voronoi.append(new_pol)
#
# new_voronoi = MultiPolygon(new_voronoi)
#
# # new_voronoi = []
# # for p in voronoi_union:
# # new_voronoi.append(p.intersection(env))
# #
# for pt_key in list(self.al_voronoi_geo_storage.keys()):
# for poly in new_voronoi:
# if self.al_voronoi_geo_storage[pt_key]['point'].within(poly) or \
# self.al_voronoi_geo_storage[pt_key]['point'].intersects(poly):
# self.al_voronoi_geo_storage[pt_key]['geo'] = poly
def generate_bilinear_geometry(self, pts): def generate_bilinear_geometry(self, pts):
self.al_bilinear_geo_storage = pts self.al_bilinear_geo_storage = pts
@@ -878,7 +887,7 @@ class ToolLevelling(AppTool, CNCjob):
pts_list = [] pts_list = []
for k in self.al_voronoi_geo_storage: for k in self.al_voronoi_geo_storage:
pts_list.append(self.al_voronoi_geo_storage[k]['point']) pts_list.append(self.al_voronoi_geo_storage[k]['point'])
self.generate_voronoi_geometry_2(pts=pts_list) self.generate_voronoi_geometry(pts=pts_list)
self.probing_gcode_text = self.probing_gcode(self.al_voronoi_geo_storage) self.probing_gcode_text = self.probing_gcode(self.al_voronoi_geo_storage)
else: else:

View File

@@ -17,7 +17,7 @@ simplejson
qrcode>=6.1 qrcode>=6.1
rtree rtree
foronoi>=1.0.3 # foronoi>=1.0.3
shapely>=1.8.0 shapely>=1.8.0
# ############################### # ###############################