"""Rhino 8 / Grasshopper Python 3. All generated geometry is in meters.

GH inputs (Item access): n, spacing, seed, scale, height_scale.
GH outputs: bodies, crowns, base, report. No document edits or baking.
The embedded BK04 cohort makes the GH component independent of file paths.
"""
import math
import random

COHORT = [{'guid': '00497d3c-cbaa-43ae-9721-784dc9b95a9d', 'area_m2': 119.05473809970138, 'aspect': 1.9918834381351966, 'height_proxy_m': 12.08952768449378}, {'guid': '00d42f6d-5e96-4470-a968-d1e8fcd33ea1', 'area_m2': 95.65854905413978, 'aspect': 1.6746808205566552, 'height_proxy_m': 12.02014916487347}, {'guid': '0123f99f-3042-4fb4-9210-23c5c1dd3d0f', 'area_m2': 77.50779828903472, 'aspect': 1.7620205780856883, 'height_proxy_m': 7.697386174734025}, {'guid': '018b957a-3bda-4bdb-a36b-1419328584ed', 'area_m2': 119.37535898584824, 'aspect': 1.8715534874823474, 'height_proxy_m': 7.582149783831752}, {'guid': '01b8798d-ca16-4e55-b6ba-bb2a67190290', 'area_m2': 88.37942869430785, 'aspect': 2.0462632218846992, 'height_proxy_m': 7.0580346174301045}, {'guid': '01d2359b-461f-4e44-87a9-2ac8fe94e57b', 'area_m2': 111.53267954560323, 'aspect': 2.2606203181031383, 'height_proxy_m': 14.826419308022171}, {'guid': '01e4142a-2d9c-4903-aa25-23d3be30bc08', 'area_m2': 100.41918336592097, 'aspect': 2.3117444314219053, 'height_proxy_m': 5.933787425325591}, {'guid': '0224a397-254f-4264-8ec7-f9d131dbe717', 'area_m2': 87.4569136478501, 'aspect': 1.6846483769100513, 'height_proxy_m': 3.586838860592357}, {'guid': '023908d8-4996-44ae-95b9-a028e62464d3', 'area_m2': 67.45803384742764, 'aspect': 2.22960357797746, 'height_proxy_m': 9.991567784730888}, {'guid': '026ff996-d58b-49be-9547-25e872bca0d6', 'area_m2': 105.8482463150967, 'aspect': 2.017954992963611, 'height_proxy_m': 10.566995787292083}, {'guid': '028ebef6-39fb-4ffe-b97d-1840cbe580e7', 'area_m2': 88.44214503848146, 'aspect': 2.101747597075919, 'height_proxy_m': 3.8787109429545334}, {'guid': '02eac420-8e94-48e3-9d10-aa1721e67c53', 'area_m2': 93.43272864392814, 'aspect': 1.6155037595214838, 'height_proxy_m': 3.254400519535518}, {'guid': '0383ee26-2b6c-46a1-827c-e19f8440fa16', 'area_m2': 101.59341823986414, 'aspect': 2.2850176877346366, 'height_proxy_m': 5.542169054932418}, {'guid': '03a96bef-95db-4651-af35-529382259cd5', 'area_m2': 79.69375819643824, 'aspect': 2.4551913583013385, 'height_proxy_m': 10.48709375876241}, {'guid': '03c3cf2a-48e3-4b6f-b3f8-01d5cb06bff7', 'area_m2': 78.33327209557808, 'aspect': 2.2774897865561776, 'height_proxy_m': 11.980597092013937}, {'guid': '03d53b95-5943-4402-9411-75c831df17dd', 'area_m2': 51.413617634906764, 'aspect': 1.8684748157247852, 'height_proxy_m': 8.139137021818005}, {'guid': '03e40560-c35f-490c-84c5-262e9b771513', 'area_m2': 42.98300718618401, 'aspect': 1.6483761879755152, 'height_proxy_m': 7.249384951724534}, {'guid': '04017ee4-83c2-4bcb-b9e4-cb2b737f4df5', 'area_m2': 58.15986074528869, 'aspect': 2.3184220809035874, 'height_proxy_m': 6.490904547524844}, {'guid': '04519350-a4db-496c-a2a1-5e075cfb6809', 'area_m2': 111.3392888341253, 'aspect': 2.1949371058147387, 'height_proxy_m': 9.092863552337962}, {'guid': '0455093a-960b-4aca-a2c4-8e768033900f', 'area_m2': 94.67606924591293, 'aspect': 2.238677563441132, 'height_proxy_m': 11.68947462392944}, {'guid': '0469c9ce-f61c-44c2-9da8-f87645a95bec', 'area_m2': 94.24897131166519, 'aspect': 2.4630627411145896, 'height_proxy_m': 6.490126251241523}, {'guid': '04b15718-631a-4a2a-9df3-20c96187ffaf', 'area_m2': 78.77601601102246, 'aspect': 2.5329327067413145, 'height_proxy_m': 10.084414716614168}, {'guid': '04dfe6a7-8596-48a2-a0ba-6e21eb4ea0f6', 'area_m2': 58.94767560536429, 'aspect': 2.3645584886850766, 'height_proxy_m': 8.13730950334283}, {'guid': '05004d16-be38-4d32-8c75-12dd4d2db701', 'area_m2': 82.74134788903565, 'aspect': 2.16416025668181, 'height_proxy_m': 8.4973198183455}, {'guid': '05059949-eea1-4837-97e8-00aa31a570c8', 'area_m2': 77.06739252618978, 'aspect': 2.305434569948062, 'height_proxy_m': 7.9268913409088855}, {'guid': '05427404-eb9c-4ffd-9fac-e831abefd2cb', 'area_m2': 71.01886552830003, 'aspect': 2.4758503174700337, 'height_proxy_m': 4.703487492183957}, {'guid': '0578a0b4-601b-4019-a4e3-99334a502ebf', 'area_m2': 60.80870385883722, 'aspect': 2.3116200131364493, 'height_proxy_m': 5.430122698231192}, {'guid': '05a798a4-5081-405d-ac87-fee2798aee7f', 'area_m2': 97.36211722185072, 'aspect': 2.490433517048638, 'height_proxy_m': 5.995705081706363}, {'guid': '05a91336-b04b-4044-9c59-af98baeaa7a7', 'area_m2': 45.317934373327944, 'aspect': 1.3794657846867708, 'height_proxy_m': 6.0507052908824965}, {'guid': '0623532d-016e-4092-9631-50d8ed44e1f2', 'area_m2': 85.96895679598535, 'aspect': 2.3994215303792066, 'height_proxy_m': 9.382678010649558}, {'guid': '0691e127-ccbc-49e1-ac25-6bd0ae1c71bf', 'area_m2': 84.75530178783607, 'aspect': 1.6920646795523677, 'height_proxy_m': 9.99598182489873}, {'guid': '06d7bd82-8f9b-490b-a268-46d7b36887c1', 'area_m2': 85.01011524829113, 'aspect': 2.42648387693282, 'height_proxy_m': 9.060000933930771}, {'guid': '07051d98-e6fe-4971-83fa-1ed848ab9af8', 'area_m2': 65.83775904108627, 'aspect': 1.8018972163547546, 'height_proxy_m': 9.45917758447378}, {'guid': '075c2193-2e85-42b5-8ee9-926c3843363f', 'area_m2': 96.74088870620112, 'aspect': 2.603500179551383, 'height_proxy_m': 12.294208643011029}, {'guid': '075ffd93-f373-4c56-8d0a-a2609fc6fabd', 'area_m2': 83.68472624640549, 'aspect': 2.754490387888561, 'height_proxy_m': 9.939574380515495}, {'guid': '07a149ff-23ad-4eb4-bc62-f3805df87556', 'area_m2': 93.66958006210115, 'aspect': 1.5650112434828247, 'height_proxy_m': 7.917006172308199}, {'guid': '07a25d90-a3c5-4326-b3a9-38bb63460ca4', 'area_m2': 102.09758463941128, 'aspect': 1.8431786255391682, 'height_proxy_m': 7.85699303942928}, {'guid': '07abd653-6868-466f-8fb5-57ba7ac67146', 'area_m2': 60.701455497213566, 'aspect': 2.551410359972258, 'height_proxy_m': 8.320194140840366}, {'guid': '08194498-4825-43c4-bf88-b4abf5abfdb6', 'area_m2': 81.43711769895658, 'aspect': 2.4501905184547357, 'height_proxy_m': 10.247748579667325}, {'guid': '08340633-bd6d-41b3-bf6f-14f159b7cb82', 'area_m2': 87.8115478619775, 'aspect': 1.2734275466227551, 'height_proxy_m': 7.634592347871966}, {'guid': '086aaf0e-23c1-465d-a70a-c6726ee8a0ad', 'area_m2': 51.3817302993551, 'aspect': 1.4667436813850432, 'height_proxy_m': 6.667231577186089}, {'guid': '0886bcb0-548c-421d-bc1b-e502bb2156ac', 'area_m2': 58.65293570651214, 'aspect': 1.808206448128296, 'height_proxy_m': 8.048887695733262}, {'guid': '08978715-c075-4693-9f7d-d3793585731f', 'area_m2': 85.25615858509524, 'aspect': 2.886608273957887, 'height_proxy_m': 7.091275522284894}, {'guid': '08a94ec2-0e27-48eb-8980-b8860f79ebc7', 'area_m2': 84.38616661015122, 'aspect': 2.3109869540970855, 'height_proxy_m': 3.1768657053787566}, {'guid': '08c9336f-2c96-4da6-876c-c453aef85874', 'area_m2': 67.94427761293437, 'aspect': 2.285374390214549, 'height_proxy_m': 8.421234312921516}, {'guid': '0939575f-4c44-4a4a-8ed9-e7c1be5f1d34', 'area_m2': 81.43337916801558, 'aspect': 1.4550209703923294, 'height_proxy_m': 3.0520455345586037}, {'guid': '094a24af-98dc-46f3-bfc9-968edc306a9a', 'area_m2': 58.01202796284676, 'aspect': 1.9031706400622315, 'height_proxy_m': 8.342581835813537}, {'guid': '094bf63b-9f43-4771-8fe9-8f6f75d4e65a', 'area_m2': 107.79645736875446, 'aspect': 2.2634760342492948, 'height_proxy_m': 8.723169971468522}, {'guid': '094e2d4a-c3bf-407d-951e-35e27ad56e8f', 'area_m2': 113.34324217455206, 'aspect': 2.198551406867172, 'height_proxy_m': 12.528765733637567}, {'guid': '09572069-0a58-4c1c-90d7-1425495797d9', 'area_m2': 90.93574720369519, 'aspect': 2.809971332715277, 'height_proxy_m': 10.91027816882561}, {'guid': '0980673e-a2c4-4edf-a740-92632f9d22b6', 'area_m2': 82.5383773614737, 'aspect': 2.6905751719060502, 'height_proxy_m': 3.7653347904250323}, {'guid': '099b4010-04b7-4210-8c77-72df74902473', 'area_m2': 95.22647857268511, 'aspect': 2.4706075742171874, 'height_proxy_m': 12.09996502141026}, {'guid': '09d24d69-12cb-4860-84f5-77ee84caa219', 'area_m2': 116.92248598526983, 'aspect': 1.7193287059637867, 'height_proxy_m': 11.319174387039672}, {'guid': '09d9ade4-e459-4d29-aa91-f0067ef13b34', 'area_m2': 61.10354694448548, 'aspect': 1.8648995928664573, 'height_proxy_m': 8.375315977571498}, {'guid': '0a0183eb-e23b-4013-be21-a88fd1fa0a78', 'area_m2': 42.71492906093938, 'aspect': 1.2773613324048394, 'height_proxy_m': 4.89353146555435}, {'guid': '0a3ccbb2-1221-4a10-9d25-4b464c9f4b39', 'area_m2': 86.10960619339585, 'aspect': 2.7635126336377747, 'height_proxy_m': 7.873264973528579}, {'guid': '0a88497a-f77a-4971-a1cc-21e2ea813a47', 'area_m2': 116.2803876513912, 'aspect': 1.8941972325155072, 'height_proxy_m': 8.793744162613528}, {'guid': '0aa52ab0-f17e-4f7f-96bc-87bc59f50908', 'area_m2': 82.53847846772436, 'aspect': 2.6116685496557253, 'height_proxy_m': 10.245147493839937}, {'guid': '0b22360b-2880-4068-93ec-fb91e727a898', 'area_m2': 114.1707636661366, 'aspect': 2.2420418464188723, 'height_proxy_m': 4.467388826609518}, {'guid': '0b54157f-43ce-4743-8d36-e24ad5138f0e', 'area_m2': 107.8264558167476, 'aspect': 2.108460286076997, 'height_proxy_m': 8.945489249358399}, {'guid': '0b5a6121-3e0f-4876-ad1f-19c8778d8698', 'area_m2': 91.81483114007254, 'aspect': 2.465104665580008, 'height_proxy_m': 9.603531095940399}, {'guid': '0b7962d5-4d68-4344-ab81-b244e744229d', 'area_m2': 99.32111785611237, 'aspect': 2.5481256558332315, 'height_proxy_m': 7.1292118939669455}, {'guid': '0ba4cb0c-83e8-4d6d-b4bd-34a9c66e6b73', 'area_m2': 70.43194301798897, 'aspect': 2.547087583640516, 'height_proxy_m': 8.685057047531554}, {'guid': '0bd626c6-2826-4ae1-96d0-7649876aae93', 'area_m2': 79.58277419732438, 'aspect': 2.2500518585953895, 'height_proxy_m': 8.319251921928316}, {'guid': '0c000425-a3b9-4f3c-b773-4e1185330a12', 'area_m2': 87.63940310563108, 'aspect': 2.294316247711495, 'height_proxy_m': 7.456512839603756}, {'guid': '0c0c4e69-cb13-4639-8c5e-314b6c54bb51', 'area_m2': 84.8249464266727, 'aspect': 2.306660200282019, 'height_proxy_m': 10.350089745742192}, {'guid': '0c11a6e2-4b53-4616-8299-35c8e58d7eaa', 'area_m2': 51.76717512219862, 'aspect': 1.8074179339093086, 'height_proxy_m': 7.492347697591471}, {'guid': '0c2daafc-97ce-41db-a8b0-e802630bbb05', 'area_m2': 116.02638308422, 'aspect': 1.902886281007157, 'height_proxy_m': 4.156236264567147}, {'guid': '0c5a5d96-4a7f-4f65-8ebc-89cdcf76ccfc', 'area_m2': 96.3511966364508, 'aspect': 2.6453990337904347, 'height_proxy_m': 10.587480426490112}, {'guid': '0c75a982-95e9-464b-b41d-7ec4e05a0902', 'area_m2': 73.57039517130005, 'aspect': 1.3451631782826978, 'height_proxy_m': 4.814252222677061}, {'guid': '0c7fd80b-6317-4721-8d73-91b0023b7b55', 'area_m2': 52.73194983796083, 'aspect': 2.6112978250200913, 'height_proxy_m': 6.664264649051851}, {'guid': '0cc0b993-b1f5-4214-a646-b5819e7b2d1c', 'area_m2': 89.56869426883455, 'aspect': 1.6898043456015681, 'height_proxy_m': 9.248112744912957}, {'guid': '0cce7a5b-0a7a-4509-8994-c3e1b316190a', 'area_m2': 101.18919279875104, 'aspect': 2.372674535443435, 'height_proxy_m': 11.793423215913652}, {'guid': '0da01c4d-1e99-4e44-b157-dc85a5682480', 'area_m2': 81.88731703353463, 'aspect': 2.2675069019344676, 'height_proxy_m': 9.234608074587404}, {'guid': '0dc6495f-2562-4375-bf48-6ccb534643b4', 'area_m2': 51.70204049139637, 'aspect': 1.9660286172061285, 'height_proxy_m': 7.6749356805138556}, {'guid': '0e0fef6b-ee93-457c-97c3-f3047e60c26c', 'area_m2': 105.56241775545145, 'aspect': 1.964728897352928, 'height_proxy_m': 11.4553357135521}, {'guid': '0e3abee0-459c-4b95-8919-fcf4277e1bf0', 'area_m2': 69.93163625485121, 'aspect': 2.0477973639837193, 'height_proxy_m': 3.119862196140076}, {'guid': '0e7e5c65-f3e4-426c-b670-2de5e8b2d62c', 'area_m2': 84.9025872388089, 'aspect': 2.3047769035125505, 'height_proxy_m': 4.1587026117556665}, {'guid': '0ebfacfb-f1fa-4672-b585-325d0a68d5c9', 'area_m2': 90.98214438506743, 'aspect': 2.8067624531631177, 'height_proxy_m': 14.655913674808403}, {'guid': '0ec70869-e42e-4948-8161-b94471f58d1b', 'area_m2': 90.84946411321043, 'aspect': 2.6938949594500365, 'height_proxy_m': 9.9356089626001}, {'guid': '0ed17884-d3dd-4d14-bd16-2e9891d5d3e6', 'area_m2': 66.06429895497074, 'aspect': 2.377070255756863, 'height_proxy_m': 7.031149729601951}, {'guid': '0f27d960-b2fe-42c3-b6be-7f61a4528d09', 'area_m2': 93.53313985044387, 'aspect': 2.481931379075448, 'height_proxy_m': 6.385026248155157}, {'guid': '0f46f4fe-9c00-419d-8a2e-d4d33d0e74c9', 'area_m2': 46.43329562111985, 'aspect': 1.6479271060666372, 'height_proxy_m': 3.7259720428512897}, {'guid': '0f6121e6-1cae-44fa-bcf3-8a9d3938ade6', 'area_m2': 107.68580718028122, 'aspect': 2.35348864491946, 'height_proxy_m': 9.470759322850778}, {'guid': '0fdf03ee-4b45-4b77-b9c0-079c6d43c34f', 'area_m2': 75.90830789890754, 'aspect': 2.2667473690525246, 'height_proxy_m': 9.248344132851054}, {'guid': '1061b854-8c14-46e6-9d0a-d3b5a5cb2c77', 'area_m2': 91.4750773469734, 'aspect': 2.383884966509758, 'height_proxy_m': 11.14617976060452}, {'guid': '106f3f08-f222-4f83-babd-ae872b9cda0f', 'area_m2': 81.17534531881144, 'aspect': 2.398056037812589, 'height_proxy_m': 8.905091362469646}, {'guid': '10891be7-5f96-4cf7-8406-bba0474b8f86', 'area_m2': 88.21188758198619, 'aspect': 2.32152126739786, 'height_proxy_m': 8.10777330817762}, {'guid': '10b32360-7e12-44f9-98f2-2a0f33e26037', 'area_m2': 96.32207948074942, 'aspect': 2.562990989166937, 'height_proxy_m': 12.315105781066109}, {'guid': '10d69a0c-9dbf-448f-9687-0dc408ed2149', 'area_m2': 116.35179298289778, 'aspect': 1.9133758501109777, 'height_proxy_m': 11.261536581254123}, {'guid': '10ead3ca-70ee-464d-95d0-90df2175f448', 'area_m2': 85.94466820241051, 'aspect': 2.2283840100094197, 'height_proxy_m': 8.848502417348238}, {'guid': '1102c795-468a-4339-bd6b-c0201f23e2dc', 'area_m2': 62.13844753037461, 'aspect': 2.4748091237068928, 'height_proxy_m': 7.807993833203388}, {'guid': '110ddfa3-7369-44d6-be6e-03c1b1e9c378', 'area_m2': 42.569658010396786, 'aspect': 1.3247304976130194, 'height_proxy_m': 2.679639934806408}, {'guid': '114e9573-dda1-4553-a6e8-a06943732604', 'area_m2': 90.92424389387733, 'aspect': 2.4080041718641727, 'height_proxy_m': 12.684847239954433}, {'guid': '11ec4d6c-649a-40ea-8910-f0b6b26bae0e', 'area_m2': 111.79099700351988, 'aspect': 1.8990112638258636, 'height_proxy_m': 11.341563290665752}, {'guid': '12106023-0562-4a08-939a-a315ef13df2e', 'area_m2': 51.363761192146505, 'aspect': 1.273991012728322, 'height_proxy_m': 7.5062302133444065}]


