mirror of
https://codeberg.org/icewind/sourcenav.git
synced 2026-06-03 10:14:11 +02:00
use file/buffreader in docs
This commit is contained in:
parent
048a2e7dbe
commit
d61a80d83f
6 changed files with 61 additions and 37 deletions
|
|
@ -10,11 +10,12 @@ navigation areas are exposed.
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
use sourcenav::get_quad_tree;
|
use sourcenav::get_quad_tree;
|
||||||
use std::io::Cursor;
|
use std::fs::File;
|
||||||
|
use std::io::BufReader;
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let file = std::fs::read("data/pl_badwater.nav")?;
|
let mut file = BufReader::new(File::open("data/pl_badwater.nav")?);
|
||||||
let tree = get_quad_tree(&mut Cursor::new(file))?;
|
let tree = get_quad_tree(&mut file)?;
|
||||||
|
|
||||||
assert_eq!(220.83125, tree.find_best_height(320.0, -1030.0, 0.0));
|
assert_eq!(220.83125, tree.find_best_height(320.0, -1030.0, 0.0));
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
use std::hint::black_box;
|
|
||||||
use criterion::{criterion_group, criterion_main, Criterion};
|
use criterion::{criterion_group, criterion_main, Criterion};
|
||||||
use sourcenav::{get_quad_tree, read_areas};
|
use sourcenav::{get_quad_tree, read_areas};
|
||||||
use std::fs::read;
|
use std::fs::read;
|
||||||
|
use std::hint::black_box;
|
||||||
use std::io::Cursor;
|
use std::io::Cursor;
|
||||||
|
|
||||||
fn bench_badwater_areas(c: &mut Criterion) {
|
fn bench_badwater_areas(c: &mut Criterion) {
|
||||||
|
|
@ -30,10 +30,19 @@ fn bench_tree_query(c: &mut Criterion) {
|
||||||
|
|
||||||
c.bench_function("badwater areas", |b| {
|
c.bench_function("badwater areas", |b| {
|
||||||
b.iter(|| {
|
b.iter(|| {
|
||||||
let _ = black_box(tree.find_best_height(black_box(320.0), black_box(-1030.0), black_box(0.0)));
|
let _ = black_box(tree.find_best_height(
|
||||||
|
black_box(320.0),
|
||||||
|
black_box(-1030.0),
|
||||||
|
black_box(0.0),
|
||||||
|
));
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
criterion_group!(benches, bench_badwater_areas, bench_badwater_quads, bench_tree_query);
|
criterion_group!(
|
||||||
criterion_main!(benches);
|
benches,
|
||||||
|
bench_badwater_areas,
|
||||||
|
bench_badwater_quads,
|
||||||
|
bench_tree_query
|
||||||
|
);
|
||||||
|
criterion_main!(benches);
|
||||||
|
|
|
||||||
|
|
@ -10,5 +10,8 @@
|
||||||
inputs.flakelight.follows = "flakelight";
|
inputs.flakelight.follows = "flakelight";
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
outputs = {mill-scale, ...}: mill-scale ./. {};
|
outputs = {mill-scale, ...}:
|
||||||
|
mill-scale ./. {
|
||||||
|
extraPaths = [./data];
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
36
src/lib.rs
36
src/lib.rs
|
|
@ -22,11 +22,12 @@ pub struct NavQuadTree(QuadTree<NavQuad, HammerUnit, [(ItemId, Rect); 4]>);
|
||||||
///
|
///
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
/// use sourcenav::get_quad_tree;
|
/// use sourcenav::get_quad_tree;
|
||||||
/// use std::io::Cursor;
|
/// use std::fs::File;
|
||||||
|
/// use std::io::BufReader;
|
||||||
///
|
///
|
||||||
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
|
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
/// let file = std::fs::read("path/to/navfile.nav")?;
|
/// let mut file = BufReader::new(File::open("path/to/navfile.nav")?);
|
||||||
/// let tree = get_quad_tree(&mut Cursor::new(file))?;
|
/// let tree = get_quad_tree(&mut file)?;
|
||||||
/// # Ok(())
|
/// # Ok(())
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
|
|
@ -67,11 +68,12 @@ impl NavQuadTree {
|
||||||
///
|
///
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
/// use sourcenav::get_quad_tree;
|
/// use sourcenav::get_quad_tree;
|
||||||
/// use std::io::Cursor;
|
/// use std::fs::File;
|
||||||
|
/// use std::io::BufReader;
|
||||||
///
|
///
|
||||||
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
|
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
/// let file = std::fs::read("path/to/navfile.nav")?;
|
/// let mut file = BufReader::new(File::open("path/to/navfile.nav")?);
|
||||||
/// let tree = get_quad_tree(&mut Cursor::new(file))?;
|
/// let tree = get_quad_tree(&mut file)?;
|
||||||
/// let areas = tree.query(150.0, -312.0);
|
/// let areas = tree.query(150.0, -312.0);
|
||||||
/// # Ok(())
|
/// # Ok(())
|
||||||
/// # }
|
/// # }
|
||||||
|
|
@ -90,11 +92,12 @@ impl NavQuadTree {
|
||||||
///
|
///
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
/// use sourcenav::get_quad_tree;
|
/// use sourcenav::get_quad_tree;
|
||||||
/// use std::io::Cursor;
|
/// use std::fs::File;
|
||||||
|
/// use std::io::BufReader;
|
||||||
///
|
///
|
||||||
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
|
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
/// let file = std::fs::read("path/to/navfile.nav")?;
|
/// let mut file = BufReader::new(File::open("path/to/navfile.nav")?);
|
||||||
/// let tree = get_quad_tree(&mut Cursor::new(file))?;
|
/// let tree = get_quad_tree(&mut file)?;
|
||||||
/// let heights = tree.find_z_height(150.0, -312.0);
|
/// let heights = tree.find_z_height(150.0, -312.0);
|
||||||
/// # Ok(())
|
/// # Ok(())
|
||||||
/// # }
|
/// # }
|
||||||
|
|
@ -125,11 +128,12 @@ impl NavQuadTree {
|
||||||
///
|
///
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
/// use sourcenav::get_quad_tree;
|
/// use sourcenav::get_quad_tree;
|
||||||
/// use std::io::Cursor;
|
/// use std::fs::File;
|
||||||
|
/// use std::io::BufReader;
|
||||||
///
|
///
|
||||||
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
|
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
/// let file = std::fs::read("path/to/navfile.nav")?;
|
/// let mut file = BufReader::new(File::open("path/to/navfile.nav")?);
|
||||||
/// let tree = get_quad_tree(&mut Cursor::new(file))?;
|
/// let tree = get_quad_tree(&mut file)?;
|
||||||
/// for quad in tree.quads() {
|
/// for quad in tree.quads() {
|
||||||
/// println!("area: {:?}", quad)
|
/// println!("area: {:?}", quad)
|
||||||
/// }
|
/// }
|
||||||
|
|
@ -143,11 +147,11 @@ impl NavQuadTree {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_tree() {
|
fn test_tree() {
|
||||||
use std::io::Cursor;
|
use std::fs::File;
|
||||||
|
use std::io::BufReader;
|
||||||
|
|
||||||
let file = std::fs::read("data/pl_badwater.nav").unwrap();
|
let mut file = BufReader::new(File::open("data/pl_badwater.nav").unwrap());
|
||||||
let mut cursor = Cursor::new(&file);
|
let tree = get_quad_tree(&mut file).unwrap();
|
||||||
let tree = get_quad_tree(&mut cursor).unwrap();
|
|
||||||
|
|
||||||
// single flat plane
|
// single flat plane
|
||||||
let point1 = (1600.0, -1300.0);
|
let point1 = (1600.0, -1300.0);
|
||||||
|
|
|
||||||
|
|
@ -3,10 +3,10 @@ use aabb_quadtree::Spatial;
|
||||||
use binrw::io::{Read, Seek};
|
use binrw::io::{Read, Seek};
|
||||||
use binrw::{BinRead, BinReaderExt, BinResult, Endian};
|
use binrw::{BinRead, BinReaderExt, BinResult, Endian};
|
||||||
use euclid::{TypedPoint2D, TypedSize2D};
|
use euclid::{TypedPoint2D, TypedSize2D};
|
||||||
|
use std::convert::TryFrom;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
use std::ops::Index;
|
use std::ops::Index;
|
||||||
use std::convert::TryFrom;
|
|
||||||
|
|
||||||
/// A 3 dimensional coordinate
|
/// A 3 dimensional coordinate
|
||||||
#[derive(Debug, BinRead)]
|
#[derive(Debug, BinRead)]
|
||||||
|
|
@ -28,7 +28,11 @@ pub struct NavAreaFlags(pub u32);
|
||||||
impl BinRead for NavAreaFlags {
|
impl BinRead for NavAreaFlags {
|
||||||
type Args<'a> = (u32,);
|
type Args<'a> = (u32,);
|
||||||
|
|
||||||
fn read_options<R: Read + Seek>(reader: &mut R, endian: Endian, args: Self::Args<'static>) -> BinResult<Self> {
|
fn read_options<R: Read + Seek>(
|
||||||
|
reader: &mut R,
|
||||||
|
endian: Endian,
|
||||||
|
args: Self::Args<'static>,
|
||||||
|
) -> BinResult<Self> {
|
||||||
let flags = match args.0 {
|
let flags = match args.0 {
|
||||||
0..=8 => reader.read_type::<u8>(endian)? as u32,
|
0..=8 => reader.read_type::<u8>(endian)? as u32,
|
||||||
9..=12 => reader.read_type::<u16>(endian)? as u32,
|
9..=12 => reader.read_type::<u16>(endian)? as u32,
|
||||||
|
|
@ -277,11 +281,12 @@ impl NavQuad {
|
||||||
///
|
///
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
/// use sourcenav::get_quad_tree;
|
/// use sourcenav::get_quad_tree;
|
||||||
/// use std::io::Cursor;
|
/// use std::fs::File;
|
||||||
|
/// use std::io::BufReader;
|
||||||
///
|
///
|
||||||
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
|
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
/// let file = std::fs::read("path/to/navfile.nav")?;
|
/// let mut file = BufReader::new(File::open("path/to/navfile.nav")?);
|
||||||
/// let tree = get_quad_tree(&mut Cursor::new(file))?;
|
/// let tree = get_quad_tree(&mut file)?;
|
||||||
/// let area = tree.query(150.0, -312.0).next().unwrap();
|
/// let area = tree.query(150.0, -312.0).next().unwrap();
|
||||||
///
|
///
|
||||||
/// let height = area.get_z_height(150.0, -312.0);
|
/// let height = area.get_z_height(150.0, -312.0);
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
pub use crate::navmesh::NavArea;
|
pub use crate::navmesh::NavArea;
|
||||||
use binrw::io::{Read, Seek};
|
use binrw::io::{Read, Seek};
|
||||||
use binrw::{BinRead, BinReaderExt};
|
use binrw::{BinRead, BinReaderExt};
|
||||||
use thiserror::Error;
|
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
/// Errors that can occur when parsing the binary nav file
|
/// Errors that can occur when parsing the binary nav file
|
||||||
#[derive(Debug, Error)]
|
#[derive(Debug, Error)]
|
||||||
|
|
@ -35,16 +35,16 @@ struct FixedString {
|
||||||
///
|
///
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
/// use sourcenav::read_areas;
|
/// use sourcenav::read_areas;
|
||||||
|
/// use std::fs::File;
|
||||||
|
/// use std::io::BufReader;
|
||||||
///
|
///
|
||||||
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
|
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
/// use std::io::Cursor;
|
/// let mut file = BufReader::new(File::open("path/to/navfile.nav")?);
|
||||||
/// let file = std::fs::read("path/to/navfile.nav")?;
|
/// let tree = read_areas(&mut file)?;
|
||||||
/// let tree = read_areas(&mut Cursor::new(file))?;
|
|
||||||
/// # Ok(())
|
/// # Ok(())
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn read_areas<R: Read + Seek>(data: &mut R) -> Result<Vec<NavArea>, ParseError> {
|
pub fn read_areas<R: Read + Seek>(data: &mut R) -> Result<Vec<NavArea>, ParseError> {
|
||||||
|
|
||||||
let magic = data.read_le()?;
|
let magic = data.read_le()?;
|
||||||
if magic != 0xFEED_FACE {
|
if magic != 0xFEED_FACE {
|
||||||
return Err(ParseError::InvalidMagicNumber(magic));
|
return Err(ParseError::InvalidMagicNumber(magic));
|
||||||
|
|
@ -97,8 +97,10 @@ pub fn read_areas<R: Read + Seek>(data: &mut R) -> Result<Vec<NavArea>, ParseErr
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test() {
|
fn test() {
|
||||||
use std::io::Cursor;
|
use std::fs::File;
|
||||||
let file = std::fs::read("data/pl_badwater.nav").unwrap();
|
use std::io::BufReader;
|
||||||
let areas = read_areas(&mut Cursor::new(file)).unwrap();
|
|
||||||
|
let mut file = BufReader::new(File::open("data/pl_badwater.nav").unwrap());
|
||||||
|
let areas = read_areas(&mut file).unwrap();
|
||||||
assert_eq!(1930, areas.len());
|
assert_eq!(1930, areas.len());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue