author | unC0Rr |
Fri, 03 Feb 2023 15:59:18 +0100 | |
branch | transitional_engine |
changeset 15917 | 60b5639cc3a5 |
parent 15916 | e82de0410da5 |
child 15918 | 9bd828451d77 |
permissions | -rw-r--r-- |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
1 |
use super::transform::RotationTransform; |
15913 | 2 |
use std::rc::Rc; |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
3 |
use vec2d::Vec2D; |
15913 | 4 |
|
15917 | 5 |
#[derive(PartialEq, Clone, Debug)] |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
6 |
pub struct Edge<I: PartialEq + Clone> { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
7 |
id: I, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
8 |
symmetrical: bool, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
9 |
reverse: bool, |
15913 | 10 |
} |
11 |
||
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
12 |
impl<I: PartialEq + Clone> Edge<I> { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
13 |
pub fn new(id: I, symmetrical: bool) -> Self { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
14 |
Self { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
15 |
id, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
16 |
symmetrical, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
17 |
reverse: false, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
18 |
} |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
19 |
} |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
20 |
|
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
21 |
pub fn reversed(&self) -> Self { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
22 |
Self { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
23 |
id: self.id.clone(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
24 |
symmetrical: self.symmetrical, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
25 |
reverse: !self.symmetrical && !self.reverse, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
26 |
} |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
27 |
} |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
28 |
} |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
29 |
|
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
30 |
#[derive(Clone)] |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
31 |
pub struct TileImage<T, I: PartialEq + Clone> { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
32 |
image: Rc<Vec2D<T>>, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
33 |
transform: RotationTransform, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
34 |
top: Edge<I>, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
35 |
right: Edge<I>, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
36 |
bottom: Edge<I>, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
37 |
left: Edge<I>, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
38 |
} |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
39 |
|
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
40 |
impl<T: Copy, I: PartialEq + Clone> TileImage<T, I> { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
41 |
pub fn new( |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
42 |
image: Vec2D<T>, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
43 |
top: Edge<I>, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
44 |
right: Edge<I>, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
45 |
bottom: Edge<I>, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
46 |
left: Edge<I>, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
47 |
) -> Self { |
15913 | 48 |
Self { |
15915 | 49 |
image: Rc::new(image), |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
50 |
transform: RotationTransform::default(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
51 |
top, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
52 |
right, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
53 |
bottom, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
54 |
left, |
15913 | 55 |
} |
56 |
} |
|
57 |
||
58 |
pub fn mirrored(&self) -> Self { |
|
59 |
Self { |
|
60 |
image: self.image.clone(), |
|
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
61 |
transform: self.transform.mirror(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
62 |
top: self.top.reversed(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
63 |
right: self.left.reversed(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
64 |
bottom: self.bottom.reversed(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
65 |
left: self.right.reversed(), |
15913 | 66 |
} |
67 |
} |
|
68 |
||
69 |
pub fn flipped(&self) -> Self { |
|
70 |
Self { |
|
71 |
image: self.image.clone(), |
|
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
72 |
transform: self.transform.flip(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
73 |
top: self.bottom.reversed(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
74 |
right: self.right.reversed(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
75 |
bottom: self.top.reversed(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
76 |
left: self.left.reversed(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
77 |
} |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
78 |
} |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
79 |
|
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
80 |
pub fn rotated90(&self) -> Self { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
81 |
Self { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
82 |
image: self.image.clone(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
83 |
transform: self.transform.rotate90(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
84 |
top: self.left.clone(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
85 |
right: self.top.clone(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
86 |
bottom: self.right.clone(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
87 |
left: self.bottom.clone(), |
15913 | 88 |
} |
89 |
} |
|
15915 | 90 |
|
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
91 |
pub fn rotated180(&self) -> Self { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
92 |
Self { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
93 |
image: self.image.clone(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
94 |
transform: self.transform.rotate90(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
95 |
top: self.bottom.clone(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
96 |
right: self.left.clone(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
97 |
bottom: self.top.clone(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
98 |
left: self.right.clone(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
99 |
} |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
100 |
} |
15915 | 101 |
|
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
102 |
pub fn rotated270(&self) -> Self { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
103 |
Self { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
104 |
image: self.image.clone(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
105 |
transform: self.transform.rotate90(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
106 |
top: self.left.clone(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
107 |
right: self.top.clone(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
108 |
bottom: self.right.clone(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
109 |
left: self.bottom.clone(), |
15915 | 110 |
} |
111 |
} |
|
15917 | 112 |
|
113 |
pub fn right_edge(&self) -> &Edge<I> { |
|
114 |
&self.right |
|
115 |
} |
|
116 |
||
117 |
pub fn bottom_edge(&self) -> &Edge<I> { |
|
118 |
&self.bottom |
|
119 |
} |
|
120 |
||
121 |
pub fn left_edge(&self) -> &Edge<I> { |
|
122 |
&self.left |
|
123 |
} |
|
124 |
||
125 |
pub fn top_edge(&self) -> &Edge<I> { |
|
126 |
&self.top |
|
127 |
} |
|
15913 | 128 |
} |
15915 | 129 |
|
130 |
#[cfg(test)] |
|
15917 | 131 |
mod tests { |
132 |
use super::*; |
|
133 |
||
134 |
#[test] |
|
135 |
fn test_edge_new() { |
|
136 |
let edge = Edge::new(1, true); |
|
137 |
assert_eq!(edge.id, 1); |
|
138 |
assert_eq!(edge.symmetrical, true); |
|
139 |
assert_eq!(edge.reverse, false); |
|
140 |
} |
|
141 |
||
142 |
#[test] |
|
143 |
fn test_edge_reversed() { |
|
144 |
let edge = Edge::new(1, true); |
|
145 |
let reversed = edge.reversed(); |
|
146 |
assert_eq!(reversed.id, edge.id); |
|
147 |
assert_eq!(reversed.symmetrical, edge.symmetrical); |
|
148 |
assert_eq!(reversed.reverse, false); |
|
149 |
||
150 |
let edge = Edge::new(1, false); |
|
151 |
let reversed = edge.reversed(); |
|
152 |
assert_eq!(reversed.id, edge.id); |
|
153 |
assert_eq!(reversed.symmetrical, edge.symmetrical); |
|
154 |
assert_eq!(reversed.reverse, true); |
|
155 |
} |
|
156 |
||
157 |
#[test] |
|
158 |
fn test_edge_equality() { |
|
159 |
let edge1 = Edge::new(1, true); |
|
160 |
let edge2 = Edge::new(1, true); |
|
161 |
assert_eq!(edge1, edge2); |
|
162 |
||
163 |
let edge1 = Edge::new(1, false); |
|
164 |
let edge2 = Edge::new(1, false); |
|
165 |
assert_eq!(edge1, edge2); |
|
166 |
||
167 |
let edge1 = Edge::new(1, false); |
|
168 |
let edge2 = Edge::new(2, false); |
|
169 |
assert_ne!(edge1, edge2); |
|
170 |
} |
|
171 |
||
172 |
#[test] |
|
173 |
fn test_edge_equality_with_reverse() { |
|
174 |
let edge1 = Edge::new(1, true); |
|
175 |
let edge2 = edge1.reversed(); |
|
176 |
assert_eq!(edge1, edge2); |
|
177 |
||
178 |
let edge1 = Edge::new(1, false); |
|
179 |
let edge2 = edge1.reversed(); |
|
180 |
assert_ne!(edge1, edge2); |
|
181 |
||
182 |
let edge1 = Edge::new(1, true); |
|
183 |
let edge2 = edge1.reversed().reversed(); |
|
184 |
assert_eq!(edge1, edge2); |
|
185 |
} |
|
186 |
} |