File size: 19,531 Bytes
d491737
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection



def pol2car(rho, pi, xi, yi):
    x = rho * np.cos(pi) + xi
    y = rho * np.sin(pi) + yi
    return (x, y)

def car2pol(xi, yi):
    rho = np.sqrt(xi**2 + yi**2)
    phi = np.arctan2(yi, xi)
    return (rho, phi)

def car2polnorm(xi, yi):
    rho = np.sqrt(xi**2 + yi**2)
    phi = np.arctan2(yi, xi)
    phi %= 2*np.pi
    phi /= 2*np.pi
    return (rho, phi)

def plot_isovist(isovists, show_axis=False, s=0.1, figsize=(5,5)):
    #transpose the matrix
    # isovists = np.transpose(isovists, (isovists.ndim-1, isovists.ndim-2))
    plt.switch_backend('agg')
    fig = plt.figure(figsize=figsize)
    points = []
    res = np.pi/90
    isovist = isovists
    for j, rho in enumerate(isovist):
        if rho < 1.0:
            pt = pol2car(rho, j*res, 0, 0)
            points.append(pt)
    x = [i[0] for i in points]
    y = [i[1] for i in points]
    ax = fig.add_subplot(111)
    ax.set_aspect('equal')
    ax.set_xlim(-1,1)
    ax.set_ylim(-1,1)
    if not show_axis:
        ax.axis('off')
    ax.scatter(x, y, s, 'black')
    return fig

from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure


def isovist_to_img(isovist, show_axis=False, s=0.1, figsize=(5,5)):
    points = []
    xy = (0, 0)
    res = np.pi/90
    isovist = isovist + 0.5
    for j, rho in enumerate(isovist):
            if rho <= 2.0:
                pt = pol2car(rho, j*res, xy[0], xy[1])
                points.append(pt)
    x = [i[0] for i in points]
    y = [i[1] for i in points]
    fig = plt.figure(figsize=figsize)
    canvas = FigureCanvas(fig)
    ax = fig.add_subplot(111)
    ax.set_aspect('equal')
    ax.set_xlim(-1,1)
    ax.set_ylim(-1,1)
    if not show_axis:
        ax.axis('off')
    ax.scatter(x, y, s, 'black')

    canvas.draw()
    image = np.fromstring(canvas.tostring_rgb(), dtype='uint8')
    return image

def isovist_to_img_a(isovist, show_axis=False, s=0.1, figsize=(5,5)):
    points = []
    xy = (0, 0)
    res = np.pi/128
    isovist = isovist + 0.5
    for j, rho in enumerate(isovist):
            if rho <= 2.0:
                pt = pol2car(rho, j*res, xy[0], xy[1])
                points.append(pt)
    x = [i[0] for i in points]
    y = [i[1] for i in points]
    fig = plt.figure(figsize=figsize)
    canvas = FigureCanvas(fig)
    ax = fig.add_subplot(111)
    ax.set_aspect('equal')
    ax.set_xlim(-1,1)
    ax.set_ylim(-1,1)
    if not show_axis:
        ax.axis('off')
    ax.scatter(x, y, s, 'black')

    canvas.draw()
    image = np.fromstring(canvas.tostring_rgb(), dtype='uint8')
    return image

def isovist_to_cartesian(isovist, x, y, scale):
    points = []
    xy = (x, y)
    res = np.pi/90
    isovist = isovist * scale
    for j, rho in enumerate(isovist):
        if rho <= scale:
            pt = pol2car(rho, j*res, xy[0], xy[1])
            points.append(pt)
        else:
            pt = pol2car(scale, j*res, xy[0], xy[1])
            points.append(pt)
    points = np.stack(points)
    return(points)

def isovist_to_cartesian_a(isovist, x, y, scale):
    points = []
    xy = (x, y)
    res = np.pi/len(isovist)*2
    isovist = isovist * scale
    for j, rho in enumerate(isovist):
        pt = pol2car(rho, j*res, xy[0], xy[1])
        points.append(pt)
    points = np.stack(points)
    return(points)

def isovist_to_cartesian_b(isovist, x, y):
    points = []
    xy = (x, y)
    res = np.pi*2
    isovist = isovist
    for j, rho in isovist:
        pt = pol2car(rho, j*res, xy[0], xy[1])
        points.append(pt)
    points = np.stack(points)
    return(points)