def layout(n=7, spacing=26.0, seed=42, scale=0.9, height_scale=1.4):
    if isinstance(n, bool) or not isinstance(n, (int, float)) or not math.isfinite(n) or int(n) != n or not 1 <= n <= 20:
        raise ValueError('n must be an integer from 1 to 20')
    if isinstance(seed, bool) or not isinstance(seed, (int, float)) or not math.isfinite(seed) or int(seed) != seed or not 0 <= seed <= 1000000:
        raise ValueError('seed must be an integer from 0 to 1000000')
    for name, value, low, high in [('spacing', spacing, 24, 60), ('scale', scale, .5, 1.3), ('height_scale', height_scale, .5, 3)]:
        if isinstance(value, bool) or not isinstance(value, (int, float)) or not math.isfinite(value) or not low <= value <= high:
            raise ValueError('%s must be between %s and %s' % (name, low, high))
    rng = random.Random(int(seed))
    blocks = []
    for j in range(int(n)):
        for i in range(int(n)):
            row = rng.choice(COHORT)
            long = math.sqrt(row['area_m2'] * row['aspect']) * scale
            short = math.sqrt(row['area_m2'] / row['aspect']) * scale
            height = row['height_proxy_m'] * height_scale
            blocks.append(dict(x=(i+.5)*spacing, y=(j+.5)*spacing, long=long, short=short, height=height, guid=row['guid']))
    return blocks, n * spacing


