-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpatchmap.py
More file actions
76 lines (61 loc) · 2.18 KB
/
patchmap.py
File metadata and controls
76 lines (61 loc) · 2.18 KB
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
# A data structure that keeps track of a set of transformations which
# might be used as part of an n-patch, and a set of cells that make up
# a current patch. We also track, for all possible patch cells, which
# transformed tiles could potentially occupy those cells. Used as part
# of recursively surrounding (n-1)-patches to construct n-patches.
from hat import *
from kitegrid import *
class PatchMap:
def __init__(self, ts):
self.shapes = {}
self.users = {}
self.occupied = set()
for T in ts:
ps = set([T * p for p in hat])
self.shapes[T] = ps
for P in ps:
self.users.setdefault(P, []).append(T)
def copy(self):
# Based on the recursive computation in surround.py, we need
# a deep copy of the occupancy map, but we can keep the other
# information as-is (it won't be modified)
ret = PatchMap([])
ret.shapes = self.shapes
ret.users = self.users
ret.occupied = self.occupied.copy()
return ret
def isOccupied(self, tp):
"""Check if an individual cell, or if any of a list of cells, is
currently occupied."""
if isinstance(tp, Point):
return tp in self.occupied
else:
ps = self.shapes[tp]
for P in ps:
if P in self.occupied:
return True
return False
def setOccupied(self, tp):
if isinstance(tp, Point):
self.occupied.add(tp)
else:
ps = self.shapes[tp]
for P in ps:
self.occupied.add(P)
def setUnoccupied(self, tp):
if isinstance(tp, Point):
self.occupied.remove(tp)
else:
ps = self.shapes[tp]
for P in ps:
self.occupied.remove(P)
def isValid(self):
"""Verify that a patch is legitimate."""
# Must be simply connected. That's basically it.
return isSimplyConnected(self.occupied)
def getCells(self, T):
return self.shapes[T]
def getUsers(self, P):
return self.users[P]
def getHalo(self):
return getOrderedHalo(self.occupied)