#####################################
### DM2_w22  #  AGruber@tugraz.at ###
### hu_04_startUP # infra_4_tetra ###
#####################################

##############################
import rhinoscriptsyntax as rs      ###
import random, time, sys            ###
sys.path.append("P:/")              ### add LW P:/ to rhino's default search path AND
sys.path.append("P:/WWW/default114/dm2")     ### add YOUR LW P:/WWW/user/ to rhino's default search path ...
import DM2_lib as dm                ### ... thus DM2_lib.py can be found !
##############################


rs.UnitSystem(4)
rs.ShowGrid(None, 0)
rs.ShowGridAxes(None, 0)
rs.ViewDisplayMode(view=None, mode="Ghosted")
dm.eA()                    ### same as: rs.DeleteObjects( rs.AllObjects() )

rs.EnableRedraw(0)
rs.Redraw()

if 1:
    print "        /\\        "
    print "       / |\\       "
    print " TETR /__|_\\ EDER  h=a/3.0*(6.0**0.5) "
    print "_____/   |  \\______vulgo: \"hoehe ist gleich a durch 3 - mal wurzel aus 6\"\n"


pointsX = [[0, 3, 0], [3, 0, 0],[5, 3, 0] ]
if 0:
    ptA = dm.pntRandVec(-3, 3)
    ptB = dm.pntRandVec(-3, 3)
    ptX = dm.pntRandVec(-3, 3)
    dm.pointsX = [ptA, ptB, ptX]  ### check in another TAB !
    pointsX = [ptA, ptB, ptX]

tetraCoords = dm.getTetraCoords( points=pointsX, flipH=0, flipAB=0, drawTetra=0, drawAll=0 )
#tetraCoords = dm.getTetraCoords( points=pointsX, flipH=0, flipAB=1, drawTetra=0, drawAll=0 )
#tetraCoords = dm.getTetraCoords( tetraCoords[0], flipH=0, flipAB=1, drawTetra=1, drawAll=0 )
print tetraCoords[0]

if 0: ### check & test & visualize
    for i, FaceCoordList in enumerate(tetraCoords):
        print "face", i, 
        print FaceCoordList
    print
    for i, cor in enumerate(tetraCoords[0]):
        print "cor", i, 
        print cor

    #rs.DeleteObjects( rs.ObjectsByType( 1 + 8192 ) )
    #dm.eA()
    rs.AddSrfPt(tetraCoords[2])         ### ! achtung! boeze surface !
    rs.AddPoints( tetraCoords[1] )      ### 
    rs.ObjectColor( rs.AllObjects()[:4], [0,200,200] )

rs.ZoomExtents()

#_____________________________here you go:
#_________________________________________

## asigning variables to verteces of main tetra
a = [0, 3, 0]
b = [3, 0, 0]
c = [4.098076211353316, 4.098076211353316, 0.0]
d = tetraCoords[1][2]
print d

## define other surfaces of main tetra
pointsA = [a,b,d]
pointsB = [b,c,d]
pointsC = [a,c,d]

## place new tetraeders on other surfaces
tetraCoords_1 = dm.getTetraCoords( points=pointsX, flipH=0, flipAB=1, drawTetra=0, drawAll=0 )
tetraCoords_2 = dm.getTetraCoords( points=pointsA, flipH=0, flipAB=0, drawTetra=0, drawAll=0 )
#rs.AddSrfPt( tetraCoords_2[1] )
tetraCoords_3 = dm.getTetraCoords( points=pointsB, flipH=0, flipAB=0, drawTetra=0, drawAll=0 )
#rs.AddSrfPt( tetraCoords_3[1] )
tetraCoords_4 = dm.getTetraCoords( points=pointsC, flipH=0, flipAB=1, drawTetra=0, drawAll=0 )
#rs.AddSrfPt( tetraCoords_4[1] )

##createvector for smaller tetras
vec_01 = rs.VectorCreate(b,a)
vec_02 = rs.VectorCreate(c,a)
vec_03 = rs.VectorCreate(d,a)
vec_04 = rs.VectorCreate(c,b)
vec_05 = rs.VectorCreate(d,b)
vec_06 = rs.VectorCreate(d,c)

##define arrays
smallTetra01 = []
smallTetra02 = []
smallTetra03 = []
smallTetra04 = []

##define vectors for each tetra
vecs1 = [vec_01,vec_02,vec_04]
vecs2 = [-vec_01,vec_04,vec_02]
vecs3 = [-vec_04,-vec_02,vec_01]
vecs4 = [-vec_03,-vec_05]

##define vector len and add to source point
for Vector in vecs1:
    Vector = Vector*0.5
    cor = rs.VectorAdd(a,Vector)
    smallTetra01.append(cor)

