-
Notifications
You must be signed in to change notification settings - Fork 0
/
polygonutil.py
67 lines (56 loc) · 2.01 KB
/
polygonutil.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
import numpy as np
from math import *
from shapely.geometry import LineString, MultiPoint, Point, Polygon
def traversePolyLine(line, dist):
accumLen = 0.
for segment in getLineSegments(line):
secLen = np.linalg.norm(segment[1]-segment[0])
remainingLen = dist - accumLen
if secLen > remainingLen:
return segment[0]+(segment[1]-segment[0])*remainingLen/secLen
accumLen += secLen
def getPolyLineDirection(line, dist):
accumLen = 0.
for segment in getLineSegments(line):
segVec = segment[1]-segment[0]
segLen = np.linalg.norm(segVec)
remainingLen = dist - accumLen
if segLen > remainingLen:
return atan2(segVec[1],segVec[0])
accumLen += segLen
def shrinkPolygon(points, shrink):
poly = Polygon(points).buffer(-shrink)
if poly.is_empty:
return []
else:
return list(poly.exterior.coords)
def rotatePoints(points,theta):
R = np.matrix([
[cos(theta), -sin(theta)],
[sin(theta), cos(theta)]
])
return map(lambda x:np.asarray(R*np.matrix(x).T).flatten(),points)
def getLineSegments(points):
lineSegments = []
for i in range(0, len(points)-1):
lineSegments.append((points[i],points[i+1]))
return lineSegments
def getPolyLineIntersections(polyline1, polyline2):
polyline1 = LineString(polyline1)
polyline2 = LineString(polyline2)
intersection = polyline1.intersection(polyline2)
if isinstance(intersection,Point):
return [np.array([intersection.x, intersection.y])]
if isinstance(intersection,MultiPoint):
return map(lambda x:np.array([x.x,x.y]),intersection)
return []
def getClosestIntersection(p1, polyline1, polyline2):
intersections = getPolyLineIntersections(polyline1, polyline2)
closest = None
closestDist = None
for p2 in intersections:
dist = np.linalg.norm(p2-p1)
if closestDist is None or dist < closestDist:
closest = p2
closestDist = dist
return closest