#############################
### diag @ IAM @ DM2_w25  ###
### hu_02 setUp_ver00_demo_03
#############################


##############################
import rhinoscriptsyntax as rs
import random, time, sys   ###
sys.path.append("P:/")     ###                  ### diag's path to DM_lib.py  .. please keep it !                                               
sys.path.append("P:/TUGonlineUsernam/dm2/")     ### *your* path to DM_lib.py                                            
sys.path.append("P:/WWW/TUGonlineUsernam/dm2/") ### *your* path to DM_lib.py                                            
sys.path.append("/Users/macpro/Desktop/DM2")    ### *your* path to DM_lib.py      
import DM_lib as dm        ###                  ### "dm" is alias for DM_lib / for adressing definitions etc in "DM_lib.py"
############################## reload(dm)

rs.ShowGrid(show=0)
rs.ShowGridAxes(show=0)
rs.ViewDisplayMode(mode="Wireframe")
rs.EnableRedraw(0)

dm.PointRadius(displayModeX=0, rad=2, styl=3)                   ### displayModeX=0="Wireframe" / rad=size of points / styl=pointStyle (try 0..5) / default 3, 3
dm.printDisplay(state=1, scale=0.1, thickness=1, color="Display") ### state=0/1 = off/on / for showing printWidth etc

rs.DeleteObjects(rs.AllObjects())
### BASIC SETUP / mandatory input:
### return_value of definition "dm.setUp_hu_02(..)" is 1 array containing 2 coord_arrays: [ [coordsCir], [coordsSqu] ] (circle & square)
### anzahl == number of coords: range from 8 to 1001 / 4*250 / default = 4*32 
### rotateSqu .. rotation of square_coords: may be set to/try (0, 0)..(-90,90) .. etc / default = (-90, 90)
allCoords = dm.setUp_hu_02( anzahl=4*8,  rotateSqu=random.uniform(-90,90) )
coordsCir=allCoords[0]
coordsSqu=allCoords[1]

##### check setup - just visualization of coords
##### don't execute  @ final version of homework
if 0: ### 0 for *your* final version !
    if 1:
        rs.AddPoints( coordsCir )
        rs.AddPoints( coordsSqu )
    if 0:
        rs.ObjectColor(dm.textDots(coordsCir), [111,0,11])
        rs.ObjectColor(dm.textDots(coordsSqu), [11,0,111])
    if 0:
        rs.AddCurve( coordsCir, 1)
        rs.AddCurve( coordsSqu, 1)
        for i in range( len(coordsSqu) ):
            p0  = coordsCir[i]
            p1  = coordsSqu[i]
            lin = rs.AddLine(p0, p1)
            rs.ObjectColor(lin, [222*(i!=0), 22*(i!=0), 222])
            rs.ObjectPrintWidth( lin, 1.0*(i==0) )
##### end check setup

#######################################
################ HERE YOU GO AS YOU GO:
dm.setTime()

#### immer gut zu wissen:
anzahl   = len(coordsSqu)
side_len = rs.Distance(coordsSqu[0], coordsSqu[int(anzahl/4)])
print "lenx of square_side =", side_len


### like 'sandUhr' / just 4 fun
#coordsCir.reverse()   

### connect coordsCir..coordsSqu
allLines = [] ### leeres array
for i in range(anzahl):
    p0 = coordsCir[i]
    p1 = coordsSqu[i]
    lin = rs.AddLine( p0, p1 )  ### rs.AddLine(..) returns ID of line > saved 2 variable "lin"
    allLines.append( lin )      ### die ID 'lin' an array allLines anhaengen

### borderlines
rs.AddCurve( coordsSqu+coordsCir )                  ### degree=3 (default !)
rs.ObjectColor( rs.AllObjects()[0], [222,0,111])    ### rs.AllObjects()[0] == zuletzt generiertes object
rs.ObjectPrintWidth( rs.AllObjects()[0], 0.5 )      ### check dm.printDisplay(..)
rs.ObjectName( rs.AllObjects()[0], "borderLine" )
borderLine = rs.AllObjects()[0]                     ### ID der curve in var 'borderLine' schreiben


#### eine (convexe !) "flaeche" mit lines fuellen
if 0:
    combis = dm.combinate(coordsSqu, 2)
    #combis = dm.combinate(coordsCir, 2) ### you may choose !
    print "anzahl coords =", anzahl, ".. number of possible combinations/pairs =", len(combis)
    random.shuffle(combis)
    for comb in combis[0:int(len(combis)/1)]:               ### anzahl festlegen .. try /1 (all !)
        if rs.Distance( comb[0], comb[1] ) > side_len*1:    ### no lines am rand
            pass
            crv = rs.AddCurve(comb, 1)
            rs.ObjectColor( crv, [222,11,11])

