Skip to content

Commit 1009f5d

Browse files
authored
Benchmark work (#31)
* Multiprocessing re-work for benchmark utility * Update pyo3 and maturin * Math fixes and some tests to check them * Formatting
1 parent 7ac9948 commit 1009f5d

File tree

7 files changed

+113
-33
lines changed

7 files changed

+113
-33
lines changed

Cargo.lock

Lines changed: 33 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ crate-type = ["cdylib"]
1010

1111
[dependencies]
1212
float_eq = "1"
13-
pyo3 = "0.18.1"
13+
pyo3 = "0.20.3"
1414
rand = "0.8.5"

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[build-system]
2-
requires = ["maturin>=0.14,<0.15"]
2+
requires = ["maturin>=1.4.0,<1.5.0"]
33
build-backend = "maturin"
44

55
[project]

python/arcade_accelerate/__init__.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,19 @@ def patch_hitboxes(patches):
2424

2525

2626
def patch_spritelist_collision(patches):
27-
patches[
28-
"arcade.sprite_list.collision"
29-
].check_for_collision_with_list = arcade_accelerate.check_for_collision_with_list
30-
patches[
31-
"arcade.sprite_list.collision"
32-
].check_for_collision_with_lists = arcade_accelerate.check_for_collision_with_lists
27+
patches["arcade.sprite_list.collision"].check_for_collision_with_list = (
28+
arcade_accelerate.check_for_collision_with_list
29+
)
30+
patches["arcade.sprite_list.collision"].check_for_collision_with_lists = (
31+
arcade_accelerate.check_for_collision_with_lists
32+
)
3333

3434

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

3838

3939
def patch_geometry(patches):
40-
patches[
41-
"arcade.geometry"
42-
].are_polygons_intersecting = arcade_accelerate.are_polygons_intersecting
40+
patches["arcade.geometry"].are_polygons_intersecting = (
41+
arcade_accelerate.are_polygons_intersecting
42+
)

src/geometry.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ use pyo3::prelude::*;
22

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

178+
#[test]
179+
fn test_empty_polygons_intersecting() {
180+
let poly_a: Vec<(f32, f32)> = vec![];
181+
let poly_b: Vec<(f32, f32)> = vec![];
182+
let result = are_polygons_intersecting(poly_a, poly_b);
183+
assert!(!result);
184+
}
185+
174186
#[test]
175187
fn test_is_point_in_box() {
176188
// point inside

src/hitbox.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ impl RotatableHitBox {
158158
let rad_cos = rad.cos();
159159
let rad_sin = rad.sin();
160160
for point in super_.points.iter() {
161-
let x = ((point.0 * rad_cos - point.1 * rad_sin) * super_.scale.0) + super_.position.0;
162-
let y = ((point.0 * rad_sin + point.1 * rad_cos) * super_.scale.1) + super_.position.1;
161+
let x = ((point.0 * rad_cos + point.1 * rad_sin) * super_.scale.0) + super_.position.0;
162+
let y = ((-point.0 * rad_sin + point.1 * rad_cos) * super_.scale.1) + super_.position.1;
163163
new_points.push((x, y));
164164
}
165165

src/math.rs

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ pub fn rotate_point(x: f32, y: f32, cx: f32, cy: f32, angle: f32) -> (f32, f32)
1414
let temp_y = y - cy;
1515

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

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

235+
fn round_float(n: f32, decimals: u32) -> f32 {
236+
let nn = 10i32.pow(decimals) as f32;
237+
(n * nn).round() / nn
238+
}
239+
235240
#[test]
236241
fn test_clamp() {
237242
let mut result = clamp(1.2, 1.0, 2.0);
@@ -392,4 +397,52 @@ mod tests {
392397
assert_eq!(result.x, 0.0);
393398
assert_eq!(result.y, 0.0);
394399
}
400+
401+
#[test]
402+
fn test_rotate_point() {
403+
let mut x: f32 = 0.0;
404+
let mut y: f32 = 0.0;
405+
let mut cx: f32 = 0.0;
406+
let mut cy: f32 = 0.0;
407+
let mut angle: f32 = 0.0;
408+
let mut point: (f32, f32) = rotate_point(x, y, cx, cy, angle);
409+
assert_float_eq!(round_float(point.0, 2), 0.0, abs <= 1.0e-3);
410+
assert_float_eq!(round_float(point.1, 2), 0.0, abs <= 1.0e-3);
411+
412+
x = 0.0;
413+
y = 0.0;
414+
cx = 0.0;
415+
cy = 0.0;
416+
angle = 90.0;
417+
point = rotate_point(x, y, cx, cy, angle);
418+
assert_float_eq!(round_float(point.0, 2), 0.0, abs <= 1.0e-3);
419+
assert_float_eq!(round_float(point.1, 2), 0.0, abs <= 1.0e-3);
420+
421+
x = 50.0;
422+
y = 50.0;
423+
cx = 0.0;
424+
cy = 0.0;
425+
angle = 0.0;
426+
point = rotate_point(x, y, cx, cy, angle);
427+
assert_float_eq!(round_float(point.0, 2), 50.0, abs <= 1.0e-3);
428+
assert_float_eq!(round_float(point.1, 2), 50.0, abs <= 1.0e-3);
429+
430+
x = 50.0;
431+
y = 0.0;
432+
cx = 0.0;
433+
cy = 0.0;
434+
angle = 90.0;
435+
point = rotate_point(x, y, cx, cy, angle);
436+
assert_float_eq!(round_float(point.0, 2), 0.0, abs <= 1.0e-3);
437+
assert_float_eq!(round_float(point.1, 2), -50.0, abs <= 1.0e-3);
438+
439+
x = 20.0;
440+
y = 10.0;
441+
cx = 10.0;
442+
cy = 10.0;
443+
angle = 180.0;
444+
point = rotate_point(x, y, cx, cy, angle);
445+
assert_float_eq!(round_float(point.0, 2), 0.0, abs <= 1.0e-3);
446+
assert_float_eq!(round_float(point.1, 2), 10.0, abs <= 1.0e-3);
447+
}
395448
}

0 commit comments

Comments
 (0)