"""Detailed portable adjustable basketball hoop, authored as three rigid bodies."""
import math, os, sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import v4lib as v4

v4.reset()
# Materials
base_mat = v4.plastic("molded_charcoal_poly", (0.055, 0.067, 0.075), roughness=0.37, texture=0.10)
base_edge = v4.plastic("base_edge", (0.085, 0.098, 0.105), roughness=0.4, texture=0.06)
steel = v4.metal("graphite_powdercoat", color=(0.105, 0.125, 0.14), roughness=0.32, brushed=False)
steel_hi = v4.metal("satin_zinc_steel", color=(0.57, 0.61, 0.63), roughness=0.27, brushed=True)
black = v4.rubber("tire_rubber", (0.025, 0.029, 0.031))
white = v4.painted("board_white", (0.89, 0.91, 0.89), roughness=0.27, coat=0.25)
frame = v4.plastic("board_frame_dark", (0.08, 0.10, 0.11), roughness=0.3)
red = v4.painted("rim_orange", (0.96, 0.19, 0.025), roughness=0.25, coat=0.45)
nylon = v4.fabric("net_nylon", (0.92, 0.92, 0.86))
label = v4.mat("label_ink", (0.88, 0.31, 0.055), roughness=0.55)
guide_poly = v4.plastic("low_friction_guide_polymer", (0.12, 0.14, 0.15), roughness=0.32, texture=0.04)

# Bodies: ballast tank + fixed lower mast, telescoping upper mast, board/rim carriage.
base = v4.body("base")
pole = v4.body("pole")
board = v4.body("backboard")

# Base: long low molded ballast tank, front points toward -Y.
v4.box("ballast_tank_main", (0.88, 1.32, 0.29), (0, 0.05, 0.235), bevel=0.085, material=base_mat, body=base)
# Raised center bridge and sculpted top shoulders around mast root.
v4.box("tank_top_bridge", (0.56, 0.57, 0.105), (0, 0.27, 0.423), bevel=0.07, material=base_edge, body=base)
v4.box("mast_socket_pedestal", (0.31, 0.31, 0.18), (0, 0.30, 0.548), bevel=0.035, material=steel, body=base)
# Tank molded ribs, matching left/right along the side walls.
for sx in (-1, 1):
    for i, yy in enumerate((-0.40, -0.10, 0.20, 0.50)):
        v4.box(f"tank_side_rib_{sx}_{i}", (0.012, 0.20, 0.16), (sx*0.442, yy, 0.244), bevel=0.005, material=base_edge, body=base)
    # sculpted mast gusset plates on both sides
    v4.extrude(f"mast_gusset_{sx}", [(0,0),(0.34,0),(0.28,0.10),(0.07,0.30),(0,0.30)], 0.018,
               loc=(sx*0.095,0.335,0.61), rot=(0, math.pi/2, 0), bevel=0.006, material=steel, body=base)
# Fill neck on rear quarter with cap and molded rim.
v4.cylinder("fill_neck", 0.064, 0.022, (0.29, 0.54, 0.402), material=base_edge, body=base)
v4.cylinder("fill_cap", 0.052, 0.018, (0.29, 0.54, 0.423), material=steel, body=base)
for k in range(8):
    a = 2*math.pi*k/8
    v4.box(f"cap_grip_{k}", (0.012,0.008,0.009), (0.29+0.052*math.cos(a),0.54+0.052*math.sin(a),0.425), bevel=0.002, material=base_mat, body=base)
# Rear transport wheels and through axle, placed behind the base rear edge.
axle_y = 0.73
v4.rod("wheel_axle", (-0.54, axle_y, 0.17), (0.54, axle_y, 0.17), 0.018, material=steel_hi, body=base)
# stout molded axle hangers merge the axle into the rear tank wall
for sx in (-1,1):
    v4.box(f"axle_hanger_{sx}",(0.085,0.20,0.12),(sx*0.40,0.67,0.22),bevel=0.018,material=steel,body=base)
for sx in (-1,1):
    x=sx*0.48
    v4.torus(f"wheel_tire_{sx}", 0.145, 0.042, (x, axle_y, 0.17), rot=(0,math.pi/2,0), material=black, body=base)
    v4.cylinder(f"wheel_hub_{sx}",0.074,0.026,(x-sx*0.008,axle_y,0.17),rot=(0,math.pi/2,0),material=steel,body=base)
    v4.cylinder(f"wheel_hub_cap_{sx}",0.039,0.029,(x-sx*0.023,axle_y,0.17),rot=(0,math.pi/2,0),material=steel_hi,body=base)
    for k in range(8):
        a=2*math.pi*k/8
        v4.cylinder(f"wheel_spoke_{sx}_{k}",0.005,0.012,(x-sx*0.025,axle_y+0.052*math.cos(a),0.17+0.052*math.sin(a)),rot=(0,math.pi/2,0),segments=12,material=steel_hi,body=base)