#### das "volumen" mit curves fuellen
if 1:
    ### 1. auf den erzeugenden points/coords berechnen
    allCoords = []
    list_of_erzeugenden_coords = []
    for lin in allLines:
        coords = rs.DivideCurve( lin, 16, create_points=1 )
        allCoords.extend( coords )  ### extend ! / not append !
        rs.Redraw()
    
    ##### einen curve_WUSEL im inneren generieren
    if 0:
        random.shuffle(allCoords)
        #rs.ObjectColor( rs.AddCurve( allCoords, 1 ), [222, 0, 0] )
        rs.ObjectColor( rs.AddCurve( allCoords, 2 ), [111, 222, 33] )
        rs.ObjectColor( rs.AddCurve( allCoords, 3 ), [33, 111, 222] )
        rs.ZoomExtents()
        rs.Redraw()
        #rs.Sleep(2000)
    
    ##### viele CRISS_CROSS / kreuz_und_quer lines generieren
    if 1:
        combis = dm.combinate(allCoords, 2)
        random.shuffle(combis)
        print "number of possible combinations =", len(combis)
        ### alle points (type=1) und  curves (type=4) entfernen
        rs.DeleteObjects( rs.ObjectsByType( 1 ) )
        
        allCurves = rs.ObjectsByType( 4 )       ### liste der IDs aller curves
        allCurves.remove( borderLine )          ### ID der rundum_curve aus liste allCurves entfernen
        rs.DeleteObjects( allCurves )           ### alle curves ausser rundum_curve entfernen
        rs.Redraw()
        for comb in combis[0:1001]:
            if rs.Distance( comb[0], comb[1] ) > side_len*0.25:
                rs.AddCurve(comb, 1)
        rs.ZoomExtents()
        
        ### die curves nach ihrer "hoehenlage" / z_coordinate sortieren und verlaufende farbe zuweisen
        ### von allen curves die z_coordinate des mittelpunks in liste schreiben
        allCrvs = rs.ObjectsByType( 4 )[0:-1]   ### alle ausser borderLine - ist "oldest" object - ergo: (list_)index = -1
        
        ######################################################################
        ### eine schnellere / smartere art der erstellung von einfachen listen
        z_values = [ rs.CurveMidPoint(crv)[2] for crv in allCrvs ] ### smart listing - rs.CurveMidPoint(crv)[2] == z_value of cordinate
        
        ### maxi/minimalwerte dieser liste sind:
        ### werden gebarucht fuer dm.reMap(..)
        z_min = min(z_values)
        z_max = max(z_values)
        print "die z_coords der mittelpunkte erstrecken sich von", round(z_min, 2), "bis", round(z_max, 2)
        ### mit der def 'reMap(..)' wird fuer jeden z_value ein color value von 0..255 berechnet
        color_values = [ int( dm.reMap(z_value, z_min, z_max, 0, 255) ) for z_value in z_values ]
        
        for i in range( len(allCrvs) ):
            c_val = color_values[i]
            rs.ObjectColor( allCrvs[i], [c_val, 200, 255-c_val] )
            if i%100==0: rs.Redraw()
        
        rs.Redraw()
        #rs.Sleep(2000)
        ##### pointCloud / viele viele points generieren und nach x||y||z_lage farbe zuweisen
        if 1:
            #allCrvs = rs.ObjectsByType( 4 )
            allCoords = []
            for i,crv in enumerate(allCrvs):
                lenx = rs.CurveLength( crv )    ### anzahl der points soll fuer jede curve separat und entsprechend curve_laenge berechnet werden
                anz_coords = int( lenx*2 )      ### *2 .. sodass abstand jeweils ca 0.5 !
                allCoords.extend( rs.DivideCurve(crv, anz_coords, 1) ) ### DivideCurve(curve_id, segments, create_points=False, return_points=True) 
                rs.DeleteObject( crv )
                if i%100==0: rs.Redraw()
            
            allPnts = rs.ObjectsByType( 1 )

            cor_values = [ rs.PointCoordinates(pnt)[2] for pnt in allPnts ] ### smart listing: [0]==x / [1]==y / [2]==z
            ### maxi/minimalwerte dieser liste sind:
            cor_min = min(cor_values)
            cor_max = max(cor_values)
            print "die z_coords der points erstrecken sich von", round(z_min, 2), "bis", round(z_max)
            ### mit der def 'reMap(..)' wird fuer jeden z_value ein color value von 0..255 berechnet
            color_values = [ int( dm.reMap(cor_value, cor_min, cor_max, 0, 255) ) for cor_value in cor_values ]   ### smart listing: cor_values werden in integers(0..255) umgerechnet (== "reMap")
            for i in range( len(allPnts) ):
                c_val = color_values[i]
                rs.ObjectColor( allPnts[i], [255-c_val, 127, c_val] )
                if i%100==0: rs.Redraw()

#################
#rs.ZoomExtents()
dm.getTime( 1 )
rs.Command("-zoom extents enter -zoom Factor 0.9 enter", 0) # change zoom for better framing





