###############
### DM2_w24 ###
### sorting ###
###############

##############################
import rhinoscriptsyntax as rs
import random, math, sys
sys.path.append("P:/WWW/limafi/dm2")     ###
import DM_lib as dm        ### 
reload(dm)
###################

###################
rs.ViewDisplayMode(view=None, mode="rendered")
rs.EnableRedraw(0)
for DM in range(10):
    dm.PointRadius(displayModeX=DM, rad=3, styl=3, verbose=0)
###################

######## need file "dachstein_5m_srfs.3dm"

dm.newEmptyLayer("DEMO::pnts", [100,0,200])

srfs = rs.ObjectsByName("040_*", 0)             ### sel by name #name wird gegeben 
for srf in srfs:
    print rs.ObjectName( srf )


coords = dm.getSurfacePoints( srfs[0] )         ### get coords of surface-Control-points !
print "len(coords)", len(coords)

############
coordsSorted = sorted(coords, key=lambda sKey: sKey[2]) ### that's essential ! (sort by z_coord)
############

if 1:
    #rs.AddPoints( coords )
    #rs.AddCurve( coordsSorted, 3)
    minZ = coordsSorted[0][2]
    maxZ = coordsSorted[-1][2] ## hoher dachstein
    print minZ, maxZ
    linX = rs.AddLine( coordsSorted[0], coordsSorted[-1] )
    linY = rs.AddLine( coordsSorted[-1], coordsSorted[-2] )

######## filter sorted coordsLists / by z_value / by (projected !) distance
if 0: 
    dachsteinGipfel = coordsSorted[-1]
    coordsDachstein = [ cor for cor in coordsSorted if rs.Distance(cor, coordsSorted[-1]) < 200.0 ]
    coordsDachstein = [ cor for cor in coordsSorted if cor[2] > maxZ-200 and rs.Distance(dm.pnt2XY0(cor), dm.pnt2XY0(coordsSorted[-1])) < 100.0 ]
    coordsRest = [ cor for cor in coordsSorted if cor not in coordsDachstein ]
    
    rs.AddPoints( coordsDachstein )
    #rs.AddPoints( coordsRest[0:300] )
    #rs.AddPoints( coordsRest[-100:] )
    
    coordsRest = sorted([ cor for cor in coordsSorted if cor not in coordsDachstein ], key=lambda sKey: sKey[2])
    mitterspitz = coordsRest[-1]
    
    rs.AddLine( dachsteinGipfel, mitterspitz )

######## manipulate surfaces .. surface"Grips"
if 0: 
    for i, cor in enumerate(coords): ## unsorted !
        #if cor[2] > maxZ-200:
        if rs.Distance(dm.pnt2XY0(cor), dm.pnt2XY0(coordsSorted[-1])) < 200.0:
            coords[i][2] =  maxZ-200 #manipuliere z-koordinate
    rs.EnableObjectGrips(srfs[0], 1) #eins
    rs.ObjectGripLocations( srfs[0], coords)
    rs.EnableObjectGrips(srfs[0], 0) #disable


######## farbverlauf via reMap(..) 
if 1: 
    for i,cor in enumerate(coordsSorted[0:]):
        zVal = cor[2]
        ### reMap( value_2_remap, in_Min, in_Max, out_Min, out_Max )
        col = dm.reMap(zVal, coordsSorted[0][2], coordsSorted[-1][2]-00.0, 0, 255, limit=1) ### get newest library !!!
        col = int(col)
        pnt = rs.AddPoint( cor )
        rs.ObjectColor( pnt, [col, 200, col] )
        if i%2000==0:
            print [col, 100, col]
            rs.Redraw()

######## sun Demo 
if 1: 
    for hor in range(6, 6+12):
        dm.setSun ( year=2024, mon=12, day=24, hour=hor, min=30, sec=0, verbose=1)
        rs.Redraw()
        rs.Sleep(100)

######## surface closest point
if 0:  
    rs.ViewDisplayMode(view=None, mode="wireframe")
    dm.newEmptyLayer("DEMO::crvs", [50,50,50])
    coords = rs.DivideCurve(linX, 100, 1)
    for cor in coords:
        dm.esc() ### stop/break executing
        params = rs.SurfaceClosestPoint(srfs[0], cor) ### returns: list(number, number): The U,V parameters of the closest point on the surface 
        # print params
        pntX = rs.EvaluateSurface(srfs[0], *params)
        rs.AddLine( cor, pntX )
        rs.Redraw()
    rs.ViewDisplayMode(view=None, mode="rendered")


#########################
rs.CurrentLayer("Default")
rs.EnableRedraw(1) # 4 the MACs

#################################################
# https://de.wikipedia.org/wiki/Schartenh%C3%B6he
# alpen: 100-300m
# 4_tausender: 30m lt. UIAA (Union Internationale des Associations d'Alpinisme)
# hymalaya: 500m