From 30e72ba4aa387d63d8a8a3039653656371ac1e7d Mon Sep 17 00:00:00 2001 From: Darren Eberly Date: Sat, 18 Mar 2023 20:00:52 -0400 Subject: [PATCH 1/2] Complete Removal of Shapely --- arcade/examples/platform_tutorial/17_views.py | 5 ++ .../geometry_python.py => geometry.py} | 1 + arcade/geometry/__init__.py | 39 -------------- arcade/geometry/geometry_shapely.py | 52 ------------------- arcade/paths.py | 52 ++++++++++++++----- arcade/paths_python.py | 40 -------------- arcade/paths_shapely.py | 31 ----------- pyproject.toml | 9 ---- util/update_quick_index.py | 20 ++----- 9 files changed, 49 insertions(+), 200 deletions(-) rename arcade/{geometry/geometry_python.py => geometry.py} (99%) delete mode 100644 arcade/geometry/__init__.py delete mode 100644 arcade/geometry/geometry_shapely.py delete mode 100644 arcade/paths_python.py delete mode 100644 arcade/paths_shapely.py diff --git a/arcade/examples/platform_tutorial/17_views.py b/arcade/examples/platform_tutorial/17_views.py index 743eeb75c..1571c215d 100644 --- a/arcade/examples/platform_tutorial/17_views.py +++ b/arcade/examples/platform_tutorial/17_views.py @@ -5,6 +5,10 @@ """ import math +import arcade_accelerate + +arcade_accelerate.bootstrap() + import arcade # Constants @@ -689,3 +693,4 @@ def main(): if __name__ == "__main__": main() main() + main() diff --git a/arcade/geometry/geometry_python.py b/arcade/geometry.py similarity index 99% rename from arcade/geometry/geometry_python.py rename to arcade/geometry.py index aff1e3253..30a4c7818 100644 --- a/arcade/geometry/geometry_python.py +++ b/arcade/geometry.py @@ -193,3 +193,4 @@ def is_point_in_polygon(x: float, y: float, polygon: PointList) -> bool: # Return true if count is odd, false otherwise return count % 2 == 1 + \ No newline at end of file diff --git a/arcade/geometry/__init__.py b/arcade/geometry/__init__.py deleted file mode 100644 index 7f7fca78c..000000000 --- a/arcade/geometry/__init__.py +++ /dev/null @@ -1,39 +0,0 @@ -""" -The geometry module contains functions for checking collisions with geometry. - -Other simpler math related functions are in the :py:mod:`arcade.math` module. -""" -from .geometry_python import ( - get_triangle_orientation, - are_lines_intersecting, - is_point_in_box, -) - -# Shapely is disabled due to https://github.com/pythonarcade/arcade/pull/1535 -# We are leaving the code in-place to make it easier for someone to revisit -# in the future and improve shapely's performance. -use_shapely = False -# try: -# import shapely # noqa: F401 -# use_shapely = True -# except ImportError: -# use_shapely = False -if use_shapely: - from .geometry_shapely import ( - are_polygons_intersecting, - is_point_in_polygon, - ) -else: - from .geometry_python import ( - are_polygons_intersecting, - is_point_in_polygon, - ) - -__all__ = [ - "are_polygons_intersecting", - "is_point_in_polygon", - "get_triangle_orientation", - "are_lines_intersecting", - "is_point_in_box", - "shapely_installed", -] diff --git a/arcade/geometry/geometry_shapely.py b/arcade/geometry/geometry_shapely.py deleted file mode 100644 index ff1557c19..000000000 --- a/arcade/geometry/geometry_shapely.py +++ /dev/null @@ -1,52 +0,0 @@ -""" -Functions for checking collisions with geometry. - -These are the shapely versions of the functions. -""" -from shapely.geometry import Polygon, Point # type: ignore -# The speedups module was deprecated in Shapely 2.0 and will be removed in the future -try: - from shapely import speedups # type: ignore - speedups.enable() -except ImportError: - pass - -from arcade.types import PointList - - -def are_polygons_intersecting(poly_a: PointList, poly_b: PointList) -> bool: - """ - Return True if two polygons intersect. - - The polygons can have 3 or more points and can be concave or convex. - - :param PointList poly_a: List of points that define the first polygon. - :param PointList poly_b: List of points that define the second polygon. - :Returns: True or false depending if polygons intersect - - :rtype bool: - """ - shapely_polygon_a = Polygon(poly_a) - shapely_polygon_b = Polygon(poly_b) - - r2 = False - r1 = shapely_polygon_a.intersects(shapely_polygon_b) - if r1: - r2 = shapely_polygon_a.touches(shapely_polygon_b) - - return r1 and not r2 - - -def is_point_in_polygon(x: float, y: float, polygon: PointList) -> bool: - """ - Use ray-tracing to see if point is inside a polygon - - :param float x: X coordinate of point - :param float y: Y coordinate of point - :param PointList polygon_point_list: List of points that define the polygon. - :Returns: True or false depending if point is in polygon - """ - shapely_point = Point(x, y) - shapely_polygon = Polygon(polygon) - - return shapely_polygon.contains(shapely_point) diff --git a/arcade/paths.py b/arcade/paths.py index e441dba38..56c976506 100644 --- a/arcade/paths.py +++ b/arcade/paths.py @@ -1,19 +1,13 @@ """ Classic A-star algorithm for path finding. """ +from typing import List, Optional, Set, Tuple, Union + +from arcade import (Sprite, SpriteList, check_for_collision_with_list, + get_sprites_at_point) +from arcade.math import get_distance, lerp_vec from arcade.types import Point -from arcade import check_for_collision_with_list, SpriteList, Sprite -from typing import Union, List, Tuple, Set, Optional - -try: - import shapely # noqa: F401 - use_shapely = True -except ImportError: - use_shapely = False -if use_shapely: - from .paths_shapely import has_line_of_sight # noqa: F401 -else: - from .paths_python import has_line_of_sight # noqa: F401 + def _spot_is_blocked(position: Point, moving_sprite: Sprite, @@ -262,6 +256,39 @@ def astar_calculate_path(start_point: Point, return revised_result +def has_line_of_sight(point_1: Point, + point_2: Point, + walls: SpriteList, + max_distance: int = -1, + check_resolution: int = 2) -> bool: + """ + Determine if we have line of sight between two points. Try to make sure + that spatial hashing is enabled on the wall SpriteList or this will be + very slow. + + :param Point point_1: Start position + :param Point point_2: End position position + :param SpriteList walls: List of all blocking sprites + :param int max_distance: Max distance point 1 can see + :param int check_resolution: Check every x pixels for a sprite. Trade-off + between accuracy and speed. + """ + distance = get_distance(point_1[0], point_1[1], + point_2[0], point_2[1]) + steps = int(distance // check_resolution) + for step in range(steps + 1): + step_distance = step * check_resolution + u = step_distance / distance + midpoint = lerp_vec(point_1, point_2, u) + if max_distance != -1 and step_distance > max_distance: + return False + # print(point_1, point_2, step, u, step_distance, midpoint) + sprite_list = get_sprites_at_point(midpoint, walls) + if len(sprite_list) > 0: + return False + return True + + # NOTE: Rewrite this # def dda_step(start: Point, end: Point): # """ @@ -292,3 +319,4 @@ def astar_calculate_path(start_point: Point, # y += y_inc # return points +# return points diff --git a/arcade/paths_python.py b/arcade/paths_python.py deleted file mode 100644 index 8e1868cf7..000000000 --- a/arcade/paths_python.py +++ /dev/null @@ -1,40 +0,0 @@ -from arcade import ( - SpriteList, - get_sprites_at_point, -) -from arcade.math import get_distance -from arcade.types import Point -from arcade.math import lerp_vec - - -def has_line_of_sight(point_1: Point, - point_2: Point, - walls: SpriteList, - max_distance: int = -1, - check_resolution: int = 2) -> bool: - """ - Determine if we have line of sight between two points. Try to make sure - that spatial hashing is enabled on the wall SpriteList or this will be - very slow. - - :param Point point_1: Start position - :param Point point_2: End position position - :param SpriteList walls: List of all blocking sprites - :param int max_distance: Max distance point 1 can see - :param int check_resolution: Check every x pixels for a sprite. Trade-off - between accuracy and speed. - """ - distance = get_distance(point_1[0], point_1[1], - point_2[0], point_2[1]) - steps = int(distance // check_resolution) - for step in range(steps + 1): - step_distance = step * check_resolution - u = step_distance / distance - midpoint = lerp_vec(point_1, point_2, u) - if max_distance != -1 and step_distance > max_distance: - return False - # print(point_1, point_2, step, u, step_distance, midpoint) - sprite_list = get_sprites_at_point(midpoint, walls) - if len(sprite_list) > 0: - return False - return True diff --git a/arcade/paths_shapely.py b/arcade/paths_shapely.py deleted file mode 100644 index 61e92cf44..000000000 --- a/arcade/paths_shapely.py +++ /dev/null @@ -1,31 +0,0 @@ -from arcade import SpriteList - -from shapely import speedups # type: ignore -from shapely.geometry import Polygon, Point # type: ignore -from shapely.geometry import LineString, Polygon # type: ignore -speedups.enable() - - -def has_line_of_sight(point_1: Point, - point_2: Point, - walls: SpriteList, - max_distance: int = -1) -> bool: - """ - Determine if we have line of sight between two points. Having a line of - sight means, that you can connect both points with straight line without - intersecting any obstacle. - Thanks to the shapely efficiency and speedups boost, this function is very - fast. It can easily test 10 000 lines_of_sight. - - :param point_1: tuple -- coordinates of first position (x, y) - :param point_2: tuple -- coordinates of second position (x, y) - :param walls: list -- Obstacle objects to check against - :param max_distance: int -- - :return: tuple -- (bool, list) - """ - line_of_sight = LineString([point_1, point_2]) - if 0 < max_distance < line_of_sight.length: - return False - if not walls: - return True - return not any((Polygon(o.get_adjusted_hit_box()).crosses(line_of_sight) for o in walls)) diff --git a/pyproject.toml b/pyproject.toml index 0620d73fa..b212b4903 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -126,15 +126,6 @@ ignore_missing_imports = true module = "pytiled_parser.*" ignore_missing_imports = true -[[tool.mypy.overrides]] -module = "shapely.*" -ignore_missing_imports = true - [[tool.mypy.overrides]] module = "PyInstaller.*" ignore_missing_imports = true - - - - - diff --git a/util/update_quick_index.py b/util/update_quick_index.py index ebaa0ed9a..e472689ec 100644 --- a/util/update_quick_index.py +++ b/util/update_quick_index.py @@ -1,8 +1,8 @@ """ Script used to create the quick index """ -import re import os +import re from pathlib import Path # The project root @@ -15,16 +15,11 @@ 'context.py': ['OpenGL Context', 'open_gl.rst'], 'drawing_support.py': ['Drawing - Utility', 'drawing_utilities.rst'], 'draw_commands.py': ['Drawing - Primitives', 'drawing_primitives.rst'], - 'geometry/__init__.py': ['Geometry Support', 'geometry.rst'], - 'geometry/geometry_generic.py': ['Geometry Support', 'geometry.rst'], - 'geometry/geometry_shapely.py': ['Geometry Support', 'geometry.rst'], - 'hitbox.py': ['Geometry Support', 'geometry.rst'], + 'geometry.py': ['Geometry Support', 'geometry.rst'], 'isometric.py': ['Isometric Map Support (incomplete)', 'isometric.rst'], 'controller.py': ['Game Controller Support', 'game_controller.rst'], 'joysticks.py': ['Joystick Support', 'joysticks.rst'], 'paths.py': ['Pathfinding', 'path_finding.rst'], - 'paths_python.py': ['Pathfinding', 'path_finding.rst'], - 'paths_shapely.py': ['Pathfinding', 'path_finding.rst'], 'perf_info.py': ['Performance Information', 'perf_info.rst'], 'perf_graph.py': ['Performance Information', 'perf_info.rst'], 'physics_engines.py': ['Physics Engines', 'physics_engines.rst'], @@ -175,15 +170,6 @@ def process_directory(directory: Path, quick_index_file): if "test" in path.name: continue - if "geometry_python.py" in path.name: - continue - - if "geometry.py" in path.name: - continue - - if "paths_python.py" in path.name: - continue - if not path.exists(): print(f"Error, can't find file: '{path.name}'") continue @@ -198,12 +184,12 @@ def process_directory(directory: Path, quick_index_file): "texture": "arcade", "texture_atlas": "arcade", "sprite_list": "arcade", - "geometry": "geometry", "text": "arcade", "gui": "arcade.gui", "property": "arcade.gui.property", "widgets": "arcade.gui", "tilemap": "arcade.tilemap", + "geometry.py": "arcade.geometry", "transforms.py": "arcade.texture.transforms", "isometric.py": "arcade.isometric", "particles": "arcade.particles", From 04be7e1d3ae1a88468a02a67cf189f1e07c134bd Mon Sep 17 00:00:00 2001 From: Darren Eberly Date: Sat, 18 Mar 2023 20:03:04 -0400 Subject: [PATCH 2/2] Removal accidental inclusion of arcade-accelerate --- arcade/examples/platform_tutorial/17_views.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/arcade/examples/platform_tutorial/17_views.py b/arcade/examples/platform_tutorial/17_views.py index 1571c215d..3ded800c0 100644 --- a/arcade/examples/platform_tutorial/17_views.py +++ b/arcade/examples/platform_tutorial/17_views.py @@ -5,10 +5,6 @@ """ import math -import arcade_accelerate - -arcade_accelerate.bootstrap() - import arcade # Constants