def isovist_to_cartesian_segment(isovist, x, y, scale):
    points = []
    segment = []
    xy = (x, y)
    res = np.pi/90
    isovist = isovist * scale
    p_rho = isovist[-1]
    for j, rho in enumerate(isovist):
        delta = abs(p_rho-rho)
        if j == 0:
            first_rho = rho
        if rho < 0.98 * scale and delta < 0.05 * scale:
            pt = pol2car(rho, j*res, xy[0], xy[1])
            segment.append(pt)
        else:
            points.append(segment)
            segment = []
        p_rho = rho
    if first_rho < 1.0 * scale and abs(rho-first_rho)< 0.05 * scale :
        if len(points) > 0:
            segment.extend(points[0])
            points[0]=segment
        else:
            points.append(segment)
    else:
        points.append(segment)
    segments = []
    for i in range(len(points)):
        if len(points[i])>0:
            segment = np.stack(points[i])
            segments.append(segment)
    return(segments)

def isovist_to_cartesian_segment_a(isovist, x, y, scale, max=0.98, min = 0.1, d=0.1):
    points = []
    segment = []
    xy = (x, y)
    res = np.pi/len(isovist)*2
    isovist = isovist * scale
    p_rho = isovist[-1]
    for j, rho in enumerate(isovist):
        delta = abs(p_rho-rho)
        if j == 0:
            first_rho = rho
        if rho < max * scale and rho > min * scale and delta < d * scale:
            pt = pol2car(rho, j*res, xy[0], xy[1])
            segment.append(pt)
        else:
            points.append(segment)
            segment = []
        p_rho = rho
    if first_rho < max * scale and  first_rho > min * scale and abs(rho-first_rho)< d * scale :
        if len(points) > 0:
            segment.extend(points[0])
            points[0]=segment
        else:
            points.append(segment)
    else:
        points.append(segment)
    segments = []
    for i in range(len(points)):
        if len(points[i])>0:
            segment = np.stack(points[i])
            segments.append(segment)
    return(segments)


def isovist_to_cartesian_segment_b(isovist, x, y):
    points = []
    segment = []
    xy = (x, y)
    res = np.pi*2
    isovist = isovist
    p_rho = isovist[-1, 1]
    _i = 0
    for j, rho in isovist:
        delta = abs(p_rho-rho)
        if _i == 0:
            first_rho = rho
        if rho < 0.98 and delta < 0.1 :
            pt = pol2car(rho, j*res, xy[0], xy[1])
            segment.append(pt)
        else:
            points.append(segment)
            segment = []
        p_rho = rho
        _i += 1
    if first_rho < 0.98  and abs(rho-first_rho)< 0.1:
        if len(points) > 0:
            segment.extend(points[0])
            points[0]=segment
        else:
            points.append(segment)
    else:
        points.append(segment)
    segments = []
    for i in range(len(points)):
        if len(points[i])>0:
            segment = np.stack(points[i])
            segments.append(segment)
    return(segments)


# plotting an isovist and return the numpy image
def plot_isovist_numpy(k, text=None, figsize=(8,8)):
    fig, ax = plt.subplots(1,1, figsize=figsize, dpi=300)

    #plot isovist
    xy = isovist_to_cartesian_a(k, 0, 0, 1.0)
    polygon = Polygon(xy, True)
    p = PatchCollection([polygon])
    p.set_facecolor('#dddddd')
    p.set_edgecolor(None)
    ax.add_collection(p)

    # style
    ax.set_aspect('equal')
    lim = 1.2
    ax.set_xlim(-lim,lim)
    ax.set_ylim(-lim,lim)
    ax.set_xticks([])
    ax.set_yticks([])
    ax.axis('off')
    if text != None:
        ax.set_title(text, size=5) # Title
        fig.tight_layout()

    # for plot with torchvision util
    fig.canvas.draw()
    data = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
    w, h = fig.canvas.get_width_height()
    im = data.reshape((int(h), int(w), -1))
    im = im.transpose((2, 0, 1))
    plt.close()
    return im



