###################################
### DM2_w23 # AGruber@tugraz.at ###
### finale CC_100 / timon.T.    ###
### project atom 2 screen       ###
###################################


##############################
import rhinoscriptsyntax as rs
import random, time, sys   ###
sys.path.append("/Users/Titi/Desktop/DM2 End:/")     ###
sys.path.append("P:/")     ###
import DM_lib as dm        ### reload( dm )
import davidCoordsList as davi
##############################      

##################
rs.UnitSystem(4)                                        # km = 5, meters = 4, cm = 3 etc
rs.ShowGrid(None, 0)                                    # grid > 0 = off
rs.ShowGridAxes(None, 0)                                # y/y/z axen display > 0/1 = off/on
rs.ViewDisplayMode(rs.CurrentView(), "wireframe")
rs.Command("cplane w t enter", 0)						# cPlane World Top
dm.PointRadius(displayModeX=0, rad=1, styl=3)
dm.PointRadius(displayModeX=1, rad=1, styl=1)
dm.printDisplay(0)                                      # nomen est omen
rs.EnableRedraw(0)
##################


dm.newEmptyLayer("PROJECT")
davidCoords = davi.davidCoords

#davidCoords = [ rs.VectorAdd(cor, [0,0, 0.3 ]) for cor in davidCoords]

print "len(davidCoords)", len(davidCoords)
#rs.AddPoints( davidCoords )

def makeAtomKern (position=[1,2,3], radius=0.1, dichte=32):
    arcs = []
    for i in range(dichte):
        pX = [ random.uniform(-1001,1001) for i in range(3) ]
        pY = [ random.uniform(-1001,1001) for i in range(3) ]
        planeX = rs.PlaneFromPoints(position, pX, pY)
        arc = rs.AddArc(planeX, radius, 180.0)
        arcs.append( arc )
    return arcs





screen1 = rs.ObjectsByName("Surface")[0]

dm.newEmptyLayer("PROJ::points", [0,0,200] )
dm.newEmptyLayer("PROJ::surface", [0 ,0,200] )

coordssurface = dm.getSurfacePoints(screen1)
#rs.AddPoints( coordssurface )
#dm.textDots(coordssurface)
center = dm.pntInbetween(coordssurface[0], coordssurface[3])
print "center", center[0]


coords = [ dm.pnt2cor( cor  ) for cor in coordssurface ]

#print coordssurface

#######
scalFac = [2.5, 5, 2.5]
scalMov = -1.5

if 1:
    random.shuffle(davidCoords)
    arcs = []
    for i,cor in enumerate( davidCoords[0:804] ):
        arcs.extend(makeAtomKern (position=cor, radius=0.05, dichte=1))
        if i%50==0: rs.Redraw()
    projected = rs.ProjectCurveToSurface(arcs, [screen1], [1,0,0])
    center[2] += scalMov

    rs.ScaleObjects(projected, center, scalFac )
    rs.ObjectColor(projected, [0,0,200])
    rs.HideObject(projected)
    dm.zA()
####################
anzahl = 12
fac = 1/anzahl
####################
dm.setTime()
lenx = len(arcs)
screenDist = center[0]


crvList = []
pntList = []
if 1:
    if 1:
        print "len(arcs)", len(arcs)
        print "len(projected)", len(projected)
        for j in range(0,anzahl+1):
            dm.esc()
            for i,arc in enumerate(arcs):
                if i%20==0:
                    pass
                    #print "arc_"+str(i),"von",lenx,"--",  dm.getTime(), "secs"
                    #rs.Redraw()
                dm.esc()
                coordsA1 = rs.CurveEditPoints( arcs[i] )
                coordsP1 = rs.CurveEditPoints( projected[i] )
                #rs.AddLine( coordsA1[0],  coordsP1[0] )

                pX = dm.pntInbetween(coordsA1[0], coordsP1[0], fac*j)
                #rs.AddPoint( pX )
                cop = rs.CopyObject( arcs[i], rs.VectorSubtract(pX, coordsA1[0] ) )
                coords = rs.CurvePoints( cop )
                if j==anzahl: ### alle coords auf screen projiziert
                    coords = [ [screenDist*1, cor[1], cor[2]] for cor in coords ] ## die letzten punkte auf screen 

                p0 = dm.pntInbetween( coords[0], coords[1], fac*j)
                p5 = dm.pntInbetween( coords[4], coords[3], fac*j)
                rs.DeleteObject( cop )
                coords[0] = p0
                coords[4] = p5
                crv = rs.AddCurve( coords, 2 )
                rs.ObjectColor( crv, [20,220,200] )
                rs.ObjectName( crv, "zwischenCrv")
                crvList.append( crv )
                ##################################################
                ### points, zunehmend mit j, also in richtg screen
                if random.uniform(0, anzahl) < j:
                    #if j: print anzahl/j
                    ###cors = random.choice( coords )
                    numX = int(dm.reMap(j, 0, anzahl, 1, anzahl-2))
                    cors = coords[0:numX-1]
                    cors = coords[0:numX-1]
                    lenx =len(cors)
                    if lenx:
                        cors = rs.CullDuplicatePoints( cors )
                        #print "lenx=", lenx, ">>", len(cors)
                        pnts = rs.AddPoints( cors )
                        rs.ObjectColor(pnts, [anzahl*(j+2), anzahl*(j+2)*0, anzahl*(j+2)*0])
                        pntList.extend( pnts )
                        rs.ObjectName( pnts, "zwischenPnt")
            
            
            rs.Redraw()
            lenPts = len(pntList)
            rs.DeleteObjects( pntList[ 0:int(len(pntList)/2)] )
            rs.DeleteObjects( crvList[ 0:int(len(crvList)/2)] )
            #crvList = crvList[ 0:int(len(crvList)/2)]
            rs.Redraw()

crvList = rs.ObjectsByName("zwischenCrv")[0:len(arcs)]
pntList = rs.ObjectsByName("zwischenPnt")[0:len(arcs)*3]
rs.SelectObjects( crvList )
rs.SelectObjects( pntList )

allObjs = rs.ObjectsByName("zwischenCrv")
allObjs.extend( rs.ObjectsByName("zwischenPnt") )


allObjs.reverse()

for i, obj in enumerate(allObjs):
    if obj not in crvList and obj not in pntList:
        rs.DeleteObject( obj )
        if i%100==0:
            rs.Redraw()
    #rs.DeleteObjects( rs.ObjectsByName("zwischenPnt")[len(arcs)*3:] )


#rs.DeleteObjects( projected )


if 0:
    print "len(arcs)", len(arcs)#, "len(arcs[0])", len(arcs[0]), "\n" 
    for i,arc in enumerate(arcs):
        #lines = rs.ProjectCurveToSurface(arcs, [screen], [1,0,0])
        pts = rs.ProjectPointToSurface( [rs.CurveStartPoint(arc), rs.CurveEndPoint(arc)], [screen1], [1,0,0])
        pts = [ rs.VectorAdd(pX, [ random.uniform(-1,1) for j in range(3) ]) for pX in pts ] ### bissl ungenauigkeit !
        projected = rs.AddCurve( pts )
        rand = random.uniform(0.1, 0.9)
        rs.ObjectColor( projected, [255*rand, 255, 255*rand])
        rs.ObjectPrintWidth( projected, rand)
    if i%20==0:
        rs.Redraw()
dm.zA()
#dm.eDup(1)
dm.printDisplay(1)                                     
rs.EnableRedraw(1)