##############################
###  DM2_w23  hu_03_setUp _demo definiton  ###
###  _diag  /  2023 10 27  ###
##############################
import rhinoscriptsyntax as rs
import random, time, sys   ###                                              
sys.path.append("P:/")     ###                                            
sys.path.append("P:/WWW\daveh2\dm2") ### add YOUR path wher YOUR DM_lib.py can be found                                            
import DM_lib as dm        ### reload(dm)    
##############################   

rs.UnitSystem(3)                       
rs.ShowGrid(view=None, show=0)
rs.ShowGridAxes(view=None, show=0)
rs.ViewDisplayMode(view=None, mode="Wireframe")
rs.EnableRedraw(0)
dm.PointRadius(displayModeX=0, rad=3, styl=3)
rs.DeleteObjects(rs.AllObjects())


######################################           
anz = random.choice( range(2**4, 2**8, 4) )
coordsCub = dm.setUp_hu_02( anz, 0)[1]  ### calling def from DM_lib to get *new* set of coords
######################################

#randomVec = [random.uniform(-30,30) for i in range(3)]
#coordsCub = [rs.VectorRotate(cor, randomVec[0], randomVec) for cor in coordsCub]

siz = rs.Distance( coordsCub[0],  coordsCub[1] )
vecX = rs.VectorSubtract( coordsCub[1],  coordsCub[0] )
vecY = rs.VectorSubtract( coordsCub[-1],  coordsCub[0] )
vecZ = dm.normVec3pnts(coordsCub[0], coordsCub[1], coordsCub[-1]) ### unitVector / length = 1.0
vecZ = rs.VectorScale( vecZ, siz )

ptA = coordsCub[0]
ptB = rs.VectorAdd( ptA, rs.VectorScale( vecX, anz/4) )
ptC = rs.VectorAdd( ptB, rs.VectorScale( vecY, anz/4) )
ptD = rs.VectorAdd( ptC, rs.VectorScale( vecX, -anz/4) )

ptE = rs.VectorAdd( ptA, rs.VectorScale( vecZ, anz/4) )
ptF = rs.VectorAdd( ptB, rs.VectorScale( vecZ, anz/4) )
ptG = rs.VectorAdd( ptC, rs.VectorScale( vecZ, anz/4) )
ptH = rs.VectorAdd( ptD, rs.VectorScale( vecZ, anz/4) )

print "*** anz =", anz
print "*** siz =", round(siz, 2), "/ edge lenght =", round(siz, 2),"*",int(anz/4),"=", round(siz*anz/4, 2)
print "*** **********\n"

### just demo ### just demo  >> remove it <<
if 1: 
    baseCrv = rs.AddCurve( [ptA, ptB, ptC, ptD, ptA], 1 ) 
    topCrv  = rs.AddCurve( [ptE, ptF, ptG, ptH, ptE], 1 ) 
    for i,car in enumerate(["A","B","C","D","E","F","G","H"]):
        rs.AddTextDot( car, [ptA, ptB, ptC, ptD,ptE, ptF, ptG, ptH][i] )
if 1:
    rs.ZoomExtents()
    dirX = rs.CurveArrows (rs.AddLine(ptA, ptB), 2)
    dirY = rs.CurveArrows (rs.AddLine(ptA, ptD), 2)
    dirZ = rs.CurveArrows (rs.AddLine(ptA, ptE), 2)
    rs.ObjectColor(rs.AllObjects()[2], [200,20,20] )
    rs.ObjectColor(rs.AllObjects()[1], [20,200,20] )
    rs.ObjectColor(rs.AllObjects()[0], [20,20,200] )

    
    coords = rs.DivideCurve(baseCrv, anz, 0)
    coords += rs.DivideCurve(topCrv, anz, 0)
    rs.DeleteObjects( [baseCrv, topCrv] )
    
    cen = dm.pntInbetween(ptA, ptG)
    ran = random.uniform(4,16)
    for i,cor in enumerate(coords):
        vec = rs.VectorSubtract( cor, cen )
        unitVec = rs.VectorUnitize( vec )
        scalVec = rs.VectorScale( unitVec, siz*anz/ran)
        circPnt = rs.VectorAdd( cen, scalVec )
    
    coords = []
    for i in range(1001):
        scalVec = rs.VectorRotate( scalVec, random.uniform(-90,90), [random.uniform(-1,1) for i in range(3)])
        coords.append( rs.VectorAdd( cen, scalVec ) )
    rs.AddPoints( coords )