# plotting isovist and boundary from  and return the numpy image
def plot_isovist_boundary_numpy(isovist, boundary, figsize=(8,8)):
    fig, ax = plt.subplots(1,1, figsize=figsize, dpi=300)

    #plot isovist
    xy = isovist_to_cartesian_a(isovist, 0, 0, 1.0)
    polygon = Polygon(xy, True)
    p = PatchCollection([polygon])
    p.set_facecolor('#eeeeee')
    p.set_edgecolor(None)
    ax.add_collection(p)


    #plot assumed boundary
    edge_patches = []
    segments = isovist_to_cartesian_segment_a(boundary, 0, 0, 1.0)
    for segment in segments:
        polygon = Polygon(segment, False)
        edge_patches.append(polygon)
    p = PatchCollection(edge_patches)
    p.set_facecolor('none')
    p.set_edgecolor('#000000')
    p.set_linewidth(0.5)
    ax.add_collection(p)

    # style
    ax.set_aspect('equal')
    lim = 1.2
    ax.set_xlim(-lim,lim)
    ax.set_ylim(-lim,lim)
    ax.set_xticks([])
    ax.set_yticks([])
    ax.axis('off')

    # for plot with torchvision util
    fig.canvas.draw()
    data = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
    w, h = fig.canvas.get_width_height()
    im = data.reshape((int(h), int(w), -1))
    im = im.transpose((2, 0, 1))
    plt.close()
    return im


# plotting two isovists (fill and edge) and return the numpy image
def plot_isovist_double_numpy(isovist1, isovist2, figsize=(8,8)):
    fig, ax = plt.subplots(1,1, figsize=figsize, dpi=300)

    #plot isovist1
    xy = isovist_to_cartesian_a(isovist1, 0, 0, 1.0)
    polygon = Polygon(xy, True)
    p = PatchCollection([polygon])
    p.set_facecolor('#dddddd')
    p.set_edgecolor(None)
    ax.add_collection(p)

    #plot isovist2 as boundary
    xy = isovist_to_cartesian_a(isovist2, 0, 0, 1.0)
    polygon = Polygon(xy, True)
    p = PatchCollection([polygon])
    p.set_facecolor('none')
    p.set_edgecolor('#000000')
    p.set_linewidth(0.2)
    ax.add_collection(p)

    # style
    ax.set_aspect('equal')
    lim = 1.2
    ax.set_xlim(-lim,lim)
    ax.set_ylim(-lim,lim)
    ax.set_xticks([])
    ax.set_yticks([])
    ax.axis('off')

    # for plot with torchvision util
    fig.canvas.draw()
    data = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
    w, h = fig.canvas.get_width_height()
    im = data.reshape((int(h), int(w), -1))
    im = im.transpose((2, 0, 1))
    plt.close()
    return im


# plotting two isovists (fill and edge) and return the numpy image
def plot_isovist_triple_numpy(isovists, locs, figsize=(8,8)):
    isovist1, isovist2, isovist3 = isovists
    loc1, loc2, loc3 = locs

    fig, ax = plt.subplots(1,1, figsize=figsize, dpi=300)

    #plot isovist1
    xy = isovist_to_cartesian_a(isovist1, loc1[0], loc1[1], 1.0)
    polygon = Polygon(xy, True)
    p = PatchCollection([polygon])
    p.set_facecolor('#ffdddd')
    p.set_edgecolor(None)
    ax.add_collection(p)

    #plot isovist2
    xy = isovist_to_cartesian_a(isovist2, loc2[0], loc2[1], 1.0)
    polygon = Polygon(xy, True)
    p = PatchCollection([polygon])
    p.set_facecolor('#ddddff')
    p.set_edgecolor(None)
    ax.add_collection(p)

    #plot isovist3 as boundary
    xy = isovist_to_cartesian_a(isovist3, 0, 0, 1.0)
    polygon = Polygon(xy, True)
    p = PatchCollection([polygon])
    p.set_facecolor('none')
    p.set_edgecolor('#000000')
    p.set_linewidth(0.2)
    ax.add_collection(p)

    ax.scatter([x[0] for x in locs], [x[1] for x in locs], c='k', s=8, marker='+')

    annotation = ['x1', 'x2', 'y']
    for i, anno in enumerate(annotation):
        ax.annotate(anno, (locs[i][0]+0.1, locs[i][1]), size=8)

    # style
    ax.set_aspect('equal')
    lim = 1.5
    ax.set_xlim(-lim,lim)
    ax.set_ylim(-lim,lim)
    ax.set_xticks([])
    ax.set_yticks([])
    ax.axis('off')

    # for plot with torchvision util
    fig.canvas.draw()
    data = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
    w, h = fig.canvas.get_width_height()
    im = data.reshape((int(h), int(w), -1))
    im = im.transpose((2, 0, 1))
    plt.close()
    return im

