author | unC0Rr |
Mon, 13 Feb 2023 11:00:12 +0100 | |
branch | transitional_engine |
changeset 15951 | 5f00829c55ec |
parent 15950 | 168f44ef9b67 |
child 15954 | 9502611bffc1 |
permissions | -rw-r--r-- |
15948
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
1 |
use super::transform::Transform; |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
2 |
use integral_geometry::Size; |
15943 | 3 |
use std::rc::Rc; |
15946
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
4 |
use vec2d::Vec2D; |
15943 | 5 |
|
15947 | 6 |
#[derive(PartialEq, Clone, Debug)] |
15946
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
7 |
pub struct Edge<I: PartialEq + Clone> { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
8 |
id: I, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
9 |
symmetrical: bool, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
10 |
reverse: bool, |
15943 | 11 |
} |
12 |
||
15946
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
13 |
impl<I: PartialEq + Clone> Edge<I> { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
14 |
pub fn new(id: I, symmetrical: bool) -> Self { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
15 |
Self { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
16 |
id, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
17 |
symmetrical, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
18 |
reverse: false, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
19 |
} |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
20 |
} |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
21 |
|
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
22 |
pub fn reversed(&self) -> Self { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
23 |
Self { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
24 |
id: self.id.clone(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
25 |
symmetrical: self.symmetrical, |
15951 | 26 |
reverse: !self.symmetrical && !self.reverse, |
15946
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
27 |
} |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
28 |
} |
15948
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
29 |
|
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
30 |
pub fn is_compatible(&self, other: &Self) -> bool { |
15949 | 31 |
self.id == other.id && ((self.reverse != other.reverse) || self.symmetrical) |
15948
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
32 |
} |
15946
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
33 |
} |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
34 |
|
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
35 |
#[derive(Clone)] |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
36 |
pub struct TileImage<T, I: PartialEq + Clone> { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
37 |
image: Rc<Vec2D<T>>, |
15949 | 38 |
pub transform: Transform, |
15946
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
39 |
top: Edge<I>, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
40 |
right: Edge<I>, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
41 |
bottom: Edge<I>, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
42 |
left: Edge<I>, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
43 |
} |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
44 |
|
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
45 |
impl<T: Copy, I: PartialEq + Clone> TileImage<T, I> { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
46 |
pub fn new( |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
47 |
image: Vec2D<T>, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
48 |
top: Edge<I>, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
49 |
right: Edge<I>, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
50 |
bottom: Edge<I>, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
51 |
left: Edge<I>, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
52 |
) -> Self { |
15943 | 53 |
Self { |
15945 | 54 |
image: Rc::new(image), |
15948
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
55 |
transform: Transform::default(), |
15946
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
56 |
top, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
57 |
right, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
58 |
bottom, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
59 |
left, |
15943 | 60 |
} |
61 |
} |
|
62 |
||
63 |
pub fn mirrored(&self) -> Self { |
|
64 |
Self { |
|
65 |
image: self.image.clone(), |
|
15946
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
66 |
transform: self.transform.mirror(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
67 |
top: self.top.reversed(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
68 |
right: self.left.reversed(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
69 |
bottom: self.bottom.reversed(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
70 |
left: self.right.reversed(), |
15943 | 71 |
} |
72 |
} |
|
73 |
||
74 |
pub fn flipped(&self) -> Self { |
|
75 |
Self { |
|
76 |
image: self.image.clone(), |
|
15946
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
77 |
transform: self.transform.flip(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
78 |
top: self.bottom.reversed(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
79 |
right: self.right.reversed(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
80 |
bottom: self.top.reversed(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
81 |
left: self.left.reversed(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
82 |
} |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
83 |
} |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
84 |
|
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
85 |
pub fn rotated90(&self) -> Self { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
86 |
Self { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
87 |
image: self.image.clone(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
88 |
transform: self.transform.rotate90(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
89 |
top: self.left.clone(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
90 |
right: self.top.clone(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
91 |
bottom: self.right.clone(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
92 |
left: self.bottom.clone(), |
15943 | 93 |
} |
94 |
} |
|
15945 | 95 |
|
15946
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
96 |
pub fn rotated180(&self) -> Self { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
97 |
Self { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
98 |
image: self.image.clone(), |
15948
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
99 |
transform: self.transform.rotate180(), |
15946
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
100 |
top: self.bottom.clone(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
101 |
right: self.left.clone(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
102 |
bottom: self.top.clone(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
103 |
left: self.right.clone(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
104 |
} |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
105 |
} |
15945 | 106 |
|
15946
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
107 |
pub fn rotated270(&self) -> Self { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
108 |
Self { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
109 |
image: self.image.clone(), |
15948
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
110 |
transform: self.transform.rotate270(), |
15949 | 111 |
top: self.right.clone(), |
112 |
right: self.bottom.clone(), |
|
113 |
bottom: self.left.clone(), |
|
114 |
left: self.top.clone(), |
|
15945 | 115 |
} |
116 |
} |
|
15947 | 117 |
|
118 |
pub fn right_edge(&self) -> &Edge<I> { |
|
119 |
&self.right |
|
120 |
} |
|
121 |
||
122 |
pub fn bottom_edge(&self) -> &Edge<I> { |
|
123 |
&self.bottom |
|
124 |
} |
|
125 |
||
126 |
pub fn left_edge(&self) -> &Edge<I> { |
|
127 |
&self.left |
|
128 |
} |
|
129 |
||
130 |
pub fn top_edge(&self) -> &Edge<I> { |
|
131 |
&self.top |
|
132 |
} |
|
15948
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
133 |
|
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
134 |
pub fn size(&self) -> Size { |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
135 |
match self.transform { |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
136 |
Transform::Rotate0(_) => self.image.size(), |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
137 |
Transform::Rotate90(_) => Size::new(self.image.size().height, self.image.size().width), |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
138 |
} |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
139 |
} |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
140 |
|
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
141 |
pub fn get(&self, row: usize, column: usize) -> Option<&T> { |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
142 |
match self.transform { |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
143 |
Transform::Rotate0(_) => { |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
144 |
let image_row = if self.transform.is_flipped() { |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
145 |
self.image.height().wrapping_sub(1).wrapping_sub(row) |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
146 |
} else { |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
147 |
row |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
148 |
}; |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
149 |
|
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
150 |
let image_column = if self.transform.is_mirrored() { |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
151 |
self.image.width().wrapping_sub(1).wrapping_sub(column) |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
152 |
} else { |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
153 |
column |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
154 |
}; |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
155 |
|
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
156 |
self.image.get(image_row, image_column) |
15950 | 157 |
} |
15948
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
158 |
Transform::Rotate90(_) => { |
15949 | 159 |
let image_row = if self.transform.is_mirrored() { |
15948
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
160 |
column |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
161 |
} else { |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
162 |
self.image.height().wrapping_sub(1).wrapping_sub(column) |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
163 |
}; |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
164 |
|
15949 | 165 |
let image_column = if self.transform.is_flipped() { |
15948
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
166 |
self.image.width().wrapping_sub(1).wrapping_sub(row) |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
167 |
} else { |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
168 |
row |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
169 |
}; |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
170 |
|
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
171 |
self.image.get(image_row, image_column) |
15950 | 172 |
} |
15948
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
173 |
} |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15947
diff
changeset
|
174 |
} |
15943 | 175 |
} |
15945 | 176 |
|
177 |
#[cfg(test)] |
|
15947 | 178 |
mod tests { |
179 |
use super::*; |
|
180 |
||
181 |
#[test] |
|
182 |
fn test_edge_new() { |
|
183 |
let edge = Edge::new(1, true); |
|
184 |
assert_eq!(edge.id, 1); |
|
185 |
assert_eq!(edge.symmetrical, true); |
|
186 |
assert_eq!(edge.reverse, false); |
|
187 |
} |
|
188 |
||
189 |
#[test] |
|
190 |
fn test_edge_reversed() { |
|
191 |
let edge = Edge::new(1, true); |
|
192 |
let reversed = edge.reversed(); |
|
193 |
assert_eq!(reversed.id, edge.id); |
|
194 |
assert_eq!(reversed.symmetrical, edge.symmetrical); |
|
195 |
assert_eq!(reversed.reverse, false); |
|
196 |
||
197 |
let edge = Edge::new(1, false); |
|
198 |
let reversed = edge.reversed(); |
|
199 |
assert_eq!(reversed.id, edge.id); |
|
200 |
assert_eq!(reversed.symmetrical, edge.symmetrical); |
|
201 |
assert_eq!(reversed.reverse, true); |
|
202 |
} |
|
203 |
||
204 |
#[test] |
|
205 |
fn test_edge_equality() { |
|
206 |
let edge1 = Edge::new(1, true); |
|
207 |
let edge2 = Edge::new(1, true); |
|
208 |
assert_eq!(edge1, edge2); |
|
209 |
||
210 |
let edge1 = Edge::new(1, false); |
|
211 |
let edge2 = Edge::new(1, false); |
|
212 |
assert_eq!(edge1, edge2); |
|
213 |
||
214 |
let edge1 = Edge::new(1, false); |
|
215 |
let edge2 = Edge::new(2, false); |
|
216 |
assert_ne!(edge1, edge2); |
|
217 |
} |
|
218 |
||
219 |
#[test] |
|
220 |
fn test_edge_equality_with_reverse() { |
|
221 |
let edge1 = Edge::new(1, true); |
|
222 |
let edge2 = edge1.reversed(); |
|
223 |
assert_eq!(edge1, edge2); |
|
224 |
||
225 |
let edge1 = Edge::new(1, false); |
|
226 |
let edge2 = edge1.reversed(); |
|
227 |
assert_ne!(edge1, edge2); |
|
228 |
||
229 |
let edge1 = Edge::new(1, true); |
|
230 |
let edge2 = edge1.reversed().reversed(); |
|
231 |
assert_eq!(edge1, edge2); |
|
232 |
} |
|
233 |
} |