-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathround_tracks_utils.py
80 lines (65 loc) · 2.53 KB
/
round_tracks_utils.py
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
78
79
80
import pcbnew
import math
from math import pi
tolerance = 10 # in nanometres
angleTolerance = 0.001 # in radians
unpickJunctionTolerance = 50000 # 0.005mm
def cloneVECTOR2I(vec):
return pcbnew.VECTOR2I(vec.x, vec.y)
def reverseTrack(t1):
#flip the track
sp = cloneVECTOR2I(t1.GetStart())
ep = cloneVECTOR2I(t1.GetEnd())
t1.SetStart(ep)
t1.SetEnd(sp)
# determines whether 2 points are close enough to be considered identical
def similarPoints(p1, p2):
return (((p1.x > p2.x - tolerance) and (p1.x < p2.x + tolerance)) and ((p1.y > p2.y - tolerance) and (p1.y < p2.y + tolerance)))
def similarAngle(a1,a2):
diff = normalizeAngle(a1-a2)
return (abs(diff)<angleTolerance)
# test if an intersection is within the bounds of a pad
def withinPad(pad, a, tracks):
# Bounding box is probably sufficient, unless we had a giant L-shaped pad or something
box = pad.GetBoundingBox()
if not box.Contains(a):
return False
# If the intersection is within the pad, return true
# But if one of the connected tracks is *entirely* within the pad, return false, since rounding won't break connectivity
inside = True
for t in tracks:
if box.Contains(t.GetEnd()):
inside = False
return inside
# shortens a track by an arbitrary amount, maintaining the angle and the endpoint
def shortenTrack(t1, amountToShorten):
# return true if amount to shorten exceeds length
if amountToShorten + tolerance >= t1.GetLength():
t1.SetStart(cloneVECTOR2I(t1.GetEnd()))
return True
angle = getTrackAngle(t1)
newX = t1.GetStart().x + math.cos(angle)*amountToShorten
newY = t1.GetStart().y + math.sin(angle)*amountToShorten
t1.SetStart(pcbnew.VECTOR2I(int(newX), int(newY)))
return False
# normalizes any angle to [-pi, pi)
def normalizeAngle(inputAngle):
while(inputAngle >= pi):
inputAngle -= 2*pi;
while(inputAngle < -pi):
inputAngle += 2*pi;
return inputAngle;
# gets the angle of a track (unnormalized)
def getTrackAngle(t1):
#use atan2 so the correct quadrant is returned
return math.atan2((t1.GetEnd().y - t1.GetStart().y), (t1.GetEnd().x - t1.GetStart().x))
# Get angle between tracks, assumes both start at their intersection
def getTrackAngleDifference(t1,t2):
a1 = math.atan2(t1.GetEnd().y - t1.GetStart().y, t1.GetEnd().x - t1.GetStart().x)
a2 = math.atan2(t2.GetEnd().y - t2.GetStart().y, t2.GetEnd().x - t2.GetStart().x)
t = a1-a2
if t > pi:
t = 2*pi-t
if t < -pi:
t = -2*pi-t
return abs(t)