# showing isovist sequence
def seq_show(locs, isovists, figsize=(8, 8)):
    # walk trough the sequence
    p_loc = np.array((0, 0))
    b_segments = []
    b_points = []
    isovists_pts = []
    res = np.pi/128
    p_loc = np.array([0,0])
    cartesian_locs = []
    for loc, isovist in zip(locs, isovists):
        rel_pos = np.asarray(pol2car(loc[0], loc[1]*2*np.pi, p_loc[0], p_loc[1]))
        for j, rho in enumerate(isovist):
            if rho < 0.98 :
                pt = pol2car(rho, j*res, rel_pos[0], rel_pos[1])
                b_points.append(pt)
        segments = isovist_to_cartesian_segment_a(isovist, rel_pos[0], rel_pos[1], 1.0)
        b_segments.extend(segments)
        isovists_pts.append(isovist_to_cartesian_a(isovist, rel_pos[0], rel_pos[1], 1.0))
        cartesian_locs.append(rel_pos)
        p_loc = rel_pos

    fig, ax = plt.subplots(1,1, figsize=figsize, dpi=96)

    
    # isovists
    isovist_poly = []
    for isovist_pts in isovists_pts:
        isovist_poly.append(Polygon(isovist_pts, True))
    r = PatchCollection(isovist_poly)
    r.set_facecolor('#000000')
    r.set_edgecolor(None)
    r.set_alpha(0.02)
    ax.add_collection(r)
    

    # isovist path
    q = PatchCollection([Polygon(cartesian_locs, False)])
    q.set_facecolor('none')
    q.set_edgecolor('#cccccc')
    q.set_linewidth(1.0)
    q.set_linestyle('dashed')
    ax.add_collection(q)
    ax.scatter([x[0] for x in cartesian_locs], [x[1] for x in cartesian_locs], s = 6.0, c='red')

    # boundaries
    edge_patches = []
    for segment in b_segments:
        polygon = Polygon(segment, False)
        edge_patches.append(polygon)
    p = PatchCollection(edge_patches)
    p.set_facecolor('none')
    p.set_edgecolor('#000000')
    p.set_linewidth(1.0)
    ax.add_collection(p)
    ax.scatter([x[0] for x in b_points], [x[1] for x in b_points], s = 0.05, c='k')


    # style
    ax.set_aspect('equal')
    lim = 1.5
    ax.set_xlim(-lim,lim)
    ax.set_ylim(-lim,lim)
    ax.set_xticks([])
    ax.set_yticks([])
    ax.axis('off')

    return fig


# plotting isovist sequence
def plot_isovist_sequence(locs, isovists, figsize=(8,8)):
    fig = seq_show(locs, isovists, figsize=figsize)

    # for plot with torchvision util
    fig.canvas.draw()
    data = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
    w, h = fig.canvas.get_width_height()
    im = data.reshape((int(h), int(w), -1))
    im = im.transpose((2, 0, 1))
    plt.close()
    return im


def index_to_loc_grid(idx, d):
    if idx == 0:
        return np.array((0., 0.), dtype=np.float32)
    elif idx == 1:
        return np.array((d, 0.), dtype=np.float32)
    elif idx == 2:
        return np.array((d, d), dtype=np.float32)
    elif idx == 3:
        return np.array((0., d), dtype=np.float32)
    elif idx == 4:
        return np.array((-d, d), dtype=np.float32)
    elif idx == 5:
        return np.array((-d, 0.), dtype=np.float32)
    elif idx == 6:
        return np.array((-d, -d), dtype=np.float32)
    elif idx == 7:
        return np.array((0., -d), dtype=np.float32)
    elif idx == 8:
        return np.array((d, -d), dtype=np.float32)
    else:
        raise NameError('Direction unknown')