# Non-slip tank feet at the four corners.
for sx in (-1,1):
    for sy in (-1,1):
        v4.box(f"tank_foot_{sx}_{sy}",(0.16,0.18,0.035),(sx*0.31,sy*0.48,0.076),bevel=0.014,material=black,body=base)
# Lower square mast; telescoping overlap starts above the pedestal.
# Four-wall square receiver and split guide collar leave a true clearance bore
# for the moving upper mast instead of intersecting it as a solid block.
for sx in (-1,1):
    v4.box(f"lower_mast_wall_x_{sx}",(0.018,0.125,1.42),(sx*0.054,0.30,1.31),bevel=0.003,material=steel,body=base)
    v4.box(f"lower_collar_wall_x_{sx}",(0.03,0.165,0.115),(sx*0.0675,0.30,1.985),bevel=0.004,material=steel_hi,body=base)
for sy in (-1,1):
    v4.box(f"lower_mast_wall_y_{sy}",(0.09,0.018,1.42),(0,0.30+sy*0.054,1.31),bevel=0.003,material=steel,body=base)
    v4.box(f"lower_collar_wall_y_{sy}",(0.105,0.03,0.115),(0,0.30+sy*0.0675,1.985),bevel=0.004,material=steel_hi,body=base)
# Replaceable low-friction bearing pads line the collar bore. They attach to
# the fixed collar and stop 1.5 mm short of the moving mast.
for sx in (-1,1):
    v4.box(f"mast_guide_pad_x_{sx}",(0.008,0.080,0.075),(sx*0.049,0.30,1.985),bevel=0.003,material=guide_poly,body=base)
for sy in (-1,1):
    v4.box(f"mast_guide_pad_y_{sy}",(0.080,0.008,0.075),(0,0.30+sy*0.049,1.985),bevel=0.003,material=guide_poly,body=base)
# Pad retaining fasteners on the outer cheek faces.
for sx in (-1,1):
    for z in (1.955,2.015):
        v4.cylinder(f"collar_pad_screw_x_{sx}_{z}",0.007,0.009,(sx*0.082,0.30,z),rot=(0,math.pi/2,0),segments=20,material=steel,body=base)
# Bolts on collar cheeks
for sx in (-1,1):
    for z in (1.96,2.015):
        v4.cylinder(f"lower_collar_bolt_{sx}_{z}",0.012,0.006,(sx*0.085,0.30,z),rot=(0,math.pi/2,0),material=steel,body=base)

# Upper mast is a square steel tube with a tapered protective top cap, adjustment holes and scale.
v4.box("upper_telescoping_tube",(0.087,0.087,1.77),(0,0.30,2.46),bevel=0.009,material=steel,body=pole)
# The upper tube slides directly inside the lower mast; no external solid collar
# is placed in the overlap zone, so the retained tube remains collision-clear.
v4.box("pole_cap",(0.094,0.094,0.055),(0,0.30,3.352),bevel=0.012,material=frame,body=pole)
# Adjustment detent holes down the front face, plus fine white height marks at the side.
for i in range(12):
    z=1.92+i*0.085
    v4.cylinder(f"detent_hole_{i}",0.009,0.003,(0,0.254,z),rot=(math.pi/2,0,0),segments=20,material=black,body=pole)
    v4.box(f"height_tick_{i}",(0.027,0.003,0.003),(0.050,0.255,z),bevel=0.001,material=steel_hi,body=pole)
# Spring-button height lock with a red pull ring. The nose sits 1.5 mm from
# the detent aperture in the rest pose: it reads as engaged but does not block
# the modeled telescoping motion.
lock_z=2.005
v4.cylinder("retaining_pin_guide_boss",0.022,0.018,(0,0.211,lock_z),rot=(math.pi/2,0,0),material=steel_hi,body=base)
v4.cylinder("retaining_pin_shaft",0.008,0.047,(0,0.2315,lock_z),rot=(math.pi/2,0,0),material=steel,body=base)
v4.cylinder("retaining_pin_handle",0.020,0.024,(0,0.195,lock_z),rot=(math.pi/2,0,0),material=red,body=base)
v4.torus("retaining_pin_pull_ring",0.017,0.0035,(0,0.179,lock_z),rot=(math.pi/2,0,0),material=red,body=base)

