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