-
Notifications
You must be signed in to change notification settings - Fork 0
/
quilt.rs
158 lines (140 loc) · 4.22 KB
/
quilt.rs
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
use crate::matrix::Matrix;
use crate::raster::Raster;
use crate::geom::PointLike2D;
pub fn knit(image: Raster) -> Quilt {
let vertex_count = (image.width() + 1) * (image.height() + 1);
let pixel_count = image.width() * image.height();
let mut locmat: Matrix<f64> = Matrix::new(3, vertex_count);
let mut colmat: Matrix<u8> = Matrix::new(4, pixel_count);
let mut vi = 0;
for vy in 0..(image.height() + 1) {
for vx in 0..(image.width() + 1) {
let vec: &mut [f64] = locmat.get_col_mut(vi);
vec[0] = vx as f64;
vec[1] = vy as f64;
vec[2] = 1.0f64;
vi += 1;
}
}
let mut pi = 0;
for py in 0..image.height() {
for px in 0..image.width() {
let color = image.get_pixel(px, py);
let vec: &mut [u8] = colmat.get_col_mut(pi);
vec[0] = color.red;
vec[1] = color.green;
vec[2] = color.blue;
vec[3] = 1;
pi += 1;
}
}
Quilt { locmat, colmat, pwidth: image.width(), pheight: image.height() }
}
/// An image represented as a quilt of colored parallelograms of uniform dimension.
///
/// ```
/// p1 ------------ p2
/// | |
/// | |
/// | |
/// | |
/// | |
/// p3 ------------ p4
/// ```
///
pub struct Quilt {
pub locmat: Matrix<f64>,
pub colmat: Matrix<u8>,
pub pwidth: usize,
pub pheight: usize
}
pub struct Vertex<'a> { colv_ref: &'a [f64] }
pub struct TileColor<'a> { colv_ref: &'a [u8] }
#[derive(Clone)]
pub struct Tile<'a> {
quilt: &'a Quilt,
px: usize,
py: usize,
pi: usize,
uli: usize
}
impl Quilt {
pub fn tiles<'a>(&'a self) -> TileIterator<'a> {
let initial = Tile { quilt: self, px: 0, py: 0, pi: 0, uli: 0 };
return TileIterator { tile: initial };
}
pub fn p1<'a>(&'a self) -> Vertex<'a> {
let colv_ref = self.locmat.get_col(0);
Vertex { colv_ref }
}
pub fn p2<'a>(&'a self) -> Vertex<'a> {
let colv_ref = self.locmat.get_col(self.pwidth);
Vertex { colv_ref }
}
pub fn p3<'a>(&'a self) -> Vertex<'a> {
let i = (self.pwidth + 1) * self.pheight;
let colv_ref = self.locmat.get_col(i);
Vertex { colv_ref }
}
pub fn p4<'a>(&'a self) -> Vertex<'a> {
let i = self.locmat.colc() - 1;
let colv_ref = self.locmat.get_col(i);
Vertex { colv_ref }
}
}
impl<'a> Vertex<'a> {
pub fn vx(&self) -> f64 { self.colv_ref[0] }
pub fn vy(&self) -> f64 { self.colv_ref[1] }
}
impl<'a> PointLike2D for Vertex<'a> {
type T = f64;
fn to_tuple(&self) -> (Self::T, Self::T) { (self.vx(), self.vy()) }
}
impl<'a> TileColor<'a> {
pub fn red(&self) -> u8 { self.colv_ref[0] }
pub fn green(&self) -> u8 { self.colv_ref[1] }
pub fn blue(&self) -> u8 { self.colv_ref[2] }
}
impl<'a> Tile<'a> {
pub fn p1(&self) -> Vertex<'a> {
let colv_ref = self.quilt.locmat.get_col(self.uli);
return Vertex { colv_ref };
}
pub fn p2(&self) -> Vertex<'a> {
let colv_ref = self.quilt.locmat.get_col(self.uli + 1);
return Vertex { colv_ref };
}
pub fn p3(&self) -> Vertex<'a> {
let colv_ref = self.quilt.locmat.get_col(self.uli +
self.quilt.pwidth + 1);
return Vertex { colv_ref };
}
pub fn p4(&self) -> Vertex<'a> {
let colv_ref = self.quilt.locmat.get_col(self.uli +
self.quilt.pwidth + 2);
return Vertex { colv_ref };
}
pub fn color(&self) -> TileColor<'a> {
let colv_ref = self.quilt.colmat.get_col(self.pi);
return TileColor { colv_ref }
}
}
pub struct TileIterator<'a> { tile: Tile<'a> }
impl<'a> Iterator for TileIterator<'a> {
type Item = Tile<'a>;
fn next(&mut self) -> Option<Self::Item> {
if self.tile.py == self.tile.quilt.pheight {
return None;
}
let elapsed = self.tile.clone();
self.tile.px += 1;
if self.tile.px >= self.tile.quilt.pwidth {
self.tile.px = 0;
self.tile.py += 1;
self.tile.uli += 1;
}
self.tile.pi += 1;
self.tile.uli += 1;
return Some(elapsed);
}
}