"""Conditional owner-photo upper-front profile; no fitted dimensions.

The existing wheel-based and EXIF-focal camera branches both flag an overly
high middle section. A conservative smooth correction holds the current rear
and nose interfaces. It changes contours, not the engine/body reference frame.
"""
import math

START_SOURCE_Z_UNITS=.13
END_SOURCE_Z_UNITS=1.99
MAXIMUM_DROP_UNITS=.10
WHEELHOUSE_BLEND_LOWER_Y_UNITS=.77


def drop_units(source_z):
    if source_z<=START_SOURCE_Z_UNITS or source_z>=END_SOURCE_Z_UNITS:return 0.
    t=(source_z-START_SOURCE_Z_UNITS)/(END_SOURCE_Z_UNITS-START_SOURCE_Z_UNITS)
    return MAXIMUM_DROP_UNITS*math.sin(math.pi*t)


def upper_body_y(y,source_z):
    """Blend estimated upper inner panels; retain the wheel-opening roof datum.

    Rigid equipment is repositioned from its mount axis, never scaled by this
    field. This is a construction interpolation, not measured body sections.
    """
    upper=1.045-.03*(source_z-1)**2-.15*max(0,source_z-1.08)**2
    fraction=min(1,max(0,(y-WHEELHOUSE_BLEND_LOWER_Y_UNITS)/(upper-WHEELHOUSE_BLEND_LOWER_Y_UNITS)))
    return y-fraction*drop_units(source_z)


def metadata(scale):
    return dict(start_source_z_units=START_SOURCE_Z_UNITS,end_source_z_units=END_SOURCE_Z_UNITS,
        maximum_drop_units=MAXIMUM_DROP_UNITS,maximum_drop_mm=MAXIMUM_DROP_UNITS*scale,
        shape='maximum_drop*sin(pi*(source_z-start)/(end-start)); zero outside the interval',
        wheelhouse_blend_lower_y_units=WHEELHOUSE_BLEND_LOWER_Y_UNITS,
        wheelhouse_blend_rule='Interpolate upper-panel height correction from zero at existing wheelhouse-opening roofY=.77 to the full correction at the old upper-wing inner edge. Rigid equipment follows its mount axis; dimensions are retained.',
        evidence='/cars/1975-mg-midget/cad/owner-photo-registration-audit.json',
        owner_photos=['IMG_6329','IMG_6855','IMG_6856'],owner_dimensions_applied=False,
        basis='Both retained wheel-camera and EXIF-focal sensitivity branches place the former far upper-front contour above the owner outline. The selected0.10-unit peak is a conservative contour construction; it is not a fitted height or the optimum of one photograph. Rear/nose stations, nominal axles, marker and engine/body pose stay.',
        limits='Camera calibration, crop/distortion, contour correspondence, fitted sections and common mounting remain uncertain. The sinusoidal correction is an estimated longitudinal profile, not a uniform body/engine move, material/tooling specification, measured seam, loaded clearance or verified removal path. Rear/nose zero values preserve current drawing interfaces; they do not certify those interfaces on the actual car.')