# showing isovist sequence grid
def seq_show_grid(locs, isovists, d=0.2, figsize=(8, 8), center=False, lim=1.5, alpha=0.02, rad=0.9, b_width=1.0, calculate_lim=False):
    # walk trough the sequence
    p_loc = np.array((0, 0))
    b_segments = []
    b_points = []
    isovists_pts = []
    res = np.pi/128
    cartesian_locs = []
    for loc, isovist in zip(locs, isovists):
        rel_pos = index_to_loc_grid(loc, d) + p_loc
        for j, rho in enumerate(isovist):
            if rho < rad :
                pt = pol2car(rho, j*res, rel_pos[0], rel_pos[1])
                b_points.append(pt)
        segments = isovist_to_cartesian_segment_a(isovist, rel_pos[0], rel_pos[1], 1.0)
        b_segments.extend(segments)
        isovists_pts.append(isovist_to_cartesian_a(isovist, rel_pos[0], rel_pos[1], 1.0))
        cartesian_locs.append(rel_pos)
        p_loc = rel_pos

    if len(b_points) > 0:
        b_points = np.stack(b_points)
    else:
        b_points =[]
    isovists_pts = np.stack(isovists_pts)
    # b_segments = np.stack(b_segments)
    cartesian_locs = np.stack(cartesian_locs)

    # set graphic properties
    isovist_path_width = 0.1
    isovist_path_pt1 = 6.0
    isovist_path_pt2 = 10.0
    isovist_boundary_pt = 0.05

    if center == True:

        bbox = get_bbox(b_points)
        center_pt = get_center_pts(bbox, np_array=True)
        b_points = [ pt - center_pt for pt in b_points]
        isovists_pts = [ pt - center_pt for pt in isovists_pts]
        b_segments =  [ pt - center_pt for pt in b_segments]
        cartesian_locs = [ pt - center_pt for pt in cartesian_locs]

    # resize image
    if calculate_lim == True:
        if bbox is not None:
            max = np.max(np.abs(bbox))
        else:
            max = 2.0
        if max > 2.0:
            lim = ((max // 0.5) + 1) * 0.5
            isovist_path_width *= 2.0/lim
            isovist_path_pt1 *= 2.0/lim
            isovist_path_pt2 *= 2.0/lim
            isovist_boundary_pt *= 2.0/lim


    fig, ax = plt.subplots(1,1, figsize=figsize, dpi=96)

    

    # isovists
    isovist_poly = []
    for isovist_pts in isovists_pts:
        isovist_poly.append(Polygon(isovist_pts, True))
    r = PatchCollection(isovist_poly)
    r.set_facecolor('#00aabb')
    r.set_edgecolor(None)
    r.set_alpha(alpha)
    ax.add_collection(r)

    

    # isovist path
    q = PatchCollection([Polygon(cartesian_locs, False)])
    q.set_facecolor('none')
    q.set_edgecolor('red')
    q.set_linewidth(isovist_path_width)
    # q.set_linestyle('dashed')
    ax.add_collection(q)
    
    # start_pt
    ax.scatter([x[0] for x in cartesian_locs[:1]], [x[1] for x in cartesian_locs[:1]], s = isovist_path_pt1, c='k', marker='s')

    # sequence
    ax.scatter([x[0] for x in cartesian_locs[1:-1]], [x[1] for x in cartesian_locs[1:-1]], s = isovist_path_pt1, c='red')

    # end pt
    ax.scatter([x[0] for x in cartesian_locs[-1:]], [x[1] for x in cartesian_locs[-1:]], s = isovist_path_pt2, c='k', marker='x')

    # boundaries
    edge_patches = []
    for segment in b_segments:
        if len(segment) > 5:
            polygon = Polygon(segment, False)
            edge_patches.append(polygon)
    p = PatchCollection(edge_patches)
    p.set_facecolor('none')
    p.set_edgecolor('#000000')
    p.set_linewidth(b_width)
    ax.scatter([x[0] for x in b_points], [x[1] for x in b_points], s = isovist_boundary_pt, c='#000000',)
    # ax.add_collection(p)


    # style
    ax.set_aspect('equal')
    lim = lim
    ax.set_xlim(-lim,lim)
    ax.set_ylim(-lim,lim)
    ax.set_xticks([])
    ax.set_yticks([])
    ax.axis('off')

    return fig

# plotting isovist sequence grid
def plot_isovist_sequence_grid(locs, isovists, figsize=(8,8), center=False, lim=1.5, alpha=0.02, rad=0.9, b_width=1.0, calculate_lim=False):
    fig = seq_show_grid(locs, isovists, figsize=figsize, center=center, lim=lim, alpha=alpha, rad=rad, b_width=b_width, calculate_lim=calculate_lim)
    # for plot with torchvision util
    fig.canvas.draw()
    data = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
    w, h = fig.canvas.get_width_height()
    im = data.reshape((int(h), int(w), -1))
    im = im.transpose((2, 0, 1))
    plt.close()
    return im

def get_bbox(pts):
    if len(pts) > 0:
        if type(pts) is list:
            pts = np.stack(pts)
        bbox = np.min(pts[:, 0]), np.max(pts[:, 0]), np.min(pts[:, 1]), np.max(pts[:, 1])
        return bbox
    else:
        return None

def get_center_pts(bbox, np_array = False):
    if bbox is not None:
        center = 0.5*(bbox[0] + bbox[1]),  0.5*(bbox[2] + bbox[3])
        if np_array:
            center = np.asarray(center)
    else:
        center = np.asarray([0,0])
    return center