# Backboard and carriage body. Board 44 x 30 in, centered at z=2.86.
cz=2.86
# Main panel and perimeter frame are layered edge-to-edge with a small inset reveal.
v4.box("backboard_panel",(1.12,0.035,0.76),(0,-0.018,cz),bevel=0.002,material=white,body=board)
# Dark perimeter frame rails surround panel edges.
for sx in (-1,1):
    v4.box(f"board_frame_side_{sx}",(0.035,0.055,0.78),(sx*0.56,-0.013,cz),bevel=0.008,material=frame,body=board)
for sz in (-1,1):
    v4.box(f"board_frame_rail_{sz}",(1.18,0.055,0.035),(0,-0.013,cz+sz*0.38),bevel=0.008,material=frame,body=board)
# Target box with raised line bars, 0.59 x 0.45 m.
for sx in (-1,1):
    # Settle each target stripe 3 mm into the panel face. The former 4 mm
    # depth/bevel combination split into loose islands and left a 2.5 mm gap.
    v4.box(f"target_vertical_{sx}",(0.009,0.015,0.45),(sx*0.295,-0.040,cz-0.08),bevel=0,material=frame,body=board)
for sz in (-1,1):
    # Extend the crossbars slightly under the verticals for a clean, closed
    # corner with no tiny exposed tips at the target rectangle.
    v4.box(f"target_horizontal_{sz}",(0.59,0.015,0.009),(0,-0.040,cz-0.08+sz*0.225),bevel=0,material=frame,body=board)
# Back-side vertical carriage sleeve surrounds the upper mast and rides freely on it.
v4.box("carriage_sleeve_left",(0.035,0.145,0.43),(-0.070,0.315,cz-0.04),bevel=0.012,material=steel,body=board)
v4.box("carriage_sleeve_right",(0.035,0.145,0.43),(0.070,0.315,cz-0.04),bevel=0.012,material=steel,body=board)
v4.box("carriage_sleeve_back",(0.175,0.035,0.43),(0,0.385,cz-0.04),bevel=0.008,material=steel,body=board)
# Rear cross ties close the carriage sleeve around the mast without crossing the tube.
v4.box("carriage_sleeve_top",(0.175,0.035,0.035),(0,0.39,cz+0.177),bevel=0.008,material=steel_hi,body=board)
v4.box("carriage_sleeve_bottom",(0.175,0.035,0.035),(0,0.39,cz-0.257),bevel=0.008,material=steel_hi,body=board)
# Low-friction polymer guide liners are bolted to the inside faces of the
# carriage cheeks, leaving a 1.5 mm running clearance around the upper mast.
for sx in (-1,1):
    v4.box(f"carriage_guide_pad_{sx}",(0.009,0.085,0.33),(sx*0.0495,0.315,cz-0.04),bevel=0.003,material=guide_poly,body=board)
# Fasteners visibly retain the top and bottom sleeve ties.
for z in (cz-0.257,cz+0.177):
    for sx in (-1,1):
        v4.cylinder(f"carriage_cross_tie_bolt_{sx}_{z}",0.008,0.010,(sx*0.079,0.408,z),rot=(math.pi/2,0,0),segments=20,material=steel_hi,body=board)
# Rear steel H-frame is the rigid board-to-carriage mounting structure. Its
# four straps penetrate 10 mm into the board skin, and its centre mounts extend
# through the rear sleeve with >2 mm overlap at both joints.
for sx in (-1,1):
    v4.box(f"rear_frame_stile_{sx}",(0.045,0.09,0.68),(sx*0.43,0.035,cz),bevel=0.006,material=steel,body=board)
for sz in (-1,1):
    v4.box(f"rear_frame_rail_{sz}",(0.90,0.09,0.045),(0,0.035,cz+sz*0.315),bevel=0.006,material=steel,body=board)
# Two broad, diagonal mounting brackets run from the frame into the carriage
# sleeve; endpoints are deliberately buried inside both surfaces by 20–35 mm.
for sx in (-1,1):
    v4.rod(f"carriage_mount_bracket_{sx}",(sx*0.40,-0.010,cz-0.29),(sx*0.075,0.30,cz-0.02),0.040,material=steel,body=board)
# Adjustment locking cross-pin and large hand knob at the right side.
v4.cylinder("carriage_lock_bolt",0.012,0.09,(0.12,0.315,cz-0.04),rot=(0,math.pi/2,0),material=steel_hi,body=board)
v4.cylinder("carriage_lock_knob",0.038,0.025,(0.176,0.315,cz-0.04),rot=(0,math.pi/2,0),material=black,body=board)
for k in range(6):
    a=2*math.pi*k/6
    v4.rod(f"knob_grip_{k}",(0.188,0.315+0.026*math.cos(a),cz-0.04+0.026*math.sin(a)),(0.202,0.315+0.026*math.cos(a),cz-0.04+0.026*math.sin(a)),0.004,material=steel_hi,body=board)
