"""Negative controls for stage1_gen's overlap check: break the assembly on purpose
and see that the check notices.  Brim (an AI agent), wake 329."""
import sys, os, io, contextlib
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
with contextlib.redirect_stdout(io.StringIO()):
    import stage1_gen as g
A = g.assembly(0.0)
print("as designed:   idler x n1 %.3f   idler x compound %.3f   wheel x crank %.3f   compound x moongear %.3f" % (
    g.ivol(A["idler"], A["n1"]), g.ivol(A["idler"], A["compound"]), g.ivol(A["wheel"], A["crank"]), g.ivol(A["compound"], A["moongear"])))
# 1. the idler turned half a tooth: tooth on tooth
r_idl = g.mesh_rot((0, 0), g.L.N1, 0.0, g.L.IDL, g.L.NI)
bad = g.make_idler(r_idl + 180.0 / g.L.NI)
print("idler half a tooth out:  idler x n1 %.3f mm3 (must be > 0)" % g.ivol(bad, A["n1"]))
# 2. the crank turned half a tooth
cr = g.mesh_rot((0, 0), g.L.N_RIM, 0.0, g.CRANK_AXIS, g.L.N_PIN)
print("crank half a tooth out:  wheel x crank %.3f mm3 (must be > 0)" % g.ivol(A["wheel"], g.make_crank(cr + 180.0 / g.L.N_PIN)))
# 3. the Moon gear half a tooth out
B = g.assembly(0.0, 0.5)
print("moon gear half a tooth out:  compound x moongear %.3f mm3 (must be > 0)" % g.ivol(B["compound"], B["moongear"]))
# 4. backlash actually present: turn the idler a hair either way and see when it first touches
for d in (0.2, 0.5, 1.0, 1.5, 2.5):
    print("idler turned %.1f deg from centred: x n1 %.4f  x compound %.4f" % (d, g.ivol(g.make_idler(r_idl + d), A["n1"]), g.ivol(g.make_idler(r_idl + d), A["compound"])))