def geometry(n=7, spacing=26.0, seed=42, scale=.9, height_scale=1.4):
    import Rhino.Geometry as G
    blocks, side = layout(n, spacing, seed, scale, height_scale)
    bodies, crowns = [], []
    for b in blocks:
        plane = G.Plane(G.Point3d(b['x'], b['y'], 0), G.Vector3d.ZAxis)
        bodies.append(G.Box(plane, G.Interval(-b['long']/2, b['long']/2), G.Interval(-b['short']/2, b['short']/2), G.Interval(0, b['height'])).ToBrep())
        crowns.append(G.Box(plane, G.Interval(-.36*b['long'], .36*b['long']), G.Interval(-.36*b['short'], .36*b['short']), G.Interval(b['height'], 1.16*b['height'])).ToBrep())
    base = G.Box(G.Plane.WorldXY, G.Interval(-4, side+4), G.Interval(-4, side+4), G.Interval(-3, 0)).ToBrep()
    assert all(g.IsValid and g.IsSolid for g in bodies + crowns + [base])
    return bodies, crowns, base, '%d synthetic masses; %.1f m square base. Heights are incomplete proxies; crowns and grid are new design choices.' % (len(bodies), side+8)


def self_check():
    blocks, side = layout()
    assert len(blocks) == 49 and side == 182
    assert blocks == layout()[0] and blocks != layout(seed=43)[0]
    assert len(layout(n=4)[0]) == 16
    assert abs(blocks[0]['long'] / blocks[0]['short'] - next(r['aspect'] for r in COHORT if r['guid'] == blocks[0]['guid'])) < 1e-10
    for b in layout(n=20, spacing=24, scale=1.3)[0]:
        assert b['long'] < 24 and 0 < b['short'] <= b['long'] and b['height'] > 0
    assert layout(height_scale=2)[0][0]['height'] > blocks[0]['height']
    assert layout(scale=1)[0][0]['long'] > blocks[0]['long']
    assert layout(spacing=30)[1] == 210
    for bad in [dict(n=0), dict(n=2.5), dict(seed=-1), dict(spacing=0), dict(scale=float('nan')), dict(height_scale=4)]:
        try:
            layout(**bad)
        except ValueError:
            pass
        else:
            raise AssertionError('Invalid input accepted: %s' % bad)
    return 'layout checks passed'


# Grasshopper supplies these five input variables; ordinary module imports do not.
if all(k in globals() for k in ('n', 'spacing', 'seed', 'scale', 'height_scale')):
    bodies, crowns, base, report = geometry(n, spacing, seed, scale, height_scale)
