-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdjacency.py
220 lines (184 loc) · 8.18 KB
/
Adjacency.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
209
210
211
212
213
214
215
216
217
218
219
220
from __future__ import annotations
import functools
from copy import deepcopy
from dataclasses import dataclass, replace
from typing import List, Tuple, Optional, Dict, Set
from glm import ivec3
AXES: List[str] = ['xForward', 'xBackward', 'yForward', 'yBackward', 'zForward', 'zBackward']
ROTATIONAL_AXES: List[str] = ['xForward', 'zForward', 'xBackward', 'zBackward']
@dataclass(frozen=True)
class StructureRotation:
structureName: str
rotation: int
def rotate(self, amount: int) -> StructureRotation:
return replace(self, rotation=(self.rotation + amount) % 4)
def __eq__(self, other: StructureRotation) -> bool:
return self.rotation == other.rotation and self.structureName == other.structureName
def __hash__(self) -> int:
return hash((self.rotation, self.structureName))
class StructureAdjacency:
name: str
xForward: Set[StructureRotation]
xBackward: Set[StructureRotation]
yForward: Set[StructureRotation]
yBackward: Set[StructureRotation]
zForward: Set[StructureRotation]
zBackward: Set[StructureRotation]
walls: List[str]
def __init__(
self,
name: str,
xForward: Optional[List[Tuple[str, int]]] = None,
xBackward: Optional[List[Tuple[str, int]]] = None,
yForward: Optional[List[Tuple[str, int]]] = None,
yBackward: Optional[List[Tuple[str, int]]] = None,
zForward: Optional[List[Tuple[str, int]]] = None,
zBackward: Optional[List[Tuple[str, int]]] = None,
walls: Optional[List[str]] = None
):
self.name = name
self.xForward = createRotationSet(xForward)
self.xBackward = createRotationSet(xBackward)
self.yForward = createRotationSet(yForward)
self.yBackward = createRotationSet(yBackward)
self.zForward = createRotationSet(zForward)
self.zBackward = createRotationSet(zBackward)
self.walls = walls
if walls is not None and not set(walls).issubset(set(AXES)):
raise ValueError(f'Walls {walls} contains value not in {AXES}')
@functools.cache
def adjacentStructures(self, axis: str, selfRotation: int) -> Set[StructureRotation]:
selfRotation = selfRotation % 4
if axis not in AXES:
raise ValueError(f'Invalid axis "{axis}"')
if selfRotation == 0:
return getattr(self, axis)
rotationAxes = [self.xForward, self.zForward, self.xBackward, self.zBackward]
match axis:
case 'xForward':
return set(map(lambda r: r.rotate(selfRotation), rotationAxes[-selfRotation + 0]))
case 'zForward':
return set(map(lambda r: r.rotate(selfRotation), rotationAxes[-selfRotation + 1]))
case 'xBackward':
return set(map(lambda r: r.rotate(selfRotation), rotationAxes[-selfRotation + 2]))
case 'zBackward':
return set(map(lambda r: r.rotate(selfRotation), rotationAxes[-selfRotation + 3]))
case 'yForward':
return set(map(lambda r: r.rotate(selfRotation), self.yForward))
case 'yBackward':
return set(map(lambda r: r.rotate(selfRotation), self.yBackward))
raise ValueError(f'Invalid axis "{axis}"')
@functools.cache
def rotatedNonWallAxes(self, selfRotation: int) -> List[str]:
selfRotation = selfRotation % 4
if self.walls is None:
return AXES
openSpaces: List[str] = []
rotatedWalls: List[str] = []
for wall in self.walls:
if wall.startswith('y'):
rotatedWalls.append(wall)
else:
rotatedWalls.append(ROTATIONAL_AXES[(selfRotation + ROTATIONAL_AXES.index(wall)) % 4])
for axis in AXES:
if axis not in rotatedWalls:
openSpaces.append(axis)
return openSpaces
def getNonWallPositions(self, selfRotation: int, pos: ivec3, stateSpaceKeys: List[ivec3]) -> Set[ivec3]:
openPositions: Set[ivec3] = set()
for wall in self.rotatedNonWallAxes(selfRotation):
openPosition = getPositionFromAxis(axis=wall, pos=pos)[0]
if openPosition in stateSpaceKeys:
openPositions.add(openPosition)
return openPositions
def createRotationSet(rotationTuples: List[Tuple[str, int]]) -> Set[StructureRotation]:
rotationSet: Set[StructureRotation] = set()
for rotationTuple in rotationTuples:
if rotationTuple[1] == -1:
rotationSet.update(getAllRotations(structureName=rotationTuple[0]))
else:
rotationSet.add(StructureRotation(structureName=rotationTuple[0], rotation=rotationTuple[1]))
return rotationSet
@functools.cache
def getAllRotations(structureName: str) -> Set[StructureRotation]:
return set(StructureRotation(structureName, r) for r in range(4))
@functools.cache
def getOppositeAxis(axis: str) -> str:
match axis:
case 'xForward':
return 'xBackward'
case 'xBackward':
return 'xForward'
case 'yForward':
return 'yBackward'
case 'yBackward':
return 'yForward'
case 'zForward':
return 'zBackward'
case 'zBackward':
return 'zForward'
raise ValueError(f'axis "{axis}" has no opposite. Axis is invalid!')
@functools.cache
def getPositionFromAxis(axis: str, pos: ivec3) -> Tuple[ivec3, str]:
match axis:
case 'xBackward':
return ivec3(pos.x - 1, pos.y, pos.z), axis
case 'xForward':
return ivec3(pos.x + 1, pos.y, pos.z), axis
case 'yBackward':
return ivec3(pos.x, pos.y - 1, pos.z), axis
case 'yForward':
return ivec3(pos.x, pos.y + 1, pos.z), axis
case 'zBackward':
return ivec3(pos.x, pos.y, pos.z - 1), axis
case 'zForward':
return ivec3(pos.x, pos.y, pos.z + 1), axis
raise ValueError(f'Axis {axis} is invalid!')
def checkSymmetry(adjacencies: Dict[str, StructureAdjacency]):
for structureName in adjacencies.keys():
adjacency = adjacencies[structureName]
for axis in AXES:
rules: Set[StructureRotation] = getattr(adjacency, axis)
for rule in rules:
if rule.structureName not in adjacencies:
raise KeyError(f'{rule.structureName} in rules for {structureName} is not a valid structure!')
otherAdjecency = adjacencies[rule.structureName]
oppositeAxis = getOppositeAxis(axis)
otherRules: Set[StructureRotation] = otherAdjecency.adjacentStructures(
oppositeAxis,
rule.rotation
)
matchingRules = list(
filter(lambda r: r.structureName == structureName and r.rotation == 0, otherRules)
)
if len(matchingRules) == 0:
raise Exception(f'No symmetrical rule found for {structureName} {axis}.{rule} in '
f'{rule.structureName}!')
elif len(matchingRules) > 1:
raise Exception(f'Too many symmetrical rules found for {structureName} {axis}.{rule}: '
f'{matchingRules}!')
def omitAdjacencies(
adjacencies: Dict[str, StructureAdjacency],
omitList: List[str],
) -> Dict[str, StructureAdjacency]:
newAdjacencies = deepcopy(adjacencies)
omitRotations: Set[StructureRotation] = set()
for structureToOmit in omitList:
newAdjacencies.pop(structureToOmit)
omitRotations.update(getAllRotations(structureToOmit))
for adjacency in newAdjacencies.values():
for axis in AXES:
rules: Set[StructureRotation] = getattr(adjacency, axis)
rules = rules.difference(omitRotations)
setattr(adjacency, axis, rules)
checkSymmetry(newAdjacencies)
return newAdjacencies
def omitAdjacenciesWithZeroWeight(
adjacencies: Dict[str, StructureAdjacency],
weights: Dict[str, float],
) -> Dict[str, StructureAdjacency]:
omitList: List[str] = []
for structureName, weight in weights.items():
if weight == 0.0:
omitList.append(structureName)
return omitAdjacencies(adjacencies, omitList)