"""Render true STEP orthographic front/side reveals; rear walls appear recessed."""
from pathlib import Path
import numpy as np
from PIL import Image, ImageDraw
from relief_compare import read_step, draw_height
import sys

root=Path(__file__).resolve().parent.parent
p=(root/'QA'/'Openwork'); p.mkdir(exist_ok=True)
specs={
 'front':dict(axes=(0,2,1),sign=-1,u=(-53,53),v=(-3,103),size=(424,424)),
 'side':dict(axes=(1,2,0),sign=1,u=(-3,253),v=(-3,103),size=(1024,424)),
}
for key, title in (('BO','B  DOUBLE-KUI'),('DO','D  SANXINGDUI PERCHED BIRD')):
 if len(sys.argv)>1 and key not in sys.argv[1:]: continue
 xyz,tri=read_step(root/'STEP'/f'{key}_Pattern_Openwork_Snack_Bin.step')
 ims=[]
 for plane,spec in specs.items():
  h=draw_height((xyz,tri),spec)
  # Near planar wall and border use strong contrast against distant geometry.
  wall=np.isfinite(h)&(h>=(-2.24 if plane=='front' else 48.65))
  solid=np.isfinite(h)
  rgb=np.empty((*h.shape,3),dtype=np.uint8);rgb[:]=(250,247,236)
  rgb[solid]=(218,215,192);rgb[wall]=(73,101,80) if key=='BO' else (96,112,77)
  relief=wall&(h>(-.12 if plane=='front' else 49.63))
  rgb[relief]=(115,137,102)
  ims.append(Image.fromarray(rgb))
 card=Image.new('RGB',(1484,476),(250,247,236))
 d=ImageDraw.Draw(card)
 d.text((12,9),title+'   /   FRONT',font_size=19,fill=(38,49,37))
 d.text((448,9),'SIDE',font_size=19,fill=(38,49,37))
 card.paste(ims[0],(12,40));card.paste(ims[1],(448,40))
 card.save(p/f'{key}.png',optimize=True)
