-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRNP.py
101 lines (76 loc) · 3.21 KB
/
RNP.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import math
import Polyline
from Utility import *
class DNP:
def __init__(self, polyline, interval, filter_angle, flag=0):
self.polyline = polyline
self.angle = 0
self.DNP = Polyline.Polyline()
self.sp = polyline.points[0]
self.k = 0
self.interval = interval
self.filter_angle = filter_angle
self.flag = flag
def get_angle(self, vector1, vector2):
dot = vector1.dotProduct(vector2)
v1l = vector1.length()
v2l = vector2.length()
temp = dot/(v1l * v2l)
if temp > 1 or temp < -1:
if temp < -1:
temp = -1
if temp > 1:
temp = 1
angle = math.acos(temp) # angle is arc
return angle
def dnp(self):
self.DNP = Polyline.Polyline()
self.DNP.points.append(self.polyline.points[0])
pre = 1
index = 1
while index <= len(self.polyline.points)-2:
vector1 = self.polyline.points[pre-1].pointTo(self.polyline.points[pre])
vector2 = self.polyline.points[index].pointTo(self.polyline.points[index+1])
angle = self.get_angle(vector1, vector2)
radToDeg(angle)
if self.flag == 0:
if radToDeg(angle) >= self.angle:
self.DNP.points.append(self.polyline.points[index])
if self.DNP.points[-1].distance(
self.DNP.points[-2]) < self.interval and self.angle < self.filter_angle:
self.DNP.points.pop()
self.angle = self.angle + 1
else:
pre = index + 1
index = index + 1
self.angle = 0
else:
index = index + 1
elif self.flag == 1:
if radToDeg(angle) >= self.filter_angle:
self.DNP.points.append(self.polyline.points[index])
pre = index + 1
index = index + 1
else:
index = index + 1
elif self.flag == 2:
if vector2.dy == 0:
self.DNP.points.append(self.polyline.points[index])
pre = index + 1
index = index + 1
else:
if radToDeg(angle) >= self.angle:
self.DNP.points.append(self.polyline.points[index])
if self.DNP.points[-1].distance(
self.DNP.points[-2]) < self.interval and self.angle < self.filter_angle:
self.DNP.points.pop()
self.angle = self.angle + 1
else:
pre = index + 1
index = index + 1
self.angle = 0
else:
index = index + 1
if self.polyline.points[-1].distance(self.DNP.points[-1]) > self.interval:
self.DNP.points.append(self.polyline.points[-1])
return self.DNP