-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from zzhgithub/opz-for-windows
Opz for windows
- Loading branch information
Showing
10 changed files
with
159 additions
and
27 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use bevy::utils::HashMap; | ||
use bit_vec::BitVec; | ||
use huffman_compress::{CodeBuilder, Tree}; | ||
|
||
use super::voxel::Voxel; | ||
|
||
pub fn compress(data: Vec<Voxel>) -> (BitVec, Tree<Voxel>) { | ||
let mut weights: HashMap<Voxel, i32> = HashMap::new(); | ||
for &voxel in &data { | ||
let count = weights.entry(voxel).or_insert(0); | ||
*count += 1; | ||
} | ||
|
||
let (book, tree) = CodeBuilder::from_iter(weights).finish(); | ||
let mut buffer = BitVec::new(); | ||
|
||
for voxel in &data { | ||
match book.encode(&mut buffer, voxel) { | ||
Ok(_) => {} | ||
Err(err) => { | ||
println!("{}", err); | ||
} | ||
} | ||
} | ||
(buffer, tree) | ||
} | ||
|
||
pub fn uncompress(buffer: &BitVec, tree: Tree<Voxel>) -> Vec<Voxel> { | ||
tree.decoder(buffer, buffer.len()).collect() | ||
} | ||
|
||
#[test] | ||
fn test() { | ||
let data = vec![ | ||
Voxel::EMPTY, | ||
Voxel::FILLED, | ||
Voxel::FILLED, | ||
Voxel::FILLED, | ||
Voxel::FILLED, | ||
Voxel::FILLED, | ||
Voxel::FILLED, | ||
Voxel::FILLED, | ||
Voxel::FILLED, | ||
Voxel::FILLED, | ||
Voxel::FILLED, | ||
]; | ||
let (buffer, tree) = compress(data.clone()); | ||
|
||
let new_data = uncompress(&buffer, tree); | ||
print!("{}", data.len()); | ||
print!("{}", new_data.len()); | ||
assert_eq!(data, new_data); | ||
} | ||
|
||
#[test] | ||
fn test_serialization() { | ||
let data = vec![ | ||
Voxel::EMPTY, | ||
Voxel::FILLED, | ||
Voxel::FILLED, | ||
Voxel::FILLED, | ||
Voxel::FILLED, | ||
Voxel::FILLED, | ||
Voxel::FILLED, | ||
Voxel::FILLED, | ||
Voxel::FILLED, | ||
Voxel::FILLED, | ||
Voxel::FILLED, | ||
]; | ||
let (buffer, tree) = compress(data.clone()); | ||
let tree_s = bincode::serialize(&tree).unwrap(); | ||
let tree_ds: Tree<Voxel> = bincode::deserialize(&tree_s).unwrap(); | ||
|
||
let a = uncompress(&buffer.clone(), tree); | ||
let b = uncompress(&buffer.clone(), tree_ds); | ||
|
||
assert_eq!(a, b); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ pub mod map_database; | |
pub mod map_generator; | ||
pub mod player_state; | ||
pub mod voxel; | ||
pub mod compress; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters