Migrated "polygon" code to shapely

This commit is contained in:
Khopa 2020-12-12 02:31:43 +01:00
parent 7c52ca15f3
commit feed55186f
12 changed files with 21 additions and 26 deletions

View File

@ -2,8 +2,10 @@ import pickle
from typing import Collection, Optional, Tuple from typing import Collection, Optional, Tuple
import logging import logging
from shapely import geometry
Zone = Collection[Tuple[float, float]] Zone = Collection[Tuple[float, float]]
Landmap = Tuple[Collection[Zone], Collection[Zone], Collection[Zone]] Landmap = Tuple[Collection[geometry.Polygon], Collection[geometry.Polygon], Collection[geometry.Polygon]]
def load_landmap(filename: str) -> Optional[Landmap]: def load_landmap(filename: str) -> Optional[Landmap]:
@ -15,22 +17,9 @@ def load_landmap(filename: str) -> Optional[Landmap]:
return None return None
def poly_contains(x, y, poly): def poly_contains(x, y, poly:geometry.Polygon):
n = len(poly) return poly.contains(geometry.Point(x, y))
inside = False
xints = 0.0
p1x, p1y = poly[0]
for i in range(n+1):
p2x, p2y = poly[i % n]
if y > min(p1y, p2y):
if y <= max(p1y, p2y):
if x <= max(p1x, p2x):
if p1y != p2y:
xints = (y-p1y)*(p2x-p1x)/(p2y-p1y)+p1x
if p1x == p2x or x <= xints:
inside = not inside
p1x, p1y = p2x, p2y
return inside
def poly_centroid(poly) -> Tuple[float, float]: def poly_centroid(poly) -> Tuple[float, float]:
x_list = [vertex[0] for vertex in poly] x_list = [vertex[0] for vertex in poly]

View File

@ -10,3 +10,6 @@ ignore_missing_imports = True
[mypy-winreg.*] [mypy-winreg.*]
ignore_missing_imports = True ignore_missing_imports = True
[mypy-shapely.*]
ignore_missing_imports = True

View File

@ -20,8 +20,9 @@ class DisplayRule:
def value(self, value: bool) -> None: def value(self, value: bool) -> None:
from qt_ui.widgets.map.QLiberationMap import QLiberationMap from qt_ui.widgets.map.QLiberationMap import QLiberationMap
self._value = value self._value = value
QLiberationMap.instance.reload_scene() if QLiberationMap.instance is not None:
QLiberationMap.instance.update() QLiberationMap.instance.reload_scene()
QLiberationMap.instance.update()
def __bool__(self) -> bool: def __bool__(self) -> bool:
return self.value return self.value

View File

@ -744,7 +744,7 @@ class QLiberationMap(QGraphicsView):
for sea_zone in self.game.theater.landmap[2]: for sea_zone in self.game.theater.landmap[2]:
print(sea_zone) print(sea_zone)
poly = QPolygonF([QPointF(*self._transform_point(Point(point[0], point[1]))) for point in sea_zone]) poly = QPolygonF([QPointF(*self._transform_point(Point(point[0], point[1]))) for point in sea_zone.exterior.coords])
if self.reference_point_setup_mode: if self.reference_point_setup_mode:
color = "sea_blue_transparent" color = "sea_blue_transparent"
else: else:
@ -752,14 +752,14 @@ class QLiberationMap(QGraphicsView):
scene.addPolygon(poly, CONST.COLORS[color], CONST.COLORS[color]) scene.addPolygon(poly, CONST.COLORS[color], CONST.COLORS[color])
for inclusion_zone in self.game.theater.landmap[0]: for inclusion_zone in self.game.theater.landmap[0]:
poly = QPolygonF([QPointF(*self._transform_point(Point(point[0], point[1]))) for point in inclusion_zone]) poly = QPolygonF([QPointF(*self._transform_point(Point(point[0], point[1]))) for point in inclusion_zone.exterior.coords])
if self.reference_point_setup_mode: if self.reference_point_setup_mode:
scene.addPolygon(poly, CONST.COLORS["grey_transparent"], CONST.COLORS["dark_grey_transparent"]) scene.addPolygon(poly, CONST.COLORS["grey_transparent"], CONST.COLORS["dark_grey_transparent"])
else: else:
scene.addPolygon(poly, CONST.COLORS["grey"], CONST.COLORS["dark_grey"]) scene.addPolygon(poly, CONST.COLORS["grey"], CONST.COLORS["dark_grey"])
for exclusion_zone in self.game.theater.landmap[1]: for exclusion_zone in self.game.theater.landmap[1]:
poly = QPolygonF([QPointF(*self._transform_point(Point(point[0], point[1]))) for point in exclusion_zone]) poly = QPolygonF([QPointF(*self._transform_point(Point(point[0], point[1]))) for point in exclusion_zone.exterior.coords])
if self.reference_point_setup_mode: if self.reference_point_setup_mode:
scene.addPolygon(poly, CONST.COLORS["grey_transparent"], CONST.COLORS["dark_dark_grey_transparent"]) scene.addPolygon(poly, CONST.COLORS["grey_transparent"], CONST.COLORS["dark_dark_grey_transparent"])
else: else:

View File

@ -8,3 +8,4 @@ tabulate~=0.8.7
mypy==0.782 mypy==0.782
mypy-extensions==0.4.3 mypy-extensions==0.4.3
jinja2>=2.11.2 jinja2>=2.11.2
shapely==1.7.1

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,6 +1,7 @@
import pickle import pickle
from dcs.mission import Mission from dcs.mission import Mission
from shapely import geometry
for terrain in ["cau", "nev", "syria", "channel", "normandy", "gulf"]: for terrain in ["cau", "nev", "syria", "channel", "normandy", "gulf"]:
print("Terrain " + terrain) print("Terrain " + terrain)
@ -15,16 +16,16 @@ for terrain in ["cau", "nev", "syria", "channel", "normandy", "gulf"]:
if terrain == "cau" and inclusion_zones: if terrain == "cau" and inclusion_zones:
# legacy # legacy
exclusion_zones.append(zone) exclusion_zones.append(geometry.Polygon(zone))
else: else:
if plane_group.units[0].type == "F-15C": if plane_group.units[0].type == "F-15C":
exclusion_zones.append(zone) exclusion_zones.append(geometry.Polygon(zone))
else: else:
inclusion_zones.append(zone) inclusion_zones.append(geometry.Polygon(zone))
for ship_group in m.country("USA").ship_group: for ship_group in m.country("USA").ship_group:
zone = [(x.position.x, x.position.y) for x in ship_group.points] zone = [(x.position.x, x.position.y) for x in ship_group.points]
seas_zones.append(zone) seas_zones.append(geometry.Polygon(zone))
with open("../{}landmap.p".format(terrain), "wb") as f: with open("../{}landmap.p".format(terrain), "wb") as f:
print(len(inclusion_zones), len(exclusion_zones), len(seas_zones)) print(len(inclusion_zones), len(exclusion_zones), len(seas_zones))