Segment.py
Go to the documentation of this file.
1 from __future__ import division
2 import numpy as np
3 import math
4 
5 from object.ExtendedSegment import ExtendedSegment
6 
7 
8 class Segment(object):
9  def __init__(self, x1, y1, x2, y2):
10  self.x1 = x1
11  self.y1 = y1
12  self.x2 = x2
13  self.y2 = y2
14  self.num_faces = 0
15  self.spatial_cluster = None
16  self.wall_cluster = None
17  self.angular_cluster = None
18  self.cluster_index = None
19  self.weight = None
20  self.direction = None
21  self.branch = None
22 
23  def set_cluster_index(self, i):
24  self.cluster_index = i
25 
26  def set_weight(self, w):
27  self.weight = w
28 
29  def set_direction(self,d):
30  self.direction = d
31 
32  def set_angular_cluster(self, c):
33  self.angular_cluster = c
34 
35  def set_spatial_cluster(self, c):
36  self.spatial_cluster = c
37 
38  def set_branch(self, b):
39  self.branch = b
40 
41  def set_wall_cluster(self, cluster):
42  self.wall_cluster = cluster
43 
44 
45 def length(x1, y1, x2, y2):
46  # return the distance between point 1 and point 2
47  dist = np.sqrt((x2 - x1)**2 + (y2 - y1)**2)
48  return dist
49 
50 
51 def point_segment_distance(px, py, x1, y1, x2, y2):
52  # compute distance between points px,py and segment find by extremes x1,y1 and x2,y2
53  dx = x2 - x1
54  dy = y2 - y1
55  if dx == dy == 0: # the segment's just a point
56  return math.hypot(px - x1, py - y1)
57  # Calculate the t that minimizes the distance.
58  t = ((px - x1) * dx + (py - y1) * dy) / (dx * dx + dy * dy)
59  # See if this represents one of the segment's
60  # end points or a point in the middle.
61  if t < 0:
62  dx = px - x1
63  dy = py - y1
64  elif t > 1:
65  dx = px - x2
66  dy = py - y2
67  else:
68  near_x = x1 + t * dx
69  near_y = y1 + t * dy
70  dx = px - near_x
71  dy = py - near_y
72  return math.hypot(dx, dy)
73 
74 
75 def intersection_lines(m1, q1, m2, q2):
76  # given 2 lines in form: (m1x+y=q1) e (m2x+y=q2) find point of intersection.
77  coefficient = np.array([[m1, 1], [m2, 1]])
78  known_term = np.array([q1, q2])
79  x = np.linalg.solve(coefficient, known_term)
80  return x
81 
82 
83 def segments_intersect(x11, y11, x12, y12, x21, y21, x22, y22):
84  # restituisce true se i segmenti si intersecano, false altrimenti
85  # whether two segments in the plane intersect:
86  # one segment is (x11, y11) to (x12, y12)
87  # the other is (x21, y21) to (x22, y22)
88  dx1 = x12 - x11
89  dy1 = y12 - y11
90  dx2 = x22 - x21
91  dy2 = y22 - y21
92  delta = dx2 * dy1 - dy2 * dx1
93  # parallel segments
94  if delta == 0:
95  return False
96  s = (dx1 * (y21 - y11) + dy1 * (x11 - x21)) / delta
97  t = (dx2 * (y11 - y21) + dy2 * (x21 - x11)) / (-delta)
98  return (0 <= s <= 1) and (0 <= t <= 1)
99 
100 
101 def segments_distance(x11, y11, x12, y12, x21, y21, x22, y22):
102  # compute distance between 2 segment, if they intersect return 0, otherwise compute it and return it.
103  # distance between two segments in the plane:
104  # one segment is (x11, y11) to (x12, y12)
105  # the other is (x21, y21) to (x22, y22)
106  if segments_intersect(x11, y11, x12, y12, x21, y21, x22, y22):
107  return 0
108  # try each of the 4 vertices w/the other segment
109  # in pratica la distanza tra 2 segmenti e' il minimo tra la distanza tra 1 estremo e 2 segmento, 2 estremo e 2 segmento,
110  # 3 estremo e 1 segmento, 4 estremo e 1 segmento
111  distances = []
112  distances.append(point_segment_distance(x11, y11, x21, y21, x22, y22))
113  distances.append(point_segment_distance(x12, y12, x21, y21, x22, y22))
114  distances.append(point_segment_distance(x21, y21, x11, y11, x12, y12))
115  distances.append(point_segment_distance(x22, y22, x11, y11, x12, y12))
116  return min(distances)
117 
118 
119 def radiant_inclination(x1, y1, x2, y2):
120  # compute radiant inclination of segment with extremes x1,y1 and x2,y2
121  # avoid division by 0
122  if x1 != x2:
123  m = (y2-y1)/(x2-x1)
124  angle = math.atan(m)
125  else:
126  angle = math.radians(90.0)
127  return angle
128 
129 
130 def assign_angular_cluster(wall_list, cluster_centers):
131  # assign each angular_cluster to every wall in wall_list
132  for index, wall in enumerate(wall_list):
133  cluster = cluster_centers[index]
134  wall.set_angular_cluster(cluster)
135  return wall_list
136 
137 
138 def spatial_clustering(threshold, wall_list):
139 
140  # assegna cluster spaziale ad ogni muro di lista_muri. 2 muri hanno stesso cluster spaziale se hanno stesso cluster
141  # angolare e se hanno distanza laterale minore di soglia. Cluster spaziale = indice di posizione del primo muro in
142  # lista_muri che appartiene a quel cluster spaziale. E questo primo muro avra' quindi come cluster spaziale il
143  # proprio indice di posizione.
144 
145  for index, wall1 in enumerate(wall_list):
146  if wall1.spatial_cluster is None:
147  # check only next ones because the previous ones are already set
148  for wall2 in wall_list[index + 1:]:
149  # if not set yet and if they have same angular_cluster
150  if(wall2.spatial_cluster is None) and (wall1.angular_cluster == wall2.angular_cluster):
151  if lateral_separation(wall1, wall2) < threshold:
152  wall2.set_spatial_cluster(index)
153  # after the scan, set the spatial cluster of first one
154  # NB: it will be set also if no other wall is been found. it will be a cluster with only one wall.
155  wall1.set_spatial_cluster(index)
156  else:
157  # gia' stato settato, ma devo controllare lo stesso i successivi perche' possono essercene alcuni che dovrebbero
158  # appartenere allo stesso cluster_spaziali ma avevano distanza laterale troppo grande.
159  for wall2 in wall_list:
160  # if not already set and if they have same angular_cluster
161  if(wall2.spatial_cluster is None) and (wall1.angular_cluster == wall2.angular_cluster):
162  if lateral_separation(wall1,wall2) < threshold:
163  wall2.set_spatial_cluster(wall1.spatial_cluster)
164  # this time don't set cluster=index because wall1 isn't the first of cluster
165  else:
166  if(wall2.spatial_cluster is not None) and (wall2.spatial_cluster != wall1.spatial_cluster) and (wall1.angular_cluster == wall2.angular_cluster) and(lateral_separation(wall1, wall2) < threshold):
167  for m in wall_list:
168  if m.spatial_cluster == wall2.spatial_cluster:
169  m.set_spatial_cluster(wall1.spatial_cluster)
170  return wall_list
171 
172 
173 def lateral_separation(wall1, wall2):
174  # compute lateral separation between wall1 and wall2
175  x1 = wall1.x1
176  y1 = wall1.y1
177  x2 = wall1.x2
178  y2 = wall1.y2
179  x3 = wall2.x1
180  y3 = wall2.y1
181  x4 = wall2.x2
182  y4 = wall2.y2
183  mid1_x = (x1+x2)/2
184  mid1_y = (y1+y2)/2
185  mid2_x = (x3+x4)/2
186  mid2_y = (y3+y4)/2
187  # equal to wall2.angular_cluster
188  d = wall1.angular_cluster
189  m = math.tan(d)
190  # if angular_cluster != 0, so non horizontal line
191  if m != 0:
192  m_perp = -1/m
193  q1 = mid1_y - (m_perp * mid1_x)
194  q2 = mid2_y - (m * mid2_x)
195  # devo calcolare la distanza tra le proiezioni di mid1 e mid2 sulla retta perp al loro cluster angolare.
196  # Questa retta la faccio passare per mid1 cosi' devo proiettare solo mid2. Lo proietto trovando l'intersezione
197  # tra la retta perp passante per mid1 e la
198  # line with angular coefficient = tan(direction) passing through mid2.
199  mid2_projected = intersection_lines(-m_perp, q1, -m, q2)
200  mid2_projected_x = mid2_projected[0]
201  mid2_projected_y = mid2_projected[1]
202  return length(mid1_x, mid1_y, mid2_projected_x, mid2_projected_y)
203  # horizontal line
204  else:
205  # mid2 will have x = mid2_x and y = mid1_y
206  return length(mid1_x, mid1_y, mid1_x, mid2_y)
207 
208 
209 def get_projected_points(wall1, wall2):
210  # dati due muri, ottengo la retta perpendicolare passante per il centro del primo muro.
211  # ottengo poi la retta passante per il centro del secondo muro avente come coefficiente angolare lo stesso del primo
212  # muro. ottenute le due rette ottengo il punto di intersezione e restituisco le coordinate del punto medio del
213  # secondo muro proiettato sulla retta ortogonale al primo muro.
214  # cioe' la proiezione del punto medio del secondo muro sulla retta ortogonale e passante per il centro del primo muro.
215  x1 = wall1.x1
216  y1 = wall1.y1
217  x2 = wall1.x2
218  y2 = wall1.y2
219  x3 = wall2.x1
220  y3 = wall2.y1
221  x4 = wall2.x2
222  y4 = wall2.y2
223  mid1_x = (x1+x2)/2
224  mid1_y = (y1+y2)/2
225  mid2_x = (x3+x4)/2
226  mid2_y = (y3+y4)/2
227  # equal to wall2.angular_cluster
228  d = wall1.angular_cluster
229  m = math.tan(d)
230  # if angular_cluster !=0, so non horizontal line
231  if m != 0:
232  m_perp = -1/m
233  q1 = mid1_y - (m_perp * mid1_x)
234  q2 = mid2_y - (m * mid2_x)
235  # devo calcolare la distanza tra le proiezioni di mid1 e mid2 sulla retta perp al loro cluster angolare.
236  # Questa retta la faccio passare per mid1 cosi' devo proiettare solo mid2. Lo proietto trovando l'intersezione
237  # tra la retta perp passante per mid1 e la retta con inclinazione = tan(direction) passante per mid2.
238  mid2_projected = intersection_lines(-m_perp, q1, -m, q2)
239  mid2_projected_x = mid2_projected[0]
240  mid2_projected_y = mid2_projected[1]
241  return mid2_projected_x, mid2_projected_y
242  # horizontal line
243  else:
244  # mid2 projected will have x = mid2_x e y = mid1_y
245  return mid2_x, mid1_y
246 
247 
248 def find_extremes(walls_list):
249  # find xmin,ymin,xmax,ymax of walls of walls_list. They form the bounding box.
250  x_coordinates = []
251  y_coordinates = []
252  for wall in walls_list:
253  x_coordinates.append(float(wall.x1))
254  x_coordinates.append(float(wall.x2))
255  y_coordinates.append(float(wall.y1))
256  y_coordinates.append(float(wall.y2))
257  xmin = min(x_coordinates)
258  xmax = max(x_coordinates)
259  ymin = min(y_coordinates)
260  ymax = max(y_coordinates)
261  del x_coordinates[:]
262  del y_coordinates[:]
263  return np.array([xmin, xmax, ymin, ymax])
264 
265 
266 def create_edges(extended_segments):
267  # create edges as intersection between extended_segments
268  edges = []
269  points = []
270  # for each extended segment check all points that intersect other extended segment
271  for index, segment in enumerate(extended_segments):
272  x1 = segment.x1
273  y1 = segment.y1
274  x2 = segment.x2
275  y2 = segment.y2
276  for segment2 in extended_segments:
277  x3 = segment2.x1
278  y3 = segment2.y1
279  x4 = segment2.x2
280  y4 = segment2.y2
281  # if extended segment are different and they meet
282  if(segment != segment2) and (segments_intersect(x1, y1, x2, y2, x3, y3, x4, y4)):
283  point = intersection(x1, y1, x2, y2, x3, y3, x4, y4)
284  # point of intersection added to points list
285  points.append(point)
286  # added also the extremes of extended segment
287  # points.append([x1,y1])
288  # points.append([x2,y2])
289  # order points
290  points.sort(key=lambda x: (x[0], x[1]))
291  for i, point in enumerate(points):
292  # creating edges. Len-1 to avoid out of bound.
293  if i < len(points)-1:
294  next = points[i+1]
295  # avoid copy
296  if not ((point[0] == next[0]) and (point[1] == next[1])):
297  edge = Segment(point[0], point[1], next[0], next[1])
298  edge.set_angular_cluster(segment.angular_cluster)
299  # for each edge set cluster = cluster of extended segment
300  edge.set_spatial_cluster(segment.spatial_cluster)
301  edges.append(edge)
302  del points[:]
303  return edges
304 
305 
306 def intersection(x1, y1, x2, y2, x3, y3, x4, y4):
307  # given the extremes of 2 segments return the point of intersection
308  if(x1 != x2) and (x3 != x4):
309  m1 = (y2-y1)/(x2-x1)
310  m2 = (y4-y3)/(x4-x3)
311  q1 = y1 - (m1*x1)
312  q2 = y3 - (m2*x3)
313  point = intersection_lines(-m1, q1, -m2, q2)
314  return point
315  if x1 == x2:
316  m2 = (y4-y3)/(x4-x3)
317  q2 = y3 - (m2*x3)
318  y = m2*x1 + q2
319  point = np.array([x1,y])
320  return point
321  if x3 == x4:
322  m1 = (y2-y1)/(x2-x1)
323  q1 = y1 - (m1*x1)
324  y = m1*x3 + q1
325  point = np.array([x3,y])
326  return point
327 
328 
329 def set_weight_offset_edges(border_lines, edges_th1):
330  for edge in edges_th1:
331  if (edge.angular_cluster, edge.spatial_cluster) in border_lines:
332  edge.set_weight(1)
333 
334 
335 def set_weights(edges, wall_list):
336  # set weight to each edge.
337  # for each edge take walls with same spatial_cluster and projected them on extended segment with same spatial_cluster
338  # first of all delete all the projections included completely in other projections, then merge projections that are
339  # not completely overlapped. finally compute the percentage of edge covered by projections.
340  tmp = []
341  projections = []
342  for edge in edges:
343  spatial_cluster = edge.spatial_cluster
344  for wall in wall_list:
345  # if wall has same cluster of edge, add the projection
346  if wall.spatial_cluster == spatial_cluster:
347  point1 = project_point(wall.x1, wall.y1, edge.x1, edge.y1, edge.x2, edge.y2)
348  point2 = project_point(wall.x2, wall.y2, edge.x1, edge.y1, edge.x2, edge.y2)
349  # add the points to tmp list, that will be ordered in order to have start < end
350  tmp.append(point1)
351  tmp.append(point2)
352  tmp.sort(key=lambda x: (x[0], x[1]))
353  # ordered such that starting point of projected segment has x minor than x of ending point.
354  tmp2 = [tmp[0][0], tmp[0][1], tmp[1][0], tmp[1][1]]
355  # add projection x1,y1,x2,y2 only if is not already inside (avoid copy that cause trouble when deleting
356  # projections included in others)
357  if not already_inside_segment(tmp2, projections):
358  projections.append(tmp2)
359  del tmp[:]
360  # remove projections included in others
361  projections[:] = [tup for tup in projections if not included(tup, projections)]
362  # merge of projections that intersect each other
363  merged = merge_overlapped(projections)
364  while merged == 1:
365  merged = merge_overlapped(projections)
366  # compute coverage of the edge
367  coverage = compute_coverage(edge, projections)
368  if length(edge.x1, edge.y1, edge.x2, edge.y2) >= 20:
369  weight = coverage / length(edge.x1, edge.y1, edge.x2, edge.y2)
370  else:
371  weight = coverage / length(edge.x1, edge.y1, edge.x2, edge.y2)
372  # weight = 0.2
373  edge.set_weight(weight)
374  del projections[:]
375  return edges
376 
377 
378 def project_point(x1, y1, x2, y2, x3, y3):
379  # given the segment with extreme x2,y2 and x3,y3, project the point1 on segment
380  # if vertical segment
381  if x2 == x3:
382  point = np.array([x2, y1])
383  return point
384  # if horizontal segment
385  if y2 == y3:
386  point = np.array([x1, y2])
387  return point
388  m = (y3-y2)/(x3-x2)
389  q = y2 - m*x2
390  m_perp = -(1/m)
391  q_perp = y1 - m_perp*x1
392  # find intersection between segment and perpendicular line pass through point1
393  point = intersection_lines(-m, q, -m_perp, q_perp)
394  return point
395 
396 
397 def already_inside_segment(seg, projections):
398  # return true if segment is already inside projections
399  for seg2 in projections:
400  if seg == seg2:
401  return True
402 
403 
404 def included(seg1, segment_list):
405  # return true if the segment is included in another segment of list
406  x1 = seg1[0]
407  y1 = seg1[1]
408  x2 = seg1[2]
409  y2 = seg1[3]
410  for seg2 in segment_list:
411  if seg1 != seg2:
412  x3 = seg2[0]
413  y3 = seg2[1]
414  x4 = seg2[2]
415  y4 = seg2[3]
416  if(x1 == x2 == x3 == x4) and (y1 >= y3) and (y2 <= y4):
417  return True
418  if(x1 > x3) and (x2 <= x4):
419  return True
420  if(x1 >= x3) and (x2 < x4):
421  return True
422  return False
423 
424 
425 def merge_overlapped(projections):
426  # return 1 if every time that 2 projections are partially overlapped, it will be created and added the union.
427  # and the 2 projections are deleted from list of projections.
428  for seg1 in projections:
429  x1 = seg1[0]
430  y1 = seg1[1]
431  x2 = seg1[2]
432  y2 = seg1[3]
433  for seg2 in projections:
434  if seg1 != seg2:
435  x3 = seg2[0]
436  y3 = seg2[1]
437  x4 = seg2[2]
438  y4 = seg2[3]
439  if(x1 == x2 == x3 == x4) and (y1 < y3) and (y3 <= y2 < y4):
440  union = [x1, y1, x2, y4]
441  projections.append(union)
442  projections.remove(seg1)
443  projections.remove(seg2)
444  return 1
445  if(x1 < x3) and (x3 <= x2 < x4):
446  union = [x1, y1, x4, y4]
447  projections.append(union)
448  projections.remove(seg1)
449  projections.remove(seg2)
450  return 1
451  return 0
452 
453 
454 def compute_coverage(edge, projections):
455  # return the sum of lengths of projections on the edge
456  x1 = edge.x1
457  y1 = edge.y1
458  x2 = edge.x2
459  y2 = edge.y2
460  coverage = 0
461  for segment in projections:
462  x3 = segment[0]
463  y3 = segment[1]
464  x4 = segment[2]
465  y4 = segment[3]
466  if x1 == x2 == x3 == x4:
467  if (y3 <= y1) and (y4 >= y2):
468  return y2 - y1
469  if (y3 >= y1) and (y4 <= y2):
470  coverage += (y4-y3)
471  if (y1 < y3 < y2) and (y4 > y2):
472  coverage += (y2-y3)
473  if (y3 < y1) and (y1 < y4 <y2):
474  coverage += (y4-y1)
475  else:
476  if(x3 <= x1) and (x4 >= x2):
477  return length(x1, y1, x2, y2)
478  if(x3 >= x1) and (x4 <= x2):
479  coverage += length(x3, y3, x4, y4)
480  if(x1 < x3 < x2) and (x4 > x2):
481  coverage += length(x3, y3, x2, y2)
482  if(x3 < x1) and (x1 < x4 < x2):
483  coverage += length(x1, y1, x4, y4)
484  return coverage
485 
486 
487 def adjacent_edges(x, y, edge, edges):
488  # return list of edges different from edge, with different m and extreme x1,y1
489  e = []
490  for edge1 in edges:
491  x1 = edge1.x1
492  y1 = edge1.y1
493  x2 = edge1.x2
494  y2 = edge1.y2
495  if (edge != edge1) and (((x1 == x) and (y1 == y)) or ((x2 == x) and (y2 == y))):
496  e.append(edge1)
497  return e
498 
499 
500 def same_m(edge, edge1, epsilon=0.05):
501  # return true if edge and edge1 have same m
502  x1 = edge.x1
503  y1 = edge.y1
504  x2 = edge.x2
505  y2 = edge.y2
506  x3 = edge1.x1
507  y3 = edge1.y1
508  x4 = edge1.x2
509  y4 = edge1.y2
510  if(x1 == x2) and (x3 == x4):
511  return True
512  if ((x1 == x2) and (x3 != x4)) or ((x1 != x2) and (x3 == x4)):
513  return False
514  m1 = (y2-y1)/(x2-x1)
515  m2 = (y4-y3)/(x4-x3)
516  if abs(m1-m2) < epsilon:
517  return True
518  return False
519 
520 
521 def set_segment_label2(angular_ordered, label):
522  # siccome facendo un clustering spaziale per ogni cluster angolare, ottengo label uguali per ogni cluster angolare,
523  # mi conviene aggiungere un fattore uguale al numero di cluster usati durante il fit del metodo di clustering
524  # (in questo caso 50)
525  c = 0
526  for ang, l in zip(angular_ordered, label):
527  for index, segment in enumerate(ang):
528  if l[index] != -1:
529  segment.set_wall_cluster(l[index] + c)
530  # in this case segments classified as outliers from DBSCAN
531  else:
532  # put inside one cluster all the outliers
533  segment.set_wall_cluster(l[index])
534  c = c + len(set(l))
535  # reconstruct list of segments. keep in mind that now are not ordered
536  walls = []
537  for ang in angular_ordered:
538  for segment in ang:
539  walls.append(segment)
540  return walls
541 
542 
543 def remove_less_representatives(extended_segments, threshold):
544  extended_segments_v2 = []
545  removed = []
546  for segment in extended_segments:
547  if segment.weight > threshold:
548  extended_segments_v2.append(segment)
549  else:
550  removed.append(segment)
551  return extended_segments_v2, removed
552 
553 
554 def remove_less_representatives_v2(extended_segments, threshold, param):
555  extended_segments_v2 = []
556  for segment in extended_segments:
557  if param < segment.weight <= threshold:
558  extended_segments_v2.append(segment)
559  return extended_segments_v2
560 
561 
562 def check_pos(x_min, x_max, y_min, y_max, x, y):
563  if x < x_min:
564  x_min = x
565  if x > x_max:
566  x_max = x
567  if y < y_min:
568  y_min = y
569  if y > y_max:
570  y_max = y
571  return x_min, x_max, y_min, y_max
572 
573 
574 def p_to_p_dist(p1, p2):
575  dist = math.sqrt((p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]))
576  return dist
577 
578 
579 def inside_segment(vertex, x1, y1, x2, y2):
580  if x1 - 10 <= vertex[0] <= x2 + 10 and y1 - 10 <= vertex[1] <= y2 + 10:
581  return True
582  return False
583 
584 
585 def create_short_ex_lines(line, walls, size, extended_lines):
586  x_min, x_max, y_min, y_max = size[0] + 20, 0, size[1] + 20, 0
587  for wall in walls:
588  if wall.spatial_cluster == line.spatial_cluster:
589  x_min, x_max, y_min, y_max = check_pos(x_min, x_max, y_min, y_max, wall.x1, wall.y1)
590  x_min, x_max, y_min, y_max = check_pos(x_min, x_max, y_min, y_max, wall.x2, wall.y2)
591  x1, x2, y1, y2 = x_min, x_max, y_min, y_max
592  x_min -= 100
593  x_max += 100
594  y_min -= 100
595  y_max += 100
596  if x_min < 0:
597  x_min = 0
598  if y_min < 0:
599  y_min = 0
600  if x_max > size[0]:
601  x_max = size[0]
602  if y_max > size[1]:
603  y_max = size[1]
604  if line.x2 - line.x1 == 0:
605  point1 = np.array([line.x2, y_min])
606  point2 = np.array([line.x2, y_max])
607  point1_real = np.array([x1, y1])
608  point2_real = np.array([x2, y2])
609  else:
610  m = (line.y2 - line.y1)/(line.x2 - line.x1)
611  q = line.y1 - m * line.x1
612  if x_max - x_min > y_max - y_min:
613  y_for_x_min = m * x_min + q
614  y_for_x_max = m * x_max + q
615  y1 = m * x1 + q
616  y2 = m * x2 + q
617  point1 = np.array([x_min, y_for_x_min])
618  point2 = np.array([x_max, y_for_x_max])
619  point1_real = np.array([x1, y1])
620  point2_real = np.array([x2, y2])
621  else:
622  x_for_y_min = (y_min - q)/m
623  x_for_y_max = (y_max - q)/m
624  x1 = (y1 - q)/m
625  x2 = (y2 - q)/m
626  point1 = np.array([x_for_y_min, y_min])
627  point2 = np.array([x_for_y_max, y_max])
628  point1_real = np.array([x1, y1])
629  point2_real = np.array([x2, y2])
630  vertices = []
631  for l in extended_lines:
632  if segments_intersect(l.x1, l.y1, l.x2, l.y2, point1[0], point1[1], point2[0], point2[1]):
633  vertex = intersection(l.x1, l.y1, l.x2, l.y2, point1[0], point1[1], point2[0], point2[1])
634  if not inside_segment(vertex, x1, y1, x2, y2):
635  vertices.append(vertex)
636  if len(vertices) >= 2:
637  d1, d2 = None, None
638  n1, n2 = None, None
639  for v in vertices:
640  d = p_to_p_dist(v, point1_real)
641  if d1 is None or d < d1:
642  d1 = d
643  n1 = v
644  for i, v in enumerate(vertices):
645  if v[0] == n1[0] and v[1] == n1[1]:
646  vertices.pop(i)
647  for v in vertices:
648  d = p_to_p_dist(v, point2_real)
649  if d2 is None or d < d2:
650  d2 = d
651  n2 = v
652  if n1[0] - n2[0] == 0:
653  if n1[1] > n2[1]:
654  n1[1] += 10
655  n2[1] -= 10
656  else:
657  n1[1] -= 10
658  n2[1] += 10
659  else:
660  m = (line.y2 - line.y1) / (line.x2 - line.x1)
661  q = line.y1 - m * line.x1
662  if n1[0] > n2[0]:
663  n1[0] += 5
664  n1[1] = m * n1[0] + q
665  n2[0] -= 5
666  n2[1] = m * n2[0] + q
667  else:
668  n1[0] -= 5
669  n1[1] = m * n1[0] + q
670  n2[0] += 5
671  n2[1] = m * n2[0] + q
672  short_line = ExtendedSegment(n1, n2, line.angular_cluster, line.spatial_cluster)
673  return short_line
674  else:
675  return None
676  # short_line = ExtendedSegment(point1, point2, line.angular_cluster, line.spatial_cluster)
677  # return short_line
Segment.find_extremes
def find_extremes(walls_list)
Definition: Segment.py:248
Segment.compute_coverage
def compute_coverage(edge, projections)
Definition: Segment.py:454
Segment.Segment
Definition: Segment.py:8
Segment.Segment.set_cluster_index
def set_cluster_index(self, i)
Definition: Segment.py:23
Segment.same_m
def same_m(edge, edge1, epsilon=0.05)
Definition: Segment.py:500
Segment.Segment.x1
x1
Definition: Segment.py:10
Segment.set_segment_label2
def set_segment_label2(angular_ordered, label)
Definition: Segment.py:521
Segment.Segment.spatial_cluster
spatial_cluster
Definition: Segment.py:15
Segment.Segment.set_branch
def set_branch(self, b)
Definition: Segment.py:38
Segment.segments_intersect
def segments_intersect(x11, y11, x12, y12, x21, y21, x22, y22)
Definition: Segment.py:83
Segment.merge_overlapped
def merge_overlapped(projections)
Definition: Segment.py:425
Segment.Segment.set_weight
def set_weight(self, w)
Definition: Segment.py:26
Segment.remove_less_representatives
def remove_less_representatives(extended_segments, threshold)
Definition: Segment.py:543
Segment.point_segment_distance
def point_segment_distance(px, py, x1, y1, x2, y2)
Definition: Segment.py:51
Segment.inside_segment
def inside_segment(vertex, x1, y1, x2, y2)
Definition: Segment.py:579
Segment.Segment.__init__
def __init__(self, x1, y1, x2, y2)
Definition: Segment.py:9
Segment.adjacent_edges
def adjacent_edges(x, y, edge, edges)
Definition: Segment.py:487
Segment.remove_less_representatives_v2
def remove_less_representatives_v2(extended_segments, threshold, param)
Definition: Segment.py:554
Segment.Segment.branch
branch
Definition: Segment.py:21
Segment.Segment.cluster_index
cluster_index
Definition: Segment.py:18
Segment.Segment.x2
x2
Definition: Segment.py:12
Segment.Segment.num_faces
num_faces
Definition: Segment.py:14
Segment.included
def included(seg1, segment_list)
Definition: Segment.py:404
Segment.Segment.direction
direction
Definition: Segment.py:20
Segment.radiant_inclination
def radiant_inclination(x1, y1, x2, y2)
Definition: Segment.py:119
Segment.spatial_clustering
def spatial_clustering(threshold, wall_list)
Definition: Segment.py:138
Segment.intersection_lines
def intersection_lines(m1, q1, m2, q2)
Definition: Segment.py:75
Segment.create_edges
def create_edges(extended_segments)
Definition: Segment.py:266
Segment.Segment.wall_cluster
wall_cluster
Definition: Segment.py:16
Segment.already_inside_segment
def already_inside_segment(seg, projections)
Definition: Segment.py:397
Segment.Segment.set_wall_cluster
def set_wall_cluster(self, cluster)
Definition: Segment.py:41
Segment.segments_distance
def segments_distance(x11, y11, x12, y12, x21, y21, x22, y22)
Definition: Segment.py:101
Segment.set_weights
def set_weights(edges, wall_list)
Definition: Segment.py:335
Segment.Segment.set_angular_cluster
def set_angular_cluster(self, c)
Definition: Segment.py:32
Segment.check_pos
def check_pos(x_min, x_max, y_min, y_max, x, y)
Definition: Segment.py:562
ExtendedSegment
Definition: ExtendedSegment.py:1
Segment.Segment.y2
y2
Definition: Segment.py:13
Segment.set_weight_offset_edges
def set_weight_offset_edges(border_lines, edges_th1)
Definition: Segment.py:329
Segment.get_projected_points
def get_projected_points(wall1, wall2)
Definition: Segment.py:209
Segment.create_short_ex_lines
def create_short_ex_lines(line, walls, size, extended_lines)
Definition: Segment.py:585
Segment.intersection
def intersection(x1, y1, x2, y2, x3, y3, x4, y4)
Definition: Segment.py:306
Segment.Segment.set_direction
def set_direction(self, d)
Definition: Segment.py:29
Segment.length
def length(x1, y1, x2, y2)
Definition: Segment.py:45
Segment.p_to_p_dist
def p_to_p_dist(p1, p2)
Definition: Segment.py:574
Segment.Segment.angular_cluster
angular_cluster
Definition: Segment.py:17
Segment.Segment.set_spatial_cluster
def set_spatial_cluster(self, c)
Definition: Segment.py:35
Segment.assign_angular_cluster
def assign_angular_cluster(wall_list, cluster_centers)
Definition: Segment.py:130
Segment.Segment.y1
y1
Definition: Segment.py:11
Segment.lateral_separation
def lateral_separation(wall1, wall2)
Definition: Segment.py:173
Segment.project_point
def project_point(x1, y1, x2, y2, x3, y3)
Definition: Segment.py:378
Segment.Segment.weight
weight
Definition: Segment.py:19


rose2
Author(s): Gabriele Somaschini, Matteo Luperto
autogenerated on Wed Jun 28 2023 02:21:53