"""Run with Python 3 inside either unpacked source folder beside frames.json.""" from pathlib import Path import json, math def sub(a, b): return tuple(x - y for x, y in zip(a, b)) def dot(a, b): return sum(x * y for x, y in zip(a, b)) def unit(a): length = math.sqrt(dot(a, a)) if length < 1e-9: raise ValueError("Zero-length direction") return tuple(x / length for x in a) def cross(a, b): x, y, z = a X, Y, Z = b return y*Z-z*Y, z*X-x*Z, x*Y-y*X def frame(die, face): ring = [die["vertices"][i] for i in face["vertices"]] c = tuple(sum(axis)/len(ring) for axis in zip(*ring)) a, b, d = ring[:3] n = unit(cross(sub(b, a), sub(d, a))) if dot(n, c) <= 0: raise ValueError("Check winding and the centred origin") right = unit(cross(face["up"], n)) up = cross(n, right) return c, right, up, n def place(basis, x, y, z): return tuple(c+x*r+y*u+z*n for c,r,u,n in zip(*basis)) library = json.loads(Path("frames.json").read_text()) for name, die in library.items(): for face in die["faces"]: basis = frame(die, face) c, right, up, n = basis assert abs(dot(cross(right, up), n)-1) < 1e-7 assert all(abs(dot(sub(die["vertices"][i], c), n)) < 1e-4 for i in face["vertices"]) print(name, len(die["faces"]), "planar faces") # One test point 0.2 mm below the first face in the D8 record. die = library["D8"] point = place(frame(die, die["faces"][0]), 0, 0, -0.2) print(tuple(round(x, 4) for x in point))