for Vector in vecs2:
    Vector = Vector*0.5
    cor = rs.VectorAdd(b,Vector)
    smallTetra02.append(cor)

for Vector in vecs3:
    Vector = Vector*0.5
    cor = rs.VectorAdd(c,Vector)
    smallTetra03.append(cor)

for Vector in vecs4:
    Vector = Vector*0.5
    cor = rs.VectorAdd(d,Vector)
    smallTetra04.append(cor)
smallTetra04.append([2.3660254037844388, 2.3660254037844388, 0.5*3.4641016151377544])

## draw small tetras
tetraCoords_small_01 = dm.getTetraCoords( points=smallTetra01, flipH=0, flipAB=0, drawTetra=0, drawAll=0 )
tetraCoords_small_02 = dm.getTetraCoords( points=smallTetra02, flipH=0, flipAB=1, drawTetra=0, drawAll=0 )
tetraCoords_small_03 = dm.getTetraCoords( points=smallTetra03, flipH=0, flipAB=1, drawTetra=0, drawAll=0 )


##put Tetras in lists
Tetras_big = [tetraCoords,tetraCoords_1,tetraCoords_2,tetraCoords_3,tetraCoords_4]
Tetras_small = [tetraCoords_small_01,tetraCoords_small_02,tetraCoords_small_03]

##draw curves 
big_left = []
big_right = []

small_left = []
small_right = []

for i in range(3):
    left = rs.AddCurve([Tetras_big[1][1][2],Tetras_big[i+2][0][0],Tetras_big[i+2][1][2]],2)
    big_left.append(left)
    right = rs.AddCurve([Tetras_big[1][1][2],Tetras_big[i+2][0][1],Tetras_big[i+2][1][2]],2)
    big_right.append(right)
    
    sleft = rs.AddCurve([Tetras_big[1][1][2],Tetras_small[i][0][1],Tetras_small[i][1][2]],2)
    small_left.append(sleft)
    sright = rs.AddCurve([Tetras_big[1][1][2],Tetras_small[i][0][0],Tetras_small[i][1][2]],2)
    small_right.append(sright)

##split curves equally and create tangents
anz = 10
coords = []
crvs = [big_left,big_right,small_left,small_right]
threads = []
for b in crvs:
    for a in range(3):
        ### curve DOMAIN
        dom = rs.CurveDomain(b[a])
        #print "*** domain : start =", dom[0], ".. end =", dom[1]
        
        for i in range(anz+1):
                pntX = rs.EvaluateCurve( b[a], dom[1]/8*i)
                pntX = rs.EvaluateCurve( b[a], dom[1]*random.uniform(0.2, 0.8))
                coords.append(pntX)
        
        ### curve TANGENT
        for i in range(anz):
            pntX = rs.EvaluateCurve( b[a], dom[1]/anz*i)
            tVec = rs.CurveTangent( b[a], dom[1]/anz*i ) 
            threads.append(rs.AddLine(pntX, pntX+(tVec*2)))

print coords[0][2]
print coords[-1][2]

##sort points and tangents along z-axis
def curvekey(threads):
    point = rs.CurveStartPoint(threads)
    return point[2]

if 1:
    coords = sorted(coords, key=lambda vecZ: (vecZ[2]) )
    threads = sorted(threads, key=curvekey)

    print round( coords[0][2], 2)
    print round(coords[-1][2], 2)

##color tangents
if 1:
    minZ = coords[0][2]
    maxZ = coords[-1][2]

    print len(coords)
    print "crvs", len(rs.ObjectsByType(4))
    for i in range(len(threads)):
        pass
       # cor = rs.PointCoordinates(pnt)
        zVal = coords[i][2]#cor[2]
        col = abs(int( dm.reMap(zVal, minZ, maxZ, 0, 255)) )
        
        #if i % 50 ==0:
        print i, col
        rs.ObjectColor(threads[i], [150,255-col,100] )

#_________________________________________
#____________________________:here you end


###############################################
#___4_lazy_experts: automated capture to LW P:/
if 0:
    rs.ZoomExtents()
    rs.Command("-zoom Factor 0.95 enter", 0) # decrease zoom for better framing
    rs.Redraw()
    if  1:
        path = "P:/ue_04/images/"
        name = "tst.png"
        rs.Command("-viewCaptureToFile Width=480  Height=360  Scale=1.5 DrawCPlaneAxes=No TransparentBackground=Yes "+path+name, 0)
        # .jpg and Scale=4 for webSite submission
        name = "tst_"+str(int(time.time()))+".jpg"
        rs.Command("-viewCaptureToFile Width=480  Height=360  Scale=4 TransparentBackground=No  "+path+name, 0)
