############################
### diag @ IAM @ DM2_w25 ###
### hu_02 setUp_ver00    ### see examples @ DM2_w23 hu_02 !
############################


##############################
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=4, styl=0)                   ### displayModeX=0="Wireframe" / rad=size of points / styl=pointStyle (try 0..5) / default 3, 3
dm.printDisplay(state=1, scale=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*32,  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:
len_coordsCir = len(coordsCir)
allCoordsLists = []
anzahl = 8
for i in range( len_coordsCir ):
    lin = rs.AddLine( coordsCir[i], coordsSqu[i])
    coords = rs.DivideCurve( lin, anzahl, 0)            ### DivideCurve(curve_id, segments, create_points=False, return_points=True) 
    allCoordsLists.append( coords )
rs.Redraw()
rs.DeleteObjects( rs.ObjectsByType( 1+4 ) )

##### 1. ring
if 0:
    ringNr = 0
    coords = [ coordsList[ringNr] for coordsList in allCoordsLists ]        ### eine liste mit den jeweils 1.coords jeder allCoordsList
    rs.AddCurve( coords )
    ### fast_version 4 last_ring
    rs.AddCurve( [ coordsList[anzahl] for coordsList in allCoordsLists ], 1)

##### ALLE ringe
if 1:
    rs.DeleteObjects( rs.ObjectsByType( 1+4 ) )
    ### slow version
    allRingCoordsLists = []
    allRingCoordsLists = [ [ coordsList[ringNr] for coordsList in allCoordsLists ] for ringNr in range(anzahl+1) ]          ### +1: weil returns anzahl+1 (start + end !)
    for coords in allRingCoordsLists:
        coords.append( coords[0] )          ### closing the curve
        rs.AddCurve( coords, 1 )
        #rs.Redraw()

    ##### fast_version 4 all rings
    if 1:
        rs.DeleteObjects( rs.ObjectsByType( 1+4 ) )
        rs.Redraw()
        for ringNr in range(anzahl+1):
            rs.CloseCurve( rs.AddCurve( [ coordsList[ringNr] for coordsList in allCoordsLists ], 1), 1001.0 ), rs.DeleteObject( rs.AllObjects()[1] )    ### CloseCurve generates new curve 

col = [222,222,11]

###########################################################################
##### RANDOM_factor - alternative berechnung der coords mittel dm.pntInbetween(..)
if 0: ### 1 ring
    rnd = 0.03      ### value 4 randomitze (entlang der kante !)
    for i in range( len_coordsCir ):
        coord_0 = coordsCir[i]
        coord_1 = coordsSqu[i]
        pnt = dm.pntInbetween(coord_0, coord_1, fac=0.5)    ### 0.5 == default
        rs.AddPoint( pnt )
        ### bissl verwackelt:
        randi = 1.0 + random.uniform( -rnd, rnd)
        if  i < 2: print ("i = "+str(i)+" randi = "+str(randi))
        pnt = dm.pntInbetween(coord_0, coord_1, fac=0.5*randi)    ### 0.5 == default
        rs.ObjectColor( rs.AddPoint( pnt ), col )


if 1:    ######### fuer alle ringe
    ###############
    rnd      = 0.01     ### value 4 randomitze (auf/ab - entlang der kante !)
    anzahl   = anzahl   ### gleiche anzahl wie bei DivideCurve
    anzahl   = 32        ### anzahl ringe 
    interval = 2        ### nur jede i_te coord von coordsCir .. make curves softer
    deg      = 3

    rs.DeleteObjects( rs.ObjectsByType( 1+4 ) )
    allCoordsLists = []
    print "\nfac =",
    for a in range( anzahl + 1 ):
        fac = 1/anzahl*a                                ### "e" wie edge
        print (str(a)+"->"+str(round(fac, 3))+(","*(a<anzahl)*1)), ### wg dem letzten "," 
        coords = []
        for i in range( 0, len_coordsCir, interval ):  ### 
            ### bissl verwackelt:
            randi = random.uniform( -rnd, rnd)  ### muss jeweil neu calculiert werden
            if 0 and not a:
                rs.ObjectColor( rs.AddLine( coordsCir[i], coordsSqu[i] ), [111,111,111] )
            pnt = dm.pntInbetween(coordsCir[i], coordsSqu[i], fac+randi)
            rs.ObjectColor( rs.AddPoint( pnt ), col)
            coords.append( pnt )
        rs.ZoomExtents()
        rs.Redraw()
        allCoordsLists.append(coords[:])    ### [The Python Tutorial | why-did-changing-list-y-also-change-list-x] .. erklaerung folgt
        coords.append(coords[0])
        #rs.AddCurve( coords, deg)            ### ctrl_point_curve
        rs.AddInterpCurve( coords, deg )      ### crv durch points
        #rs.Redraw()
    
    #####
    if 1:   ### randomisierte struktur
        rs.DeleteObjects( rs.ObjectsByColor( col ) )
        anz_levels = anzahl - 1                     ### anzahl levels
        anz_coords = len( allCoordsLists[0] ) - 0   ### anzahl coords pro level
        #dm.textDots(allCoordsLists[0])
        rnd = 2
        max_anzahl = anzahl * anz_coords * ((rnd*2)+1)
        print ("\nanz verticals = "+str( max_anzahl )+" maximal") 
        ctrl_list  = []     ### zum checken ob doppels
        for i in range( int(max_anzahl*0.2) ):
            dm.esc()
            rnd_num = random.randint(-rnd, rnd)
            level = random.randint(0, anz_levels)
            pos_0 = random.randint(0, anz_coords - 1)
            pos_1 = pos_0 + rnd_num
            if 1 and pos_1 < 0:
                pos_1 = anz_coords - 1 - rnd_num
            if 1 and pos_1 > anz_coords-1:
                pos_1 = rnd_num
            if 1 and not [level, pos_0, pos_1] in ctrl_list:
                ctrl_list.append( [level, pos_0, pos_1] )
                p0   = allCoordsLists[ level ][ pos_0 ]
                p1   = allCoordsLists[ level + 1 ][ pos_1 ]
                lin  = rs.AddLine( p0, p1 )
                name = "level_"+str(level)+"_pos_"+str(pos_0)+"/"+str(pos_1)
                rs.ObjectName( lin, name )
                if len(ctrl_list) > max_anzahl:
                    "*** brrrrrr..."
                    break
    print "len verticals = "+str(len(rs.ObjectsByName("level_*")))
    ##### JOINED & smoothed
    if 1:
        joined = rs.JoinCurves(rs.ObjectsByName("level_*"), delete_input=1)
        print "len   joined = "+str(len(joined)) 
        for i,crv in enumerate(joined):
            dm.esc()
            rs.Command("_ChangeDegree selID "+str(crv)+" enter Deformable=Yes 2 enter", 0)
            rs.Redraw()

###
len_edge_lines = len( rs.ObjectsByName("level_*") )
rs.Command("_selDup enter", 0)
rs.Sleep(1000), rs.DeleteObjects( rs.ObjectsByColor( col ) )






#################
#rs.ZoomExtents()
rs.Command("-zoom extents enter -zoom Factor 0.95 enter", 0) # change zoom for better framing





