upd xlib.image.sd

This commit is contained in:
iperov 2021-11-11 23:13:26 +04:00
parent d02f46dfc7
commit 6b6b6b2d16
3 changed files with 245 additions and 0 deletions

25
xlib/image/sd/calc.py Normal file
View file

@ -0,0 +1,25 @@
import numpy as np
import numpy.linalg as npla
def dist_to_edges(pts, pt, is_closed=False):
"""
returns array of dist from pt to edge and projection pt to edges
"""
if is_closed:
a = pts
b = np.concatenate( (pts[1:,:], pts[0:1,:]), axis=0 )
else:
a = pts[:-1,:]
b = pts[1:,:]
pa = pt-a
ba = b-a
div = np.einsum('ij,ij->i', ba, ba)
div[div==0]=1
h = np.clip( np.einsum('ij,ij->i', pa, ba) / div, 0, 1 )
x = npla.norm ( pa - ba*h[...,None], axis=1 )
return x, a+ba*h[...,None]