-
Notifications
You must be signed in to change notification settings - Fork 1
/
CEtoVissim.py
208 lines (175 loc) · 8.53 KB
/
CEtoVissim.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
'''
Created on Apr 20, 2016
@author: Mohsen
'''
'''''''''''''''''''''''
Pre-processing:
Name all the network's segments "segment"
'''''''''''''''''''''''
from scripting import *
import os.path
import math
import operator
# get a CityEngine instance
ce = CE()
# obtaining segment, street, and sidewalks OIDs
ce.setSelection(ce.getObjectsFrom(ce.scene, ce.withName('segment')))
segments = ce.getObjectsFrom(ce.selection)
# Constructing OIDs, obtaining objects' vertices, and translation to global coordination
for i in segments:
segmentsOID = ce.getOID(i)
segment_init = ce.getVertices(ce.findByOID(segmentsOID))
segment012 = []
for j in range(0, len(segment_init), 3):
x012 = segment_init[j]
y012 = segment_init[j+1]
z012 = (-1)*segment_init[j+2]
segment012.extend([x012, z012, 0.0])
streetsOID = segmentsOID + ':0'
street0_init = ce.getVertices(ce.findByOID(streetsOID))
street0 = []
for k in range(0, len(street0_init), 3):
x0 = street0_init[k]
y0 = street0_init[k + 1]
z0 = (-1) * street0_init[k + 2]
street0.extend([x0, z0, 0.0])
sidewalks1OID = segmentsOID + ':1'
sidewalk1_init = ce.getVertices(ce.findByOID(sidewalks1OID))
sidewalk1 = []
for l in range(0, len(sidewalk1_init), 3):
x1 = sidewalk1_init[l]
y1 = sidewalk1_init[l + 1]
z1 = (-1) * sidewalk1_init[l + 2]
sidewalk1.extend([x1, z1, 0.0])
sidewalks2OID = segmentsOID + ':2'
sidewalk2_init = ce.getVertices(ce.findByOID(sidewalks2OID))
sidewalk2 = []
for m in range(0, len(sidewalk2_init), 3):
x2 = sidewalk2_init[m]
y2 = sidewalk2_init[m + 1]
z2 = (-1) * sidewalk2_init[m + 2]
sidewalk2.extend([x2, z2, 0.0])
# Obtaining common nodes between street and sidewalks
segment012t = [tuple(segment012[3 * a:3 * a + 3]) for a in range(len(segment012) // 3)]
street0t = [tuple(street0[3 * a:3 * a + 3]) for a in range(len(street0) // 3)]
sidewalk1t = [tuple(sidewalk1[3 * a:3 * a + 3]) for a in range(len(sidewalk1) // 3)]
sidewalk2t = [tuple(sidewalk2[3 * a:3 * a + 3]) for a in range(len(sidewalk2) // 3)]
street0_set = set(street0t)
sidewalk1_set = set(sidewalk1t)
sidewalk2_set = set(sidewalk2t)
intersection1 = list(street0_set.intersection(sidewalk1_set))
intersection2 = list(street0_set.intersection(sidewalk2_set))
intersections = intersection1 + intersection2
# finding streets' mid-points by averaging pedestrian-street common points
if len(intersection1) == 2 and len(intersection2) == 2:
if (intersection1[0][0] + intersection1[0][1]) < (intersection1[1][0] + intersection1[1][1]):
min_inter1 = [intersection1[0][0] , intersection1[0][1]]
max_inter1 = [intersection1[1][0] , intersection1[1][1]]
else:
min_inter1 = [intersection1[1][0] , intersection1[1][1]]
max_inter1 = [intersection1[0][0] , intersection1[0][1]]
if (intersection2[0][0] + intersection2[0][1]) < (intersection2[1][0] + intersection2[1][1]):
min_inter2 = [intersection2[0][0] , intersection2[0][1]]
max_inter2 = [intersection2[1][0] , intersection2[1][1]]
else:
min_inter2 = [intersection2[1][0] , intersection2[1][1]]
max_inter2 = [intersection2[0][0] , intersection2[0][1]]
else:
print 'No exact 4 points for street ID "', streetsOID, '"', '\n'
start = [(min_inter1[0] + min_inter2[0]) / 2, (min_inter1[1] + min_inter2[1]) / 2]
end = [(max_inter1[0] + max_inter2[0]) / 2, (max_inter1[1] + max_inter2[1]) / 2]
print 'segment ID:', '"', streetsOID, '"'
print 'start:', start
print 'end:', end, '\n'
if __name__ == '__main__':
pass
'''
# segment_line
M = (segment012[4]-segment012[1])/(segment012[3]-segment012[0])
# vertical, horizontal, or diagonal?
if (segment012[3] - segment012[0]) == 0:
orientation = 'VERTICAL'
elif (segment012[4] - segment012[1]) == 0:
orientation = 'HORIZONTAL'
else:
orientation = 'DIAGONAL'
# eliminating the outlier points for segments with more than 4 common points
if len(intersections) > 4:
segments_midpoints = [(segment[0]+segment[3])/2, (segment[1]+segment[4])/2, (segment[2]+segment[5])/2]
distances1 = []
distances2 = []
for j in intersection1:
distance1 = (((segments_midpoints[0]-j[0])**2+(segments_midpoints[1]-j[1])**2+(segments_midpoints[2]-j[2])**2)**0.5)
distances1.append(distance1)
#if len(distances1) > 2:
for k in intersection2:
distance2 = (((segments_midpoints[0]-k[0])**2+(segments_midpoints[1]-k[1])**2+(segments_midpoints[2]-k[2])**2)**0.5)
distances2.append(distance2)
distances = distances1 + distances2
distance = None
for n in distances:
if distances.count(m) == 1:
distance = n
'''
'''
# Finding the projection of intersection points on the segment line (p1' and p2').
if len(intersection1) == 2:
# forming vectors e1 (segment vector), e2 (segment point to the first intersection point), and e3 (segment point to the second intersection point)
e1 = [segment012[3]-segment012[0], segment012[4]-segment012[1]]
e2 = [(intersection1[0][0] - segment012[0]), (intersection1[0][1] - segment012[1])]
e3 = [(intersection1[1][0] - segment012[0]), (intersection1[1][1] - segment012[1])]
# get dot product of e1.e2 and e1.e3
valDp1 = sum(map(operator.mul, e1, e2))
valDp2 = sum(map(operator.mul, e1, e3))
# get length of the vectors
lenLineE1 = math.sqrt(e1[0] ** 2 + e1[1] ** 2)
lenLineE2 = math.sqrt(e2[0] ** 2 + e2[1] ** 2)
lenLineE3 = math.sqrt(e3[0] ** 2 + e3[1] ** 2)
# cosign of the angle between vectors e1^e2 and e1^e3
cos1 = valDp1 / (lenLineE1 * lenLineE2)
cos2 = valDp2 / (lenLineE1 * lenLineE3)
# length of v1p1' and v1p2'
projLenOfLine1 = cos1 * lenLineE2
pp1 = [((segment012[0] + (projLenOfLine1 * e1[0]) / lenLineE1)), ((segment012[1] + (projLenOfLine1 * e1[1]) / lenLineE1))]
pp1_2 = [((valDp1 / (lenLineE1 * lenLineE2)) * lenLineE2 * e1[0] / lenLineE1), ((valDp1 / (lenLineE1 * lenLineE2)) * lenLineE2 * e1[1] / lenLineE1)]
projLenOfLine2 = cos2 * lenLineE3
pp2 =[((segment012[0] + (projLenOfLine2 * e1[0]) / lenLineE2)), ((segment012[1] + (projLenOfLine2 * e1[1]) / lenLineE2))]
pp2_2 = [((valDp2 / (lenLineE1 * lenLineE3)) * lenLineE3 * e1[0] / lenLineE1), ((valDp2 / (lenLineE1 * lenLineE3)) * lenLineE3 * e1[1] / lenLineE1)]
elif len(intersection2) == 2:
# forming vectors e1 (segment vector), e2 (segment point to the first intersection point), and e3 (segment point to the second intersection point)
e1 = [segment012[3]-segment012[0], segment012[4]-segment012[1]]
e2 = [(intersection2[0][0] - segment012[0]), (intersection2[0][1] - segment012[1])]
e3 = [(intersection2[1][0] - segment012[0]), (intersection2[1][1] - segment012[1])]
# get dot product of e1.e2 and e1.e3
valDp1 = sum(map(operator.mul, e1, e2))
valDp2 = sum(map(operator.mul, e1, e3))
# get length of the vectors
lenLineE1 = math.sqrt(e1[0] ** 2 + e1[1] ** 2)
lenLineE2 = math.sqrt(e2[0] ** 2 + e2[1] ** 2)
lenLineE3 = math.sqrt(e3[0] ** 2 + e3[1] ** 2)
# cosign of the angle between vectors e1^e2 and e1^e3
cos1 = valDp1 / (lenLineE1 * lenLineE2)
cos2 = valDp2 / (lenLineE1 * lenLineE3)
# length of v1p1' and v1p2'
projLenOfLine1 = cos1 * lenLineE2
pp1_2 = [((segment012[0] + (projLenOfLine1 * e1[0]) / lenLineE1)), ((segment012[1] + (projLenOfLine1 * e1[1]) / lenLineE1))]
projLenOfLine2 = cos2 * lenLineE3
pp2_2 =[((segment012[0] + (projLenOfLine2 * e1[0]) / lenLineE2)), ((segment012[1] + (projLenOfLine2 * e1[1]) / lenLineE2))]
else:
print "No adjacent node on the sidewalk was found!"
print 'segment:', segment012
print 'intersection1:', intersection1
print 'intersection2:', intersection2
print 'e1, e2, e3:', e1, e2, e3
#print 'lenLineE1, lenLineE2, lenLineE3:', lenLineE1, lenLineE2, lenLineE3
#print 'cos1, cos2', cos1, cos2
#print 'projLenOfLine1, projLenOfLine2:', projLenOfLine1, projLenOfLine2
print 'pp1:', pp1
print 'pp2:', pp2
print 'pp1_2:', pp1_2
print 'pp2_2:', pp2_2, '\n'
'''
# for m in distances:
#if distances.count(m) == 1:
#distances.remove(m)
#print distances