############################
### diag @ IAM @ DM2_w25 ###
### hu_03 setUp_ver00    ### see examples @ DM2_w23 hu_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:/dx6419/dm2/")     ### *your* path to DM_lib.py                                            
sys.path.append("P:/WWW/dx6419/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=1)
rs.ViewDisplayMode(mode="Wireframe")
rs.EnableRedraw(0)

dm.PointRadius(displayModeX=0, rad=3, styl=3)                   ### 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

dm.eAA()

### BASIC SETUP / mandatory input:
if 1:
    allInOne = 1
    allCoords = dm.coordsCub (what="F")
    allCoords = dm.coordsCub (edge_len=10, anz_pts=10, what="A", angle=random.uniform(0,360), axe=dm.randVec(-1,1), pos=[0,0,10], allInOne=allInOne)
    
    coordsSph = allCoords[0]
    coordsCub = allCoords[1]



##### check setup - just visualization of coords
##### don't execute  @ final version of homework
if 0:
    rs.AddPoints( coordsSph )
    
    if allInOne:
        rs.AddPoints( coordsCub )
    else:
        for i,coords in enumerate(coordsCub): 
            pts = rs.AddPoints( coords )
##### end check setup

#######################################
################ HERE YOU GO AS YOU GO:

dm.setTime
#rs.AddPoint(0,0,0)
#rs.AddPoint(1,0,0)
#rs.AddPoint(2,0,0)

anzahl = 8
coords = []

if 0:
    for x in range(anzahl):
        #rs.AddPoint( [x,0,0])
        for y in range(anzahl):
            #rs.AddPoint( [x,y,0])
            for z in range(anzahl):
                #rs.AddPoint( [x,y,z] )
                coords.append ( [x,y,z] )

allCubes = []   # Liste fur alle Wurfel-Pointsets

coords = [ [x,y,z-15] for x in range(anzahl) for y in range(anzahl) for z in range(anzahl)]
axe = dm.randVec(-5, 5)
for i,cor in enumerate(coords) :
    cor[0] -=15
    #cor = rs.VectorRotate(cor, 30, axe)
    cor = rs.VectorAdd(cor, dm.randVec(-.2, .2))
    coords [i] = cor

rs.AddPoints( coords )

allCubes.append(coords[:])  #speichert jeden Wurfel separat


coords = [ [x+22.75,y,z+10] for x in range(anzahl) for y in range(anzahl) for z in range(anzahl)]
axe = dm.randVec(5, -5)
for i,cor in enumerate(coords) :
    cor[0] -=15
    #cor = rs.VectorRotate(cor, 30, axe)
    cor = rs.VectorAdd(cor, dm.randVec(-.2, .2))
    coords [i] = cor

rs.AddPoints( coords )

allCubes.append(coords[:])  


coords = [ [x,y,z+10] for x in range(anzahl) for y in range(anzahl) for z in range(anzahl)]
axe = dm.randVec(-5, 5)
for i,cor in enumerate(coords) :
    cor[0] -=15
    #cor = rs.VectorRotate(cor, 30, axe)
    cor = rs.VectorAdd(cor, dm.randVec(-.2, .2))
    coords [i] = cor

rs.AddPoints( coords )

allCubes.append(coords[:])  


coords = [ [x+22.75,y,z-15] for x in range(anzahl) for y in range(anzahl) for z in range(anzahl)]
axe = dm.randVec(-5, 5)
for i,cor in enumerate(coords) :
    cor[0] -=15
    #cor = rs.VectorRotate(cor, 30, axe)
    cor = rs.VectorAdd(cor, dm.randVec(-.2, .2))
    coords [i] = cor

rs.AddPoints( coords )

allCubes.append(coords[:])  


rad = 5
p0 = [rad,0,0]

#rs.AddPoint( p0)
#rs.AddLine( [0,0,0], p0)
#P0 = rs.VectorRotate( p0, 10*1, [0,0,1])
#rs.AddPoint( p0 )
#rs.AddLine( [0,0,0], p0)
#P0 = rs.VectorRotate( p0, 10*2, [0,0,1] )
#rs.AddPoint( p0 )
#rs.AddPoint( [0,0,0], p0)

coords = []
for i in range(555):
    axe = dm.randVec()
    p0 = rs.VectorRotate( p0,random.uniform(0,360), axe )
    #rs.AddLine( [0,0,0],p0)
    coords.append ( p0)
rs.AddPoints(coords)
coordsSph = coords[:]

#rs.AddCurve( coords)
coords = sorted(coords, key=lambda cor: cor[2]) ### that's essential
#crv = rs.AddCurve( coords)
#rs.ObjectColor( crv, [0,0,222])
coords = sorted(coords, key=lambda cor: cor[1]) ### that's essential
crv = rs.AddCurve( coords)
rs.ObjectColor( crv, [22,222,0])

for i in range(100):
    po = random.choice( coordsSph)
    p1 = random.choice( coordsCub)
    #rs.AddLine( p0,p1 )
    
connect_dist_list =[]
for coSpu in coordsSph:
    for cube in allCubes:                   # gehe durch alle vier Wurfel
        for corCu in cube:                  # und durch ihre Punkte
            connect_dist_list.append([[coSpu, corCu], rs.Distance(coSpu, corCu)])

connect_dist_list_total = []     
lines_per_cube = 250             # Anzahl der Linien pro Wurfel
total_lines = lines_per_cube * len(allCubes)

for idx, cube in enumerate(allCubes):
    connect_dist_list = []
    
    for coSpu in coordsSph:
        for corCu in cube:
            connect_dist_list.append([[coSpu, corCu], rs.Distance(coSpu, corCu)])
    
    # nach Distanz sortieren
    connect_dist_list = sorted(connect_dist_list, key=lambda abx: abx[1])
    
    # gleiche Anzahl Linien pro Wurfel zeichnen
    for i in range(lines_per_cube):
        p0, p1 = connect_dist_list[i][0]
        line = rs.AddLine(p0, p1)
        # optional farbig, um Wurfel zu unterscheiden:
        color = [255, 0, 0]
        if idx == 1: color = [0, 255, 0]
        if idx == 2: color = [0, 0, 255]
        if idx == 3: color = [255, 255, 0]
        rs.ObjectColor(line, color)


print("Gesamt gezeichnete Linien:", total_lines)
print "connect_dist_list[10] =", connect_dist_list[10]
connect_dist_list = sorted(connect_dist_list, key=lambda abx: abx[1]) ### that's essential

for i in range(1001):
    p0 = connect_dist_list[i][0][0]
    p1 = connect_dist_list[i][0][1]
    rs.AddLine( p0,p1 )


rs.Command("selDup eter",0)
print "points =", len(rs.ObjectsByType( 1))

dm.getTime( 1 )
#################
#dm.eDup( 1 )
dm.zA( 0.95 ) ### zoom all minus 0.9


