"""Build a compact articulated industrial pan-tilt inspection camera."""
import math, os, sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import v4lib as v4

v4.reset()
# Product finishes
coat = v4.painted("graphite_powder_coat", (0.105, 0.125, 0.145), roughness=0.28, coat=0.38)
cradle_paint = v4.painted("blue_gray_cradle_powdercoat", (0.19, 0.29, 0.32), roughness=0.36, coat=0.28)
neck_anod = v4.metal("dark_anodized_neck", (0.16, 0.20, 0.22), roughness=0.31, brushed=False)
light = v4.metal("satin_anodized_aluminum", (0.62, 0.67, 0.70), roughness=0.26, brushed=True)
darkmetal = v4.metal("blackened_steel", (0.12, 0.14, 0.16), roughness=0.32, brushed=False)
rubber = v4.rubber("black_seals", (0.025, 0.03, 0.032))
glass = v4.mat("AR_coated_optical_glass", (0.09, 0.20, 0.30), metallic=0.30, roughness=0.11, coat=0.85)
ledgreen = v4.emissive("status_green", (0.08, 0.85, 0.32), 2.2)
ledamber = v4.emissive("status_amber", (1.0, 0.34, 0.04), 1.8)
white = v4.mat("laser_marking", (0.72, 0.77, 0.78), roughness=0.55)
gasket = v4.rubber("pan_bearing_elastomer", (0.035, 0.043, 0.046), roughness=0.8)

base = v4.body("base")
neck = v4.body("neck_pivot")
cradle = v4.body("tilt_bracket")
sensor = v4.body("sensor_head")

# Stationary mounting flange and pan drive, bottom rests exactly on z=0.
v4.cylinder("mounting_flange", 0.095, 0.012, (0, 0, 0.006), bevel=0.002, material=light, body=base)
v4.cylinder("base_upper_step", 0.075, 0.010, (0, 0, 0.017), bevel=0.0015, material=coat, body=base)
v4.cylinder("pan_motor_can", 0.058, 0.056, (0, 0, 0.050), bevel=0.003, material=coat, body=base)
v4.cylinder("pan_motor_lower_band", 0.060, 0.008, (0, 0, 0.027), bevel=0.001, material=darkmetal, body=base)
v4.cylinder("pan_bearing_race", 0.064, 0.008, (0, 0, 0.082), bevel=0.001, material=darkmetal, body=base)
v4.cylinder("pan_bearing_lip", 0.056, 0.004, (0, 0, 0.088), bevel=0.0008, material=light, body=base)
# A black seal occupies the pan race seam; two glands and a service loop route the base wiring.
v4.torus("pan_bearing_gasket",0.055,0.0016,(0,0,0.090),material=gasket,body=base)
for sy in (-1,1):
    yy=sy*0.023
    v4.cylinder(f"slipring_gland_flange_{sy}",0.010,0.008,(0.055,yy,0.050),rot=(0,math.pi/2,0),bevel=0.0007,material=light,body=base)
    v4.cylinder(f"slipring_gland_nut_{sy}",0.0075,0.006,(0.061,yy,0.050),rot=(0,math.pi/2,0),bevel=0.0007,material=darkmetal,body=base)
    v4.torus(f"slipring_gland_seal_{sy}",0.0065,0.001,(0.064,yy,0.050),rot=(0,math.pi/2,0),material=rubber,body=base)
v4.tube("slipring_service_loop",[(0.064,-0.023,0.050),(0.069,-0.030,0.059),(0.072,-0.018,0.066),
                                  (0.073,0.000,0.068),(0.072,0.018,0.066),(0.069,0.030,0.059),(0.064,0.023,0.050)],
        0.0028,material=rubber,body=base,resolution=20)
# Four flange holes, with contrasting socket bottoms; spaced on a bolt circle.
for i in range(4):
    a = math.radians(45 + i * 90)
    x, y = 0.077 * math.cos(a), 0.077 * math.sin(a)
    v4.cylinder(f"mount_bolt_{i}", 0.0052, 0.002, (x, y, 0.013), material=darkmetal, body=base)
    v4.cylinder(f"mount_socket_{i}", 0.0022, 0.0007, (x, y, 0.0143), material=light, body=base)
# Small perimeter scallop marks / shallow-looking fasteners on base step.
for i in range(8):
    a = 2*math.pi*i/8
    x,y = 0.068*math.cos(a),0.068*math.sin(a)
    v4.cylinder(f"base_cap_screw_{i}", 0.0027, 0.0018, (x,y,0.022), material=light, body=base)

