diff options
author | jaseg <git@jaseg.de> | 2021-11-10 21:39:03 +0100 |
---|---|---|
committer | jaseg <git@jaseg.de> | 2021-11-10 21:39:03 +0100 |
commit | d21a2e67ff34d3f29e37a01f926b9e8f72003637 (patch) | |
tree | 8218f339a3336283c060aa5657a76a463b8e1982 /gerbonara/gerber/utils.py | |
parent | 125eb821b9f5d4c58b17d43e318e9a6829120d03 (diff) | |
download | gerbonara-d21a2e67ff34d3f29e37a01f926b9e8f72003637.tar.gz gerbonara-d21a2e67ff34d3f29e37a01f926b9e8f72003637.tar.bz2 gerbonara-d21a2e67ff34d3f29e37a01f926b9e8f72003637.zip |
WIP
Diffstat (limited to 'gerbonara/gerber/utils.py')
-rw-r--r-- | gerbonara/gerber/utils.py | 145 |
1 files changed, 1 insertions, 144 deletions
diff --git a/gerbonara/gerber/utils.py b/gerbonara/gerber/utils.py index c2e81cd..492321a 100644 --- a/gerbonara/gerber/utils.py +++ b/gerbonara/gerber/utils.py @@ -61,7 +61,7 @@ def parse_gerber_value(value, settings): """ - if value is None: + if not value: return None # Handle excellon edge case with explicit decimal. "That was easy!" @@ -317,146 +317,3 @@ def sq_distance(point1, point2): return diff1 * diff1 + diff2 * diff2 -def listdir(directory, ignore_hidden=True, ignore_os=True): - """ List files in given directory. - Differs from os.listdir() in that hidden and OS-generated files are ignored - by default. - - Parameters - ---------- - directory : str - path to the directory for which to list files. - - ignore_hidden : bool - If True, ignore files beginning with a leading '.' - - ignore_os : bool - If True, ignore OS-generated files, e.g. Thumbs.db - - Returns - ------- - files : list - list of files in specified directory - """ - os_files = ('.DS_Store', 'Thumbs.db', 'ethumbs.db') - files = os.listdir(directory) - if ignore_hidden: - files = [f for f in files if not f.startswith('.')] - if ignore_os: - files = [f for f in files if not f in os_files] - return files - -def ConvexHull_qh(points): - #a hull must be a planar shape with nonzero area, so there must be at least 3 points - if(len(points)<3): - raise Exception("not a planar shape") - #find points with lowest and highest X coordinates - minxp=0; - maxxp=0; - for i in range(len(points)): - if(points[i][0]<points[minxp][0]): - minxp=i; - if(points[i][0]>points[maxxp][0]): - maxxp=i; - if minxp==maxxp: - #all points are collinear - raise Exception("not a planar shape") - #separate points into those above and those below the minxp-maxxp line - lpoints=[] - rpoints=[] - #to detemine if point X is on the left or right of dividing line A-B, compare slope of A-B to slope of A-X - #slope is (By-Ay)/(Bx-Ax) - a=points[minxp] - b=points[maxxp] - slopeab=atan2(b[1]-a[1],b[0]-a[0]) - for i in range(len(points)): - p=points[i] - if i == minxp or i == maxxp: - continue - slopep=atan2(p[1]-a[1],p[0]-a[0]) - sdiff=slopep-slopeab - if(sdiff<pi):sdiff+=2*pi - if(sdiff>pi):sdiff-=2*pi - if(sdiff>0): - lpoints+=[i] - if(sdiff<0): - rpoints+=[i] - hull=[minxp]+_findhull(rpoints, maxxp, minxp, points)+[maxxp]+_findhull(lpoints, minxp, maxxp, points) - hullo=_optimize(hull,points) - return hullo - -def _optimize(hull,points): - #find triplets that are collinear and remove middle point - toremove=[] - newhull=hull[:] - l=len(hull) - for i in range(l): - p1=hull[i] - p2=hull[(i+1)%l] - p3=hull[(i+2)%l] - #(p1.y-p2.y)*(p1.x-p3.x)==(p1.y-p3.y)*(p1.x-p2.x) - if (points[p1][1]-points[p2][1])*(points[p1][0]-points[p3][0])==(points[p1][1]-points[p3][1])*(points[p1][0]-points[p2][0]): - toremove+=[p2] - for i in toremove: - newhull.remove(i) - return newhull - -def _distance(a, b, x): - #find the distance between point x and line a-b - return abs((b[1]-a[1])*x[0]-(b[0]-a[0])*x[1]+b[0]*a[1]-a[0]*b[1])/sqrt((b[1]-a[1])**2 + (b[0]-a[0])**2 ); - -def _findhull(idxp, a_i, b_i, points): - #if no points in input, return no points in output - if(len(idxp)==0): - return []; - #find point c furthest away from line a-b - farpoint=-1 - fdist=-1.0; - for i in idxp: - d=_distance(points[a_i], points[b_i], points[i]) - if(d>fdist): - fdist=d; - farpoint=i - if(fdist<=0): - #none of the points have a positive distance from line, bad things have happened - return [] - #separate points into those inside triangle, those outside triangle left of far point, and those outside triangle right of far point - a=points[a_i] - b=points[b_i] - c=points[farpoint] - slopeac=atan2(c[1]-a[1],c[0]-a[0]) - slopecb=atan2(b[1]-c[1],b[0]-c[0]) - lpoints=[] - rpoints=[] - for i in idxp: - if i==farpoint: - #ignore triangle vertex - continue - x=points[i] - #if point x is left of line a-c it's in left set - slopeax=atan2(x[1]-a[1],x[0]-a[0]) - if slopeac==slopeax: - continue - sdiff=slopeac-slopeax - if(sdiff<-pi):sdiff+=2*pi - if(sdiff>pi):sdiff-=2*pi - if(sdiff<0): - lpoints+=[i] - else: - #if point x is right of line b-c it's in right set, otherwise it's inside triangle and can be ignored - slopecx=atan2(x[1]-c[1],x[0]-c[0]) - if slopecx==slopecb: - continue - sdiff=slopecx-slopecb - if(sdiff<-pi):sdiff+=2*pi - if(sdiff>pi):sdiff-=2*pi - if(sdiff>0): - rpoints+=[i] - #the hull segment between points a and b consists of the hull segment between a and c, the point c, and the hull segment between c and b - ret=_findhull(rpoints, farpoint, b_i, points)+[farpoint]+_findhull(lpoints, a_i, farpoint, points) - return ret - - -def convex_hull(points): - vertices = ConvexHull_qh(points) - return [points[idx] for idx in vertices] |