### just demo ### just demo  >> remove it <<

### here we go:
    
    
#Hohl David/Gruber 01


#DAS ATOM

#Umliegende Atome
if 1:

    
    
    def makeSphCoords(center = ptA, rad = 2.0, anzahl = 1001):
        sphCoords = []
        for i in range(anzahl):
            x = random.uniform(-1,1)
            y = random.uniform(-1,1)
            z = random.uniform(-1,1)
            
            randVec = [x, y, z] 
            uniVec = rs.VectorUnitize( randVec)
            radVec = rs.VectorScale( uniVec, rad )
            radVec = rs.VectorAdd(radVec, center)
            sphCoords.append( radVec )
        return sphCoords
    
    for ptX in [ptA, ptB, ptE, ptF, ptD,ptH, ptG,ptC,]:
        coords = makeSphCoords( center = ptX, rad = random.uniform(0.5, 0.5), anzahl=1001)
        rs.AddPoints( coords )
#Magnetischer Verlauf inerhalb des Atoms
if 1:
    def makeFaceCoords( p0=ptA, p1=ptB, p2=ptE, anzahl=101):
        coords = []
        for i in range(anzahl):
            vecAB = rs.VectorSubtract(p1,p0)
            uniAB = rs.VectorUnitize( vecAB )
            distAB = rs.Distance( p1, p0 )
            distAE = rs.Distance( p0, p2 )
            randAB = rs.VectorScale(uniAB, random.uniform(0,  distAB) )
            randAE = rs.VectorScale(rs.VectorUnitize( rs.VectorSubtract(p2, p0) ), random.uniform(0,  distAE) )
            randABE = rs.VectorAdd(randAB, randAE)
            ptAB = rs.VectorAdd(p0, randABE)
            coords.append( ptAB )
        return coords
    
    coords0 = makeFaceCoords( p0=ptA, p1=ptD, p2=ptE, anzahl=1001) 
    coords1 = makeFaceCoords( p0=ptC, p1=ptB, p2=ptG, anzahl=1001) 
    coords2 = makeFaceCoords( p0=ptE, p1=ptF, p2=ptH, anzahl=2) 
    
    for i in range(101):
        p0 = random.choice( coords1 )
        p1 = random.choice( coords1 )
        if rs.Distance(p0, p1) < 1:
            rs.AddCurve( [p0, p1] )

   
    pX = rs.AddPoint( dm.pntInbetween( ptD, ptF, 0.5) )
    rs.AddPoint( pX )
    for i in range(101):
        p0 = random.choice( coords0 )
        p1 = random.choice( coords1 )
        if rs.Distance(p0, p1) > 1:
            rs.AddCurve( [p0, random.choice(coords2), p1], 2 )
    
    
    

#Elektronen innerhalb des Atoms
anz = 20 
fac = 1.0
endPt = ptH
curvCoords = []

if 1: 
 kugel_koordinaten = ( (ptA[0] + ptG[0]) / 2 ,
                      (ptA[1] + ptG[1]) / 2 ,
                      (ptA[2] + ptG[2]) / 2
                    )
 radius = 4.0
 
 rs.AddPoint(kugel_koordinaten)
 rs.AddSphere(kugel_koordinaten, radius)

 random_numb = random.randint(70, 100)
 for i in range(random_numb):
    coord_x = kugel_koordinaten[0] + random.randint(-2.0, 2.0)
    coord_y = kugel_koordinaten[1] + random.randint(-2.0, 2.0)
    coord_z = kugel_koordinaten[2] + random.randint(-2.0, 2.0)
    
    random_vec = (coord_x, coord_y, coord_z)
    
    color = rs.AddPoint(random_vec)
    red_color = (255, 0, 50)
    rs.ObjectColor(color, red_color)
if curvCoords:
    rs.AddPoints(curvCoords)



##########################################################################
rs.ZoomExtents()