# Pan rotor and fork neck. Twin cheeks carry the tilt bearings at z=.235.
v4.cylinder("pan_rotor_boss", 0.053, 0.012, (0,0,0.096), bevel=0.001, material=darkmetal, body=neck)
v4.cylinder("neck_turntable", 0.071, 0.014, (0,0,0.106), bevel=0.002, material=neck_anod, body=neck)
v4.cylinder("neck_trim_ring", 0.073, 0.003, (0,0,0.114), bevel=0.0006, material=light, body=neck)
# Upright U-yoke cheeks: front/back direction is Y. Reinforced bases and sculpted shoulders.
for sx in (-1,1):
    x=sx*0.069
    v4.box(f"yoke_cheek_{sx}",(0.018,0.078,0.132),(x,0.008,0.177),bevel=0.008,material=neck_anod,body=neck)
    v4.box(f"yoke_foot_{sx}",(0.027,0.090,0.022),(x,0.006,0.127),bevel=0.004,material=light,body=neck)
    # Bearing cup points outward along X, through the cheek.
    v4.cylinder(f"tilt_bearing_outer_{sx}",0.022,0.012,(sx*0.084,0,0.235),rot=(0,math.pi/2,0),material=darkmetal,body=neck)
    v4.cylinder(f"tilt_bearing_face_{sx}",0.016,0.006,(sx*0.092,0,0.235),rot=(0,math.pi/2,0),material=light,body=neck)
    v4.cylinder(f"tilt_bearing_cap_{sx}",0.010,0.004,(sx*0.097,0,0.235),rot=(0,math.pi/2,0),material=coat,body=neck)
    v4.cylinder(f"bearing_center_screw_{sx}",0.003,0.0015,(sx*0.0998,0,0.235),rot=(0,math.pi/2,0),material=light,body=neck)
    v4.torus(f"bearing_cap_reveal_{sx}",0.0125,0.0008,(sx*0.0952,0,0.235),rot=(0,math.pi/2,0),material=darkmetal,body=neck)
    for j in range(4):
        a=2*math.pi*j/4
        yy=0.012*math.cos(a); zz=0.235+0.012*math.sin(a)
        v4.cylinder(f"bearing_face_bolt_{sx}_{j}",0.0018,0.002,(sx*0.096,yy,zz),rot=(0,math.pi/2,0),
                    bevel=0.00025,material=light,body=neck)
    v4.torus(f"trunnion_circlip_{sx}",0.006,0.0011,(sx*0.101,0,0.235),rot=(0,math.pi/2,0),material=darkmetal,body=cradle)
    v4.cylinder(f"trunnion_exposed_shaft_{sx}",0.0058,0.012,(sx*0.105,0,0.235),rot=(0,math.pi/2,0),
                bevel=0.0004,material=light,body=cradle)
    # Three small vertical cooling slots are represented by dark inset bars on rear cheek face.
    for k in range(3):
        z=0.161+k*0.017
        v4.box(f"yoke_vent_{sx}_{k}",(0.0015,0.030,0.003),(x+sx*0.0095,0.009,z),bevel=0.0012,material=darkmetal,body=neck)
# Rear tie web joins the two uprights below the tilt axle.
v4.box("yoke_rear_cross_web",(0.125,0.012,0.038),(0,0.041,0.155),bevel=0.004,material=coat,body=neck)

# Inner tilt cradle: two side arms rotate in the yoke bearings and support a forward-facing pod.
# Trunnion pins extend out of the cradle into the neck bearing cups.
for sx in (-1,1):
    v4.cylinder(f"cradle_trunnion_{sx}",0.008,0.070,(sx*0.066,0,0.235),rot=(0,math.pi/2,0),material=light,body=cradle)
    plate=v4.box(f"cradle_side_plate_{sx}",(0.008,0.132,0.112),(sx*0.058,-0.051,0.235),bevel=0.002,material=cradle_paint,body=cradle)
    for k, yy in enumerate((-0.078,-0.020)):
        cutter=v4.cylinder(f"lightening_cutter_{sx}_{k}",0.013,0.020,(sx*0.058,yy,0.235),
                           rot=(0,math.pi/2,0),bevel=0,material=None)
        v4.boolean(plate,cutter)
# A front lower bridge ties the arms and acts as a protective lens bumper.
v4.box("cradle_lower_bridge",(0.140,0.014,0.012),(0,-0.113,0.181),bevel=0.003,material=cradle_paint,body=cradle)
# Rear tie web carries the roll servo and ties both cradle cheeks together.
v4.box("cradle_rear_web",(0.120,0.014,0.034),(0,0.012,0.235),bevel=0.003,material=cradle_paint,body=cradle)
# Roll servo is mounted at the cradle rear; its output hub lies on the optical centerline.
v4.cylinder("roll_servo_case",0.020,0.022,(0,0.006,0.235),rot=(math.pi/2,0,0),material=coat,body=cradle)
v4.cylinder("roll_servo_output",0.012,0.015,(0,-0.010,0.235),rot=(math.pi/2,0,0),material=darkmetal,body=cradle)
v4.cylinder("roll_bearing_ring",0.018,0.004,(0,-0.006,0.235),rot=(math.pi/2,0,0),bevel=0.0005,material=light,body=cradle)
v4.torus("roll_bearing_seal",0.014,0.0012,(0,-0.009,0.235),rot=(math.pi/2,0,0),material=rubber,body=cradle)

