123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- # 一个简单的二维向量 Vector2
- class V2:
- def __init__(self, x, y):
- self.x = x
- self.y = y
- pass
- pass
- # 一个简单的范围碰撞计算类
- class R2:
- def __init__(self, minimum, maximum):
- self._data = (minimum, maximum)
- pass
- @property
- def min(self): return self._data[0]
- @property
- def max(self): return self._data[1]
- @property
- def length(self): return self._data[1] - self._data[0]
- def intersect(self, other):
- if self.min > other.max or self.max < other.min:
- return None
- minimum = self.min if self.min > other.min else other.min
- maximum = self.max if self.max < other.max else other.max
- return R2(minimum, maximum)
- pass
- class Bound:
- class CartesianLH:
- @staticmethod
- def top (r): return r.min
- @staticmethod
- def bottom(r): return r.max
- pass
- class CartesianRH:
- @staticmethod
- def top (r): return r.max
- @staticmethod
- def bottom(r): return r.min
- pass
- def __init__(self, x_min, x_max, y_min, y_max, cartesian):
- self._rx = R2(x_min, x_max)
- self._ry = R2(y_min, y_max)
- self._cartesian = cartesian
- pass
- @property
- def left (self): return self._rx.min
- @property
- def right (self): return self._rx.max
- @property
- def top (self): return self._cartesian.top (self._ry)
- @property
- def bottom(self): return self._cartesian.bottom(self._ry)
- @property
- def west (self): return self.left
- @property
- def east (self): return self.right
- @property
- def north(self): return self.top
- @property
- def south(self): return self.bottom
- @property
- def x(self): return self._rx.min
- @property
- def y(self): return self._ry.min
- @property
- def width (self): return self._rx.length
- @property
- def height(self): return self._ry.length
- @property
- def value(self):
- return self.west, self.south, self.east, self.north
- @property
- def coordinates(self):
- return \
- [
- [self.west, self.south],
- [self.east, self.south],
- [self.east, self.north],
- [self.west, self.north],
- [self.west, self.south]
- ]
- def intersect(self, other):
- other: Bound = other
- ix = self._rx.intersect(other._rx)
- iy = self._ry.intersect(other._ry)
- if ix is None or iy is None:
- return None
- return Bound(ix.min, ix.max, iy.min, iy.max, self._cartesian)
- @staticmethod
- def LH(left, top, right, bottom):
- return Bound(left, right, top, bottom, Bound.CartesianLH)
- @staticmethod
- def RH(left, bottom, right, top):
- return Bound(left, right, bottom, top, Bound.CartesianRH)
- @staticmethod
- def LH_XYWH(x, y, width, height):
- return Bound.LH(x, y, x + width, y + height)
- @staticmethod
- def RH_XYWH(x, y, width, height):
- return Bound.RH(x, y, x + width, y + height)
- @staticmethod
- def from_transform(width: int, height: int, transform):
- """
- 使用从 GDAL 中获取的信息构造边界对象
- :param width: 图像宽度
- :param height: 图像高度
- :param transform: 空间变换数据
- :return: 边界对象
- """
- rx = transform[1]
- ry = transform[5]
- west = transform[0]
- north = transform[3]
- east = west + width * rx
- south = north + height * ry
- return Bound.RH(west, south, east, north)
- pass
|