main
John Lauer
BFK458-12: model, chip artwork, hero image and datasheet facts
9bc5382
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
"""Overlay proof: the model's orthographic silhouette (teal) blended over the datasheet drawing views.
usage: overlay.py <model.glb, dequantized> <drawing.png> <view: top|side> <px_per_mm> <cx> <cy> <out.png>
top: looking from +Z (the stator rear face), image x = model x, image y = -model y
side: section view, image x = model z (mounting face at cx), image y = -model y (axis at cy)
cx, cy are the drawing pixel where the model origin lands (axis centre, or mounting face on the axis).
"""
import sys, numpy as np, pygltflib, matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from matplotlib.collections import PolyCollection
from PIL import Image
glb, png, view, ppm, cx, cy, out = sys.argv[1:8]
ppm, cx, cy = float(ppm), float(cx), float(cy)
g = pygltflib.GLTF2().load(glb)
blob = g.binary_blob()
def acc(i):
a = g.accessors[i]; bv = g.bufferViews[a.bufferView]
n = {"SCALAR": 1, "VEC2": 2, "VEC3": 3, "VEC4": 4}[a.type]
dt = {5126: np.float32, 5125: np.uint32, 5123: np.uint16, 5121: np.uint8}[a.componentType]
off = (bv.byteOffset or 0) + (a.byteOffset or 0)
stride = bv.byteStride or n * np.dtype(dt).itemsize
raw = np.frombuffer(blob, dtype=np.uint8, count=stride * (a.count - 1) + n * np.dtype(dt).itemsize, offset=off)
arr = np.lib.stride_tricks.as_strided(raw.view(dt), shape=(a.count, n), strides=(stride, np.dtype(dt).itemsize)) if n > 1 else raw.view(dt)[: a.count]
return np.array(arr)
def mat(node):
m = np.eye(4)
if node.matrix: m = np.array(node.matrix).reshape(4, 4).T
else:
t = np.eye(4); r = np.eye(4); s = np.eye(4)
if node.translation: t[:3, 3] = node.translation
if node.rotation:
x, y, z, w = node.rotation
r[:3, :3] = [[1-2*(y*y+z*z), 2*(x*y-z*w), 2*(x*z+y*w)], [2*(x*y+z*w), 1-2*(x*x+z*z), 2*(y*z-x*w)], [2*(x*z-y*w), 2*(y*z+x*w), 1-2*(x*x+y*y)]]
if node.scale: s[:3, :3] = np.diag(node.scale)
m = t @ r @ s
return m
tris = []
def walk(ni, M):
n = g.nodes[ni]; M2 = M @ mat(n)
if n.mesh is not None:
for p in g.meshes[n.mesh].primitives:
v = acc(p.attributes.POSITION).astype(float)
v = (M2 @ np.c_[v, np.ones(len(v))].T).T[:, :3]
idx = acc(p.indices).reshape(-1, 3)
tris.append(v[idx])
for c in n.children or []: walk(c, M2)
for r in g.scenes[g.scene or 0].nodes: walk(r, np.eye(4))
T = np.concatenate(tris) * 1000.0 # metres -> mm
# world coordinates after node transforms are the STEP frame (Z-up)
X, Y, Z = T[..., 0], T[..., 1], T[..., 2] # step2glb keeps the STEP Z-up frame in its node transform
if view == "top":
P = np.stack([cx + X * ppm, cy - Y * ppm], -1)
else:
P = np.stack([cx + Z * ppm, cy - Y * ppm], -1)
img = Image.open(png).convert("RGB")
fig = plt.figure(figsize=(img.width / 100, img.height / 100), dpi=100)
ax = fig.add_axes([0, 0, 1, 1]); ax.imshow(img); ax.axis("off")
ax.add_collection(PolyCollection(P, facecolors=(0, 0.72, 0.69, 0.10), edgecolors="none"))
ax.set_xlim(0, img.width); ax.set_ylim(img.height, 0)
fig.savefig(out, dpi=100); print("OK", out, len(T), "triangles")