# Sensor module body: compact rounded shell, lens points forward (-Y).
v4.cylinder("sensor_shell",0.049,0.096,(0,-0.056,0.235),rot=(math.pi/2,0,0),material=coat,body=sensor)
# Face escutcheon and optical stack along the viewing axis.
v4.cylinder("front_face_plate",0.044,0.004,(0,-0.106,0.235),rot=(math.pi/2,0,0),material=darkmetal,body=sensor)
# Lens barrel cylinders are oriented along Y and step out toward -Y.
v4.cylinder("lens_mount_ring",0.034,0.012,(0,-0.114,0.235),rot=(math.pi/2,0,0),material=darkmetal,body=sensor)
v4.cylinder("lens_bezel",0.028,0.004,(0,-0.1215,0.235),rot=(math.pi/2,0,0),material=light,body=sensor)
v4.cylinder("lens_retaining_ring",0.021,0.004,(0,-0.1255,0.235),rot=(math.pi/2,0,0),material=darkmetal,body=sensor)
v4.cylinder("lens_glass",0.018,0.002,(0,-0.1285,0.235),rot=(math.pi/2,0,0),material=glass,body=sensor)
# Small annular engraved details and lens reflections (two subtle concentric circles).
v4.torus("lens_inner_groove",0.014,0.0008,(0,-0.1297,0.235),rot=(math.pi/2,0,0),material=light,body=sensor)
# Status lenses on face, mounted flush to face plate.
for i, (x,matl) in enumerate(((-0.031,ledgreen),(0.031,ledamber))):
    v4.cylinder(f"front_status_led_{i}",0.0032,0.002,(x,-0.107,0.257),rot=(math.pi/2,0,0),material=matl,body=sensor)
# Side screw heads along shell; small cylinders lie tangent to left/right sides.
for sx in (-1,1):
    v4.cylinder(f"shell_side_screw_{sx}",0.0027,0.0016,(sx*0.0495,-0.056,0.235),rot=(0,math.pi/2,0),material=light,body=sensor)
# Heat-sink ribs on top, each seated on shell top.
for i in range(5):
    x=-0.030+i*0.015
    rib_z = 0.235 + math.sqrt(0.049**2 - x**2) + 0.002
    v4.box(f"top_cooling_fin_{i}",(0.005,0.053,0.004),(x,-0.055,rib_z),bevel=0.0015,material=light,body=sensor)
# Rear M12 style connector with a coupling rim, locking ring and five metal contacts.
v4.cylinder("rear_connector_flange",0.010,0.004,(0.025,-0.006,0.264),rot=(math.pi/2,0,0),material=light,body=sensor)
v4.torus("rear_connector_gasket",0.010,0.0008,(0.025,-0.007,0.264),rot=(math.pi/2,0,0),material=rubber,body=sensor)
v4.cylinder("rear_connector_body",0.007,0.010,(0.025,0.001,0.264),rot=(math.pi/2,0,0),material=darkmetal,body=sensor)
v4.torus("rear_connector_lock_ring",0.007,0.0012,(0.025,0.0045,0.264),rot=(math.pi/2,0,0),material=light,body=sensor)
for j,(px,pz) in enumerate(((0,0),(0.004,0),(-0.004,0),(0,0.004),(0,-0.004))):
    v4.cylinder(f"rear_connector_contact_{j}",0.0010,0.002,(0.025+0.003/0.004*px,0.007,0.264+0.003/0.004*pz),rot=(math.pi/2,0,0),
                bevel=0,material=light,body=sensor)
# Small model badge on top face (surface text helper snaps raised print to the shell).
v4.box("top_badge",(0.050,0.010,0.0015),(0,-0.055,0.2837),bevel=0.0008,material=darkmetal,body=sensor)
v4.text("model_marking","PT-3 AXIS",0.005,(0,-0.055,0.285),rot=(0,0,0),material=white,body=sensor)

# Joint pivots coincide with the visible bearing / trunnion centres.
v4.joint("pan_axis","revolute",base,neck,origin=(0,0,0.090),axis=(0,0,1),limits=(-math.radians(160),math.radians(160)))
v4.joint("tilt_axis","revolute",neck,cradle,origin=(0,0,0.235),axis=(1,0,0),limits=(-math.radians(25),math.radians(60)))
v4.joint("sensor_roll","revolute",cradle,sensor,origin=(0,0.006,0.235),axis=(0,-1,0),limits=(-math.radians(35),math.radians(35)))

v4.save(os.path.join(os.getcwd(),"asset.blend"))
print("saved asset.blend")
