This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
use common::OutputPicture;
|
||||
use pathfinding::prelude::dijkstra;
|
||||
use weblog::console_log;
|
||||
|
||||
pub fn get_common_height(row: &[OutputPicture], container_width: u32, margin: u32) -> f32 {
|
||||
pub struct Rect {
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
}
|
||||
|
||||
pub fn get_common_height(row: &[Rect], container_width: u32, margin: u32) -> f32 {
|
||||
let row_width: u32 = container_width - row.len() as u32 * (margin * 2);
|
||||
let total_aspect_ratio: f32 = row
|
||||
.iter()
|
||||
@@ -12,7 +15,7 @@ pub fn get_common_height(row: &[OutputPicture], container_width: u32, margin: u3
|
||||
}
|
||||
|
||||
pub fn cost(
|
||||
photos: &[OutputPicture],
|
||||
photos: &[Rect],
|
||||
i: usize,
|
||||
j: usize,
|
||||
width: u32,
|
||||
@@ -26,14 +29,14 @@ pub fn cost(
|
||||
pub fn make_successors(
|
||||
target_height: u32,
|
||||
container_width: u32,
|
||||
photos: &Vec<OutputPicture>,
|
||||
limit_node_search: usize,
|
||||
photos: &Vec<Rect>,
|
||||
node_search_limit: usize,
|
||||
margin: u32,
|
||||
) -> Vec<Vec<(usize, u32)>> {
|
||||
let mut results = vec![Vec::new(); photos.len()];
|
||||
(0..photos.len()).for_each(|start| {
|
||||
for j in start + 1..photos.len() + 1 {
|
||||
if j - start > limit_node_search {
|
||||
if j - start > node_search_limit {
|
||||
break;
|
||||
}
|
||||
results[start].push((
|
||||
@@ -45,29 +48,28 @@ pub fn make_successors(
|
||||
results
|
||||
}
|
||||
|
||||
pub fn find_ideal_node_search(target_row_height: u32, container_width: u32) -> usize {
|
||||
pub fn calc_node_search_limit(target_row_height: u32, container_width: u32) -> usize {
|
||||
let row_aspect_ratio = container_width as f32 / target_row_height as f32;
|
||||
(row_aspect_ratio / 1.5) as usize + 8
|
||||
}
|
||||
|
||||
pub fn compute_row_layout(
|
||||
container_width: u32,
|
||||
limit_node_search: usize,
|
||||
target_height: u32,
|
||||
margin: u32,
|
||||
photos: &Vec<OutputPicture>,
|
||||
) -> Option<Vec<(u32, u32)>> {
|
||||
console_log!("compute row layout for width: {}", container_width);
|
||||
|
||||
photos: &Vec<Rect>,
|
||||
) -> Option<Vec<Rect>> {
|
||||
if photos.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let node_search_limit = calc_node_search_limit(target_height, container_width);
|
||||
|
||||
let successors = make_successors(
|
||||
target_height,
|
||||
container_width,
|
||||
photos,
|
||||
limit_node_search,
|
||||
node_search_limit,
|
||||
margin,
|
||||
);
|
||||
let path = dijkstra(&0, |p| successors[*p].clone(), |p| *p == photos.len());
|
||||
@@ -76,13 +78,16 @@ pub fn compute_row_layout(
|
||||
} else {
|
||||
(Vec::new(), 0)
|
||||
};
|
||||
let mut dimensions: Vec<(u32, u32)> = Vec::with_capacity(photos.len());
|
||||
let mut dimensions: Vec<Rect> = Vec::with_capacity(photos.len());
|
||||
for i in 1..path.len() {
|
||||
let row = &photos[path[i - 1]..path[i]];
|
||||
let height = get_common_height(row, container_width, margin) as u32;
|
||||
(path[i - 1]..path[i]).for_each(|j| {
|
||||
let ratio = photos[j].width as f32 / photos[j].height as f32;
|
||||
dimensions.push(((height as f32 * ratio) as u32, height));
|
||||
dimensions.push(Rect {
|
||||
width: (height as f32 * ratio) as u32,
|
||||
height,
|
||||
});
|
||||
});
|
||||
}
|
||||
Some(dimensions)
|
||||
|
||||
Reference in New Issue
Block a user