Skip to content

Commit

Permalink
implemented random placement in bitmap
Browse files Browse the repository at this point in the history
  • Loading branch information
Inspirateur committed Nov 28, 2022
1 parent a38a202 commit e47ebbb
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 14 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ image = "*"
palette = "*"
serenity = "*"
anyhow = "*"
itertools = "*"
itertools = "*"
rand = "*"
37 changes: 24 additions & 13 deletions src/wordcloud/hxbitmap.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::{ops::{Shl, Shr}, fmt::Display, vec};
use anyhow::{Result, anyhow};
use fontdue::Metrics;
use itertools::{iproduct, enumerate};
use super::util::{bits, next_multiple};
use super::{util::{bits, next_multiple}, ring_reader::RingReader};
use rand::thread_rng;
use rand::seq::SliceRandom;
pub const CHARS: [&str; 2] = [" ", "█"];
// Horizontally Accelerated Bitmap
#[derive(Clone)]
Expand All @@ -16,14 +17,19 @@ pub struct HXBitmap {
// _w/usize::BITS
vec_w: usize,
data: Vec<usize>,
// efficiently spreads content around
poses: RingReader<(usize, usize)>
}

impl HXBitmap {
pub fn new(width: usize, height: usize) -> Self {
let _w = next_multiple(width, usize::BITS as usize);
let vec_w = _w/usize::BITS as usize;
let mut poses = Vec::from_iter(iproduct!(0..vec_w, 0..(height-3)));
poses.shuffle(&mut thread_rng());
Self {
width, height, _w, vec_w, data: vec![0; vec_w*height]
width, height, _w, vec_w, data: vec![0; vec_w*height],
poses: RingReader::new(poses)
}
}

Expand Down Expand Up @@ -103,14 +109,17 @@ impl HXBitmap {
));
}
let others = other.h_offsets();
for (vec_x, y) in iproduct!(0..(self.vec_w-other.vec_w), 0..(self.height-other.height)) {
for (dx, other) in enumerate(&others) {
if vec_x*usize::BITS as usize+dx >= self.width {
break;
}
if !self.overlaps(other, vec_x, y) {
self.add(other, vec_x, y);
return Ok((vec_x*usize::BITS as usize + dx, y));
while let Some((vec_x, y)) = self.poses.next() {
if y+other.height < self.height {
for (dx, other) in enumerate(&others) {
if vec_x*usize::BITS as usize+dx >= self.width {
break;
}
if !self.overlaps(other, vec_x, y) {
self.add(other, vec_x, y);
self.poses.reset();
return Ok((vec_x*usize::BITS as usize + dx, y));
}
}
}
}
Expand All @@ -128,7 +137,8 @@ impl Shl<u32> for HXBitmap {
height: self.height,
_w: self._w,
vec_w: self.vec_w,
data: self.data.into_iter().map(|v| v << rhs).collect()
data: self.data.into_iter().map(|v| v << rhs).collect(),
poses: self.poses.clone()
}
}
}
Expand All @@ -143,7 +153,8 @@ impl Shr<u32> for HXBitmap {
height: self.height,
_w: self._w,
vec_w: self.vec_w,
data: self.data.into_iter().map(|v| v >> rhs).collect()
data: self.data.into_iter().map(|v| v >> rhs).collect(),
poses: self.poses.clone()
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/wordcloud/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ mod indexed_chars;
mod text;
mod image;
mod rasterisable;
mod ring_reader;
pub use wordcloud::{wordcloud, Token};
36 changes: 36 additions & 0 deletions src/wordcloud/ring_reader.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#[derive(Clone)]
pub struct RingReader<E: Clone + Copy> {
data: Vec<E>,
start: usize,
end: usize,
}

impl<E: Clone + Copy> RingReader<E> {
pub fn new(data: Vec<E>) -> Self {
Self {
end: data.len(),
data: data,
start: 0
}
}

pub fn next(&mut self) -> Option<E> {
if self.start == self.end {
return None
}
if self.start >= self.data.len() {
self.start = 0;
}
let res = self.data[self.start];
self.start += 1;
Some(res)
}

pub fn reset(&mut self) {
self.end = if self.start == 0 {
self.data.len()
} else {
self.start-1
};
}
}

0 comments on commit e47ebbb

Please sign in to comment.