|
|
|
|
|
import json |
|
|
from math import floor |
|
|
|
|
|
class PixelRect: |
|
|
def __init__(self, minX: int, minY: int, width: int, height: int): |
|
|
self.min_x = floor(minX) |
|
|
self.min_y = floor(minY) |
|
|
self.max_x = floor(minX + width) |
|
|
self.max_y = floor(minY + height) |
|
|
|
|
|
|
|
|
width = int(width / 4) * 4 |
|
|
height = int(height / 4) * 4 |
|
|
|
|
|
@property |
|
|
def width(self): |
|
|
return self.max_x - self.min_x |
|
|
|
|
|
@property |
|
|
def height(self): |
|
|
return self.max_y - self.min_y |
|
|
|
|
|
def to_normalized_rect(self, width: int, height: int): |
|
|
return NormalizedRect(self.min_x / width, self.min_y / height, self.width / width, self.height / height) |
|
|
|
|
|
def String(self): |
|
|
return json.dumps(self, default=lambda o: o.__dict__, sort_keys=True) |
|
|
|
|
|
def Print(self, stream): |
|
|
stream.write(self.String()) |
|
|
|
|
|
class NormalizedRect: |
|
|
def __init__(self, minX: float, minY: float, width: float, height: float): |
|
|
self.min_x = minX |
|
|
self.min_y = minY |
|
|
|
|
|
width = max(width, 0) |
|
|
width = min(width, 1) |
|
|
self.max_x = self.min_x + width |
|
|
|
|
|
height = max(height, 0) |
|
|
height = min(height, 1) |
|
|
self.max_y = self.min_y + height |
|
|
|
|
|
def to_pixel_rect(self, width: int, height: int): |
|
|
xmin = self.min_x * width |
|
|
ymin = self.min_y * height |
|
|
width = self.width * width |
|
|
height = self.height * height |
|
|
|
|
|
return PixelRect(xmin, ymin, width, height) |
|
|
|
|
|
def union(self, x): |
|
|
if x.min_x >= 0: |
|
|
self.min_x = min(self.min_x, x.min_x) |
|
|
|
|
|
if x.min_y >= 0: |
|
|
self.min_y = min(self.min_y, x.min_y) |
|
|
|
|
|
if x.max_x <= 1: |
|
|
self.max_x = max(self.max_x, x.max_x) |
|
|
|
|
|
if x.max_y <= 1: |
|
|
self.max_y = max(self.max_y, x.max_y) |
|
|
|
|
|
def intersect(self, x): |
|
|
if x.min_x < 1: |
|
|
self.min_x = max(self.min_x, x.min_x) |
|
|
|
|
|
if x.min_y < 1: |
|
|
self.min_y = max(self.min_y, x.min_y) |
|
|
|
|
|
if x.max_x > 0: |
|
|
self.max_x = min(self.max_x, x.max_x) |
|
|
|
|
|
if x.max_y > 0: |
|
|
self.max_y = min(self.max_y, x.max_y) |
|
|
|
|
|
@property |
|
|
def width(self): |
|
|
return self.max_x - self.min_x |
|
|
|
|
|
@property |
|
|
def height(self): |
|
|
return self.max_y - self.min_y |
|
|
|
|
|
def String(self): |
|
|
return json.dumps(self, default=lambda o: o.__dict__, sort_keys=True) |
|
|
|
|
|
def Print(self, stream): |
|
|
stream.write(self.String()) |
|
|
|