rig_picker/core/geometry_utils.py
2025-12-05 10:05:33 +01:00

66 lines
1.8 KiB
Python

import bpy
from mathutils import Vector
from mathutils.geometry import intersect_line_line_2d
def is_over_region(self, context, event):
inside = (
2 < event.mouse_region_x < context.region.width - 2
and 2 < event.mouse_region_y < context.region.height - 2
and [a for a in context.screen.areas if a.as_pointer() == self.adress]
and not context.screen.show_fullscreen
)
return inside
def bound_box_center(ob):
points = [ob.matrix_world @ Vector(p) for p in ob.bound_box]
x = [v[0] for v in points]
y = [v[1] for v in points]
z = [v[2] for v in points]
return (sum(x) / len(points), sum(y) / len(points), sum(z) / len(points))
def bounding_rect(points):
x_points = sorted(p[0] for p in points)
y_points = sorted(p[1] for p in points)
return [
Vector((x_points[0], y_points[-1])),
Vector((x_points[-1], y_points[-1])),
Vector((x_points[-1], y_points[0])),
Vector((x_points[0], y_points[0])),
]
def intersect_rects(rect, border): # returns None if rectangles don't intersect
dx = min(border[1][0], rect[1][0]) - max(border[0][0], rect[0][0])
dy = min(border[0][1], rect[0][1]) - max(border[2][1], rect[2][1])
if (dx >= 0) and (dy >= 0):
return dx * dy
def point_inside_rect(point, rect):
return rect[0][0] < point[0] < rect[1][0] and rect[2][1] < point[1] < rect[0][1]
def point_over_shape(point, verts, loops, outside_point=(-1, -1)):
out = Vector(outside_point)
pt = Vector(point)
intersections = 0
for loop in loops:
for i, p in enumerate(loop):
a = Vector(verts[loop[i - 1]])
b = Vector(verts[p])
if intersect_line_line_2d(pt, out, a, b):
intersections += 1
if intersections % 2 == 1: # chek if the nb of intersection is odd
return True