Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew Fisher <matt.fisher@microsoft.com>
  • Loading branch information
Matthew Fisher committed Dec 20, 2018
0 parents commit 2ae7150
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/target
**/*.rs.bk
Cargo.lock
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "treemap"
version = "0.1.0"
authors = ["Matthew Fisher <matt.fisher@microsoft.com>"]
edition = "2018"

[dependencies]
ord_subset = "3"
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Matthew Fisher

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
163 changes: 163 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
use ord_subset::OrdSubsetIterExt;

/// Interface representing an object that can be placed
/// in a treemap layout.
///
/// # Properties
///
/// - size: corresponds to area in map.
/// - order: the sort order of the item.
/// - depth: the depth in hierarchy.
/// - bounds: the bounding rectangle of the item in the map.
trait Mappable {
fn get_size(&self) -> f64;
fn set_size(&mut self, size: f64);
fn get_bounds(&self) -> &Rect;
fn set_bounds_from_rect(&mut self, bounds: Rect);
fn set_bounds_from_points(&mut self, x: f64, y: f64, w: f64, h: f64);
fn get_order(&self) -> i32;
fn set_order(&mut self, order: i32);
fn get_depth(&self) -> i32;
fn set_depth(&mut self, depth: i32);
}

/// Model object used by MapLayout to represent data for a treemap.
trait MapModel {
/// Get the list of items in this model. It returns an array of the Mappable objects in this MapModel.
fn get_items(&self) -> &mut [Box<Mappable>];
}

/// The interface for the treemap layout algorithm.
trait Layout {
/// Arrange the items in the given MapModel to fill the given rectangle.
///
/// # Parameters
///
/// - model: The MapModel.
/// - bounds: The bounding rectangle for the layout.
fn layout(&mut self, model: Box<MapModel>, bounds: Rect);
}

#[derive(Debug, PartialEq)]
pub struct Rect {
pub x: f64,
pub y: f64,
pub w: f64,
pub h: f64,
}

impl Rect {
pub fn new() -> Rect {
Rect {
x: 0.0,
y: 0.0,
w: 1.0,
h: 1.0,
}
}

pub fn new_from_points(x: f64, y: f64, w: f64, h: f64) -> Rect {
Rect {
x: x,
y: y,
w: w,
h: h,
}
}

pub fn new_from_rect(rect: Rect) -> Rect {
Rect::new_from_points(rect.x, rect.y, rect.w, rect.h)
}

pub fn aspect_ratio(&self) -> f64 {
let s = [self.w / self.h, self.h / self.w];
*s.iter().ord_subset_max().unwrap()
}
}

pub struct MapItem {
size: f64,
bounds: Rect,
order: i32,
depth: i32,
}

impl MapItem {
pub fn new() -> MapItem {
MapItem::new_from_size_and_order(1.0, 0)
}

pub fn new_from_size_and_order(size: f64, order: i32) -> MapItem {
MapItem {
size: size,
bounds: Rect::new(),
order: order,
depth: 0,
}
}
}

impl Mappable for MapItem {
fn get_size(&self) -> f64 {
self.size
}

fn set_size(&mut self, size: f64) {
self.size = size;
}

fn get_bounds(&self) -> &Rect {
&self.bounds
}

fn set_bounds_from_rect(&mut self, bounds: Rect) {
self.bounds = bounds;
}

fn set_bounds_from_points(&mut self, x: f64, y: f64, w: f64, h: f64) {
self.bounds.x = x;
self.bounds.y = y;
self.bounds.w = w;
self.bounds.h = h;
}

fn get_order(&self) -> i32 {
self.order
}

fn set_order(&mut self, order: i32) {
self.order = order;
}

fn get_depth(&self) -> i32 {
self.depth
}

fn set_depth(&mut self, depth: i32) {
self.depth = depth;
}
}

struct TreemapLayout {
mid: i32,
}

impl TreemapLayout {
pub fn layout_items(&mut self, items: &[Box<Mappable>], bounds: Rect) {
let sorted_items = sort_descending(items);
let end = (items.len() - 1) as i32;
self.layout_items_at(sorted_items, 0, end, bounds);
}
pub fn layout_items_at(&mut self, items: &[Box<Mappable>], start: i32, end: i32, bounds: Rect) {
}
}

impl Layout for TreemapLayout {
fn layout(&mut self, model: Box<MapModel>, bounds: Rect) {
self.layout_items(model.get_items(), bounds)
}
}

fn sort_descending(items: &[Box<Mappable>]) -> &[Box<Mappable>] {
items
}
24 changes: 24 additions & 0 deletions tests/rect.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
extern crate treemap;

use treemap::Rect;

#[test]
fn new_rect() {
assert_eq!(
Rect::new(),
Rect {
x: 0.0,
y: 0.0,
w: 1.0,
h: 1.0
}
);
}

#[test]
fn aspect_ratio() {
let rect = Rect::new();
assert_eq!(rect.aspect_ratio(), 1.0);
let rect2 = Rect::new_from_points(1.0, 1.0, 1.0, 5.0);
assert_eq!(rect2.aspect_ratio(), 5.0);
}

0 comments on commit 2ae7150

Please sign in to comment.