Skip to content

Commit

Permalink
Benchmark work (#31)
Browse files Browse the repository at this point in the history
* Multiprocessing re-work for benchmark utility

* Update pyo3 and maturin

* Math fixes and some tests to check them

* Formatting
  • Loading branch information
Cleptomania authored Mar 1, 2024
1 parent 7ac9948 commit 1009f5d
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 33 deletions.
51 changes: 33 additions & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ crate-type = ["cdylib"]

[dependencies]
float_eq = "1"
pyo3 = "0.18.1"
pyo3 = "0.20.3"
rand = "0.8.5"
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[build-system]
requires = ["maturin>=0.14,<0.15"]
requires = ["maturin>=1.4.0,<1.5.0"]
build-backend = "maturin"

[project]
Expand Down
18 changes: 9 additions & 9 deletions python/arcade_accelerate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ def patch_hitboxes(patches):


def patch_spritelist_collision(patches):
patches[
"arcade.sprite_list.collision"
].check_for_collision_with_list = arcade_accelerate.check_for_collision_with_list
patches[
"arcade.sprite_list.collision"
].check_for_collision_with_lists = arcade_accelerate.check_for_collision_with_lists
patches["arcade.sprite_list.collision"].check_for_collision_with_list = (
arcade_accelerate.check_for_collision_with_list
)
patches["arcade.sprite_list.collision"].check_for_collision_with_lists = (
arcade_accelerate.check_for_collision_with_lists
)


def patch_math(patches):
patches["arcade.math"].rotate_point = arcade_accelerate.rotate_point


def patch_geometry(patches):
patches[
"arcade.geometry"
].are_polygons_intersecting = arcade_accelerate.are_polygons_intersecting
patches["arcade.geometry"].are_polygons_intersecting = (
arcade_accelerate.are_polygons_intersecting
)
12 changes: 12 additions & 0 deletions src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ use pyo3::prelude::*;

#[pyfunction]
pub fn are_polygons_intersecting(poly_a: Vec<(f32, f32)>, poly_b: Vec<(f32, f32)>) -> bool {
// If either polygon is empty, we should just return False
if poly_a.is_empty() || poly_b.is_empty() {
return false;
}
let polygons = [poly_a, poly_b];
for polygon in &polygons {
for i1 in 0..polygon.len() {
Expand Down Expand Up @@ -171,6 +175,14 @@ mod tests {
assert!(!result);
}

#[test]
fn test_empty_polygons_intersecting() {
let poly_a: Vec<(f32, f32)> = vec![];
let poly_b: Vec<(f32, f32)> = vec![];
let result = are_polygons_intersecting(poly_a, poly_b);
assert!(!result);
}

#[test]
fn test_is_point_in_box() {
// point inside
Expand Down
4 changes: 2 additions & 2 deletions src/hitbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ impl RotatableHitBox {
let rad_cos = rad.cos();
let rad_sin = rad.sin();
for point in super_.points.iter() {
let x = ((point.0 * rad_cos - point.1 * rad_sin) * super_.scale.0) + super_.position.0;
let y = ((point.0 * rad_sin + point.1 * rad_cos) * super_.scale.1) + super_.position.1;
let x = ((point.0 * rad_cos + point.1 * rad_sin) * super_.scale.0) + super_.position.0;
let y = ((-point.0 * rad_sin + point.1 * rad_cos) * super_.scale.1) + super_.position.1;
new_points.push((x, y));
}

Expand Down
57 changes: 55 additions & 2 deletions src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ pub fn rotate_point(x: f32, y: f32, cx: f32, cy: f32, angle: f32) -> (f32, f32)
let temp_y = y - cy;

// rotate point
let xnew = (temp_x * c - temp_y * s) + cx;
let ynew = (temp_x * s + temp_y * c) + cy;
let xnew = (temp_x * c + temp_y * s) + cx;
let ynew = (-temp_x * s + temp_y * c) + cy;

let precision = 10i32.pow(_PRECISION) as f32;
let x_rounded = (xnew * precision).round() / precision;
Expand Down Expand Up @@ -232,6 +232,11 @@ mod tests {
use super::*;
use float_eq::assert_float_eq;

fn round_float(n: f32, decimals: u32) -> f32 {
let nn = 10i32.pow(decimals) as f32;
(n * nn).round() / nn
}

#[test]
fn test_clamp() {
let mut result = clamp(1.2, 1.0, 2.0);
Expand Down Expand Up @@ -392,4 +397,52 @@ mod tests {
assert_eq!(result.x, 0.0);
assert_eq!(result.y, 0.0);
}

#[test]
fn test_rotate_point() {
let mut x: f32 = 0.0;
let mut y: f32 = 0.0;
let mut cx: f32 = 0.0;
let mut cy: f32 = 0.0;
let mut angle: f32 = 0.0;
let mut point: (f32, f32) = rotate_point(x, y, cx, cy, angle);
assert_float_eq!(round_float(point.0, 2), 0.0, abs <= 1.0e-3);
assert_float_eq!(round_float(point.1, 2), 0.0, abs <= 1.0e-3);

x = 0.0;
y = 0.0;
cx = 0.0;
cy = 0.0;
angle = 90.0;
point = rotate_point(x, y, cx, cy, angle);
assert_float_eq!(round_float(point.0, 2), 0.0, abs <= 1.0e-3);
assert_float_eq!(round_float(point.1, 2), 0.0, abs <= 1.0e-3);

x = 50.0;
y = 50.0;
cx = 0.0;
cy = 0.0;
angle = 0.0;
point = rotate_point(x, y, cx, cy, angle);
assert_float_eq!(round_float(point.0, 2), 50.0, abs <= 1.0e-3);
assert_float_eq!(round_float(point.1, 2), 50.0, abs <= 1.0e-3);

x = 50.0;
y = 0.0;
cx = 0.0;
cy = 0.0;
angle = 90.0;
point = rotate_point(x, y, cx, cy, angle);
assert_float_eq!(round_float(point.0, 2), 0.0, abs <= 1.0e-3);
assert_float_eq!(round_float(point.1, 2), -50.0, abs <= 1.0e-3);

x = 20.0;
y = 10.0;
cx = 10.0;
cy = 10.0;
angle = 180.0;
point = rotate_point(x, y, cx, cy, angle);
assert_float_eq!(round_float(point.0, 2), 0.0, abs <= 1.0e-3);
assert_float_eq!(round_float(point.1, 2), 10.0, abs <= 1.0e-3);
}
}

0 comments on commit 1009f5d

Please sign in to comment.