Skip to content

Commit

Permalink
WIP trying to bump aabb-quadtree
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre Fenoll <[email protected]>
  • Loading branch information
fenollp committed Jul 27, 2024
1 parent 9191261 commit 9ffa372
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 35 deletions.
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ epoll = { version = "4.3.1", optional = true }
fxhash = { version = "0.2.1", optional = true }

# appctx
aabb-quadtree = { version = "0.1.0", optional = true }
# aabb-quadtree = { git = "https://github.com/avl/aabb-quadtree.git", rev = "ef2c835", optional = true } # https://github.com/TyOverby/aabb-quadtree/pull/1 # aabb-quadtree = { version = "0.2.0", optional = true }
aabb-quadtree = { version = "0.2.0", optional = true }
euclid = { version = "0.19.9", optional = true } # Identical version as aabb-quadtree's
smallvec = { version = "0.6.14", optional = true } # Identical version as aabb-quadtree's

# hlua
hlua = { version = "0.4.1", optional = true }
Expand All @@ -53,7 +56,7 @@ framebuffer-text-drawing = ["framebuffer-drawing", "rusttype"]
input-types = []
input = ["scan", "input-types", "evdev", "epoll", "fxhash"]
battery = []
appctx = ["framebuffer-text-drawing", "input", "aabb-quadtree"]
appctx = ["framebuffer-text-drawing", "input", "aabb-quadtree", "euclid", "smallvec"]

enable-runtime-benchmarking = ["stopwatch"]

Expand Down
58 changes: 25 additions & 33 deletions src/appctx.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
#[cfg(feature = "hlua")]
use std::cell::UnsafeCell;
use std::collections::HashMap;
#[cfg(not(feature = "hlua"))]
use std::marker::PhantomData;
use std::ops::DerefMut;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::RwLock;

use aabb_quadtree::{geom, ItemId, QuadTree};
#[cfg(feature = "hlua")]
use log::warn;
use aabb_quadtree::{ItemId, QuadTree};
use euclid::{Rect, TypedPoint2D, TypedRect, UnknownUnit};
use smallvec::Array;

use crate::framebuffer::cgmath;
use crate::framebuffer::common::*;
Expand Down Expand Up @@ -44,7 +42,7 @@ pub struct ApplicationContext<'a> {
#[cfg(feature = "hlua")]
lua: UnsafeCell<Lua<'a>>,
#[cfg(not(feature = "hlua"))]
lua: PhantomData<&'a ()>,
lua: std::marker::PhantomData<&'a ()>,

input_tx: std::sync::mpsc::Sender<InputEvent>,
input_rx: std::sync::mpsc::Receiver<InputEvent>,
Expand All @@ -53,7 +51,7 @@ pub struct ApplicationContext<'a> {
wacom_ctx: RwLock<Option<ev::EvDevContext>>,
touch_ctx: RwLock<Option<ev::EvDevContext>>,

active_regions: QuadTree<ActiveRegionHandler>,
active_regions: QuadTree<ActiveRegionHandler, f32, Array<Item = (ItemId, TypedRect<f32>)>>,
ui_elements: HashMap<String, UIElementHandle>,
}

Expand All @@ -80,13 +78,13 @@ impl Default for ApplicationContext<'static> {
input_rx,
input_tx,
ui_elements: HashMap::new(),
active_regions: QuadTree::default(geom::Rect::from_points(
&geom::Point { x: 0.0, y: 0.0 },
&geom::Point {
x: xres as f32,
y: yres as f32,
},
)),
active_regions: QuadTree::default(
TypedRect::from_points([
TypedPoint2D::new(0.0, 0.0),
TypedPoint2D::new(xres as f32, yres as f32),
]),
10, // size hint for underlying HashMap::with_capacity_and_hasher
),
};

// Enable all std lib
Expand Down Expand Up @@ -149,7 +147,7 @@ impl<'a> ApplicationContext<'a> {
pub fn execute_lua(&mut self, code: &str) {
let lua = self.get_lua_ref();
if let Err(e) = lua.execute::<hlua::AnyLuaValue>(code) {
warn!("Error in Lua Context: {:?}", e);
log::warn!("Error in Lua Context: {:?}", e);
}
}

Expand Down Expand Up @@ -534,14 +532,14 @@ impl<'a> ApplicationContext<'a> {
}

pub fn find_active_region(&self, y: u16, x: u16) -> Option<(&ActiveRegionHandler, ItemId)> {
let matches = self.active_regions.query(geom::Rect::centered_with_radius(
&geom::Point {
y: f32::from(y),
x: f32::from(x),
},
2.0,
));
matches.first().map(|res| (res.0, res.2))
let matches = self.active_regions.query({
let radius = 2.0;
let v = euclid::TypedVector2D::new(radius, radius);
let p = euclid::TypedPoint2D::new(f32::from(x), f32::from(y));
// A rect centered on (x,y) with radius 2
TypedRect::from_points([(p - v), (p + v)])
});
matches.first().map(|(region, _, item)| (region, item))
}

pub fn remove_active_region_at_point(&mut self, y: u16, x: u16) -> bool {
Expand All @@ -562,16 +560,10 @@ impl<'a> ApplicationContext<'a> {
) {
self.active_regions.insert_with_box(
ActiveRegionHandler { handler, element },
geom::Rect::from_points(
&geom::Point {
x: f32::from(x),
y: f32::from(y),
},
&geom::Point {
x: f32::from(x + width),
y: f32::from(y + height),
},
),
TypedRect::from_points([
TypedPoint2D::new(f32::from(x), f32::from(y)),
TypedPoint2D::new(f32::from(x + width), f32::from(y + height)),
]),
);
}
}

0 comments on commit 9ffa372

Please sign in to comment.