folio-fold / evaluate_points.py
foliokestrel's picture
Check PAGE line outlines while moving the cut in the offline batch reviewer, version 0.4.2
f65b994 verified
Raw History Blame Contribute Delete
760 Bytes
"""Read normalized baseline or contour coordinates from PAGE XML."""
import math
def points(node,width,height,polygon):
if node is None:
return "missing",()
try:
pts=[tuple(map(float,p.split(","))) for p in node.get("points","").split()]
if any(len(p)!=2 or not all(map(math.isfinite,p)) for p in pts):
return "invalid",()
except ValueError:
return "invalid",()
if any(not (0<=x<=width and 0<=y<=height) for x,y in pts):
return "outside",()
if len(set(pts))<(3 if polygon else 2):
return "degenerate",()
if polygon and sum(a[0]*b[1]-a[1]*b[0] for a,b in zip(pts,pts[1:]+pts[:1]))==0:
return "zero_area",()
return "usable",tuple((x/width,y/height) for x,y in pts)