-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbbox.py
110 lines (88 loc) · 3.43 KB
/
bbox.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import math
import mathutils
import bmesh
from typing import List
class BBoxUV():
'''Store bounds of uv loops'''
def __init__(self, loops:List[bmesh.types.BMLoop]=None, uv_layer:bmesh.types.BMLayerItem=None) -> None:
self.min = mathutils.Vector((math.inf, math.inf))
self.max = mathutils.Vector((-math.inf, -math.inf))
if loops != None and uv_layer != None:
self.update(loops, uv_layer)
@property
def topleft(self) -> mathutils.Vector:
return mathutils.Vector((self.min.x, self.max.y))
@property
def topright(self) -> mathutils.Vector:
return mathutils.Vector((self.max.x, self.max.y))
@property
def bottomleft(self) -> mathutils.Vector:
return mathutils.Vector((self.min.x, self.min.y))
@property
def bottomright(self) -> mathutils.Vector:
return mathutils.Vector((self.max.x, self.min.y))
@property
def left(self) -> mathutils.Vector:
return mathutils.Vector((self.min.x, (self.max.y + self.min.y) * 0.5))
@property
def right(self) -> mathutils.Vector:
return mathutils.Vector((self.max.x, (self.max.y + self.min.y) * 0.5))
@property
def top(self) -> mathutils.Vector:
return mathutils.Vector(((self.max.x + self.min.x) * 0.5, self.max.y))
@property
def bottom(self) -> mathutils.Vector:
return mathutils.Vector(((self.max.x + self.min.x) * 0.5, self.min.y))
@property
def diagonal(self) -> mathutils.Vector:
return self.max - self.min
@property
def average(self) -> mathutils.Vector:
return (self.min + self.max) * 0.5
@property
def center(self) -> mathutils.Vector:
return self.average
def get_location(self, direction):
if direction == 'left':
return self.left
elif direction == 'topleft':
return self.topleft
elif direction == 'top':
return self.top
elif direction == 'topright':
return self.topright
elif direction == 'right':
return self.right
elif direction == 'bottomright':
return self.bottomright
elif direction == 'bottom':
return self.bottom
elif direction == 'bottomleft':
return self.bottomleft
elif direction == 'center':
return self.center
elif direction == 'horizontal':
return self.center
elif direction == 'vertical':
return self.center
def set_to_unit_square(self):
self.min = mathutils.Vector((0, 0))
self.max = mathutils.Vector((1, 1))
def update(self, loops:List[bmesh.types.BMLoop], uv_layer:bmesh.types.BMLayerItem) -> None:
self.min = mathutils.Vector((math.inf, math.inf))
self.max = mathutils.Vector((-math.inf, -math.inf))
for loop in loops:
uv = loop[uv_layer].uv
if uv.x > self.max.x:
self.max.x = uv.x
if uv.y > self.max.y:
self.max.y = uv.y
if uv.y < self.min.y:
self.min.y = uv.y
if uv.x < self.min.x:
self.min.x = uv.x
def merge(self, other_bbox:"BBoxUV") -> None:
self.min.x = min(self.min.x, other_bbox.min.x)
self.min.y = min(self.min.y, other_bbox.min.y)
self.max.x = max(self.max.x, other_bbox.max.x)
self.max.y = max(self.max.y, other_bbox.max.y)