File size: 2,714 Bytes
d660fa9
 
 
 
 
 
 
 
f4542ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Pathfinder_test — interactive CLI tool for verifying pixel pathfinding.

Loads the campus map, computes a BFS path between two pixel coordinates,
and renders it in a matplotlib window.

Architecture: a developer tool, not part of the simulation runtime; it
exercises backend/pathfinder.py against the real walkability map.
Design: keeps the visual debugging loop out of the server code.
"""

import sys
import os
from PIL import Image, ImageDraw, ImageFont
import matplotlib.pyplot as plt
from pathfinder import shortest_path, stats, ROOT


def show_path_on_map(path, start, end, save_path=None):
    img = Image.open(os.path.join(ROOT, "frontend", "map.png")).convert("RGBA")
    overlay = Image.new("RGBA", img.size, (0, 0, 0, 0))
    draw = ImageDraw.Draw(overlay)

    if path:
        draw.line(path, fill=(255, 30, 30, 255), width=2)

        r = 5
        sx, sy = start
        ex, ey = end
        draw.ellipse([sx - r, sy - r, sx + r, sy + r], fill=(0, 220, 0, 255))
        draw.ellipse([ex - r, ey - r, ex + r, ey + r], fill=(30, 100, 255, 255))

        try:
            font = ImageFont.truetype("arial.ttf", 14)
        except (IOError, OSError):
            font = ImageFont.load_default()
        draw.text((sx + 7, sy - 9), "S", fill=(0, 220, 0, 255), font=font)
        draw.text((ex + 7, ey - 9), "E", fill=(30, 100, 255, 255), font=font)

    composited = Image.alpha_composite(img, overlay).convert("RGB")

    if save_path:
        composited.save(os.path.join(ROOT, save_path))
        print(f"Saved: {save_path}")
    else:
        plt.figure(figsize=(14, 14))
        plt.imshow(composited)
        plt.axis("off")
        plt.tight_layout(pad=0)
        plt.show()


if __name__ == "__main__":
    cmds = [a for a in sys.argv[1:] if not a.startswith("--")]
    flags = set(a for a in sys.argv[1:] if a.startswith("--"))

    if "--help" in flags or "-h" in flags:
        print("Usage: python backend/pixel_pathfinder.py [x1 y1 x2 y2]")
        print()
        print("  x1 y1 x2 y2    pixel coordinates for start and end")
        print("  --count        count white pixels only")
        sys.exit(0)

    if "--count" in flags:
        s = stats()
        print(f"White pixels: {s['white_pixels']} / {s['width']*s['height']}")
        sys.exit(0)

    if len(cmds) >= 4:
        sx, sy, ex, ey = int(cmds[0]), int(cmds[1]), int(cmds[2]), int(cmds[3])
    else:
        sx, sy = 32, 297
        ex, ey = 959, 1205

    start = (sx, sy)
    end = (ex, ey)

    print(f"Path: ({sx},{sy}) -> ({ex},{ey})")

    path = shortest_path(start, end)
    if path:
        print(f"Found: {len(path)} pixels")
        show_path_on_map(path, start, end)
    else:
        print("No path found")