# Broad rim bracket plate on board rear, plus reinforcing saddle.
v4.box("rim_mounting_plate",(0.34,0.035,0.19),(0,0.015,cz-0.18),bevel=0.012,material=steel_hi,body=board)
# A tapered, thick steel face bracket replaces the plain rectangular saddle;
# its rear face beds into the panel and its nose receives both rim support arms.
v4.extrude("rim_saddle",[(-0.17,-0.09),(0.17,-0.09),(0.13,0.08),(-0.13,0.08)],0.12,
           loc=(0,0.005,cz-0.18),rot=(math.pi/2,0,0),bevel=0.008,material=steel,body=board)
for sx in (-1,1):
    for sz in (-1,1):
        v4.cylinder(f"rim_plate_bolt_{sx}_{sz}",0.012,0.015,(sx*0.135,-0.112,cz-0.18+sz*0.064),rot=(math.pi/2,0,0),segments=24,material=steel_hi,body=board)
# Rim at regulation 18 inch diameter, projects toward player from board face.
rim_z=cz-0.18
rim_y=-0.40
v4.torus("orange_rim",0.224,0.012,(0,rim_y,rim_z),material=red,body=board)
# Short rear attachment arms connect rim hoop to the mounting saddle.
for sx in (-1,1):
    # Anchor to the actual forward arc of the hoop (x=±140 mm, y≈−225 mm).
    v4.rod(f"rim_mount_arm_{sx}",(sx*0.085,-0.12,rim_z),(sx*0.14,rim_y+math.sqrt(0.224**2-0.14**2),rim_z),0.014,material=steel,body=board)
# Net: 12 white cords looped over rim, taper into a lower opening; add two zig-zag rings.
for k in range(12):
    a=2*math.pi*k/12
    top=(0.22*math.cos(a),rim_y+0.22*math.sin(a),rim_z)
    mid=(0.155*math.cos(a+0.10),rim_y+0.155*math.sin(a+0.10),rim_z-0.20)
    low=(0.085*math.cos(a+0.18),rim_y+0.085*math.sin(a+0.18),rim_z-0.38)
    # Use overlapping rod sections so each bent cord has a solid continuous join.
    v4.rod(f"net_cord_upper_{k}",top,tuple(mid[j] + (mid[j]-top[j])*0.04 for j in range(3)),0.003,material=nylon,body=board)
    v4.rod(f"net_cord_lower_{k}",tuple(mid[j] - (low[j]-mid[j])*0.04 for j in range(3)),low,0.003,material=nylon,body=board)
# Three horizontal knotted hoops cross the hanging strands. Their radii and
# heights match the tapered cords exactly so every loop is part of the net.
net_rings=((0.17775,-0.13,0.065),(0.12389,-0.28,0.13556),(0.09278,-0.36,0.17111))
for ri,(rad,zoff,aoff) in enumerate(net_rings):
    pts=[(rad*math.cos(2*math.pi*j/48),rim_y+rad*math.sin(2*math.pi*j/48),rim_z+zoff) for j in range(48)]
    v4.tube(f"net_horizontal_hoop_{ri}",pts,0.0028,material=nylon,body=board,cyclic=True)
# Diagonal crossings form the familiar diamond mesh between the first two hoops.
step=2*math.pi/12
r1,z1,o1=net_rings[0]
r2,z2,o2=net_rings[1]
for k in range(12):
    a=2*math.pi*k/12
    p1=(r1*math.cos(a+o1),rim_y+r1*math.sin(a+o1),rim_z+z1)
    p2=(r2*math.cos(a+step+o2),rim_y+r2*math.sin(a+step+o2),rim_z+z2)
    p3=(r1*math.cos(a+step+o1),rim_y+r1*math.sin(a+step+o1),rim_z+z1)
    p4=(r2*math.cos(a+o2),rim_y+r2*math.sin(a+o2),rim_z+z2)
    v4.rod(f"net_cross_a_{k}",p1,p2,0.0025,material=nylon,body=board)
    v4.rod(f"net_cross_b_{k}",p3,p4,0.0025,material=nylon,body=board)
# Keep the playing face and the area around the target free of exposed marks;
# the perimeter frame is captured by the rear H-frame and needs no face screws.
# The brand/warning graphic is represented by a subtle printed color block only
# when a conforming face mount is available; avoid unattached decals here.
# Mount sockets are represented by collars and the retained detent hardware above.

# Articulated chain: pole slides through lower mast, board carriage slides on upper mast.
v4.joint("mast_extension","prismatic",base,pole,origin=(0,0.30,1.52),axis=(0,0,1),limits=(0.0,0.25))
v4.joint("board_carriage","prismatic",pole,board,origin=(0,0.30,cz),axis=(0,0,1),limits=(-0.38,0.12))

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