forked from SchusterLab/maskLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilities.py
More file actions
executable file
·77 lines (61 loc) · 2.78 KB
/
utilities.py
File metadata and controls
executable file
·77 lines (61 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 6 11:53:13 2020
@author: sasha
"""
from dxfwrite import DXFEngine as dxf
from dxfwrite.algebra import rotate_2d
from dxfwrite.vector2d import vadd,midpoint,vmul_scalar,vsub
import math
# ===============================================================================
# UTILITY FUNCTIONS
# ===============================================================================
def curveAB(a,b,clockwise=True,angleDeg=90,ptDensity=120):
# generate a segmented curve from A to B specified by angle. Point density = #pts / revolution
# return list of points
# clockwise can be boolean {1,0} or sign type {1,-1}
if clockwise == 0:
clockwise = -1
angle = math.radians(angleDeg)
segments = int(angle/(2*math.pi) *ptDensity)
center = vadd(midpoint(a,b),vmul_scalar(rotate_2d(vsub(b,a),-clockwise*math.pi/2),0.5/math.tan(angle/2)))
points = []
for i in range(segments+1):
points.append(vadd(center,rotate_2d(vsub(a,center),-clockwise*i*angle/segments)))
return points
def cornerRound(vertex,quadrant,radius,clockwise=True,ptDensity=120):
#quadrant corresponds to quadrants 1-4
#generate a curve to replace the vertex
ptA = vadd(vertex,rotate_2d((0,radius),quadrant * math.pi/2))
ptB = vadd(vertex,rotate_2d((0,radius),(quadrant+1) * math.pi/2))
return clockwise>0 and curveAB(ptA,ptB,1,ptDensity=ptDensity) or curveAB(ptB,ptA,-1,ptDensity=ptDensity)
def transformedQuadrants(vflip=False,hflip=False):
#return quadrant list with vertical and horizontal flips applied. Updated to match dxfwrite style
# default quadrants:
# ^y
# 2 | 1
# --+--->x
# 3 | 4
return vflip==False and (hflip==False and [0,1,2,3,4] or [0,2,1,4,3]) or (hflip==False and [0,4,3,2,1] or [0,3,4,1,2])
def kwargStrip(kwargs,keys=['layer']):
# return kwargs with only entries specified by keys
return {k:kwargs[k] for k in keys if k in kwargs}
def doMirrored(func,canvas,pos,*args,**kwargs):
#experimental- executes function 'func' with second argument mirrored around x and y
# mirrorX : mirrors along X axis (1,0)
# mirrorY : mirrors along Y axis (0,1)
if not isinstance(pos, tuple):
print('\x1b[33mError:\x1b[0m Bad position args passed to doMirrorred()!')
return
#assign unlisted default args
kwargs = { **{'mirrorX':True,'mirrorY':True}, **kwargs}
#assign args and sanitize kwargs
mirrorX = kwargs.pop('mirrorX')
mirrorY = kwargs.pop('mirrorY')
func(canvas,(pos[0],pos[1]),*args,**kwargs)
if mirrorY:
func(canvas,(pos[0],-pos[1]),*args,**kwargs)
if mirrorX:
func(canvas,(-pos[0],pos[1]),*args,**kwargs)
if mirrorY and mirrorX:
func(canvas,(-pos[0],-pos[1]),*args,**kwargs)